source: codes/icosagcm/devel/Python/test/py/RSW2_MPAS_W02.py @ 804

Last change on this file since 804 was 802, checked in by dubos, 5 years ago

devel/unstructured : reduced, configurable log output

File size: 4.2 KB
Line 
1from dynamico import getargs
2log_master, log_world = getargs.getLogger()
3INFO, DEBUG, ERROR = log_master.info, log_master.debug, log_world.error
4
5INFO('Starting')
6
7from mpi4py import MPI
8comm = MPI.COMM_WORLD
9mpi_rank, mpi_size = comm.Get_rank(), comm.Get_size()
10INFO('%d/%d starting'%(mpi_rank,mpi_size))
11prefix='fig_RSW2_MPAS_W02/%02d'%mpi_rank
12
13INFO('Loading DYNAMICO modules ...')
14from dynamico import unstructured as unst
15from dynamico.meshes import MPAS_Format, Unstructured_PMesh as PMesh, Local_Mesh as Mesh
16from dynamico import time_step
17from dynamico import maps
18print '...Done'
19
20print 'Loading modules ...'
21import math as math
22import matplotlib.pyplot as plt
23import numpy as np
24print '...Done'
25
26grid, llm, nqdyn = 2562, 1,1 # 2562, 10242, 40962
27Omega, radius, g, gh0 = 2.*np.pi/86400., 6.4e6, 1., 2.94e4
28N, T, courant = 40, 10400., 1.2 # simulation length = N*T
29
30print 'Omega, planetary PV', Omega, 2*Omega/gh0
31planet = maps.SphereMap(radius, Omega)
32
33print 'Reading MPAS mesh ...'
34meshfile = MPAS_Format('grids/x1.%d.grid.nc'%grid)
35pmesh = PMesh(comm,meshfile)
36pmesh.partition_metis()
37mesh = Mesh(pmesh, llm, nqdyn, planet)
38print '...Done'
39lon, lat = mesh.lon_i, mesh.lat_i
40x,y,z = np.cos(lat)*np.cos(lon), np.cos(lat)*np.sin(lon), np.sin(lat)
41
42unst.setvar('g',g)
43
44c0 = math.sqrt(gh0) # phase speed of barotropic mode
45dx = mesh.de.min()
46dt = courant*dx/c0
47print dx, dt
48nt = int(math.ceil(T/dt))
49dt = T/nt
50print dx, dt, dt*c0/dx
51print T, nt
52
53scheme = time_step.RK4(None, dt)
54print dt, scheme.csjl, scheme.cfjl
55step = unst.caldyn_step_TRSW(mesh,scheme,nt)
56
57u0 = Omega*radius/12. # cf Williamson (1991), p.13
58gh1 = radius*Omega*u0+.5*u0**2
59print 'Williamson (1991) test 2, u0=', u0
60ulon = u0*np.cos(mesh.lat_e)
61Phi0 = gh0 - gh1*(np.sin(mesh.lat_i)**2)
62zeta0 = (2*u0/radius+2*Omega)*np.sin(mesh.lat_v)
63Phi0v = gh0 - (radius*Omega*u0+.5*u0**2)*(np.sin(mesh.lat_v)**2)
64q0 = zeta0/Phi0v
65fu_perp = mesh.ucov2D(0.*ulon,2*Omega*np.sin(mesh.lat_e)*ulon)
66
67# initial condition
68step.mass[:]=Phi0
69step.theta_rhodz[:]=Phi0
70step.u[:]=mesh.ucov2D(ulon,0.*ulon)
71
72gh,u = step.mass.copy(), step.u.copy()
73
74print gh.shape, gh.min(), gh.max(), u.min(), u.max()
75flow=gh,u
76
77unst.ker.dynamico_update_halo(mesh.com_edges.index, 1, u.shape[0], u) 
78                                 
79for i in range(20):
80    if True:
81        step.next()
82        gh,u = step.mass, step.u
83        print i, gh.shape, gh.min(), gh.max(), u.min(), u.max()
84        mesh.plot_i(gh-Phi0) ; plt.title('err(gh)');
85        plt.savefig('%s_err_gh_%02d.png'%(prefix,i))
86        plt.close()
87    else:
88        # advance by one time step using dynamico_ARK
89        gh,u = step.mass.copy(), step.u.copy()
90        print i, gh.shape, gh.min(), gh.max(), u.min(), u.max()
91        step.next()
92        gh_now,u_now = step.mass.copy(), step.u.copy()
93        dmass,dhs,du_slow,du_fast = step.drhodz[:], step.dtheta_rhodz[:], step.du_slow[:], step.du_fast[:]
94        du=du_slow+du_fast
95
96        # do the same using caldyn, obtain fast/slow tendencies first
97        gh_ref, uref=flow
98        junk, fast, slow = caldyn.bwd_fast_slow(flow,0.)                                                                     
99        dmass_ref, duslow_ref = slow
100        junk, dufast_ref = fast
101        du_ref = duslow_ref+dufast_ref
102        flow=scheme.next(flow)
103        gh_nowref, u_nowref=flow
104#    mesh.plot_i(gh) ; plt.title('gh');
105#    mesh.plot_i(Phi0) ; plt.title('gh');
106        fig = plt.figure(figsize=(6, 8)) 
107        f, ((ax1, ax2), (ax3, ax4), (ax5,ax6), (ax7,ax8)) = plt.subplots(4,2)
108#        mesh.plot_i(dmass-dhs) ; plt.title('dt*d(gh)/dt');
109        ax1.scatter(duslow_ref,du_slow-duslow_ref) ; ax1.set_title('du_slow');
110        ax2.scatter(dufast_ref,du_fast-dufast_ref) ; ax2.set_title('du_fast');
111        ax3.scatter(du_ref,du-du_ref) ; ax3.set_title('du');
112        ax4.scatter(gh_ref,gh-gh_ref) ; ax4.set_title('gh');
113        ax5.scatter(uref, u-uref) ; ax5.set_title('u');
114        ax6.scatter(dmass_ref,dmass-dmass_ref) ; ax6.set_title('dmass');
115        ax7.scatter(gh_nowref,gh_now-gh_nowref) ; ax7.set_title('gh_now');
116        ax8.scatter(u_nowref,u_now-u_nowref) ; ax8.set_title('u_now');
117        f.subplots_adjust(hspace=1.)
118        plt.savefig('%s_scatter_%02d.png'%(prefix,i))
119        plt.close()
120
121print 'Time spent in DYNAMICO (s) : ', unst.getvar('elapsed')
Note: See TracBrowser for help on using the repository browser.