xref: /petsc/include/petsclandau.h (revision 4ffacfe27a72f4cdf51b68a3bbb6aed96040fb2f)
1 #if !defined(PETSCLANDAU_H)
2 #define PETSCLANDAU_H
3 
4 #include <petscdmplex.h> /*I      "petscdmplex.h"    I*/
5 #include <petscts.h>
6 
7 PETSC_EXTERN PetscErrorCode DMPlexLandauPrintNorms(Vec, PetscInt);
8 PETSC_EXTERN PetscErrorCode DMPlexLandauCreateVelocitySpace(MPI_Comm,PetscInt,const char[],Vec*,Mat*,DM*);
9 PETSC_EXTERN PetscErrorCode DMPlexLandauDestroyVelocitySpace(DM*);
10 PETSC_EXTERN PetscErrorCode DMPlexLandauAddMaxwellians(DM, Vec, PetscReal, PetscReal[], PetscReal[], PetscInt, PetscInt, PetscInt, void *);
11 PETSC_EXTERN PetscErrorCode DMPlexLandauCreateMassMatrix(DM dm, Mat *Amat);
12 PETSC_EXTERN PetscErrorCode DMPlexLandauIFunction(TS, PetscReal,Vec,Vec,Vec,void *);
13 PETSC_EXTERN PetscErrorCode DMPlexLandauIJacobian(TS, PetscReal,Vec,Vec,PetscReal,Mat,Mat,void *);
14 
15 typedef int LandauIdx;
16 
17 /* the Fokker-Planck-Landau context */
18 #if !defined(LANDAU_MAX_SPECIES)
19 #if defined(PETSC_USE_DMLANDAU_2D)
20 #define LANDAU_MAX_SPECIES 10
21 #define LANDAU_MAX_GRIDS 3
22 #else
23 #define LANDAU_MAX_SPECIES 10
24 #define LANDAU_MAX_GRIDS 3
25 #endif
26 #else
27 #define LANDAU_MAX_GRIDS 3
28 #endif
29 
30 #if !defined(LANDAU_MAX_Q)
31 #if defined(LANDAU_MAX_NQ)
32 #error"LANDAU_MAX_NQ but not LANDAU_MAX_Q. Use -DLANDAU_MAX_Q=4 for Q3 elements"
33 #endif
34 #if defined(PETSC_USE_DMLANDAU_2D)
35 #define LANDAU_MAX_Q 4
36 #else
37 #define LANDAU_MAX_Q 3
38 #endif
39 #else
40 #undef LANDAU_MAX_NQ
41 #endif
42 
43 #if defined(PETSC_USE_DMLANDAU_2D)
44 #define LANDAU_MAX_Q_FACE LANDAU_MAX_Q
45 #define LANDAU_MAX_NQ (LANDAU_MAX_Q*LANDAU_MAX_Q)
46 #define LANDAU_MAX_BATCH_SZ 256
47 #define LANDAU_DIM 2
48 #else
49 #define LANDAU_MAX_Q_FACE (LANDAU_MAX_Q*LANDAU_MAX_Q)
50 #define LANDAU_MAX_NQ (LANDAU_MAX_Q*LANDAU_MAX_Q*LANDAU_MAX_Q)
51 #define LANDAU_MAX_BATCH_SZ 32
52 #define LANDAU_DIM 3
53 #endif
54 
55 typedef enum {LANDAU_CUDA, LANDAU_KOKKOS, LANDAU_CPU} LandauDeviceType;
56 
57 // static data - will be "device" data
58 typedef struct {
59   void  *invJ;  // nip*dim*dim
60   void  *D;     // nq*nb*dim
61   void  *B;     // nq*nb
62   void  *alpha; // ns
63   void  *beta;  // ns
64   void  *invMass; // ns
65   void  *w; // nip
66   void  *x; // nip
67   void  *y; // nip
68   void  *z; // nip
69   void  *Eq_m; // ns - dynamic
70   void  *f; //  nip*Nf - dynamic (IP)
71   void  *dfdx; // nip*Nf - dynamic (IP)
72   void  *dfdy; // nip*Nf - dynamic (IP)
73   void  *dfdz; // nip*Nf - dynamic (IP)
74   int   dim_,ns_,nip_,nq_,nb_;
75   void  *NCells; // remove and ise elem_offset - TODO
76   void  *species_offset; // for each grid, but same for all batched vertices
77   void  *mat_offset; // for each grid, but same for all batched vertices
78   void  *elem_offset; // for each grid, but same for all batched vertices
79   void  *ip_offset; // for each grid, but same for all batched vertices
80   void  *ipf_offset; // for each grid, but same for all batched vertices
81   void  *ipfdf_data; // for each grid, but same for all batched vertices
82   void  *maps; // for each grid, but same for all batched vertices
83   // COO
84   void             *coo_elem_offsets;
85   void             *coo_elem_point_offsets;
86   void             *coo_elem_fullNb;
87   void             *coo_vals;
88   LandauIdx        coo_n_cellsTot;
89   LandauIdx        coo_size;
90   LandauIdx        coo_max_fullnb;
91 } LandauStaticData;
92 
93 typedef enum {LANDAU_EX2_TSSOLVE, LANDAU_MATRIX_TOTAL, LANDAU_OPERATOR, LANDAU_JACOBIAN_COUNT, LANDAU_JACOBIAN, LANDAU_MASS, LANDAU_F_DF, LANDAU_KERNEL, KSP_FACTOR, KSP_SOLVE, LANDAU_NUM_TIMERS} LandauOMPTimers;
94 
95 typedef struct {
96   PetscBool      interpolate;                  /* Generate intermediate mesh elements */
97   PetscBool      gpu_assembly;
98   MPI_Comm       comm; /* global communicator to use for errors and diagnostics */
99   double         times[LANDAU_NUM_TIMERS];
100   PetscBool      use_matrix_mass;
101   /* FE */
102   PetscFE        fe[LANDAU_MAX_SPECIES];
103   /* geometry  */
104   PetscReal      radius[LANDAU_MAX_GRIDS];
105   PetscReal      re_radius;           /* RE: radius of refinement along v_perp=0, z>0 */
106   PetscReal      vperp0_radius1;      /* RE: radius of refinement along v_perp=0 */
107   PetscReal      vperp0_radius2;      /* RE: radius of refinement along v_perp=0 after origin AMR refinement */
108   PetscBool      sphere; // not used
109   PetscBool      inflate; // not used
110   PetscReal      i_radius[LANDAU_MAX_GRIDS]; // not used
111   PetscReal      e_radius; // not used
112   PetscInt       num_sections; // not used
113   /* AMR */
114   PetscBool      use_p4est;
115   PetscInt       numRERefine;        /* RE: refinement along v_perp=0, z > 0 */
116   PetscInt       nZRefine1;          /* RE: origin refinement after v_perp=0 refinement */
117   PetscInt       nZRefine2;          /* RE: origin refinement after origin AMR refinement */
118   PetscInt       numAMRRefine[LANDAU_MAX_GRIDS];  /* normal AMR - refine from origin */
119   PetscInt       postAMRRefine[LANDAU_MAX_GRIDS]; /* uniform refinement of AMR */
120   /* relativistic */
121   PetscBool      use_energy_tensor_trick;
122   PetscBool      use_relativistic_corrections;
123   /* physics */
124   PetscReal      thermal_temps[LANDAU_MAX_SPECIES];
125   PetscReal      masses[LANDAU_MAX_SPECIES];  /* mass of each species  */
126   PetscReal      charges[LANDAU_MAX_SPECIES]; /* charge of each species  */
127   PetscReal      n[LANDAU_MAX_SPECIES];       /* number density of each species  */
128   PetscReal      m_0;      /* reference mass */
129   PetscReal      v_0;      /* reference velocity */
130   PetscReal      n_0;      /* reference number density */
131   PetscReal      t_0;      /* reference time */
132   PetscReal      Ez;
133   PetscReal      epsilon0;
134   PetscReal      k;
135   PetscReal      lnLam;
136   PetscReal      electronShift;
137   PetscInt       num_species;
138   PetscInt       num_grids;
139   PetscInt       species_offset[LANDAU_MAX_GRIDS+1]; // for each grid, but same for all batched vertices
140   PetscInt       mat_offset[LANDAU_MAX_GRIDS+1]; // for each grid, but same for all batched vertices
141   // batching
142   PetscBool      jacobian_field_major_order; // this could be a type but lets not get pedantic
143   VecScatter     plex_batch;
144   Vec            work_vec;
145   IS             batch_is;
146   PetscErrorCode (*seqaij_mult)(Mat,Vec,Vec);
147   PetscErrorCode (*seqaij_multtranspose)(Mat,Vec,Vec);
148   PetscErrorCode (*seqaij_solve)(Mat,Vec,Vec);
149   PetscErrorCode (*seqaij_getdiagonal)(Mat,Vec);
150   /* COO */
151   PetscBool        coo_assembly;
152   /* cache */
153   Mat              J;
154   Mat              M;
155   Vec              X;
156   /* derived type */
157   void             *data;
158   /* computing */
159   LandauDeviceType deviceType;
160   DM               pack;
161   DM               plex[LANDAU_MAX_GRIDS];
162   LandauStaticData SData_d; /* static geometric data on device */
163   /* diagnostics */
164   PetscInt         verbose;
165   PetscLogEvent    events[20];
166   PetscLogStage    stage;
167   PetscObjectState norm_state;
168   PetscInt         batch_sz;
169   PetscInt         batch_view_idx;
170 } LandauCtx;
171 
172 #define LANDAU_SPECIES_MAJOR
173 #if !defined(LANDAU_SPECIES_MAJOR)
174 #define LAND_PACK_IDX(_b,_g) (_b*ctx->num_grids + _g)
175 #define LAND_MOFFSET(_b,_g,_nbch,_ngrid,_mat_off) (_b*_mat_off[_ngrid] + _mat_off[_g])
176 #else
177 #define LAND_PACK_IDX(_b,_g) (_g*ctx->batch_sz + _b)
178 #define LAND_MOFFSET(_b,_g,_nbch,_ngrid,_mat_off) (_nbch*_mat_off[_g] + _b*(_mat_off[_g+1] - _mat_off[_g]))
179 #endif
180 
181 typedef struct {
182   PetscReal scale;
183   LandauIdx gid;   // Landau matrix index (<10,000)
184 } pointInterpolationP4est;
185 typedef struct _lP4estVertexMaps {
186   LandauIdx                (*gIdx)[LANDAU_MAX_SPECIES][LANDAU_MAX_NQ]; // #elems *  LANDAU_MAX_NQ (spoof for max , Nb) on device,
187   LandauIdx                num_elements;
188   LandauIdx                num_reduced;
189   LandauIdx                num_face;  // (Q or Q^2 for 3D)
190   LandauDeviceType         deviceType;
191   PetscInt                 Nf;
192   pointInterpolationP4est (*c_maps)[LANDAU_MAX_Q_FACE];
193   struct _lP4estVertexMaps*d_self;
194   void                    *vp1,*vp2,*vp3;
195   PetscInt                numgrids;
196 } P4estVertexMaps;
197 
198 PETSC_EXTERN PetscErrorCode LandauCreateColoring(Mat, DM, PetscContainer *);
199 #if defined(PETSC_HAVE_CUDA)
200 PETSC_EXTERN PetscErrorCode LandauCUDAJacobian(DM[], const PetscInt, const PetscInt, const PetscInt, const PetscInt[], PetscReal[], PetscScalar[], const PetscScalar[],
201                                                const LandauStaticData *, const PetscReal, const PetscLogEvent[],const PetscInt[], const PetscInt[], Mat[], Mat);
202 PETSC_EXTERN PetscErrorCode LandauCUDACreateMatMaps(P4estVertexMaps *, pointInterpolationP4est (*)[LANDAU_MAX_Q_FACE], PetscInt[], PetscInt, PetscInt);
203 PETSC_EXTERN PetscErrorCode LandauCUDADestroyMatMaps(P4estVertexMaps *, PetscInt);
204 PETSC_EXTERN PetscErrorCode LandauCUDAStaticDataSet(DM, const PetscInt, const PetscInt, const PetscInt, PetscInt[], PetscInt[], PetscInt[], PetscReal [], PetscReal [], PetscReal[], PetscReal[], PetscReal[], PetscReal[], PetscReal[],
205                                                     PetscReal[], LandauStaticData *);
206 PETSC_EXTERN PetscErrorCode LandauCUDAStaticDataClear(LandauStaticData *);
207 #endif
208 #if defined(PETSC_HAVE_KOKKOS)
209 PETSC_EXTERN PetscErrorCode LandauKokkosJacobian(DM[], const PetscInt, const PetscInt, const PetscInt, const PetscInt[], PetscReal[], PetscScalar[],  const PetscScalar[],
210                                                  const LandauStaticData *, const PetscReal, const PetscLogEvent[], const PetscInt[], const PetscInt[], Mat[], Mat);
211 PETSC_EXTERN PetscErrorCode LandauKokkosCreateMatMaps(P4estVertexMaps *, pointInterpolationP4est (*)[LANDAU_MAX_Q_FACE], PetscInt[], PetscInt, PetscInt);
212 PETSC_EXTERN PetscErrorCode LandauKokkosDestroyMatMaps(P4estVertexMaps *, PetscInt);
213 PETSC_EXTERN PetscErrorCode LandauKokkosStaticDataSet(DM, const PetscInt, const PetscInt, const PetscInt, PetscInt[], PetscInt[], PetscInt[], PetscReal [], PetscReal [], PetscReal[], PetscReal[], PetscReal[], PetscReal[], PetscReal[],
214                                                       PetscReal[], LandauStaticData *);
215 PETSC_EXTERN PetscErrorCode LandauKokkosStaticDataClear(LandauStaticData*);
216 #endif
217 
218 #endif /* PETSCLANDAU_H */
219