HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
PetscJacobianMatNonzeroEntriesImpl.cpp
Go to the documentation of this file.
1 
6 #ifdef with_petsc
7 
8 #include <stdio.h>
9 #include <vector>
10 #include <arrayfunctions.h>
11 #include <simulation_object.h>
12 #include <mpivars_cpp.h>
13 #include <petscinterface.h>
14 
15 #undef __FUNCT__
16 #define __FUNCT__ "PetscJacobianMatNonzeroEntriesImpl"
17 
39  int width,
40  void *ctxt )
41 {
42  PETScContext* context = (PETScContext*) ctxt;
43  SimulationObject* sim = (SimulationObject*) context->simobj;
44 
45  PetscFunctionBegin;
46  int nsims = context->nsims;
47  /* initialize matrix to zero */
48  MatZeroEntries(Amat);
49 
50  for (int ns = 0; ns < nsims; ns++) {
51 
52  HyPar* solver( &(sim[ns].solver) );
53  MPIVariables* mpi( &(sim[ns].mpi) );
54 
55  int ndims = solver->ndims,
56  nvars = solver->nvars,
57  npoints = solver->npoints_local,
58  ghosts = solver->ghosts,
59  *dim = solver->dim_local,
60  *points = context->points[ns],
61  index[ndims],indexL[ndims],indexR[ndims],
62  rows[nvars],cols[nvars];
63 
64  std::vector<double> values(nvars*nvars, 1.0);
65 
66  /* loop through all computational points */
67  for (int n = 0; n < npoints; n++) {
68 
69  int *this_point = points + n*(ndims+1);
70  int p = this_point[ndims];
71  int index[ndims]; _ArrayCopy1D_(this_point,index,ndims);
72 
73  for (int dir = 0; dir < ndims; dir++) {
74 
75  int pg = (int) context->globalDOF[ns][p];
76  /* diagonal element */
77  for (int v=0; v<nvars; v++) { rows[v] = nvars*pg + v; cols[v] = nvars*pg + v; }
78  MatSetValues(Amat,nvars,rows,nvars,cols,values.data(),ADD_VALUES);
79 
80  for (int d = 1; d <= width; d++) {
81 
82  /* left neighbor */
83  _ArrayCopy1D_(index,indexL,ndims);
84  indexL[dir] -= d;
85  int pL; _ArrayIndex1D_(ndims,dim,indexL,ghosts,pL);
86  int pgL = (int) context->globalDOF[ns][pL];
87  if (pgL >= 0) {
88  for (int v=0; v<nvars; v++) { rows[v] = nvars*pg + v; cols[v] = nvars*pgL + v; }
89  MatSetValues(Amat,nvars,rows,nvars,cols,values.data(),ADD_VALUES);
90  }
91 
92  _ArrayCopy1D_(index,indexR,ndims);
93  indexR[dir] += d;
94  int pR; _ArrayIndex1D_(ndims,dim,indexR,ghosts,pR);
95  int pgR = (int) context->globalDOF[ns][pR];
96  /* right neighbor */
97  if (pgR >= 0) {
98  for (int v=0; v<nvars; v++) { rows[v] = nvars*pg + v; cols[v] = nvars*pgR + v; }
99  MatSetValues(Amat,nvars,rows,nvars,cols,values.data(),ADD_VALUES);
100  }
101 
102  }
103 
104  }
105  }
106  }
107 
108  MatAssemblyBegin(Amat,MAT_FINAL_ASSEMBLY);
109  MatAssemblyEnd (Amat,MAT_FINAL_ASSEMBLY);
110 
111  PetscFunctionReturn(0);
112 }
113 
114 #endif
std::vector< int * > points
int nvars
Definition: hypar.h:29
Structure defining a simulation.
C++ declarations for MPI-related functions.
int npoints_local
Definition: hypar.h:42
Simulation object.
int ndims
Definition: hypar.h:26
int PetscJacobianMatNonzeroEntriesImpl(Mat Amat, int width, void *ctxt)
Structure containing all solver-specific variables and functions.
Definition: hypar.h:23
#define _ArrayIndex1D_(N, imax, i, ghost, index)
Structure containing the variables for time-integration with PETSc.
int * dim_local
Definition: hypar.h:37
int ghosts
Definition: hypar.h:52
Structure of MPI-related variables.
std::vector< double * > globalDOF
#define _ArrayCopy1D_(x, y, size)
Contains macros and function definitions for common array operations.