OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
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 _CLASSIC_FIELDS_NDGRID_HH_
29#define _CLASSIC_FIELDS_NDGRID_HH_
30
31#include <algorithm>
32#include <cmath>
33#include <ostream>
34#include <vector>
35
37
38namespace interpolation {
39
57class 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
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
268protected:
269
270 //Change position
271 virtual Mesh::Iterator& addEquals(Mesh::Iterator& lhs, int difference) const;
272 virtual Mesh::Iterator& subEquals(Mesh::Iterator& lhs, int difference) const;
273 virtual Mesh::Iterator& addEquals(Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
274 virtual Mesh::Iterator& subEquals(Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
275 virtual Mesh::Iterator& addOne (Mesh::Iterator& lhs) const;
276 virtual Mesh::Iterator& subOne (Mesh::Iterator& lhs) const;
277 //Check relative position
278 virtual bool isGreater(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
279
280private:
281 std::vector< std::vector<double> > coord_m;
282 std::vector<VectorMap*> maps_m;
284
293
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 friend bool operator> (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
300};
301
302double& NDGrid::coord(const int& index, const int& dimension) {
303 return coord_m[dimension][index-1];
304}
305
306const double& NDGrid::coord(const int& index, const int& dimension) const {
307 return coord_m[dimension][index-1];
308}
309
310int NDGrid::size(const int& dimension) const {
311 return coord_m[dimension].size();
312}
313
314std::vector<double> NDGrid::coordVector(const int& dimension) const {
315 return coord_m[dimension];
316}
317
318void NDGrid::coordLowerBound(const double& x, const int& dimension, int& xIndex) const {
319 if (constantSpacing_m) {
320 double x0 = coord_m[dimension][0];
321 double x1 = coord_m[dimension][1];
322 xIndex = static_cast<int>(std::floor((x - x0)/(x1-x0)));
323 int coordSize = static_cast<int>(coord_m[dimension].size());
324 if (xIndex >= coordSize) {
325 xIndex = coordSize-1;
326 } else if (xIndex < -1) {
327 xIndex = -1;
328 }
329 } else {
330 const std::vector<double>& c_t(coord_m[dimension]);
331 xIndex = std::lower_bound(c_t.begin(), c_t.end(), x)-c_t.begin()-1;
332 }
333}
334
335void NDGrid::lowerBound(const std::vector<double>& pos, std::vector<int>& xIndex) const {
336 xIndex = std::vector<int> (pos.size());
337 for(unsigned int i=0; i < pos.size(); i++) {
338 coordLowerBound(pos[i], i, xIndex[i]);
339 }
340}
341
342double NDGrid::min(const int& dimension) const {
343 return coord_m[dimension][0];
344}
345
346double NDGrid::max(const int& dimension) const {
347 return coord_m[dimension][coord_m[dimension].size()-1];
348}
349
351 return new NDGrid(*this);
352}
353
354void NDGrid::setCoord(int dimension, int nCoords, double * x) {
355 coord_m[dimension] = std::vector<double>(x, x+nCoords);
356}
357
359 return Mesh::Iterator(std::vector<int>(coord_m.size(), 1), this);
360}
361
363 if (coord_m.size() == 0) {
364 return Mesh::Iterator(std::vector<int>(), this);
365 }
366 std::vector<int> end(coord_m.size(), 1);
367 end[0] = coord_m[0].size()+1;
368 return Mesh::Iterator(end, this);
369}
370
371void NDGrid::getPosition(const Mesh::Iterator& it, double * position) const {
372 for (unsigned int i=0; i<it.getState().size(); i++)
373 position[i] = coord_m[i][it[i]-1];
374}
375
377 return coord_m.size();
378}
379
381 return constantSpacing_m;
382}
383
384void NDGrid::setConstantSpacing(bool spacing) {
385 constantSpacing_m = spacing;
386}
387
388} // namespace interpolation
389
390#endif // _CLASSIC_FIELDS_NDGRID_HH_
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:733
constexpr double e
The value of.
Definition: Physics.h:39
Base class for meshing routines.
Definition: Mesh.h:49
std::vector< int > getState() const
virtual Mesh::Iterator & addEquals(Mesh::Iterator &lhs, int difference) const
Definition: NDGrid.cpp:92
virtual Mesh::Iterator & addOne(Mesh::Iterator &lhs) const
Definition: NDGrid.cpp:151
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:358
void coordLowerBound(const double &x, const int &dimension, int &xIndex) const
Definition: NDGrid.h:318
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:346
Mesh::Iterator end() const
Definition: NDGrid.h:362
std::vector< std::vector< double > > coord_m
Definition: NDGrid.h:281
int getPositionDimension() const
Definition: NDGrid.h:376
Mesh::Iterator getNearest(const double *position) const
Definition: NDGrid.cpp:210
void setCoord(int dimension, int nCoords, double *x)
Definition: NDGrid.h:354
friend bool operator!=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
double min(const int &dimension) const
Definition: NDGrid.h:342
double * newCoordArray(const int &dimension) const
Definition: NDGrid.cpp:82
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:335
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:384
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:118
std::vector< VectorMap * > maps_m
Definition: NDGrid.h:282
void getPosition(const Mesh::Iterator &it, double *position) const
Definition: NDGrid.h:371
Mesh * dual() const
Definition: NDGrid.cpp:232
friend Mesh::Iterator & operator-=(Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
int size(const int &dimension) const
Definition: NDGrid.h:310
std::vector< double > coordVector(const int &dimension) const
Definition: NDGrid.h:314
double & coord(const int &index, const int &dimension)
Definition: NDGrid.h:302
NDGrid * clone()
Definition: NDGrid.h:350
bool getConstantSpacing() const
Definition: NDGrid.h:380
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