source: trunk/src/calc_field_interp.m @ 1189

Last change on this file since 1189 was 1187, checked in by sommeria, 12 days ago

mask accessory updated

File size: 8.1 KB
Line 
1%'calc_field_interp': calculate fields (velocity, vort, div...) using linear interpolation if requested
2%---------------------------------------------------------------------
3% [VarVal,ListVarName,VarAttribute,errormsg]=calc_field_interp(Coord,Data,FieldName,XI,YI)
4%
5% OUTPUT:
6% VarVal: array giving the values of the calculated field
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
9%
10% INPUT:
11% Coord(nbpoints,2): matrix of x,y coordinates of the input data points
12% Data: inputfield structure
13% FieldName: string representing the field to calculate, or cell array of fields (as displayed in uvmat/FieldName)
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)
16
17%=======================================================================
18% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
19%   http://www.legi.grenoble-inp.fr
20%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
21%
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
31%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theerrormsg
32%     GNU General Public License (see LICENSE.txt) for more details.
33%=======================================================================
34
35function [VarVal,ListVarName,VarAttribute,errormsg]=calc_field_interp(Coord,Data,FieldName,XI,YI)
36
37%% initialization
38VarVal={};
39ListVarName={};
40VarAttribute={};
41InputVarList={};
42if ischar(FieldName),FieldName={FieldName};end
43if isempty(FieldName{1})
44    errormsg='no input field name entered';
45    return
46else
47    errormsg='';
48end
49check_skipped=zeros(size(FieldName));% default, =1 to mark the variables which can be calculated
50check_interp=ones(size(FieldName));% default, =1 to mark the variables which can be interpolated (not ancillary)
51Operator=cell(size(FieldName));
52
53%% analyse the list of input fields: needed variables and requested operations
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
57    if isempty(r) % no operator: the field name is a variable itself
58        ivar=find(strcmp(FieldName{ilist},Data.ListVarName));
59        if isempty(ivar)% the requested variable does not exist
60            check_skipped(ilist)=1; %variable not found
61        elseif isempty(find(strcmp(FieldName{ilist},InputVarList), 1))% the variable exists and has not been already selected
62            if exist('XI','var')&& isfield(Data.VarAttribute{ivar},'Role') &&...
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
68            end
69        end
70    else
71        if ~isfield(Data,r.UName)||~isfield(Data,r.VName)%needed input variable not found
72            check_skipped(ilist)=1;
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
77                    if ~isfield(Data,'DjUi'), errormsg='field DjUi needed to get curl through linear interp: use ProjMode=interp_tps'; return; end
78                    UName{ilist}='vort';
79                    Data.vort=Data.DjUi(:,1,2)-Data.DjUi(:,2,1);
80                case 'div'
81                    if ~isfield(Data,'DjUi'), errormsg='field DjUi needed to get div through linear interp: use ProjMode=interp_tps'; return; end
82                    UName{ilist}='div';
83                    Data.div=Data.DjUi(:,1,1)+Data.DjUi(:,2,2);
84                case 'strain'
85                    if ~isfield(Data,'DjUi'), errormsg='field DjUi needed to get strain through linear interp: use ProjMode=interp_tps'; return; end
86                    UName{ilist}='strain';
87                    Data.strain=Data.DjUi(:,1,2)+Data.DjUi(:,2,1);
88            end
89            InputVarList=[InputVarList UName{ilist}]; %the variable is added to the list if iTriScatteredInterpt is not already in the list
90        else % case 'norm' for instance
91            UName{ilist}=r.UName;
92            VName{ilist}=r.VName;
93            if isempty(find(strcmp(r.UName,InputVarList)))
94                InputVarList=[InputVarList UName{ilist}]; %the variable is added to the list if it is not already in the list
95            end
96            if isempty(find(strcmp(r.VName,InputVarList), 1))
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;
100        end
101    end
102end
103
104%% create interpolator for each variable to interpolate
105if exist('XI','var')
106    for ilist=1:numel(InputVarList)
107        F.(InputVarList{ilist})=TriScatteredInterp(Coord,Data.(InputVarList{ilist}),'linear');
108    end
109end
110
111%% perform the linear interpolation for the requested variables
112for ilist=1:numel(FieldName)
113    if ~check_skipped(ilist)
114        nbvar=numel(ListVarName);
115        switch Operator{ilist}
116            case 'vec'
117                if exist('XI','var')
118                    if check_interp(ilist)
119                    VarVal{nbvar+1}=F.(UName{ilist})(XI,YI);
120                    VarVal{nbvar+2}=F.(VName{ilist})(XI,YI);
121                    end
122                else
123                    VarVal{nbvar+1}=Data.(UName{ilist});
124                    VarVal{nbvar+2}=Data.(VName{ilist});
125                end
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')
132                    if check_interp(ilist)
133                    U2=F.(UName{ilist})(XI,YI).*F.(UName{ilist})(XI,YI);
134                    V2=F.(VName{ilist})(XI,YI).*F.(VName{ilist})(XI,YI);
135                    end
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';
142                VarAttribute{nbvar+1}.Role='scalar';
143            case {'curl','div','strain'}
144                if exist('XI','var')
145                    if check_interp(ilist)
146                    VarVal{nbvar+1}=F.(UName{ilist})(XI,YI);
147                    end
148                else
149                    VarVal{nbvar+1}=Data.(UName{ilist});
150                end
151                ListVarName{nbvar+1}=UName{ilist};
152                VarAttribute{nbvar+1}.Role='scalar';
153            otherwise
154                if ~isempty(FieldName{ilist})
155                    if exist('XI','var')
156                        if check_interp(ilist)
157                        VarVal{nbvar+1}=F.(FieldName{ilist})(XI,YI);
158                        end
159                    else
160                        VarVal{nbvar+1}= Data.(FieldName{ilist});
161                    end
162                    ListVarName{nbvar+1}=FieldName{ilist};
163                    VarAttribute{nbvar+1}.Role='scalar';
164                end
165        end
166    end
167end
168
169
170
171
172
173
Note: See TracBrowser for help on using the repository browser.