source: src/grid/grid.cpp@ a72216

Last change on this file since a72216 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
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
[185795]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;
[48b662]140}
141
142void Grid::ForceDiscreteCompatibilityCondition()
143{
[dfed1c]144 Grid::iterator iter;
[48b662]145 vmg_float val = 0.0;
146
[dfed1c]147 for (iter = Iterators().Local().Begin(); iter != Iterators().Local().End(); ++iter)
148 val += GetVal(*iter);
149
[4571da]150 val = MG::GetComm()->GlobalSum(val) / Global().GlobalSize().Product();
[48b662]151
[4571da]152 if (std::abs(val) > std::numeric_limits<vmg_float>::epsilon()) {
[48b662]153
[dfed1c]154#ifdef DEBUG_OUTPUT
155 MG::GetComm()->PrintStringOnce("WARNING: Right hand side does not satisfy the compatibility condition.");
[48b662]156#endif
157
[dfed1c]158 for (iter = Iterators().Local().Begin(); iter != Iterators().Local().End(); ++iter)
159 (*this)(*iter) -= val;
[48b662]160
[dfed1c]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
[48b662]168 }
[dfed1c]169
[48b662]170}
171
172void Grid::SetGrid(const Grid& rhs)
173{
174#ifdef DEBUG
175 IsCompatible(rhs);
176#endif
177
[4571da]178 std::memcpy(grid, rhs.grid, local.SizeTotal().Product()*sizeof(vmg_float));
[48b662]179}
180
181void Grid::SetBoundary(const Grid& rhs)
182{
183#ifdef DEBUG
184 IsCompatible(rhs);
185#endif
[dfed1c]186 Grid::iterator iter;
187
188 for (int i=0; i<3; ++i) {
[48b662]189
[dfed1c]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 }
[48b662]197}
198
199void Grid::AddGrid(const Grid& rhs)
200{
[dfed1c]201#ifdef DEBUG_MATRIX_CHECKS
[48b662]202 IsCompatible(rhs);
203#endif
204
[dfed1c]205 for (Grid::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
206 (*this)(*iter) += rhs.GetVal(*iter);
[48b662]207}
208
209void Grid::SubtractGrid(const Grid& rhs)
210{
[dfed1c]211#ifdef DEBUG_MATRIX_CHECKS
[48b662]212 IsCompatible(rhs);
213#endif
214
[dfed1c]215 for (Grid::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
216 (*this)(*iter) -= rhs.GetVal(*iter);
[48b662]217}
218
219void Grid::MultiplyScalar(const vmg_float& scalar)
220{
[dfed1c]221 for (Grid::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
222 (*this)(*iter) *= scalar;
[48b662]223}
224
225void Grid::ApplyStencil(const Stencil& stencil)
226{
[dfed1c]227 Grid::iterator grid_iter;
228 Stencil::iterator stencil_iter;
[48b662]229 TempGrid *temp = MG::GetTempGrid();
230
231 temp->SetProperties(*this);
232 temp->SetGrid(*this);
233 MG::GetComm()->CommToGhosts(*temp);
234
[dfed1c]235 for (grid_iter = Iterators().Local().Begin(); grid_iter != Iterators().Local().End(); ++grid_iter) {
[48b662]236
[dfed1c]237 (*this)(*grid_iter) = stencil.GetDiag() * temp->GetVal(*grid_iter);
[48b662]238
[dfed1c]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());
[48b662]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
[ac6d04]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");
[48b662]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
[ac6d04]263 eq &= Helper::IsEq(Extent().MeshWidth(), rhs.Extent().MeshWidth(), "MeshWidth");
[48b662]264
265 return eq;
266}
267
268bool Grid::IsConsistent() const
269{
270 bool consistent = true;
271
[ef94e7]272 for (Grid::iterator iter=Iterators().Local().Begin(); iter!=Iterators().Local().End(); ++iter)
[dfed1c]273 consistent &= Helper::CheckNumber(GetVal(*iter));
[48b662]274
275 return consistent;
276}
Note: See TracBrowser for help on using the repository browser.