Populations< FP, Categories > Class Template Reference

CPP API: mio::Populations< FP, Categories > Class Template Reference
mio::Populations< FP, Categories > Class Template Reference

A class template for compartment populations. More...

#include <populations.h>

Inheritance diagram for mio::Populations< FP, Categories >:
Collaboration diagram for mio::Populations< FP, Categories >:

Public Types

using Base = CustomIndexArray< UncertainValue< FP >, Categories... >
 
using Index = typename Base::Index
 
- Public Types inherited from mio::CustomIndexArray< UncertainValue< FP >, Categories... >
using const_iterator = typename InternalArrayType::const_iterator
 
using Index = ::mio::Index< Tags... >
 
using InternalArrayType = Eigen::Array< Type, Eigen::Dynamic, 1 >
 
using iterator = typename InternalArrayType::iterator
 
using reference = Type &
 
using Type = UncertainValue< FP >
 
using value_type = Type
 

Public Member Functions

bool apply_constraints ()
 Checks whether all compartments have non-negative values. More...
 
bool check_constraints () const
 Checks whether all compartments have non-negative values and logs an error if constraint is not satisfied. More...
 
template<class OtherType >
requires std::convertible_to< typename Base::Type::Type, OtherType > Populations< OtherType, Categories... > convert () const
 Convert internally stored data to OtherType and save into new Populations. More...
 
Eigen::VectorX< FP > get_compartments () const
 get_compartments returns an Eigen copy of the vector of populations. More...
 
template<class Arr >
decltype(auto) get_from (Arr &&y, Index const &cats) const
 get_from returns the value of a flat container at the flat index corresponding to set of enum values. More...
 
template<class T >
FP get_group_total (mio::Index< T > group_idx) const
 get_group_total returns the total population of a group in one category More...
 
size_t get_num_compartments () const
 get_num_compartments returns the number of compartments More...
 
FP get_total () const
 get_total returns the total population of all compartments More...
 
 Populations (Base &&array)
 Create populations by taking ownership of a CustomIndexArray. More...
 
template<class... Ts>
requires std::is_constructible_v< UncertainValue< FP >, Ts... > Populations (Index const &sizes, Ts... args)
 
template<class T >
void set_difference_from_group_total (Index const &midx, FP total_group_population)
 set_difference_from_group_total sets the total population for a given group from a difference More...
 
void set_difference_from_total (Index midx, FP total_population)
 set_difference_from_total takes a total population as input and sets the compartment of a given index to have the difference between the input total population and the current population in all other compartments More...
 
template<class T >
void set_group_total (mio::Index< T > group_idx, FP value)
 set_group_total sets the total population for a given group More...
 
void set_total (FP value)
 set_total sets the total population More...
 
- Public Member Functions inherited from mio::CustomIndexArray< UncertainValue< FP >, Categories... >
auto & array ()
 
auto const & array () const
 array returns a reference to the internally stored flat array. More...
 
iterator begin ()
 Get a start iterator for the elements. More...
 
const_iterator begin () const
 Get a start iterator for the elements. More...
 
requires std::convertible_to< Type, OtherType > CustomIndexArray< OtherType, Tags... > convert () const
 Convert internally stored data to OtherType and save into new CustomIndexArray. More...
 
 CustomIndexArray ()
 Create an empty CustomIndexArray with size 0. Use the resize member function to add entries. More...
 
 CustomIndexArray (Index const &dims, Iter b, Iter e)
 Initializes array with values from a range. More...
 
iterator end ()
 Get an end iterator for the elements. More...
 
const_iterator end () const
 Get an end iterator for the elements. More...
 
size_t get_flat_index (Index const &index) const
 get_flat_index returns the flat index into the stored array, given the indices of each category More...
 
Ts m_numel (product(dims))
 
Ts m_y (InternalArrayType::Constant(m_numel, 1, Type{std::forward< Ts >(args)...}))
 
constexpr size_t numel () const
 numel returns the number of elements More...
 
CustomIndexArrayoperator= (const Type &scalar)
 Assign the same value to each element of the array. More...
 
Typeoperator[] (Index const &index)
 returns the entry of the array given a MultiIndex More...
 
Type const & operator[] (Index const &index) const
 returns the entry of the array given a flat index index More...
 
 requires (std::is_constructible_v< Type, Ts... >) CustomIndexArray(Index const &dims
 CustomIndexArray constructor, that initializes the array to constant instances of CustsomIndexArray::Type. More...
 
void resize (Index new_dims)
 Resize all dimensions, invalidating entries. More...
 
void resize (mio::Index< Tag > new_dim)
 Resize a single dimension, invalidating entries. More...
 
void serialize (IOContext &io) const
 serialize this. More...
 
void set_multiple (const std::vector< typename CustomIndexArray< UncertainValue< FP >, Tags... >::Index > &indices, const UncertainValue< FP > &value)
 Set multiple entries to the same value. More...
 
mio::Index< Tagsize () const
 returns the size along the dimension provided as template parameter More...
 
Index size () const
 returns the size of the array along all dimensions. More...
 
bool operator== (const CustomIndexArray &other) const
 Equality comparison. More...
 
bool operator!= (const CustomIndexArray &other) const
 Equality comparison. More...
 
Slice< Tag, iteratorslice (Seq< size_t > idx_seq)
 Creates a slice into the multidimensional array. More...
 
Slice< Tag, const_iteratorslice (Seq< size_t > idx_seq) const
 Creates a slice into the multidimensional array. More...
 
Slice< Tag, iteratorslice (mio::Index< Tag > idx)
 Creates a slice into the multidimensional array. More...
 
Slice< Tag, const_iteratorslice (mio::Index< Tag > idx) const
 Creates a slice into the multidimensional array. More...
 
requires std::is_enum_v< Tag > Slice< Tag, iteratorslice (Tag idx)
 Creates a slice into the multidimensional array. More...
 
requires std::is_enum_v< Tag > Slice< Tag, const_iteratorslice (Tag idx) const
 Creates a slice into the multidimensional array. More...
 

Static Public Member Functions

template<class IOContext >
static IOResult< Populationsdeserialize (IOContext &io)
 deserialize an object of this class. More...
 
- Static Public Member Functions inherited from mio::CustomIndexArray< UncertainValue< FP >, Categories... >
static IOResult< CustomIndexArraydeserialize (IOContext &io)
 deserialize an object of this class. More...
 

Additional Inherited Members

- Public Attributes inherited from mio::CustomIndexArray< UncertainValue< FP >, Categories... >
Ts && args
 
- Static Protected Member Functions inherited from mio::CustomIndexArray< UncertainValue< FP >, Categories... >
static IOResult< Derived > deserialize (IOContext &io, Tag< Derived >)
 deserialize an object of a class derived from this class. More...
 

Detailed Description

template<typename FP, class... Categories>
class mio::Populations< FP, Categories >

A class template for compartment populations.

Populations can be split up into different Categories, e.g. by age group, yearly income group, gender etc. Compartmental models introduce the additional category of infection type. For the SEIR model these are Susceptible, Exposed, Infected and Removed. Every category is assumed to contain a finite number of groups.

This template can be instantiated given an arbitrary set of Categories. The Categories are assumed to be an enum class with a member Count refering to the number of elements in the enum.

The class created from this template contains a "flat array" of compartment populations and some functions for retrieving or setting the populations.

Member Typedef Documentation

◆ Base

template<typename FP , class... Categories>
using mio::Populations< FP, Categories >::Base = CustomIndexArray<UncertainValue<FP>, Categories...>

◆ Index

template<typename FP , class... Categories>
using mio::Populations< FP, Categories >::Index = typename Base::Index

Constructor & Destructor Documentation

◆ Populations() [1/2]

template<typename FP , class... Categories>
template<class... Ts>
requires std::is_constructible_v<UncertainValue<FP>, Ts...> mio::Populations< FP, Categories >::Populations ( Index const &  sizes,
Ts...  args 
)
inlineexplicit

◆ Populations() [2/2]

template<typename FP , class... Categories>
mio::Populations< FP, Categories >::Populations ( Base &&  array)
inlineexplicit

Create populations by taking ownership of a CustomIndexArray.

Member Function Documentation

◆ apply_constraints()

template<typename FP , class... Categories>
bool mio::Populations< FP, Categories >::apply_constraints ( )
inline

Checks whether all compartments have non-negative values.

This function can be used to prevent slighly negative function values in compartment sizes that came out due to roundoff errors if, e.g., population sizes were computed in a complex way. If negative values which are smaller than -1e-10 are found, an error is logged, otherwise, only a warning is logged.

Attention: This function should be used with care. It can not and will not set model parameters and compartments to meaningful values. In most cases it is preferable to use check_constraints, and correct values manually before proceeding with the simulation. The main usage for apply_constraints is in automated tests using random values for initialization.

Returns
Returns true if one (or more) constraint(s) were corrected, otherwise false.

◆ check_constraints()

template<typename FP , class... Categories>
bool mio::Populations< FP, Categories >::check_constraints ( ) const
inline

Checks whether all compartments have non-negative values and logs an error if constraint is not satisfied.

Returns
Returns true if one or more constraints are not satisfied, false otherwise.

◆ convert()

template<typename FP , class... Categories>
template<class OtherType >
requires std::convertible_to<typename Base::Type::Type, OtherType> Populations<OtherType, Categories...> mio::Populations< FP, Categories >::convert ( ) const
inline

Convert internally stored data to OtherType and save into new Populations.

Template Parameters
OtherTypeThe type to convert into.
Returns
New Populations of OtherType with copy of internal data.

◆ deserialize()

template<typename FP , class... Categories>
template<class IOContext >
static IOResult<Populations> mio::Populations< FP, Categories >::deserialize ( IOContext &  io)
inlinestatic

deserialize an object of this class.

See also
mio::deserialize

◆ get_compartments()

template<typename FP , class... Categories>
Eigen::VectorX<FP> mio::Populations< FP, Categories >::get_compartments ( ) const
inline

get_compartments returns an Eigen copy of the vector of populations.

This can be used as initial conditions for the ODE solver

Returns
Eigen::VectorX<FP> of populations

◆ get_from()

template<typename FP , class... Categories>
template<class Arr >
decltype(auto) mio::Populations< FP, Categories >::get_from ( Arr &&  y,
Index const &  cats 
) const
inline

get_from returns the value of a flat container at the flat index corresponding to set of enum values.

It is the same as get, except that it takes the values from an outside reference flat container, rather than the initial values stored within this class

TODO: It would be better, to have CustomIndexArray be able to operate on shared data. Maybe using an Eigen::Map

Parameters
ya reference to a flat container
catsenum values for each category
Returns
the population of compartment

◆ get_group_total()

template<typename FP , class... Categories>
template<class T >
FP mio::Populations< FP, Categories >::get_group_total ( mio::Index< T >  group_idx) const
inline

get_group_total returns the total population of a group in one category

Parameters
Tenum class for the category
group_idxenum value of the group
Returns
total population of the group

◆ get_num_compartments()

template<typename FP , class... Categories>
size_t mio::Populations< FP, Categories >::get_num_compartments ( ) const
inline

get_num_compartments returns the number of compartments

This corresponds to the product of the category sizes

Returns
number of compartments

◆ get_total()

template<typename FP , class... Categories>
FP mio::Populations< FP, Categories >::get_total ( ) const
inline

get_total returns the total population of all compartments

Returns
total population

◆ set_difference_from_group_total()

template<typename FP , class... Categories>
template<class T >
void mio::Populations< FP, Categories >::set_difference_from_group_total ( Index const &  midx,
FP  total_group_population 
)
inline

set_difference_from_group_total sets the total population for a given group from a difference

This function sets the population size

Parameters
total_populationthe new value for the total population
indicesThe indices of the compartment
TThe enum class of the category of the group
group_idxThe enum of the group within the category

◆ set_difference_from_total()

template<typename FP , class... Categories>
void mio::Populations< FP, Categories >::set_difference_from_total ( Index  midx,
FP  total_population 
)
inline

set_difference_from_total takes a total population as input and sets the compartment of a given index to have the difference between the input total population and the current population in all other compartments

Parameters
indicesthe index of the compartment
total_populationthe new value for the total population

◆ set_group_total()

template<typename FP , class... Categories>
template<class T >
void mio::Populations< FP, Categories >::set_group_total ( mio::Index< T >  group_idx,
FP  value 
)
inline

set_group_total sets the total population for a given group

This function rescales all the compartments populations proportionally. If all compartments have zero population, the total population gets distributed equally over all compartments

Parameters
TThe enum class of the category of the group
group_idxThe enum value of the group within the category
valuethe new value for the total population

◆ set_total()

template<typename FP , class... Categories>
void mio::Populations< FP, Categories >::set_total ( FP  value)
inline

set_total sets the total population

This function rescales all the compartments populations proportionally. If all compartments have zero population, the total population gets distributed equally over all compartments.

Parameters
valuethe new value for the total population