| 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 "base/helper.hpp"
|
|---|
| 34 | #include "grid/grid_double_iterator.hpp"
|
|---|
| 35 | #include "grid/grid_index_translations.hpp"
|
|---|
| 36 | #include "grid/grid.hpp"
|
|---|
| 37 | #include "grid/multigrid.hpp"
|
|---|
| 38 |
|
|---|
| 39 | using namespace VMG;
|
|---|
| 40 |
|
|---|
| 41 | bool GridIndexTranslations::IsGridPointOf(const Grid& grid, const Index& index_finest)
|
|---|
| 42 | {
|
|---|
| 43 | assert(grid.Father() != NULL);
|
|---|
| 44 | return index_finest[0] % Helper::intpow(2, grid.Father()->MaxLevel() - grid.Level()) == 0 &&
|
|---|
| 45 | index_finest[1] % Helper::intpow(2, grid.Father()->MaxLevel() - grid.Level()) == 0 &&
|
|---|
| 46 | index_finest[2] % Helper::intpow(2, grid.Father()->MaxLevel() - grid.Level()) == 0;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | Index GridIndexTranslations::LocalToGlobal(const Grid& grid, const Index& index_local)
|
|---|
| 50 | {
|
|---|
| 51 | return index_local - grid.Local().Begin() + grid.Global().LocalBegin();
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | Index GridIndexTranslations::LocalToGlobalFinest(const Grid& grid, const Index& index_local)
|
|---|
| 55 | {
|
|---|
| 56 | return GlobalToGlobalFinest(grid, LocalToGlobal(grid, index_local));
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | Index GridIndexTranslations::GlobalToLocal(const Grid& grid, const Index& index_global)
|
|---|
| 60 | {
|
|---|
| 61 | return index_global - grid.Global().LocalBegin() + grid.Local().Begin();
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | Index GridIndexTranslations::GlobalToGlobalFinest(const Grid& grid, const Index& index_global)
|
|---|
| 65 | {
|
|---|
| 66 | assert(grid.Father() != NULL);
|
|---|
| 67 | return Helper::intpow(2, grid.Father()->MaxLevel() - grid.Level()) * index_global;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | Index GridIndexTranslations::GlobalFinestToLocal(const Grid& grid, const Index& index_finest)
|
|---|
| 71 | {
|
|---|
| 72 | return GlobalToLocal(grid, GlobalFinestToGlobal(grid, index_finest));
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | Index GridIndexTranslations::GlobalFinestToGlobal(const Grid& grid, const Index& index_finest)
|
|---|
| 76 | {
|
|---|
| 77 | assert(grid.Father() != NULL);
|
|---|
| 78 | assert(IsGridPointOf(grid, index_finest));
|
|---|
| 79 | return index_finest / Helper::intpow(2, grid.Father()->MaxLevel() - grid.Level());
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | void GridIndexTranslations::GlobalCoarseToFine(Index& begin, Index& end)
|
|---|
| 83 | {
|
|---|
| 84 | for (int j=0; j<3; ++j) {
|
|---|
| 85 | begin[j] = 2 * begin[j];
|
|---|
| 86 | end[j] = 2 * (end[j]-1) + 1;
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | void GridIndexTranslations::GlobalFineToCoarse(Index& begin, Index& end)
|
|---|
| 91 | {
|
|---|
| 92 | for (int j=0; j<3; ++j) {
|
|---|
| 93 | begin[j] = Helper::RoundUpToNextMultiple(begin[j], 2) / 2;
|
|---|
| 94 | end[j] = Helper::RoundDownToNextMultiple(end[j]-1, 2) / 2 + 1;
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | void GridIndexTranslations::GetGridAlignment(const Grid& grid_1, GridIteratorSet& bounds_1,
|
|---|
| 99 | const Grid& grid_2, GridIteratorSet& bounds_2)
|
|---|
| 100 | {
|
|---|
| 101 | assert(grid_1.Father() != NULL);
|
|---|
| 102 | assert(grid_2.Father() != NULL);
|
|---|
| 103 |
|
|---|
| 104 | if (grid_1.Level() == grid_2.Level()) {
|
|---|
| 105 | const Index begin_global = grid_1.Global()
|
|---|
| 106 | .LocalBegin()
|
|---|
| 107 | .Clamp(grid_2.Global().LocalBegin(), grid_2.Global().LocalEnd());
|
|---|
| 108 |
|
|---|
| 109 | const Index end_global = grid_1.Global()
|
|---|
| 110 | .LocalEnd()
|
|---|
| 111 | .Clamp(grid_2.Global().LocalBegin(), grid_2.Global().LocalEnd());
|
|---|
| 112 |
|
|---|
| 113 | bounds_1 = GridIteratorSet(GlobalToLocal(grid_1, begin_global), GlobalToLocal(grid_2, end_global));
|
|---|
| 114 | bounds_2 = GridIteratorSet(GlobalToLocal(grid_2, begin_global), GlobalToLocal(grid_2, end_global));
|
|---|
| 115 |
|
|---|
| 116 | } else {
|
|---|
| 117 |
|
|---|
| 118 | const Grid& grid_c = (grid_1.Level() < grid_2.Level() ? grid_1 : grid_2);
|
|---|
| 119 | const int global_mult = Helper::intpow(2, grid_c.Father()->MaxLevel() - grid_c.Level());
|
|---|
| 120 |
|
|---|
| 121 | Index begin_finest = GlobalToGlobalFinest(grid_1, grid_1.Global().LocalBegin())
|
|---|
| 122 | .Clamp(GlobalToGlobalFinest(grid_2, grid_2.Global().LocalBegin()),
|
|---|
| 123 | GlobalToGlobalFinest(grid_2, grid_2.Global().LocalEnd()-1));
|
|---|
| 124 |
|
|---|
| 125 | Index end_finest = GlobalToGlobalFinest(grid_1, grid_1.Global().LocalEnd()-1)
|
|---|
| 126 | .Clamp(GlobalToGlobalFinest(grid_2, grid_2.Global().LocalBegin()),
|
|---|
| 127 | GlobalToGlobalFinest(grid_2, grid_2.Global().LocalEnd()-1));
|
|---|
| 128 |
|
|---|
| 129 | for (int j=0; j<3; ++j) {
|
|---|
| 130 | begin_finest[j] = Helper::RoundUpToNextMultiple(begin_finest[j], global_mult);
|
|---|
| 131 | end_finest[j] = Helper::RoundDownToNextMultiple(end_finest[j], global_mult);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | bounds_1 = GridIteratorSet(GlobalFinestToLocal(grid_1, begin_finest), GlobalFinestToLocal(grid_1, end_finest)+1);
|
|---|
| 135 | bounds_2 = GridIteratorSet(GlobalFinestToLocal(grid_2, begin_finest), GlobalFinestToLocal(grid_2, end_finest)+1);
|
|---|
| 136 |
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|