00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #ifndef _BINFABIMPLEM_H_
00053 #define _BINFABIMPLEM_H_
00054
00055 #include <cmath>
00056
00057 #include "BoxIterator.H"
00058 #include "parstream.H"
00059
00060
00061
00062
00063
00064 template <class T>
00065 BinFab<T>::BinFab()
00066 : BaseFab<List<T> >(),
00067
00068 m_origin(D_DECL(0,0,0)),
00069 m_mesh_spacing(D_DECL(0,0,0))
00070 {
00071 }
00072
00073 template <class T>
00074 BinFab<T>::BinFab(const Box& a_domain,
00075 const RealVect& a_meshSpacing,
00076 const RealVect& a_origin)
00077 {
00078 define(a_domain, a_meshSpacing, a_origin);
00079 }
00080
00081 template <class T>
00082 BinFab<T>::BinFab(const BinFab<T>& a_binfab)
00083 {
00084 define(a_binfab.m_domain, a_binfab.m_mesh_spacing, a_binfab.m_origin);
00085 BaseFab<List<T> >::copy(a_binfab);
00086 }
00087
00088 template <class T>
00089 BinFab<T>::~BinFab()
00090 {
00091 this->undefine();
00092 }
00093
00094 template <class T>
00095 void BinFab<T>::define(const Box& a_domain,
00096 const RealVect& a_meshSpacing,
00097 const RealVect& a_origin)
00098 {
00099
00100 this->m_domain = a_domain;
00101 this->m_nvar = 1;
00102 this->m_numpts = this->m_domain.numPts();
00103 BaseFab<List<T> >::define();
00104
00105 m_mesh_spacing = a_meshSpacing;
00106 m_origin = a_origin;
00107 }
00108
00109
00110
00111 template <class T>
00112 void BinFab<T>::meshSpacing(const RealVect & a_meshSpacing)
00113 {
00114 m_mesh_spacing = a_meshSpacing;
00115 }
00116
00117
00118 template <class T>
00119 RealVect BinFab<T>::meshSpacing() const
00120 {
00121 return m_mesh_spacing;
00122 }
00123
00124
00125
00126 template <class T>
00127 void BinFab<T>::origin(const RealVect& a_origin)
00128 {
00129 m_origin = a_origin;
00130 }
00131
00132
00133 template <class T>
00134 RealVect BinFab<T>::origin() const
00135 {
00136 return m_origin;
00137 }
00138
00140
00142 template <class T>
00143 void BinFab<T>::addItem(const T& a_item)
00144 {
00145 int comp = 0;
00146 IntVect binLoc = locateBin(a_item);
00147 if (this->m_domain.contains(binLoc))
00148 {
00149 this->operator()(binLoc,comp).append(a_item);
00150 }
00151 }
00152
00154 template <class T>
00155 void BinFab<T>::addItem(const T& a_item, const IntVect& a_binLoc)
00156 {
00157 int comp = 0;
00158 if (this->m_domain.contains(a_binLoc))
00159 {
00160 this->operator()(a_binLoc,comp).append(a_item);
00161 }
00162 }
00163
00165 template <class T>
00166 void BinFab<T>::addItems(const List<T>& a_List)
00167 {
00168 if (a_List.isNotEmpty())
00169 {
00170
00171 ListIterator<T> lit(a_List);
00172 int comp = 0;
00173 IntVect binLoc;
00174
00175 for (lit.rewind(); lit; ++lit)
00176 {
00177 const T& thisItem = a_List[lit];
00178 binLoc = locateBin(thisItem);
00179
00180 if (this->m_domain.contains(binLoc))
00181 {
00182 this->operator()(binLoc,comp).append(thisItem);
00183 }
00184 }
00185 }
00186 }
00187
00189
00191
00192 template <class T>
00193 void BinFab<T>::addItemsDestructive(List<T>& a_List)
00194 {
00195 if (a_List.isNotEmpty())
00196 {
00197
00198
00199 ListIterator<T> lit(a_List);
00200 int comp = 0;
00201 IntVect binLoc;
00202
00203 for (lit.rewind(); lit; )
00204 {
00205 const T& thisItem = a_List[lit];
00206 binLoc = locateBin(thisItem);
00207 if (this->m_domain.contains(binLoc))
00208 {
00209
00210
00211 this->operator()(binLoc,comp).transfer(lit);
00212 }
00213 else
00214 {
00215
00216
00217 ++lit ;
00218 }
00219 }
00220 }
00221 }
00222
00224
00225 template <class T>
00226 void BinFab<T>::addItemsDestructive(List<T>& a_List, const Box& a_valid ,bool a_in)
00227 {
00228 assert( a_valid.cellCentered() );
00229 if (a_List.isNotEmpty())
00230 {
00231
00232
00233 ListIterator<T> lit(a_List);
00234 int comp = 0;
00235 IntVect binLoc;
00236
00237 for (lit.rewind(); lit; )
00238 {
00239 const T& thisItem = a_List[lit];
00240 binLoc = locateBin(thisItem);
00241 if (this->m_domain.contains(binLoc))
00242 {
00243
00244
00245
00246
00247
00248
00249
00250 if( a_valid.contains(binLoc) )
00251 {
00252 if( a_in )
00253 {
00254
00255
00256 this->operator()(binLoc,comp).transfer(lit);
00257 }
00258 else
00259 {
00260 this->operator()(binLoc,comp).append(thisItem);
00261 ++lit ;
00262 }
00263 }
00264 else
00265 {
00266 if( a_in )
00267 {
00268 this->operator()(binLoc,comp).append(thisItem);
00269 ++lit;
00270 }
00271 else
00272 {
00273
00274
00275 this->operator()(binLoc,comp).transfer(lit);
00276 }
00277 }
00278
00279 }
00280 else
00281 {
00282
00283 ++lit ;
00284 }
00285 }
00286 }
00287 }
00288
00290
00291
00292
00293
00294 template <class T>
00295 void BinFab<T>::reBin()
00296 {
00297
00298
00299 BoxIterator bit(this->m_domain);
00300 int comp = 0;
00301 IntVect binLoc;
00302
00303
00304 for (BoxIterator bit(this->m_domain); bit.ok(); ++bit)
00305 {
00306 const IntVect thisIV = bit();
00307 List<T>& thisList = this->operator()(thisIV,comp);
00308
00309 if (thisList.isNotEmpty())
00310 {
00311
00312
00313 for (ListIterator<T> thisLit(thisList); thisLit;)
00314 {
00315 T& thisItem = thisList[thisLit];
00316 binLoc = locateBin(thisItem);
00317
00318 if (binLoc != thisIV)
00319 {
00320
00321
00322 if (this->m_domain.contains(binLoc))
00323 {
00324
00325
00326 this->operator()(binLoc,comp).transfer(thisLit);
00327 }
00328 else
00329 {
00330
00331 thisList.remove(thisLit);
00332 }
00333 }
00334 else
00335 {
00336
00337 ++thisLit;
00338 }
00339 }
00340 }
00341 }
00342
00343 }
00344
00345
00346
00347
00348
00349 template <class T>
00350 void BinFab<T>::reBin(List<T>& a_lost, const Box & a_valid, bool a_in)
00351 {
00352
00353 int comp = 0;
00354 IntVect binLoc;
00355 Box valid = a_valid.isEmpty() ? this->m_domain : a_valid ;
00356
00357
00358 for (BoxIterator bit(this->m_domain); bit.ok(); ++bit)
00359 {
00360 const IntVect thisIV = bit();
00361 List<T>& thisList = this->operator()(thisIV,comp);
00362 if (thisList.isNotEmpty())
00363 {
00364
00365 for (ListIterator<T> thisLit(thisList); thisLit;)
00366 {
00367 T& thisItem = thisList[thisLit];
00368 binLoc = locateBin(thisItem);
00369 if (binLoc != thisIV)
00370 {
00371
00372 if (valid.contains(binLoc))
00373 {
00374 if (a_in) a_lost.transfer(thisLit) ;
00375 else this->operator()(binLoc,comp).transfer(thisLit);
00376 }
00377 else if (this->m_domain.contains(binLoc))
00378 {
00379 this->operator()(binLoc,comp).transfer(thisLit);
00380 }
00381 else
00382 {
00383 if (!a_in) a_lost.transfer(thisLit);
00384 else thisList.remove(thisLit);
00385 }
00386
00387 }
00388 else
00389 {
00390
00391 ++thisLit;
00392 }
00393 }
00394 }
00395 }
00396 }
00397
00399
00400
00401
00402
00403
00404 template <class T>
00405 void BinFab<T>::transfer(BinFab<T>& a_src,
00406 const Box& a_srcBox,
00407 const Box& a_destBox)
00408 {
00409 assert(a_src.box().contains(a_srcBox));
00410 assert(a_destBox.sameSize(a_srcBox));
00411
00412 const int comp = 0, ncomps = 1;
00413
00414
00415
00416 ForAllThisBNNXCBN(List<T>,a_destBox,comp,ncomps,a_src,a_srcBox,comp)
00417 {
00418 if(a_srcR.isNotEmpty())
00419 {
00420
00421 List<T>& src = (List<T>&)a_srcR ;
00422
00423
00424 thisR.catenate(src);
00425 }
00426 } EndForTX
00427 }
00428
00430
00431
00432 template <class T>
00433 void BinFab<T>::clear()
00434 {
00435 this->undefine();
00436 this->m_domain = Box();
00437 this->m_nvar = 0;
00438 this->m_numpts = 0;
00439 m_origin = RealVect::Zero;
00440 m_mesh_spacing = RealVect::Zero;
00441 }
00442
00444
00445
00446 template <class T>
00447 int BinFab<T>::numItems(const Box& a_box) const
00448 {
00449
00450 const int comp = 0 ;
00451
00452
00453
00454 Box ibox = a_box;
00455 if (ibox.isEmpty())
00456 {
00457 ibox = this->box();
00458 }
00459
00460 int totalSize = 0;
00461 for (BoxIterator bi(ibox); bi.ok(); ++bi)
00462 {
00463 totalSize += this->operator()(bi(),comp).length();
00464 }
00465
00466
00467 return totalSize;
00468 }
00469
00471
00472
00473 template <class T>
00474 int BinFab<T>::size(const Box& a_box, const Interval& a_comps) const
00475 {
00476
00477 const int ncomps = 1 ;
00478
00479
00480
00481 int totalSize = numItems(a_box);
00482
00483 {
00484
00485 int sizeOfT;
00486 T tmp;
00487 sizeOfT = tmp.size();
00488
00489 totalSize *= sizeOfT;
00490 }
00491
00492
00493
00494
00495
00496 int numBins = a_box.numPts() * ncomps ;
00497 totalSize += numBins*sizeof(int);
00498 return totalSize;
00499 }
00500
00502
00503
00504 template <class T>
00505 void BinFab<T>::linearOut(void* a_buf, const Box& a_box, const Interval& a_comps) const
00506 {
00507
00508 const int comp = 0 ;
00509
00510
00511
00512 char* buf_ch = (char*)a_buf;
00513
00514
00515 for (BoxIterator bit(a_box); bit.ok(); ++bit)
00516 {
00517 const List<T>& thisList = this->operator()(bit(), comp);
00518
00519 int *intBuffer = (int*)buf_ch;
00520 *intBuffer = thisList.length();
00521 buf_ch += sizeof(int);
00522
00523 for (ListIterator<T> lit(thisList); lit.ok(); ++lit)
00524 {
00525 thisList[lit].linearOut(buf_ch);
00526 buf_ch += thisList[lit].size();
00527 }
00528 }
00529 }
00530
00532
00533
00534 template <class T>
00535 void
00536 BinFab<T>::linearOutDestructive(void* a_buf, const Box& a_box, const Interval& a_comps)
00537 {
00538 const int comp = 0 ;
00539
00540
00541 char* buf_ch = (char*)a_buf;
00542 for (BoxIterator bit(a_box); bit.ok(); ++bit)
00543 {
00544 List<T>& thisList = this->operator()(bit(), comp);
00545 int *intBuffer = (int*)buf_ch;
00546 *intBuffer = thisList.length();
00547 buf_ch += sizeof(int);
00548 for (ListIterator<T> lit(thisList); lit.ok(); ++lit)
00549 {
00550 thisList[lit].linearOut(buf_ch);
00551 buf_ch += thisList[lit].size();
00552 }
00553 thisList.clear();
00554 }
00555 }
00556
00558
00559
00560 template <class T>
00561 void
00562 BinFab<T>::linearIn(void* a_buf, const Box& a_box, const Interval& a_comps)
00563 {
00564
00565
00566 const int comp = 0 ;
00567
00568
00569
00570 char *buf_ch = (char*)a_buf;
00571
00572
00573 for (BoxIterator bit(a_box); bit.ok(); ++bit)
00574 {
00575 List<T>& thisList = this->operator()(bit(), comp);
00576
00577 thisList.clear();
00578
00579
00580 int numItems = *((int*)buf_ch);
00581 buf_ch += sizeof(int);
00582
00583 for (int n=0; n<numItems; ++n)
00584 {
00585 T thisBinItem;
00586 thisBinItem.linearIn( buf_ch );
00587 thisList.append(thisBinItem);
00588 buf_ch += thisBinItem.size();
00589 }
00590 }
00591 }
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00612
00613
00614 template <class T>
00615 inline
00616 IntVect
00617 BinFab<T>::locateBin(const T& a_binItem) const
00618 {
00619 IntVect binLoc;
00620 RealVect thisPos = a_binItem.position();
00621 thisPos -= m_origin;
00622 thisPos /= m_mesh_spacing;
00623
00624 for( int d=0 ; d<SpaceDim ; ++d )
00625 {
00626 binLoc[d] = (int)floor(thisPos[d]);
00627 }
00628 return binLoc;
00629 }
00630
00631 #endif