/[MITgcm]/MITgcm_contrib/gael/matlab_class/gcmfaces_smooth/diffsmooth2D_extrap_inv.m
ViewVC logotype

Annotation of /MITgcm_contrib/gael/matlab_class/gcmfaces_smooth/diffsmooth2D_extrap_inv.m

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (hide annotations) (download)
Fri Apr 30 05:17:12 2010 UTC (15 years, 2 months ago) by gforget
Branch: MAIN
Changes since 1.1: +8 -1 lines
allow using a pre-computed operator. Note: this should
be used with caution, to speed up some computational loops.

1 gforget 1.2 function [FLD]=diffsmooth2D(fld,mskOut,varargin);
2 gforget 1.1
3     %object: implementation (gforget@mit.edu) of a diffusive smoother (Weaver and Courtier 2001)
4    
5     %input: fld field to be smoothed (masked with NaN)
6     % dxCsm,dyCsm scale in first/second direction
7     %output:FLD smoothed field
8    
9     %asumption: dxCsm/dyCsm are assumed to be given at the positions of U/V points
10    
11     global mygrid;
12    
13     dxC=mygrid.DXC; dyC=mygrid.DYC;
14     dxG=mygrid.DXG; dyG=mygrid.DYG;
15     rA=mygrid.RAC;
16    
17     dxCsm=dxC; dyCsm=dyC;
18     mskFreeze=fld; mskFreeze(find(~isnan(mskFreeze)))=0; mskFreeze(find(isnan(mskFreeze)))=1;
19    
20     %check for domain edge points where no exchange is possible:
21     tmp1=mskOut; tmp1(:)=1; tmp2=exch_T_N(tmp1);
22     for iF=1:mskOut.nFaces;
23     tmp3=mskOut{iF}; tmp4=tmp2{iF};
24     tmp4=tmp4(2:end-1,1:end-2)+tmp4(2:end-1,3:end)+tmp4(1:end-2,2:end-1)+tmp4(3:end,2:end-1);
25     if ~isempty(find(isnan(tmp4)&~isnan(tmp3))); fprintf('warning: mask was modified\n'); end;
26     tmp3(isnan(tmp4))=NaN; mskOut{iF}=tmp3;
27     end;
28    
29     %put 0 first guess if needed and switch land mask:
30     fld(find(isnan(fld)))=0; fld=fld.*mskOut;
31    
32     %scale the diffusive operator:
33     tmp0=dxCsm./dxC; tmp0(isnan(mskOut))=NaN; tmp00=nanmax(tmp0);
34     tmp0=dyCsm./dyC; tmp0(isnan(mskOut))=NaN; tmp00=max([tmp00 nanmax(tmp0)]);
35     smooth2D_nbt=tmp00;
36     smooth2D_nbt=ceil(1.1*2*smooth2D_nbt^2);
37    
38     smooth2D_dt=1;
39     smooth2D_T=smooth2D_nbt*smooth2D_dt;
40     smooth2D_Kux=dxCsm.*dxCsm/smooth2D_T/2;
41     smooth2D_Kvy=dyCsm.*dyCsm/smooth2D_T/2;
42    
43     %form matrix problem:
44     tmp1=convert2array(mskOut);
45     kk=find(~isnan(tmp1));
46     KK=tmp1; KK(kk)=kk; KK=convert2array(KK,fld);
47     nn=length(kk);
48     NN=tmp1; NN(kk)=[1:nn]; %NN=convert2array(NN,fld);
49    
50 gforget 1.2 if nargin==3; doFormMatrix=varargin{1}; else; doFormMatrix=1; end;
51    
52     global dFLDdt_op;
53     if doFormMatrix==1;
54    
55 gforget 1.1 dFLDdt_op=sparse([],[],[],nn,nn,nn*5);
56    
57     for iF=1:fld.nFaces; for ii=1:3; for jj=1:3;
58     FLDones=fld; FLDones(find(~isnan(fld)))=0;
59     FLDones{iF}(ii:3:end,jj:3:end)=1;
60     FLDones(find(isnan(fld)))=NaN;
61    
62     FLDkkFROMtmp=fld; FLDkkFROMtmp(find(~isnan(fld)))=0;
63     FLDkkFROMtmp{iF}(ii:3:end,jj:3:end)=KK{iF}(ii:3:end,jj:3:end);
64     FLDkkFROMtmp(find(isnan(fld)))=0;
65    
66     FLDkkFROM=exch_T_N(FLDkkFROMtmp);
67     for iF2=1:fld.nFaces;
68     tmp1=FLDkkFROM{iF2}; tmp2=zeros(size(tmp1)-2);
69     for ii2=1:3; for jj2=1:3; tmp2=tmp2+tmp1(ii2:end-3+ii2,jj2:end-3+jj2); end; end;
70     FLDkkFROM{iF2}=tmp2;
71     end;
72     %clear FLDkkFROMtmp;
73    
74     [dTdxAtU,dTdyAtV]=calc_T_grad(FLDones,0);
75     tmpU=dTdxAtU.*smooth2D_Kux;
76     tmpV=dTdyAtV.*smooth2D_Kvy;
77     [fldDIV]=calc_UV_div(tmpU,tmpV);
78     dFLDdt=smooth2D_dt*fldDIV./rA;
79     dFLDdt=dFLDdt.*mskFreeze;
80    
81     dFLDdt=convert2array(dFLDdt); FLDkkFROM=convert2array(FLDkkFROM); FLDkkTO=convert2array(KK);
82     tmp1=find(dFLDdt~=0&~isnan(dFLDdt));
83     dFLDdt=dFLDdt(tmp1); FLDkkFROM=FLDkkFROM(tmp1); FLDkkTO=FLDkkTO(tmp1);
84     dFLDdt_op=dFLDdt_op+sparse(NN(FLDkkTO),NN(FLDkkFROM),dFLDdt,nn,nn);
85    
86     end; end; end;
87    
88 gforget 1.2 end;%if doFormMatrix==1;
89    
90 gforget 1.1 %figure; spy(dFLDdt_op);
91    
92     FLD_vec=convert2array(fld);
93     mskFreeze_vec=convert2array(mskFreeze);
94     FLD_vec(find(mskFreeze_vec==1))=0;
95     FLD_vec=FLD_vec(kk);
96    
97     INV_op=1-mskFreeze_vec(kk);
98     INV_op=sparse([1:nn],[1:nn],INV_op,nn,nn);
99     INV_op=INV_op+dFLDdt_op;
100     INV_vec=INV_op\FLD_vec;
101     INV_fld=convert2array(mskOut);
102     INV_fld(find(~isnan(INV_fld)))=INV_vec;
103    
104     FLD=convert2array(INV_fld,fld);
105    
106     return;
107    
108     %time step using matrix:
109     FLD_vec=convert2array(fld);
110     FLD_vec=FLD_vec(find(~isnan(FLD_vec)));
111     dFLDdt_vec=dFLDdt_op*FLD_vec;
112     dFLDdt_fld=convert2array(FLD);
113     dFLDdt_fld(find(~isnan(dFLDdt_fld)))=dFLDdt_vec;
114     dFLDdt_fld(find(isnan(dFLDdt_fld)))=0;
115    
116     %time-stepping loop:
117     FLD=fld;
118    
119     test1=0; it=0;
120     while ~test1;
121    
122     it=it+1;
123    
124     [dTdxAtU,dTdyAtV]=calc_T_grad(FLD,0);
125     tmpU=dTdxAtU.*smooth2D_Kux;
126     tmpV=dTdyAtV.*smooth2D_Kvy;
127     [fldDIV]=calc_UV_div(tmpU,tmpV);
128     dFLDdt=smooth2D_dt*fldDIV./rA;
129     dFLDdt=dFLDdt.*mskFreeze;
130     FLD=FLD-dFLDdt;
131    
132     tmp1=max(abs(dFLDdt));
133     if mod(it,10)==0; fprintf([num2str(it) ' del=' num2str(tmp1) ' eps=' num2str(eps) ' \n']); end;
134     test1=tmp1<eps;
135    
136     FLDstore{it}=dFLDdt;
137     end;
138    
139     if 1;
140     jj=1; FLDstore2=zeros([size(FLD{jj}) length(FLDstore)]);
141     for ii=1:length(FLDstore); FLDstore2(:,:,ii)=FLDstore{ii}{jj}; end;
142     tmp1=abs(FLDstore2(:,:,end)); [ii,jj]=find(tmp1==max(tmp1(:)));
143     figure; plot(cumsum(squeeze(FLDstore2(ii,jj,:))));
144     end;
145    
146    
147    

  ViewVC Help
Powered by ViewVC 1.1.22