OPAL (Object Oriented Parallel Accelerator Library)  2024.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 
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(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 
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 
280 private:
281  std::vector< std::vector<double> > coord_m;
282  std::vector<VectorMap*> maps_m;
284 
285  friend Mesh::Iterator operator++(Mesh::Iterator& lhs, int);
286  friend Mesh::Iterator operator--(Mesh::Iterator& lhs, int);
289  friend Mesh::Iterator operator- (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
290  friend Mesh::Iterator operator+ (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
291  friend Mesh::Iterator& operator-=(Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
292  friend Mesh::Iterator& operator+=(Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
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 
302 double& NDGrid::coord(const int& index, const int& dimension) {
303  return coord_m[dimension][index-1];
304 }
305 
306 const double& NDGrid::coord(const int& index, const int& dimension) const {
307  return coord_m[dimension][index-1];
308 }
309 
310 int NDGrid::size(const int& dimension) const {
311  return coord_m[dimension].size();
312 }
313 
314 std::vector<double> NDGrid::coordVector(const int& dimension) const {
315  return coord_m[dimension];
316 }
317 
318 void 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 
335 void 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 
342 double NDGrid::min(const int& dimension) const {
343  return coord_m[dimension][0];
344 }
345 
346 double 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 
354 void 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 
371 void 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 
384 void NDGrid::setConstantSpacing(bool spacing) {
385  constantSpacing_m = spacing;
386 }
387 
388 } // namespace interpolation
389 
390 #endif // _CLASSIC_FIELDS_NDGRID_HH_
friend Mesh::Iterator operator+(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
Definition: Mesh-inl.icc:124
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed we want its recipients to know that what they have is not the so that any problems introduced by others will not reflect on the original authors reputations any free program is threatened constantly by software patents We wish to avoid the danger that redistributors of a free program will individually obtain patent in effect making the program proprietary To prevent we have made it clear that any patent must be licensed for everyone s free use or not licensed at all The precise terms and conditions for distribution and modification follow GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR DISTRIBUTION AND MODIFICATION This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License The refers to any such program or and a work based on the Program means either the Program or any derivative work under copyright a work containing the Program or a portion of it
Definition: LICENSE:43
friend bool operator==(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
Definition: Mesh-inl.icc:172
friend bool operator<(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
Definition: Mesh-inl.icc:168
double max(const int &dimension) const
Definition: NDGrid.h:346
void setCoord(int dimension, int nCoords, double *x)
Definition: NDGrid.h:354
double min(const int &dimension) const
Definition: NDGrid.h:342
NDGrid()
////// NDGrid ///////
Definition: NDGrid.cpp:36
Mesh * dual() const
Definition: NDGrid.cpp:232
Mesh::Iterator end() const
Definition: NDGrid.h:362
virtual Mesh::Iterator & subEquals(Mesh::Iterator &lhs, int difference) const
Definition: NDGrid.cpp:118
friend bool operator>=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
Definition: Mesh-inl.icc:154
friend bool operator!=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
Definition: Mesh-inl.icc:178
friend bool operator>(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
Definition: Mesh-inl.icc:164
Base class for meshing routines.
Definition: Mesh.h:49
friend bool operator<=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
Definition: Mesh-inl.icc:159
std::vector< VectorMap * > maps_m
Definition: NDGrid.h:282
void getPosition(const Mesh::Iterator &it, double *position) const
Definition: NDGrid.h:371
friend Mesh::Iterator & operator-=(Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
Definition: Mesh-inl.icc:141
std::vector< double > coordVector(const int &dimension) const
Definition: NDGrid.h:314
virtual bool isGreater(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs) const
Definition: NDGrid.cpp:188
void lowerBound(const std::vector< double > &pos, std::vector< int > &xIndex) const
Definition: NDGrid.h:335
void setConstantSpacing(bool spacing)
Definition: NDGrid.h:384
friend Mesh::Iterator operator++(Mesh::Iterator &lhs, int)
Definition: Mesh-inl.icc:98
void coordLowerBound(const double &x, const int &dimension, int &xIndex) const
Definition: NDGrid.h:318
NDGrid * clone()
Definition: NDGrid.h:350
int toInteger(const Mesh::Iterator &lhs) const
Definition: NDGrid.cpp:197
std::vector< std::vector< double > > coord_m
Definition: NDGrid.h:281
friend Mesh::Iterator & operator+=(Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
Definition: Mesh-inl.icc:137
std::vector< int > getState() const
Definition: Mesh-inl.icc:58
friend Mesh::Iterator operator--(Mesh::Iterator &lhs, int)
Definition: Mesh-inl.icc:104
double & coord(const int &index, const int &dimension)
Definition: NDGrid.h:302
int size(const int &dimension) const
Definition: NDGrid.h:310
constexpr double e
The value of .
Definition: Physics.h:39
double * newCoordArray(const int &dimension) const
Definition: NDGrid.cpp:82
virtual Mesh::Iterator & addEquals(Mesh::Iterator &lhs, int difference) const
Definition: NDGrid.cpp:92
virtual Mesh::Iterator & subOne(Mesh::Iterator &lhs) const
Definition: NDGrid.cpp:160
virtual Mesh::Iterator & addOne(Mesh::Iterator &lhs) const
Definition: NDGrid.cpp:151
Mesh::Iterator begin() const
Definition: NDGrid.h:358
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:733
friend Mesh::Iterator operator-(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
Definition: Mesh-inl.icc:111
bool getConstantSpacing() const
Definition: NDGrid.h:380
Mesh::Iterator getNearest(const double *position) const
Definition: NDGrid.cpp:210
int getPositionDimension() const
Definition: NDGrid.h:376