HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
PetscGlobalDOF.cpp
Go to the documentation of this file.
1 
6 #ifdef with_petsc
7 
8 #include <stdlib.h>
9 #include <vector>
10 #include <basic.h>
11 #include <arrayfunctions.h>
12 #include <mpivars_cpp.h>
13 #include <simulation_object.h>
14 #include <petscinterface.h>
15 
16 static int ApplyPeriodicity( int dir,
17  int ndims,
18  int *size,
20  int ghosts,
21  double *phi )
22 {
23  int bounds[ndims], index1[ndims], index2[ndims], offset[ndims],
24  done, p1 = 0, p2 = 0;
25  _ArrayCopy1D_(size,bounds,ndims); bounds[dir] = ghosts;
26 
27  done = 0; _ArraySetValue_(index1,ndims,0);
28  while (!done) {
29  _ArraySetValue_(offset,ndims,0); offset[dir] = -ghosts;
30  _ArrayIndex1DWO_(ndims,size,index1,offset,ghosts,p1);
31  _ArrayCopy1D_(index1,index2,ndims);
32  index2[dir] = index1[dir] + size[dir]-ghosts;
33  _ArrayIndex1D_(ndims,size,index2,ghosts,p2);
34 
35  phi[p1] = phi[p2];
36  _ArrayIncrementIndex_(ndims,bounds,index1,done);
37  }
38 
39  done = 0; _ArraySetValue_(index1,ndims,0);
40  while (!done) {
41  _ArraySetValue_(offset,ndims,0); offset[dir] = size[dir];
42  _ArrayIndex1DWO_(ndims,size,index1,offset,ghosts,p1);
43  _ArrayIndex1D_(ndims,size,index1,ghosts,p2);
44 
45  phi[p1] = phi[p2];
46  _ArrayIncrementIndex_(ndims,bounds,index1,done);
47  }
48  return(0);
49 }
50 
69 int PetscGlobalDOF(void* c )
70 {
71  PETScContext* ctxt = (PETScContext*) c;
73  int nsims = ctxt->nsims;
74 
75  ctxt->globalDOF.resize(nsims, nullptr);
76 
77  /* compute MPI offset */
78  std::vector<int> local_sizes(ctxt->nproc ,0);
79  local_sizes[ctxt->rank] = ctxt->npoints;
80  MPIMax_integer(local_sizes.data(),local_sizes.data(),ctxt->nproc,&sim[0].mpi.world);
81 
82  int MPIOffset = 0;
83  for (int i=0; i<ctxt->rank; i++) MPIOffset += local_sizes[i];
84 
85  int simOffset = 0;
86  for (int ns = 0; ns < nsims; ns++) {
87 
88  HyPar* solver = &(sim[ns].solver);
89  MPIVariables* mpi = &(sim[ns].mpi);
90 
91  int *dim = solver->dim_local,
92  ndims = solver->ndims,
93  ghosts = solver->ghosts,
94  npoints = solver->npoints_local,
95  npoints_wg = solver->npoints_local_wghosts,
96  nv = ndims + 1, i;
97 
98  ctxt->globalDOF[ns] = (double*) calloc( npoints_wg, sizeof(double) );
99  _ArraySetValue_(ctxt->globalDOF[ns], npoints_wg, -1.0);
100 
101  for (int i = 0; i < npoints; i++) {
102  int p = (ctxt->points[ns]+i*nv)[ndims];
103  ctxt->globalDOF[ns][p] = (double) (i + simOffset + MPIOffset);
104  }
105 
106  for (int i=0; i<ndims; i++) {
107  if (solver->isPeriodic[i]) {
108  ApplyPeriodicity(i,ndims,dim,ghosts,ctxt->globalDOF[ns]);
109  }
110  }
111  MPIExchangeBoundariesnD(ndims,1,dim,ghosts,mpi,ctxt->globalDOF[ns]);
112 
113  simOffset += npoints;
114  }
115 
116  return 0;
117 }
118 
119 #endif
std::vector< int * > points
Structure defining a simulation.
C++ declarations for MPI-related functions.
int MPIExchangeBoundariesnD(int, int, int *, int, void *, double *)
Some basic definitions and macros.
int npoints_local
Definition: hypar.h:42
Simulation object.
int MPIMax_integer(int *, int *, int, void *)
Definition: MPIMax.c:15
int ndims
Definition: hypar.h:26
Structure containing all solver-specific variables and functions.
Definition: hypar.h:23
MPI_Comm world
#define _ArrayIndex1D_(N, imax, i, ghost, index)
#define _ArrayIndex1DWO_(N, imax, i, offset, ghost, index)
Structure containing the variables for time-integration with PETSc.
#define _ArraySetValue_(x, size, value)
int * dim_local
Definition: hypar.h:37
#define _ArrayIncrementIndex_(N, imax, i, done)
static int ApplyPeriodicity(int dir, int ndims, int *size, int ghosts, double *phi)
int ghosts
Definition: hypar.h:52
Structure of MPI-related variables.
int npoints_local_wghosts
Definition: hypar.h:42
std::vector< double * > globalDOF
#define _ArrayCopy1D_(x, y, size)
Contains macros and function definitions for common array operations.
int * isPeriodic
Definition: hypar.h:162
int PetscGlobalDOF(void *c)