source: src/grid/grid.cpp@ 2d4211

Last change on this file since 2d4211 was fcf7f6, checked in by Julian Iseringhausen <isering@…>, 14 years ago

vmg: Added license files and headers (GPLv3).

git-svn-id: https://svn.version.fz-juelich.de/scafacos/trunk@1812 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 for (iter = Iterators().Local().Begin(); iter != Iterators().Local().End(); ++iter)
138 (*this)(*iter) -= avg;
139}
140
141void Grid::ForceDiscreteCompatibilityCondition()
142{
143 Grid::iterator iter;
144 vmg_float val = 0.0;
145
146 for (iter = Iterators().Local().Begin(); iter != Iterators().Local().End(); ++iter)
147 val += GetVal(*iter);
148
149 val = MG::GetComm()->GlobalSum(val) / Global().GlobalSize().Product();
150
151 if (std::abs(val) > std::numeric_limits<vmg_float>::epsilon()) {
152
153#ifdef DEBUG_OUTPUT
154 MG::GetComm()->PrintStringOnce("WARNING: Right hand side does not satisfy the compatibility condition.");
155#endif
156
157 for (iter = Iterators().Local().Begin(); iter != Iterators().Local().End(); ++iter)
158 (*this)(*iter) -= val;
159
160#ifdef DEBUG_OUTPUT
161 val = 0.0;
162 for (iter = Iterators().Local().Begin(); iter != Iterators().Local().End(); ++iter)
163 val += GetVal(*iter);
164 val = MG::GetComm()->GlobalSumRoot(val);
165 MG::GetComm()->PrintStringOnce("Sum of grid charges after forcing the discrete compatibility condition: %e", val);
166#endif
167 }
168
169}
170
171void Grid::SetGrid(const Grid& rhs)
172{
173#ifdef DEBUG
174 IsCompatible(rhs);
175#endif
176
177 std::memcpy(grid, rhs.grid, local.SizeTotal().Product()*sizeof(vmg_float));
178}
179
180void Grid::SetBoundary(const Grid& rhs)
181{
182#ifdef DEBUG
183 IsCompatible(rhs);
184#endif
185 Grid::iterator iter;
186
187 for (int i=0; i<3; ++i) {
188
189 for (iter = Iterators().Boundary1()[i].Begin(); iter != Iterators().Boundary1()[i].End(); ++iter)
190 (*this)(*iter) = rhs.GetVal(*iter);
191
192 for (iter = Iterators().Boundary2()[i].Begin(); iter != Iterators().Boundary2()[i].End(); ++iter)
193 (*this)(*iter) = rhs.GetVal(*iter);
194
195 }
196}
197
198void Grid::AddGrid(const Grid& rhs)
199{
200#ifdef DEBUG_MATRIX_CHECKS
201 IsCompatible(rhs);
202#endif
203
204 for (Grid::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
205 (*this)(*iter) += rhs.GetVal(*iter);
206}
207
208void Grid::SubtractGrid(const Grid& rhs)
209{
210#ifdef DEBUG_MATRIX_CHECKS
211 IsCompatible(rhs);
212#endif
213
214 for (Grid::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
215 (*this)(*iter) -= rhs.GetVal(*iter);
216}
217
218void Grid::MultiplyScalar(const vmg_float& scalar)
219{
220 for (Grid::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
221 (*this)(*iter) *= scalar;
222}
223
224void Grid::ApplyStencil(const Stencil& stencil)
225{
226 Grid::iterator grid_iter;
227 Stencil::iterator stencil_iter;
228 TempGrid *temp = MG::GetTempGrid();
229
230 temp->SetProperties(*this);
231 temp->SetGrid(*this);
232 MG::GetComm()->CommToGhosts(*temp);
233
234 for (grid_iter = Iterators().Local().Begin(); grid_iter != Iterators().Local().End(); ++grid_iter) {
235
236 (*this)(*grid_iter) = stencil.GetDiag() * temp->GetVal(*grid_iter);
237
238 for (stencil_iter=stencil.begin(); stencil_iter!=stencil.end(); ++stencil_iter)
239 (*this)(*grid_iter) += stencil_iter->Val() * temp->GetVal(*grid_iter + stencil_iter->Disp());
240 }
241}
242
243bool Grid::IsCompatible(const Grid& rhs) const
244{
245 bool eq = true;
246
247 eq &= Helper::IsEq(Local().Begin(), rhs.Local().Begin(), "Local().Begin");
248 eq &= Helper::IsEq(Local().End(), rhs.Local().End(), "Local().End");
249 eq &= Helper::IsEq(Local().Size(), rhs.Local().Size(), "Local().Size");
250 eq &= Helper::IsEq(Local().SizeTotal(), rhs.Local().SizeTotal(), "Local().SizeTotal");
251
252 eq &= Helper::IsEq(Global().LocalBegin(), rhs.Global().LocalBegin(), "Global().LocalBegin");
253 eq &= Helper::IsEq(Global().LocalEnd(), rhs.Global().LocalEnd(), "Global().LocalEnd");
254 eq &= Helper::IsEq(Global().GlobalSize(), rhs.Global().GlobalSize(), "Global().GlobalSize");
255 eq &= Helper::IsEq(Global().LocalSize(), rhs.Global().LocalSize(), "Global().LocalSize");
256
257 eq &= Helper::IsEq(Local().HaloBegin1(), rhs.Local().HaloBegin1(), "Local().HaloBegin1");
258 eq &= Helper::IsEq(Local().HaloBegin2(), rhs.Local().HaloBegin2(), "Local().HaloBegin2");
259 eq &= Helper::IsEq(Local().HaloEnd1(), rhs.Local().HaloEnd1(), "Local().HaloEnd1");
260 eq &= Helper::IsEq(Local().HaloEnd2(), rhs.Local().HaloEnd2(), "Local().HaloEnd2");
261
262 eq &= Helper::IsEq(Extent().MeshWidth(), rhs.Extent().MeshWidth(), "MeshWidth");
263
264 return eq;
265}
266
267bool Grid::IsConsistent() const
268{
269 bool consistent = true;
270
271 for (Grid::iterator iter=Iterators().CompleteGrid().Begin(); iter!=Iterators().CompleteGrid().End(); ++iter)
272 consistent &= Helper::CheckNumber(GetVal(*iter));
273
274 return consistent;
275}
Note: See TracBrowser for help on using the repository browser.