Changeset 689 for codes/icosagcm/devel


Ignore:
Timestamp:
04/09/18 15:24:15 (6 years ago)
Author:
dubos
Message:

devel/unstructured : select double or single precision for physical quantities

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

Legend:

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

    r687 r689  
    1313 
    1414radian=180/math.pi 
     15 
     16def zeros(dims): return np.zeros([n for n in dims if n>1], dtype=unst.np_num) 
    1517 
    1618#------------------- Hybrid mass-based coordinate ------------- 
     
    6870 
    6971#----------------------- Cartesian mesh ----------------------- 
    70  
    71 def squeeze(dims): return np.zeros([n for n in dims if n>1]) 
    7272 
    7373# arrays is a list of arrays 
     
    177177                  Aiv,Aiv,f+0.*Aiv,le_de,Riv2,-wee) 
    178178 
    179     def field_theta(self,n=1): return squeeze((n,self.nqdyn,self.ny,self.nx,self.llm)) 
    180     def field_mass(self,n=1): return squeeze((n,self.ny,self.nx,self.llm)) 
    181     def field_z(self,n=1): return squeeze((n,self.ny,self.nx,self.llm)) 
    182     def field_w(self,n=1): return squeeze((n,self.ny,self.nx,self.llm+1)) 
     179    def field_theta(self,n=1): return zeros((n,self.nqdyn,self.ny,self.nx,self.llm)) 
     180    def field_mass(self,n=1): return zeros((n,self.ny,self.nx,self.llm)) 
     181    def field_z(self,n=1): return zeros((n,self.ny,self.nx,self.llm)) 
     182    def field_w(self,n=1): return zeros((n,self.ny,self.nx,self.llm+1)) 
    183183    def field_u(self,n=1):  
    184184        if n==1: 
     
    186186        else: 
    187187            return np.zeros((n,self.ny,2*self.nx,self.llm)) 
    188     def field_ps(self,n=1): return squeeze((n,self.ny,self.nx)) 
     188    def field_ps(self,n=1): return zeros((n,self.ny,self.nx)) 
    189189    def ucomp(self,u): return u[:,range(0,2*self.nx,2),:] 
    190190    def set_ucomp(self,uv,u): uv[:,range(0,2*self.nx,2),:]=u 
     
    279279 
    280280class Abstract_Mesh(Base_class): 
    281     def field_theta(self,n=1): return squeeze((n,self.nqdyn,self.primal_num,self.llm)) 
    282     def field_mass(self,n=1):  return squeeze((n,self.primal_num,self.llm)) 
    283     def field_z(self,n=1):     return squeeze((n,self.dual_num,self.llm)) 
    284     def field_w(self,n=1):     return squeeze((n,self.primal_num,self.llm+1)) 
    285     def field_u(self,n=1):     return squeeze((n,self.edge_num,self.llm)) 
    286     def field_ps(self,n=1):    return squeeze((n,self.primal_num,)) 
     281    def field_theta(self,n=1): return zeros((n,self.nqdyn,self.primal_num,self.llm)) 
     282    def field_mass(self,n=1):  return zeros((n,self.primal_num,self.llm)) 
     283    def field_z(self,n=1):     return zeros((n,self.dual_num,self.llm)) 
     284    def field_w(self,n=1):     return zeros((n,self.primal_num,self.llm+1)) 
     285    def field_u(self,n=1):     return zeros((n,self.edge_num,self.llm)) 
     286    def field_ps(self,n=1):    return zeros((n,self.primal_num,)) 
    287287    def ucov2D(self, ulon, ulat):  
    288288        return self.de*(ulon*np.cos(self.angle_e)+ulat*np.sin(self.angle_e)) 
  • codes/icosagcm/devel/Python/src/functions.h

    r642 r689  
    11/* KERNELS */ 
    22 
     3#undef PREC_DOUBLE 
     4 
     5#ifdef PREC_DOUBLE 
     6typedef double number; 
     7#else 
     8typedef float number; 
     9#endif 
     10 
    311enum {max_nb_stage=5}; 
    4 extern double tauj[max_nb_stage]; 
    5 extern double cslj[max_nb_stage][max_nb_stage], cflj[max_nb_stage][max_nb_stage]; 
     12extern number tauj[max_nb_stage]; 
     13extern number cslj[max_nb_stage][max_nb_stage], cflj[max_nb_stage][max_nb_stage]; 
    614 
    715void dynamico_init_params(void); 
    816 
    917void dynamico_ARK_step(int nstep, 
    10                        double *mass_col, double *rhodz, double *theta_rhodz,  
    11                        double *u, double *geopot, double *w, 
    12                        double *theta, double *ps, double *pk, double *hflux, double *qv, 
    13                        double *dmass_col, double *drhodz, double *dtheta_rhodz, 
    14                        double *du_fast, double *du_slow, 
    15                        double *dPhi_fast, double *dPhi_slow,  
    16                        double *dW_fast, double *dW_slow); 
     18                       number *mass_col, number *rhodz, number *theta_rhodz,  
     19                       number *u, number *geopot, number *w, 
     20                       number *theta, number *ps, number *pk, number *hflux, number *qv, 
     21                       number *dmass_col, number *drhodz, number *dtheta_rhodz, 
     22                       number *du_fast, number *du_slow, 
     23                       number *dPhi_fast, number *dPhi_slow,  
     24                       number *dW_fast, number *dW_slow); 
     25 
     26void dynamico_remap(number *rhodz, number *theta_rhodz, number *u); 
    1727 
    1828/* KERNELS -> XIOS */ 
  • codes/icosagcm/devel/Python/src/unstructured.pyx

    r687 r689  
    55from util import list_stencil 
    66 
    7 from ctypes import c_void_p, c_int, c_double, c_bool 
    8  
    9 #from libc.time cimport time as ctime, time_t 
     7from ctypes import c_void_p, c_int, c_double, c_float, c_bool 
    108from numpy cimport ndarray 
    119cimport numpy as np 
     10import numpy as np 
     11 
     12#--------------   choose precision of kernel computations  ------------# 
     13 
     14DEF prec_double=False 
     15 
     16IF prec_double: 
     17   c_num=c_double 
     18   ctypedef double num 
     19   np_num=np.float64 
     20ELSE: 
     21   c_num=c_float 
     22   ctypedef float num 
     23   np_num=np.float32 
     24 
     25ctypedef num *num_ptr 
    1226 
    1327#------------- direct Cython interface to DYNAMICO routines -------------# 
     28 
    1429 
    1530cdef enum: max_nb_stage=5 
    1631cdef extern : 
    17     cdef double tauj[max_nb_stage] 
    18     cdef double cslj[max_nb_stage][max_nb_stage] 
    19     cdef double cflj[max_nb_stage][max_nb_stage] 
     32    cdef num tauj[max_nb_stage] 
     33    cdef num cslj[max_nb_stage][max_nb_stage] 
     34    cdef num cflj[max_nb_stage][max_nb_stage] 
    2035    cdef int nb_stage[1] 
    2136 
    2237cdef extern from "functions.h": 
    2338     cdef void dynamico_ARK_step(int nstep, 
    24                                  double *mass_col, double *rhodz, double *theta_rhodz,  
    25                                  double *u, double *geopot, double *w, 
    26                                  double *theta, double *ps, double *pk, double *hflux, double *qv, 
    27                                  double *dmass_col, double *drhodz, double *dtheta_rhodz, 
    28                                  double *du_fast, double *du_slow, 
    29                                  double *dPhi_fast, double *dPhi_slow,  
    30                                  double *dW_fast, double *dW_slow) 
    31      cdef void dynamico_remap(double *rhodz, double *theta_rhodz, double *u) 
     39                                 num *mass_col, num *rhodz, num *theta_rhodz,  
     40                                 num *u, num *geopot, num *w, 
     41                                 num *theta, num *ps, num *pk, num *hflux, num *qv, 
     42                                 num *dmass_col, num *drhodz, num *dtheta_rhodz, 
     43                                 num *du_fast, num *du_slow, 
     44                                 num *dPhi_fast, num *dPhi_slow,  
     45                                 num *dW_fast, num *dW_slow) 
     46     cdef void dynamico_remap(num *rhodz, num *theta_rhodz, num *u) 
    3247     cdef void dynamico_init_params() 
    3348     cpdef void dynamico_setup_xios() 
     
    5974                     ['dynamico_init_metric', c_void_p,6], 
    6075                     ['dynamico_init_hybrid', c_void_p,3], 
    61                      ['dynamico_caldyn_unstructured', c_double, c_void_p,20], 
     76                     ['dynamico_caldyn_unstructured', c_num, c_void_p,20], 
    6277                     ['dynamico_partition_graph', c_int,2, c_void_p,3, c_int, c_void_p], 
    6378                     ['dynamico_init_transfer', c_int, c_int,2,c_void_p,3, c_int,2,c_void_p,3], 
     
    7590    'dual_num','max_dual_deg','edge_num','max_trisk_deg', 
    7691    'caldyn_thermo','caldyn_eta','nb_threads', 
    77     c_double,'elapsed','g', 'ptop', 'cpp', 'cppv', 
     92    c_double,'elapsed', 
     93    c_num, 'g', 'ptop', 'cpp', 'cppv', 
    7894    'Rd', 'Rv', 'preff', 'Treff', 'pbot', 'rho_bot', 'Phi_bot') 
    7995 
     
    8298#------------------------ Extension type performing a full ARK time step ---------------------- 
    8399 
    84 ctypedef double *dbl_ptr 
    85 cdef dbl_ptr ptr1(double[:] data) : return &data[0] 
    86 cdef dbl_ptr ptr2(double[:,:] data) : return &data[0,0] 
    87 cdef dbl_ptr ptr3(double[:,:,:] data) : return &data[0,0,0] 
    88 cdef dbl_ptr ptr4(double[:,:,:,:] data) : return &data[0,0,0,0] 
    89 cdef dbl_ptr ptr(data): 
     100cdef num_ptr ptr1(num[:] data) except *: return &data[0] 
     101cdef num_ptr ptr2(num[:,:] data) except *: return &data[0,0] 
     102cdef num_ptr ptr3(num[:,:,:] data) except *: return &data[0,0,0] 
     103cdef num_ptr ptr4(num[:,:,:,:] data) except *: return &data[0,0,0,0] 
     104cdef num_ptr ptr(data) except * : 
    90105    n=data.ndim 
    91106    if n==1 : return ptr1(data) 
     
    95110    if n>4: raise IndexError 
    96111         
    97 cdef alloc(dbl_ptr *p, allocator, n=1): 
     112cdef alloc(num_ptr *p, allocator, n=1): 
    98113    data=allocator(n) 
    99114    p[0]=ptr(data) 
    100115    return data 
    101116 
    102 cdef check_ptr(name, dbl_ptr p, ndarray data): 
     117cdef check_ptr(name, num_ptr p, ndarray data): 
    103118    if p != ptr(data) : print name, 'p <> ptr(data) !!' 
    104119         
     
    107122    cdef int nstep 
    108123    # pointer to allocated arrays 
    109     cdef dbl_ptr p_mass, p_theta_rhodz, p_u, p_geopot, p_W                   # prognostic 
    110     cdef dbl_ptr p_mass_col, p_dmass_col, p_ps, p_theta, p_pk, p_hflux, p_qv # diagnostic 
    111     cdef dbl_ptr p_drhodz, p_dtheta_rhodz, p_du_fast, p_du_slow                # tendencies 
    112     cdef dbl_ptr p_dPhi_fast, p_dPhi_slow, p_dW_fast, p_dW_slow                # tendencies 
     124    cdef num_ptr p_mass, p_theta_rhodz, p_u, p_geopot, p_W                   # prognostic 
     125    cdef num_ptr p_mass_col, p_dmass_col, p_ps, p_theta, p_pk, p_hflux, p_qv # diagnostic 
     126    cdef num_ptr p_drhodz, p_dtheta_rhodz, p_du_fast, p_du_slow                # tendencies 
     127    cdef num_ptr p_dPhi_fast, p_dPhi_slow, p_dW_fast, p_dW_slow                # tendencies 
    113128    # allocated arrays, must remain referenced or segfault 
    114129    cdef readonly ndarray mass, theta_rhodz, u, geopot, W 
     
    298313# i.e. index[start:end] with start=vtxdist[cell], end=vtxdist[cell+1] lists the edges of cell 'cell' 
    299314 
    300 import numpy as np 
    301  
    302315def loc_stencil(degree, stencil): 
    303316    loc=0 
Note: See TracChangeset for help on using the changeset viewer.