| 1 | /*
|
|---|
| 2 | * vmg - a versatile multigrid solver
|
|---|
| 3 | * Copyright (C) 2012 Institute for Numerical Simulation, University of Bonn
|
|---|
| 4 | *
|
|---|
| 5 | * vmg is free software: you can redistribute it and/or modify
|
|---|
| 6 | * it under the terms of the GNU General Public License as published by
|
|---|
| 7 | * the Free Software Foundation, either version 3 of the License, or
|
|---|
| 8 | * (at your option) any later version.
|
|---|
| 9 | *
|
|---|
| 10 | * vmg is distributed in the hope that it will be useful,
|
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 13 | * GNU General Public License for more details.
|
|---|
| 14 | *
|
|---|
| 15 | * You should have received a copy of the GNU General Public License
|
|---|
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * @file grid_index_translations.cpp
|
|---|
| 21 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
|
|---|
| 22 | * @date Tue May 17 11:46:37 2011
|
|---|
| 23 | *
|
|---|
| 24 | * @brief Class to convert different representations of grid
|
|---|
| 25 | * indices.
|
|---|
| 26 | *
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #ifdef HAVE_CONFIG_H
|
|---|
| 30 | #include <config.h>
|
|---|
| 31 | #endif
|
|---|
| 32 |
|
|---|
| 33 | #include <cassert>
|
|---|
| 34 |
|
|---|
| 35 | #include "base/helper.hpp"
|
|---|
| 36 | #include "grid/grid_index_translations.hpp"
|
|---|
| 37 | #include "grid/grid.hpp"
|
|---|
| 38 | #include "grid/multigrid.hpp"
|
|---|
| 39 |
|
|---|
| 40 | using namespace VMG;
|
|---|
| 41 |
|
|---|
| 42 | Index GridIndexTranslations::GlobalToLocal(const Index& index_global) const
|
|---|
| 43 | {
|
|---|
| 44 | const Index index_local = index_global - Father()->Global().LocalBegin() + Father()->Local().Begin();
|
|---|
| 45 | return index_local;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | Index GridIndexTranslations::GlobalToFiner(const Index& index_global)
|
|---|
| 49 | {
|
|---|
| 50 | const Index index_finer = 2 * index_global;
|
|---|
| 51 | return index_finer;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | Index GridIndexTranslations::GlobalToCoarser(const Index& index_global)
|
|---|
| 55 | {
|
|---|
| 56 | assert(index_global % 2 == 0);
|
|---|
| 57 | const Index index_coarser = index_global / 2;
|
|---|
| 58 | return index_coarser;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | Index GridIndexTranslations::GlobalToFiner(const Index& index_global, const int& num_levels)
|
|---|
| 62 | {
|
|---|
| 63 | const Index index_finer = Helper::intpow(2, num_levels) * index_global;
|
|---|
| 64 | return index_finer;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | Index GridIndexTranslations::GlobalToCoarser(const Index& index_global, const int& num_levels)
|
|---|
| 68 | {
|
|---|
| 69 | int quotient = Helper::intpow(2, num_levels);
|
|---|
| 70 | assert(index_global % quotient == 0);
|
|---|
| 71 | const Index index_coarser = index_global / quotient;
|
|---|
| 72 | return index_coarser;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | Index GridIndexTranslations::LocalToGlobal(const Index& index_local) const
|
|---|
| 76 | {
|
|---|
| 77 | const Index index_global = index_local - Father()->Local().Begin() + Father()->Global().LocalBegin();
|
|---|
| 78 | return index_global;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | Index GridIndexTranslations::LocalToFiner(const Index& index_local) const
|
|---|
| 82 | {
|
|---|
| 83 | assert(Father() != NULL);
|
|---|
| 84 | assert(Father()->Father() != NULL);
|
|---|
| 85 |
|
|---|
| 86 | const Multigrid& multigrid = *(Father()->Father());
|
|---|
| 87 |
|
|---|
| 88 | assert(Father()->Level() < multigrid.MaxLevel());
|
|---|
| 89 |
|
|---|
| 90 | const Index index_global_fine = GlobalToFiner(LocalToGlobal(index_local));
|
|---|
| 91 | const Index index_local_fine = multigrid(Father()->Level()+1).Indexing().GlobalToLocal(index_global_fine);
|
|---|
| 92 |
|
|---|
| 93 | return index_local_fine;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | Index GridIndexTranslations::LocalToCoarser(const Index& index_local) const
|
|---|
| 97 | {
|
|---|
| 98 | assert(Father() != NULL);
|
|---|
| 99 | assert(Father()->Father() != NULL);
|
|---|
| 100 |
|
|---|
| 101 | const Multigrid& multigrid = *(Father()->Father());
|
|---|
| 102 |
|
|---|
| 103 | assert(Father()->Level() > multigrid.MinLevel());
|
|---|
| 104 |
|
|---|
| 105 | const Index index_global_coarse = GlobalToCoarser(LocalToGlobal(index_local));
|
|---|
| 106 | const Index index_local_coarse = multigrid(Father()->Level()-1).Indexing().GlobalToLocal(index_global_coarse);
|
|---|
| 107 |
|
|---|
| 108 | return index_local_coarse;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | Index GridIndexTranslations::FinestGlobalToLocal(const Index& index_finest) const
|
|---|
| 112 | {
|
|---|
| 113 | const Index index_local = GlobalToLocal(FinestGlobalToGlobal(index_finest));
|
|---|
| 114 | return index_local;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | Index GridIndexTranslations::FinestGlobalToGlobal(const Index& index_finest) const
|
|---|
| 118 | {
|
|---|
| 119 | const int quotient = Helper::intpow(2, Father()->Father()->MaxLevel() - Father()->Level());
|
|---|
| 120 | assert(index_finest % quotient == 0);
|
|---|
| 121 | const Index index_global = index_finest / quotient;
|
|---|
| 122 | return index_global;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | void GridIndexTranslations::FineToCoarse(Index& begin, Index& end)
|
|---|
| 126 | {
|
|---|
| 127 | Index last_point = end - 1;
|
|---|
| 128 |
|
|---|
| 129 | for (int j=0; j<3; ++j) {
|
|---|
| 130 |
|
|---|
| 131 | if (begin[j] % 2 == 0)
|
|---|
| 132 | begin[j] /= 2;
|
|---|
| 133 | else
|
|---|
| 134 | begin[j] = (begin[j]+1) / 2;
|
|---|
| 135 |
|
|---|
| 136 | if (last_point[j] % 2 == 0)
|
|---|
| 137 | last_point[j] /= 2;
|
|---|
| 138 | else
|
|---|
| 139 | last_point[j] = (last_point[j]-1) / 2;
|
|---|
| 140 |
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 | end = last_point + 1;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | void GridIndexTranslations::CoarseToFine(Index& begin, Index& end, const Index& size_global)
|
|---|
| 148 | {
|
|---|
| 149 | for (int i=0; i<3; ++i) {
|
|---|
| 150 |
|
|---|
| 151 | if (size_global[i] % 2 == 0) {
|
|---|
| 152 |
|
|---|
| 153 | begin[i] = 2*begin[i];
|
|---|
| 154 | end[i] = 2*end[i];
|
|---|
| 155 |
|
|---|
| 156 | }else {
|
|---|
| 157 |
|
|---|
| 158 | if (begin[i] > 0)
|
|---|
| 159 | begin[i] = 2*begin[i] - 1;
|
|---|
| 160 | end[i] = std::max(2*end[i] - 1, begin[i]);
|
|---|
| 161 |
|
|---|
| 162 | }
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|