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{verbatime}
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 
00186 Real norm(const BoxLayoutData<FArrayBox>& A, 
00187           const Interval& interval,
00188           const int& p = 2);
00189 
00190 
00191 //======================================================================
00192 template < >
00193 BaseFab<int>* DefaultDataFactory<BaseFab<int> >::create(const Box& box, 
00194                                                         int ncomps, 
00195                                                         const DataIndex& a_datInd) const;
00196 
00197 template < >
00198 FArrayBox* DefaultDataFactory<FArrayBox>::create(const Box& box, 
00199                                                  int ncomps,
00200                                                  const DataIndex& a_datInd) const;
00201 
00202 template <class T>
00203 T* DefaultDataFactory<T>::create(const Box& box, 
00204                                  int ncomps, 
00205                                  const DataIndex& a_datInd) const
00206 {
00207   return new T(box, ncomps);
00208 }
00209 
00210 
00211 template<class T>
00212 inline bool BoxLayoutData<T>::isDefined() const
00213 {
00214   return m_isdefined;
00215 }
00216 
00217 template <class T>
00218 inline void BoxLayoutData<T>::setVector(const BoxLayoutData<T>& da, 
00219                                         const Interval& srcComps,
00220                                         const Interval& destComps)
00221 {
00222   if(&da != this)
00223     {
00224       for(DataIterator it(dataIterator()); it.ok(); ++it)
00225         {
00226           m_vector[m_boxLayout.index(it())]->copy( box(it()), destComps, 
00227                                                    box(it()), da[it()], srcComps);
00228         }
00229     }
00230 }
00231 
00232 template<class T>
00233 inline void BoxLayoutData<T>::define(const BoxLayoutData<T>& da, const Interval& comps,
00234                                      const DataFactory<T>& factory)
00235 {
00236   if(this == &da){
00237     MayDay::Error("BoxLayoutData<T>::define(const LayoutData<T>& da,.....) called with 'this'");
00238   }
00239   assert(comps.size()>0);
00240   assert(comps.end()<=m_comps);
00241   assert(comps.begin()>=0);
00242   m_boxLayout = da.boxLayout();
00243 
00244   m_comps = comps.size();
00245 
00246   Interval dest(0, m_comps-1);
00247   allocateGhostVector(factory);
00248   setVector(da, comps, dest);
00249 
00250 }
00251 
00252 template<class T>
00253 inline void BoxLayoutData<T>::define(const BoxLayout& boxes, int comps,
00254                                      const DataFactory<T>& factory)
00255 {
00256   assert(boxes.isClosed());
00257   m_boxLayout = boxes;
00258   m_comps = comps;
00259   m_isdefined = true;
00260   allocateGhostVector(factory);
00261 }
00262 
00263 template<class T>
00264 inline void BoxLayoutData<T>::define(const BoxLayout& boxes)
00265 {
00266   MayDay::Error("BoxLayoutData<T>::define(const BoxLayout& boxes)...needs comps");
00267 }
00268 
00269 template<class T>
00270 inline BoxLayoutData<T>::BoxLayoutData(const BoxLayout& boxes, int comps,
00271                                        const DataFactory<T>& factory)
00272   :m_comps(comps)
00273 {
00274   assert(boxes.isClosed());
00275   m_boxLayout = boxes;
00276   m_isdefined = true;
00277   allocateGhostVector(factory);
00278 }
00279 
00280 template<class T>
00281 inline void BoxLayoutData<T>::define(const BoxLayoutData<T>& da,
00282                                      const DataFactory<T>& factory)
00283 {
00284   if(this != &da){
00285     m_isdefined = da.m_isdefined;
00286     m_boxLayout = da.boxLayout();
00287     m_comps    = da.nComp();
00288     Interval srcAnddest(0, m_comps-1);
00289     allocateGhostVector(factory);
00290     setVector(da, srcAnddest, srcAnddest);
00291   }
00292 }
00293 
00294 
00295 template<class T>
00296 inline void BoxLayoutData<T>::allocateGhostVector(const DataFactory<T>& factory, const IntVect& ghost)
00297 {
00298   for(unsigned int i=0; i<m_vector.size(); ++i)
00299     {
00300       delete m_vector[i];
00301       m_vector[i] = NULL;
00302     }
00303 
00304   m_vector.resize(m_boxLayout.size(), NULL);
00305 
00306   for(DataIterator it(dataIterator()); it.ok(); ++it)
00307     {
00308       unsigned int index = m_boxLayout.index(it());
00309       Box abox = box(it());
00310       abox.grow(ghost);
00311       m_vector[index] = factory.create(abox, m_comps, it());
00312       if(m_vector[index] == NULL)
00313         {
00314           MayDay::Error("OutOfMemory in boxlayoutdata::allocate");
00315         }
00316     }
00317 }
00318 
00319 template<class T>
00320 inline void BoxLayoutData<T>::setVal(void (*a_setVal)(const Box& box, int comps, T& t))
00321 {
00322   for(DataIterator it(dataIterator()); it.ok(); ++it)
00323     {
00324       unsigned int index = m_boxLayout.index(it());
00325       a_setVal(box(it()), m_comps, *(m_vector[index]));
00326     }
00327 }
00328 
00329 
00330 
00331 //======================================================================
00332 
00333 #endif // BOXLAYOUTDATA_H

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