source: codes/icosagcm/devel/Python/test/py/slice_GW_NH.py @ 798

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

devel/unstructured : more fixes to mixed precision

File size: 4.2 KB
Line 
1from dynamico.meshes import Cartesian_mesh as Mesh
2from dynamico import meshes
3from dynamico import unstructured as unst
4from dynamico import dyn
5from dynamico import time_step
6from dynamico import DCMIP
7
8import math as math
9import matplotlib.pyplot as plt
10import numpy as np
11import time
12
13def run(mesh,metric,thermo,BC,caldyn_eta,u, m0ik,gas0,Phi0_il, T=300., courant=2.9):
14    caldyn_thermo = unst.thermo_entropy
15    g = mesh.dx/metric.dx_g0
16
17    dt = courant*.5*dx/np.sqrt(gas0.c2.max())
18    nt=int(T/dt)+1
19    dt=T/nt
20    print 'nt,dt,dx', nt,dt,dx
21
22    Sik = m0ik*gas0.s
23    m0ik = m0ik/dx # NB : different definitions of mass field in DYNAMICO / Slice
24    if caldyn_thermo == unst.thermo_theta:
25        theta0 = thermo.T0*np.exp(gas0.s/thermo.Cpd)
26        S0ik = m0ik*theta0
27    else:
28        S0ik = m0ik*gas0.s
29
30    u0=mesh.field_u() 
31    u0[range(0,2*nx,2),:] = u*mesh.dx # Doppler shift by u
32    flow0=(m0ik,S0ik,u0,Phi0_il,0*Phi0_il)
33
34    if False: # time stepping in Python
35        caldyn = unst.Caldyn_NH(caldyn_thermo,caldyn_eta, mesh,thermo,BC,g)
36        scheme = time_step.ARK2(caldyn.bwd_fast_slow, dt, a32=0.7)
37        #scheme = time_step.ARK3(caldyn.bwd_fast_slow, dt)
38        def next_flow(m,S,u,Phi,W):
39            m,S,u,Phi,W = scheme.advance((m,S,u,Phi,W),nt)
40            return m,S,u,Phi,W
41    else: # time stepping in Fortran
42        scheme = time_step.ARK2(None, dt, a32=0.7)
43        caldyn_step = unst.caldyn_step_NH(mesh,scheme,nt, 
44                                          caldyn_thermo,caldyn_eta,thermo,BC,g)
45        def next_flow(m,S,u,Phi,W):
46            caldyn_step.mass[:,:], caldyn_step.theta_rhodz[:,:], caldyn_step.u[:,:] = m,S,u
47            caldyn_step.geopot[:,:], caldyn_step.W[:,:] = Phi,W
48            caldyn_step.next()
49            return (caldyn_step.mass.copy(), caldyn_step.theta_rhodz.copy(), caldyn_step.u.copy(),
50                    caldyn_step.geopot.copy(), caldyn_step.W.copy())
51
52    m,S,u,Phi,W=flow0
53
54    for i in range(12):
55        time1=time.time()
56        m,S,u,Phi,W = next_flow(m,S,u,Phi,W)
57        time2=time.time()
58        print 'ms per time step : ', 1000*(time2-time1)/nt
59
60        w=.5*(W[:,0:llm]+W[:,1:llm+1])/m
61#        junk,fast,slow = caldyn.bwd_fast_slow(flow, 0.)
62#        zz=caldyn.geopot[:,0:llm]/metric.g0/1000
63        zz=Phi[:,0:llm]/metric.g0/1000
64        print 'ptop, model top (m) :', unst.getvar('ptop'), Phi.max()/unst.getvar('g')
65
66        def plt_axes():
67            plt.plot(xx,zz[:,llm/4])
68            plt.plot(xx,zz[:,llm/2])
69            plt.plot(xx,zz[:,3*llm/4])
70            plt.ylim(0,ztop/1000.)
71            plt.colorbar()
72
73#        plt.figure(figsize=(12,3))
74#        ucomp = mesh.ucomp(u)
75#        plt.pcolor(xx,zz,caldyn.pk)
76#        plt_axes()
77#        plt.title("T(t = %f s)" % ((i+1)*dt*nt))
78#        plt.savefig('fig_slice_GW_NH/T%02d.png'%i)
79#        plt.close()
80#
81        plt.figure(figsize=(12,3))
82        ucomp = mesh.ucomp(u)
83        plt.pcolor(xx,zz,ucomp[:,:]/dx)
84        plt_axes()
85        plt.title("u(t = %f s)" % ((i+1)*dt*nt))
86        plt.savefig('fig_slice_GW_NH/U%02d.png'%i)
87        plt.close()
88
89        plt.figure(figsize=(12,3))
90        plt.pcolor(xx,zz,w)
91        plt_axes()
92        plt.title("w(t = %f s)" % ((i+1)*dt*nt))
93        plt.savefig('fig_slice_GW_NH/W%02d.png'%i)
94        plt.close()
95
96def deform(x,eta): # x and eta go from 0 to 1
97    h0, dd, xi = 500., 5000., 4000.
98    cos2=lambda x: np.cos(np.pi*x)**2
99    gauss=lambda x: np.exp(-x*x)
100    xx = x*Lx
101    phis = h0*gauss(xx/dd)*cos2(xx/xi)
102    return (phis/ztop)*np.sin(np.pi*eta)
103
104#Lx, nx, llm = 3e5, 100, 1e4, 30
105Lx, nx, ztop, llm = 3e5, 300, 1e4, 80
106nqdyn, ny, dx = 1, 1, Lx/nx
107mesh = Mesh(nx,ny,llm,nqdyn,nx*dx,ny*dx,0.)
108xx,ll = mesh.xx[:,0,:]/1000, mesh.ll[:,0,:]
109
110metric, thermo, BC, m0ik, gas0, Phi0_il, u0_jk = DCMIP.DCMIP31(Lx, nx, llm, deform=deform, dTheta=1.)
111#metric, thermo, BC, m0ik, gas0, Phi0_il, u0_jk = DCMIP.DCMIP21(Lx, nx, llm, deform=deform, h0=0.)
112
113BC.rho_bot = 1e6*BC.rho_bot # large stiffness
114
115mass_bl,mass_dak,mass_dbk = meshes.compute_hybrid_coefs(m0ik)
116unst.ker.dynamico_init_hybrid(mass_bl,mass_dak,mass_dbk)
117
118run(mesh,metric,thermo,BC,unst.eta_mass,20.,  m0ik,gas0,Phi0_il, courant=2.) # lower courant number required if h0=2000m
119#run(mesh,metric,thermo,BC,unst.eta_lag,20.,  m0ik,gas0,Phi0_il)
Note: See TracBrowser for help on using the repository browser.