OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
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 _CLASSIC_FIELDS_THREEDGRID_HH_
29 #define _CLASSIC_FIELDS_THREEDGRID_HH_
30 
31 #include <algorithm>
32 #include <cmath>
33 #include <vector>
34 
36 
37 namespace 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 
84  ThreeDGrid(double dX, double dY, double dZ,
85  double minX, double minY, double minZ,
86  int numberOfXCoords, int numberOfYCoords, int numberOfZCoords);
87 
100  ThreeDGrid(int xSize, const double *x,
101  int ySize, const double *y,
102  int zSize, const double *z);
103 
111  ThreeDGrid(std::vector<double> x,
112  std::vector<double> y,
113  std::vector<double> z);
114 
116  ~ThreeDGrid();
117 
122  inline double& x(const int& i) {return x_m[i-1];}
123 
128  inline double& y(const int& j) {return y_m[j-1];}
129 
134  inline double& z(const int& k) {return z_m[k-1];}
135 
140  inline const double& x(const int& i) const {return x_m[i-1];}
141 
146  inline const double& y(const int& j) const {return y_m[j-1];}
147 
152  inline const double& z(const int& j) const {return z_m[j-1];}
153 
155  inline int xSize() const {return static_cast<int>(x_m.size());}
156 
158  inline int ySize() const {return static_cast<int>(y_m.size());}
159 
161  inline int zSize() const {return static_cast<int>(z_m.size());}
162 
164  std::vector<double> xVector() {return std::vector<double>(x_m);}
165 
167  std::vector<double> yVector() {return std::vector<double>(y_m);}
168 
170  std::vector<double> zVector() {return std::vector<double>(z_m);}
171 
173  inline double* newXArray();
174 
176  inline double* newYArray();
177 
179  inline double* newZArray();
180 
186  inline void xLowerBound(const double& x, int& xIndex) const;
187 
193  inline void yLowerBound(const double& y, int& yIndex) const;
194 
200  inline void zLowerBound(const double& z, int& zIndex) const;
201 
207  inline void lowerBound(const double& x, int& xIndex,
208  const double& y, int& yIndex,
209  const double& z, int& zIndex) const;
210 
216  inline void lowerBound(const double& x, const double& y, const double& z,
217  Mesh::Iterator& it) const;
218 
220  inline double minX() const;
221 
223  inline double maxX() const;
224 
226  inline double minY() const;
227 
229  inline double maxY() const;
230 
232  inline double minZ() const;
233 
235  inline double maxZ() const;
236 
238  inline void setX(int nXCoords, double * x);
239 
241  inline void setY(int nYCoords, double * y);
242 
244  inline void setZ(int nZCoords, double * z);
245 
247  void add(VectorMap* map);
248 
251  void remove(VectorMap* map);
252 
254  Mesh::Iterator begin() const;
255 
257  Mesh::Iterator end() const;
258 
265  virtual void getPosition(const Mesh::Iterator& it, double * position) const;
266 
268  inline int getPositionDimension() const;
269 
276  inline int toInteger(const Mesh::Iterator& lhs) const;
277 
283  inline void setConstantSpacing(bool spacing);
284 
287  void setConstantSpacing();
288 
290  inline bool getConstantSpacing() const;
291 
293  Mesh::Iterator getNearest(const double* position) const;
294 
303  static void vectorLowerBound(std::vector<double> vec, double x, int& index);
304 
305  protected:
306  // Change position
308  int difference) const;
310  int difference) const;
311  virtual Mesh::Iterator& addEquals
312  (Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
313  virtual Mesh::Iterator& subEquals
314  (Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
315  virtual Mesh::Iterator& addOne(Mesh::Iterator& lhs) const;
316  virtual Mesh::Iterator& subOne(Mesh::Iterator& lhs) const;
317  // Check relative position
318  virtual bool isGreater
319  (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
320 
321  private:
322  std::vector<double> x_m;
323  std::vector<double> y_m;
324  std::vector<double> z_m;
325  int xSize_m;
326  int ySize_m;
327  int zSize_m;
328  std::vector<VectorMap*> maps_m;
330 
335  friend Mesh::Iterator operator-
336  (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
337  friend Mesh::Iterator operator+
338  (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
339  friend Mesh::Iterator& operator-=
340  (Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
341  friend Mesh::Iterator& operator+=
342  (Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
343 
344  friend bool operator==(const Mesh::Iterator& lhs,
345  const Mesh::Iterator& rhs);
346  friend bool operator!=(const Mesh::Iterator& lhs,
347  const Mesh::Iterator& rhs);
348  friend bool operator>=(const Mesh::Iterator& lhs,
349  const Mesh::Iterator& rhs);
350  friend bool operator<=(const Mesh::Iterator& lhs,
351  const Mesh::Iterator& rhs);
352  friend bool operator< (const Mesh::Iterator& lhs,
353  const Mesh::Iterator& rhs);
354  friend bool operator> (const Mesh::Iterator& lhs,
355  const Mesh::Iterator& rhs);
356 };
357 
359  double *x = new double[x_m.size()];
360  for (unsigned int i = 0; i < x_m.size(); i++)
361  x[i] = x_m[i];
362  return x;
363 }
364 
366  double *y = new double[y_m.size()];
367  for (unsigned int i = 0; i < y_m.size(); i++)
368  y[i] = y_m[i];
369  return y;
370 }
371 
373  double *z = new double[z_m.size()];
374  for (unsigned int i = 0; i < z_m.size(); i++)
375  z[i] = z_m[i];
376  return z;
377 }
378 
379 void ThreeDGrid::lowerBound(const double& x, int& xIndex,
380  const double& y, int& yIndex,
381  const double& z, int& zIndex) const {
382  xLowerBound(x, xIndex);
383  yLowerBound(y, yIndex);
384  zLowerBound(z, zIndex);
385 }
386 
387 void ThreeDGrid::lowerBound(const double& x,
388  const double& y,
389  const double& z,
390  Mesh::Iterator& it) const {
391  xLowerBound(x, it[0]);
392  yLowerBound(y, it[1]);
393  zLowerBound(z, it[2]);
394  it[0]++;
395  it[1]++;
396  it[2]++;
397 }
398 
399 void ThreeDGrid::xLowerBound(const double& x, int& xIndex) const {
400  if (constantSpacing_m)
401  xIndex = static_cast<int>(std::floor((x - x_m[0])/(x_m[1]-x_m[0]) ));
402  else {
403  vectorLowerBound(x_m, x, xIndex);
404  }
405 }
406 
407 
408 void ThreeDGrid::yLowerBound(const double& y, int& yIndex) const {
409  if (constantSpacing_m)
410  yIndex = static_cast<int>(std::floor((y - y_m[0])/(y_m[1]-y_m[0])));
411  else
412  vectorLowerBound(y_m, y, yIndex);
413 }
414 
415 void ThreeDGrid::zLowerBound(const double& z, int& zIndex) const {
416  if (constantSpacing_m)
417  zIndex = static_cast<int>(std::floor((z - z_m[0])/(z_m[1]-z_m[0])));
418  else
419  vectorLowerBound(z_m, z, zIndex);
420 }
421 
422 double ThreeDGrid::minX() const {
423  return x_m[0];
424 }
425 
426 double ThreeDGrid::maxX() const {
427  return x_m[xSize_m-1];
428 }
429 
430 double ThreeDGrid::minY() const {
431  return y_m[0];
432 }
433 
434 double ThreeDGrid::maxY() const {
435  return y_m[ySize_m-1];
436 }
437 
438 double ThreeDGrid::minZ() const {
439  return z_m[0];
440 }
441 
442 double ThreeDGrid::maxZ() const {
443  return z_m[zSize_m-1];
444 }
445 
446 void ThreeDGrid::setX(int nXCoords, double * x) {
447  x_m = std::vector<double>(x, x+nXCoords);
448 }
449 
450 void ThreeDGrid::setY(int nYCoords, double * y) {
451  y_m = std::vector<double>(y, y+nYCoords);
452 }
453 
454 void ThreeDGrid::setZ(int nZCoords, double * z) {
455  z_m = std::vector<double>(z, z+nZCoords);
456 }
457 
459  return 3;
460 }
461 
462 int ThreeDGrid::toInteger(const Mesh::Iterator& lhs) const {
463  return (lhs.getState()[0]-1)*zSize_m*ySize_m+
464  (lhs.getState()[1]-1)*zSize_m+
465  (lhs.getState()[2]-1);
466 }
467 
468 void ThreeDGrid::setConstantSpacing(bool spacing) {
469  constantSpacing_m = spacing;
470 }
471 
473  return constantSpacing_m;
474 }
475 }
476 #endif
477 
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:733
Base class for meshing routines.
Definition: Mesh.h:49
std::vector< int > getState() const
const double & x(const int &i) const
Definition: ThreeDGrid.h:140
Mesh::Iterator begin() const
Definition: ThreeDGrid.cpp:91
void yLowerBound(const double &y, int &yIndex) const
Definition: ThreeDGrid.h:408
std::vector< double > xVector()
Definition: ThreeDGrid.h:164
friend bool operator>(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
void setY(int nYCoords, double *y)
Definition: ThreeDGrid.h:450
std::vector< double > zVector()
Definition: ThreeDGrid.h:170
void setZ(int nZCoords, double *z)
Definition: ThreeDGrid.h:454
friend bool operator<(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
std::vector< double > y_m
Definition: ThreeDGrid.h:323
virtual void getPosition(const Mesh::Iterator &it, double *position) const
Definition: ThreeDGrid.cpp:101
friend Mesh::Iterator & operator--(Mesh::Iterator &lhs)
friend Mesh::Iterator & operator++(Mesh::Iterator &lhs)
static void vectorLowerBound(std::vector< double > vec, double x, int &index)
Definition: ThreeDGrid.cpp:168
Mesh::Iterator end() const
Definition: ThreeDGrid.cpp:95
std::vector< double > x_m
Definition: ThreeDGrid.h:322
ThreeDGrid * clone()
Definition: ThreeDGrid.h:62
virtual Mesh::Iterator & subEquals(Mesh::Iterator &lhs, int difference) const
Definition: ThreeDGrid.cpp:147
void remove(VectorMap *map)
Definition: ThreeDGrid.cpp:245
friend bool operator!=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
std::vector< double > z_m
Definition: ThreeDGrid.h:324
std::vector< double > yVector()
Definition: ThreeDGrid.h:167
void zLowerBound(const double &z, int &zIndex) const
Definition: ThreeDGrid.h:415
bool getConstantSpacing() const
Definition: ThreeDGrid.h:472
void xLowerBound(const double &x, int &xIndex) const
Definition: ThreeDGrid.h:399
friend bool operator==(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
virtual Mesh::Iterator & subOne(Mesh::Iterator &lhs) const
Definition: ThreeDGrid.cpp:215
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: ThreeDGrid.cpp:231
double & y(const int &j)
Definition: ThreeDGrid.h:128
void setX(int nXCoords, double *x)
Definition: ThreeDGrid.h:446
const double & y(const int &j) const
Definition: ThreeDGrid.h:146
int getPositionDimension() const
Definition: ThreeDGrid.h:458
void add(VectorMap *map)
Definition: ThreeDGrid.cpp:257
const double & z(const int &j) const
Definition: ThreeDGrid.h:152
virtual Mesh::Iterator & addOne(Mesh::Iterator &lhs) const
Definition: ThreeDGrid.cpp:202
virtual Mesh::Iterator & addEquals(Mesh::Iterator &lhs, int difference) const
Definition: ThreeDGrid.cpp:125
int toInteger(const Mesh::Iterator &lhs) const
Definition: ThreeDGrid.h:462
void lowerBound(const double &x, int &xIndex, const double &y, int &yIndex, const double &z, int &zIndex) const
Definition: ThreeDGrid.h:379
double & x(const int &i)
Definition: ThreeDGrid.h:122
friend bool operator>=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
std::vector< VectorMap * > maps_m
Definition: ThreeDGrid.h:328
double & z(const int &k)
Definition: ThreeDGrid.h:134
Mesh::Iterator getNearest(const double *position) const
Definition: ThreeDGrid.cpp:284
friend Mesh::Iterator operator++(Mesh::Iterator &lhs, int)
Definition: Vec.h:22