Chombo + EB  3.0
CodimBox.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 _CODIMBOX_H_
12 #define _CODIMBOX_H_
13 
14 #include "FArrayBox.H"
15 #include "BoxLayoutData.H"
16 #include "NamespaceHeader.H"
17 
18 
19 /*******************************************************************************
20  */
21 /// An FArrayBox container for storage on the codimensions of a box.
22 /**
23  * The orientation of a box is described by specifying the orthogonal
24  * directions and internally, this is converted to a bit representation.
25  * Direction 'i' is bit 2^i (or via shifting as 1 << i). Directions can be
26  * specified in any order. For example, the edges in 3D orthogonal to
27  * directions i and j may be specified as (0, 1) or (1, 0). Internally, both
28  * are converted to 0x3. Orientations can be described by sequential indices
29  * or bit representations. The routines seq2bit and bit2seq can be used to
30  * switch between the two using lookup tables.
31  *
32  * For example, a 3 dimensional space has the following codimensions, bit
33  * representations, and sequential indices of the orientations:
34  * \verbatim
35  * Codim Bit Integer Sequential numOrient totOrient
36  * Dir zyx
37  * 0 000 0 0 1 0
38  * 1 001 1 0 3 1
39  * 1 010 2 1
40  * 1 100 4 2
41  * 2 011 3 0 3 4
42  * 2 101 5 1
43  * 2 110 6 2
44  * 3 111 7 0 1 7
45  * \endverbatim
46  * The 'indexFAB' and 'bitOrient' are then defined as follows:
47  * \verbatim
48  * indexFAB: 0 0 1 0 2 1 2 0
49  * bitOrient: 0 1 2 4 3 5 6 7
50  * \endverbatim
51  * \note
52  * <ul>
53  * <li> CodimBoxFactory is also defined in this file for use with
54  * BoxLayoutData and similar containers
55  * <li> A FluxBox is essentially a CodimBox with codimension 1.
56  * <li> Not much functionality exists in this class beyond creating and
57  * retrieving the FArrayBoxes.
58  * <li> Codimension 0 is the original object spanning 'SpaceDim' dimensions.
59  * Using this is no different from an FArrayBox itself.
60  * <li> Codimension 'SpaceDim' is a vertex. This is also the same as an
61  * FArrayBox that surrounds all nodes.
62  * </ul>
63  *
64  ******************************************************************************/
65 
66 class CodimBox
67 {
68 
69 public:
70  enum
71  {
72  numCD = SpaceDim + 1, ///< Number of codimensions
73  numAI = 1 << SpaceDim ///< Total orientations (array indices) for
74  ///< all codimensions
75  };
76 
77  /// Default constructor
78  CodimBox();
79 
80  /// Full constructor
81  CodimBox(const int a_codim, const Box& a_box, const int a_nvar);
82 
83  /// Destructor
84  ~CodimBox();
85 
86  /// Initialize static lookup tables
87  static void initialize();
88 
89  /// Full define function
90  void define(const int a_codim, const Box& a_box, const int a_nvar);
91 
92  /// Define function
93  void define(const Box& a_box, int a_nvar);
94 
95  /// Deallocate
96  void clear();
97 
98  /// Return the codimension
99  int getCodim() const;
100 
101  /// Number of components
102  int nComp() const;
103 
104  /// Number of different orientations of codimensional objects
105  int getNumOrient() const;
106 
107  /// Number of different orientations of codimensional objects. Used
108  /// externally for any codimension.
109  static int getNumOrient(const int a_codim);
110 
111  /// Switch from a bit orientation to a sequential index of the
112  /// orientation
113  int bit2seq(const unsigned a_bOrient) const;
114 
115  /// Switch from a sequential index to a bit representation of the
116  /// orientation
117  unsigned seq2bit(const int a_iOrient) const;
118 
119  /// Switch from a sequential index to a bit representation of the orientation
120  /// for any codimension
121  static unsigned seq2bit(const int a_codim, const int a_iOrient);
122 
123  /// Get the 'i'th orthogonal direction of an orientation
124  int getDirection(const int a_iOrient, int a_i) const;
125 
126  /// Get all the orthogonal directions of an orientation
127  void getDirections(const int a_iOrient, int *const a_dir) const;
128 
129  /// Get all the orthogonal directions of an orientation. Used externally for
130  /// any codimension.
131  static void getDirections(const int a_codim,
132  const int a_iOrient,
133  int *const a_dir);
134 
135  /// Returns cell-centered box which defines the CodimBox
136  const Box& box() const;
137 
138  /// Returns FArrayBox in the directions defined by a Box
139  const FArrayBox& operator()(const Box &a_box) const;
140  FArrayBox& operator()(const Box &a_box);
141 
142  /// Returns FArrayBox in the directions defined by an IndexType
143  const FArrayBox& operator()(const IndexType &a_ixType) const;
144  FArrayBox& operator()(const IndexType &a_ixType);
145 
146  /// Returns FArrayBox in the given direction (codimension 0)
147  const FArrayBox& operator()() const;
149 
150  /// Returns FArrayBox in the given direction (codimension 1)
151  const FArrayBox& operator()(const int a_dir0) const;
152  FArrayBox& operator()(const int a_dir0);
153 
154  /// Returns FArrayBox in the given direction (codimension 2)
155  const FArrayBox& operator()(const int a_dir0,
156  const int a_dir1) const;
157  FArrayBox& operator()(const int a_dir0,
158  const int a_dir1);
159 
160  /// Returns FArrayBox in the given direction (codimension 3)
161  const FArrayBox& operator()(const int a_dir0,
162  const int a_dir1,
163  const int a_dir2) const;
164  FArrayBox& operator()(const int a_dir0,
165  const int a_dir1,
166  const int a_dir2);
167 
168  /// Returns FArrayBox in the given direction (codimension 4)
169  const FArrayBox& operator()(const int a_dir0,
170  const int a_dir1,
171  const int a_dir2,
172  const int a_dir3) const;
173  FArrayBox& operator()(const int a_dir0,
174  const int a_dir1,
175  const int a_dir2,
176  const int a_dir3);
177 
178  /// Returns FArrayBox in the given direction (codimension 5)
179  const FArrayBox& operator()(const int a_dir0,
180  const int a_dir1,
181  const int a_dir2,
182  const int a_dir3,
183  const int a_dir4) const;
184  FArrayBox& operator()(const int a_dir0,
185  const int a_dir1,
186  const int a_dir2,
187  const int a_dir3,
188  const int a_dir4);
189 
190  /// Returns FArrayBox in the given direction (codimension 6)
191  const FArrayBox& operator()(const int a_dir0,
192  const int a_dir1,
193  const int a_dir2,
194  const int a_dir3,
195  const int a_dir4,
196  const int a_dir5) const;
197  FArrayBox& operator()(const int a_dir0,
198  const int a_dir1,
199  const int a_dir2,
200  const int a_dir3,
201  const int a_dir4,
202  const int a_dir5);
203 
204  /// Returns FArrayBox from a sequential index
205  const FArrayBox &getSequential(const int a_iOrient) const;
206  FArrayBox &getSequential(const int a_iOrient);
207 
208  /// Set all values in all FAB
209  void setVal(const Real a_x);
210 
211  /// Orient the stored box in the othogonal directions of the given
212  /// orientation
213  Box orientBox(const int a_iOrient) const;
214 
215  /// Orient a centered box in the othogonal directions of the given
216  /// orientation
217  Box orientBox(const int a_iOrient, const Box& a_cbox) const;
218 
219  /// Orient a centered box in the othogonal directions of the given
220  /// orientation. Used externally for any codimension.
221  static Box orientBox(const int a_codim,
222  const int a_iOrient,
223  const Box& a_cbox);
224 
225  /// Copy from another CodimBox
226  void copy(const Box& a_R,
227  const Interval& a_Cd,
228  const CodimBox& a_src,
229  const Interval& a_Cs);
230 
231  /// Copy from another CodimBox but to a different region
232  void copy(const Box& a_Rs, // Beware that the src and dest arguments
233  const Interval& a_Cd, // are not grouped together
234  const Box& a_Rd,
235  const CodimBox& a_src,
236  const Interval& a_Cs);
237 
238  /// Report preallocation capability as non-static but symmetric
239  static int preAllocatable();
240 
241  /// Returns size of serialized data
242  int size(const Box& a_R, const Interval& a_comps) const;
243 
244  /// Writes a serialized representation of this CodimBox
245  void linearOut(void* a_buf, const Box& a_R, const Interval& a_comps) const;
246 
247  /// Reads a serialized representation of this CodimBox
248  void linearIn(void* a_buf, const Box& a_R, const Interval& a_comps);
249 
250 protected:
251  static int numOrient[numCD]; ///< Number of orientations for an object
252  ///< of a given codimension
253  static int totOrient[numCD]; ///< Total of number of orientations for
254  ///< all smaller codimensions
255  static unsigned bitOrient[CodimBox::numAI];
256  ///< A sequential list of bit
257  ///< representations of possible
258  ///< orientations for each codimension
260  ///< Returns the index of the FArrayBox
261  ///< for a given codimension and
262  ///< orientation. The input index
263  ///< expresses the orientation as the sum
264  ///< of the orthogonal directions.
265 
266 private:
267 
268  Box m_box; ///< Cell-centered box over which this
269  ///< CodimBox is defined
270  int m_codim; ///< Codimension for which the FArrayBoxes
271  ///< are defined
272  int m_nvar; ///< Number of components in the FArrayBox
273  Vector<FArrayBox*> m_FArrayBox; ///< The FArrayBoxes for each orientation
274 
275  // Copy and assignment not allowed
276  CodimBox(const CodimBox&);
277  CodimBox& operator=(const CodimBox&);
278 
279  /// Check that static arrays are initialized
280  static bool isInitialized();
281 
282  /// Return the FArray box from a bit description of the orientation
283  FArrayBox& getFArrayBox(const unsigned i);
284  const FArrayBox& getFArrayBox(const unsigned i) const;
285 
286  /// Get all the orthogonal directions of an orientation (general)
287  static void genGetDirections(const int a_codim,
288  int a_bOrient,
289  int *const a_dir);
290 
291  /// Orient a centered box in the othogonal directions of the given
292  /// orientation (general)
293  static Box genOrientBox(int a_bOrient, Box a_cbox);
294 
295 // Only of use with a register class
296 // /// Shift all FABs by halves in one direction.
297 // void shiftHalf(const int a_dir, const int a_numHalfs);
298 // friend class LevelCodimRegister;
299 
300 };
301 
302 
303 /*******************************************************************************
304  *
305  * Class CodimBox: inline member definitions
306  *
307  ******************************************************************************/
308 
309 /*--------------------------------------------------------------------*/
310 /// Return the codimension
311 /*--------------------------------------------------------------------*/
312 
313 inline int
315 {
316  return m_codim;
317 }
318 
319 /*--------------------------------------------------------------------*/
320 /// Number of components
321 /*--------------------------------------------------------------------*/
322 
323 inline int
325 {
326  return m_nvar;
327 }
328 
329 /*--------------------------------------------------------------------*/
330 /// Number of different orientations of codimensional objects
331 /*--------------------------------------------------------------------*/
332 
333 inline int
335 {
336  return numOrient[m_codim];
337 }
338 
339 /*--------------------------------------------------------------------*/
340 /// Number of different orientations of codimensional objects. Used
341 /// externally for any codimension.
342 /*--------------------------------------------------------------------*/
343 
344 inline int
345 CodimBox::getNumOrient(const int a_codim)
346 {
348  return numOrient[a_codim];
349 }
350 
351 /*--------------------------------------------------------------------*/
352 /// Switch from a bit orientation to a sequential index of the
353 /// orientation
354 /*--------------------------------------------------------------------*/
355 
356 inline int
357 CodimBox::bit2seq(const unsigned a_bOrient) const
358 {
359  return indexFAB[a_bOrient];
360 }
361 
362 /*--------------------------------------------------------------------*/
363 /// Switch from a sequential index to a bit representation of the
364 /// orientation
365 /*--------------------------------------------------------------------*/
366 
367 inline unsigned
368 CodimBox::seq2bit(const int a_iOrient) const
369 {
370  return bitOrient[totOrient[m_codim] + a_iOrient];
371 }
372 
373 /*--------------------------------------------------------------------*/
374 /// Switch from a sequential index to a bit representation of the
375 /// orientation for any codimension
376 /*--------------------------------------------------------------------*/
377 
378 inline unsigned
379 CodimBox::seq2bit(const int a_codim, const int a_iOrient)
380 {
382  return bitOrient[totOrient[a_codim] + a_iOrient];
383 }
384 
385 /*--------------------------------------------------------------------*/
386 /// Get all the orthogonal directions of an orientation
387 /** \param[in] a_iOrient
388  * The sequential orientation index from which to
389  * get the directions
390  * \param[in] a_dir Array with minimum size [m_codim]
391  * \param[out] a_dir Integers defining the directions
392  * 0 <= dir[i] < SpaceDim
393  * \note
394  * <ul>
395  * <li> The directions are orderd from smallest to largest.
396  * </ul>
397  *//*-----------------------------------------------------------------*/
398 
399 inline void
400 CodimBox::getDirections(const int a_iOrient, int *const a_dir) const
401 {
402  genGetDirections(m_codim, seq2bit(a_iOrient), a_dir);
403 }
404 
405 /*--------------------------------------------------------------------*/
406 /// Get all the orthogonal directions of an orientation. Used
407 /// externally for any codimension.
408 /** \param[in] a_codim
409  * The codimension
410  * \param[in] a_iOrient
411  * The sequential orientation index from which to
412  * get the directions
413  * \param[in] a_dir Array with minimum size [m_codim]
414  * \param[out] a_dir Integers defining the directions
415  * 0 <= dir[i] < SpaceDim
416  * \note
417  * <ul>
418  * <li> The directions are orderd from smallest to largest.
419  * </ul>
420  *//*-----------------------------------------------------------------*/
421 
422 inline void
423 CodimBox::getDirections(const int a_codim,
424  const int a_iOrient,
425  int *const a_dir)
426 {
428  genGetDirections(a_codim, seq2bit(a_codim, a_iOrient), a_dir);
429 }
430 
431 /*--------------------------------------------------------------------*/
432 // Returns cell-centered box which defines the CodimBox
433 /*--------------------------------------------------------------------*/
434 
435 inline const Box&
437 {
438  return m_box;
439 }
440 
441 /*--------------------------------------------------------------------*/
442 /// Returns FArrayBox in the directions defined by a Box
443 /*--------------------------------------------------------------------*/
444 
445 inline FArrayBox&
447 {
448  return this->operator()(a_box.ixType());
449 }
450 
451 /*--------------------------------------------------------------------*/
452 /// Returns FArrayBox from a sequential index
453 /** \param[in] a_iOrient
454  * A sequential orientation index.
455  * \note
456  * <ul>
457  * <li> A bit description of the orientation can be obtained from
458  * \verbatim
459  * bitOrient[totOrient[a_codim] + a_iOrient]
460  * \endverbatim
461  * </ul>
462  *//*-----------------------------------------------------------------*/
463 
464 inline const FArrayBox&
465 CodimBox::getSequential(const int a_iOrient) const
466 {
467  CH_assert(a_iOrient < numOrient[m_codim]);
468  return *m_FArrayBox[a_iOrient];
469 }
470 
471 inline FArrayBox&
472 CodimBox::getSequential(const int a_iOrient)
473 {
474  CH_assert(a_iOrient < numOrient[m_codim]);
475  return *m_FArrayBox[a_iOrient];
476 }
477 
478 /*--------------------------------------------------------------------*/
479 /// Orient the stored box in the othogonal directions of the given
480 /// orientation
481 /** \param[in] a_bOrient
482  * A bit orientation index.
483  *//*-----------------------------------------------------------------*/
484 
485 inline Box
486 CodimBox::orientBox(const int a_bOrient) const
487 {
488  return orientBox(a_bOrient, m_box);
489 }
490 
491 /*--------------------------------------------------------------------*/
492 /// Orient a centered box in the othogonal directions of the given
493 /// orientation
494 /** \param[in] a_iOrient
495  * A sequential orientation index. This is
496  * converted to a bit orientation for finding the
497  * orthogonal directions.
498  * \param[in] a_cbox
499  * A centered box.
500  * \return The orientated box
501  *//*-----------------------------------------------------------------*/
502 
503 inline Box
504 CodimBox::orientBox(const int a_iOrient, const Box& a_cbox) const
505 {
506  return genOrientBox(seq2bit(a_iOrient), a_cbox);
507 }
508 
509 /*--------------------------------------------------------------------*/
510 /// Orient a centered box in the othogonal directions of the given
511 /// orientation. Used externally for any codimension.
512 /** \param[in] a_codim
513  * The codimension of the box
514  * \param[in] a_iOrient
515  * A sequential orientation index. This is
516  * converted to a bit orientation for finding the
517  * orthogonal directions.
518  * \param[in] a_cbox
519  * A centered box.
520  * \return The orientated box
521  *//*-----------------------------------------------------------------*/
522 
523 inline Box
524 CodimBox::orientBox(const int a_codim, const int a_iOrient, const Box& a_cbox)
525 {
527  return genOrientBox(seq2bit(a_codim, a_iOrient), a_cbox);
528 }
529 
530 /*--------------------------------------------------------------------*/
531 /// Report preallocation capability as non-static but symmetric
532 /** The size of the class is dependent upon the codimension. See
533  * BoxLayoutData.H for more info
534  *//*-----------------------------------------------------------------*/
535 
536 inline int
538 {
539  return 1;
540 }
541 
542 /*--------------------------------------------------------------------*/
543 /// Check that static arrays are initialized
544 /** Test assumes static arrays have at least been default
545  * initialized.
546  * \return T - Arrays initialized
547  *//*-----------------------------------------------------------------*/
548 
549 inline bool
551 {
552  return (indexFAB[0] != -1);
553 }
554 
555 /*--------------------------------------------------------------------*/
556 /// Return the FArray box from a bit description of the orientation
557 /** The appropriate FArray box for this codimension and orientation
558  * is given by a query to 'indexFAB'
559  * \param[in] a_bOrient
560  * Bit description of the orientation
561  *//*-----------------------------------------------------------------*/
562 
563 inline const FArrayBox&
564 CodimBox::getFArrayBox(const unsigned a_bOrient) const
565 {
566  return *(m_FArrayBox[bit2seq(a_bOrient)]);
567 }
568 
569 inline FArrayBox&
570 CodimBox::getFArrayBox(const unsigned a_bOrient)
571 {
572  return *(m_FArrayBox[bit2seq(a_bOrient)]);
573 }
574 
575 /*******************************************************************************
576  */
577 /// Factory object to create CodimBox(s) for BoxLayoutData and similar
578 /// containers.
579 /*
580  ******************************************************************************/
581 
582 class CodimBoxFactory : public DataFactory<CodimBox>
583 {
584 
585 public:
586  /// Constructor stores codimension
587  CodimBoxFactory(int a_codim)
588  :
589  m_codim(a_codim)
590  { }
591 
592  /// Create a new CodimBox
593  CodimBox* create(const Box& a_box,
594  int a_ncomps,
595  const DataIndex& a_datInd) const
596  {
597  return new CodimBox(m_codim, a_box, a_ncomps);
598  }
599 
600 private:
601  int m_codim; ///< Codimension
602 
603 };
604 
605 #include "NamespaceFooter.H"
606 #endif
const Box & box() const
Returns cell-centered box which defines the CodimBox.
Definition: CodimBox.H:436
static unsigned bitOrient[CodimBox::numAI]
Definition: CodimBox.H:255
Number of codimensions.
Definition: CodimBox.H:72
static int preAllocatable()
Report preallocation capability as non-static but symmetric.
Definition: CodimBox.H:537
int bit2seq(const unsigned a_bOrient) const
Definition: CodimBox.H:357
static int totOrient[numCD]
Definition: CodimBox.H:253
static Box genOrientBox(int a_bOrient, Box a_cbox)
#define CH_assert(cond)
Definition: CHArray.H:37
int getDirection(const int a_iOrient, int a_i) const
Get the 'i'th orthogonal direction of an orientation.
int getNumOrient() const
Number of different orientations of codimensional objects.
Definition: CodimBox.H:334
int m_nvar
Number of components in the FArrayBox.
Definition: CodimBox.H:272
int nComp() const
Number of components.
Definition: CodimBox.H:324
static void initialize()
Initialize static lookup tables.
FArrayBox & getFArrayBox(const unsigned i)
Return the FArray box from a bit description of the orientation.
Definition: CodimBox.H:570
CodimBox()
Default constructor.
const int SpaceDim
Definition: SPACE.H:39
int m_codim
Definition: CodimBox.H:270
int getCodim() const
Return the codimension.
Definition: CodimBox.H:314
~CodimBox()
Destructor.
void copy(const Box &a_R, const Interval &a_Cd, const CodimBox &a_src, const Interval &a_Cs)
Copy from another CodimBox.
Structure for passing component ranges in code.
Definition: Interval.H:23
void setVal(const Real a_x)
Set all values in all FAB.
void clear()
Deallocate.
unsigned seq2bit(const int a_iOrient) const
Definition: CodimBox.H:368
CodimBox & operator=(const CodimBox &)
static int indexFAB[CodimBox::numAI]
Definition: CodimBox.H:259
Definition: CodimBox.H:582
void define(const int a_codim, const Box &a_box, const int a_nvar)
Full define function.
double Real
Definition: REAL.H:33
CodimBoxFactory(int a_codim)
Constructor stores codimension.
Definition: CodimBox.H:587
Box m_box
Definition: CodimBox.H:268
int m_codim
Codimension.
Definition: CodimBox.H:601
const FArrayBox & operator()() const
Returns FArrayBox in the given direction (codimension 0)
static int numOrient[numCD]
Definition: CodimBox.H:251
void getDirections(const int a_iOrient, int *const a_dir) const
Get all the orthogonal directions of an orientation.
Definition: CodimBox.H:400
A Rectangular Domain on an Integer Lattice.
Definition: Box.H:465
An FArrayBox container for storage on the codimensions of a box.
Definition: CodimBox.H:66
Vector< FArrayBox * > m_FArrayBox
The FArrayBoxes for each orientation.
Definition: CodimBox.H:273
int size(const Box &a_R, const Interval &a_comps) const
Returns size of serialized data.
Definition: DataIndex.H:112
const FArrayBox & getSequential(const int a_iOrient) const
Returns FArrayBox from a sequential index.
Definition: CodimBox.H:465
void linearIn(void *a_buf, const Box &a_R, const Interval &a_comps)
Reads a serialized representation of this CodimBox.
Box orientBox(const int a_iOrient) const
Definition: CodimBox.H:486
static bool isInitialized()
Check that static arrays are initialized.
Definition: CodimBox.H:550
void linearOut(void *a_buf, const Box &a_R, const Interval &a_comps) const
Writes a serialized representation of this CodimBox.
Cell-Based or Node-Based Indices.
Definition: Box.H:41
Definition: FArrayBox.H:44
Factory object to data members of a BoxLayoutData container.
Definition: BoxLayoutData.H:30
Definition: CodimBox.H:73
IndexType ixType() const
centering
Definition: Box.H:1793
CodimBox * create(const Box &a_box, int a_ncomps, const DataIndex &a_datInd) const
Create a new CodimBox.
Definition: CodimBox.H:593
static void genGetDirections(const int a_codim, int a_bOrient, int *const a_dir)
Get all the orthogonal directions of an orientation (general)