model.h Source File

CPP API: model.h Source File
ide_seir/model.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Lena Ploetzke
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 IDE_SEIR_H
22 #define IDE_SEIR_H
23 
24 #include "memilio/math/eigen.h"
26 #include "ide_seir/parameters.h"
28 #include "memilio/utils/logging.h"
29 
30 #include <iostream>
31 
32 #include <vector>
33 
34 namespace mio
35 {
36 namespace iseir
37 {
38 class Model
39 {
40  using Pa = ParametersBase;
41  using Vec = Eigen::VectorX<ScalarType>;
42 
43 public:
57  Model(TimeSeries<ScalarType>&& init, ScalarType dt_init, int N_init, const Pa& Parameterset_init = Pa())
58  : parameters{Parameterset_init}
59  , m_result{std::move(init)}
61  , m_dt{dt_init}
62  , m_N{N_init}
63  {
64  }
65 
77  TimeSeries<ScalarType> const& simulate(int t_max)
78  {
79  m_l = (int)std::floor(parameters.template get<LatencyTime>() / m_dt);
80  m_k =
81  (int)std::ceil((parameters.template get<InfectiousTime>() + parameters.template get<LatencyTime>()) / m_dt);
82  if (m_result.get_time(0) > -(m_k - 1) * m_dt) {
83  log_warning("Constraint check: Initial data starts later than necessary. The simulation may be distorted. "
84  "Start the data at time {:.4f} at the latest.",
85  -(m_k - 1) * m_dt);
86  }
87 
88  while (m_result.get_last_time() < t_max) {
90  Eigen::Index idx = m_result.get_num_time_points();
91 
92  // R0t is the effective reproduction number at time t
93  auto R0t1 = parameters.template get<ContactFrequency>().get_cont_freq_mat().get_matrix_at(
95  parameters.template get<TransmissionRisk>() * parameters.template get<InfectiousTime>();
96  auto R0t2 = parameters.template get<ContactFrequency>().get_cont_freq_mat().get_matrix_at(
98  parameters.template get<TransmissionRisk>() * parameters.template get<InfectiousTime>();
99 
101  Vec::Constant(1, m_result[idx - 2][0] * exp(m_dt * (0.5 * 1 / m_N) *
102  (R0t1 * num_integration_inner_integral(idx - 2) +
103  R0t2 * num_integration_inner_integral(idx - 1))));
104  }
105  return m_result;
106  }
107 
118  {
119  Eigen::Index num_points = m_result.get_num_time_points();
120  ScalarType S, E, I, R;
121  for (Eigen::Index i = m_k; i < num_points; ++i) {
122  S = m_result[i][Eigen::Index(InfectionState::S)];
123  E = m_result[i - m_l][Eigen::Index(InfectionState::S)] - S;
124  I = m_result[i - m_k][Eigen::Index(InfectionState::S)] - m_result[i - m_l][Eigen::Index(InfectionState::S)];
125  R = m_N - S - E - I;
126  m_result_SEIR.add_time_point(m_result.get_time(i), (Vec(4) << S, E, I, R).finished());
127  }
128  return m_result_SEIR;
129  }
130 
131  // Used Parameters for the simulation.
133 
134 private:
144  {
145  if ((parameters.template get<LatencyTime>() < tau) &&
146  (parameters.template get<InfectiousTime>() + parameters.template get<LatencyTime>() > tau)) {
147  return std::tgamma(p + q) * pow(tau - parameters.template get<LatencyTime>(), p - 1) *
148  pow(parameters.template get<InfectiousTime>() + parameters.template get<LatencyTime>() - tau,
149  q - 1) /
150  (std::tgamma(p) * std::tgamma(q) * std::pow(parameters.template get<InfectiousTime>(), p + q - 1));
151  }
152  return 0.0;
153  }
154 
165  Eigen::Index idx) const
166  {
167  return (ts_ide[idx + 1][Eigen::Index(compartment)] - ts_ide[idx - 1][Eigen::Index(compartment)]) / (2 * m_dt);
168  }
169 
178  {
183  Eigen::Index i = idx - m_k + 1;
184  while (i <= idx - m_l - 2) {
187  ++i;
188  }
189  return res;
190  }
191 
192  // TimeSeries containing points of time and the corresponding number of susceptibles.
194  // TimeSeries containing points of time and the corresponding number of susceptibles, exposed,
195  // infected and recovered.
197 
198  // Timestep used for simulation.
200  // Population of the considered region.
201  int m_N{0};
202 
203  // Two Indices used for simulation.
204  Eigen::Index m_k{0};
205  Eigen::Index m_l{0};
206 };
207 } // namespace iseir
208 } // namespace mio
209 #endif
a set of parameters defined at compile time
Definition: parameter_set.h:205
double simulation time.
Definition: damping.h:58
FP & get_last_time()
time of time point at index num_time_points - 1
Definition: time_series.h:286
Eigen::Index get_num_time_points() const
number of time points in the series
Definition: time_series.h:197
FP & get_time(Eigen::Index i)
time of time point at index i
Definition: time_series.h:272
Eigen::Ref< Vector > add_time_point()
add one uninitialized time point
Definition: time_series.h:221
Eigen::Ref< const Vector > get_last_value() const
reference to value vector at time point (num_timepoints - 1)
Definition: time_series.h:320
Definition: ide_seir/model.h:39
int m_N
Definition: ide_seir/model.h:201
TimeSeries< ScalarType > const & simulate(int t_max)
Simulate the evolution of infection numbers with the given IDE SEIR model.
Definition: ide_seir/model.h:77
ScalarType generalized_beta_distribution(ScalarType tau, ScalarType p=3.0, ScalarType q=10.0) const
Density of the generalized beta distribution used for the function f_{beta} of the IDE SEIR model.
Definition: ide_seir/model.h:143
ParametersBase Pa
Definition: ide_seir/model.h:40
TimeSeries< ScalarType > const & calculate_EIR()
Calculate the distribution of the population in E, I and, R based on the calculated values for S.
Definition: ide_seir/model.h:117
Pa parameters
Definition: ide_seir/model.h:132
Model(TimeSeries< ScalarType > &&init, ScalarType dt_init, int N_init, const Pa &Parameterset_init=Pa())
Create an IDE SEIR model.
Definition: ide_seir/model.h:57
TimeSeries< ScalarType > m_result
Definition: ide_seir/model.h:193
TimeSeries< ScalarType > m_result_SEIR
Definition: ide_seir/model.h:196
Eigen::VectorX< ScalarType > Vec
Definition: ide_seir/model.h:41
ScalarType central_difference_quotient(TimeSeries< ScalarType > const &ts_ide, InfectionState compartment, Eigen::Index idx) const
Numerical differentiation of one compartment using a central difference quotient.
Definition: ide_seir/model.h:164
ScalarType m_dt
Definition: ide_seir/model.h:199
Eigen::Index m_l
Definition: ide_seir/model.h:205
Eigen::Index m_k
Definition: ide_seir/model.h:204
ScalarType num_integration_inner_integral(Eigen::Index idx) const
Numerical integration of the inner integral of the integro-differential equation for the group S usin...
Definition: ide_seir/model.h:177
double ScalarType
Configuration of memilio library.
Definition: memilio/config.h:30
static double floor(const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &x)
Definition: ad.hpp:2451
ad::internal::binary_intermediate_aa< AD_TAPE_REAL, ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 >, ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 >, ad::operations::ad_pow_aa< AD_TAPE_REAL > > pow(const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &x1, const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &x2)
Definition: ad.hpp:1610
static double ceil(const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &x)
Definition: ad.hpp:2449
ad::internal::unary_intermediate< AD_TAPE_REAL, ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 >, ad::operations::ad_exp< AD_TAPE_REAL > > exp(const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &x1)
Definition: ad.hpp:990
InfectionState
The InfectionState enum describes the possible categories for the infectious state of persons in ide_...
Definition: ide_seir/infection_state.h:33
ParameterSet< TransmissionRisk, LatencyTime, InfectiousTime, ContactFrequency > ParametersBase
Definition: ide_seir/parameters.h:96
void init()
Initialize MPI.
Definition: miompi.cpp:42
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
auto i
Definition: io.h:809
void log_warning(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:112
Definition: io.h:94