FlowModel< FP, Comp, Pop, Params, Flows > Class Template Reference

CPP API: mio::FlowModel< FP, Comp, Pop, Params, Flows > Class Template Reference
mio::FlowModel< FP, Comp, Pop, Params, Flows > Class Template Referenceabstract

A FlowModel is a CompartmentalModel defined by the flows between compartments. More...

#include <flow_model.h>

Inheritance diagram for mio::FlowModel< FP, Comp, Pop, Params, Flows >:
Collaboration diagram for mio::FlowModel< FP, Comp, Pop, Params, Flows >:

Public Types

using Base = CompartmentalModel< FP, Comp, Pop, Params >
 
- Public Types inherited from mio::CompartmentalModel< FP, Comp, Pop, Params >
using Compartments = Comp
 
using ParameterSet = Params
 
using Populations = Pop
 

Public Member Functions

template<class... Args>
 FlowModel (Args... args)
 Default constructor, forwarding args to Base constructor. More...
 
void get_derivatives (Eigen::Ref< const Eigen::VectorX< FP >> flows, Eigen::Ref< Eigen::VectorX< FP >> dydt) const
 Compute the right-hand-side of the ODE dydt = f(y, t) from flow values. More...
 
void get_derivatives (Eigen::Ref< const Eigen::VectorX< FP >> pop, Eigen::Ref< const Eigen::VectorX< FP >> y, FP t, Eigen::Ref< Eigen::VectorX< FP >> dydt) const final
 Compute the right-hand-side f(y, t) of the ODE and store it in dydt. More...
 
template<Comp Source, Comp Target>
constexpr size_t get_flat_flow_index () const
 A flat index into an array of flows (as computed by get_flows), if the only used category in Pop is Comp. More...
 
template<Comp Source, Comp Target>
size_t get_flat_flow_index (const FlowIndex &indices) const
 A flat index into an array of flows (as computed by get_flows), given the indices of each category. More...
 
virtual void get_flows (Eigen::Ref< const Eigen::VectorX< FP >>, Eigen::Ref< const Eigen::VectorX< FP >>, FP, Eigen::Ref< Eigen::VectorX< FP >>) const =0
 
Eigen::VectorX< FP > get_initial_flows () const
 Initial values for flows. More...
 
- Public Member Functions inherited from mio::CompartmentalModel< FP, Comp, Pop, Params >
bool apply_constraints ()
 Checks whether the model satisfies all constraints. More...
 
bool check_constraints () const
 Checks that the model satisfies all constraints (e.g. More...
 
 CompartmentalModel (CompartmentalModel &&)=default
 
 CompartmentalModel (const CompartmentalModel &)=default
 
 CompartmentalModel (Populations const &po, ParameterSet const &pa)
 CompartmentalModel default constructor. More...
 
void eval_right_hand_side (Eigen::Ref< const Eigen::VectorX< FP >> pop, Eigen::Ref< const Eigen::VectorX< FP >> y, FP t, Eigen::Ref< Eigen::VectorX< FP >> dydt) const
 This function evaluates the right-hand-side f of the ODE dydt = f(y, t). More...
 
Eigen::VectorX< FP > get_initial_values () const
 Get the initial conditions for the ODE dydt = f(y, t). More...
 
CompartmentalModeloperator= (CompartmentalModel &&)=default
 
CompartmentalModeloperator= (const CompartmentalModel &)=default
 
virtual ~CompartmentalModel ()=default
 

Private Types

using FlowIndex = details::filtered_index_t< Comp, Index, PopIndex >
 
using PopIndex = typename Pop::Index
 

Private Member Functions

template<size_t I = 0>
void get_rhs_impl (Eigen::Ref< const Eigen::VectorX< FP >> flows, Eigen::Ref< Eigen::VectorX< FP >> rhs, const FlowIndex &index) const
 Compute the derivatives of the compartments. More...
 

Private Attributes

Eigen::VectorX< FP > m_flow_values
 Cache to avoid allocation in get_derivatives (using get_flows). More...
 

Additional Inherited Members

- Public Attributes inherited from mio::CompartmentalModel< FP, Comp, Pop, Params >
ParameterSet parameters {}
 
Populations populations {}
 

Detailed Description

template<typename FP, class Comp, class Pop, class Params, class Flows>
class mio::FlowModel< FP, Comp, Pop, Params, Flows >

A FlowModel is a CompartmentalModel defined by the flows between compartments.

Inherits from

See also
CompartmentalModel, and defines the derivatives depending on the flows. Hence, a model implementing FlowModel has to define the function get_flows, instead of get_derivatives.

Flows is expected to be a TypeList containing types Flow<A,B>, where A and B are compartments from the enum Comp. Some examples can be found in the cpp/models/ directory, within the model.h files.

Member Typedef Documentation

◆ Base

template<typename FP , class Comp , class Pop , class Params , class Flows >
using mio::FlowModel< FP, Comp, Pop, Params, Flows >::Base = CompartmentalModel<FP, Comp, Pop, Params>

◆ FlowIndex

template<typename FP , class Comp , class Pop , class Params , class Flows >
using mio::FlowModel< FP, Comp, Pop, Params, Flows >::FlowIndex = details::filtered_index_t<Comp, Index, PopIndex>
private

◆ PopIndex

template<typename FP , class Comp , class Pop , class Params , class Flows >
using mio::FlowModel< FP, Comp, Pop, Params, Flows >::PopIndex = typename Pop::Index
private

Constructor & Destructor Documentation

◆ FlowModel()

template<typename FP , class Comp , class Pop , class Params , class Flows >
template<class... Args>
mio::FlowModel< FP, Comp, Pop, Params, Flows >::FlowModel ( Args...  args)
inline

Default constructor, forwarding args to Base constructor.

Member Function Documentation

◆ get_derivatives() [1/2]

template<typename FP , class Comp , class Pop , class Params , class Flows >
void mio::FlowModel< FP, Comp, Pop, Params, Flows >::get_derivatives ( Eigen::Ref< const Eigen::VectorX< FP >>  flows,
Eigen::Ref< Eigen::VectorX< FP >>  dydt 
) const
inline

Compute the right-hand-side of the ODE dydt = f(y, t) from flow values.

This function is generated at compile time depending on the template parameters Flows and Pop of the model.

Parameters
[in]flowsThe current flow values (as calculated by get_flows) as a flat array.
[out]dydtA reference to the calculated output.

◆ get_derivatives() [2/2]

template<typename FP , class Comp , class Pop , class Params , class Flows >
void mio::FlowModel< FP, Comp, Pop, Params, Flows >::get_derivatives ( Eigen::Ref< const Eigen::VectorX< FP >>  pop,
Eigen::Ref< const Eigen::VectorX< FP >>  y,
FP  t,
Eigen::Ref< Eigen::VectorX< FP >>  dydt 
) const
inlinefinalvirtual

Compute the right-hand-side f(y, t) of the ODE and store it in dydt.

This function uses get_flows(..., flows) and get_derivatives(flows, dydt) to provide the same interface as a CompartmentalModel.

Parameters
[in]popThe current population of the model as a flat array.
[in]yThe current state of the model as a flat array.
[in]tThe current time.
[out]dydtA reference to the calculated output.

Reimplemented from mio::CompartmentalModel< FP, Comp, Pop, Params >.

◆ get_flat_flow_index() [1/2]

template<typename FP , class Comp , class Pop , class Params , class Flows >
template<Comp Source, Comp Target>
constexpr size_t mio::FlowModel< FP, Comp, Pop, Params, Flows >::get_flat_flow_index ( ) const
inlineconstexpr

A flat index into an array of flows (as computed by get_flows), if the only used category in Pop is Comp.

Template Parameters
Sourcethe source of a flow.
Targetthe target of a flow.
Returns
A flat index into a data structure storing flow values.

◆ get_flat_flow_index() [2/2]

template<typename FP , class Comp , class Pop , class Params , class Flows >
template<Comp Source, Comp Target>
size_t mio::FlowModel< FP, Comp, Pop, Params, Flows >::get_flat_flow_index ( const FlowIndex indices) const
inline

A flat index into an array of flows (as computed by get_flows), given the indices of each category.

Calculation of flat indices (as in this->populations.get_flat_index) use the formula flat_index = ((((I_{1}) * D_{2} + I_{2}) * D_{3} + I_{3}) ... ) * D_{n} + I_{n} = I_{n} + I_{n-1} * D_{n} + I_{n-2} * D_{n} * D_{n-1} + ... + I_{1} * D_{n} * ... * D_{2} for indices (I_{1}, ... , I_{n}) and dimension (D_{1}, ... , D_{n}).

The flows use their position in the TypeList Flows instead of the template argument Comp from the base class, hence they use the Population::Index type without Comp. The position is determined by the Source and Target template arguments instead. As this dimension (the number of flows) may be larger or smaller (e.g. a S->I->R model has 3 Comp's, but 2 Flows) than Comp::Count, we cannot simply use this->populations.get_flat_index.

Instead, we omit Comp from PopIndex (getting FlowIndex), calculate the flat_index without the Comp index or Dimension, i.e. we omit I_j and D_j corresponding to Comp from the formula above. We do this by calling flatten_index with a FlowIndex, with dimensions derived from Pop via reduce_index. Then, we add the flow position I_{flow} via flow_index = flat_index * D_{flow} + I_{flow}, where D_{flow} is the number of flows.

Parameters
[in]indicesThe custom indices for each category.
Template Parameters
Sourcethe source of a flow.
Targetthe target of a flow.
Returns
A flat index into a data structure storing flow values.

◆ get_flows()

template<typename FP , class Comp , class Pop , class Params , class Flows >
virtual void mio::FlowModel< FP, Comp, Pop, Params, Flows >::get_flows ( Eigen::Ref< const Eigen::VectorX< FP >>  ,
Eigen::Ref< const Eigen::VectorX< FP >>  ,
FP  ,
Eigen::Ref< Eigen::VectorX< FP >>   
) const
pure virtual

◆ get_initial_flows()

template<typename FP , class Comp , class Pop , class Params , class Flows >
Eigen::VectorX<FP> mio::FlowModel< FP, Comp, Pop, Params, Flows >::get_initial_flows ( ) const
inline

Initial values for flows.

This can be used as initial conditions in an ODE solver. By default, this is a zero vector.

Returns
The initial flows.

◆ get_rhs_impl()

template<typename FP , class Comp , class Pop , class Params , class Flows >
template<size_t I = 0>
void mio::FlowModel< FP, Comp, Pop, Params, Flows >::get_rhs_impl ( Eigen::Ref< const Eigen::VectorX< FP >>  flows,
Eigen::Ref< Eigen::VectorX< FP >>  rhs,
const FlowIndex index 
) const
inlineprivate

Compute the derivatives of the compartments.

Compute the derivatives/rhs of the model equations for a given flow and index. Uses recursion, which is resolved at compile time depending on the Flows.

Parameters
[in]flowsCurrent change in flows, as computed by get_flows.
[out]rhsThe derivatives of the model equations.
[in,out]iThe Popindex at which to compute derivative. The entry for Comp wll be overwritten.
Template Parameters
IThe index of a flow in FlowChart.

Member Data Documentation

◆ m_flow_values

template<typename FP , class Comp , class Pop , class Params , class Flows >
Eigen::VectorX<FP> mio::FlowModel< FP, Comp, Pop, Params, Flows >::m_flow_values
mutableprivate

Cache to avoid allocation in get_derivatives (using get_flows).