Ignore:
Timestamp:
12/19/17 15:26:51 (6 years ago)
Author:
dubos
Message:

devel/unstructured : bubble test case with Fortran time stepping

Location:
codes/icosagcm/devel/Python
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • codes/icosagcm/devel/Python/dynamico/meshes.py

    r639 r642  
    148148                  Aiv,Aiv,f+0.*Aiv,le_de,Riv2,-wee) 
    149149 
    150     def field_theta(self): return squeeze((self.nqdyn,self.ny,self.nx,self.llm)) 
    151     def field_mass(self): return squeeze((self.ny,self.nx,self.llm)) 
    152     def field_z(self): return squeeze((self.ny,self.nx,self.llm)) 
    153     def field_w(self): return squeeze((self.ny,self.nx,self.llm+1)) 
    154     def field_u(self): return np.zeros((self.ny,2*self.nx,self.llm)) 
    155     def field_ps(self): return squeeze((self.ny,self.nx)) 
     150    def field_theta(self,n=1): return squeeze((n,self.nqdyn,self.ny,self.nx,self.llm)) 
     151    def field_mass(self,n=1): return squeeze((n,self.ny,self.nx,self.llm)) 
     152    def field_z(self,n=1): return squeeze((n,self.ny,self.nx,self.llm)) 
     153    def field_w(self,n=1): return squeeze((n,self.ny,self.nx,self.llm+1)) 
     154    def field_u(self,n=1):  
     155        if n==1: 
     156            return np.zeros((self.ny,2*self.nx,self.llm)) 
     157        else: 
     158            return np.zeros((n,self.ny,2*self.nx,self.llm)) 
     159    def field_ps(self,n=1): return squeeze((n,self.ny,self.nx)) 
    156160    def ucomp(self,u): return u[:,range(0,2*self.nx,2),:] 
    157161    def set_ucomp(self,uv,u): uv[:,range(0,2*self.nx,2),:]=u 
  • codes/icosagcm/devel/Python/src/functions.h

    r639 r642  
    77void dynamico_init_params(void); 
    88 
    9 void dynamico_ARK_step(double *mass_col, double *rhodz, double *theta_rhodz,  
     9void dynamico_ARK_step(int nstep, 
     10                       double *mass_col, double *rhodz, double *theta_rhodz,  
    1011                       double *u, double *geopot, double *w, 
    1112                       double *theta, double *ps, double *pk, double *hflux, double *qv, 
  • codes/icosagcm/devel/Python/src/unstructured.pyx

    r640 r642  
    1818 
    1919cdef extern from "functions.h": 
    20      cdef void dynamico_ARK_step(double *mass_col, double *rhodz, double *theta_rhodz,  
     20     cdef void dynamico_ARK_step(int nstep, 
     21                                 double *mass_col, double *rhodz, double *theta_rhodz,  
    2122                                 double *u, double *geopot, double *w, 
    2223                                 double *theta, double *ps, double *pk, double *hflux, double *qv, 
     
    7879cdef dbl_ptr ptr2(double[:,:] data) : return &data[0,0] 
    7980cdef dbl_ptr ptr3(double[:,:,:] data) : return &data[0,0,0] 
     81cdef dbl_ptr ptr4(double[:,:,:,:] data) : return &data[0,0,0,0] 
    8082cdef dbl_ptr ptr(data): 
    8183    n=data.ndim 
     
    8385    if n==2 : return ptr2(data) 
    8486    if n==3 : return ptr3(data) 
    85  
     87    if n==4 : return ptr4(data) 
     88    if n>4: raise IndexError 
     89         
    8690cdef alloc(dbl_ptr *p, allocator, n=1): 
    8791    data=allocator(n) 
     
    9397         
    9498cdef class Caldyn_step: 
     99    # number of time steps to do at each invocation of advance() 
     100    cdef int nstep 
    95101    # pointer to allocated arrays 
    96     cdef dbl_ptr p_mass, p_theta_rhodz, p_u, p_geopot, p_w                   # prognostic 
     102    cdef dbl_ptr p_mass, p_theta_rhodz, p_u, p_geopot, p_W                   # prognostic 
    97103    cdef dbl_ptr p_mass_col, p_dmass_col, p_ps, p_theta, p_pk, p_hflux, p_qv # diagnostic 
    98104    cdef dbl_ptr p_drhodz, p_dtheta_rhodz, p_du_fast, p_du_slow                # tendencies 
    99105    cdef dbl_ptr p_dPhi_fast, p_dPhi_slow, p_dW_fast, p_dW_slow                # tendencies 
    100106    # allocated arrays, must remain referenced or segfault 
    101     cdef readonly np.ndarray mass, theta_rhodz, u, geopot, w 
     107    cdef readonly np.ndarray mass, theta_rhodz, u, geopot, W 
    102108    cdef readonly np.ndarray mass_col, dmass_col, ps, theta, pk, hflux, qv 
    103109    cdef readonly np.ndarray drhodz, dtheta_rhodz, du_fast, du_slow 
    104110    cdef readonly np.ndarray dPhi_fast, dPhi_slow, dW_fast, dW_slow 
    105111 
    106     def __init__(self,mesh,time_scheme): 
     112    def __init__(self,mesh,time_scheme, nstep): 
     113        self.nstep=nstep 
    107114        #        self.mesh=mesh 
    108115        fps, ftheta, fmass = mesh.field_ps, mesh.field_theta, mesh.field_mass 
     
    113120        cdef double[:,:] cflj_ = time_scheme.cfjl 
    114121        ns = time_scheme.nstage 
    115         nb_stage[0]= ns 
     122        nb_stage[0]=ns 
     123 
    116124        cdef int i,j 
    117125        for i in range(ns): 
     
    123131        #    prognostic/diagnostic 
    124132        self.ps                       = alloc(&self.p_ps, fps)  
    125         self.mass_col, self.dmass_col = alloc(&self.p_mass_col, fps), alloc(&self.p_dmass_col, fps),  
     133        self.mass_col, self.dmass_col = alloc(&self.p_mass_col, fps), alloc(&self.p_dmass_col, fps,ns),  
    126134        self.mass, self.theta_rhodz   = alloc(&self.p_mass, fmass), alloc(&self.p_theta_rhodz, fmass), 
    127135        self.theta, self.pk           = alloc(&self.p_theta, fmass), alloc(&self.p_pk, fmass),  
    128         self.geopot, self.w           = alloc(&self.p_geopot, fw), alloc(&self.p_w, fw), 
     136        self.geopot, self.W           = alloc(&self.p_geopot, fw), alloc(&self.p_W, fw), 
    129137        self.hflux, self.u            = alloc(&self.p_hflux, fu), alloc(&self.p_u, fu)  
    130138        self.qv                       = alloc(&self.p_qv,fz) 
     
    137145        #        global elapsed 
    138146        # time1=time.time() 
    139         check_ptr('mass', self.p_mass, self.mass) 
    140         check_ptr('u', self.p_u, self.u) 
    141         dynamico_ARK_step(self.p_mass_col, self.p_mass, self.p_theta_rhodz, 
    142                           self.p_u, self.p_geopot, self.p_w, 
     147        dynamico_ARK_step(self.nstep, 
     148                          self.p_mass_col, self.p_mass, self.p_theta_rhodz, 
     149                          self.p_u, self.p_geopot, self.p_W, 
    143150                          self.p_theta, self.p_ps, self.p_pk, self.p_hflux, self.p_qv, 
    144151                          self.p_dmass_col, self.p_drhodz, self.p_dtheta_rhodz, 
     
    148155        #time2=time.time() 
    149156        #if time2>time1: elapsed=elapsed+time2-time1 
    150     def setup_TRSW(self): 
    151         setvars(('hydrostatic','caldyn_thermo','caldyn_eta'), 
    152                 (True,thermo_boussinesq,eta_lag)) 
    153         dynamico_init_params() 
    154     def setup_HPE(self, caldyn_thermo, caldyn_eta, g, BC,thermo): 
    155         setvars(('hydrostatic','caldyn_thermo','caldyn_eta', 
    156                  'g','ptop','Rd','cpp','preff','Treff'), 
    157                 (True,caldyn_thermo,caldyn_eta, 
    158                  g,BC.ptop,thermo.Rd,thermo.Cpd,thermo.p0,thermo.T0)) 
    159         dynamico_init_params() 
    160     def setup_NH(self, caldyn_thermo, caldyn_eta, g, BC,thermo): 
    161         setvars(('hydrostatic','caldyn_thermo','caldyn_eta', 
    162                  'g','ptop','Rd','cpp','preff','Treff', 
    163                  'pbot','rho_bot'), 
    164                 (False,caldyn_thermo,caldyn_eta, 
    165                  g,BC.ptop,thermo.Rd,thermo.Cpd,thermo.p0,thermo.T0, 
    166                  BC.pbot.max(), BC.rho_bot.max())) 
    167         dynamico_init_params() 
    168  
     157 
     158def caldyn_step_TRSW(mesh,time_scheme,nstep): 
     159    setvars(('hydrostatic','caldyn_thermo','caldyn_eta'), 
     160            (True,thermo_boussinesq,eta_lag)) 
     161    dynamico_init_params() 
     162    return Caldyn_step(mesh,time_scheme, nstep) 
     163def caldyn_step_HPE(mesh,time_scheme,nstep, caldyn_thermo,caldyn_eta, thermo,BC,g): 
     164    setvars(('hydrostatic','caldyn_thermo','caldyn_eta', 
     165             'g','ptop','Rd','cpp','preff','Treff'), 
     166             (True,caldyn_thermo,caldyn_eta, 
     167              g,BC.ptop,thermo.Rd,thermo.Cpd,thermo.p0,thermo.T0)) 
     168    dynamico_init_params() 
     169    return Caldyn_step(mesh,time_scheme, nstep) 
     170def caldyn_step_NH(mesh,time_scheme,nstep, caldyn_thermo, caldyn_eta, thermo,BC,g): 
     171    setvars(('hydrostatic','caldyn_thermo','caldyn_eta', 
     172             'g','ptop','Rd','cpp','preff','Treff','pbot','rho_bot'), 
     173             (False,caldyn_thermo,caldyn_eta, 
     174              g,BC.ptop,thermo.Rd,thermo.Cpd,thermo.p0,thermo.T0, 
     175              BC.pbot.max(), BC.rho_bot.max())) 
     176    dynamico_init_params() 
     177    return Caldyn_step(mesh,time_scheme, nstep) 
     178     
    169179#----------------------------- Base class for dynamics ------------------------ 
    170180 
     
    212222 
    213223class Caldyn_HPE(Caldyn): 
    214     def __init__(self,caldyn_thermo,caldyn_eta, mesh,metric,thermo,BC,g): 
     224    def __init__(self,caldyn_thermo,caldyn_eta, mesh,thermo,BC,g): 
    215225        Caldyn.__init__(self,mesh) 
    216226        setvars(('hydrostatic','caldyn_thermo','caldyn_eta', 
     
    231241 
    232242class Caldyn_NH(Caldyn): 
    233     def __init__(self,caldyn_thermo,caldyn_eta, mesh,metric,thermo,BC,g): 
     243    def __init__(self,caldyn_thermo,caldyn_eta, mesh,thermo,BC,g): 
    234244        Caldyn.__init__(self,mesh) 
    235245        setvars(('hydrostatic','caldyn_thermo','caldyn_eta', 
  • codes/icosagcm/devel/Python/test/fig_RSW2_MPAS_W02

    • Property svn:ignore set to
      *.png
  • codes/icosagcm/devel/Python/test/py/NH_3D_bubble.py

    r632 r642  
    5353    return thermo, mesh, params, (mass_ik,Sik,ujk,Phi_il,Wil), gas 
    5454 
    55 Lx, nx, llm, thetac, T, Nslice, courant = 2000., 100, 50, 30., 5., 10, 2.8 
    56 #Lx, nx, llm, thetac, T, Nslice, courant = 2000., 50, 25, 1., 50., 10, 2.8 
     55#Lx, nx, llm, thetac, T, Nslice, courant = 2000., 100, 50, 30., 5., 10, 2.8 
     56Lx, nx, llm, thetac, T, Nslice, courant = 2000., 50, 25, 30, 5., 10, 2.8 
    5757#Lx, nx, llm, thetac, T, Nslice, courant = 3000., 75, 25, -30, 5., 10, 2.8 
    5858 
     
    6161thermo, mesh, params, flow0, gas0 = thermal_bubble_3D(Lx,nx,Ly,ny,llm, thetac=thetac) 
    6262 
    63 caldyn_thermo, caldyn_eta = unst.thermo_entropy, unst.eta_mass 
    64 caldyn = unst.Caldyn_NH(caldyn_thermo,caldyn_eta, mesh,params,thermo,params,params.g) 
    65  
    6663# compute hybrid coefs from initial distribution of mass 
    6764mass_bl,mass_dak,mass_dbk = meshes.compute_hybrid_coefs(flow0[0]) 
    6865unst.ker.dynamico_init_hybrid(mass_bl,mass_dak,mass_dbk) 
    69 print mass_dbk 
    7066 
    7167dz = flow0[3].max()/(params.g*llm) 
     
    7470nt = int(math.ceil(T/dt)) 
    7571dt = T/nt 
    76 print 'Time step : %g s' % dt 
     72print 'Time step : %d x %g s' % (nt,dt) 
    7773 
    78 scheme = time_step.ARK2(caldyn.bwd_fast_slow, dt) 
     74caldyn_thermo, caldyn_eta = unst.thermo_entropy, unst.eta_mass 
     75#caldyn_thermo, caldyn_eta = unst.thermo_entropy, unst.eta_lag 
    7976 
    80 flow=flow0 
     77if False: # time stepping in Python 
     78    caldyn = unst.Caldyn_NH(caldyn_thermo,caldyn_eta, mesh,thermo,params,params.g) 
     79    scheme = time_step.ARK2(caldyn.bwd_fast_slow, dt) 
     80    def next_flow(m,S,u,Phi,W): 
     81        # junk,fast,slow = caldyn.bwd_fast_slow(flow, 0.) 
     82        return scheme.advance((m,S,u,Phi,W),nt) 
     83 
     84else: # time stepping in Fortran 
     85    scheme = time_step.ARK2(None, dt) 
     86    caldyn_step = unst.caldyn_step_NH(mesh,scheme,nt, caldyn_thermo,caldyn_eta, 
     87                                      thermo,params,params.g) 
     88    def next_flow(m,S,u,Phi,W): 
     89        # junk,fast,slow = caldyn.bwd_fast_slow(flow, 0.) 
     90        caldyn_step.mass[:,:,:], caldyn_step.theta_rhodz[:,:,:], caldyn_step.u[:,:,:] = m,S,u 
     91        caldyn_step.geopot[:,:,:], caldyn_step.W[:,:,:] = Phi,W 
     92        caldyn_step.next() 
     93        return (caldyn_step.mass.copy(), caldyn_step.theta_rhodz.copy(), caldyn_step.u.copy(), 
     94                caldyn_step.geopot.copy(), caldyn_step.W.copy()) 
     95     
     96m,S,u,Phi,W=flow0 
    8197w=mesh.field_mass() 
    8298z=mesh.field_mass() 
     99 
    83100for it in range(Nslice): 
    84     junk,fast,slow = caldyn.bwd_fast_slow(flow, 0.) 
    85     m,S,u,Phi,W = flow 
    86     s=S/m ; # s=.5*(s+abs(s)) 
     101    s=S/m ; s=.5*(s+abs(s)) 
    87102    for l in range(llm): 
    88103        w[:,:,l]=.5*params.g*(W[:,:,l+1]+W[:,:,l])/m[:,:,l] 
     
    92107    jj=ny/2 
    93108    xx,zz,ss,ww = mesh.xx[:,jj,:]/1000., z[:,jj,:]/1000., s[:,jj,:], w[:,jj,:] 
     109 
     110    f, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(12,4)) 
    94111     
    95     f, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(12,4)) 
    96  
    97112    c=ax1.contourf(xx,zz,ss,20) 
    98113    ax1.set_xlim((-.5,.5)), ax1.set_xlabel('x (km)') 
     
    100115    plt.colorbar(c,ax=ax1) 
    101116    ax1.set_title('Specific entropy at t=%g s (J/K/kg)' % (it*T,)) 
    102 #    plt.show() 
     117    #    plt.show() 
    103118     
    104 #    plt.figure(figsize=(12,5)) 
     119    #    plt.figure(figsize=(12,5)) 
    105120    c=ax2.contourf(xx,zz,ww,20) 
    106121    ax2.set_xlim((-.5,.5)), ax2.set_xlabel('x (km)') 
     
    108123    plt.colorbar(c,ax=ax2) 
    109124    ax2.set_title('Vertical velocity at t=%g s (m/s)' % (it*T,)) 
    110 #    plt.tight_layout() 
    111 #    plt.show() 
     125    #    plt.tight_layout() 
     126    #    plt.show() 
    112127    plt.savefig('fig_NH_3D_bubble/%02d.png'%it) 
    113128     
    114129    time1, elapsed1 =time.time(), unst.getvar('elapsed') 
    115     flow = scheme.advance(flow,nt) 
     130    m,S,u,Phi,W = next_flow(m,S,u,Phi,W) 
    116131    time2, elapsed2 =time.time(), unst.getvar('elapsed') 
    117132    factor = 1000./(4*nt) 
  • codes/icosagcm/devel/Python/test/py/RSW2_MPAS_W02.py

    r641 r642  
    3838#scheme = time_step.RKn_simple(1,caldyn.bwd_fast_slow, dt) # forward Euler scheme 
    3939print dt, scheme.csjl, scheme.cfjl 
    40 step = unst.Caldyn_step(mesh,scheme) 
    41 step.setup_TRSW() 
     40step = unst.caldyn_step_TRSW(mesh,scheme,nt) 
    4241 
    4342u0 = Omega*radius/12. # cf Williamson (1991), p.13 
     
    6362for i in range(20): 
    6463    if True: 
    65         for j in range(nt): 
    66             step.next() 
     64        step.next() 
    6765        gh,u = step.mass, step.u 
    6866        print i, gh.shape, gh.min(), gh.max(), u.min(), u.max() 
  • codes/icosagcm/devel/Python/test/py/slice_GW_hydro.py

    r631 r642  
    1515    caldyn_thermo = unst.thermo_entropy 
    1616    g = mesh.dx/metric.dx_g0 
    17     caldyn = unst.Caldyn_HPE(caldyn_thermo,caldyn_eta, mesh,metric,thermo,BC,g) 
    1817 
    1918    Sik = m0ik*gas0.s 
     
    2524        S0ik = m0ik*gas0.s 
    2625 
    27     u0=mesh.field_u()  
     26    u0=mesh.field_u() 
    2827    u0[:,range(0,2*nx,2),:] = 100.*mesh.dx # Doppler shift 100 m/s 
    2928    flow0=(m0ik,S0ik,u0) 
     
    3534    dt=T/nt 
    3635    print 'nt,dt,dx', nt,dt,dx 
    37     scheme = time_step.ARK2(caldyn.bwd_fast_slow, dt, a32=0.7) 
    38     #scheme = time_step.ARK3(caldyn.bwd_fast_slow, dt) 
    3936 
    40     flow=flow0 
     37    m,S,u=flow0 
     38 
     39    if False: # time stepping in Python 
     40        caldyn = unst.Caldyn_HPE(caldyn_thermo,caldyn_eta, mesh,thermo,BC,g) 
     41        scheme = time_step.ARK2(caldyn.bwd_fast_slow, dt, a32=0.7) 
     42        #scheme = time_step.ARK3(caldyn.bwd_fast_slow, dt) 
     43        def next_flow(m,S,u): 
     44            m,S,u = scheme.advance((m,S,u),nt) 
     45            return m,S,u,caldyn.geopot 
     46    else: # time stepping in Fortran 
     47        scheme = time_step.ARK2(None, dt, a32=0.7) 
     48        caldyn_step = unst.caldyn_step_HPE(mesh,scheme,nt, caldyn_thermo,caldyn_eta,thermo,BC,g) 
     49        def next_flow(m,S,u): 
     50            caldyn_step.mass[:,:], caldyn_step.theta_rhodz[:,:], caldyn_step.u[:,:] = m,S,u 
     51            caldyn_step.next() 
     52            return caldyn_step.mass, caldyn_step.theta_rhodz, caldyn_step.u, caldyn_step.geopot 
     53         
    4154    for i in range(10): 
    4255        time1=time.time() 
    43         flow = scheme.advance(flow,nt) 
     56        m,S,u,geopot = next_flow(m,S,u) 
    4457        time2=time.time() 
    45         m,S,u = flow 
    46         print 'ms per time step : ', 1000*(time2-time1)/nt 
    47         print 'ptop, model top (m) :', unst.getvar('ptop'), caldyn.geopot.max()/unst.getvar('g') 
    48         junk,fast,slow = caldyn.bwd_fast_slow(flow, 0.) 
    49         zz=caldyn.geopot[:,0:llm]/metric.g0/1000 
     58        print 'ms per time step : ', 1000*(time2-time1)/nt, 1000*unst.getvar('elapsed')/(nt*(i+1)) 
     59        print 'ptop, model top (m) :', unst.getvar('ptop'), geopot.max()/unst.getvar('g') 
     60        #        junk,fast,slow = caldyn.bwd_fast_slow(flow, 0.) 
     61        zz=geopot[:,0:llm]/metric.g0/1000 
    5062        plt.figure(figsize=(12,3)) 
    5163        ucomp = mesh.ucomp(u) 
Note: See TracChangeset for help on using the changeset viewer.