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