/** * @file grid_index_translations.cpp * @author Julian Iseringhausen * @date Tue May 17 11:46:37 2011 * * @brief Class to convert different representations of grid * indices. * */ #ifdef HAVE_CONFIG_H #include #endif #include #include "base/helper.hpp" #include "grid/grid_index_translations.hpp" #include "grid/grid.hpp" #include "grid/multigrid.hpp" using namespace VMG; Index GridIndexTranslations::GlobalToLocal(const Index& index_global) const { return index_global - Father()->Global().BeginLocal() + Father()->Local().Begin(); } Index GridIndexTranslations::GlobalToFiner(const Index& index_global) { return 2 * index_global; } Index GridIndexTranslations::GlobalToCoarser(const Index& index_global) { for (int i=0; i<3; ++i) assert(index_global[i] % 2 == 0); return index_global / 2; } Index GridIndexTranslations::GlobalToFiner(const Index& index_global, const int& num_levels) { return Helper::intpow(2, num_levels) * index_global; } Index GridIndexTranslations::GlobalToCoarser(const Index& index_global, const int& num_levels) { int quotient = Helper::intpow(2, num_levels); for (int i=0; i<3; ++i) assert(index_global[i] % quotient == 0); return index_global / quotient; } Index GridIndexTranslations::LocalToGlobal(const Index& index_local) const { return index_local - Father()->Local().Begin() + Father()->Global().BeginLocal(); } Index GridIndexTranslations::LocalToFiner(const Index& index_local) const { assert(Father() != NULL); assert(Father()->Father() != NULL); const Multigrid& multigrid = *(Father()->Father()); assert(Father()->Level() < multigrid.MaxLevel()); Index index_global_fine = GlobalToFiner(LocalToGlobal(index_local)); return multigrid(Father()->Level()+1).Indexing().GlobalToLocal(index_global_fine); } Index GridIndexTranslations::LocalToCoarser(const Index& index_local) const { assert(Father() != NULL); assert(Father()->Father() != NULL); const Multigrid& multigrid = *(Father()->Father()); assert(Father()->Level() > multigrid.MinLevel()); Index index_global_coarse = GlobalToCoarser(LocalToGlobal(index_local)); return multigrid(Father()->Level()-1).Indexing().GlobalToLocal(index_global_coarse); } Index GridIndexTranslations::FinestGlobalToLocal(const Index& index_finest) const { return GlobalToLocal(FinestGlobalToGlobal(index_finest)); } Index GridIndexTranslations::FinestGlobalToGlobal(const Index& index_finest) const { int quotient = Helper::intpow(2, Father()->Father()->MaxLevel() - Father()->Level()); for (int i=0; i<3; ++i) assert(index_finest[i] % quotient == 0); return index_finest / quotient; } void GridIndexTranslations::FineToCoarse(Index& begin, Index& end) { Index last_point = end - 1; for (int j=0; j<3; ++j) { if (begin[j] % 2 == 0) begin[j] /= 2; else begin[j] = (begin[j]+1) / 2; if (last_point[j] % 2 == 0) last_point[j] /= 2; else last_point[j] = (last_point[j]-1) / 2; } end = last_point + 1; } void GridIndexTranslations::CoarseToFine(Index& begin, Index& end, const Index& size_global) { for (int i=0; i<3; ++i) { if (size_global[i] % 2 == 0) { begin[i] = 2*begin[i]; end[i] = 2*end[i]; }else { if (begin[i] > 0) begin[i] = 2*begin[i] - 1; end[i] = std::max(2*end[i] - 1, begin[i]); } } }