00001 #ifdef CH_LANG_CC
00002
00003
00004
00005
00006
00007
00008
00009 #endif
00010
00011 #ifndef _IVSFABI_H_
00012 #define _IVSFABI_H_
00013
00014 #include "MayDay.H"
00015 #include "IntVectSet.H"
00016 #include "parstream.H"
00017 #include "SPMD.H"
00018 #include "DebugOut.H"
00019 #include "SPMD.H"
00020 #include "NamespaceHeader.H"
00021
00022 template <class T>
00023 bool IVSFAB<T>::s_verbose = false;
00024
00025 template <class T>
00026 void
00027 IVSFAB<T>::setVerbose(bool a_verbose)
00028 {
00029 s_verbose = a_verbose;
00030 }
00031
00032 template <class T> inline
00033 const IntVectSet&
00034 IVSFAB<T>::getIVS() const
00035 {
00036 return m_ivs;
00037 }
00038
00039 template <class T> inline
00040 IVSFAB<T>::IVSFAB():m_dataPtr(0),m_nComp(0),m_nIvs(0),m_loVect(IntVect::Unit), m_hiVect(IntVect::Zero), m_isDefined(false)
00041 {
00042
00043 }
00044
00045 template <class T> inline
00046 IVSFAB<T>::~IVSFAB()
00047 {
00048 clear();
00049 }
00050
00051 template <class T> inline
00052 IVSFAB<T>::IVSFAB(const IntVectSet& a_ivsin,
00053 const int& a_nvarin)
00054 {
00055 define( a_ivsin, a_nvarin );
00056 }
00057
00058
00059
00060 template<class T> inline
00061 void IVSFAB<T>::define(const IntVectSet& a_ivsin,
00062 const int& a_nvarin )
00063 {
00064 m_dataPtr = NULL;
00065 m_nComp = a_nvarin;
00066 m_nIvs = 0;
00067 m_loVect = IntVect::Zero;
00068 m_hiVect = IntVect::Unit;
00069 m_ivmap.define( a_ivsin.minBox(), a_nvarin );
00070 m_ivs = a_ivsin;
00071
00072 CH_assert(a_nvarin > 0);
00073 if (!a_ivsin.isEmpty())
00074 {
00075 IVSIterator ivsit(a_ivsin);
00076 for (ivsit.reset(); ivsit.ok(); ++ivsit)
00077 {
00078 const IntVect& iv = ivsit();
00079 m_ivmap(iv,0) = m_nIvs;
00080 m_nIvs++;
00081 }
00082
00083 if ( (m_nIvs > 0) && m_nComp > 0)
00084 {
00085 m_dataPtr = new T[m_nComp*m_nIvs];
00086 }
00087 }
00088 if (m_nIvs > 0)
00089 {
00090
00091 for (int dir = 0; dir < CH_SPACEDIM; ++dir)
00092 {
00093 m_loVect[dir] = 1;
00094 m_hiVect[dir] = 1;
00095 }
00096 m_hiVect[0] = m_nIvs;
00097
00098 }
00099 else
00100 {
00101 m_loVect = IntVect::Unit;
00102 m_hiVect = IntVect::Zero;
00103 m_dataPtr = NULL;
00104 }
00105 m_isDefined=true;
00106
00107 }
00108
00109 template <class T> inline
00110 void
00111 IVSFAB<T>::setVal(const T& a_value)
00112 {
00113 CH_assert(isDefined());
00114 for (int ivec = 0; ivec < m_nIvs*m_nComp; ivec++)
00115 m_dataPtr[ivec] = a_value;
00116 }
00117
00118 template <class T> inline
00119 void
00120 IVSFAB<T>::copy(const Box& a_fromBox,
00121 const Interval& a_dstInterval,
00122 const Box& a_toBox,
00123 const IVSFAB<T>& a_src,
00124 const Interval& a_srcInterval)
00125 {
00126 IntVect shift = a_toBox.smallEnd() - a_fromBox.smallEnd();
00127 IntVect shift2 = a_toBox.bigEnd() - a_fromBox.bigEnd();
00128 CH_assert(shift==shift2);
00129 CH_assert(isDefined());
00130 CH_assert(a_src.isDefined());
00131 CH_assert(a_srcInterval.size() == a_dstInterval.size());
00132 CH_assert(a_dstInterval.begin() >= 0);
00133 CH_assert(a_srcInterval.begin() >= 0);
00134 CH_assert(a_dstInterval.end() < m_nComp);
00135 CH_assert(a_srcInterval.end() < a_src.m_nComp);
00136
00137 if ((!m_ivs.isEmpty()) && (!a_src.m_ivs.isEmpty()))
00138 {
00139
00140 IntVectSet ivsIntersect = m_ivs & a_toBox;
00141
00142 IntVectSet set2 = a_src. m_ivs & a_fromBox;
00143
00144 set2.shift(shift);
00145
00146 ivsIntersect &= set2;
00147
00148 IVSFAB<T>& thisFAB = *this;
00149 int compSize = a_srcInterval.size();
00150 for (IVSIterator ivit(ivsIntersect); ivit.ok(); ++ivit)
00151 {
00152 const IntVect& iv = ivit();
00153 IntVect iv2 = iv - shift;
00154 for (int icomp = 0; icomp < compSize; icomp++)
00155 {
00156 int isrccomp = a_srcInterval.begin() + icomp;
00157 int idstcomp = a_dstInterval.begin() + icomp;
00158 thisFAB(iv, idstcomp) = a_src(iv2, isrccomp);
00159 }
00160 }
00161 }
00162 }
00163
00164
00165 template <> inline
00166 int IVSFAB<Real>::size(const Box& a_region,
00167 const Interval& a_comps) const
00168 {
00169
00170
00171 IntVectSet subset = m_ivs & a_region;
00172 int numberThings = subset.numPts();
00173 int retval = numberThings*sizeof(Real)*a_comps.size() + sizeof(int);
00174 retval += SpaceDim*sizeof(int)*numberThings;
00175 retval += 2*SpaceDim*sizeof(Real);
00176 return retval;
00177 }
00178
00179
00180 template <> inline void IVSFAB<Real>::linearIn(void* a_buf, const Box& a_region, const Interval& a_comps)
00181 {
00182
00183 IntVectSet subset = m_ivs & a_region;
00184
00185
00186 IntVect ibl, ibh;
00187 Real* rptr = (Real*)a_buf;
00188 for ( int idir=0; idir<SpaceDim; idir++ ) {
00189 ibl[idir] = *rptr++;
00190 ibh[idir] = *rptr++;
00191 }
00192
00193
00194 IntVect shift = a_region.smallEnd() - ibl;
00195 IntVect shift2 = a_region.bigEnd() - ibh;
00196 CH_assert(shift==shift2);
00197
00198 int* iptr = (int*)rptr;
00199 int count_received = *iptr++;
00200 rptr = (Real*)iptr;
00201 for ( int i=0; i<count_received; i++ )
00202 {
00203 IntVect iv2;
00204 iptr = (int*)rptr;
00205 for ( int idir=0; idir<SpaceDim; idir++ ) iv2[idir] = *iptr++;
00206 rptr = (Real*)iptr;
00207
00208
00209 iv2 += shift;
00210
00211 for ( int c=a_comps.begin(); c<=a_comps.end(); c++ )
00212 {
00213 int iloc = getIndex(iv2, c);
00214 m_dataPtr[iloc] = *rptr++;
00215 }
00216 }
00217 }
00218
00219
00220 template <> inline void IVSFAB<Real>::linearOut(void* a_buf,
00221 const Box& a_region,
00222 const Interval& a_comps) const
00223 {
00224 IntVectSet subset = m_ivs & a_region;
00225
00226 Real* rptr = (Real*)a_buf;
00227 for ( int idir=0; idir<SpaceDim; idir++ ) {
00228 *rptr++ = a_region.smallEnd(idir);
00229 *rptr++ = a_region.bigEnd(idir);
00230 }
00231
00232 int* iptr = (int*)rptr;
00233 int count = subset.numPts();
00234 *iptr++ = count;
00235 rptr = (Real*)iptr;
00236
00237 for ( IVSIterator ivsit(subset); ivsit.ok(); ++ivsit )
00238 {
00239 const IntVect& iv = ivsit();
00240 iptr = (int*)rptr;
00241 for ( int idir=0; idir<SpaceDim; idir++ ) *iptr++ = iv[idir];
00242 rptr = (Real*)iptr;
00243 for ( int c=a_comps.begin(); c<=a_comps.end(); c++ )
00244 {
00245 int iloc = getIndex(iv, c);
00246 *rptr++ = m_dataPtr[iloc];
00247 }
00248 }
00249 CH_assert( ((char*)rptr) - ((char*)a_buf) == size(a_region,a_comps) );
00250 }
00251
00252
00253
00254
00255 template <> inline
00256 int IVSFAB<int>::size(const Box& a_region,
00257 const Interval& a_comps) const
00258 {
00259
00260
00261 IntVectSet subset = m_ivs & a_region;
00262 int numberThings = subset.numPts();
00263 int retval = numberThings*sizeof(int)*a_comps.size() + sizeof(int);
00264 retval += SpaceDim*sizeof(int)*numberThings;
00265 retval += 2*SpaceDim*sizeof(int);
00266 return retval;
00267 }
00268
00269
00270
00271 template <> inline void IVSFAB<int>::linearOut(void* a_buf,
00272 const Box& a_region,
00273 const Interval& a_comps) const
00274 {
00275 IntVectSet subset = m_ivs & a_region;
00276
00277 int* rptr = (int*)a_buf;
00278 for ( int idir=0; idir<SpaceDim; idir++ )
00279 {
00280 *rptr++ = a_region.smallEnd(idir);
00281 *rptr++ = a_region.bigEnd(idir);
00282 }
00283
00284 int* iptr = (int*)rptr;
00285 int count = subset.numPts();
00286 *iptr++ = count;
00287 rptr = (int*)iptr;
00288
00289 for ( IVSIterator ivsit(subset); ivsit.ok(); ++ivsit )
00290 {
00291 const IntVect& iv = ivsit();
00292 iptr = (int*)rptr;
00293 for ( int idir=0; idir<SpaceDim; idir++ ) *iptr++ = iv[idir];
00294 rptr = (int*)iptr;
00295 for ( int c=a_comps.begin(); c<=a_comps.end(); c++ )
00296 {
00297 int iloc = getIndex(iv, c);
00298 *rptr++ = m_dataPtr[iloc];
00299 }
00300 }
00301
00302 }
00303
00304 template <> inline void IVSFAB<int>::linearIn(void* a_buf, const Box& a_region, const Interval& a_comps)
00305 {
00306
00307 IntVectSet subset = m_ivs & a_region;
00308
00309
00310 IntVect ibl, ibh;
00311 int* rptr = (int*)a_buf;
00312 for ( int idir=0; idir<SpaceDim; idir++ )
00313 {
00314 ibl[idir] = *rptr++;
00315 ibh[idir] = *rptr++;
00316 }
00317
00318
00319 IntVect shift = a_region.smallEnd() - ibl;
00320 IntVect shift2 = a_region.bigEnd() - ibh;
00321 CH_assert(shift==shift2);
00322
00323 int* iptr = (int*)rptr;
00324 int count_received = *iptr++;
00325 rptr = (int*)iptr;
00326 for ( int i=0; i<count_received; i++ )
00327 {
00328 IntVect iv2;
00329 iptr = (int*)rptr;
00330 for ( int idir=0; idir<SpaceDim; idir++ ) iv2[idir] = *iptr++;
00331 rptr = (int*)iptr;
00332
00333
00334 iv2 += shift;
00335
00336 for ( int c=a_comps.begin(); c<=a_comps.end(); c++ )
00337 {
00338 int iloc = getIndex(iv2, c);
00339 m_dataPtr[iloc] = *rptr++;
00340 }
00341 }
00342 }
00343
00344 template <class T> inline
00345 int IVSFAB<T>::size(const Box& a_region,
00346 const Interval& a_comps) const
00347 {
00348 IntVectSet subset = m_ivs & a_region;
00349 int numberThings = subset.numPts();
00350
00351
00352
00353
00354
00355
00356 int retval=sizeof(int);
00357 for( int icomp=a_comps.begin(); icomp<=a_comps.end(); icomp++ ) {
00358 for( IVSIterator ivsit(subset); ivsit.ok(); ++ivsit ) {
00359 const IntVect& iv = ivsit();
00360 int iloc = getIndex(iv,icomp);
00361 retval += m_dataPtr[iloc].linearSize();
00362 }
00363 }
00364 retval += SpaceDim*sizeof(int)*numberThings;
00365 retval += 2*SpaceDim*sizeof(int);
00366 return retval;
00367 }
00368
00369 template <class T> inline
00370 void IVSFAB<T>::linearOut(void* a_buf,
00371 const Box& a_region,
00372 const Interval& a_comps) const
00373 {
00374
00375
00376
00377 IntVectSet subset = m_ivs & a_region;
00378
00379
00380 Real* rptr = (Real*)a_buf;
00381 for ( int idir=0; idir<SpaceDim; idir++ ) {
00382 *rptr++ = a_region.smallEnd(idir);
00383 *rptr++ = a_region.bigEnd(idir);
00384 }
00385
00386 int* iptr = (int*)rptr;
00387
00388 int count = subset.numPts();
00389 *iptr++ = count;
00390
00391
00392
00393 char* cptr = (char*)iptr;
00394 for ( IVSIterator ivsit(subset); ivsit.ok(); ++ivsit )
00395 {
00396 const IntVect& iv = ivsit();
00397
00398 iptr = (int*)cptr;
00399 for ( int idir=0; idir<SpaceDim; idir++ ) *iptr++ = iv[idir];
00400 cptr = (char*)iptr;
00401
00402 for( int c=a_comps.begin(); c<=a_comps.end(); c++ ) {
00403 int iloc = getIndex(iv, c);
00404 m_dataPtr[iloc].linearOut( (void*)cptr );
00405 cptr += m_dataPtr[iloc].linearSize();
00406 }
00407 }
00408 if( cptr - ((char*)a_buf) != size(a_region,a_comps) ) {
00409 pout() << " region " << a_region << endl;
00410 pout() << " comps " << a_comps.begin() << " to " << a_comps.end() << endl;
00411 pout() << " size " << size(a_region,a_comps) << endl;
00412 pout() << " ptrdiff " << (cptr - ((char*)a_buf)) << endl;
00413 }
00414 CH_assert( cptr - ((char*)a_buf) == size(a_region,a_comps) );
00415 }
00416
00417 template <class T> inline
00418 void IVSFAB<T>::linearIn(void* a_buf, const Box& a_region, const Interval& a_comps)
00419 {
00420
00421
00422
00423 IntVectSet subset = m_ivs & a_region;
00424
00425
00426 IntVect ibl, ibh;
00427 Real* rptr = (Real*)a_buf;
00428 for ( int idir=0; idir<SpaceDim; idir++ ) {
00429 ibl[idir] = *rptr++;
00430 ibh[idir] = *rptr++;
00431 }
00432
00433
00434 IntVect shift = a_region.smallEnd() - ibl;
00435 IntVect shift2 = a_region.bigEnd() - ibh;
00436 CH_assert(shift==shift2);
00437
00438 int* iptr = (int*)rptr;
00439
00440 int count_received = *iptr++;
00441
00442
00443
00444 char* cptr = (char*)iptr;
00445 for ( int i=0; i<count_received; i++ )
00446 {
00447 IntVect iv2;
00448 iptr = (int*)cptr;
00449 for ( int idir=0; idir<SpaceDim; idir++ ) iv2[idir] = *iptr++;
00450 cptr = (char*)iptr;
00451
00452
00453 iv2 += shift;
00454
00455 for( int c=a_comps.begin(); c<=a_comps.end(); c++ ) {
00456 int iloc = getIndex(iv2, c);
00457 m_dataPtr[iloc].linearIn( (void*)cptr );
00458
00459 cptr += m_dataPtr[iloc].linearSize();
00460 }
00461 }
00462 }
00463
00464 template <class T> inline
00465 int
00466 IVSFAB<T>::getIndex(const IntVect& a_iv, const int& a_comp) const
00467 {
00468 CH_assert(isDefined());
00469 CH_assert(m_ivs.contains(a_iv));
00470 CH_assert((a_comp >= 0) && (a_comp < m_nComp));
00471
00472 int ioffset = m_ivmap(a_iv, 0);
00473 CH_assert(ioffset >= 0);
00474 CH_assert(ioffset < m_nIvs);
00475
00476 ioffset += m_nIvs*a_comp;
00477 return ioffset;
00478 }
00479
00480 template <class T> inline
00481 void
00482 IVSFAB<T>::clear()
00483 {
00484 m_nComp = 0;
00485 m_nIvs = 0;
00486 m_ivs.makeEmpty();
00487 m_ivmap.clear();
00488 if (m_dataPtr != NULL)
00489 {
00490 delete[] m_dataPtr;
00491 m_dataPtr = NULL;
00492 }
00493 m_isDefined = false;
00494 }
00495
00496 template <class T> inline
00497 bool
00498 IVSFAB<T>::isDefined() const
00499 {
00500 return (m_isDefined);
00501 }
00502
00503 template <class T> inline
00504 int
00505 IVSFAB<T>::numIvs() const
00506 {
00507 return m_nIvs;
00508 }
00509
00510 template <class T> inline
00511 int
00512 IVSFAB<T>::nComp() const
00513 {
00514 return m_nComp;
00515 }
00516
00517 template <class T> inline
00518 T&
00519 IVSFAB<T>::operator() (const IntVect& a_ndin,
00520 const int& a_comp)
00521 {
00522 CH_assert(isDefined());
00523 CH_assert(a_comp >= 0);
00524 CH_assert(a_comp < m_nComp);
00525 int iloc = getIndex(a_ndin, a_comp);
00526 return(m_dataPtr[iloc]);
00527 }
00528
00529 template <class T> inline
00530 const T&
00531 IVSFAB<T>::operator() (const IntVect& a_ndin,
00532 const int& a_comp) const
00533 {
00534 CH_assert(isDefined());
00535 CH_assert(a_comp >= 0);
00536 CH_assert(a_comp < m_nComp);
00537 int iloc = getIndex(a_ndin, a_comp);
00538 return(m_dataPtr[iloc]);
00539 }
00540
00541 template <class T> inline
00542 const T*
00543 IVSFAB<T>::dataPtr(const int& a_comp) const
00544 {
00545 CH_assert(isDefined());
00546 CH_assert(a_comp >= 0);
00547 CH_assert(a_comp < m_nComp);
00548 return m_dataPtr + a_comp*m_nIvs;
00549 }
00550
00551 template <class T> inline
00552 T*
00553 IVSFAB<T>::dataPtr(const int& a_comp)
00554 {
00555 CH_assert(isDefined());
00556 CH_assert(a_comp >= 0);
00557 CH_assert(a_comp < m_nComp);
00558 return m_dataPtr + a_comp*m_nIvs;
00559 }
00560
00561 template <class T> inline
00562 const int*
00563 IVSFAB<T>::loVect() const
00564 {
00565 return m_loVect.getVect();
00566 }
00567
00568 template <class T> inline
00569 const int*
00570 IVSFAB<T>::hiVect() const
00571 {
00572 return m_hiVect.getVect();
00573 }
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588 #include "NamespaceFooter.H"
00589 #endif