[804] | 1 | from __future__ import print_function |
---|
| 2 | |
---|
| 3 | print('Loading DYNAMICO modules ...') |
---|
| 4 | from dynamico import unstructured as unst |
---|
| 5 | from dynamico.precision import asnum |
---|
| 6 | from dynamico.dev.meshes import MPAS_Format, Unstructured_Mesh |
---|
| 7 | from dynamico.dev.numba import jit |
---|
| 8 | from dynamico import time_step |
---|
| 9 | print('...Done') |
---|
| 10 | |
---|
| 11 | print('Loading modules ...') |
---|
| 12 | import math as math |
---|
| 13 | import matplotlib.pyplot as plt |
---|
| 14 | import numpy as np |
---|
| 15 | from numba import int32, float64 |
---|
| 16 | import numba |
---|
| 17 | import time |
---|
| 18 | |
---|
| 19 | print('...Done') |
---|
| 20 | |
---|
| 21 | grid, llm, nqdyn = 10242, 1,1 # 2562, 10242, 40962 |
---|
| 22 | Omega, radius, g = 2.*np.pi/86400., 6.4e6, 1. |
---|
| 23 | N, T, courant = 10, 10800., 0.5 # simulation length = N*T |
---|
| 24 | |
---|
| 25 | def f(lon,lat): return 2*Omega*np.sin(lat) # Coriolis parameter |
---|
| 26 | print('Reading MPAS mesh ...') |
---|
| 27 | meshfile = MPAS_Format('grids/x1.%d.grid.nc'%grid) |
---|
| 28 | mesh=Unstructured_Mesh(meshfile, llm, nqdyn, radius, f) |
---|
| 29 | print('...Done') |
---|
| 30 | lon, lat = mesh.lon_i, mesh.lat_i |
---|
| 31 | x,y,z = np.cos(lat)*np.cos(lon), np.cos(lat)*np.sin(lon), np.sin(lat) |
---|
| 32 | |
---|
| 33 | u0 = Omega*radius/12. # cf Williamson (1991), p.13 |
---|
| 34 | gh1 = radius*Omega*u0+.5*u0**2 |
---|
| 35 | print('u0=', u0) |
---|
| 36 | ulon = u0*np.cos(mesh.lat_e) |
---|
| 37 | |
---|
| 38 | dx = mesh.de.min() |
---|
| 39 | dt = courant*dx/u0 |
---|
| 40 | nt = int(math.ceil(T/dt)) |
---|
| 41 | dt = T/nt |
---|
| 42 | print(dx, dt, dt*u0/dx) |
---|
| 43 | print(T, nt) |
---|
| 44 | |
---|
| 45 | q0 = np.exp(-32.*(x+1.)**2) |
---|
| 46 | q0, u0 = asnum([q0, mesh.ucov2D(ulon,0.*ulon)]) |
---|
| 47 | |
---|
| 48 | @jit |
---|
| 49 | def upwind(mesh,u,q,flux): |
---|
| 50 | # compute upwind fluxes |
---|
| 51 | for edge in range(mesh.edge_num): |
---|
| 52 | left = mesh.left[edge]-1 |
---|
| 53 | right = mesh.right[edge]-1 |
---|
| 54 | ue = u[edge]*mesh.le_de[edge] |
---|
| 55 | if ue>0: |
---|
| 56 | flux[edge]=ue*q[left] |
---|
| 57 | else: |
---|
| 58 | flux[edge]=ue*q[right] |
---|
| 59 | |
---|
| 60 | @jit |
---|
| 61 | def advance(mesh, scheme, u, q, flux): |
---|
| 62 | scheme(mesh,u,q,flux) |
---|
| 63 | # advance by -divergence of flux |
---|
| 64 | for cell in range(mesh.primal_num): |
---|
| 65 | div=0. |
---|
| 66 | for iedge in range(mesh.primal_deg[cell]): |
---|
| 67 | edge = mesh.primal_edge[cell,iedge]-1 |
---|
| 68 | div = div + flux[edge]*mesh.primal_ne[cell,iedge] |
---|
| 69 | q[cell] = q[cell] - dt*div/mesh.Ai[cell] |
---|
| 70 | |
---|
| 71 | q = q0 |
---|
| 72 | flux = mesh.field_u() |
---|
| 73 | |
---|
[805] | 74 | mesh_data = mesh.data() |
---|
[804] | 75 | |
---|
| 76 | for i in range(N): |
---|
| 77 | mesh.plot_i(q) ; plt.title('q'); plt.xlim([0.,360.]) |
---|
| 78 | plt.savefig('fig_Godunov/q_%02d.png'%i) |
---|
| 79 | plt.close() |
---|
| 80 | print(i) |
---|
| 81 | start_time = time.time() |
---|
| 82 | for iter in range(100): |
---|
| 83 | advance(mesh_data, upwind, u0,q,flux) |
---|
| 84 | elapsed_time = time.time() - start_time |
---|
| 85 | print('elapsed time : %g ms/step'%(1000.*elapsed_time/iter)) |
---|
| 86 | |
---|
| 87 | print('Time spent in DYNAMICO (s) : ', unst.getvar('elapsed')) |
---|