1 | from dynamico import getargs |
---|
2 | log_master, log_world = getargs.getLogger() |
---|
3 | INFO, DEBUG, ERROR = log_master.info, log_master.debug, log_world.error |
---|
4 | |
---|
5 | INFO('Starting') |
---|
6 | |
---|
7 | from mpi4py import MPI |
---|
8 | comm = MPI.COMM_WORLD |
---|
9 | mpi_rank, mpi_size = comm.Get_rank(), comm.Get_size() |
---|
10 | INFO('%d/%d starting'%(mpi_rank,mpi_size)) |
---|
11 | prefix='fig_RSW2_MPAS_W02/%02d'%mpi_rank |
---|
12 | |
---|
13 | INFO('Loading DYNAMICO modules ...') |
---|
14 | from dynamico import unstructured as unst |
---|
15 | from dynamico.meshes import MPAS_Format, Unstructured_PMesh as PMesh, Local_Mesh as Mesh |
---|
16 | from dynamico import time_step |
---|
17 | from dynamico import maps |
---|
18 | print '...Done' |
---|
19 | |
---|
20 | print 'Loading modules ...' |
---|
21 | import math as math |
---|
22 | import matplotlib.pyplot as plt |
---|
23 | import numpy as np |
---|
24 | print '...Done' |
---|
25 | |
---|
26 | grid, llm, nqdyn = 2562, 1,1 # 2562, 10242, 40962 |
---|
27 | Omega, radius, g, gh0 = 2.*np.pi/86400., 6.4e6, 1., 2.94e4 |
---|
28 | N, T, courant = 40, 10400., 1.2 # simulation length = N*T |
---|
29 | |
---|
30 | print 'Omega, planetary PV', Omega, 2*Omega/gh0 |
---|
31 | planet = maps.SphereMap(radius, Omega) |
---|
32 | |
---|
33 | print 'Reading MPAS mesh ...' |
---|
34 | meshfile = MPAS_Format('grids/x1.%d.grid.nc'%grid) |
---|
35 | pmesh = PMesh(comm,meshfile) |
---|
36 | pmesh.partition_metis() |
---|
37 | mesh = Mesh(pmesh, llm, nqdyn, planet) |
---|
38 | print '...Done' |
---|
39 | lon, lat = mesh.lon_i, mesh.lat_i |
---|
40 | x,y,z = np.cos(lat)*np.cos(lon), np.cos(lat)*np.sin(lon), np.sin(lat) |
---|
41 | |
---|
42 | unst.setvar('g',g) |
---|
43 | |
---|
44 | c0 = math.sqrt(gh0) # phase speed of barotropic mode |
---|
45 | dx = mesh.de.min() |
---|
46 | dt = courant*dx/c0 |
---|
47 | print dx, dt |
---|
48 | nt = int(math.ceil(T/dt)) |
---|
49 | dt = T/nt |
---|
50 | print dx, dt, dt*c0/dx |
---|
51 | print T, nt |
---|
52 | |
---|
53 | scheme = time_step.RK4(None, dt) |
---|
54 | print dt, scheme.csjl, scheme.cfjl |
---|
55 | step = unst.caldyn_step_TRSW(mesh,scheme,nt) |
---|
56 | |
---|
57 | u0 = Omega*radius/12. # cf Williamson (1991), p.13 |
---|
58 | gh1 = radius*Omega*u0+.5*u0**2 |
---|
59 | print 'Williamson (1991) test 2, u0=', u0 |
---|
60 | ulon = u0*np.cos(mesh.lat_e) |
---|
61 | Phi0 = gh0 - gh1*(np.sin(mesh.lat_i)**2) |
---|
62 | zeta0 = (2*u0/radius+2*Omega)*np.sin(mesh.lat_v) |
---|
63 | Phi0v = gh0 - (radius*Omega*u0+.5*u0**2)*(np.sin(mesh.lat_v)**2) |
---|
64 | q0 = zeta0/Phi0v |
---|
65 | fu_perp = mesh.ucov2D(0.*ulon,2*Omega*np.sin(mesh.lat_e)*ulon) |
---|
66 | |
---|
67 | # initial condition |
---|
68 | step.mass[:]=Phi0 |
---|
69 | step.theta_rhodz[:]=Phi0 |
---|
70 | step.u[:]=mesh.ucov2D(ulon,0.*ulon) |
---|
71 | |
---|
72 | gh,u = step.mass.copy(), step.u.copy() |
---|
73 | |
---|
74 | print gh.shape, gh.min(), gh.max(), u.min(), u.max() |
---|
75 | flow=gh,u |
---|
76 | |
---|
77 | unst.ker.dynamico_update_halo(mesh.com_edges.index, 1, u.shape[0], u) |
---|
78 | |
---|
79 | for 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 | |
---|
121 | print 'Time spent in DYNAMICO (s) : ', unst.getvar('elapsed') |
---|