OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
ThreeDGrid.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012, Chris Rogers
3 * All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * 1. Redistributions of source code must retain the above copyright notice,
7 * this list of conditions and the following disclaimer.
8 * 2. Redistributions in binary form must reproduce the above copyright notice,
9 * this list of conditions and the following disclaimer in the documentation
10 * and/or other materials provided with the distribution.
11 * 3. Neither the name of STFC nor the names of its contributors may be used to
12 * endorse or promote products derived from this software without specific
13 * prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef _OPALX_FIELDS_THREEDGRID_HH_
29#define _OPALX_FIELDS_THREEDGRID_HH_
30
31#include <algorithm>
32#include <cmath>
33#include <vector>
34
36
37namespace interpolation {
38
57 class ThreeDGrid : public Mesh {
58 public:
59 class Iterator;
60
62 ThreeDGrid* clone() { return new ThreeDGrid(*this); }
63
65 Mesh* dual() const;
66
68 ThreeDGrid();
69
85 double dX, double dY, double dZ, double minX, double minY, double minZ,
86 int numberOfXCoords, int numberOfYCoords, int numberOfZCoords);
87
101 int xSize, const double* x, int ySize, const double* y, int zSize, const double* z);
102
110 ThreeDGrid(std::vector<double> x, std::vector<double> y, std::vector<double> z);
111
113 ~ThreeDGrid();
114
119 inline double& x(const int& i) { return x_m[i - 1]; }
120
125 inline double& y(const int& j) { return y_m[j - 1]; }
126
131 inline double& z(const int& k) { return z_m[k - 1]; }
132
137 inline const double& x(const int& i) const { return x_m[i - 1]; }
138
143 inline const double& y(const int& j) const { return y_m[j - 1]; }
144
149 inline const double& z(const int& j) const { return z_m[j - 1]; }
150
152 inline int xSize() const { return static_cast<int>(x_m.size()); }
153
155 inline int ySize() const { return static_cast<int>(y_m.size()); }
156
158 inline int zSize() const { return static_cast<int>(z_m.size()); }
159
161 std::vector<double> xVector() { return std::vector<double>(x_m); }
162
164 std::vector<double> yVector() { return std::vector<double>(y_m); }
165
167 std::vector<double> zVector() { return std::vector<double>(z_m); }
168
170 inline double* newXArray();
171
173 inline double* newYArray();
174
176 inline double* newZArray();
177
183 inline void xLowerBound(const double& x, int& xIndex) const;
184
190 inline void yLowerBound(const double& y, int& yIndex) const;
191
197 inline void zLowerBound(const double& z, int& zIndex) const;
198
204 inline void lowerBound(
205 const double& x, int& xIndex, const double& y, int& yIndex, const double& z,
206 int& zIndex) const;
207
213 inline void lowerBound(
214 const double& x, const double& y, const double& z, Mesh::Iterator& it) const;
215
217 inline double minX() const;
218
220 inline double maxX() const;
221
223 inline double minY() const;
224
226 inline double maxY() const;
227
229 inline double minZ() const;
230
232 inline double maxZ() const;
233
235 inline void setX(int nXCoords, double* x);
236
238 inline void setY(int nYCoords, double* y);
239
241 inline void setZ(int nZCoords, double* z);
242
244 void add(VectorMap* map);
245
248 void remove(VectorMap* map);
249
251 Mesh::Iterator begin() const;
252
254 Mesh::Iterator end() const;
255
262 virtual void getPosition(const Mesh::Iterator& it, double* position) const;
263
265 inline int getPositionDimension() const;
266
273 inline int toInteger(const Mesh::Iterator& lhs) const;
274
280 inline void setConstantSpacing(bool spacing);
281
284 void setConstantSpacing();
285
287 inline bool getConstantSpacing() const;
288
290 Mesh::Iterator getNearest(const double* position) const;
291
300 static void vectorLowerBound(std::vector<double> vec, double x, int& index);
301
302 protected:
303 // Change position
304 virtual Mesh::Iterator& addEquals(Mesh::Iterator& lhs, int difference) const;
305 virtual Mesh::Iterator& subEquals(Mesh::Iterator& lhs, int difference) const;
306 virtual Mesh::Iterator& addEquals(Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
307 virtual Mesh::Iterator& subEquals(Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
308 virtual Mesh::Iterator& addOne(Mesh::Iterator& lhs) const;
309 virtual Mesh::Iterator& subOne(Mesh::Iterator& lhs) const;
310 // Check relative position
311 virtual bool isGreater(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
312
313 private:
314 std::vector<double> x_m;
315 std::vector<double> y_m;
316 std::vector<double> z_m;
320 std::vector<VectorMap*> maps_m;
322
331
332 friend bool operator==(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
333 friend bool operator!=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
334 friend bool operator>=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
335 friend bool operator<=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
336 friend bool operator<(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
337 friend bool operator>(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
338 };
339
341 double* x = new double[x_m.size()];
342 for (unsigned int i = 0; i < x_m.size(); i++)
343 x[i] = x_m[i];
344 return x;
345 }
346
348 double* y = new double[y_m.size()];
349 for (unsigned int i = 0; i < y_m.size(); i++)
350 y[i] = y_m[i];
351 return y;
352 }
353
355 double* z = new double[z_m.size()];
356 for (unsigned int i = 0; i < z_m.size(); i++)
357 z[i] = z_m[i];
358 return z;
359 }
360
362 const double& x, int& xIndex, const double& y, int& yIndex, const double& z,
363 int& zIndex) const {
364 xLowerBound(x, xIndex);
365 yLowerBound(y, yIndex);
366 zLowerBound(z, zIndex);
367 }
368
370 const double& x, const double& y, const double& z, Mesh::Iterator& it) const {
371 xLowerBound(x, it[0]);
372 yLowerBound(y, it[1]);
373 zLowerBound(z, it[2]);
374 it[0]++;
375 it[1]++;
376 it[2]++;
377 }
378
379 void ThreeDGrid::xLowerBound(const double& x, int& xIndex) const {
381 xIndex = static_cast<int>(std::floor((x - x_m[0]) / (x_m[1] - x_m[0])));
382 else {
383 vectorLowerBound(x_m, x, xIndex);
384 }
385 }
386
387 void ThreeDGrid::yLowerBound(const double& y, int& yIndex) const {
389 yIndex = static_cast<int>(std::floor((y - y_m[0]) / (y_m[1] - y_m[0])));
390 else
391 vectorLowerBound(y_m, y, yIndex);
392 }
393
394 void ThreeDGrid::zLowerBound(const double& z, int& zIndex) const {
396 zIndex = static_cast<int>(std::floor((z - z_m[0]) / (z_m[1] - z_m[0])));
397 else
398 vectorLowerBound(z_m, z, zIndex);
399 }
400
401 double ThreeDGrid::minX() const { return x_m[0]; }
402
403 double ThreeDGrid::maxX() const { return x_m[xSize_m - 1]; }
404
405 double ThreeDGrid::minY() const { return y_m[0]; }
406
407 double ThreeDGrid::maxY() const { return y_m[ySize_m - 1]; }
408
409 double ThreeDGrid::minZ() const { return z_m[0]; }
410
411 double ThreeDGrid::maxZ() const { return z_m[zSize_m - 1]; }
412
413 void ThreeDGrid::setX(int nXCoords, double* x) { x_m = std::vector<double>(x, x + nXCoords); }
414
415 void ThreeDGrid::setY(int nYCoords, double* y) { y_m = std::vector<double>(y, y + nYCoords); }
416
417 void ThreeDGrid::setZ(int nZCoords, double* z) { z_m = std::vector<double>(z, z + nZCoords); }
418
419 int ThreeDGrid::getPositionDimension() const { return 3; }
420
421 int ThreeDGrid::toInteger(const Mesh::Iterator& lhs) const {
422 return (lhs.getState()[0] - 1) * zSize_m * ySize_m + (lhs.getState()[1] - 1) * zSize_m
423 + (lhs.getState()[2] - 1);
424 }
425
426 void ThreeDGrid::setConstantSpacing(bool spacing) { constantSpacing_m = spacing; }
427
429} // namespace interpolation
430#endif
std::vector< int > getState() const
Base class for meshing routines.
Definition Mesh.h:49
Mesh::Iterator begin() const
void yLowerBound(const double &y, int &yIndex) const
Definition ThreeDGrid.h:387
friend bool operator>(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
void setY(int nYCoords, double *y)
Definition ThreeDGrid.h:415
void setZ(int nZCoords, double *z)
Definition ThreeDGrid.h:417
std::vector< double > zVector()
Definition ThreeDGrid.h:167
std::vector< double > xVector()
Definition ThreeDGrid.h:161
friend Mesh::Iterator & operator++(Mesh::Iterator &lhs)
friend bool operator<(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
std::vector< double > y_m
Definition ThreeDGrid.h:315
const double & y(const int &j) const
Definition ThreeDGrid.h:143
virtual void getPosition(const Mesh::Iterator &it, double *position) const
static void vectorLowerBound(std::vector< double > vec, double x, int &index)
Mesh::Iterator end() const
std::vector< double > x_m
Definition ThreeDGrid.h:314
virtual Mesh::Iterator & subEquals(Mesh::Iterator &lhs, int difference) const
const double & x(const int &i) const
Definition ThreeDGrid.h:137
void remove(VectorMap *map)
friend bool operator!=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
std::vector< double > z_m
Definition ThreeDGrid.h:316
const double & z(const int &j) const
Definition ThreeDGrid.h:149
friend Mesh::Iterator operator-(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
void zLowerBound(const double &z, int &zIndex) const
Definition ThreeDGrid.h:394
friend Mesh::Iterator & operator--(Mesh::Iterator &lhs)
bool getConstantSpacing() const
Definition ThreeDGrid.h:428
void xLowerBound(const double &x, int &xIndex) const
Definition ThreeDGrid.h:379
friend Mesh::Iterator & operator+=(Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
friend bool operator==(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
std::vector< double > yVector()
Definition ThreeDGrid.h:164
virtual Mesh::Iterator & subOne(Mesh::Iterator &lhs) const
double & x(const int &i)
Definition ThreeDGrid.h:119
friend bool operator<=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
double & z(const int &k)
Definition ThreeDGrid.h:131
friend Mesh::Iterator operator--(Mesh::Iterator &lhs, int)
virtual bool isGreater(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs) const
void setX(int nXCoords, double *x)
Definition ThreeDGrid.h:413
friend Mesh::Iterator operator+(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
int getPositionDimension() const
Definition ThreeDGrid.h:419
double & y(const int &j)
Definition ThreeDGrid.h:125
void add(VectorMap *map)
virtual Mesh::Iterator & addOne(Mesh::Iterator &lhs) const
virtual Mesh::Iterator & addEquals(Mesh::Iterator &lhs, int difference) const
friend Mesh::Iterator & operator-=(Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
int toInteger(const Mesh::Iterator &lhs) const
Definition ThreeDGrid.h:421
void lowerBound(const double &x, int &xIndex, const double &y, int &yIndex, const double &z, int &zIndex) const
Definition ThreeDGrid.h:361
friend bool operator>=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
std::vector< VectorMap * > maps_m
Definition ThreeDGrid.h:320
Mesh::Iterator getNearest(const double *position) const
friend Mesh::Iterator operator++(Mesh::Iterator &lhs, int)