Changeset 805


Ignore:
Timestamp:
02/08/19 08:10:04 (5 years ago)
Author:
dubos
Message:

devel/Python : moving Fortran-based features to dynamico.dev module (TBC)

Location:
codes/icosagcm/devel/Python
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • codes/icosagcm/devel/Python/dynamico/dev/meshes.py

    r804 r805  
    22 
    33from dynamico import meshes 
    4 from dynamico.meshes import MPAS_Format 
    5 from dynamico.dev.numba import int32, float64, store_data 
     4from dynamico.meshes import MPAS_Format, Unstructured_PMesh, compute_hybrid_coefs 
     5from dynamico import unstructured as unst 
     6from dynamico.dev import numba 
     7from dynamico.dev.numba import int32, float64 
    68 
    7 class Unstructured_Mesh(meshes.Unstructured_Mesh): 
    8     def get_data(self): 
    9         return store_data(self, 
    10                    int32,      'primal_num dual_num edge_num', 
    11                    int32[:],   'primal_deg left right', 
    12                    int32[:,:], 'primal_edge primal_ne', 
    13                    float64[:], 'le_de Ai') 
     9class MeshData(numba.NumbaData): 
     10    signature=(int32,      'primal_num dual_num edge_num', 
     11               int32[:],   'primal_deg left right', 
     12               int32[:,:], 'primal_edge primal_ne', 
     13               float64[:], 'le_de le de lon_e lat_e Ai') 
     14    def to_dynamico(self): 
     15        max_primal_deg, max_dual_deg, max_trisk_deg = [x.shape[1] for x in self.primal_edge, self.dual_edge, self.trisk] 
     16        unst.init_mesh(self.llm, self.nqdyn, self.edge_num, self.primal_num, self.dual_num, 
     17                  max_trisk_deg, max_primal_deg, max_dual_deg, 
     18                  self.primal_deg, self.primal_edge, self.primal_ne, 
     19                  self.dual_deg, self.dual_edge, self.dual_ne, self.dual_vertex, 
     20                  self.left, self.right, self.down, self.up, self.trisk_deg, self.trisk, 
     21                  self.Ai, self.Av, self.fv, self.le_de, self.Riv2, self.wee) 
     22 
     23# note inheritance order : inherit first from DevMesh to override empty to_dynamico() from AbstractMesh 
     24class Cartesian_Mesh(MeshData, meshes.Cartesian_Mesh): pass 
     25class Unstructured_Mesh(MeshData, meshes.Unstructured_Mesh): pass 
     26class Local_Mesh(MeshData, meshes.Local_Mesh): pass 
  • codes/icosagcm/devel/Python/dynamico/dev/numba.py

    r804 r805  
    66import numba 
    77 
    8 def store_data(obj,*args): 
    9     """Returns a jitclass instance containing attributes copied from obj. *args is of the form type,names,type,names ... where names is a string 'attr1 attr2 attr3' containing space-separated names of attributes of obj. Those attributes are declared to numba with the type preceding them. The result of store_data can be used as argument to a @jit function.""" 
    10     spec = [] 
    11     for item in args: 
    12         if isinstance(item,str):  
    13             spec = spec + [(name,thetype) for name in item.split(' ')] 
    14         else: 
    15             thetype=item 
    16     @numba.jitclass(spec) 
    17     class JitClass(object): 
    18         def __init__(self): pass 
    19     data=JitClass() 
    20     for name,thetype in spec: setattr(data, name, getattr(obj,name)) 
    21     return data 
     8class NumbaData(object): 
     9    """A base class to extract data from derived class instances and use it as argument to @jit functions. Derived classes must set the 'signature' attribute.""" 
     10    def data(self): 
     11        """Returns a jitclass instance containing attributes copied from self, using self.signature which is of the form type,names,type,names ... where names is a string 'attr1 attr2 attr3' containing space-separated names of attributes of self. Those attributes are declared to numba with the type preceding them. The result of data() can be used as argument to a @jit function.""" 
     12        spec = [] 
     13        for item in self.signature: 
     14            if isinstance(item,str):  
     15                spec = spec + [(name,thetype) for name in item.split(' ')] 
     16            else: 
     17                thetype=item 
     18        @numba.jitclass(spec) 
     19        class JitClass(object): 
     20            def __init__(self): pass 
     21        data=JitClass() 
     22        for name,thetype in spec: setattr(data, name, getattr(self,name)) 
     23        return data 
    2224 
    2325jit=numba.jit(nopython=True, nogil=True, error_model='numpy', fastmath=True) 
  • codes/icosagcm/devel/Python/dynamico/meshes.py

    r804 r805  
    9090    return z.transpose() 
    9191 
    92 class Cartesian_mesh(Base_class): 
     92class Cartesian_Mesh(Base_class): 
    9393    def __init__(self,nx,ny,llm,nqdyn,Lx,Ly,f): 
    9494        dx,dy = Lx/nx, Ly/ny 
     
    115115        def zeros(shape,n=1): return [np.zeros(shape) for i in range(n)] if n>1 else np.zeros(shape) 
    116116        def indices(shape,n=1): return [np.zeros(shape,np.int32) for i in range(n)] if n>1 else np.zeros(shape,np.int32) 
     117 
     118        primal_num, dual_num, edge_num = nx*ny, nx*ny, 2*nx*ny 
    117119 
    118120        Aiv, lon_i,lon_v,lat_i,lat_v   = zeros( nx*ny,   5) 
     
    178180 
    179181        Aiv[:]=dx*dy # Ai=Av=dx*dy 
    180         le_de=le/de 
    181         unst.init_mesh(llm,nqdyn,2*nx*ny,nx*ny,nx*ny,4,4,4, 
    182                   primal_deg,primal_edge,primal_ne, 
    183                   dual_deg,dual_edge,dual_ne,dual_vertex, 
    184                   left,right,down,up,trisk_deg,trisk, 
    185                   Aiv,Aiv,f+0.*Aiv,le_de,Riv2,wee) 
    186         self.set_members(locals(), 'primal_deg', 'primal_edge', 'primal_ne', 'primal_vertex', 
     182        le_de, fv = le/de, f+0.*Aiv 
     183         
     184        self.set_members(locals(), 'primal_num',  'dual_num', 'edge_num', 
     185                         'primal_deg', 'primal_edge', 'primal_ne', 'primal_vertex', 
    187186                         'dual_deg', 'dual_edge', 'dual_ne', 'dual_vertex', 
    188187                         'left','right','down','up', 
    189188                         'trisk_deg','trisk','Riv2','wee', 
    190                          'lon_i','lat_i','lon_v','lat_v', 
     189                         'lon_i','lat_i','lon_v','lat_v', 'fv', 
    191190                         'le_de','le','de','lon_e','lat_e','angle_e', 
    192191                         'primal_i','primal_j','edge_i','edge_j') 
    193192        self.Ai, self.Av = Aiv, Aiv 
     193        self.to_dynamico() 
     194    def to_dynamico(self): pass 
    194195    def ncwrite(self, name): 
    195196        """The method writes Cartesian mesh on the disc. 
     
    399400 
    400401class Abstract_Mesh(Base_class): 
     402    def to_dynamico(self): pass 
    401403    def field_theta(self,n=1): return zeros((n,self.nqdyn,self.primal_num,self.llm)) 
    402404    def field_mass(self,n=1):  return zeros((n,self.primal_num,self.llm)) 
     
    436438        self.lon_v, self.lat_v = mapping.map(self.lon_v, self.lat_v) 
    437439        self.lon_e, self.lat_e = mapping.map(self.lon_e, self.lat_e) 
    438      
     440 
    439441class Unstructured_Mesh(Abstract_Mesh): 
    440442    def __init__(self, gridfile, llm, nqdyn, radius, f): 
     
    466468                         'primal_num', 'dual_num', 'edge_num', 'primal_deg', 'primal_vertex', 'primal_ne', 
    467469                         'lat_i', 'lon_i', 'lat_v', 'lon_v', 'lat_e', 'lon_e', 'angle_e', 
    468                          'Ai', 'Av', 'fv', 'le', 'de', 'le_de', 
     470                         'Ai', 'Av', 'fv', 'le', 'de', 'down', 'up', 'le_de', 
    469471                         'trisk_deg', 'trisk', 'wee', 'Riv2', 'dual_ne', 
    470472                         'left', 'right', 
    471473                         'primal_edge') 
    472474        gridfile.normalize(self) 
    473         max_primal_deg, max_dual_deg, max_trisk_deg = [ x.shape[1] for x in primal_edge, dual_edge, trisk] 
    474         unst.init_mesh(llm, nqdyn, edge_num, primal_num,dual_num, 
    475                   max_trisk_deg, max_primal_deg, max_dual_deg, 
    476                   primal_deg,primal_edge,primal_ne, 
    477                   dual_deg,dual_edge,dual_ne,dual_vertex, 
    478                   left,right,down,up,trisk_deg,trisk, 
    479                   self.Ai,self.Av,self.fv,le_de,Riv2,wee) 
     475        self.to_dynamico() 
    480476        self.primal  = tri.Triangulation(lon_i*radian, lat_i*radian) 
    481477        self.dual    = tri.Triangulation(lon_v*radian, lat_v*radian) 
     
    604600        for edge in range(edge_num):  
    605601            if edge_deg[edge]<2: up[edge]=down[edge] 
    606         max_primal_deg, max_dual_deg, max_trisk_deg = [x.shape[1] for x in primal_edge, dual_edge, trisk] 
    607602 
    608603        # construct own primal mesh for XIOS output 
     
    638633                         'trisk_deg', 'trisk', 'wee', 
    639634                         'dual_deg', 'dual_edge', 'dual_ne', 'dual_vertex', 'Riv2', 
    640                          'left','right','primal_deg','primal_edge','primal_ne', 
     635                         'left', 'right', 'down', 'up',  
     636                         'primal_deg', 'primal_edge', 'primal_ne', 
    641637                         'vertices_V1', 'edges_E2', 
    642638                         'lon_i', 'lat_i', 'Ai', 
     
    651647        self.fv = mapping.coriolis(self.lon_v,self.lat_v) # Coriolis parameter 
    652648        # propagate info to Fortran side 
    653         unst.init_mesh(llm, nqdyn, self.edge_num, self.primal_num, self.dual_num, 
    654                   max_trisk_deg, max_primal_deg, max_dual_deg, 
    655                   primal_deg,primal_edge,primal_ne, 
    656                   dual_deg,dual_edge,dual_ne,dual_vertex, 
    657                   left,right,down,up,trisk_deg,trisk, 
    658                   self.Ai,self.Av,self.fv,self.le_de,self.Riv2,self.wee) 
    659  
     649        self.to_dynamico() 
    660650        # setup halo transfers - NB : llm must be set before we call set_dynamico_transfer 
    661651        self.com_primal = parallel.Halo_Xchange( 
     
    767757    return bounds 
    768758 
    769 class Stencil_glob: 
     759class Stencil_glob(object): 
    770760    def __init__(self, degree, neigh, weight=None): 
    771761        self.degree, self.neigh, self.weight = degree, neigh, weight 
     
    773763        return Stencil(cells, self.degree, self.neigh, neigh_dict, self.weight) 
    774764             
    775 class Stencil: 
     765class Stencil(object): 
    776766    def __init__(self, cells, degree, neigh, neigh_dict, weight=None): 
    777767        get = degree.dim.getter(cells) 
  • codes/icosagcm/devel/Python/test/py/Godunov.py

    r804 r805  
    7272flux = mesh.field_u() 
    7373 
    74 mesh_data = mesh.get_data() 
     74mesh_data = mesh.data() 
    7575 
    7676for i in range(N):     
  • codes/icosagcm/devel/Python/test/py/NH_3D_bubble.py

    r753 r805  
    33from dynamico import time_step 
    44from dynamico import DCMIP 
    5 from dynamico import meshes 
     5from dynamico.dev import meshes 
    66from dynamico import precision as prec 
    7 from dynamico.meshes import Cartesian_mesh as Mesh 
     7from dynamico.dev.meshes import Cartesian_Mesh as Mesh 
    88import math as math 
    99import matplotlib.pyplot as plt 
  • codes/icosagcm/devel/Python/test/py/RSW2_MPAS_W02.py

    r802 r805  
    1313INFO('Loading DYNAMICO modules ...') 
    1414from dynamico import unstructured as unst 
    15 from dynamico.meshes import MPAS_Format, Unstructured_PMesh as PMesh, Local_Mesh as Mesh 
     15from dynamico.dev.meshes import MPAS_Format, Unstructured_PMesh as PMesh, Local_Mesh as Mesh 
    1616from dynamico import time_step 
    1717from dynamico import maps 
Note: See TracChangeset for help on using the changeset viewer.