model.h Source File

CPP API: model.h Source File
sde_sirs/model.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Nils Wassmuth, Rene Schmieding, Martin J. Kuehn
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 
21 #ifndef MIO_SDE_SIR_MODEL_H
22 #define MIO_SDE_SIR_MODEL_H
23 
28 #include "sde_sirs/parameters.h"
29 
30 namespace mio
31 {
32 namespace ssirs
33 {
34 
35 /********************
36  * define the model *
37  ********************/
38 
42 
43 template <typename FP>
44 class Model
45  : public mio::StochasticModel<FP, InfectionState, mio::Populations<FP, InfectionState>, Parameters<FP>, Flows>
46 {
47 public:
49 
51  : Base(typename Base::Populations({InfectionState::Count}, 0.0), typename Base::ParameterSet())
52  {
53  }
54 
55  void get_flows(Eigen::Ref<const Eigen::VectorX<FP>> pop, Eigen::Ref<const Eigen::VectorX<FP>> y, FP t,
56  Eigen::Ref<Eigen::VectorX<FP>> flows) const
57  {
58  auto& params = Base::parameters;
59  // effective contact rate by contact rate between groups i and j and damping j
60  FP season_val =
61  (1 + params.template get<Seasonality<FP>>() *
62  sin(std::numbers::pi_v<ScalarType> * ((params.template get<StartDay<FP>>() + t) / 182.5 + 0.5)));
63 
64  FP coeffStoI = season_val *
65  params.template get<ContactPatterns<FP>>().get_matrix_at(SimulationTime<FP>(t))(0, 0) *
66  params.template get<TransmissionProbabilityOnContact<FP>>() / Base::populations.get_total();
67 
68  flows[this->template get_flat_flow_index<InfectionState::Susceptible, InfectionState::Infected>()] =
69  coeffStoI * y[(size_t)InfectionState::Susceptible] * pop[(size_t)InfectionState::Infected];
70  flows[this->template get_flat_flow_index<InfectionState::Infected, InfectionState::Recovered>()] =
71  (1.0 / params.template get<TimeInfected<FP>>()) * y[(size_t)InfectionState::Infected];
72  flows[this->template get_flat_flow_index<InfectionState::Recovered, InfectionState::Susceptible>()] =
73  (1.0 / params.template get<TimeImmune<FP>>()) * y[(size_t)InfectionState::Recovered];
74  }
75 
76  void get_noise(Eigen::Ref<const Eigen::VectorX<FP>> pop, Eigen::Ref<const Eigen::VectorX<FP>> y, FP t,
77  Eigen::Ref<Eigen::VectorX<FP>> noise) const
78  {
79  Eigen::VectorX<FP> flows(Flows::size());
80  get_flows(pop, y, t, flows);
81  flows = flows.array().sqrt() * Base::white_noise(Flows::size()).array();
82  this->get_derivatives(flows, noise);
83  }
84 };
85 
86 } // namespace ssirs
87 } // namespace mio
88 
89 #endif // MIO_SDE_SIRS_MODEL_H
A class template for compartment populations.
Definition: populations.h:55
double simulation time.
Definition: damping.h:58
A CompartmentalModel with an additional get_noise member.
Definition: stochastic_model.h:35
auto white_noise(Eigen::Index size) const
Sample a vector of independent standard normal distributed values from the model's RNG.
Definition: stochastic_model.h:66
Definition: sde_sirs/model.h:46
Model()
Definition: sde_sirs/model.h:50
void get_flows(Eigen::Ref< const Eigen::VectorX< FP >> pop, Eigen::Ref< const Eigen::VectorX< FP >> y, FP t, Eigen::Ref< Eigen::VectorX< FP >> flows) const
Definition: sde_sirs/model.h:55
void get_noise(Eigen::Ref< const Eigen::VectorX< FP >> pop, Eigen::Ref< const Eigen::VectorX< FP >> y, FP t, Eigen::Ref< Eigen::VectorX< FP >> noise) const
Calculate random changes to the population at a certain time point.
Definition: sde_sirs/model.h:76
Parameters of SIR model.
Definition: sde_sirs/parameters.h:147
ad::internal::unary_intermediate< AD_TAPE_REAL, ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 >, ad::operations::ad_sin< AD_TAPE_REAL > > sin(const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &x1)
Definition: ad.hpp:913
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
constexpr std::tuple_element< I, std::tuple< Index< CategoryTags >... > >::type & get(Index< CategoryTags... > &i) noexcept
Retrieves the Index (by reference) at the Ith position of a MultiIndex.
Definition: index.h:294
A Flow defines a possible transition between two Compartments in a FlowModel.
Definition: flow.h:33
Collection of types. Each type is mapped to an index of type size_t.
Definition: type_list.h:32
static constexpr size_t size()
returns the number of Types in TypeList
Definition: type_list.h:45
The start day in the SIRS model The start day defines in which season the simulation can be started I...
Definition: sde_sirs/parameters.h:108