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 #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
00144
00145
00146
00147
00148
00149
00150
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