source: codes/icosagcm/devel/Python/test/py/bubble.py @ 617

Last change on this file since 617 was 617, checked in by dubos, 6 years ago

devel : tests for unstructured-mesh prototype

File size: 6.8 KB
Line 
1import math as math
2import numpy as np
3import matplotlib.pyplot as plt
4import matplotlib.animation as manimation
5
6import dynamico.dyn as dyn
7import dynamico.time_step as time_step
8import dynamico.DCMIP as DCMIP
9import dynamico.advect as advect
10from dynamico import unstructured as unst
11
12def reldiff(a,b):
13    return (a-b)/(np.absolute(a).max()+np.absolute(b).max()+1e-20)
14
15def go(model,courant,scheme,start):
16    def plotter(metric, flow, scalars, t):
17        mik, gas_ik, ujk, Phi_il, Wil = model.diagnose(*flow)
18        Q1, Q2 = scalars
19       
20        mil = metric.ml_ext(mik)
21        wil = Wil/mil*metric.g0
22        wik = metric.mk_ext(Wil)/mik*metric.g0
23        xik = metric.xik/1000
24        xjl = metric.xjl/1000
25        xil = metric.xil/1000
26        zil = Phi_il/metric.g0/1000
27        zik = metric.mk_int(zil)
28        zjl = metric.mj(zil)
29        print 's ',np.max(gas_ik.s),np.min(gas_ik.s)
30        print 'ujk ',np.max(ujk),np.min(ujk)
31        print 'wil ',np.max(wil),np.min(wil)
32        sik = gas_ik.s
33        sik = .5*(abs(sik)+sik)
34
35        plt.clf()
36        plt.contourf(xik, zik, sik, 20 )
37#        plt.contourf(xik, zik, wik, 20 )
38        plt.xlim((-1,1)), plt.xlabel('x (km)')
39        plt.ylim((0,ztop/1000.)), plt.ylabel('z (km)')
40        plt.colorbar()
41        plt.title("t = %f s" % (t))
42        plt.savefig('fig_bubble/bubble.png',bbox_inches='tight')
43        writer.grab_frame()
44       
45    transport = advect.SplitTransport(model.metric, advect.HorizVanLeer, advect.VertVanLeer)
46    replace = lambda fd,fv : fv
47    return DCMIP.run_with_scalars(model,scheme,transport,replace, plotter, courant, start,scalars, T1, N1, N2)
48
49Lx, ztop, nx, llm = 2000.,1000., 100, 50
50nqdyn, ny, dx = 1, 1, Lx/nx
51T1, N1, N2 = 5., 1, 200
52#T1, N1, N2 = 5., 2, 5
53
54metric, thermo, BC, m0ik, gas0, Phi0_il, u0_jk = DCMIP.thermal_bubble(Lx,nx,llm, ztop=ztop, zc=350.)
55Sik = m0ik*gas0.s
56start = (m0ik, Sik, u0_jk, Phi0_il, 0*Phi0_il)
57scalars = (Sik, m0ik*metric.mk_int(Phi0_il))
58
59model = dyn.Nonhydro(thermo,metric,BC)
60solver, caldyn_eta = dyn.MassBasedNH(model), unst.eta_mass
61#solver, caldyn_eta = dyn.LagrangianNH(model), unst.eta_lag
62
63rho_bot=BC.rho_bot
64if False: # explicit time-stepping with rigid lid
65    def rigid(flow,tau): # impose rigid BCs at top and bottom
66        flow, fast, slow = solver.bwd_fast_slow(flow,0.)
67        fast[4][:,0]=0.
68        fast[4][:,-1]=0.
69        return flow,fast,slow
70    BC.rho_bot = 100*rho_bot # moderate stiffness
71    courant, time_scheme = 2.0, lambda dt : time_step.RK4(rigid, dt)
72else: # HEVI time-stepping with pressure BC
73    # Note : parameters BC.rigid_top and BC.rigid_bottom are True by default, but are ignored by the Python code...
74    BC.rigid_top=False
75    BC.rigid_bottom=False
76    BC.rho_bot = 1e6*rho_bot # large stiffness
77#    BC.rho_bot = 100*rho_bot # moderate stiffness
78    courant, time_scheme = 3.0, lambda dt : time_step.ARK2(solver.bwd_fast_slow, dt, a32=0.7)
79
80FFMpegWriter = manimation.writers['ffmpeg']
81metadata = dict(title='Aquaplanet', artist='DYNAMICO_LMDZ5',
82                comment='Movie support!')
83writer = FFMpegWriter(fps=15, metadata=metadata, bitrate=4000, codec="h263p")
84fig = plt.figure(figsize=(12,5))
85with writer.saving(fig, "fig_bubble/bubble.avi", 100):
86    m0ik, gas0_ik, u0jk, Phi0_il, W0il = go(model, courant, time_scheme, start)
87
88#-------------------------------------------------------------------------------------------------------
89# Now use final state to check that Fortran and Python implementations produce the exact same tendencies
90#-------------------------------------------------------------------------------------------------------
91
92#W0il[:,0]=0.
93#W0il[:,-1]=0.
94
95unst.setvar('nb_threads', 1)
96mesh = unst.Cartesian_mesh(nx,ny,llm,nqdyn,Lx,ny*dx,0.)
97xx_ik, xx_il, ll = mesh.xx[:,0,:]/1000, mesh.xxp1[:,0,:]/1000, mesh.ll[:,0,:]
98
99mass_bl,mass_dak,mass_dbk=unst.compute_hybrid_coefs(m0ik)
100unst.init_hybrid(mass_bl,mass_dak,mass_dbk)
101
102caldyn_thermo = unst.thermo_entropy
103#caldyn_thermo = unst.thermo_theta
104g = mesh.dx/metric.dx_g0
105caldyn = unst.Caldyn_NH(caldyn_thermo,caldyn_eta, mesh,metric,thermo,BC,g)
106unst.setvar('rigid',False)
107
108s0ik = gas0_ik.s
109if caldyn_thermo == unst.thermo_theta:
110    s0ik = thermo.T0*np.exp(s0ik/thermo.Cpd)
111
112# 2 momentum components in DYNAMICO vs 1 component in slice
113u0jk = u0jk + 100*dx # shift horizontal wind by 100 m/s
114u0_ker = mesh.field_u()
115mesh.set_ucomp(u0_ker,u0jk)
116
117tau=0.1
118# impose hybdrid distribution of mass (Fortran)
119flow = (m0ik/dx,s0ik*m0ik/dx,u0_ker,Phi0_il,W0il/dx) # NB : different definitions of mass field in DYNAMICO / Python
120flow,fast,slow = caldyn.bwd_fast_slow(flow, 0.)
121# shift Phi from target value Phi_bot
122Phi0_il = Phi0_il+g*100.*np.exp(-10.*xx_il*xx_il)
123m,S,u,Phi,W = flow
124flow = m,S,u,Phi0_il,W
125# compute tendencies (0=Python)
126flow0 = (m*dx,S*dx,u0jk,Phi0_il,W0il, 0., 0.) # NB : different definitions of mass field in DYNAMICO / Python
127flow0, fast0, slow0 = solver.bwd_fast_slow(flow0,tau,True) 
128# compute tendencies (Fortran)
129flow,fast,slow = caldyn.bwd_fast_slow(flow, tau) 
130
131
132zz_il = Phi0_il/metric.g0/1000.
133zz_ik=.5*(zz_il[:,0:-1]+zz_il[:,1:])
134print
135print 'ptop, model top (km) :', unst.getvar('ptop'), zz_il.max()
136       
137for data0,data,dname in ((flow0,flow,'flow'),(slow0,slow,'slow'),(fast0,fast,'fast')):
138    def plot(xx,zz,var,vname):
139        title = "%s_%s" % (vname,dname)
140        filename = "bubble/bubble_%s.png" % title
141        var = 1.*var
142        if var.size>1:
143            plt.figure(figsize=(12,5))
144            plt.contourf(xx,zz,var,20)
145#                    plt.pcolor(xx,zz,var)
146            plt.xlim((-1.,1.)), plt.xlabel('x (km)')
147            plt.ylim((0.,1.)), plt.ylabel('z (km)')       
148            plt.colorbar()
149            plt.title(title)
150            plt.savefig(filename,bbox_inches='tight')
151            print title, var.min(), var.max()
152            if var.max()>1e100:
153                raise OverflowError
154                   
155    dm,dS,du,dPhi,dW = data   
156    du=mesh.ucomp(du)/dx
157    du=du[0,:,:]
158
159    dm0, dS0, du0, dPhi0, dW0, junk, junk = data0
160    dm0, dS0, du0, dW0 = dm0/dx, dS0/dx, du0/dx, dW0/dx
161
162    if dname == 'flow':
163        hflux=mesh.ucomp(caldyn.hflux)
164        plot(xx_ik, zz_ik, dm,'m')
165        plot(xx_ik, zz_ik, du,'u')
166        plot(xx_ik, zz_ik, du-du0,'uu')
167        plot(xx_ik, zz_ik, dS/m,'s')
168        plot(xx_il, zz_il, dW,'w')
169        plot(xx_il, zz_il, dW-dW0,'ww')
170    else:
171        dS=reldiff(dS,dS0)
172        dW=dW-dW0
173        dPhi=dPhi-dPhi0
174        du=reldiff(du,du0)
175        plot(xx_ik, zz_ik, dS,'dS')
176        plot(xx_il, zz_il, dPhi,'dPhi')
177        plot(xx_il, zz_il, dW,'dW')
178        plot(xx_ik, zz_ik, du,'du')
179
180if unst.getvar('rigid'):
181    print 'Fortran BCs : rigid'
182else:
183    print 'Fortran BCs : pressure/soft', unst.getvars('ptop','rho_bot','Phi_bot','pbot')
184
185if BC.rigid_top:
186    print 'Top BC : rigid'
187else:
188    print 'Top BC : pressure', BC.ptop
189
190if BC.rigid_bottom:
191    print 'Bottom BC : rigid'
192else:
193    print 'Bottom BC : soft', BC.rho_bot[0], BC.Phi_bot[0], BC.pbot[0]
Note: See TracBrowser for help on using the repository browser.