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

Last change on this file since 2fad0e0 was 48b662, checked in by Olaf Lenz <olenz@…>, 14 years ago

Moved files in scafacos_fcs one level up.

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

  • Property mode set to 100644
File size: 10.1 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>
15#include <limits>
16
17#include "base/helper.hpp"
18#include "comm/comm.hpp"
19#include "grid/grid.hpp"
20#include "grid/tempgrid.hpp"
21#include "mg.hpp"
22
23using namespace VMG;
24
25vmg_float Grid::correction;
26
27Grid::Grid(const Comm* comm, const Index& size, const vmg_float& width)
28{
29 grid = new vmg_float[size.Product()];
30
31 global.Size() = size;
32 global.Begin() = 0;
33 global.End() = size;
34
35 local.SizeTotal() = size;
36 local.HaloBegin1() = 0;
37 local.HaloBegin2() = 0;
38 local.HaloEnd1() = 0;
39 local.HaloEnd2() = 0;
40
41 extent.MeshWidth() = width;
42
43 if (comm->BoundaryConditions() == Dirichlet || comm->BoundaryConditions() == Quasiperiodic) {
44
45 local.Size() = size - 2;
46 local.Begin() = 1;
47 local.End() = size-1;
48 local.BoundaryBegin1() = 0;
49 local.BoundaryEnd1() = 1;
50 local.BoundaryBegin2() = size-1;
51 local.BoundaryEnd2() = size;
52
53 }else if (comm->BoundaryConditions() == Periodic) {
54
55 local.Size() = size;
56 local.Begin() = 0;
57 local.End() = 0;
58 local.BoundaryBegin1() = 0;
59 local.BoundaryEnd1() = 0;
60 local.BoundaryBegin2() = 0;
61 local.BoundaryEnd2() = 0;
62
63 }
64}
65
66void Grid::InitGrid()
67{
68 grid = new vmg_float[local.SizeTotal().Product()];
69}
70
71void Grid::Clear()
72{
73 for (int i=0; i<local.SizeTotal().Product(); ++i)
74 grid[i] = 0.0;
75}
76
77void Grid::ClearInner()
78{
79 Index i;
80
81 for (i.X()=local.Begin().X(); i.X()<local.End().X(); ++i.X())
82 for (i.Y()=local.Begin().Y(); i.Y()<local.End().Y(); ++i.Y())
83 for (i.Z()=local.Begin().Z(); i.Z()<local.End().Z(); ++i.Z())
84 (*this)(i) = 0.0;
85}
86
87void Grid::ClearHalo()
88{
89 for (int i=Local().HaloBegin1().X(); i<Local().HaloEnd1().X(); i++)
90 for (int j=0; j<Local().SizeTotal().Y(); j++)
91 for (int k=0; k<Local().SizeTotal().Z(); k++)
92 (*this)(i,j,k) = 0.0;
93
94 for (int i=Local().HaloBegin2().X(); i<Local().HaloEnd2().X(); i++)
95 for (int j=0; j<Local().SizeTotal().Y(); j++)
96 for (int k=0; k<Local().SizeTotal().Z(); k++)
97 (*this)(i,j,k) = 0.0;
98
99 for (int i=0; i<Local().SizeTotal().X(); i++)
100 for (int j=Local().HaloBegin1().Y(); j<Local().HaloEnd1().Y(); j++)
101 for (int k=0; k<Local().SizeTotal().Z(); k++)
102 (*this)(i,j,k) = 0.0;
103
104 for (int i=0; i<Local().SizeTotal().X(); i++)
105 for (int j=Local().HaloBegin2().Y(); j<Local().HaloEnd2().Y(); j++)
106 for (int k=0; k<Local().SizeTotal().Z(); k++)
107 (*this)(i,j,k) = 0.0;
108
109 for (int i=0; i<Local().SizeTotal().X(); i++)
110 for (int j=0; j<Local().SizeTotal().Y(); j++)
111 for (int k=Local().HaloBegin1().Z(); k<Local().HaloEnd1().Z(); k++)
112 (*this)(i,j,k) = 0.0;
113
114 for (int i=0; i<Local().SizeTotal().X(); i++)
115 for (int j=0; j<Local().SizeTotal().Y(); j++)
116 for (int k=Local().HaloBegin2().Z(); k<Local().HaloEnd2().Z(); k++)
117 (*this)(i,j,k) = 0.0;
118}
119
120void Grid::ClearBoundary()
121{
122 for (int i=Local().BoundaryBegin1().X(); i<Local().BoundaryEnd1().X(); i++)
123 for (int j=Local().Begin().Y(); j<Local().End().Y(); j++)
124 for (int k=Local().Begin().Z(); k<Local().End().Z(); k++)
125 (*this)(i,j,k) = 0.0;
126
127 for (int i=Local().BoundaryBegin2().X(); i<Local().BoundaryEnd2().X(); i++)
128 for (int j=Local().Begin().Y(); j<Local().End().Y(); j++)
129 for (int k=Local().Begin().Z(); k<Local().End().Z(); k++)
130 (*this)(i,j,k) = 0.0;
131
132 for (int i=0; i<Local().SizeTotal().X(); i++)
133 for (int j=Local().BoundaryBegin1().Y(); j<Local().BoundaryEnd1().Y(); j++)
134 for (int k=Local().Begin().Z(); k<Local().End().Z(); k++)
135 (*this)(i,j,k) = 0.0;
136
137 for (int i=0; i<Local().SizeTotal().X(); i++)
138 for (int j=Local().BoundaryBegin2().Y(); j<Local().BoundaryEnd2().Y(); j++)
139 for (int k=Local().Begin().Z(); k<Local().End().Z(); k++)
140 (*this)(i,j,k) = 0.0;
141
142 for (int i=0; i<Local().SizeTotal().X(); i++)
143 for (int j=0; j<Local().SizeTotal().Y(); j++)
144 for (int k=Local().BoundaryBegin1().Z(); k<Local().BoundaryEnd1().Z(); k++)
145 (*this)(i,j,k) = 0.0;
146
147 for (int i=0; i<Local().SizeTotal().X(); i++)
148 for (int j=0; j<Local().SizeTotal().Y(); j++)
149 for (int k=Local().BoundaryBegin2().Z(); k<Local().BoundaryEnd2().Z(); k++)
150 (*this)(i,j,k) = 0.0;
151}
152
153void Grid::SetAverageToZero()
154{
155 vmg_float avg = 0.0;
156
157 for (int i=Local().Begin().X(); i<Local().End().X(); i++)
158 for (int j=Local().Begin().Y(); j<Local().End().Y(); j++)
159 for (int k=Local().Begin().Z(); k<Local().End().Z(); k++)
160 avg += GetVal(i,j,k);
161
162 avg /= Local().Size().Product();
163
164 for (int i=Local().Begin().X(); i<Local().End().X(); i++)
165 for (int j=Local().Begin().Y(); j<Local().End().Y(); j++)
166 for (int k=Local().Begin().Z(); k<Local().End().Z(); k++)
167 (*this)(i,j,k) -= avg;
168}
169
170//TODO: Put a global communication step her
171void Grid::ForceDiscreteCompatibilityCondition()
172{
173 vmg_float val = 0.0;
174
175 for (int i=Local().Begin().X(); i<Local().End().X(); i++)
176 for (int j=Local().Begin().Y(); j<Local().End().Y(); j++)
177 for (int k=Local().Begin().Z(); k<Local().End().Z(); k++)
178 val += GetVal(i,j,k);
179
180 val = MG::GetComm()->AllReduce(val);
181
182 if (fabs(val) <= Global().Size().Product() * std::numeric_limits<vmg_float>::epsilon()) {
183#ifdef DEBUGVERBOSE
184 printf("Multigrid: Right hand side satisfies compatibility condition.\n");
185#endif
186 }else {
187 printf("Multigrid: Right hand side does NOT satisfy the compatibility condition.\n\tTotal sum: %e\nForcing compatibility condition now\n", val);
188
189 val *= MeshWidth() * MeshWidth() * MeshWidth();
190
191 for (int i=Local().Begin().X(); i<Local().End().X(); i++)
192 for (int j=Local().Begin().Y(); j<Local().End().Y(); j++)
193 for (int k=Local().Begin().Z(); k<Local().End().Z(); k++)
194 (*this)(i,j,k) -= val;
195 }
196}
197
198void Grid::SetGrid(const Grid& rhs)
199{
200#ifdef DEBUG
201 IsCompatible(rhs);
202#endif
203
204 for (int i=0; i<Local().SizeTotal().X(); i++)
205 for (int j=0; j<Local().SizeTotal().Y(); j++)
206 for (int k=0; k<Local().SizeTotal().Z(); k++)
207 (*this)(i,j,k) = rhs.GetVal(i,j,k);
208}
209
210void Grid::SetBoundary(const Grid& rhs)
211{
212#ifdef DEBUG
213 IsCompatible(rhs);
214#endif
215
216 for (int i=Local().BoundaryBegin1().X(); i<Local().BoundaryEnd1().X(); i++)
217 for (int j=Local().Begin().Y(); j<Local().End().Y(); j++)
218 for (int k=Local().Begin().Z(); k<Local().End().Z(); k++)
219 (*this)(i,j,k) = rhs.GetVal(i,j,k);
220
221 for (int i=Local().BoundaryBegin2().X(); i<Local().BoundaryEnd2().X(); i++)
222 for (int j=Local().Begin().Y(); j<Local().End().Y(); j++)
223 for (int k=Local().Begin().Z(); k<Local().End().Z(); k++)
224 (*this)(i,j,k) = rhs.GetVal(i,j,k);
225
226 for (int i=0; i<Local().SizeTotal().X(); i++)
227 for (int j=Local().BoundaryBegin1().Y(); j<Local().BoundaryEnd1().Y(); j++)
228 for (int k=Local().Begin().Z(); k<Local().End().Z(); k++)
229 (*this)(i,j,k) = rhs.GetVal(i,j,k);
230
231 for (int i=0; i<Local().SizeTotal().X(); i++)
232 for (int j=Local().BoundaryBegin2().Y(); j<Local().BoundaryEnd2().Y(); j++)
233 for (int k=Local().Begin().Z(); k<Local().End().Z(); k++)
234 (*this)(i,j,k) = rhs.GetVal(i,j,k);
235
236 for (int i=0; i<Local().SizeTotal().X(); i++)
237 for (int j=0; j<Local().SizeTotal().Y(); j++)
238 for (int k=Local().BoundaryBegin1().Z(); k<Local().BoundaryEnd1().Z(); k++)
239 (*this)(i,j,k) = rhs.GetVal(i,j,k);
240
241 for (int i=0; i<Local().SizeTotal().X(); i++)
242 for (int j=0; j<Local().SizeTotal().Y(); j++)
243 for (int k=Local().BoundaryBegin2().Z(); k<Local().BoundaryEnd2().Z(); k++)
244 (*this)(i,j,k) = rhs.GetVal(i,j,k);
245}
246
247void Grid::AddGrid(const Grid& rhs)
248{
249#ifdef DEBUG
250 IsCompatible(rhs);
251#endif
252
253 for (int i=0; i<Local().SizeTotal().X(); i++)
254 for (int j=0; j<Local().SizeTotal().Y(); j++)
255 for (int k=0; k<Local().SizeTotal().Z(); k++)
256 (*this)(i,j,k) += rhs.GetVal(i,j,k);
257}
258
259void Grid::SubtractGrid(const Grid& rhs)
260{
261#ifdef DEBUG
262 IsCompatible(rhs);
263#endif
264
265 for (int i=0; i<Local().SizeTotal().X(); i++)
266 for (int j=0; j<Local().SizeTotal().Y(); j++)
267 for (int k=0; k<Local().SizeTotal().Z(); k++)
268 (*this)(i,j,k) -= rhs.GetVal(i,j,k);
269}
270
271void Grid::MultiplyScalar(const vmg_float& scalar)
272{
273 for (int i=0; i<Local().SizeTotal().X(); i++)
274 for (int j=0; j<Local().SizeTotal().Y(); j++)
275 for (int k=0; k<Local().SizeTotal().Z(); k++)
276 (*this)(i,j,k) *= scalar;
277}
278
279void Grid::ApplyStencil(const Stencil& stencil)
280{
281 TempGrid *temp = MG::GetTempGrid();
282 Stencil::iterator iter;
283
284 temp->SetProperties(*this);
285 temp->SetGrid(*this);
286 MG::GetComm()->CommToGhosts(*temp);
287
288 for (int i=Local().Begin().X(); i<Local().End().X(); i++)
289 for (int j=Local().Begin().Y(); j<Local().End().Y(); j++)
290 for (int k=Local().Begin().Z(); k<Local().End().Z(); k++) {
291
292 (*this)(i,j,k) = stencil.GetDiag() * temp->GetVal(i,j,k);
293
294 for (iter=stencil.begin(); iter!=stencil.end(); ++iter)
295 (*this)(i,j,k) += iter->val * temp->GetVal(i+iter->m, j+iter->n, k+iter->o);
296 }
297}
298
299bool Grid::IsCompatible(const Grid& rhs) const
300{
301 bool eq = true;
302
303 eq &= Helper::IsEq(Local().Begin(), rhs.Local().Begin(), "Local().Begin");
304 eq &= Helper::IsEq(Local().End(), rhs.Local().End(), "Local().End");
305 eq &= Helper::IsEq(Local().Size(), rhs.Local().Size(), "Local().Size");
306 eq &= Helper::IsEq(Local().SizeTotal(), rhs.Local().SizeTotal(), "Local().SizeTotal");
307
308 eq &= Helper::IsEq(Global().Begin(), rhs.Global().Begin(), "Global().Begin");
309 eq &= Helper::IsEq(Global().End(), rhs.Global().End(), "Global().End");
310 eq &= Helper::IsEq(Global().Size(), rhs.Global().Size(), "Global().Size");
311
312 eq &= Helper::IsEq(Local().HaloBegin1(), rhs.Local().HaloBegin1(), "Local().HaloBegin1");
313 eq &= Helper::IsEq(Local().HaloBegin2(), rhs.Local().HaloBegin2(), "Local().HaloBegin2");
314 eq &= Helper::IsEq(Local().HaloEnd1(), rhs.Local().HaloEnd1(), "Local().HaloEnd1");
315 eq &= Helper::IsEq(Local().HaloEnd2(), rhs.Local().HaloEnd2(), "Local().HaloEnd2");
316
317 eq &= Helper::IsEq(MeshWidth(), rhs.MeshWidth(), "MeshWidth");
318
319 return eq;
320}
321
322bool Grid::IsConsistent() const
323{
324 bool consistent = true;
325
326 for (int i=0; i<Local().SizeTotal().X(); i++)
327 for (int j=0; j<Local().SizeTotal().Y(); j++)
328 for (int k=0; k<Local().SizeTotal().Z(); k++)
329 consistent &= Helper::CheckNumber(GetVal(i,j,k));
330
331 return consistent;
332}
333
334Grid::~Grid()
335{
336 delete [] grid;
337}
Note: See TracBrowser for help on using the repository browser.