source: src/grid/grid.cpp@ 0260d3

Last change on this file since 0260d3 was ef94e7, checked in by Julian Iseringhausen <isering@…>, 13 years ago

vmg: Fix force calculation.

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

  • Property mode set to 100644
File size: 7.3 KB
RevLine 
[fcf7f6]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
[48b662]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>
[dfed1c]33#include <cmath>
[48b662]34#include <limits>
35
36#include "base/helper.hpp"
[dfed1c]37#include "base/stencil.hpp"
[48b662]38#include "comm/comm.hpp"
39#include "grid/grid.hpp"
40#include "grid/tempgrid.hpp"
41#include "mg.hpp"
42
43using namespace VMG;
44
[dfed1c]45void Grid::InitGrid()
[48b662]46{
[dfed1c]47 grid = new vmg_float[local.SizeTotal().Product()];
48}
[48b662]49
[dfed1c]50Grid::~Grid()
51{
52 delete [] grid;
53}
[48b662]54
[dfed1c]55Grid& Grid::operator=(const Grid& rhs)
56{
57 if (this != &rhs) {
[48b662]58
[dfed1c]59 global = rhs.Global();
60 local = rhs.Local();
61 extent = rhs.Extent();
62 iterators = rhs.Iterators();
63 father = rhs.Father();
[48b662]64
[dfed1c]65 index_translations = rhs.Indexing();
66 level = rhs.Level();
[48b662]67
[dfed1c]68 delete [] grid;
69 grid = new vmg_float[local.SizeTotal().Product()];
[48b662]70
[dfed1c]71 SetGrid(rhs);
[48b662]72
73 }
74
[dfed1c]75 return *this;
[48b662]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{
[dfed1c]86 for (Grid::iterator iter=Iterators().Local().Begin(); iter!=Iterators().Local().End(); ++iter)
87 (*this)(*iter) = 0.0;
[48b662]88}
89
90void Grid::ClearHalo()
91{
[dfed1c]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
[48b662]104}
105
106void Grid::ClearBoundary()
107{
[dfed1c]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
[48b662]120}
121
122void Grid::SetAverageToZero()
123{
[dfed1c]124 Grid::iterator iter;
[48b662]125 vmg_float avg = 0.0;
126
[dfed1c]127 for (iter = Iterators().Local().Begin(); iter != Iterators().Local().End(); ++iter)
128 avg += GetVal(*iter);
[48b662]129
[4571da]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
[48b662]136
[dfed1c]137 for (iter = Iterators().Local().Begin(); iter != Iterators().Local().End(); ++iter)
138 (*this)(*iter) -= avg;
[48b662]139}
140
141void Grid::ForceDiscreteCompatibilityCondition()
142{
[dfed1c]143 Grid::iterator iter;
[48b662]144 vmg_float val = 0.0;
145
[dfed1c]146 for (iter = Iterators().Local().Begin(); iter != Iterators().Local().End(); ++iter)
147 val += GetVal(*iter);
148
[4571da]149 val = MG::GetComm()->GlobalSum(val) / Global().GlobalSize().Product();
[48b662]150
[4571da]151 if (std::abs(val) > std::numeric_limits<vmg_float>::epsilon()) {
[48b662]152
[dfed1c]153#ifdef DEBUG_OUTPUT
154 MG::GetComm()->PrintStringOnce("WARNING: Right hand side does not satisfy the compatibility condition.");
[48b662]155#endif
156
[dfed1c]157 for (iter = Iterators().Local().Begin(); iter != Iterators().Local().End(); ++iter)
158 (*this)(*iter) -= val;
[48b662]159
[dfed1c]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
[48b662]167 }
[dfed1c]168
[48b662]169}
170
171void Grid::SetGrid(const Grid& rhs)
172{
173#ifdef DEBUG
174 IsCompatible(rhs);
175#endif
176
[4571da]177 std::memcpy(grid, rhs.grid, local.SizeTotal().Product()*sizeof(vmg_float));
[48b662]178}
179
180void Grid::SetBoundary(const Grid& rhs)
181{
182#ifdef DEBUG
183 IsCompatible(rhs);
184#endif
[dfed1c]185 Grid::iterator iter;
186
187 for (int i=0; i<3; ++i) {
[48b662]188
[dfed1c]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 }
[48b662]196}
197
198void Grid::AddGrid(const Grid& rhs)
199{
[dfed1c]200#ifdef DEBUG_MATRIX_CHECKS
[48b662]201 IsCompatible(rhs);
202#endif
203
[dfed1c]204 for (Grid::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
205 (*this)(*iter) += rhs.GetVal(*iter);
[48b662]206}
207
208void Grid::SubtractGrid(const Grid& rhs)
209{
[dfed1c]210#ifdef DEBUG_MATRIX_CHECKS
[48b662]211 IsCompatible(rhs);
212#endif
213
[dfed1c]214 for (Grid::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
215 (*this)(*iter) -= rhs.GetVal(*iter);
[48b662]216}
217
218void Grid::MultiplyScalar(const vmg_float& scalar)
219{
[dfed1c]220 for (Grid::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
221 (*this)(*iter) *= scalar;
[48b662]222}
223
224void Grid::ApplyStencil(const Stencil& stencil)
225{
[dfed1c]226 Grid::iterator grid_iter;
227 Stencil::iterator stencil_iter;
[48b662]228 TempGrid *temp = MG::GetTempGrid();
229
230 temp->SetProperties(*this);
231 temp->SetGrid(*this);
232 MG::GetComm()->CommToGhosts(*temp);
233
[dfed1c]234 for (grid_iter = Iterators().Local().Begin(); grid_iter != Iterators().Local().End(); ++grid_iter) {
[48b662]235
[dfed1c]236 (*this)(*grid_iter) = stencil.GetDiag() * temp->GetVal(*grid_iter);
[48b662]237
[dfed1c]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());
[48b662]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
[ac6d04]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");
[48b662]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
[ac6d04]262 eq &= Helper::IsEq(Extent().MeshWidth(), rhs.Extent().MeshWidth(), "MeshWidth");
[48b662]263
264 return eq;
265}
266
267bool Grid::IsConsistent() const
268{
269 bool consistent = true;
270
[ef94e7]271 for (Grid::iterator iter=Iterators().Local().Begin(); iter!=Iterators().Local().End(); ++iter)
[dfed1c]272 consistent &= Helper::CheckNumber(GetVal(*iter));
[48b662]273
274 return consistent;
275}
Note: See TracBrowser for help on using the repository browser.