source: codes/icosagcm/devel/Python/test/py/NH_3D_bubble_parallel.py @ 766

Last change on this file since 766 was 766, checked in by jisesh, 6 years ago

devel/Python : added parallel NH_3D_bubble

File size: 9.0 KB
Line 
1from dynamico import unstructured as unst
2from dynamico import dyn
3from dynamico import time_step
4from dynamico import DCMIP
5from dynamico import meshes
6from dynamico import xios
7from dynamico import precision as prec
8#from dynamico.meshes import Cartesian_mesh as Mesh
9import math as math
10import matplotlib.pyplot as plt
11import numpy as np
12import time
13import argparse
14
15parser = argparse.ArgumentParser()
16
17parser.add_argument("-mpi_ni", type=int,
18                    default=64, choices=None,
19                    help="number of x processors")
20parser.add_argument("-mpi_nj", type=int,
21                    default=64, choices=None,
22                    help="number of y processors")
23args = parser.parse_args()
24
25
26def thermal_bubble_3D(Lx,nx,Ly,ny,llm,ztop=1000., zc=350.,
27                      rc=250, thetac=0.5, x0=0., y0=0.):
28    Cpd, Rd, g, p0,theta0, T0 = 1004.5, 287.,9.81, 1e5, 300., 300.
29    nqdyn = 1
30   
31    Phi   = lambda eta : g*ztop*eta
32    p     = lambda Phi : p0*np.exp(-Phi/(Rd*T0))
33    zz    = lambda p: -(Rd*T0*np.log(p/p0))/g
34    rr    = lambda x,y,p: np.sqrt((x-x0)**2 + (y-y0)**2 + (zz(p)-zc)**2)
35    sa    = lambda x,y,p: rr(x,y,p) < rc
36    deform = lambda x,y,p: (0.5*thetac*(1+np.cos(np.pi*rr(x,y,p)/rc)))*sa(x,y,p)
37    temp  =  lambda p: theta0*(p/p0)**(Rd/Cpd)
38    T     = lambda x,y,p: deform(x,y,p) + temp(p)
39    phi0  = 45.       # Reference latitude North pi/4 (deg)
40    omega = 7.292e-5  # Angular velocity of the Earth (s^-1)
41    f0    = 2*omega*np.sin(phi0)
42    lap   = 0.005      # Lapse rate (K m^-1)
43    Rd    = 287.0      # Gas constant for dryy air (j kg^-1 K^-1)
44
45    def eta(alpha) : return (1-(lap*ztop*alpha/(T0)))**(g/(Rd*lap)) # roughly equispaced levels
46    #def eta(alpha) : return alpha/float(llm)
47
48    filename = 'cart_%03d_%03d.nc'%(nx,ny)
49    print 'Reading Cartesian mesh ...'
50    def coriolis(lon,lat): return f0+0.*lon
51    meshfile = meshes.DYNAMICO_Format(filename)
52    radius = None
53    #mesh = Mesh(nx,ny,llm,nqdyn,Lx,Ly,0.)
54    print('----read--------')
55    pmesh = meshes.Unstructured_PMesh(comm,meshfile)
56    pmesh.partition_curvilinear(args.mpi_ni,args.mpi_nj)
57    mesh  = meshes.Local_Mesh(pmesh, llm, nqdyn, radius, coriolis)
58
59    print(mesh.__dict__.keys())
60
61    alpha_k = (np.arange(llm)  +.5)/llm
62    alpha_l = (np.arange(llm+1)+ 0.)/llm
63    x_ik, alpha_ik = np.meshgrid(mesh.lon_i, alpha_k, indexing='ij')
64    y_ik, alpha_ik = np.meshgrid(mesh.lat_i, alpha_k, indexing='ij')
65    x_il, alpha_il = np.meshgrid(mesh.lon_i, alpha_l, indexing='ij')
66    y_il, alpha_il = np.meshgrid(mesh.lat_i, alpha_l, indexing='ij')
67    x_ek, alpha_ek = np.meshgrid(mesh.lon_e, alpha_k, indexing='ij')
68    y_ek, alpha_ek = np.meshgrid(mesh.lat_e, alpha_k, indexing='ij')
69
70    print('alpha_l=',alpha_l)
71
72    thermo = dyn.Ideal_perfect(Cpd, Rd, p0, T0)
73
74    eta_il = eta(alpha_il)
75    eta_ik = eta(alpha_ik)
76    eta_ek = eta(alpha_ek)
77
78    print('eta_il=',eta_il)
79    #Phi_il = Phi(mesh.llp1/float(llm)) #llp is not defined in local mesh
80    #Phi_ik = Phi((mesh.ll+.5)/llm)
81    Phi_il = Phi(eta_il)
82    Phi_ik = Phi(eta_ik)
83    p_ik = p(Phi_ik)
84    T_ik = T(x_ik, y_ik, p_ik)
85
86    gas = thermo.set_pT(p_ik,T_ik)
87    mass_ik = mesh.field_mass()
88    for l in range(llm): 
89        mass_ik[:,l]=(Phi_il[:,l+1]-Phi_il[:,l])/(g*gas.v[:,l])
90    Sik, ujk, Wil  = gas.s*mass_ik, mesh.field_u(), mesh.field_w()
91   
92    print 'ztop (m) = ', Phi_il[0,-1]/g, ztop
93    ptop = p(g*ztop)
94    print 'ptop (Pa) = ', gas.p[0,-1], ptop
95    params=dyn.Struct()
96    params.ptop=ptop
97    params.dx=dx
98    params.dx_g0=dx/g
99    params.g = g
100    #pbot = p(Phi_il[:,:,0])
101    #gas_bot = thermo.set_pT(pbot, temp(pbot))
102    # define parameters for lower BC
103    pbot = p(eta_il[0])
104    print 'min p, T :', pbot.min(), temp(pbot/p0)
105    gas_bot = thermo.set_pT(pbot, temp(pbot/p0))
106    params.pbot = gas_bot.p
107    params.rho_bot = 1e6/gas_bot.v
108   
109    return thermo, mesh, params, prec.asnum([mass_ik,Sik,ujk,Phi_il,Wil]), gas
110
111with xios.Client() as client: # setup XIOS which creates the DYNAMICO communicator
112    comm = client.comm
113    mpi_rank, mpi_size = comm.Get_rank(), comm.Get_size()
114    print '%d/%d starting'%(mpi_rank,mpi_size)
115
116    #Lx, nx, llm, thetac, T, Nslice, courant = 2000., 100, 50, 30., 5., 10, 2.8
117    Lx, nx, llm, thetac = 2000., 200, 79, 30
118    #Lx, nx, llm, thetac, T, Nslice, courant = 3000., 75, 25, -30, 5., 10, 2.8
119
120    nqdyn, dx = 1, Lx/nx
121    Ly,ny,dy = Lx,nx,dx
122
123    g=9.81
124    unst.setvar('g',g)
125
126    print('bubble call-----------')
127    thermo, mesh, params, flow0, gas0 = thermal_bubble_3D(Lx,nx,Ly,ny,llm, thetac=thetac)
128    print('bubble done-----------')
129
130
131    # compute hybrid coefs from initial distribution of mass
132    mass_bl,mass_dak,mass_dbk = meshes.compute_hybrid_coefs(flow0[0])
133    print 'Type of mass_bl, mass_dak, mass_dbk : ', [x.dtype for x in mass_bl, mass_dak, mass_dbk]
134    unst.ker.dynamico_init_hybrid(mass_bl,mass_dak,mass_dbk)
135
136    #dz = flow0[3].max()/(params.g*llm)
137    #dt = courant*.5/np.sqrt(gas0.c2.max()*(dx**-2+dy**-2+dz**-2))
138    #dt = courant*.5/np.sqrt(gas0.c2.max()*(dx**-2+dy**-2))
139    #nt = int(math.ceil(T/dt))
140    #dt = T/nt
141    #print 'Time step : %d x %g s' % (nt,dt)
142
143
144    T, nslice, dt = 3600., 1, 3600.
145    #T, nslice  = 3600., 4
146
147    with xios.Context_Curvilinear(mesh,1, dt) as context:
148        # now XIOS knows about the mesh and we can write to disk
149        for i in range(48): # 2 days
150            context.update_calendar(i)
151           
152            print 'send_field', i, gas0.T.shape
153    #    context.send_field_primal('ps', lat_i)
154            context.send_field_primal('temp', gas0.T)
155            context.send_field_primal('p', gas0.p)
156    print(gas0.__dict__.keys())
157    print('size of T, p',np.shape(gas0.T),np.shape(gas0.p))
158    print('************DONE************')
159
160#    #caldyn_thermo, caldyn_eta = unst.thermo_theta, unst.eta_mass
161#    caldyn_thermo, caldyn_eta = unst.thermo_entropy, unst.eta_mass
162#    #caldyn_thermo, caldyn_eta = unst.thermo_entropy, unst.eta_lag
163#
164#    if False: # time stepping in Python
165#        caldyn = unst.Caldyn_NH(caldyn_thermo,caldyn_eta, mesh,thermo,params,params.g)
166#        scheme = time_step.ARK2(caldyn.bwd_fast_slow, dt)
167#        def next_flow(m,S,u,Phi,W):
168#            # junk,fast,slow = caldyn.bwd_fast_slow(flow, 0.)
169#            return scheme.advance((m,S,u,Phi,W),nt)
170#
171#    else: # time stepping in Fortran
172#        scheme = time_step.ARK2(None, dt)
173#        caldyn_step = unst.caldyn_step_NH(mesh,scheme,nt, caldyn_thermo,caldyn_eta,
174#                                      thermo,params,params.g)
175#        def next_flow(m,S,u,Phi,W):
176#            # junk,fast,slow = caldyn.bwd_fast_slow(flow, 0.)
177#            caldyn_step.mass[:,:], caldyn_step.theta_rhodz[:,:], caldyn_step.u[:,:] = m,S,u
178#            caldyn_step.geopot[:,:], caldyn_step.W[:,:] = Phi,W
179#            caldyn_step.next()
180#            return (caldyn_step.mass.copy(), caldyn_step.theta_rhodz.copy(), caldyn_step.u.copy(),
181#                caldyn_step.geopot.copy(), caldyn_step.W.copy())
182#   
183#    m,S,u,Phi,W=flow0
184#    if caldyn_thermo == unst.thermo_theta:
185#        s=S/m
186#        theta = thermo.T0*np.exp(s/thermo.Cpd)
187#        S=m*theta
188#        title_format = 'Potential temperature at t=%g s (K)'
189#    else:
190#        title_format = 'Specific entropy at t=%g s (J/K/kg)'
191#   
192#    w=mesh.field_mass()
193#    z=mesh.field_mass()
194#   
195#    for it in range(Nslice):
196#        s=S/m ; s=.5*(s+abs(s))
197#        for l in range(llm):
198#            #w[:,:,l]=.5*params.g*(W[:,:,l+1]+W[:,:,l])/m[:,:,l]
199#            w[:,l]=.5*params.g*(W[:,l+1]+W[:,l])/m[:,l]
200#            #z[:,:,l]=.5*(Phi[:,:,l+1]+Phi[:,:,l])/params.g
201#            z[:,l]=.5*(Phi[:,l+1]+Phi[:,l])/params.g
202#           
203#        print 'ptop, model top (m) :', unst.getvar('ptop'), Phi.max()/unst.getvar('g')
204#        jj=ny/2
205#        #xx,zz,ss,ww = mesh.xx[jj,:,:]/1000., z[jj,:,:]/1000., s[jj,:,:], w[jj,:,:]
206#        #xx,zz,ss,ww = x_ik[jj,:]/1000., z[jj,:]/1000., s[jj,:], w[jj,:]
207#   
208#        #f, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(12,4))
209#       
210#        #c=ax1.contourf(xx,zz,ss,20)
211#        #ax1.set_xlim((-.5,.5)), ax1.set_xlabel('x (km)')
212#        #ax1.set_ylim((0.,1.)), ax1.set_ylabel('z (km)')       
213#        #plt.colorbar(c,ax=ax1)
214#        #ax1.set_title(title_format % (it*T,))
215#   
216#        #    plt.show()
217#       
218#        #    plt.figure(figsize=(12,5))
219#        #c=ax2.contourf(xx,zz,ww,20)
220#        #ax2.set_xlim((-.5,.5)), ax2.set_xlabel('x (km)')
221#        #ax2.set_ylim((0.,1.)), ax2.set_ylabel('z (km)')       
222#        #plt.colorbar(c,ax=ax2)
223#        #ax2.set_title('Vertical velocity at t=%g s (m/s)' % (it*T,))
224#        #    plt.tight_layout()
225#        #    plt.show()
226#        #plt.savefig('fig_NH_3D_bubble/%02d.png'%it)
227#       
228#        time1, elapsed1 =time.time(), unst.getvar('elapsed')
229#        m,S,u,Phi,W = next_flow(m,S,u,Phi,W)
230#        time2, elapsed2 =time.time(), unst.getvar('elapsed')
231#        factor = 1000./nt
232#        print 'ms per full time step : ', factor*(time2-time1), factor*(elapsed2-elapsed1)
233#        factor = 1e9/(4*nt*nx*ny*llm)
234#        print 'nanosec per gridpoint per full time step : ', factor*(time2-time1), factor*(elapsed2-elapsed1)
235#           
Note: See TracBrowser for help on using the repository browser.