Chombo + EB  3.0
BCFunc.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 _BCFUNC_H_
12 #define _BCFUNC_H_
13 
14 #include "IntVect.H"
15 #include "RealVect.H"
16 #include "FArrayBox.H"
17 #include "LevelData.H"
18 #include "ProblemDomain.H"
19 #include "RefCountedPtr.H"
20 #include "DataIndex.H"
21 #include "NamespaceHeader.H"
22 
23 ///
24 /**
25  function interface for ghost cell boundary conditions
26  of AMRPoissonOp. If you are using Neumann or Dirichlet
27  boundary conditions, it is easiest to use the functions
28  provided.
29  */
30 typedef void(*BCFunc)(FArrayBox& a_state,
31  const Box& a_valid,
32  const ProblemDomain& a_domain,
33  Real a_dx,
34  bool a_homogeneous);
35 
36 /// For use with pure periodic BC
37 void doNothingBC(FArrayBox& a_state,
38  const Box& a_valid,
39  const ProblemDomain& a_domain,
40  Real a_dx,
41  bool a_homogeneous);
42 
43 
44 //! \class BCFunction
45 //! This base class represents a boundary condition for an elliptic or parabolic
46 //! partial differential equation.
48 {
49 public:
50 
51  //! Base class constructor. Called by all subclass constructors.
53  {
54  }
55 
56  //! Destructor.
57  virtual ~BCFunction()
58  {
59  }
60 
61  //! Computes values of a solution, \f$\phi\f$, on ghost cells. These ghost values
62  //! impose the boundary condition represented by the BCFunction object.
63  //! \param a_state The values of \f$\phi\f$ on the box given by \a a_valid.
64  //! \param a_valid The box on which the boundary condition is to be imposed.
65  //! \param a_domain The problem domain on which this boundary condition is imposed.
66  //! \param a_dx The grid spacing.
67  //! \param a_homogeneous If set to true, ghost values are computed for a homogeneous
68  //! boundary condition. This is useful for multigrid solves.
69  virtual void operator()(FArrayBox& a_state,
70  const Box& a_valid,
71  const ProblemDomain& a_domain,
72  Real a_dx,
73  bool a_homogeneous) = 0;
74 
75  //! Computes the values of \f$\phi\f$ on ghost cells specifying a data index.
76  //! By default, this calls the version of the method without a DataIndex.
77  //! \param a_state The values of \f$\phi\f$ on the box given by \a a_valid.
78  //! \param a_valid The box on which the boundary condition is to be imposed.
79  //! \param a_domain The problem domain on which this boundary condition is imposed.
80  //! \param a_dx The grid spacing.
81  //! \param a_index A DataIndex that can be used in the calculation.
82  //! \param a_homogeneous If set to true, ghost values are computed for a homogeneous
83  //! boundary condition. This is useful for multigrid solves.
84  virtual void operator()(FArrayBox& a_state,
85  const Box& a_valid,
86  const ProblemDomain& a_domain,
87  Real a_dx,
88  const DataIndex& a_index,
89  bool a_homogeneous)
90  {
91  operator()(a_state, a_valid, a_domain, a_dx, a_homogeneous);
92  }
93 
94  //! Sets the time associated with this boundary condition. By default, this
95  //! method does nothing, so this aspect of BCFunction can be ignored for
96  //! time-independent boundary conditions.
97  //! \param a_time The time to be passed to the boundary condition.
98  virtual void setTime(const Real& a_time)
99  {
100  }
101 
102  /// Fill the ghost cells for a single level
103  void fillGhostCells(const LevelData<FArrayBox>& phi, const Real dx,
104  const bool homogeneous)
105  {
106  const DisjointBoxLayout& dbl = phi.disjointBoxLayout();
108  for (DataIterator dit = ph.dataIterator(); dit.ok(); ++dit)
109  operator()(ph[dit], dbl[dit], dbl.physDomain(), dx, homogeneous);
110  }
111 
112 
113  /// Fill the ghost cells for a Hierarchy of levels.
115  const Real dx0, const Vector<int>& refV,
116  const bool homogeneous)
117  {
118  Real dx = dx0;
119  for (int i=0; i<phi.size(); i++)
120  {
121  fillGhostCells(*phi[i], dx, homogeneous);
122  if (i<phi.size()-1)
123  dx /= refV[i];
124  }
125  }
126 
127 private:
128 
129  // Copy constructor and assignment operator are disallowed.
130  BCFunction(const BCFunction&);
132 
133 };
134 
135 //! \class BCHolder
136 //! This class is a catch-all that can use a function pointer or a BCFunction
137 //! object to impose boundary conditions.
138 class BCHolder
139 {
140 public:
141 
142  //! Default constructor. Seems like this should be disallowed, since BCHolder
143  //! isn't sub-classable, and there's no way to set the member pointer after
144  //! creation.
145  BCHolder():m_funcptr(NULL)
146  {
147  }
148 
149  //! Creates a BCHolder using a function pointer.
150  BCHolder(BCFunc funcptr):m_funcptr(funcptr)
151  {
152  }
153 
154  //! Creates a BCHolder using a BCFunction instance.
155  BCHolder(RefCountedPtr<BCFunction> refptr):m_funcptr(NULL),m_bc(refptr)
156  {
157  }
158 
159  //! Imposes a boundary condition on a solution whose state is described
160  //! by the given FArrayBox. The signature for this method matches its
161  //! counterpart in BCFunction.
162  void operator()(FArrayBox& a_state,
163  const Box& a_valid,
164  const ProblemDomain& a_domain,
165  Real a_dx,
166  bool a_homogeneous,
167  const DataIndex a_index = DataIndex())
168  {
169  if (m_funcptr != NULL)
170  {
171  m_funcptr(a_state, a_valid, a_domain, a_dx, a_homogeneous);
172  }
173  else
174  {
175  m_bc->operator()(a_state, a_valid, a_domain, a_dx, a_index, a_homogeneous);
176  }
177  }
178 
179  //! Sets the time associated with the boundary condition, a la BCFunction::setTime.
180  //! If this BCHolder holds a function pointer, this call does nothing.
181  void setTime(Real a_time)
182  {
183  if (!m_bc.isNull())
184  m_bc->setTime(a_time);
185  }
186 
187  /// Provides access to the bc function
189  {
190  return RefCountedPtr<BCFunction>(m_bc);
191  }
192 
193 protected:
196 };
197 
198 ///
199 /**
200  given
201  pos [x,y,z] position on center of cell edge
202  int dir direction, x being 0
203  int side -1 for low, +1 = high,
204  fill in the a_values array
205 */
206 
207 typedef void(*BCValueFunc)(Real* a_pos,
208  int* a_dir,
209  Side::LoHiSide* a_side,
210  Real* a_value);
211 
212 
214 {
215 public:
217  {
218  }
219 
220  virtual void operator()(Real* a_pos,
221  int* a_dir,
222  Side::LoHiSide* a_side,
223  Real* a_value) = 0;
224 };
225 
227 {
228 public:
229  BCValueHolder():m_funcptr(NULL)
230  {
231  }
232 
233  BCValueHolder(BCValueFunc funcptr):m_funcptr(funcptr)
234  {
235  }
236 
237  BCValueHolder(RefCountedPtr<BCValueFunction> refptr):m_funcptr(NULL),m_bc(refptr)
238  {
239  }
240 
241  virtual ~BCValueHolder()
242  {
243  }
244 
245  virtual void operator()(Real* a_pos,
246  int* a_dir,
247  Side::LoHiSide* a_side,
248  Real* a_value)
249  {
250  if (m_funcptr != NULL)
251  {
252  m_funcptr(a_pos, a_dir, a_side, a_value);
253  }
254  else
255  {
256  m_bc->operator()(a_pos, a_dir, a_side, a_value);
257  }
258  }
259 
260 protected:
263 };
264 
265 // Class used by ConstDiriNeumBC to define BCs which can be passed anywhere a
266 // BCFunction/BCHolder is needed
268 {
269 public:
270  ConstBCFunction(const IntVect& a_loSideType,
271  const RealVect& a_loSideValue,
272  const IntVect& a_hiSideType,
273  const RealVect& a_hiSideValue);
274 
275  ~ConstBCFunction();
276 
277  virtual void operator()(FArrayBox& a_state,
278  const Box& a_valid,
279  const ProblemDomain& a_domain,
280  Real a_dx,
281  bool a_homogeneous);
282 
283 protected:
286 
289 };
290 
291 ///
292 /**
293  A helper function to produce the needed object for constant
294  Dirichlet/Neumann on all the faces. The return value can be passed to
295  anything expecting a BCFunction/BCHolder.
296 
297  "a_loSideType/a_hiSideType" specify the type of boundary condition in a
298  given direction by having the enter corresponding to the direction set to
299  0 for Neumann or 1 for Dirichlet (on the low or high side, respectively).
300  "a_loSideValue/a_hiSideValue" specify the (constant) value for boundary
301  condition specified above.
302 
303  For example, in 2D if "a_loSideType" = (1,0), "a_hiSideType" = (1,1),
304  "a_loSideValue" = (0.0,1.0) and "a_hiSideValue" = (0.0,0.0) then the
305  boundary conditions are:
306 
307  Low side x: Dirichlet = 0.0
308  Low side y: Neumann = 1.0
309  High side x: Dirichlet = 0.0
310  High side y: Dirichlet = 0.0
311  */
313  const RealVect& a_loSideValue,
314  const IntVect& a_hiSideType,
315  const RealVect& a_hiSideValue);
316 
317 ///
318 /**
319  Neumann bc for a particular side, specified component interval
320  For use in AMRPoissonOp.
321  */
322 void NeumBC(FArrayBox& a_state,
323  const Box& a_valid,
324  Real a_dx,
325  bool a_homogeneous,
326  const BCValueHolder& a_value,
327  int a_dir,
328  Side::LoHiSide a_side,
329  Interval& a_interval);
330 
331 ///
332 /**
333  Neumann bc for a particular side, all components
334  For use in AMRPoissonOp.
335  */
336 void NeumBC(FArrayBox& a_state,
337  const Box& a_valid,
338  Real a_dx,
339  bool a_homogeneous,
340  const BCValueHolder& a_value,
341  int a_dir,
342  Side::LoHiSide a_side);
343 
344 ///
345 /**
346  Neumann bcs for all sides
347  For use in AMRPoissonOp.
348  */
349 void NeumBC(FArrayBox& a_state,
350  const Box& a_valid,
351  Real a_dx,
352  bool a_homogeneous,
353  BCValueHolder a_value);
354 
355 ///
356 /**
357  Dirichlet boundary conditions for a side, specified component interval
358  For use in AMRPoissonOp.
359  */
360 void DiriBC(FArrayBox& a_state,
361  const Box& a_valid,
362  Real a_dx,
363  bool a_homogeneous,
364  BCValueHolder a_value,
365  int a_dir,
366  Side::LoHiSide a_side,
367  Interval& a_interval,
368  int a_order = 1);
369 
370 ///
371 /**
372  Dirichlet boundary conditions for a side, all components.
373  For use in AMRPoissonOp.
374  */
375 void DiriBC(FArrayBox& a_state,
376  const Box& a_valid,
377  Real a_dx,
378  bool a_homogeneous,
379  BCValueHolder a_value,
380  int a_dir,
381  Side::LoHiSide a_side,
382  int a_order = 1);
383 
384 ///
385 /**
386  Dirichlet boundary conditions for one side.
387  For use in AMRPoissonOp.
388  */
389 void DiriBC(FArrayBox& a_state,
390  const Box& a_valid,
391  Real a_dx,
392  bool a_homogeneous,
393  BCValueHolder a_value,
394  int a_order = 1);
395 
396 ///
397 /**
398  No slip vector bc (zero all comps).
399  need a_state.ncomp == spacedim
400  For use in ResistivityOp, for example.
401  */
402 void NoSlipVectorBC(FArrayBox& a_state,
403  const Box& a_valid,
404  Real a_dx,
405  int a_dir,
406  Side::LoHiSide a_side,
407  int a_order = 2);
408 
409 ///
410 /**
411  0 normal comp, reflective for all other comps
412  need a_state.ncomp == spacedim
413  For use in ResistivityOp, for example.
414  */
415 void ReflectiveVectorBC(FArrayBox& a_state,
416  const Box& a_valid,
417  Real a_dx,
418  int a_dir,
419  Side::LoHiSide a_side,
420  int a_order = 2);
421 
422 
423 ///
424 /**
425  Extrapolation boundary conditions for a side, specified component interval
426  For use in AMRPoissonOp.
427  */
428 void ExtrapolateBC(FArrayBox& a_state,
429  const Box& a_valid,
430  Real a_dx,
431  int a_dir,
432  Side::LoHiSide a_side,
433  Interval& a_interval,
434  int a_order = 1);
435 
436 ///
437 /**
438  Extrapolation boundary conditions for a side, all components.
439  For use in AMRPoissonOp.
440  */
441 void ExtrapolateBC(FArrayBox& a_state,
442  const Box& a_valid,
443  Real a_dx,
444  int a_dir,
445  Side::LoHiSide a_side,
446  int a_order = 1);
447 
448 ///
449 /**
450  Extrapolation boundary conditions for one side.
451  For use in AMRPoissonOp.
452  */
453 void ExtrapolateBC(FArrayBox& a_state,
454  const Box& a_valid,
455  Real a_dx,
456  int a_order = 1);
457 
458 
459 
460 
461 #include "NamespaceFooter.H"
462 #endif
virtual ~BCValueFunction()
Definition: BCFunc.H:216
virtual void setTime(const Real &a_time)
Definition: BCFunc.H:98
virtual void operator()(FArrayBox &a_state, const Box &a_valid, const ProblemDomain &a_domain, Real a_dx, bool a_homogeneous)=0
Definition: BCFunc.H:226
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: BCFunc.H:84
void fillGhostCells(const Vector< LevelData< FArrayBox > *> &phi, const Real dx0, const Vector< int > &refV, const bool homogeneous)
Fill the ghost cells for a Hierarchy of levels.
Definition: BCFunc.H:114
const ProblemDomain & physDomain() const
RefCountedPtr< BCFunction > m_bc
Definition: BCFunc.H:195
Definition: BCFunc.H:138
A class to facilitate interaction with physical boundary conditions.
Definition: ProblemDomain.H:130
virtual ~BCValueHolder()
Definition: BCFunc.H:241
void operator()(FArrayBox &a_state, const Box &a_valid, const ProblemDomain &a_domain, Real a_dx, bool a_homogeneous, const DataIndex a_index=DataIndex())
Definition: BCFunc.H:162
BCFunc m_funcptr
Definition: BCFunc.H:194
BCValueHolder(RefCountedPtr< BCValueFunction > refptr)
Definition: BCFunc.H:237
void fillGhostCells(const LevelData< FArrayBox > &phi, const Real dx, const bool homogeneous)
Fill the ghost cells for a single level.
Definition: BCFunc.H:103
RealVect m_hiSideValue
Definition: BCFunc.H:288
one dimensional dynamic array
Definition: Vector.H:52
Definition: BCFunc.H:47
RefCountedPtr< BCFunction > ConstDiriNeumBC(const IntVect &a_loSideType, const RealVect &a_loSideValue, const IntVect &a_hiSideType, const RealVect &a_hiSideValue)
void(* BCValueFunc)(Real *a_pos, int *a_dir, Side::LoHiSide *a_side, Real *a_value)
Definition: BCFunc.H:207
void doNothingBC(FArrayBox &a_state, const Box &a_valid, const ProblemDomain &a_domain, Real a_dx, bool a_homogeneous)
For use with pure periodic BC.
DataIterator dataIterator() const
Definition: LayoutDataI.H:79
virtual bool ok() const
return true if this iterator is still in its Layout
Definition: LayoutIterator.H:110
Definition: DataIterator.H:140
BCFunction()
Base class constructor. Called by all subclass constructors.
Definition: BCFunc.H:52
BCHolder(BCFunc funcptr)
Creates a BCHolder using a function pointer.
Definition: BCFunc.H:150
Definition: BCFunc.H:213
IntVect m_hiSideType
Definition: BCFunc.H:287
void(* BCFunc)(FArrayBox &a_state, const Box &a_valid, const ProblemDomain &a_domain, Real a_dx, bool a_homogeneous)
Definition: BCFunc.H:30
IntVect m_loSideType
Definition: BCFunc.H:284
void ExtrapolateBC(FArrayBox &a_state, const Box &a_valid, Real a_dx, int a_dir, Side::LoHiSide a_side, Interval &a_interval, int a_order=1)
Structure for passing component ranges in code.
Definition: Interval.H:23
BCHolder(RefCountedPtr< BCFunction > refptr)
Creates a BCHolder using a BCFunction instance.
Definition: BCFunc.H:155
void setTime(Real a_time)
Definition: BCFunc.H:181
void NeumBC(FArrayBox &a_state, const Box &a_valid, Real a_dx, bool a_homogeneous, const BCValueHolder &a_value, int a_dir, Side::LoHiSide a_side, Interval &a_interval)
BCValueHolder(BCValueFunc funcptr)
Definition: BCFunc.H:233
double Real
Definition: REAL.H:33
BCHolder()
Definition: BCFunc.H:145
void DiriBC(FArrayBox &a_state, const Box &a_valid, Real a_dx, bool a_homogeneous, BCValueHolder a_value, int a_dir, Side::LoHiSide a_side, Interval &a_interval, int a_order=1)
A BoxLayout that has a concept of disjointedness.
Definition: DisjointBoxLayout.H:31
LoHiSide
Definition: LoHiSide.H:27
RefCountedPtr< BCFunction > getBCFunction()
Provides access to the bc function.
Definition: BCFunc.H:188
Definition: BCFunc.H:267
RealVect m_loSideValue
Definition: BCFunc.H:285
void ReflectiveVectorBC(FArrayBox &a_state, const Box &a_valid, Real a_dx, int a_dir, Side::LoHiSide a_side, int a_order=2)
BCValueHolder()
Definition: BCFunc.H:229
A Rectangular Domain on an Integer Lattice.
Definition: Box.H:465
A Real vector in SpaceDim-dimensional space.
Definition: RealVect.H:41
RefCountedPtr< BCValueFunction > m_bc
Definition: BCFunc.H:262
BCFunction & operator=(const BCFunction &)
Definition: DataIndex.H:112
const DisjointBoxLayout & disjointBoxLayout() const
Definition: LevelData.H:196
void NoSlipVectorBC(FArrayBox &a_state, const Box &a_valid, Real a_dx, int a_dir, Side::LoHiSide a_side, int a_order=2)
An integer Vector in SpaceDim-dimensional space.
Definition: CHArray.H:42
Definition: FArrayBox.H:44
virtual void operator()(Real *a_pos, int *a_dir, Side::LoHiSide *a_side, Real *a_value)
Definition: BCFunc.H:245
virtual ~BCFunction()
Destructor.
Definition: BCFunc.H:57
BCValueFunc m_funcptr
Definition: BCFunc.H:261