| 1 | /**
|
|---|
| 2 | * @file tempgrid.cpp
|
|---|
| 3 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
|
|---|
| 4 | * @date Mon Apr 18 12:55:05 2011
|
|---|
| 5 | *
|
|---|
| 6 | * @brief VMG::TempGrid
|
|---|
| 7 | *
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | #ifdef HAVE_CONFIG_H
|
|---|
| 11 | #include <config.h>
|
|---|
| 12 | #endif
|
|---|
| 13 |
|
|---|
| 14 | #include "base/discretization.hpp"
|
|---|
| 15 | #include "base/stencil.hpp"
|
|---|
| 16 | #include "comm/comm.hpp"
|
|---|
| 17 | #include "grid/grid_index_translations.hpp"
|
|---|
| 18 | #include "grid/tempgrid.hpp"
|
|---|
| 19 | #include "interface/interface.hpp"
|
|---|
| 20 | #include "mg.hpp"
|
|---|
| 21 |
|
|---|
| 22 | using namespace VMG;
|
|---|
| 23 |
|
|---|
| 24 | void TempGrid::SetProperties(const Grid& rhs)
|
|---|
| 25 | {
|
|---|
| 26 | local = rhs.Local();
|
|---|
| 27 | global = rhs.Global();
|
|---|
| 28 | extent = rhs.Extent();
|
|---|
| 29 |
|
|---|
| 30 | iterators.SetSubgrids(rhs.Local());
|
|---|
| 31 |
|
|---|
| 32 | level = rhs.Level();
|
|---|
| 33 |
|
|---|
| 34 | Allocate();
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | void TempGrid::SetProperties(const GlobalIndices& global_, const LocalIndices& local_, const SpatialExtent& extent_)
|
|---|
| 38 | {
|
|---|
| 39 | local = local_;
|
|---|
| 40 | global = global_;
|
|---|
| 41 | extent = extent_;
|
|---|
| 42 |
|
|---|
| 43 | iterators.SetSubgrids(local_);
|
|---|
| 44 |
|
|---|
| 45 | Allocate();
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | void TempGrid::SetPropertiesToGlobalCoarseGrid()
|
|---|
| 49 | {
|
|---|
| 50 | Interface* interface = MG::GetInterface();
|
|---|
| 51 |
|
|---|
| 52 | global = interface->Global().back();
|
|---|
| 53 | extent = interface->Extent().back();
|
|---|
| 54 |
|
|---|
| 55 | for (int i=0; i<3; ++i) {
|
|---|
| 56 |
|
|---|
| 57 | if (interface->BoundaryConditions()[i] == Dirichlet ||
|
|---|
| 58 | interface->BoundaryConditions()[i] == Open) {
|
|---|
| 59 |
|
|---|
| 60 | local.Size()[i] = global.SizeGlobal()[i] - 2;
|
|---|
| 61 | local.SizeTotal()[i] = global.SizeGlobal()[i];
|
|---|
| 62 | local.Begin()[i] = 1;
|
|---|
| 63 | local.End()[i] = local.SizeTotal()[i] - 1;
|
|---|
| 64 | local.HaloBegin1()[i] = 0;
|
|---|
| 65 | local.HaloEnd1()[i] = 0;
|
|---|
| 66 | local.HaloBegin2()[i] = 0;
|
|---|
| 67 | local.HaloEnd2()[i] = 0;
|
|---|
| 68 | local.BoundaryBegin1()[i] = 0;
|
|---|
| 69 | local.BoundaryEnd1()[i] = 1;
|
|---|
| 70 | local.BoundaryBegin2()[i] = local.SizeTotal()[i] - 1;
|
|---|
| 71 | local.BoundaryEnd2()[i] = local.SizeTotal()[i];
|
|---|
| 72 |
|
|---|
| 73 | }else if (interface->BoundaryConditions()[i] == Periodic) {
|
|---|
| 74 |
|
|---|
| 75 | local.Size()[i] = global.SizeGlobal()[i];
|
|---|
| 76 | local.SizeTotal()[i] = global.SizeGlobal()[i];
|
|---|
| 77 | local.Begin()[i] = 0;
|
|---|
| 78 | local.End()[i] = global.SizeGlobal()[i];
|
|---|
| 79 | local.HaloBegin1()[i] = 0;
|
|---|
| 80 | local.HaloEnd1()[i] = 0;
|
|---|
| 81 | local.HaloBegin2()[i] = 0;
|
|---|
| 82 | local.HaloEnd2()[i] = 0;
|
|---|
| 83 | local.BoundaryBegin1()[i] = 0;
|
|---|
| 84 | local.BoundaryEnd1()[i] = 0;
|
|---|
| 85 | local.BoundaryBegin2()[i] = 0;
|
|---|
| 86 | local.BoundaryEnd2()[i] = 0;
|
|---|
| 87 |
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | level = interface->MinLevel();
|
|---|
| 93 |
|
|---|
| 94 | iterators.SetSubgrids(local);
|
|---|
| 95 |
|
|---|
| 96 | Allocate();
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | void TempGrid::SetPropertiesToFiner(const Grid& grid, const Boundary& boundary)
|
|---|
| 100 | {
|
|---|
| 101 | assert(grid.Father() != NULL);
|
|---|
| 102 | assert(grid.Level() < grid.Father()->MaxLevel());
|
|---|
| 103 |
|
|---|
| 104 | const Index off = GridIndexTranslations::EndOffset(boundary);
|
|---|
| 105 |
|
|---|
| 106 | this->Global().BeginFinest() = grid.Global().BeginFinest();
|
|---|
| 107 | this->Global().EndFinest() = grid.Global().EndFinest();
|
|---|
| 108 | this->Global().SizeFinest() = grid.Global().SizeFinest();
|
|---|
| 109 | this->Global().SizeGlobal() = 2 * (grid.Global().SizeGlobal()-off) + off;
|
|---|
| 110 | this->Global().BeginLocal() = grid.Global().BeginLocal();
|
|---|
| 111 | this->Global().EndLocal() = grid.Global().EndLocal();
|
|---|
| 112 |
|
|---|
| 113 | GridIndexTranslations::CoarseToFine(this->Global().BeginLocal(), this->Global().EndLocal(), this->Global().SizeGlobal());
|
|---|
| 114 |
|
|---|
| 115 | this->Global().SizeLocal() = this->Global().EndLocal() - this->Global().BeginLocal();
|
|---|
| 116 | this->Global().BoundaryType() = (*(grid.Father()))(grid.Level()+1).Global().BoundaryType();
|
|---|
| 117 |
|
|---|
| 118 | this->Local().Size() = this->Global().SizeLocal();
|
|---|
| 119 | this->Local().SizeTotal() = this->Global().SizeLocal();
|
|---|
| 120 | this->Local().Begin() = 0;
|
|---|
| 121 | this->Local().End() = this->Global().SizeLocal();
|
|---|
| 122 |
|
|---|
| 123 | for (int i=0; i<3; ++i) {
|
|---|
| 124 |
|
|---|
| 125 | if (grid.Local().HaloEnd1()[i] - grid.Local().HaloBegin1()[i] > 0) {
|
|---|
| 126 | this->Local().SizeTotal()[i] += 1;
|
|---|
| 127 | this->Local().Begin()[i] += 1;
|
|---|
| 128 | this->Local().End()[i] += 1;
|
|---|
| 129 | this->Local().HaloBegin1()[i] = 0;
|
|---|
| 130 | this->Local().HaloEnd1()[i] = 1;
|
|---|
| 131 | }else {
|
|---|
| 132 | this->Local().HaloBegin1()[i] = 0;
|
|---|
| 133 | this->Local().HaloEnd1()[i] = 0;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | if (grid.Local().BoundaryEnd1()[i] - grid.Local().BoundaryBegin1()[i] > 0) {
|
|---|
| 137 | this->Local().Size()[i] -= 1;
|
|---|
| 138 | this->Local().Begin()[i] += 1;
|
|---|
| 139 | this->Local().BoundaryBegin1()[i] = 0;
|
|---|
| 140 | this->Local().BoundaryEnd1()[i] = 1;
|
|---|
| 141 | }else {
|
|---|
| 142 | this->Local().BoundaryBegin1()[i] = 0;
|
|---|
| 143 | this->Local().BoundaryEnd1()[i] = 0;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | if (grid.Local().HaloEnd2()[i] - grid.Local().HaloBegin2()[i] > 0) {
|
|---|
| 147 | this->Local().SizeTotal()[i] += 1;
|
|---|
| 148 | this->Local().HaloBegin2()[i] = this->Local().End()[i];
|
|---|
| 149 | this->Local().HaloEnd2()[i] = this->Local().End()[i] + 1;
|
|---|
| 150 | }else {
|
|---|
| 151 | this->Local().HaloBegin2()[i] = 0;
|
|---|
| 152 | this->Local().HaloEnd2()[i] = 0;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | if (grid.Local().BoundaryEnd2()[i] - grid.Local().BoundaryBegin2()[i] > 0) {
|
|---|
| 156 | this->Local().Size()[i] -= 1;
|
|---|
| 157 | this->Local().End()[i] -= 1;
|
|---|
| 158 | this->Local().BoundaryBegin2()[i] = this->Local().End()[i];
|
|---|
| 159 | this->Local().BoundaryEnd2()[i] = this->Local().End()[i]+1;
|
|---|
| 160 | }else {
|
|---|
| 161 | this->Local().BoundaryBegin2()[i] = 0;
|
|---|
| 162 | this->Local().BoundaryEnd2()[i] = 0;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | this->Local().AlignmentBegin() = this->Local().Begin();
|
|---|
| 168 | this->Local().AlignmentEnd() = this->local.End();
|
|---|
| 169 |
|
|---|
| 170 | this->Extent().Size() = grid.Extent().Size();
|
|---|
| 171 | this->Extent().SizeFactor() = grid.Extent().SizeFactor();
|
|---|
| 172 | this->Extent().Begin() = grid.Extent().Begin();
|
|---|
| 173 | this->Extent().End() = grid.Extent().End();
|
|---|
| 174 | this->Extent().MeshWidth() = 0.5 * grid.Extent().MeshWidth();
|
|---|
| 175 |
|
|---|
| 176 | this->level = grid.Level() + 1;
|
|---|
| 177 |
|
|---|
| 178 | iterators.SetSubgrids(local);
|
|---|
| 179 |
|
|---|
| 180 | Allocate();
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | void TempGrid::SetPropertiesToCoarser(const Grid& grid, const Boundary& boundary)
|
|---|
| 184 | {
|
|---|
| 185 | assert(grid.Father() != NULL);
|
|---|
| 186 | assert(grid.Level() > grid.Father()->MinLevel());
|
|---|
| 187 |
|
|---|
| 188 | const Index off = GridIndexTranslations::EndOffset(boundary);
|
|---|
| 189 |
|
|---|
| 190 | this->level = grid.Level() - 1;
|
|---|
| 191 |
|
|---|
| 192 | this->Global().BeginFinest() = grid.Global().BeginFinest();
|
|---|
| 193 | this->Global().EndFinest() = grid.Global().EndFinest();
|
|---|
| 194 | this->Global().SizeFinest() = grid.Global().SizeFinest();
|
|---|
| 195 |
|
|---|
| 196 | this->Global().BeginLocal() = grid.Global().BeginLocal();
|
|---|
| 197 | this->Global().EndLocal() = grid.Global().EndLocal();
|
|---|
| 198 |
|
|---|
| 199 | GridIndexTranslations::FineToCoarse(this->Global().BeginLocal(), this->Global().EndLocal());
|
|---|
| 200 |
|
|---|
| 201 | this->Global().SizeLocal() = this->Global().EndLocal() - this->Global().BeginLocal();
|
|---|
| 202 | this->Global().SizeGlobal() = (grid.Global().SizeGlobal()-off)/2 + off;
|
|---|
| 203 | this->Global().BoundaryType() = (*(grid.Father()))(grid.Level()-1).Global().BoundaryType();
|
|---|
| 204 |
|
|---|
| 205 | this->Local().SizeTotal() = this->Global().SizeLocal();
|
|---|
| 206 | this->Local().Size() = this->Global().SizeLocal();
|
|---|
| 207 | this->Local().Begin() = 0;
|
|---|
| 208 | this->Local().End() = this->Global().SizeLocal();
|
|---|
| 209 |
|
|---|
| 210 | for (int i=0; i<3; ++i) {
|
|---|
| 211 |
|
|---|
| 212 | if (grid.Local().HaloEnd1()[i] - grid.Local().HaloBegin1()[i] > 0) {
|
|---|
| 213 | this->Local().SizeTotal()[i] += 1;
|
|---|
| 214 | this->Local().Begin()[i] += 1;
|
|---|
| 215 | this->Local().End()[i] += 1;
|
|---|
| 216 | this->Local().HaloBegin1()[i] = 0;
|
|---|
| 217 | this->Local().HaloEnd1()[i] = 1;
|
|---|
| 218 | }else {
|
|---|
| 219 | this->Local().HaloBegin1()[i] = 0;
|
|---|
| 220 | this->Local().HaloEnd1()[i] = 0;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | if (grid.Local().BoundaryEnd1()[i] - grid.Local().BoundaryBegin1()[i] > 0) {
|
|---|
| 224 | this->Local().Size()[i] -= 1;
|
|---|
| 225 | this->Local().Begin()[i] += 1;
|
|---|
| 226 | this->Local().BoundaryBegin1()[i] = 0;
|
|---|
| 227 | this->Local().BoundaryEnd1()[i] = 1;
|
|---|
| 228 | }else {
|
|---|
| 229 | this->Local().BoundaryBegin1()[i] = 0;
|
|---|
| 230 | this->Local().BoundaryEnd1()[i] = 0;
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | if (grid.Local().HaloEnd2()[i] - grid.Local().HaloBegin2()[i] > 0) {
|
|---|
| 234 | this->Local().SizeTotal()[i] += 1;
|
|---|
| 235 | this->Local().HaloBegin2()[i] = this->Local().End()[i];
|
|---|
| 236 | this->Local().HaloEnd2()[i] = this->Local().End()[i] + 1;
|
|---|
| 237 | }else {
|
|---|
| 238 | this->Local().HaloBegin2()[i] = 0;
|
|---|
| 239 | this->Local().HaloEnd2()[i] = 0;
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | if (grid.Local().BoundaryEnd2()[i] - grid.Local().BoundaryBegin2()[i] > 0) {
|
|---|
| 243 | this->Local().Size()[i] -= 1;
|
|---|
| 244 | this->Local().End()[i] -= 1;
|
|---|
| 245 | this->Local().BoundaryBegin2()[i] = this->Local().End()[i];
|
|---|
| 246 | this->Local().BoundaryEnd2()[i] = this->Local().End()[i]+1;
|
|---|
| 247 | }else {
|
|---|
| 248 | this->Local().BoundaryBegin2()[i] = 0;
|
|---|
| 249 | this->Local().BoundaryEnd2()[i] = 0;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | this->Local().AlignmentBegin() = this->Local().Begin();
|
|---|
| 255 | this->Local().AlignmentEnd() = this->local.End();
|
|---|
| 256 |
|
|---|
| 257 | this->Extent().Size() = grid.Extent().Size();
|
|---|
| 258 | this->Extent().SizeFactor() = grid.Extent().SizeFactor();
|
|---|
| 259 | this->Extent().Begin() = grid.Extent().Begin();
|
|---|
| 260 | this->Extent().End() = grid.Extent().End();
|
|---|
| 261 | this->Extent().MeshWidth() = 2.0 * grid.Extent().MeshWidth();
|
|---|
| 262 |
|
|---|
| 263 | iterators.SetSubgrids(local);
|
|---|
| 264 |
|
|---|
| 265 | Allocate();
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | void TempGrid::ImportFromResidual(Grid& sol, Grid& rhs)
|
|---|
| 269 | {
|
|---|
| 270 | #ifdef DEBUG
|
|---|
| 271 | this->IsCompatible(sol);
|
|---|
| 272 | this->IsCompatible(rhs);
|
|---|
| 273 | #endif
|
|---|
| 274 |
|
|---|
| 275 | Grid::iterator grid_iter;
|
|---|
| 276 | Stencil::iterator stencil_iter;
|
|---|
| 277 | vmg_float residual;
|
|---|
| 278 | Index i;
|
|---|
| 279 |
|
|---|
| 280 | const vmg_float prefactor = MG::GetDiscretization()->OperatorPrefactor(sol);
|
|---|
| 281 | const Stencil& A = MG::GetDiscretization()->GetStencil();
|
|---|
| 282 |
|
|---|
| 283 | this->Clear();
|
|---|
| 284 |
|
|---|
| 285 | MG::GetComm()->CommToGhosts(sol);
|
|---|
| 286 |
|
|---|
| 287 | for (grid_iter=Iterators().Local().Begin(); grid_iter!=Iterators().Local().End(); ++grid_iter) {
|
|---|
| 288 |
|
|---|
| 289 | residual = rhs.GetVal(*grid_iter) - prefactor * A.GetDiag() * sol.GetVal(*grid_iter);
|
|---|
| 290 |
|
|---|
| 291 | for (stencil_iter=A.begin(); stencil_iter!=A.end(); ++stencil_iter)
|
|---|
| 292 | residual -= prefactor * stencil_iter->Val() * sol.GetVal(*grid_iter + stencil_iter->Disp());
|
|---|
| 293 |
|
|---|
| 294 | (*this)(*grid_iter) = residual;
|
|---|
| 295 |
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | MG::GetComm()->CommToGhosts(*this);
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | void TempGrid::Allocate()
|
|---|
| 302 | {
|
|---|
| 303 | const int size = local.SizeTotal().Product();
|
|---|
| 304 |
|
|---|
| 305 | if (size > size_max) {
|
|---|
| 306 | size_max = size;
|
|---|
| 307 | delete [] grid;
|
|---|
| 308 | grid = new vmg_float[size];
|
|---|
| 309 | }
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | TempGrid::TempGrid()
|
|---|
| 313 | {
|
|---|
| 314 | size_max = 0;
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | TempGrid::~TempGrid()
|
|---|
| 318 | {
|
|---|
| 319 | }
|
|---|