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


Main Page   Class Hierarchy   Compound List   File List  

GridData1.c

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


Quickstart     Users Guide     Programmers Reference     Installation      Examples     Download



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