/[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.1 - (hide annotations) (download)
Wed Feb 10 14:48:13 2010 UTC (15 years, 5 months ago) by gforget
Branch: MAIN
matlab_class: smoothing filter

1 gforget 1.1 function [FLD]=diffsmooth2D(fld,mskOut);
2    
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     dFLDdt_op=sparse([],[],[],nn,nn,nn*5);
51    
52     for iF=1:fld.nFaces; for ii=1:3; for jj=1:3;
53     FLDones=fld; FLDones(find(~isnan(fld)))=0;
54     FLDones{iF}(ii:3:end,jj:3:end)=1;
55     FLDones(find(isnan(fld)))=NaN;
56    
57     FLDkkFROMtmp=fld; FLDkkFROMtmp(find(~isnan(fld)))=0;
58     FLDkkFROMtmp{iF}(ii:3:end,jj:3:end)=KK{iF}(ii:3:end,jj:3:end);
59     FLDkkFROMtmp(find(isnan(fld)))=0;
60    
61     FLDkkFROM=exch_T_N(FLDkkFROMtmp);
62     for iF2=1:fld.nFaces;
63     tmp1=FLDkkFROM{iF2}; tmp2=zeros(size(tmp1)-2);
64     for ii2=1:3; for jj2=1:3; tmp2=tmp2+tmp1(ii2:end-3+ii2,jj2:end-3+jj2); end; end;
65     FLDkkFROM{iF2}=tmp2;
66     end;
67     %clear FLDkkFROMtmp;
68    
69     [dTdxAtU,dTdyAtV]=calc_T_grad(FLDones,0);
70     tmpU=dTdxAtU.*smooth2D_Kux;
71     tmpV=dTdyAtV.*smooth2D_Kvy;
72     [fldDIV]=calc_UV_div(tmpU,tmpV);
73     dFLDdt=smooth2D_dt*fldDIV./rA;
74     dFLDdt=dFLDdt.*mskFreeze;
75    
76     dFLDdt=convert2array(dFLDdt); FLDkkFROM=convert2array(FLDkkFROM); FLDkkTO=convert2array(KK);
77     tmp1=find(dFLDdt~=0&~isnan(dFLDdt));
78     dFLDdt=dFLDdt(tmp1); FLDkkFROM=FLDkkFROM(tmp1); FLDkkTO=FLDkkTO(tmp1);
79     dFLDdt_op=dFLDdt_op+sparse(NN(FLDkkTO),NN(FLDkkFROM),dFLDdt,nn,nn);
80    
81     end; end; end;
82    
83     %figure; spy(dFLDdt_op);
84    
85     FLD_vec=convert2array(fld);
86     mskFreeze_vec=convert2array(mskFreeze);
87     FLD_vec(find(mskFreeze_vec==1))=0;
88     FLD_vec=FLD_vec(kk);
89    
90     INV_op=1-mskFreeze_vec(kk);
91     INV_op=sparse([1:nn],[1:nn],INV_op,nn,nn);
92     INV_op=INV_op+dFLDdt_op;
93     INV_vec=INV_op\FLD_vec;
94     INV_fld=convert2array(mskOut);
95     INV_fld(find(~isnan(INV_fld)))=INV_vec;
96    
97     FLD=convert2array(INV_fld,fld);
98    
99     return;
100    
101     %time step using matrix:
102     FLD_vec=convert2array(fld);
103     FLD_vec=FLD_vec(find(~isnan(FLD_vec)));
104     dFLDdt_vec=dFLDdt_op*FLD_vec;
105     dFLDdt_fld=convert2array(FLD);
106     dFLDdt_fld(find(~isnan(dFLDdt_fld)))=dFLDdt_vec;
107     dFLDdt_fld(find(isnan(dFLDdt_fld)))=0;
108    
109     %time-stepping loop:
110     FLD=fld;
111    
112     test1=0; it=0;
113     while ~test1;
114    
115     it=it+1;
116    
117     [dTdxAtU,dTdyAtV]=calc_T_grad(FLD,0);
118     tmpU=dTdxAtU.*smooth2D_Kux;
119     tmpV=dTdyAtV.*smooth2D_Kvy;
120     [fldDIV]=calc_UV_div(tmpU,tmpV);
121     dFLDdt=smooth2D_dt*fldDIV./rA;
122     dFLDdt=dFLDdt.*mskFreeze;
123     FLD=FLD-dFLDdt;
124    
125     tmp1=max(abs(dFLDdt));
126     if mod(it,10)==0; fprintf([num2str(it) ' del=' num2str(tmp1) ' eps=' num2str(eps) ' \n']); end;
127     test1=tmp1<eps;
128    
129     FLDstore{it}=dFLDdt;
130     end;
131    
132     if 1;
133     jj=1; FLDstore2=zeros([size(FLD{jj}) length(FLDstore)]);
134     for ii=1:length(FLDstore); FLDstore2(:,:,ii)=FLDstore{ii}{jj}; end;
135     tmp1=abs(FLDstore2(:,:,end)); [ii,jj]=find(tmp1==max(tmp1(:)));
136     figure; plot(cumsum(squeeze(FLDstore2(ii,jj,:))));
137     end;
138    
139    
140    

  ViewVC Help
Powered by ViewVC 1.1.22