source: src/grid/grid.cpp@ 5ba22b

Last change on this file since 5ba22b was a72216, checked in by Olaf Lenz <olenz@…>, 13 years ago

Fixed permissions.

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

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