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

BoxLayoutData.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 BOXLAYOUTDATA_H
00029 #define BOXLAYOUTDATA_H
00030 
00031 #include "LayoutData.H"
00032 #include "Interval.H"
00033 #include "FArrayBox.H"
00034 
00036 template <class T> class DataFactory
00037 {
00038 public:
00040 
00042   virtual T* create(const Box& box, int ncomps, const DataIndex& a_datInd) const=0;
00043 
00044 };
00045 
00046 
00048 template <class T> class DefaultDataFactory : public DataFactory<T>
00049 {
00050 public:
00052 
00054   virtual T* create(const Box& box, int ncomps, const DataIndex& a_datInd) const;
00055 
00056 };
00057 
00058 
00059 template<class T> class LevelData;
00060 
00061 
00063 
00101 template<class T> 
00102 class BoxLayoutData : public LayoutData<T>
00103 {
00104 public:
00106   BoxLayoutData():m_comps(0) {m_isdefined = false;}
00107 
00108   virtual ~BoxLayoutData(){;}
00110   BoxLayoutData(const BoxLayout& boxes, int comps, 
00111                 const DataFactory<T>& factory = DefaultDataFactory<T>());
00112 
00114   virtual void define(const BoxLayout& boxes, int comps, 
00115                       const DataFactory<T>& factory = DefaultDataFactory<T>());
00116 
00118   virtual void define(const BoxLayoutData<T>& da,
00119                       const DataFactory<T>& factory = DefaultDataFactory<T>());
00120 
00122 
00125   virtual void define(const BoxLayoutData<T>& da, const Interval& comps,
00126                       const DataFactory<T>& factory = DefaultDataFactory<T>());
00127 
00129   virtual void define(const BoxLayout& boxes);
00130 
00132   int nComp() const { return m_comps;}
00133 
00135   Interval interval() const
00136   {
00137     Interval outint(0, m_comps-1);
00138     return(outint);
00139   }
00140 
00141 
00143   /* User writes a function with the signature:
00144      \begin{verbatim}
00145      void setFunction(const Box& box, int comps, T& t){ your code here;}
00146      \end{verbatim}
00147      They can then hand this off to LayoutData::setVal.  This class
00148      then cycles through all the T objects and invokes this function.  Function
00149      must not be inline. (I'm still trying to figure out a nice way to send
00150      in non-static member functions).
00151      */
00152   void setVal(void (*setFunction)(const Box& box, int comps, T& t));
00153 
00154 
00156   virtual bool isDefined() const;
00157 protected:
00158 
00159   int             m_comps;
00160   bool            m_isdefined;
00161 
00162   friend class LevelData<T>;
00163   void setVector(const BoxLayoutData<T>& da, 
00164                  const Interval& srcComps,
00165                  const Interval& destComps);
00166   void allocateGhostVector(const DataFactory<T>& factory, 
00167                            const IntVect& ghost = IntVect::TheZeroVector());
00168 };
00169 
00171 
00190 Real norm(const BoxLayoutData<FArrayBox>& A, 
00191           const Interval& interval,
00192           const int& p = 2);
00193 
00194 
00195 //======================================================================
00196 template < >
00197 BaseFab<int>* DefaultDataFactory<BaseFab<int> >::create(const Box& box, 
00198                                                         int ncomps, 
00199                                                         const DataIndex& a_datInd) const;
00200 
00201 template < >
00202 FArrayBox* DefaultDataFactory<FArrayBox>::create(const Box& box, 
00203                                                  int ncomps,
00204                                                  const DataIndex& a_datInd) const;
00205 
00206 template <class T>
00207 T* DefaultDataFactory<T>::create(const Box& box, 
00208                                  int ncomps, 
00209                                  const DataIndex& a_datInd) const
00210 {
00211   return new T(box, ncomps);
00212 }
00213 
00214 
00215 template<class T>
00216 inline bool BoxLayoutData<T>::isDefined() const
00217 {
00218   return m_isdefined;
00219 }
00220 
00221 template <class T>
00222 inline void BoxLayoutData<T>::setVector(const BoxLayoutData<T>& da, 
00223                                         const Interval& srcComps,
00224                                         const Interval& destComps)
00225 {
00226   if(&da != this)
00227     {
00228       for(DataIterator it(dataIterator()); it.ok(); ++it)
00229         {
00230           m_vector[m_boxLayout.index(it())]->copy( box(it()), destComps, 
00231                                                    box(it()), da[it()], srcComps);
00232         }
00233     }
00234 }
00235 
00236 template<class T>
00237 inline void BoxLayoutData<T>::define(const BoxLayoutData<T>& da, const Interval& comps,
00238                                      const DataFactory<T>& factory)
00239 {
00240   if(this == &da){
00241     MayDay::Error("BoxLayoutData<T>::define(const LayoutData<T>& da,.....) called with 'this'");
00242   }
00243   assert(comps.size()>0);
00244   assert(comps.end()<=m_comps);
00245   assert(comps.begin()>=0);
00246   m_boxLayout = da.boxLayout();
00247 
00248   m_comps = comps.size();
00249 
00250   Interval dest(0, m_comps-1);
00251   allocateGhostVector(factory);
00252   setVector(da, comps, dest);
00253 
00254 }
00255 
00256 template<class T>
00257 inline void BoxLayoutData<T>::define(const BoxLayout& boxes, int comps,
00258                                      const DataFactory<T>& factory)
00259 {
00260   assert(boxes.isClosed());
00261   m_boxLayout = boxes;
00262   m_comps = comps;
00263   m_isdefined = true;
00264   allocateGhostVector(factory);
00265 }
00266 
00267 template<class T>
00268 inline void BoxLayoutData<T>::define(const BoxLayout& boxes)
00269 {
00270   MayDay::Error("BoxLayoutData<T>::define(const BoxLayout& boxes)...needs comps");
00271 }
00272 
00273 template<class T>
00274 inline BoxLayoutData<T>::BoxLayoutData(const BoxLayout& boxes, int comps,
00275                                        const DataFactory<T>& factory)
00276   :m_comps(comps)
00277 {
00278   assert(boxes.isClosed());
00279   m_boxLayout = boxes;
00280   m_isdefined = true;
00281   allocateGhostVector(factory);
00282 }
00283 
00284 template<class T>
00285 inline void BoxLayoutData<T>::define(const BoxLayoutData<T>& da,
00286                                      const DataFactory<T>& factory)
00287 {
00288   if(this != &da){
00289     m_isdefined = da.m_isdefined;
00290     m_boxLayout = da.boxLayout();
00291     m_comps    = da.nComp();
00292     Interval srcAnddest(0, m_comps-1);
00293     allocateGhostVector(factory);
00294     setVector(da, srcAnddest, srcAnddest);
00295   }
00296 }
00297 
00298 
00299 template<class T>
00300 inline void BoxLayoutData<T>::allocateGhostVector(const DataFactory<T>& factory, const IntVect& ghost)
00301 {
00302   for(unsigned int i=0; i<m_vector.size(); ++i)
00303     {
00304       delete m_vector[i];
00305       m_vector[i] = NULL;
00306     }
00307 
00308   m_vector.resize(m_boxLayout.size(), NULL);
00309 
00310   for(DataIterator it(dataIterator()); it.ok(); ++it)
00311     {
00312       unsigned int index = m_boxLayout.index(it());
00313       Box abox = box(it());
00314       abox.grow(ghost);
00315       m_vector[index] = factory.create(abox, m_comps, it());
00316       if(m_vector[index] == NULL)
00317         {
00318           MayDay::Error("OutOfMemory in boxlayoutdata::allocate");
00319         }
00320     }
00321 }
00322 
00323 template<class T>
00324 inline void BoxLayoutData<T>::setVal(void (*a_setVal)(const Box& box, int comps, T& t))
00325 {
00326   for(DataIterator it(dataIterator()); it.ok(); ++it)
00327     {
00328       unsigned int index = m_boxLayout.index(it());
00329       a_setVal(box(it()), m_comps, *(m_vector[index]));
00330     }
00331 }
00332 
00333 
00334 
00335 //======================================================================
00336 
00337 #endif // BOXLAYOUTDATA_H

Generated on Wed Apr 16 14:31:04 2003 for EBChombo by doxygen1.2.16