| [48b662] | 1 | /**
|
|---|
| 2 | * @file grid.cpp
|
|---|
| 3 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
|
|---|
| 4 | * @date Mon Apr 18 12:53:27 2011
|
|---|
| 5 | *
|
|---|
| 6 | * @brief VMG::Grid
|
|---|
| 7 | *
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | #ifdef HAVE_CONFIG_H
|
|---|
| 11 | #include <config.h>
|
|---|
| 12 | #endif
|
|---|
| 13 |
|
|---|
| 14 | #include <cstdio>
|
|---|
| [dfed1c] | 15 | #include <cmath>
|
|---|
| [48b662] | 16 | #include <limits>
|
|---|
| 17 |
|
|---|
| 18 | #include "base/helper.hpp"
|
|---|
| [dfed1c] | 19 | #include "base/stencil.hpp"
|
|---|
| [48b662] | 20 | #include "comm/comm.hpp"
|
|---|
| 21 | #include "grid/grid.hpp"
|
|---|
| 22 | #include "grid/tempgrid.hpp"
|
|---|
| 23 | #include "mg.hpp"
|
|---|
| 24 |
|
|---|
| 25 | using namespace VMG;
|
|---|
| 26 |
|
|---|
| 27 | vmg_float Grid::correction;
|
|---|
| 28 |
|
|---|
| [dfed1c] | 29 | void Grid::InitGrid()
|
|---|
| [48b662] | 30 | {
|
|---|
| [dfed1c] | 31 | grid = new vmg_float[local.SizeTotal().Product()];
|
|---|
| 32 | }
|
|---|
| [48b662] | 33 |
|
|---|
| [dfed1c] | 34 | Grid::~Grid()
|
|---|
| 35 | {
|
|---|
| 36 | delete [] grid;
|
|---|
| 37 | }
|
|---|
| [48b662] | 38 |
|
|---|
| [dfed1c] | 39 | Grid& Grid::operator=(const Grid& rhs)
|
|---|
| 40 | {
|
|---|
| 41 | if (this != &rhs) {
|
|---|
| [48b662] | 42 |
|
|---|
| [dfed1c] | 43 | global = rhs.Global();
|
|---|
| 44 | local = rhs.Local();
|
|---|
| 45 | extent = rhs.Extent();
|
|---|
| 46 | iterators = rhs.Iterators();
|
|---|
| 47 | father = rhs.Father();
|
|---|
| [48b662] | 48 |
|
|---|
| [dfed1c] | 49 | index_translations = rhs.Indexing();
|
|---|
| 50 | level = rhs.Level();
|
|---|
| [48b662] | 51 |
|
|---|
| [dfed1c] | 52 | delete [] grid;
|
|---|
| 53 | grid = new vmg_float[local.SizeTotal().Product()];
|
|---|
| [48b662] | 54 |
|
|---|
| [dfed1c] | 55 | SetGrid(rhs);
|
|---|
| [48b662] | 56 |
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| [dfed1c] | 59 | return *this;
|
|---|
| [48b662] | 60 | }
|
|---|
| 61 |
|
|---|
| 62 | void Grid::Clear()
|
|---|
| 63 | {
|
|---|
| 64 | for (int i=0; i<local.SizeTotal().Product(); ++i)
|
|---|
| 65 | grid[i] = 0.0;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | void Grid::ClearInner()
|
|---|
| 69 | {
|
|---|
| [dfed1c] | 70 | for (Grid::iterator iter=Iterators().Local().Begin(); iter!=Iterators().Local().End(); ++iter)
|
|---|
| 71 | (*this)(*iter) = 0.0;
|
|---|
| [48b662] | 72 | }
|
|---|
| 73 |
|
|---|
| 74 | void Grid::ClearHalo()
|
|---|
| 75 | {
|
|---|
| [dfed1c] | 76 | Grid::iterator iter;
|
|---|
| 77 |
|
|---|
| 78 | for (int i=0; i<3; ++i) {
|
|---|
| 79 |
|
|---|
| 80 | for (iter = Iterators().Halo1()[i].Begin(); iter != Iterators().Halo1()[i].End(); ++iter)
|
|---|
| 81 | (*this)(*iter) = 0.0;
|
|---|
| 82 |
|
|---|
| 83 | for (iter = Iterators().Halo2()[i].Begin(); iter != Iterators().Halo2()[i].End(); ++iter)
|
|---|
| 84 | (*this)(*iter) = 0.0;
|
|---|
| 85 |
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| [48b662] | 88 | }
|
|---|
| 89 |
|
|---|
| 90 | void Grid::ClearBoundary()
|
|---|
| 91 | {
|
|---|
| [dfed1c] | 92 | Grid::iterator iter;
|
|---|
| 93 |
|
|---|
| 94 | for (int i=0; i<3; ++i) {
|
|---|
| 95 |
|
|---|
| 96 | for (iter = Iterators().Boundary1()[i].Begin(); iter != Iterators().Boundary1()[i].End(); ++iter)
|
|---|
| 97 | (*this)(*iter) = 0.0;
|
|---|
| 98 |
|
|---|
| 99 | for (iter = Iterators().Boundary2()[i].Begin(); iter != Iterators().Boundary2()[i].End(); ++iter)
|
|---|
| 100 | (*this)(*iter) = 0.0;
|
|---|
| 101 |
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| [48b662] | 104 | }
|
|---|
| 105 |
|
|---|
| 106 | void Grid::SetAverageToZero()
|
|---|
| 107 | {
|
|---|
| [dfed1c] | 108 | Grid::iterator iter;
|
|---|
| [48b662] | 109 | vmg_float avg = 0.0;
|
|---|
| 110 |
|
|---|
| [dfed1c] | 111 | for (iter = Iterators().Local().Begin(); iter != Iterators().Local().End(); ++iter)
|
|---|
| 112 | avg += GetVal(*iter);
|
|---|
| [48b662] | 113 |
|
|---|
| 114 | avg /= Local().Size().Product();
|
|---|
| 115 |
|
|---|
| [dfed1c] | 116 | for (iter = Iterators().Local().Begin(); iter != Iterators().Local().End(); ++iter)
|
|---|
| 117 | (*this)(*iter) -= avg;
|
|---|
| [48b662] | 118 | }
|
|---|
| 119 |
|
|---|
| 120 | void Grid::ForceDiscreteCompatibilityCondition()
|
|---|
| 121 | {
|
|---|
| [dfed1c] | 122 | Grid::iterator iter;
|
|---|
| [48b662] | 123 | vmg_float val = 0.0;
|
|---|
| 124 |
|
|---|
| [dfed1c] | 125 | for (iter = Iterators().Local().Begin(); iter != Iterators().Local().End(); ++iter)
|
|---|
| 126 | val += GetVal(*iter);
|
|---|
| 127 |
|
|---|
| 128 | val = MG::GetComm()->GlobalSum(val);
|
|---|
| [48b662] | 129 |
|
|---|
| [ac6d04] | 130 | if (fabs(val) > Global().GlobalSize().Product() * std::numeric_limits<vmg_float>::epsilon()) {
|
|---|
| [48b662] | 131 |
|
|---|
| [dfed1c] | 132 | #ifdef DEBUG_OUTPUT
|
|---|
| 133 | MG::GetComm()->PrintStringOnce("WARNING: Right hand side does not satisfy the compatibility condition.");
|
|---|
| [48b662] | 134 | #endif
|
|---|
| 135 |
|
|---|
| [ac6d04] | 136 | val *= Helper::pow_2(this->Extent().MeshWidth().Max());
|
|---|
| [dfed1c] | 137 | for (iter = Iterators().Local().Begin(); iter != Iterators().Local().End(); ++iter)
|
|---|
| 138 | (*this)(*iter) -= val;
|
|---|
| [48b662] | 139 |
|
|---|
| [dfed1c] | 140 | #ifdef DEBUG_OUTPUT
|
|---|
| 141 | val = 0.0;
|
|---|
| 142 | for (iter = Iterators().Local().Begin(); iter != Iterators().Local().End(); ++iter)
|
|---|
| 143 | val += GetVal(*iter);
|
|---|
| 144 | val = MG::GetComm()->GlobalSumRoot(val);
|
|---|
| 145 | MG::GetComm()->PrintStringOnce("Sum of grid charges after forcing the discrete compatibility condition: %e", val);
|
|---|
| 146 | #endif
|
|---|
| [48b662] | 147 | }
|
|---|
| [dfed1c] | 148 |
|
|---|
| [48b662] | 149 | }
|
|---|
| 150 |
|
|---|
| 151 | void Grid::SetGrid(const Grid& rhs)
|
|---|
| 152 | {
|
|---|
| 153 | #ifdef DEBUG
|
|---|
| 154 | IsCompatible(rhs);
|
|---|
| 155 | #endif
|
|---|
| 156 |
|
|---|
| [dfed1c] | 157 | for (Grid::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
|
|---|
| 158 | (*this)(*iter) = rhs.GetVal(*iter);
|
|---|
| [48b662] | 159 | }
|
|---|
| 160 |
|
|---|
| 161 | void Grid::SetBoundary(const Grid& rhs)
|
|---|
| 162 | {
|
|---|
| 163 | #ifdef DEBUG
|
|---|
| 164 | IsCompatible(rhs);
|
|---|
| 165 | #endif
|
|---|
| [dfed1c] | 166 | Grid::iterator iter;
|
|---|
| 167 |
|
|---|
| 168 | for (int i=0; i<3; ++i) {
|
|---|
| [48b662] | 169 |
|
|---|
| [dfed1c] | 170 | for (iter = Iterators().Boundary1()[i].Begin(); iter != Iterators().Boundary1()[i].End(); ++iter)
|
|---|
| 171 | (*this)(*iter) = rhs.GetVal(*iter);
|
|---|
| 172 |
|
|---|
| 173 | for (iter = Iterators().Boundary2()[i].Begin(); iter != Iterators().Boundary2()[i].End(); ++iter)
|
|---|
| 174 | (*this)(*iter) = rhs.GetVal(*iter);
|
|---|
| 175 |
|
|---|
| 176 | }
|
|---|
| [48b662] | 177 | }
|
|---|
| 178 |
|
|---|
| 179 | void Grid::AddGrid(const Grid& rhs)
|
|---|
| 180 | {
|
|---|
| [dfed1c] | 181 | #ifdef DEBUG_MATRIX_CHECKS
|
|---|
| [48b662] | 182 | IsCompatible(rhs);
|
|---|
| 183 | #endif
|
|---|
| 184 |
|
|---|
| [dfed1c] | 185 | for (Grid::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
|
|---|
| 186 | (*this)(*iter) += rhs.GetVal(*iter);
|
|---|
| [48b662] | 187 | }
|
|---|
| 188 |
|
|---|
| 189 | void Grid::SubtractGrid(const Grid& rhs)
|
|---|
| 190 | {
|
|---|
| [dfed1c] | 191 | #ifdef DEBUG_MATRIX_CHECKS
|
|---|
| [48b662] | 192 | IsCompatible(rhs);
|
|---|
| 193 | #endif
|
|---|
| 194 |
|
|---|
| [dfed1c] | 195 | for (Grid::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
|
|---|
| 196 | (*this)(*iter) -= rhs.GetVal(*iter);
|
|---|
| [48b662] | 197 | }
|
|---|
| 198 |
|
|---|
| 199 | void Grid::MultiplyScalar(const vmg_float& scalar)
|
|---|
| 200 | {
|
|---|
| [dfed1c] | 201 | for (Grid::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
|
|---|
| 202 | (*this)(*iter) *= scalar;
|
|---|
| [48b662] | 203 | }
|
|---|
| 204 |
|
|---|
| 205 | void Grid::ApplyStencil(const Stencil& stencil)
|
|---|
| 206 | {
|
|---|
| [dfed1c] | 207 | Grid::iterator grid_iter;
|
|---|
| 208 | Stencil::iterator stencil_iter;
|
|---|
| [48b662] | 209 | TempGrid *temp = MG::GetTempGrid();
|
|---|
| 210 |
|
|---|
| 211 | temp->SetProperties(*this);
|
|---|
| 212 | temp->SetGrid(*this);
|
|---|
| 213 | MG::GetComm()->CommToGhosts(*temp);
|
|---|
| 214 |
|
|---|
| [dfed1c] | 215 | for (grid_iter = Iterators().Local().Begin(); grid_iter != Iterators().Local().End(); ++grid_iter) {
|
|---|
| [48b662] | 216 |
|
|---|
| [dfed1c] | 217 | (*this)(*grid_iter) = stencil.GetDiag() * temp->GetVal(*grid_iter);
|
|---|
| [48b662] | 218 |
|
|---|
| [dfed1c] | 219 | for (stencil_iter=stencil.begin(); stencil_iter!=stencil.end(); ++stencil_iter)
|
|---|
| 220 | (*this)(*grid_iter) += stencil_iter->Val() * temp->GetVal(*grid_iter + stencil_iter->Disp());
|
|---|
| [48b662] | 221 | }
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | bool Grid::IsCompatible(const Grid& rhs) const
|
|---|
| 225 | {
|
|---|
| 226 | bool eq = true;
|
|---|
| 227 |
|
|---|
| 228 | eq &= Helper::IsEq(Local().Begin(), rhs.Local().Begin(), "Local().Begin");
|
|---|
| 229 | eq &= Helper::IsEq(Local().End(), rhs.Local().End(), "Local().End");
|
|---|
| 230 | eq &= Helper::IsEq(Local().Size(), rhs.Local().Size(), "Local().Size");
|
|---|
| 231 | eq &= Helper::IsEq(Local().SizeTotal(), rhs.Local().SizeTotal(), "Local().SizeTotal");
|
|---|
| 232 |
|
|---|
| [ac6d04] | 233 | eq &= Helper::IsEq(Global().LocalBegin(), rhs.Global().LocalBegin(), "Global().LocalBegin");
|
|---|
| 234 | eq &= Helper::IsEq(Global().LocalEnd(), rhs.Global().LocalEnd(), "Global().LocalEnd");
|
|---|
| 235 | eq &= Helper::IsEq(Global().GlobalSize(), rhs.Global().GlobalSize(), "Global().GlobalSize");
|
|---|
| 236 | eq &= Helper::IsEq(Global().LocalSize(), rhs.Global().LocalSize(), "Global().LocalSize");
|
|---|
| [48b662] | 237 |
|
|---|
| 238 | eq &= Helper::IsEq(Local().HaloBegin1(), rhs.Local().HaloBegin1(), "Local().HaloBegin1");
|
|---|
| 239 | eq &= Helper::IsEq(Local().HaloBegin2(), rhs.Local().HaloBegin2(), "Local().HaloBegin2");
|
|---|
| 240 | eq &= Helper::IsEq(Local().HaloEnd1(), rhs.Local().HaloEnd1(), "Local().HaloEnd1");
|
|---|
| 241 | eq &= Helper::IsEq(Local().HaloEnd2(), rhs.Local().HaloEnd2(), "Local().HaloEnd2");
|
|---|
| 242 |
|
|---|
| [ac6d04] | 243 | eq &= Helper::IsEq(Extent().MeshWidth(), rhs.Extent().MeshWidth(), "MeshWidth");
|
|---|
| [48b662] | 244 |
|
|---|
| 245 | return eq;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | bool Grid::IsConsistent() const
|
|---|
| 249 | {
|
|---|
| 250 | bool consistent = true;
|
|---|
| 251 |
|
|---|
| [dfed1c] | 252 | for (Grid::iterator iter=Iterators().CompleteGrid().Begin(); iter!=Iterators().CompleteGrid().End(); ++iter)
|
|---|
| 253 | consistent &= Helper::CheckNumber(GetVal(*iter));
|
|---|
| [48b662] | 254 |
|
|---|
| 255 | return consistent;
|
|---|
| 256 | }
|
|---|