2 #include "Interface/Mesh.hh"
3 #include "Utils/Exception.hh"
8 TwoDGrid::TwoDGrid() : _x(2,0), _y(2,0), _xSize(2), _ySize(2), _maps(), _constantSpacing(false)
10 _x[1] = 1.;
_y[1] = 1.;
13 TwoDGrid::TwoDGrid(
double dX,
double dY,
double minX,
double minY,
int numberOfXCoords,
int numberOfYCoords)
14 : _x(), _y(), _xSize(numberOfXCoords), _ySize(numberOfYCoords), _maps(), _constantSpacing(true)
16 if(numberOfXCoords < 2 || numberOfYCoords < 2)
throw(MAUS::Exception(MAUS::Exception::recoverable,
"2D Grid must be at least 2x2 grid",
"TwoDGrid::TwoDGrid(...)"));
17 for(
int i=0; i<numberOfXCoords; i++)
_x.push_back(minX+i*dX);
18 for(
int j=0; j<numberOfYCoords; j++)
_y.push_back(minY+j*dY);
22 TwoDGrid::TwoDGrid(
int xSize,
const double *x,
int ySize,
const double *y) : _x(x, x+xSize), _y(y, y+ySize), _xSize(xSize), _ySize(ySize), _maps(), _constantSpacing(false)
24 if(xSize < 2 || ySize < 2)
throw(MAUS::Exception(MAUS::Exception::recoverable,
"2D Grid must be at least 2x2 grid",
"TwoDGrid::TwoDGrid(...)"));
30 TwoDGrid::TwoDGrid(std::vector<double> x, std::vector<double> y) : _x (x), _y(y), _xSize(x.size()), _ySize(y.size()), _maps(), _constantSpacing(false)
32 if(
_xSize < 2 ||
_ySize < 2)
throw(MAUS::Exception(MAUS::Exception::recoverable,
"2D Grid must be at least 2x2 grid",
"TwoDGrid::TwoDGrid(...)"));
42 return Mesh::Iterator(std::vector<int>(2,1),
this);
47 std::vector<int> end(2, 1); end[0] =
_xSize+1;
return Mesh::Iterator(end,
this);
52 position[0] =
x(it._state[0]);
53 position[1] =
y(it._state[1]);
59 else if( it._state[0]<0 ) position[0] =
MinX() + (
x(2)-
x(1))*(it._state[0]-0.5);
60 else position[0] = 0.5*(
x(it._state[0]+1)+
x(it._state[0]));
63 else if( it._state[1]<0 ) position[1] =
MinY() + (
y(2)-
y(1))*(it._state[1]-0.5);
64 else position[1] = 0.5*(
y(it._state[1]+1)+
y(it._state[1]));
69 if(difference < 0)
return SubEquals(lhs, -difference);
70 lhs._state[0] += difference/(
_ySize);
71 lhs._state[1] += difference%(
_ySize);
72 if(lhs._state[1] >
_ySize) {lhs._state[1] -=
_ySize; lhs._state[0]++;}
78 if(difference < 0)
return AddEquals(lhs, -difference);
79 lhs._state[0] -= difference/(
_ySize);
80 lhs._state[1] -= difference%(
_ySize);
81 if(lhs._state[1] < 1) {lhs._state[1] +=
_ySize; lhs._state[0]--;}
97 if(lhs._state[1] == this->ySize()) {lhs._state[1] = 1; ++lhs._state[0];}
104 if(lhs._state[1] == 1) {lhs._state[1] = this->
ySize(); --lhs._state[0];}
105 else --lhs._state[1];
111 if(lhs._state[0] > rhs._state[0])
return true;
112 else if (lhs._state[0] == rhs._state[0] && lhs._state[1] > rhs._state[1])
return true;
120 if(
_maps.begin() ==
_maps.end()) {
delete this; }
132 for (
unsigned int i = 0; i <
_x.size()-1; i++)
135 for (
unsigned int i = 0; i <
_y.size()-1; i++)
142 std::vector<int> index(2);
143 LowerBound(position[0], index[0], position[1], index[1]);
144 if(index[0] <
_xSize-1 && index[0] >= 0)
145 index[0] += (2*(position[0] -
_x[index[0]]) >
_x[index[0]+1]-
_x[index[0]] ? 2 : 1);
148 if(index[1] <
_ySize-1 && index[1] >= 0)
149 index[1] += (2*(position[1] -
_y[index[1]]) >
_y[index[1]+1]-
_y[index[1]] ? 2 : 1);
152 if(index[0] < 1) index[0] = 1;
153 if(index[1] < 1) index[1] = 1;
156 return Mesh::Iterator(index,
this);
166 for(
unsigned int i=0; i<2; i++) {
_coord.push_back(std::vector<double>(2,0));
_coord.back()[1] = 1.;}
171 NDGrid::NDGrid(std::vector<int> size, std::vector<const double *> gridCoordinates) : _coord(), _maps(), _constantSpacing(false)
173 for(
unsigned int i=0; i<size.size(); i++)
176 throw(MAUS::Exception(MAUS::Exception::recoverable,
"ND Grid must be at least 2x2x...x2 grid",
"NDGrid::NDGrid(...)"));
177 _coord.push_back(std::vector<double>(gridCoordinates[i], gridCoordinates[i] + size[i]) );
182 NDGrid::NDGrid(
int nDimensions,
int* size,
double* spacing,
double*
min) : _coord(nDimensions), _maps(), _constantSpacing(true)
184 for(
int i=0; i<nDimensions; i++)
186 if(size[i] < 2)
throw(MAUS::Exception(MAUS::Exception::recoverable,
"ND Grid must be at least 2x2x...x2 grid",
"NDGrid::NDGrid(...)"));
187 _coord[i] = std::vector<double>(size[i]);
188 for(
unsigned int j=0; j<
_coord[i].size(); j++)
_coord[i][j] = min[i] + j*spacing[i];
192 NDGrid::NDGrid(std::vector< std::vector<double> > gridCoordinates) : _coord(gridCoordinates), _maps(), _constantSpacing(false)
194 for(
unsigned int i=0; i<gridCoordinates.size(); i++)
195 if(gridCoordinates[i].
size() < 2)
throw(MAUS::Exception(MAUS::Exception::recoverable,
"ND Grid must be at least 2x2x...x2 grid",
"NDGrid::NDGrid(...)"));
202 double * array =
new double[
_coord[dimension].size() ];
203 for(
unsigned int i=0; i<_coord[dimension].size(); i++) array[i] = _coord[dimension][i];
211 if(difference < 0)
return SubEquals(lhs, -difference);
212 std::vector<int> index (
_coord.size(),0);
213 std::vector<int> content(
_coord.size(),1);
214 for(
int i=
int(index.size()-2); i>=0; i--) content[i] = content[i+1]*
_coord[i+1].
size();
215 for(
int i=0; i<int(index.size()); i++)
217 index[i] = difference/content[i];
218 difference -= index[i] * content[i];
220 for(
unsigned int i=0; i<index.size(); i++) lhs._state[i] += index[i];
221 for(
int i=
int(index.size())-1; i>0; i--)
223 if(lhs._state[i] >
int(
_coord[i].size()) )
226 lhs._state[i] -=
_coord[i].size();
235 if(difference < 0)
return AddEquals(lhs, -difference);
236 std::vector<int> index (
_coord.size(),0);
237 std::vector<int> content(
_coord.size(),1);
238 for(
int i=
int(index.size()-2); i>=0; i--) content[i] = content[i+1]*
_coord[i+1].
size();
239 for(
int i=0; i<int(index.size()); i++)
241 index[i] = difference/content[i];
242 difference -= index[i] * content[i];
244 for(
unsigned int i=0; i<index.size(); i++) lhs._state[i] -= index[i];
245 for(
int i=
int(index.size())-1; i>0; i--)
247 if(lhs._state[i] < 1)
250 lhs._state[i] +=
_coord[i].size();
269 while(lhs[i] ==
int(
_coord[i].
size()) && i>0) {lhs[i]=1; i--;}
276 lhs[
_coord.size()-1] -= 1;
279 while(lhs[i] == 0 && i>0)
281 lhs._state[i] =
_coord[i].size();
291 for(
unsigned int i=0; i<
_coord.size(); i++)
292 for(
unsigned int j=0; j<
_coord[i].size()-1; j++)
300 while(lhs._state[i] == rhs._state[i] && i<rhs._state.size()-1) i++;
301 return (lhs[i] > rhs[i]);
307 std::vector<int> index (
_coord.size(),0);
308 std::vector<int> content(
_coord.size(),1);
309 for(
int i=
int(index.size()-2); i>=0; i--) content[i] = content[i+1]*
_coord[i+1].
size();
310 for(
int i=0; i<int(index.size()); i++) difference += (lhs._state[i]-1) * (content[i]);
317 std::vector<int> index(
_coord.size());
318 std::vector<double> pos (position, position+
_coord.size());
320 for(
unsigned int i=0; i<
_coord.size(); i++)
322 if(index[i] <
int(
_coord[i].
size()-1) && index[i] >= 0) index[i] += (2*(position[i] -
_coord[i][index[i]]) >
_coord[i][index[i]+1]-
_coord[i][index[i]] ? 2 : 1);
324 if(index[i] < 1) index[i] = 1;
327 return Mesh::Iterator(index,
this);
virtual Mesh::Iterator & SubOne(Mesh::Iterator &lhs) const
std::vector< VectorMap * > _maps
void SetConstantSpacing()
constexpr double e
The value of .
void LowerBound(const std::vector< double > &pos, std::vector< int > &xIndex) const
bool IsGreater(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs) const
PETE_TUTree< FnFabs, typename T::PETE_Expr_t > fabs(const PETE_Expr< T > &l)
virtual bool IsGreater(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs) const
virtual void Position(const Mesh::Iterator &it, double *position) const
std::vector< std::vector< double > > _coord
Mesh::Iterator & SubEquals(Mesh::Iterator &lhs, int difference) const
virtual void CentrePosition(const Mesh::Iterator &it, double *position) const
Mesh::Iterator End() const
virtual Mesh::Iterator & AddEquals(Mesh::Iterator &lhs, int difference) const
virtual Mesh::Iterator & AddOne(Mesh::Iterator &lhs) const
void LowerBound(const double &x, int &xIndex, const double &y, int &yIndex) const
virtual Mesh::Iterator & SubEquals(Mesh::Iterator &lhs, int difference) const
NDGrid()
////// NDGrid ///////
Mesh::Iterator Nearest(const double *position) const
Mesh::Iterator & SubOne(Mesh::Iterator &lhs) const
Mesh::Iterator Nearest(const double *position) const
Mesh::Iterator & AddOne(Mesh::Iterator &lhs) const
const T * find(const T table[], const std::string &name)
Look up name.
int size(const int &dimension) const
void SetConstantSpacing()
void Remove(VectorMap *map)
int ToInteger(const Mesh::Iterator &lhs) const
Mesh::Iterator Begin() const
std::string::iterator iterator
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Mesh::Iterator & AddEquals(Mesh::Iterator &lhs, int difference) const
double * newCoordArray(const int &dimension) const