Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

BinFabImplem.H

Go to the documentation of this file.
00001 /* _______              __
00002   / ___/ /  ___  __ _  / /  ___
00003  / /__/ _ \/ _ \/  ' \/ _ \/ _ \
00004  \___/_//_/\___/_/_/_/_.__/\___/ 
00005 */
00006 //
00007 // This software is copyright (C) by the Lawrence Berkeley
00008 // National Laboratory.  Permission is granted to reproduce
00009 // this software for non-commercial purposes provided that
00010 // this notice is left intact.
00011 // 
00012 // It is acknowledged that the U.S. Government has rights to
00013 // this software under Contract DE-AC03-765F00098 between
00014 // the U.S.  Department of Energy and the University of
00015 // California.
00016 //
00017 // This software is provided as a professional and academic
00018 // contribution for joint exchange. Thus it is experimental,
00019 // is provided ``as is'', with no warranties of any kind
00020 // whatsoever, no support, no promise of updates, or printed
00021 // documentation. By using this software, you acknowledge
00022 // that the Lawrence Berkeley National Laboratory and
00023 // Regents of the University of California shall have no
00024 // liability with respect to the infringement of other
00025 // copyrights by any part of this software.
00026 //
00027 
00028 #ifndef CH_BINFABIMPLEM_H
00029 #define CH_BINFABIMPLEM_H
00030 
00031 #include "BoxIterator.H"
00032 #include "List.H"
00033 
00034 
00035 //
00036 // Implementation.=====================================
00037 //
00038 
00039 template <class T>
00040 BinFab<T>::BinFab()
00041   : BaseFab<List<T> >(),
00042   m_origin(D_DECL(1.0e8, 1.0e8, 1.0e8)),
00043   m_mesh_spacing(D_DECL(0,0,0))
00044 {
00045 }
00046 
00047 template <class T>
00048 BinFab<T>::BinFab(const Box& a_domain, const RealVect& a_mesh_spacing,
00049                   const RealVect& a_origin, 
00050                   const ProblemDomain& a_probdomain)
00051 {
00052   define(a_domain, a_mesh_spacing, a_origin, a_probdomain);
00053 }
00054 
00055 template <class T> 
00056 BinFab<T>::BinFab(const BinFab& a_binfab)
00057 {
00058   define(a_binfab.domain, a_binfab.m_mesh_spacing, a_binfab.m_origin,
00059          a_binfab.m_probdomain);
00060   copy(a_binfab);
00061 }
00062 
00063 
00064 template <class T>
00065 BinFab<T>::~BinFab()
00066 {
00067   undefine();
00068 }
00069 
00070 template <class T>
00071 void
00072 BinFab<T>::define(const Box& a_domain, const RealVect& a_mesh_spacing,
00073                   const RealVect& a_origin, const ProblemDomain& a_probdomain)
00074 {
00075   domain = a_domain;
00076   nvar = 1;
00077   numpts = domain.numPts();
00078   
00079   BaseFab<List<T> >::define();
00080   
00081   m_mesh_spacing = a_mesh_spacing;
00082   m_origin = a_origin;
00083   // periodic stuff not implemented yet
00084   assert (!a_probdomain.isPeriodic());
00085   m_probdomain = a_probdomain;
00086 }
00087 
00088 
00089 template <class T>
00090 void
00091 BinFab<T>::reBin()
00092 {
00093   // loop through each bin and determine if each binItem is 
00094   // in the correct bin.
00095   BoxIterator bit(domain);
00096   int comp = 0;
00097   
00098   for (bit.begin(); bit.ok(); ++bit) 
00099     {
00100       const IntVect thisIV = bit();      
00101       List<T>& thisList = operator()(thisIV,comp);
00102 
00103       if (thisList.length() > 0) 
00104         {
00105           // now index through the List and figure out which bin this
00106           // particle should be in
00107           ListIterator<T> thisLit(thisList);
00108           
00109           IntVect binLoc;
00110           //RealVect thisPos;
00111           
00112           for (thisLit.rewind(); thisLit; ++thisLit)
00113             {
00114               T& thisItem = thisList[thisLit];
00115 
00116 #if 0 
00117               // moved bin-location stuff into separate function
00118               thisPos = thisItem.position();
00119               thisPos -= m_origin;
00120               thisPos /= m_mesh_spacing;
00121               D_TERM( binLoc[0] = (int) thisPos[0]; ,
00122                       binLoc[1] = (int) thisPos[1]; ,
00123                       binLoc[2] = (int) thisPos[2]; );
00124 #endif
00125 
00126               binLoc = locateBin(thisItem);
00127               
00128               if (binLoc != thisIV) 
00129                 {
00130                   // binItem needs to be moved.
00131                   // note that if binItem moves outside of domain, it is lost
00132                   
00133                   if (domain.contains(binLoc)) 
00134                     operator()(binLoc,comp).append(thisItem);
00135                   
00136                   thisList.remove(thisLit);
00137                 }
00138             
00139               
00140             } // end loop over items in this List of binItems
00141         } // end if this list has items
00142       
00143     } // end loop over bins
00144 }
00145 
00146 template <class T>
00147 void
00148 BinFab<T>::addItem(const T& a_item)
00149 {
00150     int comp = 0 ;
00151     IntVect binLoc = locateBin(a_item) ;
00152     if (domain.contains(binLoc))
00153         operator()(binLoc,comp).append(a_item) ;
00154 }
00155 
00156 template <class T>
00157 void
00158 BinFab<T>::addItems(const List<T>& a_List)
00159 {
00160   // loop through items in List, and add to appropriate locations
00161   ListIterator<T> lit(a_List);
00162 
00163   int comp = 0;
00164   IntVect binLoc;
00165   //RealVect thisPos;
00166 
00167   if (a_List.length() > 0) 
00168     {
00169       for (lit.rewind(); lit; ++lit)
00170         {
00171 
00172           // want to put bin-location stuff in a separate function
00173           const T& thisItem =  a_List[lit];
00174 #if 0          
00175           // want to put bin-location stuff in a separate function
00176           thisPos = thisItem.position();
00177           thisPos -= m_origin;
00178           thisPos /= m_mesh_spacing;
00179           
00180           D_TERM( binLoc[0] = (int) thisPos[0]; ,
00181                   binLoc[1] = (int) thisPos[1]; ,
00182                   binLoc[2] = (int) thisPos[2]; );
00183 #endif
00184           binLoc = locateBin(thisItem);
00185           
00186           if (domain.contains(binLoc))
00187             operator()(binLoc,comp).append(thisItem);
00188         }
00189     }
00190 }
00191 
00192 template <class T>
00193 void
00194 BinFab<T>::addItemsDestructive(List<T>& a_List)
00195 {
00196   // loop through items in List, add to appropriate bins, 
00197   // and remove from List
00198   ListIterator<T> lit(a_List);
00199   int comp = 0;
00200   IntVect binLoc;
00201 
00202   if (a_List.length() > 0) 
00203     {
00204       for (lit.rewind(); lit; ++lit)
00205         {
00206           const T& thisItem =  a_List[lit];
00207           binLoc = locateBin(thisItem);
00208           if (domain.contains(binLoc))
00209             {
00210               operator()(binLoc,comp).append(thisItem);
00211               a_List.remove(lit);
00212             }
00213         }
00214     }
00215 }
00216 
00217 template <class T>
00218 void
00219 BinFab<T>::clear()
00220 {
00221   undefine();
00222   
00223   domain = Box();
00224   nvar = 0;
00225   numpts = 0;
00226   m_origin = RealVect(D_DECL(1.0e8, 1.0e8, 1.0e8));
00227   m_mesh_spacing = RealVect(D_DECL(1.0e8, 1.0e8, 1.0e8));
00228 
00229 }
00230 
00231 template <class T>
00232 int
00233 BinFab<T>::size(const Box& box, const Interval& comps) const
00234 {
00235   int totalSize=0;
00236   BoxIterator bit(box);
00237 
00238   // first extract the size of the binItem:
00239   bit.begin();
00240   const List<T>& thisList = operator()(bit(), comps.begin());
00241   int sizeOfT = thisList.firstElement().size();
00242 
00243   for (int comp=comps.begin(); comp<= comps.end(); comp++) 
00244     {
00245       for (bit.begin(); bit.ok(); ++bit) 
00246         {
00247           const List<T>& thisList = operator()(bit(), comp);
00248           totalSize += thisList.length();
00249         }
00250     }
00251 
00252   // totalSize now has total number of binItems.  now compute total size
00253   totalSize *= sizeOfT;
00254 
00255   // will also need to add an integer for each list to determine how 
00256   // many binItems are in each list. (is this the right thing to do?)
00257   // alternate approach would be to simply unpack a List<T> on the other
00258   // end and then do an addItems call with that list.  that will probably
00259   // be slower, though.
00260   int numBins = box.numPts();
00261   totalSize += numBins*sizeof(int);
00262 
00263   return totalSize;
00264 
00265 }
00266 
00267 template <class T> 
00268 void
00269 BinFab<T>::linearOut(void* buf, const Box& R, const Interval& comps) const
00270 {
00271   // all we do in this function is loop over the box (and components),
00272   // and call linearOut on the individual items...
00273   BoxIterator bit(R);
00274   for (int comp=comps.begin(); comp<= comps.end(); ++comp)
00275     {
00276       for (bit.begin(); bit.ok(); ++bit) 
00277         {
00278           const List<T>& thisList = operator()(bit(), comp);
00279           int* intBuffer = (int*)buf;
00280           *intBuffer = thisList.length();
00281 
00282           // now loop over the items in the list.
00283           ListIterator<T> lit(thisList);
00284           for (lit.rewind(); lit; ++lit)
00285             {
00286               thisList[lit].linearOut(buf);
00287             }
00288         }
00289     }
00290 }
00291 
00292 template <class T>
00293 void
00294 BinFab<T>::linearIn(void* buf, const Box& R, const Interval& comps)
00295 {
00296   // should be just the inverse of linearOut
00297   BoxIterator bit(R);
00298   for (int comp= comps.begin(); comp<= comps.end(); ++comp)
00299     {
00300       for (bit.begin(); bit.ok(); ++bit) 
00301         {
00302           List<T>& thisList = operator()(bit(), comp);
00303           int* intBuffer = (int*)buf;
00304           int numItems = *intBuffer;
00305           for (int n=0; n<numItems; ++n)
00306             {
00307               T thisBinItem;
00308               thisBinItem.linearIn(buf);
00309               thisList.append(thisBinItem);
00310             }
00311         }
00312     }
00313 
00314 }
00315 
00316 template <class T>
00317 IntVect
00318 BinFab<T>::locateBin(const T& a_binItem) const
00319 {
00320   IntVect binLoc;
00321   RealVect thisPos = a_binItem.position();
00322   thisPos -= m_origin;
00323   thisPos /= m_mesh_spacing;
00324 
00325   D_TERM( binLoc[0] = (int) thisPos[0]; ,
00326           binLoc[1] = (int) thisPos[1]; ,
00327           binLoc[2] = (int) thisPos[2]; );
00328 
00329   return binLoc;
00330 }
00331 
00332   
00333 
00334 #endif 

Generated on Tue Jul 2 10:42:19 2002 for Chombo by doxygen1.2.16