1#ifndef UTOPIA_CORE_GRIDS_SQUARE_HH
2#define UTOPIA_CORE_GRIDS_SQUARE_HH
70 if constexpr (
dim > 1) {
79 throw std::invalid_argument(
"Given the extent of the physical "
80 "space and the specified resolution, a mapping with "
81 "exactly square cells could not be found! Either adjust "
82 "the physical space, the resolution of the grid, or "
83 "choose another grid. Effective resolution was:\n"
84 +
efr_ss.str() +
", but should be the same in all "
97 return std::accumulate(this->_shape.begin(),
this->_shape.end(),
98 1, std::multiplies<IndexType>());
126 static_assert(
dim <= 2,
"MultiIndex only implemented for 1D and 2D!");
128 if constexpr (
dim == 1) {
161 static_assert(
dim == 2,
162 "SquareGrid::vertices_of is only implemented for 2D!");
191 static_assert(
dim == 2,
192 "SquareGrid::cell_at only implemented for 2D!");
195 using midx_et =
typename MultiIndex::elem_type;
217 throw std::invalid_argument(
"The given position is outside "
218 "the non-periodic space associated with this grid!");
259 static_assert(
dim <= 2,
260 "SquareGrid::boundary_cells only implemented for 1D and 2D!");
265 std::set<IndexType>
bc_ids;
268 if constexpr (
dim == 1) {
270 throw std::invalid_argument(
"Invalid value for argument "
271 "`select` in call to method SquareGrid::boundary_cells! "
272 "Available arguments (for currently selected "
273 "dimensionality) are: "
274 "'all', 'left', 'right'. Given value: '" +
select +
"'");
291 else if constexpr (
dim == 2) {
296 throw std::invalid_argument(
"Invalid value for argument "
297 "`select` in call to method SquareGrid::boundary_cells! "
298 "Available arguments (for currently selected "
299 "dimensionality) are: "
300 "'all', 'left', 'right', 'bottom', 'top'. Given value: '"
315 auto hint = bc_ids.begin();
417 +
"' available for rectangular grid discretization!");
469 "VonNeumann neighborhood is implemented only for 1D or 2D "
491 if constexpr (
dim >= 2) {
504 dist <= distance - 1 -
i/2 + (
i%2)/2;
537 static_assert(((
dim == 1)
or (
dim == 2)),
538 "VonNeumann neighborhood is implemented only for 1D or 2D "
566 if constexpr (
dim >= 2) {
579 dist <= distance - (
i + 1);
606 dist <= distance - (
i + 1);
647 "VonNeumann neighborhood is implemented only for 1D or 2D space!");
660 if constexpr (
dim >= 2) {
673 static_assert(((
dim == 1)
or (
dim == 2)),
674 "VonNeumann neighborhood is only implemented in 1 or 2 dimensions "
688 if constexpr (
dim >= 2) {
740 static_assert(
dim == 2,
741 "Moore neighborhood is only available in 2D!");
780 static_assert(
dim == 2,
781 "Moore neighborhood is only available in 2D!");
823 static_assert(
dim == 2,
"Moore neighborhood is only available in 2D!");
847 static_assert(
dim == 2,
"Moore neighborhood is only available in 2D!");
890 template<DimType axis>
892 if constexpr (
axis == 0) {
919 template<DimType axis,
bool periodic>
924 static_assert(
dim <= 2,
925 "Unsupported dimensionality of underlying space! Need be 1 or 2.");
936 if constexpr (periodic) {
950 if constexpr (periodic) {
979 template<DimType axis,
bool periodic>
985 static_assert(
dim <= 2,
986 "Unsupported dimensionality of underlying space! Need be 1 or 2.");
1001 if constexpr (periodic) {
1031 template<DimType axis,
bool periodic>
1037 static_assert(
dim <= 2,
1038 "Unsupported dimensionality of underlying space! Need be 1 or 2.");
1042 if (distance == 0) {
1053 if constexpr (periodic) {
1101 if (distance <= 1) {
1102 return std::pow(2 + 1,
dim) - 1;
1105 return std::pow(2 * distance + 1,
dim) - 1;
1120 return 2 * distance;
1125 while (distance > 0) {
1138 +
"' available for rectangular grid discretization!");
1158 template<
bool check_shape=false>
1173 this->
shape().print(shape_ss,
"Grid Shape:");
1175 throw std::invalid_argument(
"The grid shape is too small to "
1176 "accomodate a neighborhood with 'distance' parameter set "
1178 +
" in a periodic space!\n" +
shape_ss.str());
The base class for all grid discretizations used by the CellManager.
Definition base.hh:99
const DistType _resolution
How many cells to place per length unit of space.
Definition base.hh:127
bool is_periodic() const
Whether the space this grid maps to is periodic.
Definition base.hh:304
const std::shared_ptr< Space > _space
The space that is to be discretized.
Definition base.hh:120
NBFuncID< Self > _nb_empty
A neighborhood function for empty neighborhood.
Definition base.hh:323
const std::shared_ptr< Space > & space() const
Const reference to the space this grid maps to.
Definition base.hh:299
const NBMode & nb_mode() const
Const reference to the currently selected neighborhood mode.
Definition base.hh:198
auto nb_size() const
Maximum size of the currently selected neighborhood.
Definition base.hh:208
const Config & nb_params() const
The neighborhood parameters of the currently selected neighborhood.
Definition base.hh:203
A grid discretization using square cells.
Definition square.hh:31
GridStructure structure() const override
Structure of the grid.
Definition square.hh:116
std::vector< SpaceVec > vertices_of(const IndexType id) const override
Returns the vertices of the cell with the given ID.
Definition square.hh:160
IndexType num_cells() const override
Number of square cells required to fill the physical space.
Definition square.hh:96
DataIO::Config Config
The configuration type.
Definition square.hh:46
NBFuncID< Base > get_nb_func_vonNeumann(const Config &nb_params)
Returns a standalone von-Neumann neighborhood function.
Definition square.hh:441
const SpaceVec _cell_extent
The extent of each cell of this square discretization (same for all)
Definition square.hh:55
IndexType cell_at(const SpaceVec &pos) const override
Return the ID of the cell covering the given point in physical space.
Definition square.hh:190
SpaceVec barycenter_of(const IndexType id) const override
Returns the barycenter of the cell with the given ID.
Definition square.hh:140
MultiIndex shape() const override
Get shape of the square grid.
Definition square.hh:111
SquareGrid(std::shared_ptr< Space > space, const Config &cfg)
Construct a rectangular grid discretization.
Definition square.hh:64
std::set< IndexType > boundary_cells(std::string select="all") const override
Retrieve a set of cell indices that are at a specified boundary.
Definition square.hh:257
void add_neighbors_in_(const IndexType root_id, IndexContainer &neighbor_ids) const
Add both direct neighbors to a container of indices.
Definition square.hh:920
DistType get_nb_param_distance(const Config ¶ms) const
Extract the distance neighborhood parameter from the given config.
Definition square.hh:1159
NBFuncID< Base > get_nb_func(NBMode nb_mode, const Config &nb_params) override
Retrieve the neighborhood function depending on the mode and parameters.
Definition square.hh:403
NBFuncID< Base > get_nb_func_Moore(const Config &nb_params)
Returns a standalone Moore neighborhood function.
Definition square.hh:713
NBFuncID< Base > _nb_Moore_nonperiodic
Moore neighbors for non-periodic 2D grid.
Definition square.hh:844
NBFuncID< Base > _nb_Moore_periodic
Moore neighbors for periodic 2D grid.
Definition square.hh:820
MultiIndex midx_of(const IndexType id) const override
Returns the multi-index of the cell with the given ID.
Definition square.hh:125
SpaceVec extent_of(const IndexType) const override
Returns the extent of the cell with the given ID.
Definition square.hh:149
SpaceVec effective_resolution() const override
The effective cell resolution into each physical space dimension.
Definition square.hh:105
MultiIndexType< dim > MultiIndex
The type of multi-index like arrays, e.g. the grid shape.
Definition square.hh:43
void add_high_val_neighbor_in_(const IndexType root_id, const DistType distance, IndexContainer &neighbor_ids) const
Add a neighbor on the high (ID) value side to an index container.
Definition square.hh:1032
static constexpr DimType dim
The dimensionality of the space to be discretized (for easier access)
Definition square.hh:37
typename Space::SpaceVec SpaceVec
The type of vectors that have a relation to physical space.
Definition square.hh:40
MultiIndex determine_shape() const
Given the resolution, return the grid shape required to fill the space.
Definition square.hh:388
NBFuncID< Base > _nb_vonNeumann_periodic
The Von-Neumann neighborhood for periodic grids.
Definition square.hh:643
DistType expected_num_neighbors(const NBMode &nb_mode, const Config &nb_params) const override
Computes the expected number of neighbors for a neighborhood mode.
Definition square.hh:1090
constexpr IndexType id_shift() const
Return the shift in cell indices necessary if moving along an axis.
Definition square.hh:891
const MultiIndex _shape
The (multi-index) shape of the grid, resulting from resolution.
Definition square.hh:52
NBFuncID< Base > _nb_vonNeumann_nonperiodic
The Von-Neumann neighborhood for non-periodic grids.
Definition square.hh:670
void add_low_val_neighbor_in_(const IndexType root_id, const DistType distance, IndexContainer &neighbor_ids) const
Add a neighbor on the low (ID) value side to an index container.
Definition square.hh:980
NBMode
Possible neighborhood types; availability depends on choice of grid.
Definition base.hh:52
std::string nb_mode_to_string(const NBMode &nb_mode)
Given an NBMode enum value, return the corresponding string key.
Definition base.hh:78
GridStructure
Available grid implementations.
Definition base.hh:13
std::function< IndexContainer(const IndexType)> NBFuncID
Type of the neighborhood calculating function.
Definition base.hh:92
@ vonNeumann
The vonNeumann neighborhood, i.e. only nearest neighbors.
@ Moore
The Moore neighborhood, i.e. nearest and next nearest neighbors.
@ empty
Every entity is utterly alone in the world.
@ square
A square lattice grid.
YAML::Node Config
Type of a variadic dictionary-like data structure used throughout Utopia.
Definition types.hh:71
Container select_entities(const Manager &mngr, const DataIO::Config &sel_cfg)
Select entities according to parameters specified in a configuration.
Definition select.hh:213
@ vertices
Iterate over vertices.
arma::Col< IndexType >::fixed< dim > MultiIndexType
Type for index type vectors that are associated with a physical space.
Definition types.hh:53
std::vector< IndexType > IndexContainer
Type for container of indices.
Definition types.hh:43
unsigned short DimType
Type for dimensions, i.e. very small unsigned integers.
Definition types.hh:34
unsigned int DistType
Type for distancens, i.e. intermediately long unsigned integers.
Definition types.hh:37
std::size_t IndexType
Type for indices, i.e. values used for container indexing, agent IDs, ...
Definition types.hh:40
SpaceVecType< dim > SpaceVec
The type for vectors relating to physical space.
Definition space.hh:33
static constexpr std::size_t dim
The dimensionality of the space.
Definition space.hh:30