Chombo + EB  3.0
IVSFABI.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 _IVSFABI_H_
12 #define _IVSFABI_H_
13 
14 #include "MayDay.H"
15 #include "IntVectSet.H"
16 #include "parstream.H"
17 #include "SPMD.H"
18 #include "DebugOut.H"
19 #include "SPMD.H"
20 #include "NamespaceHeader.H"
21 
22 template <class T>
23 bool IVSFAB<T>::s_verbose = false;
24 
25 template <class T>
26 void
27 IVSFAB<T>::setVerbose(bool a_verbose)
28 {
29  s_verbose = a_verbose;
30 }
31 /*************/
32 template <class T> inline
33 const IntVectSet&
35 {
36  return m_ivs;
37 }
38 /******************/
39 template <class T> inline
41 {
42  setDefaultValues();
43 }
44 /******************/
45 template <class T> inline
47 {
48  clear();
49 }
50 /******************/
51 template <class T> inline
53  const int& a_nvarin)
54 {
55  setDefaultValues();
56  define(a_ivsin, a_nvarin);
57 }
58 /******************/
59 template <class T> inline
60 void
62  const int& a_nvarin)
63 {
64  clear();
65  m_isDefined = true;
66  CH_assert(a_nvarin > 0);
67  m_ivs = a_ivsin;
68  m_nComp = a_nvarin;
69  m_nIvs = 0;
70  if (!a_ivsin.isEmpty())
71  {
72  Box minbox = a_ivsin.minBox();
73  m_ivmap.resize(minbox, 1);
74  IVSIterator ivsit(a_ivsin);
75  for (ivsit.reset(); ivsit.ok(); ++ivsit)
76  {
77  const IntVect& iv = ivsit();
78  m_ivmap(iv,0) = m_nIvs;
79  m_nIvs++;
80  }
81 
82  if ( (m_nIvs > 0) && m_nComp > 0)
83  {
84  m_dataPtr = new T[m_nComp*m_nIvs];
85  }
86  else
87  {
88  m_dataPtr = NULL;
89  }
90  }
91  else
92  {
93  m_dataPtr = NULL;
94  }
95  if (m_nIvs > 0)
96  {
97  //set up face low and high vectors
98  for (int dir = 0; dir < CH_SPACEDIM; ++dir)
99  {
100  m_loVect[dir] = 1;
101  m_hiVect[dir] = 1;
102  }
103  m_hiVect[0] = m_nIvs;
104 
105  }
106  else
107  {
108  m_loVect = IntVect::Unit;
109  m_hiVect = IntVect::Zero;
110  m_dataPtr = NULL;
111  }
112 
113 }
114 /******************/
115 template <class T> inline
116 void
117 IVSFAB<T>::setVal(const T& a_value)
118 {
119  CH_assert(isDefined());
120  for (int ivec = 0; ivec < m_nIvs*m_nComp; ivec++)
121  m_dataPtr[ivec] = a_value;
122 }
123 /******************/
124 template <class T> inline
125 void
126 IVSFAB<T>::copy(const Box& a_fromBox,
127  const Interval& a_dstInterval,
128  const Box& a_toBox,
129  const IVSFAB<T>& a_src,
130  const Interval& a_srcInterval)
131 {
132  CH_assert(a_fromBox == a_toBox);
133  CH_assert(isDefined());
134  CH_assert(a_src.isDefined());
135  CH_assert(a_srcInterval.size() == a_dstInterval.size());
136  CH_assert(a_dstInterval.begin() >= 0);
137  CH_assert(a_srcInterval.begin() >= 0);
138  CH_assert(a_dstInterval.end() < m_nComp);
139  CH_assert(a_srcInterval.end() < a_src.m_nComp);
140 
141  if ((!m_ivs.isEmpty()) && (!a_src.m_ivs.isEmpty()))
142  {
143  Box intBox = a_fromBox;
144  IntVectSet ivsIntersect = m_ivs;
145  ivsIntersect &= a_src.m_ivs; //how did this ever work without this line?
146  ivsIntersect &= intBox; //
147  IVSFAB<T>& thisFAB = *this;
148  int compSize = a_srcInterval.size();
149  for (IVSIterator ivit(ivsIntersect); ivit.ok(); ++ivit)
150  {
151  const IntVect& iv = ivit();
152  for (int icomp = 0; icomp < compSize; icomp++)
153  {
154  int isrccomp = a_srcInterval.begin() + icomp;
155  int idstcomp = a_dstInterval.begin() + icomp;
156  thisFAB(iv, idstcomp) = a_src(iv, isrccomp);
157  }
158  }
159  }
160 }
161 /******************/
162 
163 template <> inline
164 int IVSFAB<Real>::size(const Box& a_region,
165  const Interval& a_comps) const
166 {
167  // size of actual data plus size of count ..
168  // specialization for T=Real.
169  IntVectSet subset = m_ivs & a_region;
170  int numberThings = subset.numPts();
171  return numberThings*sizeof(Real)*a_comps.size() + sizeof(int);
172 }
173 
174 template <class T> inline
175 int IVSFAB<T>::size(const Box& a_region,
176  const Interval& a_comps) const
177 {
178  // Note: it is assumed that there is one component.
179  // and, that all "T" have equal size.
180  // message includes count ...
181  CH_assert( a_comps.begin() == 0 && a_comps.end() == 0 );
182  IntVectSet subset = m_ivs & a_region;
183  int numberThings = subset.numPts();
184  T dummy;
185  int sizeofThing = dummy.linearSize();
186  int retval = numberThings*sizeofThing + sizeof(int);
187  return retval;
188 }
189 
190 
191 /********************/
192 template <> inline void IVSFAB<Real>::linearOut(void* a_buf,
193  const Box& a_region,
194  const Interval& a_comps) const
195 {
196  IntVectSet subset = m_ivs & a_region;
197  int count = subset.numPts();
198  int* iptr = (int*)a_buf;
199  *iptr++ = count;
200  Real* rptr = (Real*)iptr;
201  for ( IVSIterator ivsit(subset); ivsit.ok(); ++ivsit )
202  {
203  const IntVect& iv = ivsit();
204  for ( int c=a_comps.begin(); c<=a_comps.end(); c++ )
205  {
206  int iloc = getIndex(iv, c);
207  *rptr++ = m_dataPtr[iloc];
208  }
209  }
210  CH_assert( ((char*)rptr) - ((char*)a_buf) == size(a_region,a_comps) );
211 }
212 
213 template <class T> inline
214 void IVSFAB<T>::linearOut(void* a_buf,
215  const Box& a_region,
216  const Interval& a_comps) const
217 {
218  // Note: it is assumed that there is one component.
219  // and all "T" have equal size.
220  // message includes count
221  IntVectSet subset = m_ivs & a_region;
222  int count = subset.numPts();
223  int *iptr = (int*)a_buf;
224  *iptr++ = count;
225  T dummy;
226  int Tsz = dummy.linearSize();
227  char* cptr = (char*)iptr;
228  for ( IVSIterator ivsit(subset); ivsit.ok(); ++ivsit )
229  {
230  const IntVect& iv = ivsit();
231  int iloc = getIndex(iv, 0); // assume 1 component ...
232  m_dataPtr[iloc].linearOut( (void*)cptr );
233  cptr += Tsz;
234  }
235  CH_assert( a_comps.begin() == 0 && a_comps.end() == 0 );
236  CH_assert( cptr - ((char*)a_buf) == size(a_region,a_comps) );
237 }
238 /********************/
239 template <> inline void IVSFAB<Real>::linearIn(void* a_buf, const Box& a_region, const Interval& a_comps)
240 {
241  // message includes count
242  IntVectSet subset = m_ivs & a_region;
243  int count_expected = subset.numPts();
244  int *iptr = (int*)a_buf;
245  int count_received = *iptr++;
246  if ( count_expected != count_received )
247  {
248  MayDay::Error("IVSFAB::linearin: counts do not match");
249  }
250  Real* rptr = (Real*)iptr;
251  for ( IVSIterator ivsit(subset); ivsit.ok(); ++ivsit )
252  {
253  const IntVect& iv = ivsit();
254  for ( int c=a_comps.begin(); c<=a_comps.end(); c++ )
255  {
256  int iloc = getIndex(iv, c);
257  m_dataPtr[iloc] = *rptr++;
258  }
259  }
260  CH_assert( ((char*)rptr) - ((char*)a_buf) == size(a_region,a_comps) );
261 }
262 
263 template <class T> inline
264 void IVSFAB<T>::linearIn(void* a_buf, const Box& a_region, const Interval& a_comps)
265 {
266  // Note: it is assumed that there is one component.
267  // and all "T" have same size
268  // message includes count
269  IntVectSet subset = m_ivs & a_region;
270  int count_expected = subset.numPts();
271  int *iptr = (int*)a_buf;
272  int count_received = *iptr++;
273  CH_assert( count_expected == count_received );
274  T dummy;
275  int Tsz = dummy.linearSize();
276  char* cptr = (char*)iptr;
277  for ( IVSIterator ivsit(subset); ivsit.ok(); ++ivsit )
278  {
279  const IntVect& iv = ivsit();
280  int iloc = getIndex(iv, 0);
281  m_dataPtr[iloc].linearIn( (void*)cptr );
282  cptr += Tsz;
283  }
284  CH_assert( a_comps.begin() == 0 && a_comps.end() == 0 );
285  CH_assert( cptr - ((char*)a_buf) == size(a_region,a_comps) );
286 }
287 
288 template <class T> inline
289 int
290 IVSFAB<T>::getIndex(const IntVect& a_iv, const int& a_comp) const
291 {
292  CH_assert(isDefined());
293  CH_assert(m_ivs.contains(a_iv));
294  CH_assert((a_comp >= 0) && (a_comp < m_nComp));
295 
296  int ioffset = m_ivmap(a_iv, 0);
297  CH_assert(ioffset >= 0);
298  CH_assert(ioffset < m_nIvs);
299  //now add offset from componentnitude
300  ioffset += m_nIvs*a_comp;
301  return ioffset;
302 }
303 /********************/
304 template <class T> inline
305 void
307 {
308  m_nComp = 0;
309  m_nIvs = 0;
310  m_ivs.makeEmpty();
311  m_ivmap.clear();
312  if (m_dataPtr != NULL)
313  {
314  delete[] m_dataPtr;
315  m_dataPtr = NULL;
316  }
317  m_isDefined = false;
318 }
319 /*************************/
320 template <class T> inline
321 bool
323 {
324  return (m_isDefined);
325 }
326 /*************************/
327 template <class T> inline
328 int
330 {
331  return m_nIvs;
332 }
333 /*************************/
334 template <class T> inline
335 int
337 {
338  return m_nComp;
339 }
340 /*************************/
341 template <class T> inline
342 T&
344  const int& a_comp)
345 {
346  CH_assert(isDefined());
347  CH_assert(a_comp >= 0);
348  CH_assert(a_comp < m_nComp);
349  int iloc = getIndex(a_ndin, a_comp);
350  return(m_dataPtr[iloc]);
351 }
352 /**************************/
353 template <class T> inline
354 const T&
356  const int& a_comp) const
357 {
358  CH_assert(isDefined());
359  CH_assert(a_comp >= 0);
360  CH_assert(a_comp < m_nComp);
361  int iloc = getIndex(a_ndin, a_comp);
362  return(m_dataPtr[iloc]);
363 }
364 /******************/
365 template <class T> inline
366 const T*
367 IVSFAB<T>::dataPtr(const int& a_comp) const
368 {
369  CH_assert(isDefined());
370  CH_assert(a_comp >= 0);
371  CH_assert(a_comp < m_nComp);
372  return m_dataPtr + a_comp*m_nIvs;
373 }
374 /******************/
375 template <class T> inline
376 T*
377 IVSFAB<T>::dataPtr(const int& a_comp)
378 {
379  CH_assert(isDefined());
380  CH_assert(a_comp >= 0);
381  CH_assert(a_comp < m_nComp);
382  return m_dataPtr + a_comp*m_nIvs;
383 }
384 /******************/
385 template <class T> inline
386 const int*
388 {
389  return m_loVect.getVect();
390 }
391 /******************/
392 template <class T> inline
393 const int*
395 {
396  return m_hiVect.getVect();
397 }
398 /******************/
399 template <class T> inline
400 void
402 {
403  m_isDefined = false;
404  m_dataPtr = NULL;
405  m_nIvs = 0;
406  m_nComp = 0;
407  m_loVect = IntVect::Unit;
408  m_hiVect = IntVect::Zero;
409 }
410 /******************/
411 template <class T> inline
412 IVSFAB<T>&
414 {
415  MayDay::Error("IVSFAB operator = not defined");
416  return *this;
417 }
418 /******************/
419 template <class T> inline
420 IVSFAB<T>::IVSFAB (const IVSFAB<T>& a_input)
421 {
422  MayDay::Error("IVSFAB copy constructor not defined");
423 }
424 
425 #include "NamespaceFooter.H"
426 #endif
static void setVerbose(bool a_verbose)
Definition: IVSFABI.H:27
const int * hiVect() const
Definition: IVSFABI.H:394
An irregular domain on an integer lattice.
Definition: IntVectSet.H:44
#define CH_SPACEDIM
Definition: SPACE.H:52
#define CH_assert(cond)
Definition: CHArray.H:37
void copy(const Box &a_fromBox, const Interval &a_destInterval, const Box &a_toBox, const IVSFAB< T > &a_src, const Interval &a_srcInterval)
Definition: IVSFABI.H:126
void define(const IntVectSet &a_region, const int &a_nvarin)
Definition: IVSFABI.H:61
const int * loVect() const
Definition: IVSFABI.H:387
int size() const
Definition: Interval.H:64
IntVectSet m_ivs
Definition: IVSFAB.H:188
int begin() const
Definition: Interval.H:86
void linearIn(void *buf, const Box &R, const Interval &comps)
Definition: IVSFABI.H:264
void linearOut(void *buf, const Box &R, const Interval &comps) const
Definition: IVSFABI.H:214
int getIndex(const IntVect &a_iv, const int &a_comp) const
Definition: IVSFABI.H:290
Definition: IVSFAB.H:32
int m_nComp
Definition: IVSFAB.H:176
int numPts() const
Returns the number of IntVects in this IntVectSet.
void reset()
same as begin()
Definition: IntVectSet.H:722
bool ok() const
returns true if this iterator is still in its IntVectSet
Definition: IntVectSet.H:711
void setVal(const T &value)
Definition: IVSFABI.H:117
long numPts() const
int numIvs() const
Definition: IVSFABI.H:329
int size(const Box &R, const Interval &comps) const
Definition: IVSFABI.H:175
Structure for passing component ranges in code.
Definition: Interval.H:23
void setDefaultValues()
Definition: IVSFABI.H:401
static const IntVect Unit
Definition: IntVect.H:632
~IVSFAB()
Definition: IVSFABI.H:46
double Real
Definition: REAL.H:33
const IntVectSet & getIVS() const
Definition: IVSFABI.H:34
int nComp() const
Definition: IVSFABI.H:336
T & operator()(const IntVect &a_iv, const int &varlocin)
Definition: IVSFABI.H:343
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.
static const IntVect Zero
Definition: IntVect.H:627
bool isDefined() const
Definition: IVSFABI.H:322
A Rectangular Domain on an Integer Lattice.
Definition: Box.H:465
IVSFAB< T > & operator=(const IVSFAB< T > &)
Definition: IVSFABI.H:413
An integer Vector in SpaceDim-dimensional space.
Definition: CHArray.H:42
int end() const
Definition: Interval.H:91
Iterator for an IntVectSet.
Definition: IntVectSet.H:640
void clear()
Definition: IVSFABI.H:306
const Box & minBox() const
Returns the minimum enclosing box of this IntVectSet.
bool isEmpty() const
Returns true if no IntVects are in this IntVectSet.
const T * dataPtr(const int &a_comp) const
Definition: IVSFABI.H:367
IVSFAB()
Definition: IVSFABI.H:40