OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
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 <vector>
32 #include <algorithm>
33 #include <math.h>
34 #include <ostream>
35 
37 
38 namespace 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() {;}
111 
122  inline double& coord(const int& index, const int& dimension);
123 
134  inline const double& coord(const int& index, const int& dimension) const;
135 
144  inline int size(const int& dimension) const;
145 
154  inline std::vector<double> coordVector(const int& dimension) const;
155 
166  double* newCoordArray (const int& dimension) const;
167 
178  inline void coordLowerBound(const double& x, const int& dimension, int& xIndex) const;
179 
192  inline void lowerBound(const std::vector<double>& pos, std::vector<int>& xIndex) const;
193 
195  inline double min(const int& dimension) const;
196 
198  inline double max(const int& dimension) const;
199 
206  inline void setCoord(int dimension, int nCoords, double * x);
207 
210  inline Mesh::Iterator begin() const;
211 
214  inline Mesh::Iterator end() const;
215 
223  inline void getPosition(const Mesh::Iterator& it, double * position) const;
224 
226  inline int getPositionDimension() const;
227 
229  inline bool getConstantSpacing() const;
230 
238  inline void setConstantSpacing(bool spacing);
239 
245  void setConstantSpacing(double tolerance_m = 1e-9);
246 
254  int toInteger(const Mesh::Iterator& lhs) const;
255 
263  Mesh::Iterator getNearest(const double* position) const;
264 
265 protected:
266 
267  //Change position
268  virtual Mesh::Iterator& addEquals(Mesh::Iterator& lhs, int difference) const;
269  virtual Mesh::Iterator& subEquals(Mesh::Iterator& lhs, int difference) const;
270  virtual Mesh::Iterator& addEquals(Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
271  virtual Mesh::Iterator& subEquals(Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
272  virtual Mesh::Iterator& addOne (Mesh::Iterator& lhs) const;
273  virtual Mesh::Iterator& subOne (Mesh::Iterator& lhs) const;
274  //Check relative position
275  virtual bool isGreater(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
276 
277 private:
278  std::vector< std::vector<double> > coord_m;
279  std::vector<VectorMap*> maps_m;
281 
282  friend Mesh::Iterator operator++(Mesh::Iterator& lhs, int);
283  friend Mesh::Iterator operator--(Mesh::Iterator& lhs, int);
286  friend Mesh::Iterator operator- (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
287  friend Mesh::Iterator operator+ (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
288  friend Mesh::Iterator& operator-=(Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
289  friend Mesh::Iterator& operator+=(Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
290 
291  friend bool operator==(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
292  friend bool operator!=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
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 };
298 
299 double& NDGrid::coord(const int& index, const int& dimension) {
300  return coord_m[dimension][index-1];
301 }
302 
303 const double& NDGrid::coord(const int& index, const int& dimension) const {
304  return coord_m[dimension][index-1];
305 }
306 
307 int NDGrid::size(const int& dimension) const {
308  return coord_m[dimension].size();
309 }
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>(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 {
340  return coord_m[dimension][0];
341 }
342 
343 double NDGrid::max(const int& dimension) const {
344  return coord_m[dimension][coord_m[dimension].size()-1];
345 }
346 
348  return new NDGrid(*this);
349 }
350 
351 void NDGrid::setCoord(int dimension, int nCoords, double * x) {
352  coord_m[dimension] = std::vector<double>(x, x+nCoords);
353 }
354 
356  return Mesh::Iterator(std::vector<int>(coord_m.size(), 1), this);
357 }
358 
360  if (coord_m.size() == 0) {
361  return Mesh::Iterator(std::vector<int>(), this);
362  }
363  std::vector<int> end(coord_m.size(), 1);
364  end[0] = coord_m[0].size()+1;
365  return Mesh::Iterator(end, this);
366 }
367 
368 void NDGrid::getPosition(const Mesh::Iterator& it, double * position) const {
369  for (unsigned int i=0; i<it.getState().size(); i++)
370  position[i] = coord_m[i][it[i]-1];
371 }
372 
374  return coord_m.size();
375 }
376 
378  return constantSpacing_m;
379 }
380 
381 void NDGrid::setConstantSpacing(bool spacing) {
382  constantSpacing_m = spacing;
383 }
384 
385 } // namespace interpolation
386 
387 #endif // _CLASSIC_FIELDS_NDGRID_HH_
virtual Mesh::Iterator & subEquals(Mesh::Iterator &lhs, int difference) const
Definition: NDGrid.cpp:114
friend Mesh::Iterator operator+(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
int getPositionDimension() const
Definition: NDGrid.h:373
std::vector< VectorMap * > maps_m
Definition: NDGrid.h:279
constexpr double e
The value of .
Definition: Physics.h:40
virtual Mesh::Iterator & addOne(Mesh::Iterator &lhs) const
Definition: NDGrid.cpp:147
friend Mesh::Iterator & operator+=(Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
double max(const int &dimension) const
Definition: NDGrid.h:343
bool getConstantSpacing() const
Definition: NDGrid.h:377
friend bool operator!=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
double min(const int &dimension) const
Definition: NDGrid.h:339
friend Mesh::Iterator & operator-=(Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
NDGrid * clone()
Definition: NDGrid.h:347
friend bool operator>=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
friend Mesh::Iterator operator--(Mesh::Iterator &lhs, int)
friend bool operator<=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
double & coord(const int &index, const int &dimension)
Definition: NDGrid.h:299
virtual Mesh::Iterator & addEquals(Mesh::Iterator &lhs, int difference) const
Definition: NDGrid.cpp:88
std::vector< double > coordVector(const int &dimension) const
Definition: NDGrid.h:311
virtual Mesh::Iterator & subOne(Mesh::Iterator &lhs) const
Definition: NDGrid.cpp:156
void setConstantSpacing(bool spacing)
Definition: NDGrid.h:381
friend bool operator<(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
void setCoord(int dimension, int nCoords, double *x)
Definition: NDGrid.h:351
Mesh::Iterator end() const
Definition: NDGrid.h:359
double * newCoordArray(const int &dimension) const
Definition: NDGrid.cpp:78
void lowerBound(const std::vector< double > &pos, std::vector< int > &xIndex) const
Definition: NDGrid.h:332
int size(const int &dimension) const
Definition: NDGrid.h:307
Mesh * dual() const
Definition: NDGrid.cpp:228
virtual bool isGreater(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs) const
Definition: NDGrid.cpp:184
Base class for meshing routines.
Definition: Mesh.h:49
friend Mesh::Iterator operator-(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
void getPosition(const Mesh::Iterator &it, double *position) const
Definition: NDGrid.h:368
NDGrid()
////// NDGrid ///////
Definition: NDGrid.cpp:36
friend bool operator>(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
friend Mesh::Iterator operator++(Mesh::Iterator &lhs, int)
std::vector< int > getState() const
int toInteger(const Mesh::Iterator &lhs) const
Definition: NDGrid.cpp:193
std::vector< std::vector< double > > coord_m
Definition: NDGrid.h:278
Mesh::Iterator begin() const
Definition: NDGrid.h:355
friend bool operator==(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
void coordLowerBound(const double &x, const int &dimension, int &xIndex) const
Definition: NDGrid.h:315
Mesh::Iterator getNearest(const double *position) const
Definition: NDGrid.cpp:206
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:816