| 1 | /**
|
|---|
| 2 | * @file grid_index_translations.cpp
|
|---|
| 3 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
|
|---|
| 4 | * @date Tue May 17 11:46:37 2011
|
|---|
| 5 | *
|
|---|
| 6 | * @brief Class to convert different representations of grid
|
|---|
| 7 | * indices.
|
|---|
| 8 | *
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | #ifdef HAVE_CONFIG_H
|
|---|
| 12 | #include <config.h>
|
|---|
| 13 | #endif
|
|---|
| 14 |
|
|---|
| 15 | #include <cassert>
|
|---|
| 16 |
|
|---|
| 17 | #include "base/helper.hpp"
|
|---|
| 18 | #include "grid/grid_index_translations.hpp"
|
|---|
| 19 | #include "grid/grid.hpp"
|
|---|
| 20 | #include "grid/multigrid.hpp"
|
|---|
| 21 |
|
|---|
| 22 | using namespace VMG;
|
|---|
| 23 |
|
|---|
| 24 | Index GridIndexTranslations::GlobalToLocal(const Index& index_global) const
|
|---|
| 25 | {
|
|---|
| 26 | return index_global - Father()->Global().BeginLocal() + Father()->Local().Begin();
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | Index GridIndexTranslations::GlobalToFiner(const Index& index_global)
|
|---|
| 30 | {
|
|---|
| 31 | return 2 * index_global;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | Index GridIndexTranslations::GlobalToCoarser(const Index& index_global)
|
|---|
| 35 | {
|
|---|
| 36 | for (int i=0; i<3; ++i)
|
|---|
| 37 | assert(index_global[i] % 2 == 0);
|
|---|
| 38 |
|
|---|
| 39 | return index_global / 2;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | Index GridIndexTranslations::GlobalToFiner(const Index& index_global, const int& num_levels)
|
|---|
| 43 | {
|
|---|
| 44 | return Helper::intpow(2, num_levels) * index_global;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | Index GridIndexTranslations::GlobalToCoarser(const Index& index_global, const int& num_levels)
|
|---|
| 48 | {
|
|---|
| 49 | int quotient = Helper::intpow(2, num_levels);
|
|---|
| 50 |
|
|---|
| 51 | for (int i=0; i<3; ++i)
|
|---|
| 52 | assert(index_global[i] % quotient == 0);
|
|---|
| 53 |
|
|---|
| 54 | return index_global / quotient;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | Index GridIndexTranslations::LocalToGlobal(const Index& index_local) const
|
|---|
| 58 | {
|
|---|
| 59 | return index_local - Father()->Local().Begin() + Father()->Global().BeginLocal();
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | Index GridIndexTranslations::LocalToFiner(const Index& index_local) const
|
|---|
| 63 | {
|
|---|
| 64 | assert(Father() != NULL);
|
|---|
| 65 | assert(Father()->Father() != NULL);
|
|---|
| 66 |
|
|---|
| 67 | const Multigrid& multigrid = *(Father()->Father());
|
|---|
| 68 |
|
|---|
| 69 | assert(Father()->Level() < multigrid.MaxLevel());
|
|---|
| 70 |
|
|---|
| 71 | Index index_global_fine = GlobalToFiner(LocalToGlobal(index_local));
|
|---|
| 72 |
|
|---|
| 73 | return multigrid(Father()->Level()+1).Indexing().GlobalToLocal(index_global_fine);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | Index GridIndexTranslations::LocalToCoarser(const Index& index_local) const
|
|---|
| 77 | {
|
|---|
| 78 | assert(Father() != NULL);
|
|---|
| 79 | assert(Father()->Father() != NULL);
|
|---|
| 80 |
|
|---|
| 81 | const Multigrid& multigrid = *(Father()->Father());
|
|---|
| 82 |
|
|---|
| 83 | assert(Father()->Level() > multigrid.MinLevel());
|
|---|
| 84 |
|
|---|
| 85 | Index index_global_coarse = GlobalToCoarser(LocalToGlobal(index_local));
|
|---|
| 86 |
|
|---|
| 87 | return multigrid(Father()->Level()-1).Indexing().GlobalToLocal(index_global_coarse);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | Index GridIndexTranslations::FinestGlobalToLocal(const Index& index_finest) const
|
|---|
| 91 | {
|
|---|
| 92 | return GlobalToLocal(FinestGlobalToGlobal(index_finest));
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | Index GridIndexTranslations::FinestGlobalToGlobal(const Index& index_finest) const
|
|---|
| 96 | {
|
|---|
| 97 | int quotient = Helper::intpow(2, Father()->Father()->MaxLevel() - Father()->Level());
|
|---|
| 98 |
|
|---|
| 99 | for (int i=0; i<3; ++i)
|
|---|
| 100 | assert(index_finest[i] % quotient == 0);
|
|---|
| 101 |
|
|---|
| 102 | return index_finest / quotient;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | void GridIndexTranslations::FineToCoarse(Index& begin, Index& end)
|
|---|
| 106 | {
|
|---|
| 107 | Index last_point = end - 1;
|
|---|
| 108 |
|
|---|
| 109 | for (int j=0; j<3; ++j) {
|
|---|
| 110 |
|
|---|
| 111 | if (begin[j] % 2 == 0)
|
|---|
| 112 | begin[j] /= 2;
|
|---|
| 113 | else
|
|---|
| 114 | begin[j] = (begin[j]+1) / 2;
|
|---|
| 115 |
|
|---|
| 116 | if (last_point[j] % 2 == 0)
|
|---|
| 117 | last_point[j] /= 2;
|
|---|
| 118 | else
|
|---|
| 119 | last_point[j] = (last_point[j]-1) / 2;
|
|---|
| 120 |
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 | end = last_point + 1;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | void GridIndexTranslations::CoarseToFine(Index& begin, Index& end, const Index& size_global)
|
|---|
| 128 | {
|
|---|
| 129 | for (int i=0; i<3; ++i) {
|
|---|
| 130 |
|
|---|
| 131 | if (size_global[i] % 2 == 0) {
|
|---|
| 132 |
|
|---|
| 133 | begin[i] = 2*begin[i];
|
|---|
| 134 | end[i] = 2*end[i];
|
|---|
| 135 |
|
|---|
| 136 | }else {
|
|---|
| 137 |
|
|---|
| 138 | if (begin[i] > 0)
|
|---|
| 139 | begin[i] = 2*begin[i] - 1;
|
|---|
| 140 | end[i] = std::max(2*end[i] - 1, begin[i]);
|
|---|
| 141 |
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|