OPAL (Object Oriented Parallel Accelerator Library)  2024.1
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 
331  friend Mesh::Iterator operator++(Mesh::Iterator& lhs, int);
332  friend Mesh::Iterator operator--(Mesh::Iterator& lhs, int);
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 
std::vector< double > xVector()
Definition: ThreeDGrid.h:164
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
double minY() const
Definition: ThreeDGrid.h:430
std::vector< double > yVector()
Definition: ThreeDGrid.h:167
std::vector< double > z_m
Definition: ThreeDGrid.h:324
bool getConstantSpacing() const
Definition: ThreeDGrid.h:472
void yLowerBound(const double &y, int &yIndex) const
Definition: ThreeDGrid.h:408
friend bool operator<(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
Definition: Mesh-inl.icc:168
std::vector< double > zVector()
Definition: ThreeDGrid.h:170
double maxY() const
Definition: ThreeDGrid.h:434
double maxX() const
Definition: ThreeDGrid.h:426
double minX() const
Definition: ThreeDGrid.h:422
int toInteger(const Mesh::Iterator &lhs) const
Definition: ThreeDGrid.h:462
double & x(const int &i)
Definition: ThreeDGrid.h:122
virtual bool isGreater(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs) const
Definition: ThreeDGrid.cpp:231
std::vector< double > x_m
Definition: ThreeDGrid.h:322
void add(VectorMap *map)
Definition: ThreeDGrid.cpp:257
friend bool operator==(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
Definition: Mesh-inl.icc:172
Mesh::Iterator begin() const
Definition: ThreeDGrid.cpp:91
void lowerBound(const double &x, int &xIndex, const double &y, int &yIndex, const double &z, int &zIndex) const
Definition: ThreeDGrid.h:379
Base class for meshing routines.
Definition: Mesh.h:49
std::vector< VectorMap * > maps_m
Definition: ThreeDGrid.h:328
const double & z(const int &j) const
Definition: ThreeDGrid.h:152
virtual void getPosition(const Mesh::Iterator &it, double *position) const
Definition: ThreeDGrid.cpp:101
const double & y(const int &j) const
Definition: ThreeDGrid.h:146
virtual Mesh::Iterator & subEquals(Mesh::Iterator &lhs, int difference) const
Definition: ThreeDGrid.cpp:147
friend Mesh::Iterator operator++(Mesh::Iterator &lhs, int)
Definition: Mesh-inl.icc:98
std::vector< double > y_m
Definition: ThreeDGrid.h:323
friend Mesh::Iterator operator--(Mesh::Iterator &lhs, int)
Definition: Mesh-inl.icc:104
void setY(int nYCoords, double *y)
Definition: ThreeDGrid.h:450
Definition: Vec.h:21
virtual Mesh::Iterator & subOne(Mesh::Iterator &lhs) const
Definition: ThreeDGrid.cpp:215
void setZ(int nZCoords, double *z)
Definition: ThreeDGrid.h:454
Mesh::Iterator getNearest(const double *position) const
Definition: ThreeDGrid.cpp:284
void zLowerBound(const double &z, int &zIndex) const
Definition: ThreeDGrid.h:415
int getPositionDimension() const
Definition: ThreeDGrid.h:458
ThreeDGrid * clone()
Definition: ThreeDGrid.h:62
friend bool operator!=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
Definition: Mesh-inl.icc:178
double minZ() const
Definition: ThreeDGrid.h:438
const double & x(const int &i) const
Definition: ThreeDGrid.h:140
friend bool operator>(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
Definition: Mesh-inl.icc:164
double & y(const int &j)
Definition: ThreeDGrid.h:128
double maxZ() const
Definition: ThreeDGrid.h:442
std::vector< int > getState() const
Definition: Mesh-inl.icc:58
friend bool operator>=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
Definition: Mesh-inl.icc:154
virtual Mesh::Iterator & addEquals(Mesh::Iterator &lhs, int difference) const
Definition: ThreeDGrid.cpp:125
static void vectorLowerBound(std::vector< double > vec, double x, int &index)
Definition: ThreeDGrid.cpp:168
double & z(const int &k)
Definition: ThreeDGrid.h:134
Mesh::Iterator end() const
Definition: ThreeDGrid.cpp:95
void setX(int nXCoords, double *x)
Definition: ThreeDGrid.h:446
friend bool operator<=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
Definition: Mesh-inl.icc:159
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:733
virtual Mesh::Iterator & addOne(Mesh::Iterator &lhs) const
Definition: ThreeDGrid.cpp:202
void xLowerBound(const double &x, int &xIndex) const
Definition: ThreeDGrid.h:399