OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
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 <math.h>
32 
33 #include <algorithm>
34 #include <vector>
35 
37 
38 namespace interpolation {
39 
58 class ThreeDGrid : public Mesh {
59  public:
60  class Iterator;
61 
63  ThreeDGrid * clone() {return new ThreeDGrid(*this);}
64 
66  Mesh * dual () const;
67 
69  ThreeDGrid();
70 
85  ThreeDGrid(double dX, double dY, double dZ,
86  double minX, double minY, double minZ,
87  int numberOfXCoords, int numberOfYCoords, int numberOfZCoords);
88 
101  ThreeDGrid(int xSize, const double *x,
102  int ySize, const double *y,
103  int zSize, const double *z);
104 
112  ThreeDGrid(std::vector<double> x,
113  std::vector<double> y,
114  std::vector<double> z);
115 
117  ~ThreeDGrid();
118 
123  inline double& x(const int& i) {return x_m[i-1];}
124 
129  inline double& y(const int& j) {return y_m[j-1];}
130 
135  inline double& z(const int& k) {return z_m[k-1];}
136 
141  inline const double& x(const int& i) const {return x_m[i-1];}
142 
147  inline const double& y(const int& j) const {return y_m[j-1];}
148 
153  inline const double& z(const int& j) const {return z_m[j-1];}
154 
156  inline int xSize() const {return static_cast<int>(x_m.size());}
157 
159  inline int ySize() const {return static_cast<int>(y_m.size());}
160 
162  inline int zSize() const {return static_cast<int>(z_m.size());}
163 
165  std::vector<double> xVector() {return std::vector<double>(x_m);}
166 
168  std::vector<double> yVector() {return std::vector<double>(y_m);}
169 
171  std::vector<double> zVector() {return std::vector<double>(z_m);}
172 
174  inline double* newXArray();
175 
177  inline double* newYArray();
178 
180  inline double* newZArray();
181 
187  inline void xLowerBound(const double& x, int& xIndex) const;
188 
194  inline void yLowerBound(const double& y, int& yIndex) const;
195 
201  inline void zLowerBound(const double& z, int& zIndex) const;
202 
208  inline void lowerBound(const double& x, int& xIndex,
209  const double& y, int& yIndex,
210  const double& z, int& zIndex) const;
211 
217  inline void lowerBound(const double& x, const double& y, const double& z,
218  Mesh::Iterator& it) const;
219 
221  inline double minX() const;
222 
224  inline double maxX() const;
225 
227  inline double minY() const;
228 
230  inline double maxY() const;
231 
233  inline double minZ() const;
234 
236  inline double maxZ() const;
237 
239  inline void setX(int nXCoords, double * x);
240 
242  inline void setY(int nYCoords, double * y);
243 
245  inline void setZ(int nZCoords, double * z);
246 
248  void add(VectorMap* map);
249 
252  void remove(VectorMap* map);
253 
255  Mesh::Iterator begin() const;
256 
258  Mesh::Iterator end() const;
259 
266  virtual void getPosition(const Mesh::Iterator& it, double * position) const;
267 
269  inline int getPositionDimension() const;
270 
277  inline int toInteger(const Mesh::Iterator& lhs) const;
278 
284  inline void setConstantSpacing(bool spacing);
285 
288  void setConstantSpacing();
289 
291  inline bool getConstantSpacing() const;
292 
294  Mesh::Iterator getNearest(const double* position) const;
295 
304  static void vectorLowerBound(std::vector<double> vec, double x, int& index);
305 
306  protected:
307  // Change position
309  int difference) const;
311  int difference) const;
312  virtual Mesh::Iterator& addEquals
313  (Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
314  virtual Mesh::Iterator& subEquals
315  (Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
316  virtual Mesh::Iterator& addOne(Mesh::Iterator& lhs) const;
317  virtual Mesh::Iterator& subOne(Mesh::Iterator& lhs) const;
318  // Check relative position
319  virtual bool isGreater
320  (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
321 
322  private:
323  std::vector<double> x_m;
324  std::vector<double> y_m;
325  std::vector<double> z_m;
326  int xSize_m;
327  int ySize_m;
328  int zSize_m;
329  std::vector<VectorMap*> maps_m;
331 
332  friend Mesh::Iterator operator++(Mesh::Iterator& lhs, int);
333  friend Mesh::Iterator operator--(Mesh::Iterator& lhs, int);
336  friend Mesh::Iterator operator-
337  (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
338  friend Mesh::Iterator operator+
339  (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
340  friend Mesh::Iterator& operator-=
341  (Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
342  friend Mesh::Iterator& operator+=
343  (Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
344 
345  friend bool operator==(const Mesh::Iterator& lhs,
346  const Mesh::Iterator& rhs);
347  friend bool operator!=(const Mesh::Iterator& lhs,
348  const Mesh::Iterator& rhs);
349  friend bool operator>=(const Mesh::Iterator& lhs,
350  const Mesh::Iterator& rhs);
351  friend bool operator<=(const Mesh::Iterator& lhs,
352  const Mesh::Iterator& rhs);
353  friend bool operator< (const Mesh::Iterator& lhs,
354  const Mesh::Iterator& rhs);
355  friend bool operator> (const Mesh::Iterator& lhs,
356  const Mesh::Iterator& rhs);
357 };
358 
360  double *x = new double[x_m.size()];
361  for (unsigned int i = 0; i < x_m.size(); i++)
362  x[i] = x_m[i];
363  return x;
364 }
365 
367  double *y = new double[y_m.size()];
368  for (unsigned int i = 0; i < y_m.size(); i++)
369  y[i] = y_m[i];
370  return y;
371 }
372 
374  double *z = new double[z_m.size()];
375  for (unsigned int i = 0; i < z_m.size(); i++)
376  z[i] = z_m[i];
377  return z;
378 }
379 
380 void ThreeDGrid::lowerBound(const double& x, int& xIndex,
381  const double& y, int& yIndex,
382  const double& z, int& zIndex) const {
383  xLowerBound(x, xIndex);
384  yLowerBound(y, yIndex);
385  zLowerBound(z, zIndex);
386 }
387 
388 void ThreeDGrid::lowerBound(const double& x,
389  const double& y,
390  const double& z,
391  Mesh::Iterator& it) const {
392  xLowerBound(x, it[0]);
393  yLowerBound(y, it[1]);
394  zLowerBound(z, it[2]);
395  it[0]++;
396  it[1]++;
397  it[2]++;
398 }
399 
400 void ThreeDGrid::xLowerBound(const double& x, int& xIndex) const {
401  if (constantSpacing_m)
402  xIndex = static_cast<int>(floor((x - x_m[0])/(x_m[1]-x_m[0]) ));
403  else {
404  vectorLowerBound(x_m, x, xIndex);
405  }
406 }
407 
408 
409 void ThreeDGrid::yLowerBound(const double& y, int& yIndex) const {
410  if (constantSpacing_m)
411  yIndex = static_cast<int>(floor((y - y_m[0])/(y_m[1]-y_m[0])));
412  else
413  vectorLowerBound(y_m, y, yIndex);
414 }
415 
416 void ThreeDGrid::zLowerBound(const double& z, int& zIndex) const {
417  if (constantSpacing_m)
418  zIndex = static_cast<int>(floor((z - z_m[0])/(z_m[1]-z_m[0])));
419  else
420  vectorLowerBound(z_m, z, zIndex);
421 }
422 
423 double ThreeDGrid::minX() const {
424  return x_m[0];
425 }
426 
427 double ThreeDGrid::maxX() const {
428  return x_m[xSize_m-1];
429 }
430 
431 double ThreeDGrid::minY() const {
432  return y_m[0];
433 }
434 
435 double ThreeDGrid::maxY() const {
436  return y_m[ySize_m-1];
437 }
438 
439 double ThreeDGrid::minZ() const {
440  return z_m[0];
441 }
442 
443 double ThreeDGrid::maxZ() const {
444  return z_m[zSize_m-1];
445 }
446 
447 void ThreeDGrid::setX(int nXCoords, double * x) {
448  x_m = std::vector<double>(x, x+nXCoords);
449 }
450 
451 void ThreeDGrid::setY(int nYCoords, double * y) {
452  y_m = std::vector<double>(y, y+nYCoords);
453 }
454 
455 void ThreeDGrid::setZ(int nZCoords, double * z) {
456  z_m = std::vector<double>(z, z+nZCoords);
457 }
458 
460  return 3;
461 }
462 
463 int ThreeDGrid::toInteger(const Mesh::Iterator& lhs) const {
464  return (lhs.getState()[0]-1)*zSize_m*ySize_m+
465  (lhs.getState()[1]-1)*zSize_m+
466  (lhs.getState()[2]-1);
467 }
468 
469 void ThreeDGrid::setConstantSpacing(bool spacing) {
470  constantSpacing_m = spacing;
471 }
472 
474  return constantSpacing_m;
475 }
476 }
477 #endif
478 
void xLowerBound(const double &x, int &xIndex) const
Definition: ThreeDGrid.h:400
Mesh::Iterator getNearest(const double *position) const
Definition: ThreeDGrid.cpp:284
double minY() const
Definition: ThreeDGrid.h:431
Mesh::Iterator end() const
Definition: ThreeDGrid.cpp:95
std::vector< double > xVector()
Definition: ThreeDGrid.h:165
const double & z(const int &j) const
Definition: ThreeDGrid.h:153
void zLowerBound(const double &z, int &zIndex) const
Definition: ThreeDGrid.h:416
friend bool operator!=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
std::vector< double > z_m
Definition: ThreeDGrid.h:325
ThreeDGrid * clone()
Definition: ThreeDGrid.h:63
double & z(const int &k)
Definition: ThreeDGrid.h:135
friend Mesh::Iterator operator++(Mesh::Iterator &lhs, int)
void add(VectorMap *map)
Definition: ThreeDGrid.cpp:257
void setZ(int nZCoords, double *z)
Definition: ThreeDGrid.h:455
virtual Mesh::Iterator & subOne(Mesh::Iterator &lhs) const
Definition: ThreeDGrid.cpp:215
std::vector< double > zVector()
Definition: ThreeDGrid.h:171
const double & x(const int &i) const
Definition: ThreeDGrid.h:141
void setY(int nYCoords, double *y)
Definition: ThreeDGrid.h:451
const double & y(const int &j) const
Definition: ThreeDGrid.h:147
std::vector< double > y_m
Definition: ThreeDGrid.h:324
bool getConstantSpacing() const
Definition: ThreeDGrid.h:473
virtual Mesh::Iterator & subEquals(Mesh::Iterator &lhs, int difference) const
Definition: ThreeDGrid.cpp:147
friend bool operator>=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
double & x(const int &i)
Definition: ThreeDGrid.h:123
virtual void getPosition(const Mesh::Iterator &it, double *position) const
Definition: ThreeDGrid.cpp:101
void yLowerBound(const double &y, int &yIndex) const
Definition: ThreeDGrid.h:409
double minX() const
Definition: ThreeDGrid.h:423
friend bool operator>(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
Mesh::Iterator begin() const
Definition: ThreeDGrid.cpp:91
virtual bool isGreater(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs) const
Definition: ThreeDGrid.cpp:231
int toInteger(const Mesh::Iterator &lhs) const
Definition: ThreeDGrid.h:463
double maxZ() const
Definition: ThreeDGrid.h:443
static void vectorLowerBound(std::vector< double > vec, double x, int &index)
Definition: ThreeDGrid.cpp:168
void setX(int nXCoords, double *x)
Definition: ThreeDGrid.h:447
virtual Mesh::Iterator & addEquals(Mesh::Iterator &lhs, int difference) const
Definition: ThreeDGrid.cpp:125
double maxY() const
Definition: ThreeDGrid.h:435
double maxX() const
Definition: ThreeDGrid.h:427
Definition: Vec.h:21
Base class for meshing routines.
Definition: Mesh.h:49
friend bool operator==(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
std::vector< VectorMap * > maps_m
Definition: ThreeDGrid.h:329
double & y(const int &j)
Definition: ThreeDGrid.h:129
std::vector< int > getState() const
friend Mesh::Iterator operator--(Mesh::Iterator &lhs, int)
virtual Mesh::Iterator & addOne(Mesh::Iterator &lhs) const
Definition: ThreeDGrid.cpp:202
friend bool operator<(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
void lowerBound(const double &x, int &xIndex, const double &y, int &yIndex, const double &z, int &zIndex) const
Definition: ThreeDGrid.h:380
int getPositionDimension() const
Definition: ThreeDGrid.h:459
friend bool operator<=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
std::vector< double > yVector()
Definition: ThreeDGrid.h:168
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:816
std::vector< double > x_m
Definition: ThreeDGrid.h:323
double minZ() const
Definition: ThreeDGrid.h:439