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
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