| 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>
|
|---|
| 15 | #include <cmath>
|
|---|
| 16 | #include <limits>
|
|---|
| 17 |
|
|---|
| 18 | #include "base/helper.hpp"
|
|---|
| 19 | #include "base/stencil.hpp"
|
|---|
| 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 |
|
|---|
| 29 | void Grid::InitGrid()
|
|---|
| 30 | {
|
|---|
| 31 | grid = new vmg_float[local.SizeTotal().Product()];
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | Grid::~Grid()
|
|---|
| 35 | {
|
|---|
| 36 | delete [] grid;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | Grid& Grid::operator=(const Grid& rhs)
|
|---|
| 40 | {
|
|---|
| 41 | if (this != &rhs) {
|
|---|
| 42 |
|
|---|
| 43 | global = rhs.Global();
|
|---|
| 44 | local = rhs.Local();
|
|---|
| 45 | extent = rhs.Extent();
|
|---|
| 46 | iterators = rhs.Iterators();
|
|---|
| 47 | father = rhs.Father();
|
|---|
| 48 |
|
|---|
| 49 | index_translations = rhs.Indexing();
|
|---|
| 50 | level = rhs.Level();
|
|---|
| 51 |
|
|---|
| 52 | delete [] grid;
|
|---|
| 53 | grid = new vmg_float[local.SizeTotal().Product()];
|
|---|
| 54 |
|
|---|
| 55 | SetGrid(rhs);
|
|---|
| 56 |
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | return *this;
|
|---|
| 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 | {
|
|---|
| 70 | for (Grid::iterator iter=Iterators().Local().Begin(); iter!=Iterators().Local().End(); ++iter)
|
|---|
| 71 | (*this)(*iter) = 0.0;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | void Grid::ClearHalo()
|
|---|
| 75 | {
|
|---|
| 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 |
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | void Grid::ClearBoundary()
|
|---|
| 91 | {
|
|---|
| 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 |
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | void Grid::SetAverageToZero()
|
|---|
| 107 | {
|
|---|
| 108 | Grid::iterator iter;
|
|---|
| 109 | vmg_float avg = 0.0;
|
|---|
| 110 |
|
|---|
| 111 | for (iter = Iterators().Local().Begin(); iter != Iterators().Local().End(); ++iter)
|
|---|
| 112 | avg += GetVal(*iter);
|
|---|
| 113 |
|
|---|
| 114 | avg /= Local().Size().Product();
|
|---|
| 115 |
|
|---|
| 116 | for (iter = Iterators().Local().Begin(); iter != Iterators().Local().End(); ++iter)
|
|---|
| 117 | (*this)(*iter) -= avg;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | void Grid::ForceDiscreteCompatibilityCondition()
|
|---|
| 121 | {
|
|---|
| 122 | Grid::iterator iter;
|
|---|
| 123 | vmg_float val = 0.0;
|
|---|
| 124 |
|
|---|
| 125 | for (iter = Iterators().Local().Begin(); iter != Iterators().Local().End(); ++iter)
|
|---|
| 126 | val += GetVal(*iter);
|
|---|
| 127 |
|
|---|
| 128 | val = MG::GetComm()->GlobalSum(val);
|
|---|
| 129 |
|
|---|
| 130 | if (fabs(val) > Global().SizeGlobal().Product() * std::numeric_limits<vmg_float>::epsilon()) {
|
|---|
| 131 |
|
|---|
| 132 | #ifdef DEBUG_OUTPUT
|
|---|
| 133 | MG::GetComm()->PrintStringOnce("WARNING: Right hand side does not satisfy the compatibility condition.");
|
|---|
| 134 | #endif
|
|---|
| 135 |
|
|---|
| 136 | val *= MeshWidth() * MeshWidth();
|
|---|
| 137 | for (iter = Iterators().Local().Begin(); iter != Iterators().Local().End(); ++iter)
|
|---|
| 138 | (*this)(*iter) -= val;
|
|---|
| 139 |
|
|---|
| 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
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | void Grid::SetGrid(const Grid& rhs)
|
|---|
| 152 | {
|
|---|
| 153 | #ifdef DEBUG
|
|---|
| 154 | IsCompatible(rhs);
|
|---|
| 155 | #endif
|
|---|
| 156 |
|
|---|
| 157 | for (Grid::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
|
|---|
| 158 | (*this)(*iter) = rhs.GetVal(*iter);
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | void Grid::SetBoundary(const Grid& rhs)
|
|---|
| 162 | {
|
|---|
| 163 | #ifdef DEBUG
|
|---|
| 164 | IsCompatible(rhs);
|
|---|
| 165 | #endif
|
|---|
| 166 | Grid::iterator iter;
|
|---|
| 167 |
|
|---|
| 168 | for (int i=0; i<3; ++i) {
|
|---|
| 169 |
|
|---|
| 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 | }
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | void Grid::AddGrid(const Grid& rhs)
|
|---|
| 180 | {
|
|---|
| 181 | #ifdef DEBUG_MATRIX_CHECKS
|
|---|
| 182 | IsCompatible(rhs);
|
|---|
| 183 | #endif
|
|---|
| 184 |
|
|---|
| 185 | for (Grid::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
|
|---|
| 186 | (*this)(*iter) += rhs.GetVal(*iter);
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | void Grid::SubtractGrid(const Grid& rhs)
|
|---|
| 190 | {
|
|---|
| 191 | #ifdef DEBUG_MATRIX_CHECKS
|
|---|
| 192 | IsCompatible(rhs);
|
|---|
| 193 | #endif
|
|---|
| 194 |
|
|---|
| 195 | for (Grid::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
|
|---|
| 196 | (*this)(*iter) -= rhs.GetVal(*iter);
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | void Grid::MultiplyScalar(const vmg_float& scalar)
|
|---|
| 200 | {
|
|---|
| 201 | for (Grid::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
|
|---|
| 202 | (*this)(*iter) *= scalar;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | void Grid::ApplyStencil(const Stencil& stencil)
|
|---|
| 206 | {
|
|---|
| 207 | Grid::iterator grid_iter;
|
|---|
| 208 | Stencil::iterator stencil_iter;
|
|---|
| 209 | TempGrid *temp = MG::GetTempGrid();
|
|---|
| 210 |
|
|---|
| 211 | temp->SetProperties(*this);
|
|---|
| 212 | temp->SetGrid(*this);
|
|---|
| 213 | MG::GetComm()->CommToGhosts(*temp);
|
|---|
| 214 |
|
|---|
| 215 | for (grid_iter = Iterators().Local().Begin(); grid_iter != Iterators().Local().End(); ++grid_iter) {
|
|---|
| 216 |
|
|---|
| 217 | (*this)(*grid_iter) = stencil.GetDiag() * temp->GetVal(*grid_iter);
|
|---|
| 218 |
|
|---|
| 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());
|
|---|
| 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 |
|
|---|
| 233 | eq &= Helper::IsEq(Global().BeginFinest(), rhs.Global().BeginFinest(), "Global().BeginFinest");
|
|---|
| 234 | eq &= Helper::IsEq(Global().EndFinest(), rhs.Global().EndFinest(), "Global().EndFinest");
|
|---|
| 235 | eq &= Helper::IsEq(Global().BeginLocal(), rhs.Global().BeginLocal(), "Global().BeginLocal");
|
|---|
| 236 | eq &= Helper::IsEq(Global().EndLocal(), rhs.Global().EndLocal(), "Global().EndLocal");
|
|---|
| 237 | eq &= Helper::IsEq(Global().SizeGlobal(), rhs.Global().SizeGlobal(), "Global().SizeGlobal");
|
|---|
| 238 | eq &= Helper::IsEq(Global().SizeLocal(), rhs.Global().SizeLocal(), "Global().SizeLocal");
|
|---|
| 239 |
|
|---|
| 240 | eq &= Helper::IsEq(Local().HaloBegin1(), rhs.Local().HaloBegin1(), "Local().HaloBegin1");
|
|---|
| 241 | eq &= Helper::IsEq(Local().HaloBegin2(), rhs.Local().HaloBegin2(), "Local().HaloBegin2");
|
|---|
| 242 | eq &= Helper::IsEq(Local().HaloEnd1(), rhs.Local().HaloEnd1(), "Local().HaloEnd1");
|
|---|
| 243 | eq &= Helper::IsEq(Local().HaloEnd2(), rhs.Local().HaloEnd2(), "Local().HaloEnd2");
|
|---|
| 244 |
|
|---|
| 245 | eq &= Helper::IsEq(MeshWidth(), rhs.MeshWidth(), "MeshWidth");
|
|---|
| 246 |
|
|---|
| 247 | return eq;
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | bool Grid::IsConsistent() const
|
|---|
| 251 | {
|
|---|
| 252 | bool consistent = true;
|
|---|
| 253 |
|
|---|
| 254 | for (Grid::iterator iter=Iterators().CompleteGrid().Begin(); iter!=Iterators().CompleteGrid().End(); ++iter)
|
|---|
| 255 | consistent &= Helper::CheckNumber(GetVal(*iter));
|
|---|
| 256 |
|
|---|
| 257 | return consistent;
|
|---|
| 258 | }
|
|---|