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


Main Page   Class Hierarchy   Compound List   File List  

GridData2.c

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


Quickstart     Users Guide     Programmers Reference     Installation      Examples     Download



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