HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
TransferToPETSc.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 <simulation_object.h>
13 #include <bandedmatrix.h>
14 #include <petscinterface_struct.h>
15 
16 #undef __FUNCT__
17 #define __FUNCT__ "TransferVecToPETSc"
18 
24 int TransferVecToPETSc( const double* const u,
25  Vec Y,
26  void* ctxt,
27  const int sim_idx,
28  const int offset )
29 {
30  PETScContext* context = (PETScContext*) ctxt;
31  SimulationObject* sim = (SimulationObject*) context->simobj;
32  double* Yarr;
33 
34  PetscFunctionBegin;
35  VecGetArray(Y,&Yarr);
36  std::vector<int> index(sim[sim_idx].solver.ndims,0);
37  ArrayCopynD( sim[sim_idx].solver.ndims,
38  u,
39  (Yarr+offset),
40  sim[sim_idx].solver.dim_local,
41  sim[sim_idx].solver.ghosts,
42  0,
43  index.data(),
44  sim[sim_idx].solver.nvars );
45  VecRestoreArray(Y,&Yarr);
46 
47  PetscFunctionReturn(0);
48 }
49 
53 int TransferMatToPETSc( void *J,
54  Mat A,
55  void *ctxt )
56 {
57  BandedMatrix *M = (BandedMatrix*) J;
58  PetscErrorCode ierr = 0;
59  int bs = M->BlockSize, nbands = M->nbands, bs2 = bs*bs;
60 
61  for (int i=0; i<M->nrows_local; i++) {
62  int colind[nbands];
63  double val[bs][bs*nbands];
64  for (int n=0; n<nbands; n++) {
65  colind[n] = M->ncol[nbands*i+n];
66  for (int p=0; p<bs; p++) {
67  for (int q = 0; q<bs; q++) {
68  val[p][n*bs+q] = M->data[i*nbands*bs2+n*bs2+p*bs+q];
69  }
70  }
71  }
72  MatSetValuesBlocked(A,1,&M->nrow[i],M->nbands,&colind[0],&val[0][0],INSERT_VALUES);
73  }
74 
75  MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
76  MatAssemblyEnd (A,MAT_FINAL_ASSEMBLY);
77 
78  return(0);
79 }
80 
81 #endif
INLINE int ArrayCopynD(int, const double *, double *, int *, int, int, int *, int)
int * dim_local
Definition: hypar.h:37
double * data
Definition: bandedmatrix.h:24
int ghosts
Definition: hypar.h:52
Structure for defining a banded block matrix.
Definition: bandedmatrix.h:18
Data structure and some function declarations for banded block matrices.
int TransferMatToPETSc(void *, Mat, void *)
int TransferVecToPETSc(const double *const, Vec, void *, const int, const int)
Simulation object.
int nvars
Definition: hypar.h:29
Contains structure that defines the interface for time integration with PETSc (https://petsc.org/release/)
Structure containing the variables for time-integration with PETSc.
Structure defining a simulation.
Some basic definitions and macros.
Contains macros and function definitions for common array operations.