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