Utopia 2
Framework for studying models of complex & adaptive systems.
|
A grid discretization using square cells. More...
#include <square.hh>
Public Types | |
using | Base = Grid< Space > |
Base class type. | |
using | SpaceVec = typename Space::SpaceVec |
The type of vectors that have a relation to physical space. | |
using | MultiIndex = MultiIndexType< dim > |
The type of multi-index like arrays, e.g. the grid shape. | |
using | Config = DataIO::Config |
The configuration type. | |
Public Types inherited from Utopia::Grid< Space > | |
using | Self = Grid< Space > |
Type of this class, i.e. the base grid class. | |
using | SpaceVec = typename Space::SpaceVec |
The type of vectors that have a relation to physical space. | |
using | MultiIndex = MultiIndexType< dim > |
The type of multi-index like arrays, e.g. the grid shape. | |
using | Config = DataIO::Config |
The configuration type. | |
Public Member Functions | |
SquareGrid (std::shared_ptr< Space > space, const Config &cfg) | |
Construct a rectangular grid discretization. | |
IndexType | num_cells () const override |
Number of square cells required to fill the physical space. | |
SpaceVec | effective_resolution () const override |
The effective cell resolution into each physical space dimension. | |
MultiIndex | shape () const override |
Get shape of the square grid. | |
GridStructure | structure () const override |
Structure of the grid. | |
MultiIndex | midx_of (const IndexType id) const override |
Returns the multi-index of the cell with the given ID. | |
SpaceVec | barycenter_of (const IndexType id) const override |
Returns the barycenter of the cell with the given ID. | |
SpaceVec | extent_of (const IndexType) const override |
Returns the extent of the cell with the given ID. | |
std::vector< SpaceVec > | vertices_of (const IndexType id) const override |
Returns the vertices of the cell with the given ID. | |
IndexType | cell_at (const SpaceVec &pos) const override |
Return the ID of the cell covering the given point in physical space. | |
std::set< IndexType > | boundary_cells (std::string select="all") const override |
Retrieve a set of cell indices that are at a specified boundary. | |
Public Member Functions inherited from Utopia::Grid< Space > | |
Grid (std::shared_ptr< Space > space, const Config &cfg) | |
Construct a grid discretization. | |
virtual | ~Grid ()=default |
Virtual destructor to allow polymorphic destruction. | |
IndexContainer | neighbors_of (const IndexType id) const |
Returns the indices of the neighbors of the cell with the given ID. | |
void | select_neighborhood (NBMode nb_mode, const Config &nb_params={}) |
const NBMode & | nb_mode () const |
Const reference to the currently selected neighborhood mode. | |
const Config & | nb_params () const |
The neighborhood parameters of the currently selected neighborhood. | |
auto | nb_size () const |
Maximum size of the currently selected neighborhood. | |
auto | resolution () const |
Get scalar resolution value of this grid. | |
std::string | structure_name () const |
Structure of the grid as std::string. | |
const std::shared_ptr< Space > & | space () const |
Const reference to the space this grid maps to. | |
bool | is_periodic () const |
Whether the space this grid maps to is periodic. | |
Static Public Attributes | |
static constexpr DimType | dim = Space::dim |
The dimensionality of the space to be discretized (for easier access) | |
Static Public Attributes inherited from Utopia::Grid< Space > | |
static constexpr DimType | dim = Space::dim |
The dimensionality of the space to be discretized (for easier access) | |
Protected Member Functions | |
NBFuncID< Base > | get_nb_func (NBMode nb_mode, const Config &nb_params) override |
Retrieve the neighborhood function depending on the mode and parameters. | |
NBFuncID< Base > | get_nb_func_vonNeumann (const Config &nb_params) |
Returns a standalone von-Neumann neighborhood function. | |
NBFuncID< Base > | get_nb_func_Moore (const Config &nb_params) |
Returns a standalone Moore neighborhood function. | |
template<DimType axis> | |
constexpr IndexType | id_shift () const |
Return the shift in cell indices necessary if moving along an axis. | |
template<DimType axis, bool periodic> | |
void | add_neighbors_in_ (const IndexType root_id, IndexContainer &neighbor_ids) const |
Add both direct neighbors to a container of indices. | |
template<DimType axis, bool periodic> | |
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. | |
template<DimType axis, bool periodic> | |
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. | |
DistType | expected_num_neighbors (const NBMode &nb_mode, const Config &nb_params) const override |
Computes the expected number of neighbors for a neighborhood mode. | |
template<bool check_shape = false> | |
DistType | get_nb_param_distance (const Config ¶ms) const |
Extract the distance neighborhood parameter from the given config. | |
Protected Attributes | |
NBFuncID< Base > | _nb_vonNeumann_periodic |
The Von-Neumann neighborhood for periodic grids. | |
NBFuncID< Base > | _nb_vonNeumann_nonperiodic |
The Von-Neumann neighborhood for non-periodic grids. | |
NBFuncID< Base > | _nb_Moore_periodic |
Moore neighbors for periodic 2D grid. | |
NBFuncID< Base > | _nb_Moore_nonperiodic |
Moore neighbors for non-periodic 2D grid. | |
Protected Attributes inherited from Utopia::Grid< Space > | |
const std::shared_ptr< Space > | _space |
The space that is to be discretized. | |
const DistType | _resolution |
How many cells to place per length unit of space. | |
NBMode | _nb_mode |
Neighborhood mode. | |
Config | _nb_params |
Neighborhood parameters. | |
NBFuncID< Self > | _nb_func |
Neighborhood function (working on cell IDs) | |
NBFuncID< Self > | _nb_empty |
A neighborhood function for empty neighborhood. | |
Private Member Functions | |
MultiIndex | determine_shape () const |
Given the resolution, return the grid shape required to fill the space. | |
Private Attributes | |
const MultiIndex | _shape |
The (multi-index) shape of the grid, resulting from resolution. | |
const SpaceVec | _cell_extent |
The extent of each cell of this square discretization (same for all) | |
A grid discretization using square cells.
This is a grid discretization where the cells are vector spaces that are spanned by orthogonal basis vectors and each cell has the same physical extent in each dimension. In the 2D case, this refers to perfectly square cells; in 3D these would be perfect cubes, etc.
0, 1, …, N_x, N_x + 1, …, 2*N_x, 2*N_x + 1, …, N_x * N_y - 1
using Utopia::SquareGrid< Space >::Config = DataIO::Config |
The configuration type.
using Utopia::SquareGrid< Space >::MultiIndex = MultiIndexType<dim> |
The type of multi-index like arrays, e.g. the grid shape.
using Utopia::SquareGrid< Space >::SpaceVec = typename Space::SpaceVec |
The type of vectors that have a relation to physical space.
|
inline |
Construct a rectangular grid discretization.
space | The space to construct the discretization for |
cfg | Further configuration parameters |
|
inlineprotected |
Add a neighbor on the high (ID) value side to an index container.
This function takes an index container and populates it with the index of a neighboring cell in different dimensions, specified by template parameter 0 < axis
< number of dimensions - 1.
The algorithm first calculates whether the given root cell index has a back boundary in the chosen dimension. If so, the neighboring cell is only added if the grid is periodic.
root_id | Which cell to find the agents of |
distance | Which distance the neighbor has to the root cell |
neighbor_ids | The container to populate with the indices |
axis | The axis along which to add the neighbor (0-based!) |
periodic | Whether the grid is periodic |
|
inlineprotected |
Add a neighbor on the low (ID) value side to an index container.
This function takes an index container and populates it with the indices of neighboring cells in different dimensions, specified by template parameter 0 < axis
< number of dimensions - 1.
The algorithm first calculates whether the given root cell index has a front boundary in the chosen dimension. If so, the neighboring cell is only added if the grid is periodic.
root_id | Which cell to find the agents of |
distance | Which distance the neighbor has to the root cell |
neighbor_ids | The container to populate with the indices |
axis | The axis along which to add the neighbor (0-based!) |
periodic | Whether the grid is periodic |
|
inlineprotected |
Add both direct neighbors to a container of indices.
This function takes an index container and populates it with the indices of neighboring cells in different dimensions, specified by template parameter 0 < axis
< number of dimensions - 1.
The algorithm first calculates whether the given root cell index has a front or back boundary in the chosen dimension. If so, the neighboring cell is only added if the grid is periodic.
root_id | Which cell to find the agents of |
neighbor_ids | The container to populate with the indices |
axis | The axis along which to add the neighbors (0-based!) |
periodic | Whether the grid is periodic |
|
inlineoverridevirtual |
Returns the barycenter of the cell with the given ID.
Implements Utopia::Grid< Space >.
|
inlineoverridevirtual |
Retrieve a set of cell indices that are at a specified boundary.
select | Which boundary to return the cell IDs of. If 'all', all boundary cells are returned. Other available values depend on the dimensionality of the grid: 1D: left, right 2D: bottom, top 3D: back, front |
Implements Utopia::Grid< Space >.
|
inlineoverridevirtual |
Return the ID of the cell covering the given point in physical space.
Cells are interpreted as covering half-open intervals in space, i.e., including their low-value edges and excluding their high-value edges. The special case of points on high-value edges for non-periodic space behaves such that these points are associated with the cells at the boundary.
Implements Utopia::Grid< Space >.
|
inlineprivate |
Given the resolution, return the grid shape required to fill the space.
Integer rounding takes place here. A physical space of extents of 2.1 length units in each dimension and a resolution of two cells per unit length will result in 4 cells in each dimension, each cell's size scaled up slightly and the effective resolution thus slightly smaller than the specified resolution.
|
inlineoverridevirtual |
The effective cell resolution into each physical space dimension.
For a square lattice, this is just the quotient of grid shape and extent of physical space, separately in each dimension
Implements Utopia::Grid< Space >.
|
inlineoverrideprotectedvirtual |
Computes the expected number of neighbors for a neighborhood mode.
This function is used to calculate the amount of memory that should be reserved for the neighbor_ids vector. For the calculation it uses the member variables: dim
and the distance
parameter from the given configuration.
For a von Neumann neighborhood, the number of neighbors is: { 2 * distance for distance = 1 N(dim, distance) = { 2 * sum_{distances} N(dim-1, distance) { for distance > 1)
For a Moore neighborhood, the number of neighbors is:
N(dim, distance) = (2 * distance + 1)^2 - 1
Implements Utopia::Grid< Space >.
|
inlineoverridevirtual |
Returns the extent of the cell with the given ID.
Implements Utopia::Grid< Space >.
|
inlineoverrideprotectedvirtual |
Retrieve the neighborhood function depending on the mode and parameters.
Implements Utopia::Grid< Space >.
|
inlineprotected |
Returns a standalone Moore neighborhood function.
It extracts the distance parameter from the configuration and depending on the distance parameter and the periodicity of the space decides between four different neighborhood calculation functions. The returned callable does rely on the SquareGrid object, but it includes all parameters from the configuration that can be computed once and then captured; this avoids recomputation.
nb_params | The configuration for the Moore neighborhood method. Expected keys: distance (optional, defaults to 1), which refers to the Chebychev distance of the included neighbors. |
|
inlineprotected |
Returns a standalone von-Neumann neighborhood function.
It extracts the distance parameter from the configuration and depending on the distance parameter and the periodicity of the space decides between four different neighborhood calculation functions. The returned callable does rely on the SquareGrid object, but it includes all parameters from the configuration that can be computed once and then captured; this avoids recomputation.
nb_params | The configuration for the von-Neumann neighborhood method. Expected keys: distance (optional, defaults to 1), which refers to the Manhattan distance of included neighbors. |
|
inlineprotected |
Extract the distance
neighborhood parameter from the given config.
1
.check_shape | Whether to check the shape of the grid is large enough for this distance. For all SquareGrid neighborhoods in periodic space, the grid needs to be at least 2 * distance + 1 cells wide in each dimension. |
params | The neighborhood parameters to extract the distance parameter from. |
|
inlineconstexprprotected |
Return the shift in cell indices necessary if moving along an axis.
It returns in the different cases:
axis | In which dimension the shift is desired |
|
inlineoverridevirtual |
Returns the multi-index of the cell with the given ID.
Implements Utopia::Grid< Space >.
|
inlineoverridevirtual |
Number of square cells required to fill the physical space.
This is calculated simply from the _shape member.
Implements Utopia::Grid< Space >.
|
inlineoverridevirtual |
|
inlineoverridevirtual |
|
inlineoverridevirtual |
Returns the vertices of the cell with the given ID.
Only available for 2D currently; the vertices are given in counter-clockwise order, starting with the position of the bottom left-hand vertex of the cell.
Implements Utopia::Grid< Space >.
The extent of each cell of this square discretization (same for all)
Moore neighbors for non-periodic 2D grid.
Moore neighbors for periodic 2D grid.
|
protected |
The Von-Neumann neighborhood for non-periodic grids.
|
protected |
The Von-Neumann neighborhood for periodic grids.
|
private |
The (multi-index) shape of the grid, resulting from resolution.
|
staticconstexpr |
The dimensionality of the space to be discretized (for easier access)