OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
NDGrid.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017, 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_NDGRID_HH_
29#define _OPALX_FIELDS_NDGRID_HH_
30
31#include <algorithm>
32#include <cmath>
33#include <ostream>
34#include <vector>
35
37
38namespace interpolation {
39
57 class NDGrid : public Mesh {
58 public:
59 class Iterator;
60
62 inline NDGrid* clone();
63
65 Mesh* dual() const;
66
68 NDGrid();
69
84 NDGrid(int nDimensions, int* size, double* spacing, double* min);
85
94 NDGrid(std::vector<int> size, std::vector<const double*> gridCoordinates);
95
107 NDGrid(std::vector<std::vector<double> > gridCoordinates);
108
110 NDGrid(const NDGrid& grid);
111
113 ~NDGrid() { ; }
114
125 inline double& coord(const int& index, const int& dimension);
126
137 inline const double& coord(const int& index, const int& dimension) const;
138
147 inline int size(const int& dimension) const;
148
157 inline std::vector<double> coordVector(const int& dimension) const;
158
169 double* newCoordArray(const int& dimension) const;
170
181 inline void coordLowerBound(const double& x, const int& dimension, int& xIndex) const;
182
195 inline void lowerBound(const std::vector<double>& pos, std::vector<int>& xIndex) const;
196
198 inline double min(const int& dimension) const;
199
201 inline double max(const int& dimension) const;
202
209 inline void setCoord(int dimension, int nCoords, double* x);
210
213 inline Mesh::Iterator begin() const;
214
217 inline Mesh::Iterator end() const;
218
226 inline void getPosition(const Mesh::Iterator& it, double* position) const;
227
229 inline int getPositionDimension() const;
230
232 inline bool getConstantSpacing() const;
233
241 inline void setConstantSpacing(bool spacing);
242
248 void setConstantSpacing(double tolerance_m = 1e-9);
249
257 int toInteger(const Mesh::Iterator& lhs) const;
258
266 Mesh::Iterator getNearest(const double* position) const;
267
268 protected:
269 // Change position
270 virtual Mesh::Iterator& addEquals(Mesh::Iterator& lhs, int difference) const;
271 virtual Mesh::Iterator& subEquals(Mesh::Iterator& lhs, int difference) const;
272 virtual Mesh::Iterator& addEquals(Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
273 virtual Mesh::Iterator& subEquals(Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
274 virtual Mesh::Iterator& addOne(Mesh::Iterator& lhs) const;
275 virtual Mesh::Iterator& subOne(Mesh::Iterator& lhs) const;
276 // Check relative position
277 virtual bool isGreater(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
278
279 private:
280 std::vector<std::vector<double> > coord_m;
281 std::vector<VectorMap*> maps_m;
283
292
293 friend bool operator==(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
294 friend bool operator!=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
295 friend bool operator>=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
296 friend bool operator<=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
297 friend bool operator<(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
298 friend bool operator>(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
299 };
300
301 double& NDGrid::coord(const int& index, const int& dimension) {
302 return coord_m[dimension][index - 1];
303 }
304
305 const double& NDGrid::coord(const int& index, const int& dimension) const {
306 return coord_m[dimension][index - 1];
307 }
308
309 int NDGrid::size(const int& dimension) const { return coord_m[dimension].size(); }
310
311 std::vector<double> NDGrid::coordVector(const int& dimension) const {
312 return coord_m[dimension];
313 }
314
315 void NDGrid::coordLowerBound(const double& x, const int& dimension, int& xIndex) const {
316 if (constantSpacing_m) {
317 double x0 = coord_m[dimension][0];
318 double x1 = coord_m[dimension][1];
319 xIndex = static_cast<int>(std::floor((x - x0) / (x1 - x0)));
320 int coordSize = static_cast<int>(coord_m[dimension].size());
321 if (xIndex >= coordSize) {
322 xIndex = coordSize - 1;
323 } else if (xIndex < -1) {
324 xIndex = -1;
325 }
326 } else {
327 const std::vector<double>& c_t(coord_m[dimension]);
328 xIndex = std::lower_bound(c_t.begin(), c_t.end(), x) - c_t.begin() - 1;
329 }
330 }
331
332 void NDGrid::lowerBound(const std::vector<double>& pos, std::vector<int>& xIndex) const {
333 xIndex = std::vector<int>(pos.size());
334 for (unsigned int i = 0; i < pos.size(); i++) {
335 coordLowerBound(pos[i], i, xIndex[i]);
336 }
337 }
338
339 double NDGrid::min(const int& dimension) const { return coord_m[dimension][0]; }
340
341 double NDGrid::max(const int& dimension) const {
342 return coord_m[dimension][coord_m[dimension].size() - 1];
343 }
344
345 NDGrid* NDGrid::clone() { return new NDGrid(*this); }
346
347 void NDGrid::setCoord(int dimension, int nCoords, double* x) {
348 coord_m[dimension] = std::vector<double>(x, x + nCoords);
349 }
350
352 return Mesh::Iterator(std::vector<int>(coord_m.size(), 1), this);
353 }
354
356 if (coord_m.size() == 0) {
357 return Mesh::Iterator(std::vector<int>(), this);
358 }
359 std::vector<int> end(coord_m.size(), 1);
360 end[0] = coord_m[0].size() + 1;
361 return Mesh::Iterator(end, this);
362 }
363
364 void NDGrid::getPosition(const Mesh::Iterator& it, double* position) const {
365 for (unsigned int i = 0; i < it.getState().size(); i++)
366 position[i] = coord_m[i][it[i] - 1];
367 }
368
369 int NDGrid::getPositionDimension() const { return coord_m.size(); }
370
372
373 void NDGrid::setConstantSpacing(bool spacing) { constantSpacing_m = spacing; }
374
375} // namespace interpolation
376
377#endif // _OPALX_FIELDS_NDGRID_HH_
std::vector< int > getState() const
Base class for meshing routines.
Definition Mesh.h:49
virtual Mesh::Iterator & addEquals(Mesh::Iterator &lhs, int difference) const
Definition NDGrid.cpp:89
virtual Mesh::Iterator & addOne(Mesh::Iterator &lhs) const
Definition NDGrid.cpp:150
friend bool operator>(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
virtual Mesh::Iterator & subOne(Mesh::Iterator &lhs) const
Definition NDGrid.cpp:160
Mesh::Iterator begin() const
Definition NDGrid.h:351
void coordLowerBound(const double &x, const int &dimension, int &xIndex) const
Definition NDGrid.h:315
int toInteger(const Mesh::Iterator &lhs) const
Definition NDGrid.cpp:197
friend Mesh::Iterator & operator++(Mesh::Iterator &lhs)
friend bool operator<(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
double max(const int &dimension) const
Definition NDGrid.h:341
Mesh::Iterator end() const
Definition NDGrid.h:355
int getPositionDimension() const
Definition NDGrid.h:369
Mesh::Iterator getNearest(const double *position) const
Definition NDGrid.cpp:210
void setCoord(int dimension, int nCoords, double *x)
Definition NDGrid.h:347
std::vector< std::vector< double > > coord_m
Definition NDGrid.h:280
friend bool operator!=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
double min(const int &dimension) const
Definition NDGrid.h:339
double * newCoordArray(const int &dimension) const
Definition NDGrid.cpp:79
friend Mesh::Iterator operator-(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
friend Mesh::Iterator & operator--(Mesh::Iterator &lhs)
friend Mesh::Iterator & operator+=(Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
friend bool operator==(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
NDGrid()
////// NDGrid ///////
Definition NDGrid.cpp:36
void lowerBound(const std::vector< double > &pos, std::vector< int > &xIndex) const
Definition NDGrid.h:332
friend bool operator<=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
friend Mesh::Iterator operator--(Mesh::Iterator &lhs, int)
void setConstantSpacing(bool spacing)
Definition NDGrid.h:373
friend Mesh::Iterator operator+(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
virtual Mesh::Iterator & subEquals(Mesh::Iterator &lhs, int difference) const
Definition NDGrid.cpp:116
std::vector< VectorMap * > maps_m
Definition NDGrid.h:281
void getPosition(const Mesh::Iterator &it, double *position) const
Definition NDGrid.h:364
Mesh * dual() const
Definition NDGrid.cpp:234
friend Mesh::Iterator & operator-=(Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
int size(const int &dimension) const
Definition NDGrid.h:309
std::vector< double > coordVector(const int &dimension) const
Definition NDGrid.h:311
double & coord(const int &index, const int &dimension)
Definition NDGrid.h:301
NDGrid * clone()
Definition NDGrid.h:345
bool getConstantSpacing() const
Definition NDGrid.h:371
friend bool operator>=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
friend Mesh::Iterator operator++(Mesh::Iterator &lhs, int)
virtual bool isGreater(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs) const
Definition NDGrid.cpp:188