#include #include #include #include #include "cctk.h" #include "cctk_Parameters.h" #include "defs.hh" #include "dh.hh" #include "th.hh" #include "vect.hh" #include "gh.hh" using namespace std; using namespace CarpetLib; // Constructors gh:: gh (vector const & reffacts_, centering const refcent_, int const mgfact_, centering const mgcent_, vector > const & baseextents_, i2vect const & boundary_width_) : reffacts(reffacts_), refcent(refcent_), mgfact(mgfact_), mgcent(mgcent_), baseextents(baseextents_), boundary_width(boundary_width_) { assert (reffacts.size() >= 1); assert (all (reffacts.front() == 1)); for (int rl = 1; rl < (int)reffacts.size(); ++ rl) { assert (all (reffacts.AT(rl) >= reffacts.AT(rl-1))); assert (all (reffacts.AT(rl) % reffacts.AT(rl-1) == 0)); } assert (refcent == vertex_centered or refcent == cell_centered); assert (mgfact >= 1); assert (mgcent == vertex_centered or mgcent == cell_centered); assert (baseextents.size() >= 1); assert (baseextents.AT(0).size() >= 1); assert (baseextents.AT(0).size() == reffacts.size()); for (int ml = 1; ml < (int)baseextents.size(); ++ ml) { assert (baseextents.AT(ml).size() == baseextents.AT(ml-1).size()); } for (int ml = 0; ml < (int)baseextents.size(); ++ ml) { for (int rl = 1; rl < (int)baseextents.AT(ml).size(); ++ rl) { ibbox const & cbox = baseextents.AT(ml).AT(rl-1); ibbox const & fbox = baseextents.AT(ml).AT(rl); assert (all (cbox.stride() * reffacts.AT(rl-1) == fbox.stride() * reffacts.AT(rl))); } } assert (all (all (boundary_width >= 0))); for (int ml = 0; ml < (int)baseextents.size(); ++ ml) { for (int rl = 0; rl < (int)baseextents.AT(ml).size(); ++ rl) { ibbox const & box = baseextents.AT(ml).AT(rl); // This condition must hold even for zero-sized grid arrays assert (all (box.shape() / box.stride() >= boundary_width[0] + boundary_width[1])); } } } // Destructors gh:: ~gh () { } // Modifiers void gh:: regrid (rregs const & superregs, mregs const & regs) { DECLARE_CCTK_PARAMETERS; superregions = superregs; // Save the grid hierarchy oldregions.clear (); swap (oldregions, regions); regions = regs; // Consistency checks // Note: there may be zero refinement levels // Check multigrid consistency assert (mglevels()>0); for (int ml=1; ml=0); for (int c=0; c0) { assert (components(rl-1)>0); assert (all (extent(ml,rl,0).stride() * reffacts.AT(rl) == extent(ml,rl-1,0).stride() * reffacts.AT(rl-1))); // Check contained-ness: // first take all coarse grids ibset coarse; for (int c=0; c::iterator t=ths.begin(); t!=ths.end(); ++t) { (*t)->regrid(); } for (list::iterator d=dhs.begin(); d!=dhs.end(); ++d) { (*d)->regrid(); } } bool gh:: recompose (int const rl, bool const do_prolongate) { // Handle changes in number of mglevels if (oldregions.size() != regions.size()) { oldregions.resize (regions.size()); } bool const do_recompose = level_did_change(rl); if (do_recompose) { // Recompose the other hierarchies for (list::iterator d=dhs.begin(); d!=dhs.end(); ++d) { (*d)->recompose (rl, do_prolongate); } // Overwrite old with new grid hierarchy for (int ml=0; ml=0 and rl= (int)oldregions.AT(ml).size()) return true; if (regions.AT(ml).AT(rl).size() != oldregions.AT(ml).AT(rl).size()) { return true; } for (int c=0; c=0 and ml=0 and minrl<=maxrl and maxrl<=reflevels()); // Try finer levels first for (rl = maxrl-1; rl >= minrl; --rl) { // Align (round) the position to the nearest existing grid point // on this refinement level ivect const str = baseextent(ml,rl).stride(); aligned_ipos = ivect(floor(rpos / rvect(str) + rvect(0.5))) * str; gh::cregs const & regs = superregions.AT(rl); // Search all superregions linearly. Each superregion corresponds // to a "refined region", and the number of superregions is thus // presumably independent of the number of processors. for (size_t r = 0; r < regs.size(); ++r) { region_t const & reg = regs.AT(r); if (reg.extent.contains(aligned_ipos)) { // We found the superregion to which this grid point belongs // Search the superregion hierarchically pseudoregion_t const * const preg = reg.processors->search(aligned_ipos); assert (preg); // We now know the refinement level, component, and index to // which this grid point belongs c = preg->component; return; } } } // for rl // The point does not belong to any component on any refinement // level rl = -1; c = -1; } void gh:: locate_position (ivect const & ipos, int const ml, int const minrl, int const maxrl, int & rl, int & c, ivect & aligned_ipos) const { assert (ml>=0 and ml=0 and minrl<=maxrl and maxrl<=reflevels()); // Try finer levels first for (rl = maxrl-1; rl >= minrl; --rl) { // Align (round) the position to the nearest existing grid point // on this refinement level ivect const str = baseextent(ml, rl).stride(); aligned_ipos = ivect(floor(rvect(ipos) / rvect(str) + rvect(0.5))) * str; gh::cregs const & regs = superregions.AT(rl); // Search all superregions linearly. Each superregion corresponds // to a "refined region", and the number of superregions is thus // presumably independent of the number of processors. for (size_t r = 0; r < regs.size(); ++r) { region_t const & reg = regs.AT(r); if (reg.extent.contains(aligned_ipos)) { // We found the superregion to which this grid point belongs // Search the superregion hierarchically pseudoregion_t const * const preg = reg.processors->search(aligned_ipos); assert (preg); // We now know the refinement level, component, and index to // which this grid point belongs c = preg->component; return; } } } // for rl // The point does not belong to any component on any refinement // level rl = -1; c = -1; } // Time hierarchy management void gh:: add (th * const t) { ths.push_back (t); } void gh:: remove (th * const t) { ths.remove (t); } // Data hierarchy management void gh:: add (dh * const d) { dhs.push_back (d); } void gh:: remove (dh * const d) { dhs.remove (d); } // Memory usage size_t gh:: memory () const { return memoryof (reffacts) + memoryof (refcent) + memoryof (mgfact) + memoryof (mgcent) + memoryof (baseextents) + memoryof (boundary_width) + memoryof (regions) + memoryof (oldregions) + memoryof (ths) + memoryof (dhs); } // Output void gh:: do_output_bboxes (ostream & os) const { for (int ml=0; ml::const_iterator d = dhs.begin(); d != dhs.end(); ++ d, isfirst = false) { if (not isfirst) os << ","; os << *d; } } os << "}"; return os; }