source: src/grid/grid.cpp@ 6f05224

Last change on this file since 6f05224 was 4571da, checked in by Julian Iseringhausen <isering@…>, 14 years ago

vmg: Implement fourth-order discretization of the Poisson equation.

git-svn-id: https://svn.version.fz-juelich.de/scafacos/trunk@1762 5161e1c8-67bf-11de-9fd5-51895aff932f

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