Proto  3.2
Proto_LevelBoxData.H
Go to the documentation of this file.
1 #pragma once
2 #ifndef _PROTO_LEVELBOXDATA_H_
3 #define _PROTO_LEVELBOXDATA_H_
4 
5 #include "Proto_MemType.H"
6 #include "Proto_Memory.H"
7 #include "Proto_Point.H"
8 #include "Proto_Centering.H"
9 #include "Proto_Box.H"
10 #include "Proto_Centering.H"
11 #include "Proto_Copier.H"
13 #include "Proto_DataIterator.H"
14 #include "Proto_NeighborIterator.H"
15 #include "Proto_MayDay.H"
16 #include "Proto_SPMD.H"
17 #include <cstdlib> //for size_t
18 #include <iostream>
19 #include <cstring> // for Writing data to .vtk files.
20 
21 namespace Proto
22 {
24  // Forward Declarations
25  template<typename T, unsigned int C, MemType MEM, Centering CTR>
26  class LevelBoxData;
27 
28 
29 // =======================================================================
30 // LEVEL BOX DATA
31 
32  template<typename T, unsigned int C, MemType MEM, Centering CTR>
34 
35  /// Level Box Data
36  /**
37  A distributed data holder implementing the abstraction of a union of logically rectangular patches.
38  */
39  template
40  <typename T, unsigned int C=1, MemType MEM=MEMTYPE_DEFAULT, Centering CTR = PR_CELL>
41  class LevelBoxData
42  {
43  public:
44 
45  inline LevelBoxData();
46 
47  inline LevelBoxData(const DisjointBoxLayout& a_layout, const Point& a_ghostRegionSizes);
48 
49  template<unsigned int DST_DATA_COMPONENTS>
50  inline LevelBoxData(const LevelBoxData<T, DST_DATA_COMPONENTS, MEM, CTR>& a_srcData, unsigned int a_firstComponent);
51 
52  inline void define(const DisjointBoxLayout& a_layout, const Point& a_ghostRegionSizes);
53 
54  inline unsigned int numBoxes() const;
55 
56  inline void setToZero(int a_comp = -1);
57 
58  inline void setVal(T a_value, int a_comp = -1);
59 
60  /** Set data to random noise between two values */
61  inline void setRandom(T a_low, T a_high);
62 
63  /** Periodic boundaries are not considered domain boundaries */
64  inline void setDomainBoundary(T a_value, int a_comp = -1);
65 
66  inline void iota(Array<T,DIM> a_dx, Array<T,DIM> a_offset = Array<T,DIM>::Zeros());
67 
68  /// Function Initialization
69  /**
70  Initializes *this using a function with a signature similar to that used by
71  Proto::forallInPlace_p.
72 
73  Func signature:
74  void a_func(Point& a_point, Var<T, C, MEM>& a_thisData, ... )
75 
76  Like Proto::forall and its variants, this function can accept additional data
77  holders (e.g. other LevelBoxData instances) so long as all inputs are compatible
78  in the sense of LevelBoxData::compatible(...).
79  */
80  template<typename Func, typename... Srcs>
81  inline void initialize(Func& a_func, Srcs&... a_srcs);
82 
83  /// Exchange
84  /**
85  Copies data from the valid regions of *this into ghost regions. When MPI is
86  enabled, this function also takes care of any necessary communication between
87  patches on different processes.
88  */
89  inline void exchange();
90 
91  template< template<typename, unsigned int, MemType, Centering> class E_COPIER,
92  typename... Args>
93  inline void defineExchange(Args... a_args);
94  /// Copy To
95  /**
96  Copies data from the valid regions of this into the valid regions of another
97  LevelBoxData. This function copies both valid data and ghost regions if applicable.
98  If <code>a_dest</code> has a larger ghost region then <code>*this</code>, the user
99  is expected to call <code>exchange</code> to correctly fill the extra ghost cells.
100 
101  \param a_dest The destination to which data in *this will be copied.
102  */
103  template<MemType DST_MEM>
104  inline void copyTo(LevelBoxData<T, C, DST_MEM, CTR>& a_dest) const;
105 
106  template<MemType DST_MEM>
107  inline void copyToSimple(LevelBoxData<T, C, DST_MEM, CTR>& a_dest) const;
108 
109  /// Copy With Coarsen
110  /**
111  Coarsen's the data in *this by applying a geometric average before copying
112  into the destination.
113  */
114  inline void coarsenTo(LevelBoxData& a_dest, Point a_refRatio) const;
116  /// Linear Offset
117  /**
118  Returns the serial index of the first data element of this stored on
119  this a_proc.
120  */
121  inline unsigned int offset(int a_proc) const;
122  inline unsigned int offset() const { return offset(Proto::procID()); }
123 
124  /// Patch Size
125  /**
126  Computes the size (in data cells) of a single patch in *this.
127  The output accounts for both ghost cells and centering.
128  */
129  inline unsigned int patchSize() const;
130 
131  /// Patch Box
132  /**
133  Returns the Box corresponding to the valid cells of the data at a given index.
134  This box does not include ghost cells, but does account for centering.
135  For cell centered data, this is the same as layout()[a_index], but will
136  in general be different for other types of centering.
137  */
138  inline Box patchBox(const DataIndex<BoxPartition>& a_index) const;
140  /// Linear In (Patch, Box)
141  /**
142  Reads data from a serial buffer, populating the patch associated with
143  a_index within the subset specified by a_box.
144 
145  \param a_buffer The read buffer
146  \param a_box A box defining the subset to read into
147  \param a_index The index defining the patch to read into
148  */
149  inline void linearIn(void* a_buffer,
150  const Box& a_box,
151  const DataIndex<BoxPartition>& a_index);
152 
153  /// Linear Out (Patch, Box)
154  /**
155  Writes data to a serial buffer, from the patch associated with
156  a_index within the subset specified by a_box.
157 
158  \param a_buffer The write buffer
159  \param a_box A box defining the subset to write from
160  \param a_index The index defining the patch to write from
161  */
162  inline void linearOut(void* a_buffer,
163  const Box& a_box,
164  const DataIndex<BoxPartition>& a_index) const;
165 
166  /// Serial Size (Patch, Box)
167  /**
168  Computes the size in bytes of the serial data buffer needed to hold
169  the patch associated with a_index within the subset specified by a_box.
170 
171  \param a_box A box defining the subset of a patch
172  \param a_index The index defining the patch
173  */
174  inline size_t linearSize(const Box& a_box,
175  const DataIndex<BoxPartition>& a_index) const;
176  /// Linear In (Patch)
177  /**
178  Overload of LinearIn that always reads in a full patch
179  */
180  inline void linearIn(void* a_buffer,
181  const DataIndex<BoxPartition>& a_index);
182  /// Linear Out (Patch)
183  /**
184  Overload of LinearOut that always writes out a full patch
185  */
186  inline void linearOut(void* a_buffer,
187  const DataIndex<BoxPartition>& a_index) const;
188  /// Size (Patch)
189  /**
190  Overload of Size that always computes the size of a full patch
191  */
192  inline size_t linearSize( const DataIndex<BoxPartition>& a_index) const;
193 
194  /// Linear In (All Local Data)
195  /**
196  Overload of LinearIn that reads all local data
197  */
198  inline void linearIn(void* a_buffer);
199  /// Linear Out (All Local Data)
200  /**
201  Overload of LinearOut that writes out all local data
202  */
203  inline void linearOut(void* a_buffer) const;
204  /// Size (All Local Data)
205  /**
206  Overload of Size that computes the full local size of the data in *this
207  */
208  inline size_t linearSize() const;
209 
210  /// Get Box Layout
211  inline DisjointBoxLayout layout() const { return m_layout; }
212 
213  /// Get Ghost Size
214  inline Point ghost() const { return m_ghost; }
215 
216  /// Increment
217  /**
218  Add a scaled multiple of another LevelBoxData.
219  */
220  inline void increment(LevelBoxData<T, C, MEM, CTR>& a_data, T a_scale = 1.0);
221 
222  /// Multiply By Scalar
223  inline void operator*=(T a_scale);
224 
225  /// Add a Constant
226  inline void operator+=(T a_scale);
227 
228  /// Reduction
229  /**
230  Computes a reduction for a component over all valid cells (ghost region is ignored)
231  */
232  template<Proto::Operation OP>
233  inline double reduce(unsigned int a_comp = 0) const;
234 
235  /// Maximum Absolute Value
236  /**
237  Computes the maximum absolute value for a component over all valid cells (ghost region is ignored)
238  */
239  inline double absMax(unsigned int a_comp = 0) const;
240 
241  /// Sum
242  /**
243  Computes the sum of a component over all valid cells (ghost region is ignored)
244  */
245  inline double sum(unsigned int a_comp = 0) const;
246 
247  /// Max
248  /**
249  Computes the max value of a component over all valid cells (ghost region is ignored)
250  */
251  inline double max(unsigned int a_comp = 0) const;
252 
253  /// Min
254  /**
255  Computes the min value of a component over all valid cells (ghost region is ignored)
256  */
257  inline double min(unsigned int a_comp = 0) const;
258 
259  /// Integral
260  /**
261  Compute the integral over this using an isotropic grid spacing.
262 
263  \param a_dx Isotropic grid spacing
264  \param a_comp A component to integrate over
265  */
266  inline double integrate(double a_dx, unsigned int a_comp = 0) const;
267 
268  /// Integral (Anisotropic)
269  /**
270  Compute the integral over this using an isotropic grid spacing.
271 
272  \param a_dx Isotropic grid spacing
273  \param a_comp A component to integrate over
274  */
275  inline double integrate(Array<double, DIM> a_dx, unsigned int a_comp = 0) const;
276 
277  /// Integral of Absolute Value
278  /**
279  Compute the integral of the absolute value over this using an isotropic grid spacing.
280 
281  \param a_dx Isotropic grid spacing
282  \param a_comp A component to integrate over
283  */
284  inline double integrateAbs(double a_dx, unsigned int a_comp = 0) const;
285 
286  /// Integral of Absolute Value (Anisotropic)
287  /**
288  Compute the integral of the absolute value over this using an isotropic grid spacing.
289 
290  \param a_dx Isotropic grid spacing
291  \param a_comp A component to integrate over
292  */
293  inline double integrateAbs(Array<double, DIM> a_dx, unsigned int a_comp = 0) const;
294 
295  /// Query Layout Compatibility
296  template<typename _T, unsigned int _C, MemType _MEM, Centering _CTR>
297  inline bool compatible(const LevelBoxData<_T, _C, _MEM, _CTR>& a_data) const;
298 
299  /// Query Layout Compatibility
300  inline bool compatible(const DisjointBoxLayout& a_layout) const;
301  /// Query Layout Compatibility
302  inline bool compatible(const DataIterator<BoxPartition>& a_iter) const;
303  /// Query Layout Compatibility
304  inline bool compatible(const DataIndex<BoxPartition>& a_index) const;
305 
306  //=======================================
307  // DATA ITERATOR INTERFACE
308 
309  /// Get Patch
310  inline BoxData<T,C,MEM>& operator[](const DataIndex<BoxPartition>& a_index);
311  /// Get Const Patch
312  inline const BoxData<T,C,MEM>& operator[](const DataIndex<BoxPartition>& a_index) const;
313  /// Get Patch (Face / Edge Centering)
314  inline BoxData<T,C,MEM>& operator()(const DataIndex<BoxPartition>& a_index, unsigned int a_dir = 0);
315  /// Get Const Patch (Face / Edge Centering)
316  inline const BoxData<T,C,MEM>& operator()(const DataIndex<BoxPartition>& a_index, unsigned int a_dir = 0) const;
317 
318  /// Get Iterator
319  inline DataIterator<BoxPartition> begin() const;
320  inline DataIterator<BoxPartition> end() const;
321 
322  /// deprecated but useful
324  {
325  return begin();
326  }
327  /// For debugging purposes.
328  int s_verbosity = 0;
329 
330  private:
331 
332  std::vector<std::vector<shared_ptr<BoxData<T, C, MEM> > >> m_data;
336  std::shared_ptr<LevelExchangeCopier<T, C, MEM, CTR>> m_exchangeCopier;
337 
338  // disallow copy constructors and assignment operators
339  // to avoid very hard-to-find performance problems
342  }; // end class LevelBoxData
343 
344 // =======================================================================
345 // LEVEL BOX DATA COPIER OP
346 
347  /// Level Copier Operator
348  /**
349  Copier operator which provides the necessary interface to copy to and from
350  LevelBoxData using the Copier interface. See Proto_Copier.H.
351  */
352  template
353  <typename T, unsigned int C, MemType SRC_MEM, MemType DST_MEM, Centering CTR>
355  {
356  public:
359 
360  inline LevelCopierOp(){};
361 
362  inline LevelCopierOp(
365 
366  /// Get Serialized Size
367  inline int linearSize(const Box& a_bx,
368  const DataIndex<BoxPartition>& a_index) const;
369 
370  /// Serial Write To Buffer
371  inline void linearOut(void* a_buf, const LevelMotionItem& a_info);
372  //inline void linearOut(void* a_buf,
373  // const Box& a_bx,
374  // const DataIndex<BoxPartition>& a_index) const;
375 
376  /// Serial Read From Buffer
377  inline void linearIn(void* a_buf, const LevelMotionItem& a_info);
378  //inline void linearIn(void* a_buf,
379  // const Box& a_bx,
380  // const DataIndex<BoxPartition>& a_index) const;
381 
382  /// Local Copy
383  inline void localCopy(const LevelMotionItem& a_info);
384  //inline void localCopy(
385  // const Box& a_domain, const DataIndex<BoxPartition>& a_domainIndex,
386  // const Box& a_range, const DataIndex<BoxPartition>& a_rangeIndex) const;
387  };
388 // =======================================================================
389 // LEVEL BOX DATA COPIERS
390 
391  /// Level Copier
392  /**
393  A Copier used to implement copying of data between LevelBoxData objects.
394  */
395  template<typename T, unsigned int C, MemType SRC_MEM, MemType DST_MEM, Centering CTR>
396  class LevelCopier
397  : public Copier<LevelCopierOp<T, C, SRC_MEM, DST_MEM, CTR>, BoxPartition, BoxPartition, SRC_MEM, DST_MEM>
398  {
399  public:
400 
401  inline LevelCopier(){};
402 
403  /// Build Copier Motion Plan
404  inline void buildMotionPlans(LevelCopierOp<T, C, SRC_MEM, DST_MEM, CTR>& a_op);
405  };
406 
407  /// Exchange Copier
408  /**
409  A Copier used to execute the data copies necessary for filling ghost regions
410  within a LevelBoxData.
411  */
412  template<typename T, unsigned int C, MemType MEM, Centering CTR>
413  class LevelExchangeCopier
414  : public Copier<LevelCopierOp<T, C, MEM, MEM, CTR>, BoxPartition, BoxPartition, MEM, MEM>
415  {
416  public:
417 
419 
420  /// Build Copier Motion Plan
421  inline virtual void buildMotionPlans(LevelCopierOp<T, C, MEM, MEM, CTR>& a_op);
422  };
423 
424 
425  /// Interpolate Boundaries
426  /**
427  General utility function for interpolating data into coarse-fine boundaries.
428  This version creates a temporary LevelBoxData representing the coarsened fine region.
429  */
430  template<typename T, unsigned int C, MemType MEM, Centering CTR>
431  void interpBoundaries(
434  InterpStencil<T>& a_interp);
435 
436  /// Interpolate Boundaries
437  /**
438  General utility function for interpolating data into coarse-fine boundaries.
439  This version does not create any temporary LevelBoxData but requires an additional
440  input dataholder representing the coarsened fine region.
441  */
442  template<typename T, unsigned int C, MemType MEM, Centering CTR>
443  void interpBoundaries(
446  LevelBoxData<T, C, MEM, CTR>& a_crseFine,
447  InterpStencil<T>& a_interp);
448 
449  /// Average Down
450  /**
451  General utility function for averaging fine data onto coarse data
452  */
453  template<typename T, unsigned int C, MemType MEM, Centering CTR>
454  void averageDown(
457  Point a_refRatio);
458 
459  /// Average Down (Scalar Refinement Ratio)
460  /**
461  General utility function for averaging fine data onto coarse data
462  */
463  template<typename T, unsigned int C, MemType MEM, Centering CTR>
464  void averageDown(
467  int a_refRatio);
468 
469  /// Average Down
470  /**
471  General utility function for averaging fine data onto coarse data
472  */
473  template<typename T, unsigned int C, MemType MEM, Centering CTR>
474  void averageDown(
477  LevelBoxData<T, C, MEM, CTR>& a_crseFine,
478  Point a_refRatio);
479 
480  /// Average Down (Scalar Refinement Ratio)
481  /**
482  General utility function for averaging fine data onto coarse data
483  */
484  template<typename T, unsigned int C, MemType MEM, Centering CTR>
485  void averageDown(
488  LevelBoxData<T, C, MEM, CTR>& a_crseFine,
489  int a_refRatio);
490 
491 
492 #include "implem/Proto_LevelBoxDataImplem.H"
493 } //end namespace Proto
494 #endif
double integrate(double a_dx, unsigned int a_comp=0) const
Integral.
Definition: Proto_LevelBoxData.H:637
DataIterator< BoxPartition > dataIterator() const
deprecated but useful
Definition: Proto_LevelBoxData.H:323
void iota(Array< T, DIM > a_dx, Array< T, DIM > a_offset=Array< T, DIM >::Zeros())
Definition: Proto_LevelBoxData.H:129
DataIterator< BoxPartition > end() const
Definition: Proto_LevelBoxData.H:518
void defineExchange(Args... a_args)
Definition: Proto_LevelBoxData.H:335
Distributed Data Iterator.
Definition: Proto_DataIndex.H:10
std::vector< std::vector< shared_ptr< BoxData< T, C, MEM > > > > m_data
Definition: Proto_LevelBoxData.H:332
Box patchBox(const DataIndex< BoxPartition > &a_index) const
Patch Box.
Multidimensional Rectangular Array.
Definition: Proto_BoxData.H:314
int s_verbosity
For debugging purposes.
Definition: Proto_LevelBoxData.H:328
Disjoint Box Layout.
Definition: Proto_DisjointBoxLayout.H:30
LevelExchangeCopier()
Definition: Proto_LevelBoxData.H:418
LevelBoxData< T, C, DST_MEM, CTR > * m_dst
Definition: Proto_LevelBoxData.H:358
unsigned int patchSize() const
Patch Size.
double reduce(unsigned int a_comp=0) const
Reduction.
Definition: Proto_LevelBoxData.H:599
void initialize(Func &a_func, Srcs &... a_srcs)
Function Initialization.
DataIterator< BoxPartition > begin() const
Get Iterator.
Definition: Proto_LevelBoxData.H:512
MemType
Definition: Proto_MemType.H:7
void interpBoundaries(LevelBoxData< T, C, MEM, CTR > &a_crse, LevelBoxData< T, C, MEM, CTR > &a_fine, InterpStencil< T > &a_interp)
Interpolate Boundaries.
Definition: Proto_LevelBoxData.H:955
Level Box Data.
Definition: Proto_HDF5.H:17
Definition: Proto_InterpStencil.H:14
bool m_isDefined
Definition: Proto_LevelBoxData.H:335
BoxData< T, C, MEM > & operator[](const DataIndex< BoxPartition > &a_index)
Get Patch.
Definition: Proto_LevelBoxData.H:525
size_t linearSize() const
Size (All Local Data)
Definition: Proto_LevelBoxData.H:500
Level Copier Operator.
Definition: Proto_LevelBoxData.H:354
An interval in DIM dimensional space.
Definition: Proto_Box.H:29
void exchange()
Exchange.
LevelBoxData< T, C, MEM, CTR > & operator=(const LevelBoxData< T, C, MEM, CTR > &a_rhs)
DisjointBoxLayout layout() const
Get Box Layout.
Definition: Proto_LevelBoxData.H:211
BoxData< T, C, MEM > & operator()(const DataIndex< BoxPartition > &a_index, unsigned int a_dir=0)
Get Patch (Face / Edge Centering)
Definition: Proto_LevelBoxData.H:541
std::shared_ptr< LevelExchangeCopier< T, C, MEM, CTR > > m_exchangeCopier
Definition: Proto_LevelBoxData.H:336
LevelCopierOp()
Definition: Proto_LevelBoxData.H:360
double sum(unsigned int a_comp=0) const
Sum.
Definition: Proto_LevelBoxData.H:630
MotionItem< BoxPartition, BoxPartition > LevelMotionItem
Definition: Proto_LevelBoxData.H:23
int procID()
Get Local Process ID.
Definition: Proto_SPMD.H:39
Level Copier.
Definition: Proto_LevelBoxData.H:396
void operator*=(T a_scale)
Multiply By Scalar.
Definition: Proto_LevelBoxData.H:574
void increment(LevelBoxData< T, C, MEM, CTR > &a_data, T a_scale=1.0)
Increment.
Definition: Proto_LevelBoxData.H:558
void linearIn(void *a_buffer, const Box &a_box, const DataIndex< BoxPartition > &a_index)
Linear In (Patch, Box)
Definition: Proto_LevelBoxData.H:415
void copyTo(LevelBoxData< T, C, DST_MEM, CTR > &a_dest) const
Copy To.
Definition: Proto_LevelBoxData.H:363
void setDomainBoundary(T a_value, int a_comp=-1)
void operator+=(T a_scale)
Add a Constant.
Definition: Proto_LevelBoxData.H:586
void linearOut(void *a_buffer, const Box &a_box, const DataIndex< BoxPartition > &a_index) const
Linear Out (Patch, Box)
Definition: Proto_LevelBoxData.H:449
unsigned int numBoxes() const
void setVal(T a_value, int a_comp=-1)
Definition: Proto_Array.H:17
void averageDown(LevelBoxData< T, C, MEM, CTR > &a_crse, LevelBoxData< T, C, MEM, CTR > &a_fine, Point a_refRatio)
Average Down.
Definition: Proto_LevelBoxData.H:994
void copyToSimple(LevelBoxData< T, C, DST_MEM, CTR > &a_dest) const
Definition: Proto_LevelBoxData.H:349
LevelCopier()
Definition: Proto_LevelBoxData.H:401
Integer Valued Vector.
Definition: Proto_Point.H:24
Point ghost() const
Get Ghost Size.
Definition: Proto_LevelBoxData.H:214
void coarsenTo(LevelBoxData &a_dest, Point a_refRatio) const
Copy With Coarsen.
Definition: Proto_LevelBoxData.H:391
DisjointBoxLayout m_layout
Definition: Proto_LevelBoxData.H:334
void define(const DisjointBoxLayout &a_layout, const Point &a_ghostRegionSizes)
Definition: Proto_LevelBoxData.H:64
bool compatible(const LevelBoxData< _T, _C, _MEM, _CTR > &a_data) const
Query Layout Compatibility.
Definition: Proto_LevelBoxData.H:1043
Definition: Proto_Centering.H:9
unsigned int offset() const
Definition: Proto_LevelBoxData.H:122
void setRandom(T a_low, T a_high)
double min(unsigned int a_comp=0) const
Min.
Definition: Proto_LevelBoxData.H:676
void setToZero(int a_comp=-1)
double max(unsigned int a_comp=0) const
Max.
Definition: Proto_LevelBoxData.H:669
LevelBoxData< T, C, SRC_MEM, CTR > * m_src
Definition: Proto_LevelBoxData.H:357
double absMax(unsigned int a_comp=0) const
Maximum Absolute Value.
Definition: Proto_LevelBoxData.H:623
double integrateAbs(double a_dx, unsigned int a_comp=0) const
Integral of Absolute Value.
Definition: Proto_LevelBoxData.H:653
#define MEMTYPE_DEFAULT
Definition: Proto_MemType.H:24
Abstract Generic Parallel Copier.
Definition: Proto_Copier.H:60
LevelBoxData()
Definition: Proto_LevelBoxData.H:4
Centering
Definition: Proto_Centering.H:7
Point m_ghost
Definition: Proto_LevelBoxData.H:333
Definition: Proto_Copier.H:22
Exchange Copier.
Definition: Proto_LevelBoxData.H:33