AMROC Main     Blockstructured Adaptive Mesh Refinement in object-oriented C++


Main Page   Class Hierarchy   Compound List   File List  

GridData3.c

Go to the documentation of this file.
00001 #ifndef _included_GridData_3_c
00002 #define _included_GridData_3_c
00003 
00009 template <class Type>
00010 GridData(3)<Type>::GridData(3)()
00011   : _bbox(),
00012     _extents(0,0),
00013     _step(3,1),
00014     _size(0),
00015     _bottom(0),
00016     _data((Type *) NULL) {}
00017 
00018 template <class Type>
00019 GridData(3)<Type>::GridData(3)(BBox const &bb)
00020   : _bbox(bb),
00021     _extents(bb.extents()),
00022     _step(bb.stepsize()),
00023     _size(bb.size()),
00024     _bottom(bb.bottom()),
00025     _data(_size ? new Type[_size] : ((Type *) NULL)) 
00026     {
00027 #ifdef DEBUG_PRINT_GD
00028     assert (_data);
00029     fill(9999);
00030     ( comm_service::log() << "Data Check"
00031                           << _bbox 
00032                           << ": " << _data[_size-1]
00033                           << endl ).flush();
00034 #endif
00035 #ifdef DEBUG_PRINT_GD_MEMORY
00036     DAGHMemoryTrace::alloc(sizeof(Type)*_size);
00037 #endif
00038     }
00039 
00040 template <class Type>
00041 GridData(3)<Type>::GridData(3) (int const i, int const j, int const k,
00042                                 int const ii, int const jj, int const kk)
00043   : _bbox(3,i,j,k,ii,jj,kk,1),
00044     _extents(_bbox.extents()),
00045     _step(_bbox.stepsize()),
00046     _size(_bbox.size()),
00047     _bottom(_bbox.bottom()),
00048     _data(_size ? new Type[_size] : ((Type *) NULL)) 
00049     {
00050 #ifdef DEBUG_PRINT_GD
00051     assert (_data);
00052     fill(9999);
00053     ( comm_service::log() << "Data Check"
00054                           << _bbox 
00055                           << ": " << _data[_size-1]
00056                           << endl ).flush();
00057 #endif
00058 #ifdef DEBUG_PRINT_GD_MEMORY
00059     DAGHMemoryTrace::alloc(sizeof(Type)*_size);
00060 #endif
00061     }
00062 
00063 template <class Type>
00064 GridData(3)<Type>::GridData(3) (int const i, int const j, int const k,
00065                                 int const ii, int const jj, int const kk, int const s)
00066   : _bbox(3,i,j,k,ii,jj,kk,s),
00067     _extents(_bbox.extents()),
00068     _step(_bbox.stepsize()),
00069     _size(_bbox.size()),
00070     _bottom(_bbox.bottom()),
00071     _data(_size ? new Type[_size] : ((Type *) NULL)) 
00072     {
00073 #ifdef DEBUG_PRINT_GD
00074     assert (_data);
00075     fill(9999);
00076     ( comm_service::log() << "Data Check"
00077                           << _bbox 
00078                           << ": " << _data[_size-1]
00079                           << endl ).flush();
00080 #endif
00081 #ifdef DEBUG_PRINT_GD_MEMORY
00082     DAGHMemoryTrace::alloc(sizeof(Type)*_size);
00083 #endif
00084     }
00085 
00086 template <class Type>
00087 GridData(3)<Type>::GridData(3) (int const i, int const j, int const k,
00088                                 int const ii, int const jj, int const kk, 
00089                                 int const s, int const ss, int const sss)
00090   : _bbox(3,i,j,k,ii,jj,kk,s,ss,sss),
00091     _extents(_bbox.extents()),
00092     _step(_bbox.stepsize()),
00093     _size(_bbox.size()),
00094     _bottom(_bbox.bottom()),
00095     _data(_size ? new Type[_size] : ((Type *) NULL)) 
00096     {
00097 #ifdef DEBUG_PRINT_GD
00098     assert (_data);
00099     fill(9999);
00100     ( comm_service::log() << "Data Check"
00101                           << _bbox 
00102                           << ": " << _data[_size-1]
00103                           << endl ).flush();
00104 #endif
00105 #ifdef DEBUG_PRINT_GD_MEMORY
00106     DAGHMemoryTrace::alloc(sizeof(Type)*_size);
00107 #endif
00108     }
00109 
00110 template <class Type>
00111 GridData(3)<Type>::GridData(3)(BBox const &bb, Type *databuf)
00112   : _bbox(bb),
00113     _extents(bb.extents()),
00114     _step(bb.stepsize()),
00115     _size(bb.size()),
00116     _bottom(bb.bottom()),
00117     _data(databuf)
00118     {
00119 #ifdef DEBUG_PRINT_GD
00120     assert (_data);
00121     fill(9999);
00122     ( comm_service::log() << "Data Check"
00123                           << _bbox 
00124                           << ": " << _data[_size-1]
00125                           << endl ).flush();
00126 #endif
00127     }
00128 
00129 
00130 template <class Type>
00131 GridData(3)<Type>::GridData(3)(GridDataBucket<Type> &gdbkt)
00132   : _bbox(gdbkt.bbox()),
00133     _extents((gdbkt.bbox()).extents()),
00134     _step((gdbkt.bbox()).stepsize()),
00135     _size((gdbkt.bbox()).size()),
00136     _bottom((gdbkt.bbox()).bottom()),
00137     _data(gdbkt.data())
00138     {
00139 #ifdef DEBUG_PRINT_GD
00140     assert (_data);
00141     fill(9999);
00142     ( comm_service::log() << "Data Check"
00143                           << _bbox 
00144                           << ": " << _data[_size-1]
00145                           << endl ).flush();
00146 #endif
00147     }
00148 
00149 template <class Type>
00150 GridData(3)<Type>::GridData(3)(GridDataBucket<Type> &gdbkt, int const n)
00151   : _bbox(gdbkt.bbox(n)),
00152     _extents((gdbkt.bbox(n)).extents()),
00153     _step((gdbkt.bbox(n)).stepsize()),
00154     _size((gdbkt.bbox(n)).size()),
00155     _bottom((gdbkt.bbox(n)).bottom()),
00156     _data(gdbkt.data(n))
00157     {
00158 #ifdef DEBUG_PRINT_GD
00159     assert (_data);
00160     fill(9999);
00161     ( comm_service::log() << "Data Check"
00162                           << _bbox 
00163                           << ": " << _data[_size-1]
00164                           << endl ).flush();
00165 #endif
00166     }
00167 
00168 template <class Type>
00169 GridData(3)<Type>::GridData(3)(GridData(3)<Type> const &other)
00170   : _bbox(other._bbox),
00171     _extents(other._extents),
00172     _step(other._step),
00173     _size(other._size),
00174     _bottom(other._bottom),
00175     _data(_size ? new Type[_size] : ((Type *) NULL))
00176     {
00177 #ifdef DEBUG_PRINT_GD
00178     assert (_data);
00179     fill(9999);
00180     ( comm_service::log() << "Data Check"
00181                           << _bbox
00182                           << ": " << _data[_size-1]
00183                           << endl ).flush();
00184 #endif
00185 #ifdef DEBUG_PRINT_GD_MEMORY
00186     DAGHMemoryTrace::alloc(sizeof(Type)*_size);
00187 #endif
00188     }
00189 
00190 template <class Type>
00191 void GridData(3)<Type>::allocate(BBox const &bb)
00192   {
00193    _bbox = bb;
00194    _extents = _bbox.extents();
00195    _step = _bbox.stepsize();
00196    _size = _bbox.size();
00197    _bottom = _bbox.bottom();
00198    _data = _size ? new Type[_size] : (Type *) NULL;
00199 #ifdef DEBUG_PRINT_GD
00200     assert (_data);
00201     fill(9999);
00202     ( comm_service::log() << "Data Check"
00203                           << _bbox 
00204                           << ": " << _data[_size-1]
00205                           << endl ).flush();
00206 #endif
00207 #ifdef DEBUG_PRINT_GD_MEMORY
00208     DAGHMemoryTrace::alloc(sizeof(Type)*_size);
00209 #endif
00210   }
00211 
00212 template <class Type>
00213 void GridData(3)<Type>::allocate(BBox const &bb, Type *databuf)
00214   {
00215    _bbox = bb;
00216    _extents = _bbox.extents();
00217    _step = _bbox.stepsize();
00218    _size = _bbox.size();
00219    _bottom = _bbox.bottom();
00220 #ifdef DEBUG_PRINT_GD
00221    assert(!_data);
00222 #endif
00223    _data = databuf;
00224 #ifdef DEBUG_PRINT_GD
00225     assert (_data);
00226     fill(9999);
00227     ( comm_service::log() << "Data Check"
00228                           << _bbox 
00229                           << ": " << _data[_size-1]
00230                           << endl ).flush();
00231 #endif
00232   }
00233 
00234 template <class Type>
00235 void GridData(3)<Type>::fill(Type const &val)
00236   { register int i; for (i = 0; i < _size; i++) _data[i] = val; }
00237 
00238 template <class Type>
00239 void GridData(3)<Type>::copy(GridData(3)<Type> const &gd)
00240   {
00241    if (&gd != this)
00242      {
00243       BBox intersection = _bbox * gd._bbox;
00244       if (!intersection.empty())
00245         { 
00246          Coords max_step = _step.getmax(gd._step);
00247          BBox to(intersection); to.setstepsize(_step); 
00248          BBox from(intersection); from.setstepsize(gd._step);
00249          gd_CopyRegion(gd, to, from, max_step); 
00250         }
00251      }
00252   }
00253 
00254 template <class Type>
00255 void GridData(3)<Type>::copy(GridData(3)<Type> const &gd, BBox const &where)
00256   {
00257    if (&gd != this)
00258      {
00259       BBox intersection = _bbox * gd._bbox * where;
00260       if (!intersection.empty())
00261         { 
00262          Coords max_step = _step.getmax(gd._step);
00263          BBox to(intersection); to.setstepsize(_step); 
00264          BBox from(intersection); from.setstepsize(gd._step);
00265          gd_CopyRegion(gd, to, from, max_step); 
00266         }
00267      }
00268   }
00269 
00270 template <class Type>
00271 void GridData(3)<Type>::copy(GridData(3)<Type> const &gd, BBox const &to,
00272                                                 BBox const &from)
00273   {
00274    const Coords  toshift = from.lower() - to.lower();
00275    BBox newfrom = gd._bbox * from * shiftabs(_bbox * to, toshift);
00276    if (!newfrom.empty())
00277      {
00278       BBox newto = shiftabs(newfrom, -toshift);
00279       Coords max_step = _step.getmax(gd._step);
00280       newto.setstepsize(_step);
00281       newfrom.setstepsize(gd._step);
00282       gd_CopyRegion(gd, newto, newfrom, max_step);
00283      }
00284   }
00285 
00286 template <class Type>
00287 void GridData(3)<Type>::copy(GridDataBucket<Type> const &gdbkt)
00288   {
00289    BBox const &gdbktbb = gdbkt.bbox();
00290    BBox intersection = _bbox * gdbktbb;
00291    if (!intersection.empty())
00292      { 
00293       Coords max_step = _step.getmax(gdbktbb.stepsize());
00294       BBox to(intersection); to.setstepsize(_step); 
00295       BBox from(intersection); from.setstepsize(gdbktbb.stepsize());
00296       gdb_CopyRegion(gdbkt, to, from, max_step); 
00297      }
00298   }
00299 
00300 template <class Type>
00301 void GridData(3)<Type>::copy(GridDataBucket<Type> const &gdbkt, BBox const &where)
00302   {
00303    BBox const &gdbktbb = gdbkt.bbox();
00304    BBox intersection = _bbox * gdbktbb * where;
00305    if (!intersection.empty())
00306     { 
00307      Coords max_step = _step.getmax(gdbktbb.stepsize());
00308      BBox to(intersection); to.setstepsize(_step); 
00309      BBox from(intersection); from.setstepsize(gdbktbb.stepsize());
00310      gdb_CopyRegion(gdbkt, to, from, max_step); 
00311     }
00312   }
00313 
00314 template <class Type>
00315 void GridData(3)<Type>::copy(GridDataBucket<Type> const &gdbkt, 
00316                                 BBox const &to, BBox const &from)
00317   {
00318    BBox const &gdbktbb = gdbkt.bbox();
00319    const Coords  toshift = from.lower() - to.lower();
00320    BBox newfrom = gdbktbb * from * shiftabs(_bbox * to, toshift);
00321    if (!newfrom.empty())
00322      {
00323       BBox newto = shiftabs(newfrom, -toshift);
00324       Coords max_step = _step.getmax(gdbktbb.stepsize());
00325       newto.setstepsize(_step);
00326       newfrom.setstepsize(gdbktbb.stepsize());
00327       gdb_CopyRegion(gdbkt, newto, newfrom, max_step);
00328      }
00329   }
00330 
00331 template <class Type>
00332 void GridData(3)<Type>::copy(GridDataBucket<Type> const &gdbkt, int const n)
00333   {
00334    BBox const &gdbktbb = gdbkt.bbox(n);
00335    BBox intersection = _bbox * gdbktbb;
00336    if (!intersection.empty())
00337      { 
00338       Coords max_step = _step.getmax(gdbktbb.stepsize());
00339       BBox to(intersection); to.setstepsize(_step); 
00340       BBox from(intersection); from.setstepsize(gdbktbb.stepsize());
00341       gdb_CopyRegion(gdbkt, n, to, from, max_step); 
00342      }
00343   }
00344 
00345 template <class Type>
00346 void GridData(3)<Type>::copy(GridDataBucket<Type> const &gdbkt, int const n,
00347                                                 BBox const &where)
00348   {
00349    BBox const &gdbktbb = gdbkt.bbox(n);
00350    BBox intersection = _bbox * gdbktbb * where;
00351    if (!intersection.empty())
00352     { 
00353      Coords max_step = _step.getmax(gdbktbb.stepsize());
00354      BBox to(intersection); to.setstepsize(_step); 
00355      BBox from(intersection); from.setstepsize(gdbktbb.stepsize());
00356      gdb_CopyRegion(gdbkt, n, to, from, max_step); 
00357     }
00358   }
00359 
00360 template <class Type>
00361 void GridData(3)<Type>::copy(GridDataBucket<Type> const &gdbkt, int const n,
00362                                 BBox const &to, BBox const &from)
00363   {
00364    BBox const &gdbktbb = gdbkt.bbox(n);
00365    const Coords  toshift = from.lower() - to.lower();
00366    BBox newfrom = gdbktbb * from * shiftabs(_bbox * to, toshift);
00367    if (!newfrom.empty())
00368      {
00369       BBox newto = shiftabs(newfrom, -toshift);
00370       Coords max_step = _step.getmax(gdbktbb.stepsize());
00371       newto.setstepsize(_step);
00372       newfrom.setstepsize(gdbktbb.stepsize());
00373       gdb_CopyRegion(gdbkt, n, newto, newfrom, max_step);
00374      }
00375   }
00376 
00377 template <class Type>
00378 void GridData(3)<Type>::gd_CopyRegion(GridData(3)<Type> const &src, 
00379                         BBox const &to, BBox const &from, Coords const &step)
00380   {
00381    GridData(3)<Type> &dst = *this;
00382 
00383    if (dst._bbox==src._bbox && to==from && to==dst._bbox) 
00384      memcpy((void *) dst._data, (void *) src._data, sizeof(Type)*to.size());
00385    else {
00386      const int di = from.lower(0)-to.lower(0);
00387      const int dj = from.lower(1)-to.lower(1);
00388      const int dk = from.lower(2)-to.lower(2);
00389 
00390      BeginFastIndex3(src, src._bbox, src._data, const Type);
00391      BeginFastIndex3(dst, dst._bbox, dst._data, Type);
00392 
00393      for_3(i, j, k, to, step)
00394        FastIndex3(dst,i,j,k) = FastIndex3(src,i+di,j+dj,k+dk);
00395      end_for
00396 
00397      EndFastIndex3(dst);
00398      EndFastIndex3(src);
00399    }
00400   }
00401 
00402 template <class Type>
00403 void GridData(3)<Type>::gdb_CopyRegion(GridDataBucket<Type> const &gdbkt, 
00404                         BBox const &to, BBox const &from, Coords const &step)
00405   {
00406    GridData(3)<Type> &dst = *this;
00407 
00408    if (dst._bbox==gdbkt.bbox() && to==from && to==dst._bbox) 
00409      memcpy((void *) dst._data, (void *) gdbkt.data(), sizeof(Type)*to.size());
00410    else {
00411      const int di = from.lower(0)-to.lower(0);
00412      const int dj = from.lower(1)-to.lower(1);
00413      const int dk = from.lower(2)-to.lower(2);
00414 
00415      BeginFastIndex3(src, gdbkt.bbox(), gdbkt.data(), const Type);
00416      BeginFastIndex3(dst, dst.bbox(), dst.data(), Type);
00417      for_3(i, j, k, to, step)
00418        FastIndex3(dst,i,j,k) = FastIndex3(src,i+di,j+dj,k+dk);
00419      end_for
00420      EndFastIndex3(dst);
00421      EndFastIndex3(src);
00422    }
00423   }
00424 
00425 template <class Type>
00426 void GridData(3)<Type>::gdb_CopyRegion(GridDataBucket<Type> const &gdbkt, int const n,
00427                         BBox const &to, BBox const &from, Coords const &step)
00428   {
00429    GridData(3)<Type> &dst = *this;
00430 
00431    if (dst._bbox==gdbkt.bbox(n) && to==from && to==dst._bbox) 
00432      memcpy((void *) dst._data, (void *) gdbkt.data(n), sizeof(Type)*to.size());
00433    else {
00434      const int di = from.lower(0)-to.lower(0);
00435      const int dj = from.lower(1)-to.lower(1);
00436      const int dk = from.lower(2)-to.lower(2);
00437 
00438      BeginFastIndex3(src, gdbkt.bbox(n), gdbkt.data(n), const Type);
00439      BeginFastIndex3(dst, dst.bbox(), dst.data(), Type);
00440      for_3(i, j, k, to, step)
00441        FastIndex3(dst,i,j,k) = FastIndex3(src,i+di,j+dj,k+dk);
00442      end_for
00443      EndFastIndex3(dst);
00444      EndFastIndex3(src);
00445    }
00446   }
00447 
00448 template <class Type>
00449 void GridData(3)<Type>::PackRegion(Type *sendbuf, BBox const &from) const
00450   {
00451    GridData(3)<Type> const &src = *this;
00452 
00453    if (from==src._bbox) 
00454      memcpy((void *) sendbuf, (void *) src._data, sizeof(Type)*from.size());
00455    else {
00456      register int p = 0;
00457 
00458      BeginFastIndex3(src, src._bbox, src._data, const Type);
00459 
00460      for_3(i, j, k, from, _step)
00461        sendbuf[p++] = FastIndex3(src,i,j,k);
00462      end_for
00463 
00464      EndFastIndex3(src);
00465    }
00466   }
00467 
00468 template <class Type>
00469 void GridData(3)<Type>::UnPackRegion(Type const *recvbuf, BBox const &to)
00470   {
00471    GridData(3)<Type> &dst = *this;
00472 
00473    if (to==dst._bbox) 
00474      memcpy((void *) dst._data, (void *) recvbuf, sizeof(Type)*to.size());
00475    else {
00476      register int p = 0;
00477 
00478      BeginFastIndex3(dst, dst._bbox, dst._data, Type);
00479 
00480      for_3(i, j, k, to, _step)
00481        FastIndex3(dst,i,j,k) = recvbuf[p++];
00482      end_for
00483 
00484      EndFastIndex3(dst);
00485    }
00486   }
00487 
00488 template <class Type>
00489 ostream&  operator << (ostream& os, const GridData(3)<Type> &gd)
00490   {
00491    if (&gd == (GridData(3)<Type> *)NULL) return os;
00492 
00493    os << "BBox: " << gd.bbox() << " " << "Extents: " << gd.extents() << " ";
00494    os << "Step: " << gd.stepsize() << " ";
00495    os << "Size: " << gd.size() << " " << "Bottom: " << gd.bottom() << " ";
00496 
00497    os << "\n";
00498   
00499    if (gd.bbox().empty()) return os;
00500 
00501    const Coords &l = gd.lower();
00502    const Coords &u = gd.upper();
00503    const Coords &step = gd.stepsize();
00504    for (register int k=l(2);k<=u(2);k+=step(2)) {
00505    for (register int j=l(1);j<=u(1);j+=step(1)) {
00506    for (register int i=l(0);i<=u(0);i+=step(0)) 
00507      { os << "[" << i << "," << j << "," << k << "]=" << gd(i, j, k) << " "; }
00508    os << "\n"; } os << "\n";}
00509 
00510    return os;
00511   }
00512 
00513 template <class Type>
00514 ofstream&  operator << (ofstream& ofs, const GridData(3)<Type> &gd)
00515   {
00516    if (&gd == (GridData(3)<Type> *)NULL) return ofs;
00517 
00518    ofs.write((char*)&gd.bbox(),sizeof(BBox));
00519    ofs.write((char*)gd.data(),gd.size()*sizeof(Type));
00520 
00521    return ofs;
00522   }
00523 
00524 template <class Type>
00525 ifstream&  operator >> (ifstream& ifs, GridData(3)<Type> &gd)
00526   {
00527    if (&gd == (GridData(3)<Type> *)NULL) return ifs;
00528 
00529    BBox bb;
00530    ifs.read((char*)&bb,sizeof(BBox));
00531 
00532    if (!gd.ok_to_index())
00533      { gd.allocate(bb); }
00534 
00535    if (bb == gd.bbox()) {
00536      ifs.read((char*)gd.data(),gd.size()*sizeof(Type));
00537    }
00538    else {
00539      GridData(3)<Type> tmpgd(bb);
00540      ifs.read((char*)tmpgd.data(),tmpgd.size()*sizeof(Type));
00541      gd.copy(tmpgd);  
00542    } 
00543 
00544    return ifs;
00545   }
00546 
00547 template <class Type>
00548 strstream&  operator << (strstream& ofs, const GridData(3)<Type> &gd)
00549   {
00550    if (&gd == (GridData(3)<Type> *)NULL) return ofs;
00551 
00552    ofs.write((char*)&gd.bbox(),sizeof(BBox));
00553    ofs.write((char*)gd.data(),gd.size()*sizeof(Type));
00554 
00555    return ofs;
00556   }
00557 
00558 template <class Type>
00559 strstream&  operator >> (strstream& ifs, GridData(3)<Type> &gd)
00560   {
00561    if (&gd == (GridData(3)<Type> *)NULL) return ifs;
00562 
00563    BBox bb;
00564    ifs.read((char*)&bb,sizeof(BBox));
00565 
00566    if (!gd.ok_to_index())
00567      { gd.allocate(bb); }
00568 
00569    if (bb == gd.bbox()) {
00570      ifs.read((char*)gd.data(),gd.size()*sizeof(Type));
00571    }
00572    else {
00573      GridData(3)<Type> tmpgd(bb);
00574      ifs.read((char*)tmpgd.data(),tmpgd.size()*sizeof(Type));
00575      gd.copy(tmpgd);  
00576    } 
00577 
00578    return ifs;
00579   }
00580 
00581   #include "GridDataOps3.h"
00582   #include "GridDataOpsRel3.h"
00583   #include "GridDataOpsRed3.h"
00584 
00585 #endif


Quickstart     Users Guide     Programmers Reference     Installation      Examples     Download



AMROC Main      Home      Contact
last update: 06/01/04