parameters.h Source File

CPP API: parameters.h Source File
ode_sir/parameters.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 SIR_PARAMETERS_H
22 #define SIR_PARAMETERS_H
23 
29 
30 namespace mio
31 {
32 namespace osir
33 {
34 
35 /**************************************
36  * Define Parameters of the SIR model *
37  **************************************/
38 
42 template <typename FP>
46  {
47  return Type(size, 1.0);
48  }
49  static std::string name()
50  {
51  return "TransmissionProbabilityOnContact";
52  }
53 };
54 
58 template <typename FP>
59 struct TimeInfected {
62  {
63  return Type(size, 6.0);
64  }
65  static std::string name()
66  {
67  return "TimeInfected";
68  }
69 };
70 
74 template <typename FP>
78  {
79  return Type(1, static_cast<Eigen::Index>((size_t)size));
80  }
81  static std::string name()
82  {
83  return "ContactPatterns";
84  }
85 };
86 
87 template <typename FP>
89 
93 template <typename FP>
94 class Parameters : public ParametersBase<FP>
95 {
96 public:
97  Parameters(AgeGroup num_agegroups)
98  : ParametersBase<FP>(num_agegroups)
99  , m_num_groups{num_agegroups}
100  {
101  }
102 
104  {
105  return m_num_groups;
106  }
107 
122  {
123  FP tol_times = 1e-1;
124 
125  int corrected = false;
126 
127  for (auto i = AgeGroup(0); i < AgeGroup(m_num_groups); i++) {
128  if (this->template get<TimeInfected<FP>>()[i] < tol_times) {
129  log_warning(
130  "Constraint check: Parameter TimeInfected changed from {} to {}. Please note that "
131  "unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
132  "and reset parameters.",
133  this->template get<TimeInfected<FP>>()[i], tol_times);
134  this->template get<TimeInfected<FP>>()[i] = tol_times;
135  corrected = true;
136  }
137  if (this->template get<TransmissionProbabilityOnContact<FP>>()[i] < 0.0 ||
138  this->template get<TransmissionProbabilityOnContact<FP>>()[i] > 1.0) {
139  log_warning("Constraint check: Parameter TransmissionProbabilityOnContact changed from {} to {} ",
140  this->template get<TransmissionProbabilityOnContact<FP>>()[i], 0.0);
141  this->template get<TransmissionProbabilityOnContact<FP>>() = 0.0;
142  corrected = true;
143  }
144  }
145  return corrected;
146  }
147 
153  bool check_constraints() const
154  {
155  FP tol_times = 1e-1;
156 
157  for (auto i = AgeGroup(0); i < AgeGroup(m_num_groups); i++) {
158 
159  if (this->template get<TimeInfected<FP>>()[i] < tol_times) {
160  log_warning(
161  "Constraint check: Parameter TimeInfected {} smaller or equal {}. Please note that "
162  "unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
163  "and reset parameters.",
164  this->template get<TimeInfected<FP>>()[i], tol_times);
165  return true;
166  }
167  if (this->template get<TransmissionProbabilityOnContact<FP>>()[i] < 0.0 ||
168  this->template get<TransmissionProbabilityOnContact<FP>>()[i] > 1.0) {
169  log_error("Constraint check: Parameter TransmissionProbabilityOnContact {} smaller {} or "
170  "greater {}",
171  this->template get<TransmissionProbabilityOnContact<FP>>()[i], 0.0, 1.0);
172  return true;
173  }
174  }
175  return false;
176  }
177 
178 private:
180  : ParametersBase<FP>(std::move(base))
181  , m_num_groups(this->template get<ContactPatterns<FP>>().get_cont_freq_mat().get_num_groups())
182  {
183  }
184 
185 public:
190  template <class IOContext>
191  static IOResult<Parameters> deserialize(IOContext& io)
192  {
193  BOOST_OUTCOME_TRY(auto&& base, ParametersBase<FP>::deserialize(io));
194  return success(Parameters(std::move(base)));
195  }
196 
197 private:
199 };
200 
201 } // namespace osir
202 } // namespace mio
203 
204 #endif // SIR_PARAMETERS_H
A class template for an array with custom indices.
Definition: custom_index_array.h:136
a set of parameters defined at compile time
Definition: parameter_set.h:205
const ParameterTagTraits< Tag >::Type & get() const
get value of a parameter
Definition: parameter_set.h:262
The UncertainContactMatrix class consists of a ContactMatrix with fixed baseline and uncertain Dampin...
Definition: uncertain_matrix.h:43
Parameters of SIR model.
Definition: ode_sir/parameters.h:95
bool check_constraints() const
Checks whether all Parameters satisfy their corresponding constraints and logs an error if constraint...
Definition: ode_sir/parameters.h:153
static IOResult< Parameters > deserialize(IOContext &io)
deserialize an object of this class.
Definition: ode_sir/parameters.h:191
bool apply_constraints()
Checks whether all Parameters satisfy their corresponding constraints and applies them,...
Definition: ode_sir/parameters.h:121
Parameters(AgeGroup num_agegroups)
Definition: ode_sir/parameters.h:97
AgeGroup m_num_groups
Definition: ode_sir/parameters.h:198
Parameters(ParametersBase< FP > &&base)
Definition: ode_sir/parameters.h:179
AgeGroup get_num_groups()
Definition: ode_sir/parameters.h:103
int size(Comm comm)
Return the size of the given communicator.
Definition: miompi.cpp:75
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
auto success()
Create an object that is implicitly convertible to a succesful IOResult<void>.
Definition: io.h:359
void log_error(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:100
boost::outcome_v2::unchecked< T, IOStatus > IOResult
Value-or-error type for operations that return a value but can fail.
Definition: io.h:353
Definition: io.h:94
The AgeGroup struct is used as a dynamically sized tag for all age dependent categories.
Definition: age_group.h:32
the contact patterns within the society are modelled using a ContactMatrix
Definition: ode_sir/parameters.h:75
static std::string name()
Definition: ode_sir/parameters.h:81
UncertainContactMatrix< FP > Type
Definition: ode_sir/parameters.h:76
static Type get_default(AgeGroup size)
Definition: ode_sir/parameters.h:77
the infectious time in day unit
Definition: ode_sir/parameters.h:59
static Type get_default(AgeGroup size)
Definition: ode_sir/parameters.h:61
CustomIndexArray< UncertainValue< FP >, AgeGroup > Type
Definition: ode_sir/parameters.h:60
static std::string name()
Definition: ode_sir/parameters.h:65
probability of getting infected from a contact
Definition: ode_sir/parameters.h:43
static std::string name()
Definition: ode_sir/parameters.h:49
static Type get_default(AgeGroup size)
Definition: ode_sir/parameters.h:45
CustomIndexArray< UncertainValue< FP >, AgeGroup > Type
Definition: ode_sir/parameters.h:44