1 | print 'Loading modules ...' |
---|
2 | import math as math |
---|
3 | import matplotlib.pyplot as plt |
---|
4 | import numpy as np |
---|
5 | print '...Done' |
---|
6 | |
---|
7 | print 'Loading DYNAMICO modules ...' |
---|
8 | from dynamico import unstructured as unst |
---|
9 | from dynamico.meshes import MPAS_Mesh as Mesh |
---|
10 | from dynamico import time_step |
---|
11 | print '...Done' |
---|
12 | |
---|
13 | grid, llm, nqdyn = 10242, 1,1 # 2562, 10242, 40962 |
---|
14 | Omega, radius, g, gh0 = 2.*np.pi/86400., 6.4e6, 1., 2.94e4 |
---|
15 | N, T, courant = 40, 10800., 1.2 # simulation length = N*T |
---|
16 | |
---|
17 | print 'Omega, planetary PV', Omega, 2*Omega/gh0 |
---|
18 | |
---|
19 | def f(lon,lat): return 2*Omega*np.sin(lat) # Coriolis parameter |
---|
20 | print 'Reading MPAS mesh ...' |
---|
21 | mesh = Mesh('grids/x1.%d.grid.nc'%grid, llm, nqdyn, radius, f) |
---|
22 | print '...Done' |
---|
23 | lon, lat = mesh.lon_i, mesh.lat_i |
---|
24 | x,y,z = np.cos(lat)*np.cos(lon), np.cos(lat)*np.sin(lon), np.sin(lat) |
---|
25 | |
---|
26 | unst.setvar('g',g) |
---|
27 | |
---|
28 | c0 = math.sqrt(gh0) # phase speed of barotropic mode |
---|
29 | dx = mesh.de.min() |
---|
30 | dt = courant*dx/c0 |
---|
31 | nt = int(math.ceil(T/dt)) |
---|
32 | dt = T/nt |
---|
33 | print dx, dt, dt*c0/dx |
---|
34 | print T, nt |
---|
35 | |
---|
36 | caldyn = unst.Caldyn_RSW(mesh) |
---|
37 | scheme = time_step.RK4(caldyn.bwd_fast_slow, dt) |
---|
38 | #scheme = time_step.RKn_simple(1,caldyn.bwd_fast_slow, dt) # forward Euler scheme |
---|
39 | print dt, scheme.csjl, scheme.cfjl |
---|
40 | step = unst.caldyn_step_TRSW(mesh,scheme,nt) |
---|
41 | |
---|
42 | u0 = Omega*radius/12. # cf Williamson (1991), p.13 |
---|
43 | gh1 = radius*Omega*u0+.5*u0**2 |
---|
44 | print 'Williamson (1991) test 2, u0=', u0 |
---|
45 | ulon = u0*np.cos(mesh.lat_e) |
---|
46 | Phi0 = gh0 - gh1*(np.sin(mesh.lat_i)**2) |
---|
47 | zeta0 = (2*u0/radius+2*Omega)*np.sin(mesh.lat_v) |
---|
48 | Phi0v = gh0 - (radius*Omega*u0+.5*u0**2)*(np.sin(mesh.lat_v)**2) |
---|
49 | q0 = zeta0/Phi0v |
---|
50 | fu_perp = mesh.ucov2D(0.*ulon,2*Omega*np.sin(mesh.lat_e)*ulon) |
---|
51 | |
---|
52 | # initial condition |
---|
53 | step.mass[:]=Phi0 |
---|
54 | step.theta_rhodz[:]=Phi0 |
---|
55 | step.u[:]=mesh.ucov2D(ulon,0.*ulon) |
---|
56 | |
---|
57 | gh,u = step.mass.copy(), step.u.copy() |
---|
58 | |
---|
59 | print gh.shape, gh.min(), gh.max(), u.min(), u.max() |
---|
60 | flow=gh,u |
---|
61 | |
---|
62 | for i in range(20): |
---|
63 | if True: |
---|
64 | step.next() |
---|
65 | gh,u = step.mass, step.u |
---|
66 | print i, gh.shape, gh.min(), gh.max(), u.min(), u.max() |
---|
67 | mesh.plot_i(gh-Phi0) ; plt.title('err(gh)'); |
---|
68 | plt.savefig('fig_RSW2_MPAS_W02/err_gh_%02d.png'%i) |
---|
69 | plt.close() |
---|
70 | else: |
---|
71 | # advance by one time step using dynamico_ARK |
---|
72 | gh,u = step.mass.copy(), step.u.copy() |
---|
73 | print i, gh.shape, gh.min(), gh.max(), u.min(), u.max() |
---|
74 | step.next() |
---|
75 | gh_now,u_now = step.mass.copy(), step.u.copy() |
---|
76 | dmass,dhs,du_slow,du_fast = step.drhodz[:], step.dtheta_rhodz[:], step.du_slow[:], step.du_fast[:] |
---|
77 | du=du_slow+du_fast |
---|
78 | |
---|
79 | # do the same using caldyn, obtain fast/slow tendencies first |
---|
80 | gh_ref, uref=flow |
---|
81 | junk, fast, slow = caldyn.bwd_fast_slow(flow,0.) |
---|
82 | dmass_ref, duslow_ref = slow |
---|
83 | junk, dufast_ref = fast |
---|
84 | du_ref = duslow_ref+dufast_ref |
---|
85 | flow=scheme.next(flow) |
---|
86 | gh_nowref, u_nowref=flow |
---|
87 | # mesh.plot_i(gh) ; plt.title('gh'); |
---|
88 | # mesh.plot_i(Phi0) ; plt.title('gh'); |
---|
89 | fig = plt.figure(figsize=(6, 8)) |
---|
90 | f, ((ax1, ax2), (ax3, ax4), (ax5,ax6), (ax7,ax8)) = plt.subplots(4,2) |
---|
91 | # mesh.plot_i(dmass-dhs) ; plt.title('dt*d(gh)/dt'); |
---|
92 | ax1.scatter(duslow_ref,du_slow-duslow_ref) ; ax1.set_title('du_slow'); |
---|
93 | ax2.scatter(dufast_ref,du_fast-dufast_ref) ; ax2.set_title('du_fast'); |
---|
94 | ax3.scatter(du_ref,du-du_ref) ; ax3.set_title('du'); |
---|
95 | ax4.scatter(gh_ref,gh-gh_ref) ; ax4.set_title('gh'); |
---|
96 | ax5.scatter(uref, u-uref) ; ax5.set_title('u'); |
---|
97 | ax6.scatter(dmass_ref,dmass-dmass_ref) ; ax6.set_title('dmass'); |
---|
98 | ax7.scatter(gh_nowref,gh_now-gh_nowref) ; ax7.set_title('gh_now'); |
---|
99 | ax8.scatter(u_nowref,u_now-u_nowref) ; ax8.set_title('u_now'); |
---|
100 | f.subplots_adjust(hspace=1.) |
---|
101 | plt.savefig('fig_RSW2_MPAS_W02/scatter_%02d.png'%i) |
---|
102 | plt.close() |
---|
103 | |
---|
104 | print 'Time spent in DYNAMICO (s) : ', unst.getvar('elapsed') |
---|