Version: SMASH-2.0
smash::RectangularLattice< T > Class Template Reference

#include <lattice.h>

template<typename T>
class smash::RectangularLattice< T >

A container class to hold all the arrays on the lattice and access them.

Template Parameters
TThe type of the contained values.

Definition at line 47 of file lattice.h.

Collaboration diagram for smash::RectangularLattice< T >:
[legend]

Public Types

using iterator = typename std::vector< T >::iterator
 Iterator of lattice. More...
 
using const_iterator = typename std::vector< T >::const_iterator
 Const interator of lattice. More...
 

Public Member Functions

 RectangularLattice (const std::array< double, 3 > &l, const std::array< int, 3 > &n, const std::array< double, 3 > &orig, bool per, const LatticeUpdate upd)
 Rectangular lattice constructor. More...
 
 RectangularLattice (RectangularLattice< T > const &rl)
 Copy-constructor. More...
 
void reset ()
 Sets all values on lattice to zeros. More...
 
bool out_of_bounds (int ix, int iy, int iz) const
 Checks if 3D index is out of lattice bounds. More...
 
ThreeVector cell_center (int ix, int iy, int iz) const
 Find the coordinates of a given cell. More...
 
ThreeVector cell_center (int index) const
 Find the coordinate of cell center given the 1d index of the cell. More...
 
const std::array< double, 3 > & lattice_sizes () const
 
const std::array< int, 3 > & dimensions () const
 
const std::array< double, 3 > & cell_sizes () const
 
const std::array< double, 3 > & origin () const
 
bool periodic () const
 
LatticeUpdate when_update () const
 
iterator begin ()
 
const_iterator begin () const
 
iterator end ()
 
const_iterator end () const
 
T & operator[] (std::size_t i)
 
const T & operator[] (std::size_t i) const
 
std::size_t size () const
 
T & node (int ix, int iy, int iz)
 Take the value of a cell given its 3-D indices. More...
 
bool value_at (const ThreeVector &r, T &value)
 Interpolates lattice quantity to coordinate r. More...
 
template<typename F >
void iterate_sublattice (const std::array< int, 3 > &lower_bounds, const std::array< int, 3 > &upper_bounds, F &&func)
 A sub-lattice iterator, which iterates in a 3D-structured manner and calls a function on every cell. More...
 
template<typename F >
void iterate_in_radius (const ThreeVector &point, const double r_cut, F &&func)
 Iterates only nodes, whose cell centers lie not further than r_cut in x, y, z directions from the given point and applies a function to each node. More...
 
template<typename L >
bool identical_to_lattice (const L *lat) const
 Checks if lattices of possibly different types have identical structure. More...
 

Protected Attributes

std::vector< T > lattice_
 The lattice itself, array containing physical quantities. More...
 
const std::array< double, 3 > lattice_sizes_
 Lattice sizes in x, y, z directions. More...
 
const std::array< int, 3 > n_cells_
 Number of cells in x,y,z directions. More...
 
const std::array< double, 3 > cell_sizes_
 Cell sizes in x, y, z directions. More...
 
const std::array< double, 3 > origin_
 Coordinates of the left down nearer corner. More...
 
const bool periodic_
 Whether the lattice is periodic. More...
 
const LatticeUpdate when_update_
 When the lattice should be recalculated. More...
 

Private Member Functions

int positive_modulo (int i, int n) const
 Returns division modulo, which is always between 0 and n-1 in is not suitable, because it returns results from -(n-1) to n-1. More...
 

Member Typedef Documentation

◆ iterator

template<typename T>
using smash::RectangularLattice< T >::iterator = typename std::vector<T>::iterator

Iterator of lattice.

Definition at line 170 of file lattice.h.

◆ const_iterator

template<typename T>
using smash::RectangularLattice< T >::const_iterator = typename std::vector<T>::const_iterator

Const interator of lattice.

Definition at line 172 of file lattice.h.

Constructor & Destructor Documentation

◆ RectangularLattice() [1/2]

template<typename T>
smash::RectangularLattice< T >::RectangularLattice ( const std::array< double, 3 > &  l,
const std::array< int, 3 > &  n,
const std::array< double, 3 > &  orig,
bool  per,
const LatticeUpdate  upd 
)
inline

Rectangular lattice constructor.

Parameters
[in]l3-dimensional array (lx,ly,lz) indicates the size of the lattice in x, y, z directions respectively [fm].
[in]n3-dimensional array (nx,ny,nz) indicates the number of cells of the lattice in x, y, z directions respectively. Each cell in the lattice is labeled by three integers i, j, k where \(i\in[0, nx-1]\), \(j\in[0, ny-1]\), \(k\in[0, nz-1]\). The sizes of each cell are given by lx/nx, ly/ny, lz/nz in x,y,z directions respectively.
[in]origA 3-dimensional array indicating the coordinates of the origin [fm].
[in]perBoolean indicating whether a periodic boundary condition is applied.
[in]updEnum indicating how frequently the lattice is updated.

Definition at line 66 of file lattice.h.

70  : lattice_sizes_(l),
71  n_cells_(n),
72  cell_sizes_{l[0] / n[0], l[1] / n[1], l[2] / n[2]},
73  origin_(orig),
74  periodic_(per),
75  when_update_(upd) {
76  lattice_.resize(n_cells_[0] * n_cells_[1] * n_cells_[2]);
77  logg[LLattice].debug(
78  "Rectangular lattice created: sizes[fm] = (", lattice_sizes_[0], ",",
79  lattice_sizes_[1], ",", lattice_sizes_[2], "), dims = (", n_cells_[0],
80  ",", n_cells_[1], ",", n_cells_[2], "), origin = (", origin_[0], ",",
81  origin_[1], ",", origin_[2], "), periodic: ", periodic_);
82  if (n_cells_[0] < 1 || n_cells_[1] < 1 || n_cells_[2] < 1 ||
83  lattice_sizes_[0] < 0.0 || lattice_sizes_[1] < 0.0 ||
84  lattice_sizes_[2] < 0.0) {
85  throw std::invalid_argument(
86  "Lattice sizes should be positive, "
87  "lattice dimensions should be > 0.");
88  }
89  }

◆ RectangularLattice() [2/2]

template<typename T>
smash::RectangularLattice< T >::RectangularLattice ( RectangularLattice< T > const &  rl)
inline

Copy-constructor.

Definition at line 92 of file lattice.h.

93  : lattice_(rl.lattice_),
94  lattice_sizes_(rl.lattice_sizes_),
95  n_cells_(rl.n_cells_),
96  cell_sizes_(rl.cell_sizes_),
97  origin_(rl.origin_),
98  periodic_(rl.periodic_),
99  when_update_(rl.when_update_) {}

Member Function Documentation

◆ reset()

template<typename T>
void smash::RectangularLattice< T >::reset ( )
inline

Sets all values on lattice to zeros.

Definition at line 102 of file lattice.h.

102 { std::fill(lattice_.begin(), lattice_.end(), T()); }
Here is the caller graph for this function:

◆ out_of_bounds()

template<typename T>
bool smash::RectangularLattice< T >::out_of_bounds ( int  ix,
int  iy,
int  iz 
) const
inline

Checks if 3D index is out of lattice bounds.

Parameters
[in]ixThe index of the cell in x direction.
[in]iyThe index of the cell in y direction.
[in]izThe index of the cell in z direction.
Returns
Whether the cell is out of the lattice.

Definition at line 112 of file lattice.h.

112  {
113  // clang-format off
114  return !periodic_ &&
115  (ix < 0 || ix >= n_cells_[0] ||
116  iy < 0 || iy >= n_cells_[1] ||
117  iz < 0 || iz >= n_cells_[2]);
118  // clang-format on
119  }
Here is the caller graph for this function:

◆ cell_center() [1/2]

template<typename T>
ThreeVector smash::RectangularLattice< T >::cell_center ( int  ix,
int  iy,
int  iz 
) const
inline

Find the coordinates of a given cell.

Parameters
[in]ixThe index of the cell in x direction.
[in]iyThe index of the cell in y direction.
[in]izThe index of the cell in z direction.
Returns
Coordinates of the center of the given cell [fm].

Definition at line 129 of file lattice.h.

129  {
130  return ThreeVector(origin_[0] + cell_sizes_[0] * (ix + 0.5),
131  origin_[1] + cell_sizes_[1] * (iy + 0.5),
132  origin_[2] + cell_sizes_[2] * (iz + 0.5));
133  }
Here is the caller graph for this function:

◆ cell_center() [2/2]

template<typename T>
ThreeVector smash::RectangularLattice< T >::cell_center ( int  index) const
inline

Find the coordinate of cell center given the 1d index of the cell.

Parameters
[in]index1-dimensional index of the given cell. It can be related to a 3-dimensional one by index = ix + nx (iy + iz * ny).
Returns
Coordinates of the center of the given cell [fm].

Definition at line 143 of file lattice.h.

143  {
144  const int ix = index % n_cells_[0];
145  index = index / n_cells_[0];
146  const int iy = index % n_cells_[1];
147  const int iz = index / n_cells_[1];
148  return cell_center(ix, iy, iz);
149  }

◆ lattice_sizes()

template<typename T>
const std::array<double, 3>& smash::RectangularLattice< T >::lattice_sizes ( ) const
inline
Returns
Lengths of the lattice in x, y, z directions.

Definition at line 152 of file lattice.h.

152 { return lattice_sizes_; }

◆ dimensions()

template<typename T>
const std::array<int, 3>& smash::RectangularLattice< T >::dimensions ( ) const
inline
Returns
Number of cells in x, y, z directions.

Definition at line 155 of file lattice.h.

155 { return n_cells_; }
Here is the caller graph for this function:

◆ cell_sizes()

template<typename T>
const std::array<double, 3>& smash::RectangularLattice< T >::cell_sizes ( ) const
inline
Returns
Lengths of one cell in x, y, z directions.

Definition at line 158 of file lattice.h.

158 { return cell_sizes_; }
Here is the caller graph for this function:

◆ origin()

template<typename T>
const std::array<double, 3>& smash::RectangularLattice< T >::origin ( ) const
inline
Returns
Lattice origin: left, down, near corner coordinates.

Definition at line 161 of file lattice.h.

161 { return origin_; }
Here is the caller graph for this function:

◆ periodic()

template<typename T>
bool smash::RectangularLattice< T >::periodic ( ) const
inline
Returns
If lattice is periodic or not.

Definition at line 164 of file lattice.h.

164 { return periodic_; }

◆ when_update()

template<typename T>
LatticeUpdate smash::RectangularLattice< T >::when_update ( ) const
inline
Returns
The enum, which tells at which time lattice needs to be updated.

Definition at line 167 of file lattice.h.

167 { return when_update_; }
Here is the caller graph for this function:

◆ begin() [1/2]

template<typename T>
iterator smash::RectangularLattice< T >::begin ( )
inline
Returns
First element of lattice.

Definition at line 174 of file lattice.h.

174 { return lattice_.begin(); }

◆ begin() [2/2]

template<typename T>
const_iterator smash::RectangularLattice< T >::begin ( ) const
inline
Returns
First element of lattice (const).

Definition at line 176 of file lattice.h.

176 { return lattice_.begin(); }

◆ end() [1/2]

template<typename T>
iterator smash::RectangularLattice< T >::end ( )
inline
Returns
Last element of lattice.

Definition at line 178 of file lattice.h.

178 { return lattice_.end(); }

◆ end() [2/2]

template<typename T>
const_iterator smash::RectangularLattice< T >::end ( ) const
inline
Returns
Last element of lattice (const).

Definition at line 180 of file lattice.h.

180 { return lattice_.end(); }

◆ operator[]() [1/2]

template<typename T>
T& smash::RectangularLattice< T >::operator[] ( std::size_t  i)
inline
Returns
ith element of lattice.

Definition at line 182 of file lattice.h.

182 { return lattice_[i]; }

◆ operator[]() [2/2]

template<typename T>
const T& smash::RectangularLattice< T >::operator[] ( std::size_t  i) const
inline
Returns
ith element of lattice (const).

Definition at line 184 of file lattice.h.

184 { return lattice_[i]; }

◆ size()

template<typename T>
std::size_t smash::RectangularLattice< T >::size ( ) const
inline
Returns
Size of lattice.

Definition at line 186 of file lattice.h.

186 { return lattice_.size(); }
Here is the caller graph for this function:

◆ node()

template<typename T>
T& smash::RectangularLattice< T >::node ( int  ix,
int  iy,
int  iz 
)
inline

Take the value of a cell given its 3-D indices.

Parameters
[in]ixThe index of the cell in x direction.
[in]iyThe index of the cell in y direction.
[in]izThe index of the cell in z direction.
Returns
Physical quantity evaluated at the cell center.

Definition at line 196 of file lattice.h.

196  {
197  return periodic_
198  ? lattice_[positive_modulo(ix, n_cells_[0]) +
199  n_cells_[0] *
200  (positive_modulo(iy, n_cells_[1]) +
201  n_cells_[1] * positive_modulo(iz, n_cells_[2]))]
202  : lattice_[ix + n_cells_[0] * (iy + n_cells_[1] * iz)];
203  }
Here is the caller graph for this function:

◆ value_at()

template<typename T>
bool smash::RectangularLattice< T >::value_at ( const ThreeVector r,
T &  value 
)
inline

Interpolates lattice quantity to coordinate r.

Result is stored in the value variable. Returns true if coordinate r is on the lattice, false if out of the lattice. In the latter case, the value is set to the default value (usually 0).

Parameters
[in]rPosition where the physical quantity would be evaluated.
[out]valuePhysical quantity evaluated at the nearest cell to the given position.
Returns
Boolean indicates whether the position r is located inside the lattice.
Todo:
(oliiny): maybe 1-order interpolation instead of 0-order?

Definition at line 219 of file lattice.h.

219  {
220  const int ix = std::floor((r.x1() - origin_[0]) / cell_sizes_[0]);
221  const int iy = std::floor((r.x2() - origin_[1]) / cell_sizes_[1]);
222  const int iz = std::floor((r.x3() - origin_[2]) / cell_sizes_[2]);
223  if (out_of_bounds(ix, iy, iz)) {
224  value = T();
225  return false;
226  } else {
227  value = node(ix, iy, iz);
228  return true;
229  }
230  }

◆ iterate_sublattice()

template<typename T>
template<typename F >
void smash::RectangularLattice< T >::iterate_sublattice ( const std::array< int, 3 > &  lower_bounds,
const std::array< int, 3 > &  upper_bounds,
F &&  func 
)
inline

A sub-lattice iterator, which iterates in a 3D-structured manner and calls a function on every cell.

Template Parameters
FType of the function. Arguments are the current node and the 3 integer indices of the cell.
Parameters
[in]lower_boundsStarting numbers for iterating ix, iy, iz.
[in]upper_boundsEnding numbers for iterating ix, iy, iz.
[in]funcFunction acting on the cells (such as taking value).

Definition at line 243 of file lattice.h.

244  {
245  logg[LLattice].debug(
246  "Iterating sublattice with lower bound index (", lower_bounds[0], ",",
247  lower_bounds[1], ",", lower_bounds[2], "), upper bound index (",
248  upper_bounds[0], ",", upper_bounds[1], ",", upper_bounds[2], ")");
249 
250  if (periodic_) {
251  for (int iz = lower_bounds[2]; iz < upper_bounds[2]; iz++) {
252  const int z_offset = positive_modulo(iz, n_cells_[2]) * n_cells_[1];
253  for (int iy = lower_bounds[1]; iy < upper_bounds[1]; iy++) {
254  const int y_offset =
255  n_cells_[0] * (positive_modulo(iy, n_cells_[1]) + z_offset);
256  for (int ix = lower_bounds[0]; ix < upper_bounds[0]; ix++) {
257  const int index = positive_modulo(ix, n_cells_[0]) + y_offset;
258  func(lattice_[index], ix, iy, iz);
259  }
260  }
261  }
262  } else {
263  for (int iz = lower_bounds[2]; iz < upper_bounds[2]; iz++) {
264  const int z_offset = iz * n_cells_[1];
265  for (int iy = lower_bounds[1]; iy < upper_bounds[1]; iy++) {
266  const int y_offset = n_cells_[0] * (iy + z_offset);
267  for (int ix = lower_bounds[0]; ix < upper_bounds[0]; ix++) {
268  func(lattice_[ix + y_offset], ix, iy, iz);
269  }
270  }
271  }
272  }
273  }
Here is the caller graph for this function:

◆ iterate_in_radius()

template<typename T>
template<typename F >
void smash::RectangularLattice< T >::iterate_in_radius ( const ThreeVector point,
const double  r_cut,
F &&  func 
)
inline

Iterates only nodes, whose cell centers lie not further than r_cut in x, y, z directions from the given point and applies a function to each node.

Useful for adding quantities from one particle to the lattice.

Template Parameters
FType of the function. Arguments are the current node and the 3 integer indices of the cell.
Parameters
[in]pointPosition, usually the position of particle [fm].
[in]r_cutMaximum distance from the cell center to the given position. [fm]
[in]funcFunction acting on the cells (such as taking value).

Definition at line 288 of file lattice.h.

289  {
290  std::array<int, 3> l_bounds, u_bounds;
291 
292  /* Array holds value at the cell center: r_center = r_0 + (i+0.5)cell_size,
293  * where i is index in any direction. Therefore we want cells with condition
294  * (r-r_cut)*csize - 0.5 < i < (r+r_cut)*csize - 0.5, r = r_center - r_0 */
295  for (int i = 0; i < 3; i++) {
296  l_bounds[i] =
297  std::ceil((point[i] - origin_[i] - r_cut) / cell_sizes_[i] - 0.5);
298  u_bounds[i] =
299  std::ceil((point[i] - origin_[i] + r_cut) / cell_sizes_[i] - 0.5);
300  }
301 
302  if (!periodic_) {
303  for (int i = 0; i < 3; i++) {
304  if (l_bounds[i] < 0) {
305  l_bounds[i] = 0;
306  }
307  if (u_bounds[i] > n_cells_[i]) {
308  u_bounds[i] = n_cells_[i];
309  }
310  if (l_bounds[i] > n_cells_[i] || u_bounds[i] < 0) {
311  return;
312  }
313  }
314  }
315  iterate_sublattice(l_bounds, u_bounds, std::forward<F>(func));
316  }
Here is the caller graph for this function:

◆ identical_to_lattice()

template<typename T>
template<typename L >
bool smash::RectangularLattice< T >::identical_to_lattice ( const L *  lat) const
inline

Checks if lattices of possibly different types have identical structure.

Template Parameters
LType of the other lattice.
Parameters
[in]latThe other lattice being compared with the current one
Returns
Whether the two lattices have the same sizes, cell numbers, origins, and boundary conditions.

Definition at line 327 of file lattice.h.

327  {
328  return n_cells_[0] == lat->dimensions()[0] &&
329  n_cells_[1] == lat->dimensions()[1] &&
330  n_cells_[2] == lat->dimensions()[2] &&
331  std::abs(lattice_sizes_[0] - lat->lattice_sizes()[0]) <
332  really_small &&
333  std::abs(lattice_sizes_[1] - lat->lattice_sizes()[1]) <
334  really_small &&
335  std::abs(lattice_sizes_[2] - lat->lattice_sizes()[2]) <
336  really_small &&
337  std::abs(origin_[0] - lat->origin()[0]) < really_small &&
338  std::abs(origin_[1] - lat->origin()[1]) < really_small &&
339  std::abs(origin_[2] - lat->origin()[2]) < really_small &&
340  periodic_ == lat->periodic();
341  }

◆ positive_modulo()

template<typename T>
int smash::RectangularLattice< T >::positive_modulo ( int  i,
int  n 
) const
inlineprivate

Returns division modulo, which is always between 0 and n-1 in is not suitable, because it returns results from -(n-1) to n-1.

Parameters
[in]iDividend.
[in]nDivisor.
Returns
Positive remainder.

Definition at line 368 of file lattice.h.

368  {
369  /* (i % n + n) % n would be correct, but slow.
370  * Instead I rely on the fact that i should never go too far
371  * in negative region and replace i%n + n by i + 256 * n = i + (n << 8) */
372  // FIXME: This should use asserts, also checking for under- or overflows.
373  return (i + (n << 8)) % n;
374  }
Here is the caller graph for this function:

Member Data Documentation

◆ lattice_

template<typename T>
std::vector<T> smash::RectangularLattice< T >::lattice_
protected

The lattice itself, array containing physical quantities.

Definition at line 345 of file lattice.h.

◆ lattice_sizes_

template<typename T>
const std::array<double, 3> smash::RectangularLattice< T >::lattice_sizes_
protected

Lattice sizes in x, y, z directions.

Definition at line 347 of file lattice.h.

◆ n_cells_

template<typename T>
const std::array<int, 3> smash::RectangularLattice< T >::n_cells_
protected

Number of cells in x,y,z directions.

Definition at line 349 of file lattice.h.

◆ cell_sizes_

template<typename T>
const std::array<double, 3> smash::RectangularLattice< T >::cell_sizes_
protected

Cell sizes in x, y, z directions.

Definition at line 351 of file lattice.h.

◆ origin_

template<typename T>
const std::array<double, 3> smash::RectangularLattice< T >::origin_
protected

Coordinates of the left down nearer corner.

Definition at line 353 of file lattice.h.

◆ periodic_

template<typename T>
const bool smash::RectangularLattice< T >::periodic_
protected

Whether the lattice is periodic.

Definition at line 355 of file lattice.h.

◆ when_update_

template<typename T>
const LatticeUpdate smash::RectangularLattice< T >::when_update_
protected

When the lattice should be recalculated.

Definition at line 357 of file lattice.h.


The documentation for this class was generated from the following file:
smash::RectangularLattice::out_of_bounds
bool out_of_bounds(int ix, int iy, int iz) const
Checks if 3D index is out of lattice bounds.
Definition: lattice.h:112
smash::RectangularLattice::n_cells_
const std::array< int, 3 > n_cells_
Number of cells in x,y,z directions.
Definition: lattice.h:349
smash::RectangularLattice::positive_modulo
int positive_modulo(int i, int n) const
Returns division modulo, which is always between 0 and n-1 in is not suitable, because it returns res...
Definition: lattice.h:368
smash::RectangularLattice::lattice_sizes_
const std::array< double, 3 > lattice_sizes_
Lattice sizes in x, y, z directions.
Definition: lattice.h:347
smash::logg
std::array< einhard::Logger<>, std::tuple_size< LogArea::AreaTuple >::value > logg
An array that stores all pre-configured Logger objects.
Definition: logging.cc:39
smash::really_small
constexpr double really_small
Numerical error tolerance.
Definition: constants.h:37
smash::RectangularLattice::cell_center
ThreeVector cell_center(int ix, int iy, int iz) const
Find the coordinates of a given cell.
Definition: lattice.h:129
smash::RectangularLattice::cell_sizes_
const std::array< double, 3 > cell_sizes_
Cell sizes in x, y, z directions.
Definition: lattice.h:351
smash::RectangularLattice::when_update_
const LatticeUpdate when_update_
When the lattice should be recalculated.
Definition: lattice.h:357
smash::RectangularLattice::lattice_
std::vector< T > lattice_
The lattice itself, array containing physical quantities.
Definition: lattice.h:345
smash::RectangularLattice::node
T & node(int ix, int iy, int iz)
Take the value of a cell given its 3-D indices.
Definition: lattice.h:196
smash::pdg::n
constexpr int n
Neutron.
Definition: pdgcode_constants.h:30
smash::RectangularLattice::periodic_
const bool periodic_
Whether the lattice is periodic.
Definition: lattice.h:355
smash::LLattice
static constexpr int LLattice
Definition: lattice.h:25
smash::RectangularLattice::origin_
const std::array< double, 3 > origin_
Coordinates of the left down nearer corner.
Definition: lattice.h:353
smash::RectangularLattice::iterate_sublattice
void iterate_sublattice(const std::array< int, 3 > &lower_bounds, const std::array< int, 3 > &upper_bounds, F &&func)
A sub-lattice iterator, which iterates in a 3D-structured manner and calls a function on every cell.
Definition: lattice.h:243