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

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

devel/unstructured : towards XIOS output with curvilinear mesh

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