model.h Source File

CPP API: model.h Source File
ode_sir/model.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Daniel Abele, Jan Kleinert, 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 ODESIR_MODEL_H
22 #define ODESIR_MODEL_H
23 
29 #include "ode_sir/parameters.h"
30 
31 namespace mio
32 {
33 namespace osir
34 {
35 
36 /********************
37  * define the model *
38  ********************/
39 
40 template <typename FP>
41 class Model
42  : public mio::CompartmentalModel<FP, InfectionState, mio::Populations<FP, AgeGroup, InfectionState>, Parameters<FP>>
43 {
44  using Base =
46 
47 public:
48  using typename Base::ParameterSet;
49  using typename Base::Populations;
50 
51  Model(const Populations& pop, const ParameterSet& params)
52  : Base(pop, params)
53  {
54  }
55 
56  Model(int num_agegroups)
57  : Base(Populations({AgeGroup(num_agegroups), InfectionState::Count}), ParameterSet(AgeGroup(num_agegroups)))
58  {
59  }
60 
61  void get_derivatives(Eigen::Ref<const Eigen::VectorX<FP>> pop, Eigen::Ref<const Eigen::VectorX<FP>> y, FP t,
62  Eigen::Ref<Eigen::VectorX<FP>> dydt) const override
63  {
64  auto params = this->parameters;
65  AgeGroup n_agegroups = params.get_num_groups();
66  ContactMatrixGroup<FP> const& contact_matrix = params.template get<ContactPatterns<FP>>();
67 
68  for (auto i = AgeGroup(0); i < n_agegroups; i++) {
69 
73 
74  for (auto j = AgeGroup(0); j < n_agegroups; j++) {
75 
77  size_t Ij = this->populations.get_flat_index({j, InfectionState::Infected});
79 
80  const FP Nj = pop[Sj] + pop[Ij] + pop[Rj];
81  const FP divNj = (Nj < Limits<FP>::zero_tolerance()) ? FP(0.0) : FP(1.0 / Nj);
82 
83  FP coeffStoI = contact_matrix.get_matrix_at(SimulationTime<FP>(t))(
84  static_cast<Eigen::Index>((size_t)i), static_cast<Eigen::Index>((size_t)j)) *
85  params.template get<TransmissionProbabilityOnContact<FP>>()[i] * divNj;
86 
87  dydt[Si] += -coeffStoI * y[Si] * pop[Ij];
88  dydt[Ii] += coeffStoI * y[Si] * pop[Ij];
89  }
90  dydt[Ii] -= (1.0 / params.template get<TimeInfected<FP>>()[i]) * y[Ii];
91  dydt[Ri] = (1.0 / params.template get<TimeInfected<FP>>()[i]) * y[Ii];
92  }
93  }
94 
99  template <class IOContext>
100  void serialize(IOContext& io) const
101  {
102  auto obj = io.create_object("Model");
103  obj.add_element("Parameters", this->parameters);
104  obj.add_element("Populations", this->populations);
105  }
106 
111  template <class IOContext>
112  static IOResult<Model> deserialize(IOContext& io)
113  {
114  auto obj = io.expect_object("Model");
115  auto par = obj.expect_element("Parameters", Tag<ParameterSet>{});
116  auto pop = obj.expect_element("Populations", Tag<Populations>{});
117  return apply(
118  io,
119  [](auto&& par_, auto&& pop_) {
120  return Model{pop_, par_};
121  },
122  par, pop);
123  }
124 };
125 
126 } // namespace osir
127 } // namespace mio
128 
129 #endif // ODESIR_MODEL_H
represents a collection of contact frequency matrices that whose sum is the total number of contacts.
Definition: contact_matrix.h:536
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
Definition: custom_index_array.h:309
A class template for compartment populations.
Definition: populations.h:55
double simulation time.
Definition: damping.h:58
Parameters of the simulation that are the same everywhere within the Model.
Definition: abm/parameters.h:764
Definition: ode_sir/model.h:43
void serialize(IOContext &io) const
serialize this.
Definition: ode_sir/model.h:100
static IOResult< Model > deserialize(IOContext &io)
deserialize an object of this class.
Definition: ode_sir/model.h:112
Model(const Populations &pop, const ParameterSet &params)
Definition: ode_sir/model.h:51
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 override
Definition: ode_sir/model.h:61
Model(int num_agegroups)
Definition: ode_sir/model.h:56
Parameters of SIR model.
Definition: ode_sir/parameters.h:95
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
boost::outcome_v2::in_place_type_t< T > Tag
Type that is used for overload resolution.
Definition: io.h:407
auto i
Definition: io.h:809
details::ApplyResultT< F, T... > apply(IOContext &io, F f, const IOResult< T > &... rs)
Evaluate a function with zero or more unpacked IOResults as arguments.
Definition: io.h:481
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
boost::outcome_v2::unchecked< T, IOStatus > IOResult
Value-or-error type for operations that return a value but can fail.
Definition: io.h:353
The AgeGroup struct is used as a dynamically sized tag for all age dependent categories.
Definition: age_group.h:32
CompartmentalModel is a template for a compartmental model for an array of initial populations and a ...
Definition: compartmental_model.h:59
mio::Populations< FP, AgeGroup, InfectionState > Populations
Definition: compartmental_model.h:62
probability of getting infected from a contact
Definition: ode_sir/parameters.h:43