Chombo + EB  3.2
PetscCompGrid.H
Go to the documentation of this file.
1 #ifdef CH_LANG_CC
2 /*
3  * _______ __
4  * / ___/ / ___ __ _ / / ___
5  * / /__/ _ \/ _ \/ V \/ _ \/ _ \
6  * \___/_//_/\___/_/_/_/_.__/\___/
7  * Please refer to Copyright.txt, in Chombo's root directory.
8  */
9 #endif
10 
11 #ifndef _PETSCCOMPGRID_H_
12 #define _PETSCCOMPGRID_H_
13 #include "Stencil.H"
14 #include "BCFunc.H"
15 #include "DisjointBoxLayout.H"
16 #include "BoxIterator.H"
18 #ifdef CH_USE_PETSC
19 #include <petsc.h>
20 #endif
21 #include "NamespaceHeader.H"
22 
23 typedef enum {GHOST=-1,FINE_COVERED=-2,DIRI_BC=-3,
25 
26 std::ostream& operator<< (std::ostream& os, GID_type);
27 
28 //! \class PetscCompGrid
29 //! This base class organizes the construction of a PETSc matrix, with an AMR hierarchy
31 {
32 
33 public:
34  //! Destructor.
35  virtual ~PetscCompGrid();
36 
37 #ifdef CH_USE_PETSC
38 
39  //! Base class constructor. Called by all subclass constructors.
40  PetscCompGrid(int a_dof) : m_gid0(0),m_mat(0),m_Pmat(0),m_CFStencilRad(2),m_writeMatlab(false),
42  m_num_extra_nnz(0),m_repartition(PETSC_FALSE),
44  {
45 #if defined(PETSC_USE_LOG)
46  PetscLogEventRegister("createMatrix",PETSC_VIEWER_CLASSID,&m_event0);
47  PetscLogEventRegister("PetscCompGrid",PETSC_VIEWER_CLASSID,&m_event1);
48  PetscLogEventRegister("Prepartsition",PETSC_VIEWER_CLASSID,&m_event2);
49 #endif
50  }
51 
52  virtual void define( const ProblemDomain &a_cdomain,
53  Vector<DisjointBoxLayout> &a_grids,
54  Vector<int> &a_refratios,
55  BCHolder a_bc,
56  const RealVect &a_cdx,
57  int a_numLevels=-1, int a_ibase=0);
58 
59  virtual void clean();
60 
61  Mat getMatrix() const { return m_mat; }
62  Mat getPMatrix() const { return m_Pmat; }
63  void setMatlab(bool b=true) {m_writeMatlab = b;}
64  void setRepartition(bool b=true) {m_repartition = (b ? PETSC_TRUE : PETSC_FALSE);}
65  void setVerbose(int a_v){ m_verbose=a_v;}
67 
68  // main Mat creation routines
69  PetscErrorCode createMatrix(int a_makePmat=0);
70  // solver uses these, not used in base class, used in app code
71  PetscErrorCode putChomboInPetsc( const Vector<LevelData<FArrayBox> * > &rhs, Vec b )const;
72  PetscErrorCode putPetscInChombo( Vec b, Vector<LevelData<FArrayBox> * > &rhs )const;
73  // derived type needs to tell me how many ghosts it needs
74  virtual IntVect getGhostVect()const = 0;
75  virtual void addExtraCovered(GID_type,int,const DataIndex&,BaseFab<PetscInt>&) {} // default no op
76 protected:
77  // core stencil ops - base class has basic implementations except for equations
78  virtual void createOpStencil(IntVect,int,const DataIndex&,StencilTensor &) = 0;
79  virtual void applyBCs(IntVect,int,const DataIndex&,Box,StencilTensor &);
80  virtual void InterpToFine(IntVect,int,const DataIndex&,StencilTensor &);
81  virtual void InterpToCoarse(IntVect,int,const DataIndex&,StencilTensor &);
82  // helper for FourthOrderInterpStencil
83  IntVect getCFStencil(const ProblemDomain &a_cdom, const IntVect a_ivc);
84  // at end add stencil to matrix - done
85  PetscErrorCode AddStencilToMat(IntVect,int,const DataIndex&,StencilTensor &, Mat);
86  // utils
87  void NodeDefine(StencilNode &a_node, IntVect a_iv, int a_lev, Real a_val)
88  {
89  a_node.first.setIV(a_iv);
90  a_node.first.setLevel(a_lev);
91  a_node.second.define(1);
92  a_node.second.setValue(a_val); // this sets diagonal
93  }
94  void setCFCoverMaps(int a_nlev);
95  void setCoverMaps(int a_nlev);
96  PetscErrorCode permuteDataAndMaps(Vector<StencilTensor> &patchStencil);
97  // data
102 public:
104 protected:
107 public:
109 protected:
110  Mat m_mat;
111  Mat m_Pmat;
113  // all possible stencils, on box(-m_CFStencilRad:+m_CFStencilRad)^SpaceDim
119  const int m_dof;
120 #if defined(PETSC_USE_LOG)
121  PetscLogEvent m_event0;
122  PetscLogEvent m_event1;
123  PetscLogEvent m_event2;
124 #endif
125 public:
127  // repartitioning data
128  PetscBool m_repartition;
129 private:
130  PetscInt m_patch_size;
131  VecScatter m_from_petscscat;
132  Vec m_origvec; // buffer to hold Chombo data in a PETSc vector
133 private:
134  // Disallowed for all the usual reasons
135  void operator=(const PetscCompGrid& a_input)
136  {
137  MayDay::Error("invalid operator");
138  }
139  // Disallowed for all the usual reasons
140  PetscCompGrid(const PetscCompGrid& a_input):m_dof(0)
141  {
142  MayDay::Error("invalid operator");
143  }
144 #endif // ifdef petsc
145 
146 };
147 
148 class CompBC : public BCFunction
149 {
150 public:
151  CompBC() {;}
152  CompBC(int a_nSource, IntVect a_nGhosts);
153  virtual ~CompBC();
154  void define(int a_nSource, IntVect a_nGhosts);
155 
156  virtual void createCoefs() = 0;
157  virtual void operator()( FArrayBox& a_state,
158  const Box& a_valid,
159  const ProblemDomain& a_domain,
160  Real a_dx,
161  bool a_homogeneous) = 0;
162 
163  virtual void operator()( FArrayBox& a_state,
164  const Box& a_valid,
165  const ProblemDomain& a_domain,
166  Real a_dx,
167  const DataIndex& a_index,
168  bool a_homogeneous)
169  {
170  operator()(a_state, a_valid, a_domain, a_dx, a_homogeneous);
171  }
172 
173  IntVect nGhosts()const{return m_nGhosts;}
174  int nSources()const{return m_nSources;}
175 #ifdef CH_USE_PETSC
176  PetscReal getCoef(int a_iSrc, int a_iGhost=0);
177 #else
178  Real getCoef(int a_iSrc, int a_iGhost=0);
179 #endif
180 protected:
181 #ifdef CH_USE_PETSC
182  PetscReal *m_Rcoefs;
183 #else
184  Vector<Real> m_Rcoefs;
185 #endif
187  int m_nSources; // degreee is m_nSources-1
189 
190 };
191 
192 class ConstDiriBC : public CompBC
193 {
194 public:
195  ConstDiriBC(int a_nSource=1, IntVect a_nGhosts=IntVect::Unit) : CompBC(a_nSource,a_nGhosts) {}
196  virtual void createCoefs();
197  virtual void operator()( FArrayBox& a_state,
198  const Box& a_valid,
199  const ProblemDomain& a_domain,
200  Real a_dx,
201  bool a_homogeneous);
202 };
203 
204 #include "NamespaceFooter.H"
205 
206 #endif
PetscBool m_repartition
Definition: PetscCompGrid.H:128
std::pair< IndexML, StencilTensorValue > StencilNode
Definition: Stencil.H:139
Vector< int > m_refRatios
Definition: PetscCompGrid.H:100
virtual void InterpToFine(IntVect, int, const DataIndex &, StencilTensor &)
Definition: PetscCompGrid.H:148
IntVect m_nGhosts
Definition: PetscCompGrid.H:186
Vector< RefCountedPtr< LevelData< BaseFab< PetscInt > > > > m_GIDs
Definition: PetscCompGrid.H:103
Definition: BCFunc.H:136
A class to facilitate interaction with physical boundary conditions.
Definition: ProblemDomain.H:141
Definition: PetscCompGrid.H:24
Definition: PetscCompGrid.H:24
PetscInt m_patchid0
Definition: PetscCompGrid.H:108
PetscInt m_patch_size
Definition: PetscCompGrid.H:130
Definition: BCFunc.H:46
virtual void define(const ProblemDomain &a_cdomain, Vector< DisjointBoxLayout > &a_grids, Vector< int > &a_refratios, BCHolder a_bc, const RealVect &a_cdx, int a_numLevels=-1, int a_ibase=0)
int m_verbose
Definition: PetscCompGrid.H:118
PetscErrorCode AddStencilToMat(IntVect, int, const DataIndex &, StencilTensor &, Mat)
std::map< IndexML, StencilTensorValue > StencilTensor
Definition: Stencil.H:137
Definition: PetscCompGrid.H:24
Mat m_mat
Definition: PetscCompGrid.H:110
virtual IntVect getGhostVect() const =0
int m_CFStencilRad
Definition: PetscCompGrid.H:114
virtual void operator()(FArrayBox &a_state, const Box &a_valid, const ProblemDomain &a_domain, Real a_dx, const DataIndex &a_index, bool a_homogeneous)
Definition: PetscCompGrid.H:163
ConstDiriBC(int a_nSource=1, IntVect a_nGhosts=IntVect::Unit)
Definition: PetscCompGrid.H:195
void setCFCoverMaps(int a_nlev)
PetscErrorCode createMatrix(int a_makePmat=0)
int m_num_extra_nnz
Definition: PetscCompGrid.H:126
PetscInt m_nlocrealpatches
Definition: PetscCompGrid.H:108
void NodeDefine(StencilNode &a_node, IntVect a_iv, int a_lev, Real a_val)
Definition: PetscCompGrid.H:87
Mat m_Pmat
Definition: PetscCompGrid.H:111
IntVect nGhosts() const
Definition: PetscCompGrid.H:173
int nSources() const
Definition: PetscCompGrid.H:174
PetscReal * m_Rcoefs
Definition: PetscCompGrid.H:182
Vector< DisjointBoxLayout > m_grids
Definition: PetscCompGrid.H:99
void operator=(const PetscCompGrid &a_input)
Definition: PetscCompGrid.H:135
Mat getMatrix() const
Definition: PetscCompGrid.H:61
PetscCompGrid(int a_dof)
Base class constructor. Called by all subclass constructors.
Definition: PetscCompGrid.H:40
static const IntVect Unit
Definition: IntVect.H:659
Definition: PetscCompGrid.H:30
virtual void createOpStencil(IntVect, int, const DataIndex &, StencilTensor &)=0
bool m_isDefined
Definition: PetscCompGrid.H:188
Vector< ProblemDomain > m_domains
Definition: PetscCompGrid.H:98
double Real
Definition: REAL.H:33
virtual void addExtraCovered(GID_type, int, const DataIndex &, BaseFab< PetscInt > &)
Definition: PetscCompGrid.H:75
virtual ~PetscCompGrid()
Destructor.
VecScatter m_from_petscscat
Definition: PetscCompGrid.H:131
const int m_dof
Definition: PetscCompGrid.H:119
virtual void InterpToCoarse(IntVect, int, const DataIndex &, StencilTensor &)
std::ostream & operator<<(std::ostream &os, GID_type)
static void Error(const char *const a_msg=m_nullString, int m_exitCode=CH_DEFAULT_ERROR_CODE)
Print out message to cerr and exit with the specified exit code.
Definition: BaseFab.H:77
PetscInt m_gid0
Definition: PetscCompGrid.H:108
PetscCompGrid(const PetscCompGrid &a_input)
Definition: PetscCompGrid.H:140
A Rectangular Domain on an Integer Lattice.
Definition: Box.H:465
A Real vector in SpaceDim-dimensional space.
Definition: RealVect.H:41
Mat getPMatrix() const
Definition: PetscCompGrid.H:62
Definition: PetscCompGrid.H:23
Definition: DataIndex.H:112
virtual void clean()
Vec m_origvec
Definition: PetscCompGrid.H:132
Definition: PetscCompGrid.H:23
PetscErrorCode putPetscInChombo(Vec b, Vector< LevelData< FArrayBox > * > &rhs) const
An integer Vector in SpaceDim-dimensional space.
Definition: CHArray.H:42
Definition: FArrayBox.H:45
IntVect getCFStencil(const ProblemDomain &a_cdom, const IntVect a_ivc)
PetscErrorCode putChomboInPetsc(const Vector< LevelData< FArrayBox > * > &rhs, Vec b) const
void setCoverMaps(int a_nlev)
Vector< RefCountedPtr< LevelData< BaseFab< PetscInt > > > > m_fineCoverGIDs
Definition: PetscCompGrid.H:106
int m_nSources
Definition: PetscCompGrid.H:187
Vector< RefCountedPtr< LevelData< BaseFab< PetscInt > > > > m_crsSupportGIDs
Definition: PetscCompGrid.H:105
void setVerbose(int a_v)
Definition: PetscCompGrid.H:65
virtual void applyBCs(IntVect, int, const DataIndex &, Box, StencilTensor &)
PetscErrorCode permuteDataAndMaps(Vector< StencilTensor > &patchStencil)
Definition: PetscCompGrid.H:23
void setMatlab(bool b=true)
Definition: PetscCompGrid.H:63
GID_type
Definition: PetscCompGrid.H:23
bool m_writeMatlab
Definition: PetscCompGrid.H:116
BaseFab< FourthOrderInterpStencil * > m_FCStencils
Definition: PetscCompGrid.H:115
void setAverageFineSolutionToCoarse(bool b=true)
Definition: PetscCompGrid.H:66
bool m_averageFineSolutionToCoarse
Definition: PetscCompGrid.H:117
CompBC()
Definition: PetscCompGrid.H:151
Definition: PetscCompGrid.H:192
void setRepartition(bool b=true)
Definition: PetscCompGrid.H:64
BCHolder m_bc
Definition: PetscCompGrid.H:112
Vector< RealVect > m_dxs
Definition: PetscCompGrid.H:101