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

Last change on this file since 631 was 631, checked in by dubos, 7 years ago

devel/Python : extract pure Python stuff from cython module unstructured.pyx

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