source: trunk/src/calc_field_interp.m @ 1191

Last change on this file since 1191 was 1187, checked in by sommeria, 3 months ago

mask accessory updated

File size: 8.1 KB
RevLine 
[654]1%'calc_field_interp': calculate fields (velocity, vort, div...) using linear interpolation if requested
[515]2%---------------------------------------------------------------------
[579]3% [VarVal,ListVarName,VarAttribute,errormsg]=calc_field_interp(Coord,Data,FieldName,XI,YI)
[515]4%
5% OUTPUT:
[575]6% VarVal: array giving the values of the calculated field
[579]7% ListVarName: corresponding list of variable names
8% VarAttribute: corresponding list of variable attributes, each term #ilist is of the form VarAttribute{ilist}.tag=value
[515]9%
10% INPUT:
[654]11% Coord(nbpoints,2): matrix of x,y coordinates of the input data points
[579]12% Data: inputfield structure
13% FieldName: string representing the field to calculate, or cell array of fields (as displayed in uvmat/FieldName)
[654]14% XI, YI: set of x and y coordinates where the fields need to be linearly interpolated,
15%        if XI, YI are missing, there is no interpolation (case of colors in vector plots)
[809]16
17%=======================================================================
[1126]18% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
[809]19%   http://www.legi.grenoble-inp.fr
[1127]20%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
[654]21%
[809]22%     This file is part of the toolbox UVMAT.
23%
24%     UVMAT is free software; you can redistribute it and/or modify
25%     it under the terms of the GNU General Public License as published
26%     by the Free Software Foundation; either version 2 of the license,
27%     or (at your option) any later version.
28%
29%     UVMAT is distributed in the hope that it will be useful,
30%     but WITHOUT ANY WARRANTY; without even the implied warranty of
[1187]31%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theerrormsg
[809]32%     GNU General Public License (see LICENSE.txt) for more details.
33%=======================================================================
34
[579]35function [VarVal,ListVarName,VarAttribute,errormsg]=calc_field_interp(Coord,Data,FieldName,XI,YI)
[515]36
[644]37%% initialization
[579]38VarVal={};
[515]39ListVarName={};
40VarAttribute={};
[517]41InputVarList={};
[579]42if ischar(FieldName),FieldName={FieldName};end
[1187]43if isempty(FieldName{1})
44    errormsg='no input field name entered';
45    return
46else
47    errormsg='';
48end
[579]49check_skipped=zeros(size(FieldName));% default, =1 to mark the variables which can be calculated
[654]50check_interp=ones(size(FieldName));% default, =1 to mark the variables which can be interpolated (not ancillary)
[579]51Operator=cell(size(FieldName));
[644]52
53%% analyse the list of input fields: needed variables and requested operations
[579]54for ilist=1:numel(FieldName)
55    Operator{ilist}='';%default empty operator (vec, norm,...)
56    r=regexp(FieldName{ilist},'(?<Operator>(^vec|^norm|^curl|^div|^strain))\((?<UName>.+),(?<VName>.+)\)$','names');% analyse the field name
[863]57    if isempty(r) % no operator: the field name is a variable itself
[650]58        ivar=find(strcmp(FieldName{ilist},Data.ListVarName));
[863]59        if isempty(ivar)% the requested variable does not exist
[579]60            check_skipped(ilist)=1; %variable not found
[1078]61        elseif isempty(find(strcmp(FieldName{ilist},InputVarList), 1))% the variable exists and has not been already selected
[864]62            if exist('XI','var')&& isfield(Data.VarAttribute{ivar},'Role') &&...
[863]63                    (strcmp(Data.VarAttribute{ivar}.Role,'ancillary')||strcmp(Data.VarAttribute{ivar}.Role,'warnflag')||strcmp(Data.VarAttribute{ivar}.Role,'errorflag'))
64                check_interp(ilist)=0; % ancillary variable, not interpolated ?????
65                check_skipped(ilist)=1; %variable not used
66            else
67                InputVarList=[InputVarList FieldName{ilist}];% the variable is added to the list of input variables
[521]68            end
[517]69        end
70    else
[579]71        if ~isfield(Data,r.UName)||~isfield(Data,r.VName)%needed input variable not found
[521]72            check_skipped(ilist)=1;
[579]73        elseif strcmp(r.Operator,'curl')||strcmp(r.Operator,'div')||strcmp(r.Operator,'strain')
74            Operator{ilist}=r.Operator;
75            switch r.Operator
76                case 'curl'% case of CivX data format
[675]77                    if ~isfield(Data,'DjUi'), errormsg='field DjUi needed to get curl through linear interp: use ProjMode=interp_tps'; return; end
[579]78                    UName{ilist}='vort';
79                    Data.vort=Data.DjUi(:,1,2)-Data.DjUi(:,2,1);
80                case 'div'
[675]81                    if ~isfield(Data,'DjUi'), errormsg='field DjUi needed to get div through linear interp: use ProjMode=interp_tps'; return; end
[579]82                    UName{ilist}='div';
83                    Data.div=Data.DjUi(:,1,1)+Data.DjUi(:,2,2);
84                case 'strain'
[675]85                    if ~isfield(Data,'DjUi'), errormsg='field DjUi needed to get strain through linear interp: use ProjMode=interp_tps'; return; end
[579]86                    UName{ilist}='strain';
87                    Data.strain=Data.DjUi(:,1,2)+Data.DjUi(:,2,1);
88            end
[1002]89            InputVarList=[InputVarList UName{ilist}]; %the variable is added to the list if iTriScatteredInterpt is not already in the list
[988]90        else % case 'norm' for instance
[521]91            UName{ilist}=r.UName;
92            VName{ilist}=r.VName;
[1078]93            if isempty(find(strcmp(r.UName,InputVarList)))
[521]94                InputVarList=[InputVarList UName{ilist}]; %the variable is added to the list if it is not already in the list
95            end
[1078]96            if isempty(find(strcmp(r.VName,InputVarList), 1))
[521]97                InputVarList=[InputVarList VName{ilist}]; %the variable is added to the list if it is not already in the list
98            end
99            Operator{ilist}=r.Operator;
[517]100        end
[515]101    end
102end
[644]103
104%% create interpolator for each variable to interpolate
[517]105if exist('XI','var')
106    for ilist=1:numel(InputVarList)
107        F.(InputVarList{ilist})=TriScatteredInterp(Coord,Data.(InputVarList{ilist}),'linear');
108    end
[515]109end
[644]110
111%% perform the linear interpolation for the requested variables
[579]112for ilist=1:numel(FieldName)
[521]113    if ~check_skipped(ilist)
[533]114        nbvar=numel(ListVarName);
115        switch Operator{ilist}
116            case 'vec'
[517]117                if exist('XI','var')
[667]118                    if check_interp(ilist)
[533]119                    VarVal{nbvar+1}=F.(UName{ilist})(XI,YI);
120                    VarVal{nbvar+2}=F.(VName{ilist})(XI,YI);
[654]121                    end
[517]122                else
[533]123                    VarVal{nbvar+1}=Data.(UName{ilist});
124                    VarVal{nbvar+2}=Data.(VName{ilist});
[517]125                end
[533]126                ListVarName{nbvar+1}=UName{ilist};
127                ListVarName{nbvar+2}=VName{ilist};
128                VarAttribute{nbvar+1}.Role='vector_x';
129                VarAttribute{nbvar+2}.Role='vector_y';
130            case 'norm'
131                if exist('XI','var')
[667]132                    if check_interp(ilist)
[533]133                    U2=F.(UName{ilist})(XI,YI).*F.(UName{ilist})(XI,YI);
134                    V2=F.(VName{ilist})(XI,YI).*F.(VName{ilist})(XI,YI);
[654]135                    end
[533]136                else
137                    U2=Data.(UName{ilist}).*Data.(UName{ilist});
138                    V2=Data.(VName{ilist}).*Data.(VName{ilist});
139                end
140                VarVal{nbvar+1}=sqrt(U2+V2);
141                ListVarName{nbvar+1}='norm';
[517]142                VarAttribute{nbvar+1}.Role='scalar';
[579]143            case {'curl','div','strain'}
144                if exist('XI','var')
[667]145                    if check_interp(ilist)
[579]146                    VarVal{nbvar+1}=F.(UName{ilist})(XI,YI);
[654]147                    end
[579]148                else
149                    VarVal{nbvar+1}=Data.(UName{ilist});
150                end
151                ListVarName{nbvar+1}=UName{ilist};
152                VarAttribute{nbvar+1}.Role='scalar';
[533]153            otherwise
[579]154                if ~isempty(FieldName{ilist})
[533]155                    if exist('XI','var')
[667]156                        if check_interp(ilist)
[579]157                        VarVal{nbvar+1}=F.(FieldName{ilist})(XI,YI);
[654]158                        end
[533]159                    else
[579]160                        VarVal{nbvar+1}= Data.(FieldName{ilist});
[533]161                    end
[579]162                    ListVarName{nbvar+1}=FieldName{ilist};
[533]163                    VarAttribute{nbvar+1}.Role='scalar';
164                end
165        end
[515]166    end
167end
[644]168
[515]169
170
171
172
173
Note: See TracBrowser for help on using the repository browser.