00001 #ifdef CH_LANG_CC
00002
00003
00004
00005
00006
00007
00008
00009 #endif
00010
00011 #ifndef _MINIIVFABI_H_
00012 #define _MINIIVFABI_H_
00013 #include "MayDay.H"
00014 #include "IntVectSet.H"
00015 #include "VoFIterator.H"
00016 #include "parstream.H"
00017 #include "SPMD.H"
00018 #include "DebugOut.H"
00019 #include "EBDebugOut.H"
00020 #include "SPMD.H"
00021 #include "NamespaceHeader.H"
00022
00023
00024
00025 template <class T> inline
00026 MiniIVFAB<T>::MiniIVFAB():m_nComp(0),m_data(0)
00027 {
00028
00029 }
00030
00031 template <class T> inline
00032 MiniIVFAB<T>::~MiniIVFAB()
00033 {
00034
00035 }
00036
00037 template <class T> inline
00038 MiniIVFAB<T>::MiniIVFAB(const Interval& a_comps,
00039 MiniIVFAB<T>& a_original)
00040 :m_vofs(a_original.m_vofs),
00041 m_Memory(a_original.m_Memory),
00042 m_nComp(a_comps.size()),
00043 m_data(a_original.dataPtr(a_comps.begin())){ ; }
00044
00045
00046 template <class T> inline
00047 MiniIVFAB<T>::MiniIVFAB(const IntVectSet& a_ivsin,
00048 const EBGraph& a_ebgraph,
00049 const int& a_nvarin)
00050 {
00051 define(a_ivsin, a_ebgraph, a_nvarin);
00052 }
00053
00054 template <class T> inline
00055 void
00056 MiniIVFAB<T>::define(const IntVectSet& a_ivsin,
00057 const EBGraph& a_ebGraph,
00058 const int& a_nvarin)
00059 {
00060
00061 CH_assert(a_nvarin > 0);
00062
00063 m_nComp=a_nvarin;
00064
00065 if (!a_ivsin.isEmpty())
00066 {
00067 int nVoFs = 0;
00068
00069 IVSIterator ivsit(a_ivsin);
00070 for (ivsit.reset(); ivsit.ok(); ++ivsit)
00071 {
00072 nVoFs += a_ebGraph.numVoFs(ivsit());
00073 }
00074
00075
00076
00077 if (nVoFs > 0)
00078 {
00079 if(!m_vofs)
00080 {
00081 m_vofs=shared_ptr<Vector<VolIndex> >(new Vector<VolIndex>(nVoFs));
00082 m_Memory= shared_ptr<Vector<T> >(new Vector<T>(nVoFs*a_nvarin));
00083 m_data = &(m_Memory->operator[](0));
00084
00085 VoFIterator vofit(a_ivsin, a_ebGraph);
00086 *m_vofs = vofit.getVector();
00087 }
00088 }
00089 else
00090 {
00091 m_vofs.reset();
00092 }
00093 }
00094 }
00095
00096 template <class T> inline
00097 void
00098 MiniIVFAB<T>::copy(const Box& a_fromBox,
00099 const Interval& a_dstInterval,
00100 const Box& a_toBox,
00101 const MiniIVFAB<T>& a_src,
00102 const Interval& a_srcInterval)
00103 {
00104 T* to=NULL;
00105 if(m_vofs)
00106 {
00107 to = getIndex((*m_vofs)[0],a_dstInterval.begin());
00108 }
00109 for(unsigned int i=0; i<m_vofs->size(); i++, to++)
00110 {
00111 const VolIndex& vof = (*m_vofs)[i];
00112 if(a_fromBox.contains(vof.gridIndex()))
00113 {
00114 const T* from= a_src.getIndex(vof,a_srcInterval.begin());
00115
00116 for(unsigned int c=0; c<a_srcInterval.size(); c++)
00117 {
00118 to[c*m_vofs->size()]=from[c*a_src.m_vofs->size()];
00119 }
00120 }
00121 }
00122 }
00123
00124
00125
00126
00127 template <class T> inline
00128 int MiniIVFAB<T>::size(const Box& a_region,
00129 const Interval& a_comps) const
00130 {
00131 int count = 0;
00132 T tmp;
00133
00134 if(m_vofs){
00135 for (unsigned int i=0; i<m_vofs->size(); i++)
00136 {
00137 if (a_region.contains((*m_vofs)[i].gridIndex())) count++;
00138 }
00139 }
00140 if (count > 0)
00141 {
00142 return sizeof(int) + count*CH_XD::linearSize((*m_vofs)[0]) + count*a_comps.size()*CH_XD::linearSize(tmp);
00143 }
00144 return sizeof(int);
00145 }
00146
00147 template <class T> inline
00148 void MiniIVFAB<T>::linearOut(void* a_buf,
00149 const Box& a_region,
00150 const Interval& a_comps) const
00151 {
00152 char* buffer = (char*)a_buf;
00153 buffer += sizeof(int);
00154 int count = 0;
00155 if (m_vofs)
00156 {
00157 const T* ptr = &(this->m_data[0]);
00158 for (unsigned int i=0; i<m_vofs->size(); i++, ptr++)
00159 {
00160 const VolIndex& v = (*m_vofs)[i];
00161 if (a_region.contains(v.gridIndex()))
00162 {
00163 count++;
00164 CH_XD::linearOut(buffer, v);
00165 buffer+= CH_XD::linearSize(v);
00166 for (int c=a_comps.begin(); c<=a_comps.end(); c++)
00167 {
00168 CH_XD::linearOut(buffer, *(ptr+c*(m_vofs->size())));
00169 buffer += CH_XD::linearSize(*ptr);
00170 }
00171 }
00172 }
00173 }
00174 int* b = (int*)a_buf;
00175 *b = count;
00176 }
00177
00178 template <class T> inline
00179 void MiniIVFAB<T>::linearIn(void* a_buf, const Box& a_region, const Interval& a_comps)
00180 {
00181 int* b = (int*)a_buf;
00182 int count = *b;
00183 char* buffer = (char*)a_buf;
00184 buffer += sizeof(int);
00185 for (int i=0; i<count; i++)
00186 {
00187 VolIndex v;
00188 CH_XD::linearIn(v, buffer);
00189 buffer += linearSize(v);
00190 for (int c=a_comps.begin(); c<=a_comps.end(); c++)
00191 {
00192 T* ptr = getIndex(v, c);
00193 CH_XD::linearIn(*ptr, buffer);
00194 buffer+=CH_XD::linearSize(*ptr);
00195 }
00196 }
00197 }
00198
00199 template <class T> inline
00200 T*
00201 MiniIVFAB<T>::getIndex(const VolIndex& a_vof, const int& a_comp) const
00202 {
00203
00204 CH_assert((a_comp >= 0) && (a_comp < this->m_nComp));
00205
00206 T* dataPtr = (T*)&(this->m_data[0]);
00207 for (unsigned int i=0; i<m_vofs->size(); ++i)
00208 {
00209 if (a_vof == (*m_vofs)[i]) break;
00210 dataPtr++;
00211 }
00212 dataPtr += a_comp*m_vofs->size();
00213 return dataPtr;
00214 }
00215
00216
00217 template <class T> inline
00218 void MiniIVFAB<T>::setVal(const T& a_val)
00219 {
00220 if(m_vofs)
00221 for(int i=0; i<m_vofs->size()*m_nComp; i++) m_data[i]=a_val;
00222 }
00223
00224 template <class T> inline
00225 void MiniIVFAB<T>::setVal(int a_comp, const T& a_val)
00226 {
00227 if(m_vofs)
00228 for(int i=a_comp*m_nComp; i<m_vofs->size()*(a_comp+1)*m_nComp; i++) m_data[i]=a_val;
00229 }
00230
00231
00232 template <class T> inline
00233 void MiniIVFAB<T>::setVal(const T& a_val, const Box& a_region, int a_startcomp, int a_ncomp)
00234 {
00235 if(m_vofs)
00236 forall(*this, a_region, a_startcomp, a_startcomp, a_ncomp, false, [a_val](T& d, const T& s){d=a_val;});
00237 }
00238
00239
00240
00241
00242 template <class T> inline
00243 int MiniIVFAB<T>::numVoFs() const
00244 {
00245 if(m_vofs)
00246 return m_vofs->size();
00247 return 0;
00248 }
00249
00250 template <class T> inline
00251 Vector<VolIndex>
00252 MiniIVFAB<T>::getVoFs() const
00253 {
00254
00255 if(m_vofs)
00256 {
00257 return *m_vofs;
00258 }
00259 else
00260 {
00261 return Vector<VolIndex>();
00262 }
00263 }
00264
00265 template <class T>
00266 template <typename F>
00267 void MiniIVFAB<T>::forall(const MiniIVFAB<T>& a_src, const Box& a_box, int a_srccomp, int a_destcomp,
00268 int a_numcomp, bool sameRegBox, const F& func)
00269 {
00270 if(!m_vofs) return;
00271 if (sameRegBox)
00272 {
00273 Real* l = dataPtr(a_destcomp);
00274 const Real* r = a_src.dataPtr(a_srccomp);
00275 int nvof = m_vofs->size();
00276 for (int i=0; i<a_numcomp*nvof; i++)
00277 {
00278 func(l[i], r[i]);
00279 }
00280 }
00281 else
00282 {
00283 const Vector<VolIndex>& vofs = *m_vofs;
00284 for(int i=0; i<vofs.size(); i++)
00285 {
00286 const VolIndex& vof=vofs[i];
00287 if(a_box.contains(vof.gridIndex()))
00288 {
00289 for (int icomp = 0; icomp < a_numcomp; ++icomp)
00290 {
00291 func(this->operator()(vof, a_destcomp+icomp), a_src(vof, a_srccomp+icomp));
00292 }
00293 }
00294 }
00295 }
00296 }
00297
00298
00299 #include "NamespaceFooter.H"
00300 #endif
00301