HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
PetscJacobianMatNonzeroEntriesImpl.cpp File Reference

Contains the function to set nonzero entries of the Jacobian matrix. More...

#include <stdio.h>
#include <vector>
#include <arrayfunctions.h>
#include <simulation_object.h>
#include <mpivars_cpp.h>
#include <petscinterface.h>

Go to the source code of this file.

Macros

#define __FUNCT__   "PetscJacobianMatNonzeroEntriesImpl"
 

Functions

int PetscJacobianMatNonzeroEntriesImpl (Mat Amat, int width, void *ctxt)
 

Detailed Description

Contains the function to set nonzero entries of the Jacobian matrix.

Author
Debojyoti Ghosh

Definition in file PetscJacobianMatNonzeroEntriesImpl.cpp.

Macro Definition Documentation

◆ __FUNCT__

#define __FUNCT__   "PetscJacobianMatNonzeroEntriesImpl"

Definition at line 16 of file PetscJacobianMatNonzeroEntriesImpl.cpp.

Function Documentation

◆ PetscJacobianMatNonzeroEntriesImpl()

int PetscJacobianMatNonzeroEntriesImpl ( Mat  Amat,
int  width,
void *  ctxt 
)

Set non-zero entries of the Jacobian matrix for the implicit time integration of the governing equations: The ODE, obtained after discretizing the governing PDE in space, is expressed as follows:

\begin{equation} \frac {d{\bf U}}{dt} = {\bf L}\left({\bf U}\right) \Rightarrow \frac {d{\bf U}}{dt} - {\bf L}\left({\bf U}\right) = 0, \end{equation}

where \({\bf L}\) is the spatially discretized right-hand-side, and \({\bf U}\) represents the entire solution vector.

The Jacobian is thus given by:

\begin{equation} {\bf J} = \left[\alpha{\bf I} - \frac {\partial {\bf L}} {\partial {\bf U}} \right] \end{equation}

where \(\alpha\) is the shift coefficient (PETScContext::shift) of the time integration method.

All functions and variables whose names start with Vec, Mat, PC, KSP, SNES, and TS are defined by PETSc. Refer to the PETSc documentation (https://petsc.org/release/docs/). Usually, googling with the function or variable name yields the specific doc page dealing with that function/variable.

Parameters
AmatMatrix
widthStencil width
ctxtApplication context

Definition at line 38 of file PetscJacobianMatNonzeroEntriesImpl.cpp.

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 }
std::vector< int * > points
Structure defining a simulation.
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.
Structure of MPI-related variables.
std::vector< double * > globalDOF
#define _ArrayCopy1D_(x, y, size)