source: codes/icosagcm/devel/Python/test/py/RSW_2D_mesh.py @ 747

Last change on this file since 747 was 747, checked in by dubos, 6 years ago

devel/unstructured : more fixes to mixed precision

File size: 4.3 KB
Line 
1from dynamico import meshes
2from dynamico import unstructured as unst
3from dynamico import time_step
4
5import math as math
6import matplotlib.pyplot as plt
7import numpy as np
8import netCDF4 as cdf
9
10class DYNAMICO_Format(meshes.Mesh_Format):
11    """ Netcdf input file handling.
12    Creates a dictionary of names corresponding to original data file."""
13
14    start_index=1 # indexing starts at 1 as in Fortran
15    translate = { 'down_up':'edge_down_up', 'left_right':'edge_left_right' } #new name is edge_left_right
16
17    def __init__(self,gridfilename):
18        try:
19            self.nc = cdf.Dataset(gridfilename, "r")
20            print('#NC4: Opening file:',gridfilename)
21        except RuntimeError:
22            print """ Unable to open grid file %s, maybe you forgot to download it ?
23            To do so, go to the 'Python/' dir and execute './get_MPAS_grids.sh'.
24            """ % gridfile
25            raise
26    def get_pdim(self,comm,name):
27        if name in self.translate: name = self.translate[name]
28        return parallel.PDim(self.nc.dimensions[name], comm)
29    def getvar(self,name):
30        if name in self.translate: name = self.translate[name]
31        return self.nc.variables[name][:]
32    def get_pvar(self,pdim,name):
33        # provides parallel access to a NetCDF variable, with name translation
34        # first deal with special cases
35        if name == 'edge_deg': return parallel.CstPArray1D(pdim, np.int32, 2)
36        # general case
37        if name in self.translate: name = self.translate[name]
38        return parallel.PArray(pdim, self.nc.variables[name])
39    def normalize(self, mesh, radius): pass
40
41#--------------------------------- Main program -----------------------------
42
43nx,ny,llm,nqdyn=128,128,1,1
44Lx,Ly,g,f = 8.,8.,1.,1.
45dx,dy=Lx/nx,Ly/ny
46
47filename, llm, nqdyn, g, f, radius = 'cart128.nc', 1, 1, 1., 1., None
48unst.setvar('g',g)
49
50print 'Reading Cartesian mesh ...'
51meshfile = DYNAMICO_Format(filename)
52#print("#NC4: check point: 0")
53#print(meshfile)
54
55def coriolis(lon,lat):
56   return f+0.*lon
57
58mesh=meshes.Unstructured_Mesh(meshfile, llm, nqdyn, radius, coriolis)
59caldyn = unst.Caldyn_RSW(mesh)
60#print(caldyn.__dict__.keys())
61
62xx = (mesh.lat_i-nx/2.)*dx
63yy = (mesh.lon_i-ny/2.)*dy
64
65x1,x2,yy = xx-1., xx+1., yy
66u0 = mesh.field_u()  # flow initally at rest
67h0 = 1+0.1*(np.exp(-2.*(x1*x1+yy*yy))+
68            np.exp(-2.*(x2*x2+yy*yy)))
69flow0=meshes.asnum([h0,u0])
70
71#print('h0 : ', h0[1234]-1.)
72#print('h0 : ', h0[2345]-1.)
73
74cfl = .8
75dt = cfl/math.sqrt((nx/Lx)**2+(ny/Ly)**2)
76
77T=1.
78N=int(T/dt)+1
79dt=T/N
80print N,dt,Lx/nx
81#scheme = time_step.ARK2(caldyn.bwd_fast_slow, dt, precision=unst.np_num)
82scheme = time_step.RK4(caldyn.bwd_fast_slow, dt, precision=unst.np_num)
83
84flow=flow0
85
86def minmax(name, x): print('Min/max %s :'%name, x.min(), x.max() )
87def reshape(data): return data.reshape((nx,ny))
88
89x, y = map(reshape, (xx,yy) )
90
91for i in range(10):
92    h,u=flow
93    flow, fast, slow = caldyn.bwd_fast_slow(flow, unst.zero_num)
94    junk, du_fast = fast
95    dh, du_slow = slow
96#    minmax('lon',mesh.lon_i)
97#    minmax('lat',mesh.lat_i)
98#    minmax('x',xx)
99#    minmax('y',yy)
100    minmax('PV', caldyn.qv-1.)
101#    minmax('geopot', caldyn.geopot)
102#    minmax('du_fast', du_fast)
103    plt.figure(); plt.pcolor(x,y,reshape(caldyn.qv))
104    plt.colorbar(); plt.title('potential vorticity')
105    plt.savefig('fig_RSW_2D_mesh/%03d.png'%i)
106    plt.close()
107#    plt.figure(); plt.pcolor(mesh.x,mesh.y,h)
108#    plt.colorbar(); plt.title('h')
109#    plt.show()
110#    plt.pcolor(x,y,vcomp(u)/dx)
111#    plt.colorbar(); plt.title('v')
112#    plt.show()
113    for j in range(5):
114        unst.elapsed=0.
115        flow = scheme.advance(flow,N)
116#        flow = scheme.advance(flow,5)
117        # flops
118        # mass flux : 1add+1mul per edge          =>  4
119        # div U     : 4 add per cell              =>  4
120        # KE        : 4*(2add+1mul) per cell      => 12
121        # grad KE   : 1 add per edge              =>  2
122        # grad h    : 1 add per edge              =>  2
123        # qv    : 4+4+1 add +4mul + 1div per cell => 10
124        # qu    : 1add+1mul per edge              =>  4
125        # TrisK : 4add+4mul+4add+1add per edge    => 26
126        # Total :                                    64 FLOPS/cell
127        print i,j, unst.elapsed, 100.*(1.-unst.getvar('elapsed')/unst.elapsed)
128        print 'GFlops', 64*(N*nx*ny)/unst.elapsed/1e9
129        unst.setvar('elapsed',0.)
Note: See TracBrowser for help on using the repository browser.