Chombo + EB  3.0
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
70 void
72 setVal(const T& a_value)
73 {
74  CH_assert(isDefined());
75  for (int ivec = 0; ivec < m_nFaces*m_nComp; ivec++)
76  {
77  m_data[ivec] = a_value;
78  }
79 }
80 /******************/
81 template <class T> inline
82 void
84 setVal(int ivar, const T& a_value)
85 {
86  CH_assert(isDefined());
87  CH_assert(ivar >= 0);
88  CH_assert(ivar < m_nComp);
89 
90  for (int ivec = ivar; ivec < m_nFaces*m_nComp; ivec += m_nComp)
91  {
92  m_data[ivec] = a_value;
93  }
94 }
95 /******************/
96 template <class T> inline
97 void
99 copy(const Box& a_fromBox,
100  const Interval& a_dstInterval,
101  const Box& a_toBox,
102  const MiniIFFAB<T>& a_src,
103  const Interval& a_srcInterval)
104 {
105  CH_assert(isDefined());
106  CH_assert(a_src.isDefined());
107  CH_assert(a_srcInterval.size() == a_dstInterval.size());
108  CH_assert(a_dstInterval.begin() >= 0);
109  CH_assert(a_srcInterval.begin() >= 0);
110  CH_assert(a_dstInterval.end() < m_nComp);
111  CH_assert(a_srcInterval.end() < a_src.m_nComp);
112 
113  if ((!m_box.isEmpty()) && (!a_src.m_box.isEmpty()))
114  {
115  CH_assert(a_fromBox == a_toBox);
116  Box intBox = a_toBox;
117  Box boxIntersect = m_box;
118  boxIntersect &= a_src.m_box;
119  boxIntersect &= intBox;
120  int compSize = a_srcInterval.size();
121 
122  for (int iface = 0; iface < m_faces.size(); iface++)
123  {
124  const FaceIndex& face = m_faces[iface];
125  const IntVect& ivHi = face.gridIndex(Side::Hi);
126  const IntVect& ivLo = face.gridIndex(Side::Lo);
127  if (boxIntersect.contains(ivHi) || boxIntersect.contains(ivLo))
128  {
129  for (int icomp = 0; icomp < compSize; icomp++)
130  {
131  int isrccomp = a_srcInterval.begin() + icomp;
132  int idstcomp = a_dstInterval.begin() + icomp;
133  (*this)(face, idstcomp) = a_src(face, isrccomp);
134  }
135  }
136  }
137  }
138 }
139 /*********/
140 template <class T> inline
141 T*
143 getIndex(const FaceIndex& a_face, const int& a_comp) const
144 {
145  CH_assert(m_isDefined);
146  CH_assert((a_comp >= 0) && (a_comp < this->m_nComp));
147 
148  T* dataPtr = this->m_data;
149  bool found = false;
150  T* retval=NULL;
151  for (int iface = 0; iface < m_faces.size(); ++iface)
152  {
153  if (a_face == m_faces[iface])
154  {
155  found = true;
156  retval=dataPtr;
157  }
158  if (found) break;
159  dataPtr++;
160  }
161  if (!found)
162  {
163  MayDay::Error("MiniIFFAB:index not found");
164  }
165  retval += a_comp*this->m_nFaces;
166  return retval;
167 
168 }
169 /*********/
170 template <class T> inline
171 void
174 {
175  if (m_data != NULL)
176  {
177  delete[] m_data;
178  m_data = NULL;
179  }
180  m_faces.resize(0);
181  setDefaultValues();
182 }
183 /*************************/
184 template <class T> inline
185 T&
187  const int& a_comp)
188 {
189  CH_assert(isDefined());
190  CH_assert(a_ndin.direction() == m_direction);
191 
192  T* dataPtr = getIndex(a_ndin, a_comp);
193  return(*dataPtr);
194 }
195 /**************************/
196 template <class T> inline
197 const T&
199  const int& a_comp) const
200 {
201  CH_assert(isDefined());
202  CH_assert(a_ndin.direction() == m_direction);
203 
204  T* dataPtr = getIndex(a_ndin, a_comp);
205  return(*dataPtr);
206 }
207 /******************/
208 template <class T> inline
209 T*
210 MiniIFFAB<T>::dataPtr(const int& a_comp)
211 {
212  CH_assert(a_comp >= 0);
213  CH_assert(a_comp <= m_nComp);
214  return m_data + a_comp*m_nFaces;
215 }
216 /******************/
217 template <class T> inline
218 const T*
219 MiniIFFAB<T>::dataPtr(const int& a_comp) const
220 {
221  CH_assert(a_comp >= 0);
222  CH_assert(a_comp <= m_nComp);
223  return m_data + a_comp*m_nFaces;
224 }
225 /******************/
226 template <class T> inline
227 void
229 {
230  m_isDefined = false;
231  m_data = NULL;
232  m_nFaces = 0;
233  m_nComp = 0;
234  m_direction = -1;
235 }
236 /******************/
237 template <class T> inline
238 void
240 getFaceSubset(Vector<FaceIndex>& a_subFaces, const Box& a_subBox) const
241 {
242  a_subFaces.resize(0);
243  for (int iface = 0; iface < m_faces.size(); iface++)
244  {
245  if ((a_subBox.contains(m_faces[iface].gridIndex(Side::Lo))) ||
246  (a_subBox.contains(m_faces[iface].gridIndex(Side::Hi))))
247  {
248  a_subFaces.push_back(m_faces[iface]);
249  }
250  }
251 }
252 /*********************/
253 template <class T> inline
254 int
256 size(const Box& a_region,
257  const Interval& a_comps) const
258 {
259  CH_assert(isDefined());
260  //create set of cells in fab that are also in the input region
261  Vector<FaceIndex> faces;
262  getFaceSubset(faces, a_region);
263 
264  //account for list of faces
265  int facesize = linearListSize(faces);
266 
267  //add for each data pt
268  int datasize = 0;
269  for (int iface = 0; iface < faces.size(); iface++)
270  {
271  for (int icomp = a_comps.begin(); icomp <= a_comps.end(); icomp++)
272  {
273  const T& dataPt = (*this)(faces[iface], icomp);
274  int pointsize = CH_XD::linearSize(dataPt);
275  datasize += pointsize;
276  }
277  }
278 
279  int retval = facesize + datasize;
280 
281  if (s_verbose)
282  {
283  pout() << "###############" << std::endl;
284  pout() << "MiniIFFAB size() for region " << m_ebgraph.getRegion() << std::endl;
285  pout() << " a_comps = (" << a_comps.begin() << "," << a_comps.end() << ")," << endl;
286  pout() << "m_box = " << m_box;
287  pout() << "size = " << retval << std::endl;
288  pout() << "###############" << std::endl;
289  }
290 
291  return retval;
292 }
293 /********************/
294 template <class T> inline
295 void
297 linearOut(void* a_buf,
298  const Box& a_region,
299  const Interval& a_comps) const
300 {
301  CH_assert(isDefined());
302  Vector<FaceIndex> faces;
303  getFaceSubset(faces, a_region);
304 
305  //output the faces.
306  unsigned char* charbuffer = (unsigned char *) a_buf;
307  linearListOut(charbuffer, faces);
308  charbuffer += linearListSize(faces);
309 
310  //output the data
311  const MiniIFFAB<T>& thisFAB = *this;
312  for (int iface = 0; iface < faces.size(); iface++)
313  {
314  const FaceIndex& face = faces[iface];
315  for (int icomp = a_comps.begin(); icomp <= a_comps.end(); icomp++)
316  {
317  //output the data into the buffer
318  const T& dataPt = thisFAB(face, icomp);
319  CH_XD::linearOut(charbuffer, dataPt);
320  //increment the buffer offset
321  charbuffer += CH_XD::linearSize(dataPt);
322  }
323  }
324 }
325 /********************/
326 template <class T> inline
327 void
329 linearIn(void* a_buf, const Box& a_region, const Interval& a_comps)
330 {
331  CH_assert(isDefined());
332 
333  unsigned char* charbuffer = (unsigned char *) a_buf;
334  Vector<FaceIndex> faces;
335  linearListIn(faces, charbuffer);
336  charbuffer += linearListSize(faces);
337 
338  for (int iface = 0; iface < faces.size(); iface++)
339  {
340  const FaceIndex& face = faces[iface];
341  for (int icomp = a_comps.begin(); icomp <= a_comps.end(); icomp++)
342  {
343  //input the data
344  T& dataPt = (*this)(face, icomp);
345  CH_XD::linearIn(dataPt, charbuffer) ;
346  //increment the buffer offset
347  charbuffer += CH_XD::linearSize(dataPt);
348  }
349  }
350 }
351 /********************/
352 /******************/
353 
354 #include "NamespaceFooter.H"
355 #endif
std::ostream & pout()
Use this in place of std::cout for program output.
int m_nComp
Definition: MiniIFFAB.H:196
#define CH_assert(cond)
Definition: CHArray.H:37
~MiniIFFAB()
Definition: MiniIFFABI.H:29
bool isDefined() const
Definition: MiniIFFAB.H:123
void setVal(const T &value)
Definition: MiniIFFABI.H:72
Definition: FaceIndex.H:28
void linearListOut(void *const a_outBuf, const Vector< T > &a_inputT)
Definition: SPMDI.H:255
IntVect size() const
size functions
Definition: Box.H:1814
void clear()
Definition: MiniIFFABI.H:173
int size(const Box &R, const Interval &comps) const
Definition: MiniIFFABI.H:256
MiniIFFAB()
Definition: MiniIFFABI.H:23
int size() const
Definition: Interval.H:64
Definition: LoHiSide.H:31
Geometric description within a box.
Definition: EBGraph.H:432
const int SpaceDim
Definition: SPACE.H:39
void setDefaultValues()
Definition: MiniIFFABI.H:228
void resize(unsigned int isize)
Definition: Vector.H:323
void linearIn(void *buf, const Box &R, const Interval &comps)
Definition: MiniIFFABI.H:329
void push_back(const T &in)
Definition: Vector.H:283
Structure for passing component ranges in code.
Definition: Interval.H:23
void linearOut(void *const a_outBuf, const T &inputT)
Definition: SPMDI.H:32
int linearSize(const T &inputT)
Definition: SPMDI.H:20
T * dataPtr(const int &a_comp)
Definition: MiniIFFABI.H:210
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.
int begin() const
Definition: Interval.H:86
bool isEmpty() const
{ Comparison Functions}
Definition: Box.H:1863
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:99
T & operator()(const FaceIndex &a_face, const int &varlocin)
Definition: MiniIFFABI.H:186
void define(const Box &a_box, const EBGraph &a_ebgraph, const int &a_direction, const int &a_nvarin)
Definition: MiniIFFABI.H:48
void linearListIn(Vector< T > &a_outputT, const void *const a_inBuf)
Definition: SPMDI.H:226
Definition: MiniIFFAB.H:34
bool contains(const IntVect &p) const
Definition: Box.H:1888
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:287
const IntVect & gridIndex(const Side::LoHiSide &a_sd) const
const int & direction() const
size_t size() const
Definition: Vector.H:177
T * getIndex(const FaceIndex &a_face, const int &a_comp) const
Definition: MiniIFFABI.H:143
int end() const
Definition: Interval.H:91
void linearIn(T &a_outputT, const void *const inBuf)
Definition: SPMDI.H:26
Box m_box
Definition: MiniIFFAB.H:184
void getFaceSubset(Vector< FaceIndex > &a_subFaces, const Box &a_subBox) const
Definition: MiniIFFABI.H:240
void linearOut(void *buf, const Box &R, const Interval &comps) const
Definition: MiniIFFABI.H:297