source: codes/icosagcm/devel/Python/dynamico/xios.py @ 783

Last change on this file since 783 was 783, checked in by jisesh, 6 years ago

devel/unstructured : fix XIOS info for curvilinear meshes

File size: 4.8 KB
Line 
1import numpy as np
2import cxios
3from unstructured import ker
4from dynamico.meshes import radian
5from mpi4py import MPI
6
7#-----------------------------------------------------------------------------
8
9def setup_curvilinear(cat,id,nx,ny,displ,index_own_i,index_own_j,lon,lat):
10    mesh = cxios.Handle(cat,id)
11    def log(name,data) : print 'setup_curvilinear : %s shape min max'%name, data.shape, data.min(), data.max()
12    log('index_own_i',index_own_i)
13    log('index_own_j',index_own_j)
14    log('lon',lon)
15    log('lat',lat)
16    mesh.set_attr(type='curvilinear',
17                  ni_glo=nx, i_index=index_own_i,
18                  nj_glo=ny, j_index=index_own_j)
19    mesh.set_attr(lonvalue_1d=lon.flatten(), latvalue_1d=lat.flatten())
20
21def cf_boundaries(degree,points,lon,lat):
22    n, nvertex = len(degree), degree.max()
23    bnd_lon, bnd_lat = np.zeros((n,nvertex)),np.zeros((n,nvertex))
24    for ij in range(n):
25        nb=degree[ij]
26        for k in range(nb):
27            vertex = points[ij,k]
28            bnd_lon[ij,k]=lon[vertex]
29            bnd_lat[ij,k]=lat[vertex]
30        for k in range(nb,nvertex):   # repeat last vertex if nb<nvertex
31            bnd_lon[ij,k]=bnd_lon[ij,nb-1]
32            bnd_lat[ij,k]=bnd_lat[ij,nb-1]
33    return bnd_lon, bnd_lat
34
35def setup_unstructured(cat,id, degree,vertex,lon,lat,lon_v,lat_v, ncell_glo, index_own_glo, displ): # cat in 'domain','domaingroup'
36    # ncell_glo (int) : global number of cells
37    # index_own_glo : global indices of cells own by this MPI process
38    # displ : min of index_own across all MPI processes
39    bnd_lon, bnd_lat = cf_boundaries(degree, vertex, lon_v, lat_v)
40    ncell, nvertex = bnd_lon.shape
41    mesh = cxios.Handle(cat,id)
42    mesh.set_attr(type='unstructured')
43    mesh.set_attr(ni_glo=ncell_glo, ni=ncell, ibegin=displ, i_index=index_own_glo)
44    mesh.set_attr(lonvalue_1d=lon, latvalue_1d=lat)
45    mesh.set_attr(nvertex=nvertex,bounds_lon_1d=bnd_lon, bounds_lat_1d=bnd_lat)
46
47#-----------------------------------------------------------------------------
48
49class Client:
50    def __enter__(self):
51        ker.dynamico_setup_xios()
52        self.comm=MPI.COMM_WORLD # FIXME : we should use the communicator returned by XIOS
53        return self
54    def __exit__(self, type, value, traceback):
55        print 'xios_finalize()'
56        cxios.finalize()
57
58class Context:
59    def __enter__(self):
60        print 'cxios.context_close_definition()'
61        cxios.context_close_definition()
62        return self
63    def __exit__(self, type, value, traceback):
64        print 'xios_context_finalize()'
65        cxios.context_finalize()
66    def init_llm(self, mesh, nqtot):
67        self.mesh=mesh
68        # vertical axis, nq
69        llm = mesh.llm
70        lev, levp1, nq = [cxios.Handle('axis',name) for name in 'lev', 'levp1', 'nq']
71        lev.set_attr(n_glo=llm, value=np.arange(llm))
72        levp1.set_attr(n_glo=llm+1, value=np.arange(llm+1))
73        nq.set_attr(n_glo=nqtot, value=np.arange(nqtot))
74    def init_calendar(self, step):
75        calendar=cxios.Handle('calendar_wrapper')
76        dtime = cxios.Duration(second=step)
77        calendar.set_attr(timestep=dtime)
78        calendar.update_timestep()
79        std=cxios.Handle('fieldgroup','standard_output')
80        std.set_attr(freq_op=dtime)
81        freq_offset = cxios.Duration(second=0)
82        std.set_attr(freq_offset=freq_offset)
83    def update_calendar(self, i):
84        ker.dynamico_xios_update_calendar(i)
85    def send_field_primal(self,name, data):
86        own_loc = self.mesh.primal_own_loc
87        if data.ndim==1:
88            data = data[own_loc]
89        if data.ndim==2:
90            data = data[own_loc,:]
91            data = data.transpose() # XIOS expects contiguous horizontal slices
92        data = np.ascontiguousarray(data)
93        cxios.send_field(name, data)
94
95class Context_Curvilinear(Context):
96    def __init__(self, mesh, nqtot, step):
97        self.init_llm(mesh, nqtot)
98        # primal mesh
99        own = mesh.primal_own_loc
100        lon_i, lat_i = [ x[own] for x in mesh.lon_i, mesh.lat_i ]
101        primal_i, primal_j = [ x[own] for x in mesh.primal_i, mesh.primal_j ]
102        setup_curvilinear('domaingroup','i', mesh.nx, mesh.ny, mesh.displ,
103                          primal_i, primal_j, lon_i, lat_i)
104        # calendar
105        self.init_calendar(step)
106
107class Context_Unstructured(Context):
108    def __init__(self, pmesh, mesh,nqtot, step):
109        self.init_llm(mesh, nqtot)
110        # primal mesh
111        lon_i, lat_i, lon_v, lat_v = [x*radian for x in mesh.lon_i, mesh.lat_i, mesh.lon_v, mesh.lat_v]
112        setup_unstructured('domaingroup','i', mesh.primal_own_deg, mesh.primal_vertex,
113                           lon_i[mesh.primal_own_loc], lat_i[mesh.primal_own_loc],
114                           lon_v, lat_v,
115                           pmesh.dim_primal.n, mesh.primal_own_glo, mesh.displ)
116        # calendar
117        self.init_calendar(step)
Note: See TracBrowser for help on using the repository browser.