00001 #ifdef CH_LANG_CC
00002
00003
00004
00005
00006
00007
00008
00009 #endif
00010
00011 #ifndef _MINIIFFABI_H_
00012 #define _MINIIFFABI_H_
00013 #include "MayDay.H"
00014 #include "FaceIterator.H"
00015 #include <typeinfo>
00016 #include "NamespaceHeader.H"
00017
00018 template <class T>
00019 bool MiniIFFAB<T>::s_verbose = false;
00020
00021
00022 template <class T> inline
00023 MiniIFFAB<T>::MiniIFFAB()
00024 {
00025 setDefaultValues();
00026 }
00027
00028 template <class T> inline
00029 MiniIFFAB<T>::~MiniIFFAB()
00030 {
00031 clear();
00032 }
00033
00034 template <class T> inline
00035 MiniIFFAB<T>::
00036 MiniIFFAB(const Box& a_box,
00037 const EBGraph& a_ebgraph,
00038 const int& a_direction,
00039 const int& a_nvarin)
00040 {
00041 setDefaultValues();
00042 define(a_box, a_ebgraph, a_direction, a_nvarin);
00043 }
00044
00045 template <class T> inline
00046 void
00047 MiniIFFAB<T>::
00048 define(const Box& a_box,
00049 const EBGraph& a_ebgraph,
00050 const int& a_direction,
00051 const int& a_nvarin)
00052 {
00053 clear();
00054 m_isDefined = true;
00055 CH_assert(a_nvarin > 0);
00056 CH_assert((a_direction >= 0) && (a_direction < SpaceDim));
00057
00058 m_direction = a_direction;
00059 m_ebgraph = a_ebgraph;
00060 m_nComp = a_nvarin;
00061 m_box = a_box;
00062
00063 m_faces = m_ebgraph.getMultiValuedFaces(a_direction, a_box);
00064 m_nFaces = m_faces.size();
00065
00066 m_data = new T[m_nComp*m_nFaces];
00067 }
00068
00069 template <class T> inline
00070 MiniIFFAB<T>::
00071 MiniIFFAB(const IntVectSet& a_ivs,
00072 const EBGraph& a_ebgraph,
00073 const int& a_direction,
00074 const int& a_nvarin)
00075 {
00076 setDefaultValues();
00077 define(a_ivs, a_ebgraph, a_direction, a_nvarin);
00078 }
00079
00080 template <class T> inline
00081 void
00082 MiniIFFAB<T>::
00083 define(const IntVectSet& a_ivs,
00084 const EBGraph& a_ebgraph,
00085 const int& a_direction,
00086 const int& a_nvarin)
00087 {
00088 clear();
00089 m_isDefined = true;
00090 CH_assert(a_nvarin > 0);
00091 CH_assert((a_direction >= 0) && (a_direction < SpaceDim));
00092
00093 m_direction = a_direction;
00094 m_ebgraph = a_ebgraph;
00095 m_nComp = a_nvarin;
00096 m_box = a_ivs.minBox();
00097
00098 FaceIterator faceit(a_ivs, a_ebgraph, a_direction, FaceStop::SurroundingWithBoundary);
00099 m_faces = faceit.getVector();
00100 m_nFaces = m_faces.size();
00101
00102 m_data = new T[m_nComp*m_nFaces];
00103 }
00104
00105 template <class T> inline
00106 void
00107 MiniIFFAB<T>::
00108 setVal(const T& a_value)
00109 {
00110 CH_assert(isDefined());
00111 for (int ivec = 0; ivec < m_nFaces*m_nComp; ivec++)
00112 {
00113 m_data[ivec] = a_value;
00114 }
00115 }
00116
00117 template <class T> inline
00118 void
00119 MiniIFFAB<T>::
00120 setVal(int ivar, const T& a_value)
00121 {
00122 CH_assert(isDefined());
00123 CH_assert(ivar >= 0);
00124 CH_assert(ivar < m_nComp);
00125
00126 for (int ivec = ivar; ivec < m_nFaces*m_nComp; ivec += m_nComp)
00127 {
00128 m_data[ivec] = a_value;
00129 }
00130 }
00131
00132 template <class T> inline
00133 void
00134 MiniIFFAB<T>::
00135 copy(const Box& a_fromBox,
00136 const Interval& a_dstInterval,
00137 const Box& a_toBox,
00138 const MiniIFFAB<T>& a_src,
00139 const Interval& a_srcInterval)
00140 {
00141 CH_assert(isDefined());
00142 CH_assert(a_src.isDefined());
00143 CH_assert(a_srcInterval.size() == a_dstInterval.size());
00144 CH_assert(a_dstInterval.begin() >= 0);
00145 CH_assert(a_srcInterval.begin() >= 0);
00146 CH_assert(a_dstInterval.end() < m_nComp);
00147 CH_assert(a_srcInterval.end() < a_src.m_nComp);
00148
00149 if ((!m_box.isEmpty()) && (!a_src.m_box.isEmpty()))
00150 {
00151 CH_assert(a_fromBox == a_toBox);
00152 Box intBox = a_toBox;
00153 Box boxIntersect = m_box;
00154 boxIntersect &= a_src.m_box;
00155 boxIntersect &= intBox;
00156 int compSize = a_srcInterval.size();
00157
00158 for (int iface = 0; iface < m_faces.size(); iface++)
00159 {
00160 const FaceIndex& face = m_faces[iface];
00161 const IntVect& ivHi = face.gridIndex(Side::Hi);
00162 const IntVect& ivLo = face.gridIndex(Side::Lo);
00163 if (boxIntersect.contains(ivHi) || boxIntersect.contains(ivLo))
00164 {
00165 for (int icomp = 0; icomp < compSize; icomp++)
00166 {
00167 int isrccomp = a_srcInterval.begin() + icomp;
00168 int idstcomp = a_dstInterval.begin() + icomp;
00169 (*this)(face, idstcomp) = a_src(face, isrccomp);
00170 }
00171 }
00172 }
00173 }
00174 }
00175
00176 template <class T> inline
00177 T*
00178 MiniIFFAB<T>::
00179 getIndex(const FaceIndex& a_face, const int& a_comp) const
00180 {
00181 CH_assert(m_isDefined);
00182 CH_assert((a_comp >= 0) && (a_comp < this->m_nComp));
00183
00184 T* dataPtr = this->m_data;
00185 bool found = false;
00186 T* retval=NULL;
00187 for (int iface = 0; iface < m_faces.size(); ++iface)
00188 {
00189 if (a_face == m_faces[iface])
00190 {
00191 found = true;
00192 retval=dataPtr;
00193 }
00194 if (found) break;
00195 dataPtr++;
00196 }
00197 if (!found)
00198 {
00199 MayDay::Error("MiniIFFAB:index not found");
00200 }
00201 retval += a_comp*this->m_nFaces;
00202 return retval;
00203
00204 }
00205 template <class T> inline
00206 MiniIFFAB<T>::MiniIFFAB(const Interval& a_comps,
00207 MiniIFFAB<T>& a_original)
00208 :m_data(a_original.dataPtr(a_comps.begin())),
00209 m_faces(a_original.m_faces),
00210 m_nFaces(a_original.m_nFaces),
00211 m_nComp(a_comps.size()),
00212 m_direction(a_original.m_direction),
00213 m_aliased(true),
00214 m_isDefined(a_original.m_isDefined)
00215 {
00216 }
00217
00218
00219 template <class T> inline
00220 void
00221 MiniIFFAB<T>::
00222 clear()
00223 {
00224 if ((m_data != NULL)&&(!m_aliased))
00225 {
00226 delete[] m_data;
00227 m_data = NULL;
00228 }
00229 m_faces.resize(0);
00230 setDefaultValues();
00231 }
00232
00233 template <class T> inline
00234 T&
00235 MiniIFFAB<T>::operator() (const FaceIndex& a_ndin,
00236 const int& a_comp)
00237 {
00238 CH_assert(isDefined());
00239 CH_assert(a_ndin.direction() == m_direction);
00240
00241 T* dataPtr = getIndex(a_ndin, a_comp);
00242 return(*dataPtr);
00243 }
00244
00245 template <class T> inline
00246 const T&
00247 MiniIFFAB<T>::operator() (const FaceIndex& a_ndin,
00248 const int& a_comp) const
00249 {
00250 CH_assert(isDefined());
00251 CH_assert(a_ndin.direction() == m_direction);
00252
00253 T* dataPtr = getIndex(a_ndin, a_comp);
00254 return(*dataPtr);
00255 }
00256
00257 template <class T> inline
00258 T*
00259 MiniIFFAB<T>::dataPtr(const int& a_comp)
00260 {
00261 CH_assert(a_comp >= 0);
00262 CH_assert(a_comp <= m_nComp);
00263 return m_data + a_comp*m_nFaces;
00264 }
00265
00266 template <class T> inline
00267 const T*
00268 MiniIFFAB<T>::dataPtr(const int& a_comp) const
00269 {
00270 CH_assert(a_comp >= 0);
00271 CH_assert(a_comp <= m_nComp);
00272 return m_data + a_comp*m_nFaces;
00273 }
00274
00275 template <class T> inline
00276 void
00277 MiniIFFAB<T>::setDefaultValues()
00278 {
00279 m_isDefined = false;
00280 m_data = NULL;
00281 m_nFaces = 0;
00282 m_nComp = 0;
00283 m_direction = -1;
00284 }
00285
00286 template <class T> inline
00287 void
00288 MiniIFFAB<T>::
00289 getFaceSubset(Vector<FaceIndex>& a_subFaces, const Box& a_subBox) const
00290 {
00291 a_subFaces.resize(0);
00292 for (int iface = 0; iface < m_faces.size(); iface++)
00293 {
00294 if ((a_subBox.contains(m_faces[iface].gridIndex(Side::Lo))) ||
00295 (a_subBox.contains(m_faces[iface].gridIndex(Side::Hi))))
00296 {
00297 a_subFaces.push_back(m_faces[iface]);
00298 }
00299 }
00300 }
00301
00302 template <class T> inline
00303 int
00304 MiniIFFAB<T>::
00305 size(const Box& a_region,
00306 const Interval& a_comps) const
00307 {
00308 CH_assert(isDefined());
00309
00310 Vector<FaceIndex> faces;
00311 getFaceSubset(faces, a_region);
00312
00313
00314 int facesize = linearListSize(faces);
00315
00316
00317 int datasize = 0;
00318 for (int iface = 0; iface < faces.size(); iface++)
00319 {
00320 for (int icomp = a_comps.begin(); icomp <= a_comps.end(); icomp++)
00321 {
00322 const T& dataPt = (*this)(faces[iface], icomp);
00323 int pointsize = CH_XD::linearSize(dataPt);
00324 datasize += pointsize;
00325 }
00326 }
00327
00328 int retval = facesize + datasize;
00329
00330 if (s_verbose)
00331 {
00332 pout() << "###############" << std::endl;
00333 pout() << "MiniIFFAB size() for region " << m_ebgraph.getRegion() << std::endl;
00334 pout() << " a_comps = (" << a_comps.begin() << "," << a_comps.end() << ")," << endl;
00335 pout() << "m_box = " << m_box;
00336 pout() << "size = " << retval << std::endl;
00337 pout() << "###############" << std::endl;
00338 }
00339
00340 return retval;
00341 }
00342
00343 template <class T> inline
00344 void
00345 MiniIFFAB<T>::
00346 linearOut(void* a_buf,
00347 const Box& a_region,
00348 const Interval& a_comps) const
00349 {
00350 CH_assert(isDefined());
00351 Vector<FaceIndex> faces;
00352 getFaceSubset(faces, a_region);
00353
00354
00355 unsigned char* charbuffer = (unsigned char *) a_buf;
00356 linearListOut(charbuffer, faces);
00357 charbuffer += linearListSize(faces);
00358
00359
00360 const MiniIFFAB<T>& thisFAB = *this;
00361 for (int iface = 0; iface < faces.size(); iface++)
00362 {
00363 const FaceIndex& face = faces[iface];
00364 for (int icomp = a_comps.begin(); icomp <= a_comps.end(); icomp++)
00365 {
00366
00367 const T& dataPt = thisFAB(face, icomp);
00368 CH_XD::linearOut(charbuffer, dataPt);
00369
00370 charbuffer += CH_XD::linearSize(dataPt);
00371 }
00372 }
00373 }
00374
00375 template <class T> inline
00376 void
00377 MiniIFFAB<T>::
00378 linearIn(void* a_buf, const Box& a_region, const Interval& a_comps)
00379 {
00380 CH_assert(isDefined());
00381
00382 unsigned char* charbuffer = (unsigned char *) a_buf;
00383 Vector<FaceIndex> faces;
00384 linearListIn(faces, charbuffer);
00385 charbuffer += linearListSize(faces);
00386
00387 for (int iface = 0; iface < faces.size(); iface++)
00388 {
00389 const FaceIndex& face = faces[iface];
00390 for (int icomp = a_comps.begin(); icomp <= a_comps.end(); icomp++)
00391 {
00392
00393 T& dataPt = (*this)(face, icomp);
00394 CH_XD::linearIn(dataPt, charbuffer) ;
00395
00396 charbuffer += CH_XD::linearSize(dataPt);
00397 }
00398 }
00399 }
00400
00401
00402
00403 #include "NamespaceFooter.H"
00404 #endif