Chombo + EB  3.2
MiniIFFABI.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 _MINIIFFABI_H_
12 #define _MINIIFFABI_H_
13 #include "MayDay.H"
14 #include "FaceIterator.H"
15 #include <typeinfo>
16 #include "NamespaceHeader.H"
17 
18 template <class T>
19 bool MiniIFFAB<T>::s_verbose = false;
20 
21 /******************/
22 template <class T> inline
24 {
25  setDefaultValues();
26 }
27 /******************/
28 template <class T> inline
30 {
31  clear();
32 }
33 /******************/
34 template <class T> inline
36 MiniIFFAB(const Box& a_box,
37  const EBGraph& a_ebgraph,
38  const int& a_direction,
39  const int& a_nvarin)
40 {
41  setDefaultValues();
42  define(a_box, a_ebgraph, a_direction, a_nvarin);
43 }
44 /******************/
45 template <class T> inline
46 void
48 define(const Box& a_box,
49  const EBGraph& a_ebgraph,
50  const int& a_direction,
51  const int& a_nvarin)
52 {
53  clear();
54  m_isDefined = true;
55  CH_assert(a_nvarin > 0);
56  CH_assert((a_direction >= 0) && (a_direction < SpaceDim));
57 
58  m_direction = a_direction;
59  m_ebgraph = a_ebgraph;
60  m_nComp = a_nvarin;
61  m_box = a_box;
62 
63  m_faces = m_ebgraph.getMultiValuedFaces(a_direction, a_box);
64  m_nFaces = m_faces.size();
65 
66  m_data = new T[m_nComp*m_nFaces];
67 }
68 /******************/
69 template <class T> inline
71 MiniIFFAB(const IntVectSet& a_ivs,
72  const EBGraph& a_ebgraph,
73  const int& a_direction,
74  const int& a_nvarin)
75 {
76  setDefaultValues();
77  define(a_ivs, a_ebgraph, a_direction, a_nvarin);
78 }
79 /******************/
80 template <class T> inline
81 void
83 define(const IntVectSet& a_ivs,
84  const EBGraph& a_ebgraph,
85  const int& a_direction,
86  const int& a_nvarin)
87 {
88  clear();
89  m_isDefined = true;
90  CH_assert(a_nvarin > 0);
91  CH_assert((a_direction >= 0) && (a_direction < SpaceDim));
92 
93  m_direction = a_direction;
94  m_ebgraph = a_ebgraph;
95  m_nComp = a_nvarin;
96  m_box = a_ivs.minBox();
97 
98  FaceIterator faceit(a_ivs, a_ebgraph, a_direction, FaceStop::SurroundingWithBoundary);
99  m_faces = faceit.getVector();
100  m_nFaces = m_faces.size();
101 
102  m_data = new T[m_nComp*m_nFaces];
103 }
104 /******************/
105 template <class T> inline
106 void
108 setVal(const T& a_value)
109 {
110  CH_assert(isDefined());
111  for (int ivec = 0; ivec < m_nFaces*m_nComp; ivec++)
112  {
113  m_data[ivec] = a_value;
114  }
115 }
116 /******************/
117 template <class T> inline
118 void
120 setVal(int ivar, const T& a_value)
121 {
122  CH_assert(isDefined());
123  CH_assert(ivar >= 0);
124  CH_assert(ivar < m_nComp);
125 
126  for (int ivec = ivar; ivec < m_nFaces*m_nComp; ivec += m_nComp)
127  {
128  m_data[ivec] = a_value;
129  }
130 }
131 /******************/
132 template <class T> inline
133 void
135 copy(const Box& a_fromBox,
136  const Interval& a_dstInterval,
137  const Box& a_toBox,
138  const MiniIFFAB<T>& a_src,
139  const Interval& a_srcInterval)
140 {
141  CH_assert(isDefined());
142  CH_assert(a_src.isDefined());
143  CH_assert(a_srcInterval.size() == a_dstInterval.size());
144  CH_assert(a_dstInterval.begin() >= 0);
145  CH_assert(a_srcInterval.begin() >= 0);
146  CH_assert(a_dstInterval.end() < m_nComp);
147  CH_assert(a_srcInterval.end() < a_src.m_nComp);
148 
149  if ((!m_box.isEmpty()) && (!a_src.m_box.isEmpty()))
150  {
151  CH_assert(a_fromBox == a_toBox);
152  Box intBox = a_toBox;
153  Box boxIntersect = m_box;
154  boxIntersect &= a_src.m_box;
155  boxIntersect &= intBox;
156  int compSize = a_srcInterval.size();
157 
158  for (int iface = 0; iface < m_faces.size(); iface++)
159  {
160  const FaceIndex& face = m_faces[iface];
161  const IntVect& ivHi = face.gridIndex(Side::Hi);
162  const IntVect& ivLo = face.gridIndex(Side::Lo);
163  if (boxIntersect.contains(ivHi) || boxIntersect.contains(ivLo))
164  {
165  for (int icomp = 0; icomp < compSize; icomp++)
166  {
167  int isrccomp = a_srcInterval.begin() + icomp;
168  int idstcomp = a_dstInterval.begin() + icomp;
169  (*this)(face, idstcomp) = a_src(face, isrccomp);
170  }
171  }
172  }
173  }
174 }
175 /*********/
176 template <class T> inline
177 T*
179 getIndex(const FaceIndex& a_face, const int& a_comp) const
180 {
181  CH_assert(m_isDefined);
182  CH_assert((a_comp >= 0) && (a_comp < this->m_nComp));
183 
184  T* dataPtr = this->m_data;
185  bool found = false;
186  T* retval=NULL;
187  for (int iface = 0; iface < m_faces.size(); ++iface)
188  {
189  if (a_face == m_faces[iface])
190  {
191  found = true;
192  retval=dataPtr;
193  }
194  if (found) break;
195  dataPtr++;
196  }
197  if (!found)
198  {
199  MayDay::Error("MiniIFFAB:index not found");
200  }
201  retval += a_comp*this->m_nFaces;
202  return retval;
203 
204 }
205 template <class T> inline
207  MiniIFFAB<T>& a_original)
208  :m_data(a_original.dataPtr(a_comps.begin())),
209  m_faces(a_original.m_faces),
210  m_nFaces(a_original.m_nFaces),
211  m_nComp(a_comps.size()),
212  m_direction(a_original.m_direction),
213  m_aliased(true),
214  m_isDefined(a_original.m_isDefined)
215 {
216 }
217 
218 /*********/
219 template <class T> inline
220 void
223 {
224  if ((m_data != NULL)&&(!m_aliased))
225  {
226  delete[] m_data;
227  m_data = NULL;
228  }
229  m_faces.resize(0);
231 }
232 /*************************/
233 template <class T> inline
234 T&
236  const int& a_comp)
237 {
238  CH_assert(isDefined());
239  CH_assert(a_ndin.direction() == m_direction);
240 
241  T* dataPtr = getIndex(a_ndin, a_comp);
242  return(*dataPtr);
243 }
244 /**************************/
245 template <class T> inline
246 const T&
248  const int& a_comp) const
249 {
250  CH_assert(isDefined());
251  CH_assert(a_ndin.direction() == m_direction);
252 
253  T* dataPtr = getIndex(a_ndin, a_comp);
254  return(*dataPtr);
255 }
256 /******************/
257 template <class T> inline
258 T*
259 MiniIFFAB<T>::dataPtr(const int& a_comp)
260 {
261  CH_assert(a_comp >= 0);
262  CH_assert(a_comp <= m_nComp);
263  return m_data + a_comp*m_nFaces;
264 }
265 /******************/
266 template <class T> inline
267 const T*
268 MiniIFFAB<T>::dataPtr(const int& a_comp) const
269 {
270  CH_assert(a_comp >= 0);
271  CH_assert(a_comp <= m_nComp);
272  return m_data + a_comp*m_nFaces;
273 }
274 /******************/
275 template <class T> inline
276 void
278 {
279  m_isDefined = false;
280  m_data = NULL;
281  m_nFaces = 0;
282  m_nComp = 0;
283  m_direction = -1;
284 }
285 /******************/
286 template <class T> inline
287 void
289 getFaceSubset(Vector<FaceIndex>& a_subFaces, const Box& a_subBox) const
290 {
291  a_subFaces.resize(0);
292  for (int iface = 0; iface < m_faces.size(); iface++)
293  {
294  if ((a_subBox.contains(m_faces[iface].gridIndex(Side::Lo))) ||
295  (a_subBox.contains(m_faces[iface].gridIndex(Side::Hi))))
296  {
297  a_subFaces.push_back(m_faces[iface]);
298  }
299  }
300 }
301 /*********************/
302 template <class T> inline
303 int
305 size(const Box& a_region,
306  const Interval& a_comps) const
307 {
308  CH_assert(isDefined());
309  //create set of cells in fab that are also in the input region
310  Vector<FaceIndex> faces;
311  getFaceSubset(faces, a_region);
312 
313  //account for list of faces
314  int facesize = linearListSize(faces);
315 
316  //add for each data pt
317  int datasize = 0;
318  for (int iface = 0; iface < faces.size(); iface++)
319  {
320  for (int icomp = a_comps.begin(); icomp <= a_comps.end(); icomp++)
321  {
322  const T& dataPt = (*this)(faces[iface], icomp);
323  int pointsize = CH_XD::linearSize(dataPt);
324  datasize += pointsize;
325  }
326  }
327 
328  int retval = facesize + datasize;
329 
330  if (s_verbose)
331  {
332  pout() << "###############" << std::endl;
333  pout() << "MiniIFFAB size() for region " << m_ebgraph.getRegion() << std::endl;
334  pout() << " a_comps = (" << a_comps.begin() << "," << a_comps.end() << ")," << endl;
335  pout() << "m_box = " << m_box;
336  pout() << "size = " << retval << std::endl;
337  pout() << "###############" << std::endl;
338  }
339 
340  return retval;
341 }
342 /********************/
343 template <class T> inline
344 void
346 linearOut(void* a_buf,
347  const Box& a_region,
348  const Interval& a_comps) const
349 {
350  CH_assert(isDefined());
351  Vector<FaceIndex> faces;
352  getFaceSubset(faces, a_region);
353 
354  //output the faces.
355  unsigned char* charbuffer = (unsigned char *) a_buf;
356  linearListOut(charbuffer, faces);
357  charbuffer += linearListSize(faces);
358 
359  //output the data
360  const MiniIFFAB<T>& thisFAB = *this;
361  for (int iface = 0; iface < faces.size(); iface++)
362  {
363  const FaceIndex& face = faces[iface];
364  for (int icomp = a_comps.begin(); icomp <= a_comps.end(); icomp++)
365  {
366  //output the data into the buffer
367  const T& dataPt = thisFAB(face, icomp);
368  CH_XD::linearOut(charbuffer, dataPt);
369  //increment the buffer offset
370  charbuffer += CH_XD::linearSize(dataPt);
371  }
372  }
373 }
374 /********************/
375 template <class T> inline
376 void
378 linearIn(void* a_buf, const Box& a_region, const Interval& a_comps)
379 {
380  CH_assert(isDefined());
381 
382  unsigned char* charbuffer = (unsigned char *) a_buf;
383  Vector<FaceIndex> faces;
384  linearListIn(faces, charbuffer);
385  charbuffer += linearListSize(faces);
386 
387  for (int iface = 0; iface < faces.size(); iface++)
388  {
389  const FaceIndex& face = faces[iface];
390  for (int icomp = a_comps.begin(); icomp <= a_comps.end(); icomp++)
391  {
392  //input the data
393  T& dataPt = (*this)(face, icomp);
394  CH_XD::linearIn(dataPt, charbuffer) ;
395  //increment the buffer offset
396  charbuffer += CH_XD::linearSize(dataPt);
397  }
398  }
399 }
400 /********************/
401 /******************/
402 
403 #include "NamespaceFooter.H"
404 #endif
std::ostream & pout()
Use this in place of std::cout for program output.
bool isDefined() const
Definition: MiniIFFAB.H:150
int m_nComp
Definition: MiniIFFAB.H:223
int size(const Box &R, const Interval &comps) const
Definition: MiniIFFABI.H:305
int m_nFaces
Definition: MiniIFFAB.H:222
IntVect size() const
size functions
Definition: Box.H:1803
An irregular domain on an integer lattice.
Definition: IntVectSet.H:44
#define CH_assert(cond)
Definition: CHArray.H:37
~MiniIFFAB()
Definition: MiniIFFABI.H:29
void linearOut(void *buf, const Box &R, const Interval &comps) const
Definition: MiniIFFABI.H:346
void setVal(const T &value)
Definition: MiniIFFABI.H:108
Definition: FaceIndex.H:28
void linearListOut(void *const a_outBuf, const Vector< T > &a_inputT)
Definition: SPMDI.H:258
int size() const
Definition: Interval.H:75
bool m_aliased
Definition: MiniIFFAB.H:225
int begin() const
Definition: Interval.H:97
Iterator over faces within an IntVectSet and an Ebgraph.
Definition: FaceIterator.H:67
EBGraph m_ebgraph
Definition: MiniIFFAB.H:212
Definition: FaceIterator.H:48
bool contains(const IntVect &p) const
Definition: Box.H:1871
const IntVect & gridIndex(const Side::LoHiSide &a_sd) const
void clear()
Definition: MiniIFFABI.H:222
MiniIFFAB()
Definition: MiniIFFABI.H:23
const int & direction() const
Definition: LoHiSide.H:31
Geometric description within a box.
Definition: EBGraph.H:427
const int SpaceDim
Definition: SPACE.H:38
void setDefaultValues()
Definition: MiniIFFABI.H:277
void resize(unsigned int isize)
Definition: Vector.H:346
void linearIn(void *buf, const Box &R, const Interval &comps)
Definition: MiniIFFABI.H:378
void push_back(const T &in)
Definition: Vector.H:295
Structure for passing component ranges in code.
Definition: Interval.H:23
static bool s_verbose
Definition: MiniIFFAB.H:209
const Vector< FaceIndex > & getVector() const
T * m_data
Definition: MiniIFFAB.H:220
void linearOut(void *const a_outBuf, const T &inputT)
Definition: SPMDI.H:33
int m_direction
Definition: MiniIFFAB.H:224
int linearSize(const T &inputT)
Definition: SPMDI.H:21
size_t size() const
Definition: Vector.H:192
Vector< FaceIndex > m_faces
Definition: MiniIFFAB.H:221
T * dataPtr(const int &a_comp)
Definition: MiniIFFABI.H:259
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.
void getFaceSubset(Vector< FaceIndex > &a_subFaces, const Box &a_subBox) const
Definition: MiniIFFABI.H:289
T * getIndex(const FaceIndex &a_face, const int &a_comp) const
Definition: MiniIFFABI.H:179
A Rectangular Domain on an Integer Lattice.
Definition: Box.H:465
void copy(const Box &a_intBox, const Interval &a_destInterval, const Box &a_toBox, const MiniIFFAB< T > &a_src, const Interval &a_srcInterval)
Definition: MiniIFFABI.H:135
T & operator()(const FaceIndex &a_face, const int &varlocin)
Definition: MiniIFFABI.H:235
void linearListIn(Vector< T > &a_outputT, const void *const a_inBuf)
Definition: SPMDI.H:229
Definition: MiniIFFAB.H:34
An integer Vector in SpaceDim-dimensional space.
Definition: CHArray.H:42
Definition: LoHiSide.H:30
int linearListSize(const Vector< T > &a_input)
Definition: SPMDI.H:290
bool m_isDefined
Definition: MiniIFFAB.H:226
int end() const
Definition: Interval.H:102
bool isEmpty() const
{ Comparison Functions}
Definition: Box.H:1846
void linearIn(T &a_outputT, const void *const inBuf)
Definition: SPMDI.H:27
const Box & minBox() const
Returns the minimum enclosing box of this IntVectSet.
Box m_box
Definition: MiniIFFAB.H:211
const Box & getRegion() const
Definition: EBGraph.H:829
void define(const IntVectSet &a_ivs, const EBGraph &a_ebgraph, const int &a_direction, const int &a_nvarin)
Definition: MiniIFFABI.H:83