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

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

devel/unstructured : write/read Cartesian mesh as an unstructured mesh

File size: 4.1 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., 1.
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
66h0 = 1+0.1*(np.exp(-2.*(x1*x1+yy*yy))+
67            np.exp(-2.*(x2*x2+yy*yy)))
68u0 = mesh.field_u()  # flow initally at rest
69flow0=(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
81scheme = time_step.ARK2(caldyn.bwd_fast_slow, dt)
82
83flow=flow0
84
85def minmax(name, x): print('Min/max %s :'%name, x.min(), x.max() )
86def reshape(data): return data.reshape((nx,ny))
87
88x, y = map(reshape, (xx,yy) )
89
90for i in range(10):
91    h,u=flow
92    flow, fast, slow = caldyn.bwd_fast_slow(flow, 0.)
93    junk, du_fast = fast
94    dh, du_slow = slow
95#    minmax('lon',mesh.lon_i)
96#    minmax('lat',mesh.lat_i)
97#    minmax('x',xx)
98#    minmax('y',yy)
99    minmax('PV', caldyn.qv-1.)
100#    minmax('geopot', caldyn.geopot)
101#    minmax('du_fast', du_fast)
102    plt.figure(); plt.pcolor(x,y,reshape(caldyn.qv))
103    plt.colorbar(); plt.title('potential vorticity')
104    plt.savefig('fig_RSW_2D_mesh/%02d.png'%i)
105#    plt.figure(); plt.pcolor(mesh.x,mesh.y,h)
106#    plt.colorbar(); plt.title('h')
107#    plt.show()
108#    plt.pcolor(x,y,vcomp(u)/dx)
109#    plt.colorbar(); plt.title('v')
110#    plt.show()
111    for j in range(5):
112        unst.elapsed=0.
113        flow = scheme.advance(flow,N)
114        # flops
115        # mass flux : 1add+1mul per edge          =>  4
116        # div U     : 4 add per cell              =>  4
117        # KE        : 4*(2add+1mul) per cell      => 12
118        # grad KE   : 1 add per edge              =>  2
119        # grad h    : 1 add per edge              =>  2
120        # qv    : 4+4+1 add +4mul + 1div per cell => 10
121        # qu    : 1add+1mul per edge              =>  4
122        # TrisK : 4add+4mul+4add+1add per edge    => 26
123        # Total :                                    64 FLOPS/cell
124        print i,j, unst.elapsed, 100.*(1.-unst.getvar('elapsed')/unst.elapsed)
125        print 'GFlops', 64*(N*nx*ny)/unst.elapsed/1e9
126        unst.setvar('elapsed',0.)
Note: See TracBrowser for help on using the repository browser.