1 | import time |
---|
2 | import math |
---|
3 | import numpy as np |
---|
4 | cimport numpy as np |
---|
5 | import dynamico.wrap as wrap |
---|
6 | from ctypes import c_void_p, c_int, c_double, c_bool |
---|
7 | |
---|
8 | #from libc.time cimport time as ctime, time_t |
---|
9 | from numpy cimport ndarray |
---|
10 | |
---|
11 | #------------- direct Cython interface to DYNAMICO routines -------------# |
---|
12 | |
---|
13 | cdef enum: max_nb_stage=5 |
---|
14 | cdef extern : |
---|
15 | cdef double tauj[max_nb_stage] |
---|
16 | cdef double cslj[max_nb_stage][max_nb_stage] |
---|
17 | cdef double cflj[max_nb_stage][max_nb_stage] |
---|
18 | cdef int nb_stage[1] |
---|
19 | |
---|
20 | cdef extern from "functions.h": |
---|
21 | cdef void dynamico_ARK_step(int nstep, |
---|
22 | double *mass_col, double *rhodz, double *theta_rhodz, |
---|
23 | double *u, double *geopot, double *w, |
---|
24 | double *theta, double *ps, double *pk, double *hflux, double *qv, |
---|
25 | double *dmass_col, double *drhodz, double *dtheta_rhodz, |
---|
26 | double *du_fast, double *du_slow, |
---|
27 | double *dPhi_fast, double *dPhi_slow, |
---|
28 | double *dW_fast, double *dW_slow) |
---|
29 | cdef void dynamico_init_params() |
---|
30 | cpdef void dynamico_setup_xios() |
---|
31 | cpdef void dynamico_xios_set_timestep(double) |
---|
32 | cpdef void dynamico_xios_update_calendar(int) |
---|
33 | |
---|
34 | #------------- import and wrap DYNAMICO routines -------------# |
---|
35 | |
---|
36 | ker=wrap.Struct() # store imported fun X as funs.X |
---|
37 | |
---|
38 | check_args = False # use True instead of False for debugging, probably with some overhead |
---|
39 | |
---|
40 | try: |
---|
41 | kernels = wrap.SharedLib(vars(ker), 'libicosa.so', check_args=check_args) |
---|
42 | setvar, setvars, getvar, getvars = kernels.setvar, kernels.setvars, kernels.getvar, kernels.getvars |
---|
43 | except OSError: |
---|
44 | print """ |
---|
45 | Unable to load shared library 'libkernels.so' ! |
---|
46 | """ |
---|
47 | raise |
---|
48 | |
---|
49 | # providing a full prototype enables type-checking when calling |
---|
50 | # if a number n is present in the prototype, the previous type is repeated n times |
---|
51 | kernels.import_funs([ |
---|
52 | ['dynamico_setup_xios',None], |
---|
53 | ['dynamico_xios_set_timestep',c_double], |
---|
54 | ['dynamico_xios_update_calendar',c_int], |
---|
55 | ['dynamico_init_mesh',c_void_p,13], |
---|
56 | ['dynamico_init_metric', c_void_p,6], |
---|
57 | ['dynamico_init_hybrid', c_void_p,3], |
---|
58 | ['dynamico_caldyn_unstructured', c_double, c_void_p,20], |
---|
59 | ['dynamico_partition_graph', c_int,2, c_void_p,3, c_int, c_void_p], |
---|
60 | ]) |
---|
61 | |
---|
62 | # set/get global variables |
---|
63 | eta_mass,eta_lag=(1,2) |
---|
64 | thermo_theta,thermo_entropy,thermo_moist,thermo_boussinesq=(1,2,3,4) |
---|
65 | |
---|
66 | kernels.addvars( |
---|
67 | c_bool,'hydrostatic','debug_hevi_solver', |
---|
68 | c_int,'llm','nqdyn','primal_num','max_primal_deg', |
---|
69 | 'dual_num','max_dual_deg','edge_num','max_trisk_deg', |
---|
70 | 'caldyn_thermo','caldyn_eta','nb_threads', |
---|
71 | c_double,'elapsed','g', 'ptop', 'cpp', 'cppv', |
---|
72 | 'Rd', 'Rv', 'preff', 'Treff', 'pbot', 'rho_bot', 'Phi_bot') |
---|
73 | |
---|
74 | elapsed=0. |
---|
75 | |
---|
76 | #------------------------ Extension type performing a full ARK time step ---------------------- |
---|
77 | |
---|
78 | ctypedef double *dbl_ptr |
---|
79 | cdef dbl_ptr ptr1(double[:] data) : return &data[0] |
---|
80 | cdef dbl_ptr ptr2(double[:,:] data) : return &data[0,0] |
---|
81 | cdef dbl_ptr ptr3(double[:,:,:] data) : return &data[0,0,0] |
---|
82 | cdef dbl_ptr ptr4(double[:,:,:,:] data) : return &data[0,0,0,0] |
---|
83 | cdef dbl_ptr ptr(data): |
---|
84 | n=data.ndim |
---|
85 | if n==1 : return ptr1(data) |
---|
86 | if n==2 : return ptr2(data) |
---|
87 | if n==3 : return ptr3(data) |
---|
88 | if n==4 : return ptr4(data) |
---|
89 | if n>4: raise IndexError |
---|
90 | |
---|
91 | cdef alloc(dbl_ptr *p, allocator, n=1): |
---|
92 | data=allocator(n) |
---|
93 | p[0]=ptr(data) |
---|
94 | return data |
---|
95 | |
---|
96 | cdef check_ptr(name, dbl_ptr p, ndarray data): |
---|
97 | if p != ptr(data) : print name, 'p <> ptr(data) !!' |
---|
98 | |
---|
99 | cdef class Caldyn_step: |
---|
100 | # number of time steps to do at each invocation of advance() |
---|
101 | cdef int nstep |
---|
102 | # pointer to allocated arrays |
---|
103 | cdef dbl_ptr p_mass, p_theta_rhodz, p_u, p_geopot, p_W # prognostic |
---|
104 | cdef dbl_ptr p_mass_col, p_dmass_col, p_ps, p_theta, p_pk, p_hflux, p_qv # diagnostic |
---|
105 | cdef dbl_ptr p_drhodz, p_dtheta_rhodz, p_du_fast, p_du_slow # tendencies |
---|
106 | cdef dbl_ptr p_dPhi_fast, p_dPhi_slow, p_dW_fast, p_dW_slow # tendencies |
---|
107 | # allocated arrays, must remain referenced or segfault |
---|
108 | cdef readonly ndarray mass, theta_rhodz, u, geopot, W |
---|
109 | cdef readonly ndarray mass_col, dmass_col, ps, theta, pk, hflux, qv |
---|
110 | cdef readonly ndarray drhodz, dtheta_rhodz, du_fast, du_slow |
---|
111 | cdef readonly ndarray dPhi_fast, dPhi_slow, dW_fast, dW_slow |
---|
112 | |
---|
113 | def __init__(self,mesh,time_scheme, nstep): |
---|
114 | self.nstep=nstep |
---|
115 | # self.mesh=mesh |
---|
116 | fps, ftheta, fmass = mesh.field_ps, mesh.field_theta, mesh.field_mass |
---|
117 | fw, fu, fz = mesh.field_w, mesh.field_u, mesh.field_z |
---|
118 | # collect coefficients of time scheme |
---|
119 | cdef double[:] tauj_ = time_scheme.tauj |
---|
120 | cdef double[:,:] cslj_ = time_scheme.csjl |
---|
121 | cdef double[:,:] cflj_ = time_scheme.cfjl |
---|
122 | ns = time_scheme.nstage |
---|
123 | nb_stage[0]=ns |
---|
124 | |
---|
125 | cdef int i,j |
---|
126 | for i in range(ns): |
---|
127 | tauj[i]=tauj_[i] |
---|
128 | for j in range(ns): |
---|
129 | cslj[i][j]=cslj_[i,j] |
---|
130 | cflj[i][j]=cflj_[i,j] |
---|
131 | # allocate arrays, store pointers to avoid overhead when calling dynamico |
---|
132 | # prognostic/diagnostic |
---|
133 | self.ps = alloc(&self.p_ps, fps) |
---|
134 | self.mass_col, self.dmass_col = alloc(&self.p_mass_col, fps), alloc(&self.p_dmass_col, fps,ns), |
---|
135 | self.mass, self.theta_rhodz = alloc(&self.p_mass, fmass), alloc(&self.p_theta_rhodz, fmass), |
---|
136 | self.theta, self.pk = alloc(&self.p_theta, fmass), alloc(&self.p_pk, fmass), |
---|
137 | self.geopot, self.W = alloc(&self.p_geopot, fw), alloc(&self.p_W, fw), |
---|
138 | self.hflux, self.u = alloc(&self.p_hflux, fu), alloc(&self.p_u, fu) |
---|
139 | self.qv = alloc(&self.p_qv,fz) |
---|
140 | # tendencies |
---|
141 | self.drhodz, self.dtheta_rhodz = alloc(&self.p_drhodz,fmass,ns), alloc(&self.p_dtheta_rhodz,fmass,ns) |
---|
142 | self.du_fast, self.du_slow = alloc(&self.p_du_fast,fu,ns), alloc(&self.p_du_slow,fu,ns) |
---|
143 | self.dPhi_fast, self.dPhi_slow = alloc(&self.p_dPhi_fast,fw,ns), alloc(&self.p_dPhi_slow,fw,ns) |
---|
144 | self.dW_fast, self.dW_slow = alloc(&self.p_dW_fast,fw,ns), alloc(&self.p_dW_slow,fw,ns) |
---|
145 | def next(self): |
---|
146 | # global elapsed |
---|
147 | # time1=time.time() |
---|
148 | dynamico_ARK_step(self.nstep, |
---|
149 | self.p_mass_col, self.p_mass, self.p_theta_rhodz, |
---|
150 | self.p_u, self.p_geopot, self.p_W, |
---|
151 | self.p_theta, self.p_ps, self.p_pk, self.p_hflux, self.p_qv, |
---|
152 | self.p_dmass_col, self.p_drhodz, self.p_dtheta_rhodz, |
---|
153 | self.p_du_fast, self.p_du_slow, |
---|
154 | self.p_dPhi_fast, self.p_dPhi_slow, |
---|
155 | self.p_dW_fast, self.p_dW_slow) |
---|
156 | #time2=time.time() |
---|
157 | #if time2>time1: elapsed=elapsed+time2-time1 |
---|
158 | |
---|
159 | def caldyn_step_TRSW(mesh,time_scheme,nstep): |
---|
160 | setvars(('hydrostatic','caldyn_thermo','caldyn_eta'), |
---|
161 | (True,thermo_boussinesq,eta_lag)) |
---|
162 | dynamico_init_params() |
---|
163 | return Caldyn_step(mesh,time_scheme, nstep) |
---|
164 | def caldyn_step_HPE(mesh,time_scheme,nstep, caldyn_thermo,caldyn_eta, thermo,BC,g): |
---|
165 | setvars(('hydrostatic','caldyn_thermo','caldyn_eta', |
---|
166 | 'g','ptop','Rd','cpp','preff','Treff'), |
---|
167 | (True,caldyn_thermo,caldyn_eta, |
---|
168 | g,BC.ptop,thermo.Rd,thermo.Cpd,thermo.p0,thermo.T0)) |
---|
169 | dynamico_init_params() |
---|
170 | return Caldyn_step(mesh,time_scheme, nstep) |
---|
171 | def caldyn_step_NH(mesh,time_scheme,nstep, caldyn_thermo, caldyn_eta, thermo,BC,g): |
---|
172 | setvars(('hydrostatic','caldyn_thermo','caldyn_eta', |
---|
173 | 'g','ptop','Rd','cpp','preff','Treff','pbot','rho_bot'), |
---|
174 | (False,caldyn_thermo,caldyn_eta, |
---|
175 | g,BC.ptop,thermo.Rd,thermo.Cpd,thermo.p0,thermo.T0, |
---|
176 | BC.pbot.max(), BC.rho_bot.max())) |
---|
177 | dynamico_init_params() |
---|
178 | return Caldyn_step(mesh,time_scheme, nstep) |
---|
179 | |
---|
180 | #----------------------------- Base class for dynamics ------------------------ |
---|
181 | |
---|
182 | class Caldyn: |
---|
183 | def __init__(self,mesh): |
---|
184 | self.mesh=mesh |
---|
185 | fps, ftheta, fmass = mesh.field_ps, mesh.field_theta, mesh.field_mass |
---|
186 | fw, fu, fz = mesh.field_w, mesh.field_u, mesh.field_z |
---|
187 | self.ps, self.ms, self.dms = fps(), fps(), fps() |
---|
188 | self.s, self.hs, self.dhs = ftheta(), ftheta(), ftheta() |
---|
189 | self.pk, self.berni, self.geopot, self.hflux = fmass(),fmass(),fw(),fu() |
---|
190 | self.qu, self.qv = fu(),fz() |
---|
191 | self.fmass, self.ftheta, self.fu, self.fw = fmass, ftheta, fu, fw |
---|
192 | def bwd_fast_slow(self, flow, tau): |
---|
193 | global elapsed |
---|
194 | time1=time.time() |
---|
195 | flow,fast,slow = self._bwd_fast_slow_(flow,tau) |
---|
196 | time2=time.time() |
---|
197 | elapsed=elapsed+time2-time1 |
---|
198 | return flow,fast,slow |
---|
199 | |
---|
200 | # when calling caldyn_unstructured, arrays for tendencies must be re-created each time |
---|
201 | # to avoid overwriting in the same memory space when time scheme is multi-stage |
---|
202 | |
---|
203 | #-------------------------- Shallow-water dynamics --------------------- |
---|
204 | |
---|
205 | class Caldyn_RSW(Caldyn): |
---|
206 | def __init__(self,mesh): |
---|
207 | Caldyn.__init__(self,mesh) |
---|
208 | setvars(('hydrostatic','caldyn_thermo','caldyn_eta'), |
---|
209 | (True,thermo_boussinesq,eta_lag)) |
---|
210 | self.dhs = self.fmass() |
---|
211 | dynamico_init_params() |
---|
212 | def _bwd_fast_slow_(self, flow, tau): |
---|
213 | h,u = flow |
---|
214 | # h*s = h => uniform buoyancy s=1 => shallow-water |
---|
215 | dh, du_slow, du_fast, hs, buf = self.fmass(), self.fu(), self.fu(), h.copy(), self.geopot |
---|
216 | ker.dynamico_caldyn_unstructured(tau, self.ms, h, hs, u, self.geopot, buf, |
---|
217 | self.s, self.ps, self.pk, self.hflux, self.qv, |
---|
218 | self.dms, dh, self.dhs, du_fast, du_slow, |
---|
219 | buf, buf, buf, buf) |
---|
220 | return (h,u), (0.,du_fast), (dh,du_slow) |
---|
221 | |
---|
222 | #----------------------------------- HPE ------------------------------------ |
---|
223 | |
---|
224 | class Caldyn_HPE(Caldyn): |
---|
225 | def __init__(self,caldyn_thermo,caldyn_eta, mesh,thermo,BC,g): |
---|
226 | Caldyn.__init__(self,mesh) |
---|
227 | setvars(('hydrostatic','caldyn_thermo','caldyn_eta', |
---|
228 | 'g','ptop','Rd','cpp','preff','Treff'), |
---|
229 | (True,caldyn_thermo,caldyn_eta, |
---|
230 | g,BC.ptop,thermo.Rd,thermo.Cpd,thermo.p0,thermo.T0)) |
---|
231 | dynamico_init_params() |
---|
232 | def _bwd_fast_slow_(self, flow, tau): |
---|
233 | dm, dS, du_slow, du_fast, buf = self.fmass(), self.ftheta(), self.fu(), self.fu(), self.geopot |
---|
234 | m,S,u = flow |
---|
235 | ker.dynamico_caldyn_unstructured(tau, self.ms, m, S, u, self.geopot, buf, |
---|
236 | self.s, self.ps, self.pk, self.hflux, self.qv, |
---|
237 | self.dms, dm, dS, du_fast, du_slow, |
---|
238 | buf, buf, buf, buf) |
---|
239 | return (m,S,u), (0.,0.,du_fast), (dm,dS,du_slow) |
---|
240 | |
---|
241 | #----------------------------------- NH ------------------------------------ |
---|
242 | |
---|
243 | class Caldyn_NH(Caldyn): |
---|
244 | def __init__(self,caldyn_thermo,caldyn_eta, mesh,thermo,BC,g): |
---|
245 | Caldyn.__init__(self,mesh) |
---|
246 | setvars(('hydrostatic','caldyn_thermo','caldyn_eta', |
---|
247 | 'g','ptop','Rd','cpp','preff','Treff', |
---|
248 | 'pbot','rho_bot'), |
---|
249 | (False,caldyn_thermo,caldyn_eta, |
---|
250 | g,BC.ptop,thermo.Rd,thermo.Cpd,thermo.p0,thermo.T0, |
---|
251 | BC.pbot.max(), BC.rho_bot.max())) |
---|
252 | dynamico_init_params() |
---|
253 | def bwd_fast_slow(self, flow, tau): |
---|
254 | ftheta, fmass, fu, fw = self.ftheta, self.fmass, self.fu, self.fw |
---|
255 | dm, dS, du_slow, du_fast = fmass(), ftheta(), fu(), fu() |
---|
256 | dPhi_slow, dPhi_fast, dW_slow, dW_fast = fw(), fw(), fw(), fw() |
---|
257 | m,S,u,Phi,W = flow |
---|
258 | ker.dynamico_caldyn_unstructured(tau, self.ms, m, S, u, Phi, W, |
---|
259 | self.s, self.ps, self.pk, self.hflux, self.qv, |
---|
260 | self.dms, dm, dS, du_fast, du_slow, |
---|
261 | dPhi_fast, dPhi_slow, dW_fast, dW_slow) |
---|
262 | return ((m,S,u,Phi,W), (0.,0.,du_fast,dPhi_fast,dW_fast), |
---|
263 | (dm,dS,du_slow,dPhi_slow,dW_slow)) |
---|
264 | |
---|
265 | #------------------------ Copy mesh info to Fortran side ------------------- |
---|
266 | |
---|
267 | def init_mesh(llm, nqdyn, edge_num, primal_num, dual_num, |
---|
268 | max_trisk_deg, max_primal_deg, max_dual_deg, |
---|
269 | primal_nb, primal_edge, primal_ne, |
---|
270 | dual_nb,dual_edge,dual_ne,dual_vertex, |
---|
271 | left,right,down,up,trisk_deg,trisk, |
---|
272 | Ai, Av, fv, le_de, Riv2, wee): |
---|
273 | setvars( ('llm','nqdyn','edge_num','primal_num','dual_num', |
---|
274 | 'max_trisk_deg','max_primal_deg','max_dual_deg'), |
---|
275 | (llm, nqdyn, edge_num, primal_num, dual_num, |
---|
276 | max_trisk_deg, max_primal_deg, max_dual_deg) ) |
---|
277 | print('init_mesh ...') |
---|
278 | ker.dynamico_init_mesh(primal_nb,primal_edge,primal_ne, |
---|
279 | dual_nb,dual_edge,dual_ne,dual_vertex, |
---|
280 | left,right,down,up,trisk_deg,trisk) |
---|
281 | print ('...done') |
---|
282 | print('init_metric ...') |
---|
283 | ker.dynamico_init_metric(Ai,Av,fv,le_de,Riv2,wee) |
---|
284 | print ('...done') |
---|
285 | |
---|
286 | #------------------------ Mesh partitioning ------------------------ |
---|
287 | |
---|
288 | # Helper functions and interface to ParMETIS |
---|
289 | # list_stencil converts an adjacency graph from array format index[num_cells, MAX_EDGES] to compressed format |
---|
290 | # loc_stencil returns the start/end indices (vtxdist) expected by ParMETIS |
---|
291 | # i.e. index[start:end] with start=vtxdist[cell], end=vtxdist[cell+1] lists the edges of cell 'cell' |
---|
292 | |
---|
293 | def list_stencil(degree, stencil, cond=lambda x:True): |
---|
294 | for i in range(degree.size): |
---|
295 | for j in range(degree[i]): |
---|
296 | s=stencil[i,j] |
---|
297 | if cond(s): yield stencil[i,j] |
---|
298 | |
---|
299 | def loc_stencil(degree, stencil): |
---|
300 | loc=0 |
---|
301 | for i in range(degree.size): |
---|
302 | yield loc |
---|
303 | loc=loc+degree[i] |
---|
304 | yield loc |
---|
305 | |
---|
306 | def partition_mesh(degree, stencil, nparts): |
---|
307 | # arguments : PArray1D and PArray2D describing mesh, number of desired partitions |
---|
308 | dim_cell, degree, stencil = degree.dim, degree.data, stencil.data |
---|
309 | comm, vtxdist, idx_start, idx_end = dim_cell.comm, dim_cell.vtxdist, dim_cell.start, dim_cell.end |
---|
310 | mpi_rank, mpi_size = comm.Get_rank(), comm.Get_size() |
---|
311 | adjncy_loc, xadj_loc = list_stencil(degree, stencil), loc_stencil(degree, stencil) |
---|
312 | adjncy_loc, xadj_loc = [np.asarray(list(x), dtype=np.int32) for x in (adjncy_loc, xadj_loc)] |
---|
313 | owner = np.zeros(idx_end-idx_start, dtype=np.int32); |
---|
314 | ker.dynamico_partition_graph(mpi_rank, mpi_size, vtxdist, xadj_loc, adjncy_loc, nparts, owner) |
---|
315 | return owner |
---|