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

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

devel/Python : introduced Python/dynamico/dev/ subdirectory

File size: 2.4 KB
Line 
1from __future__ import print_function
2
3print('Loading DYNAMICO modules ...')
4from dynamico import unstructured as unst
5from dynamico.precision import asnum
6from dynamico.dev.meshes import MPAS_Format, Unstructured_Mesh
7from dynamico.dev.numba import jit
8from dynamico import time_step
9print('...Done')
10
11print('Loading modules ...')
12import math as math
13import matplotlib.pyplot as plt
14import numpy as np
15from numba import int32, float64
16import numba
17import time
18
19print('...Done')
20
21grid, llm, nqdyn = 10242, 1,1 # 2562, 10242, 40962
22Omega, radius, g = 2.*np.pi/86400., 6.4e6, 1.
23N, T, courant = 10, 10800., 0.5 # simulation length = N*T
24
25def f(lon,lat): return 2*Omega*np.sin(lat) # Coriolis parameter
26print('Reading MPAS mesh ...')
27meshfile = MPAS_Format('grids/x1.%d.grid.nc'%grid)
28mesh=Unstructured_Mesh(meshfile, llm, nqdyn, radius, f)
29print('...Done')
30lon, lat = mesh.lon_i, mesh.lat_i
31x,y,z = np.cos(lat)*np.cos(lon), np.cos(lat)*np.sin(lon), np.sin(lat)
32
33u0 = Omega*radius/12. # cf Williamson (1991), p.13
34gh1 = radius*Omega*u0+.5*u0**2
35print('u0=', u0)
36ulon = u0*np.cos(mesh.lat_e)
37
38dx = mesh.de.min()
39dt = courant*dx/u0
40nt = int(math.ceil(T/dt))
41dt = T/nt
42print(dx, dt, dt*u0/dx)
43print(T, nt)
44
45q0 = np.exp(-32.*(x+1.)**2)
46q0, u0 = asnum([q0, mesh.ucov2D(ulon,0.*ulon)])
47
48@jit
49def 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
61def 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   
71q = q0
72flux = mesh.field_u()
73
74mesh_data = mesh.get_data()
75
76for 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
87print('Time spent in DYNAMICO (s) : ', unst.getvar('elapsed'))
Note: See TracBrowser for help on using the repository browser.