lct_populations.h Source File

CPP API: lct_populations.h Source File
lct_populations.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 #ifndef MIO_EPI_LCT_POPULATIONS_H
21 #define MIO_EPI_LCT_POPULATIONS_H
22 
23 #include "memilio/config.h" // IWYU pragma: keep
24 #include "memilio/math/eigen.h" // IWYU pragma: keep
25 #include "memilio/epidemiology/lct_infection_state.h" // IWYU pragma: keep for ease of use
29 #include <cstddef>
30 
31 namespace mio
32 {
33 
55 template <typename FP, class... LctStates>
57 {
58 public:
60  using InternalArrayType = Eigen::Array<Type, Eigen::Dynamic, 1>;
61  using LctStatesGroups = TypeList<LctStates...>;
62  static size_t constexpr num_groups = sizeof...(LctStates);
63  static_assert(num_groups >= 1, "The number of LctStates provided should be at least one.");
64 
67  : m_y(InternalArrayType::Constant(get_count(), UncertainValue<FP>(0.0)))
68  {
69  }
70 
75  size_t get_num_compartments() const
76  {
77  return m_y.size();
78  }
79 
84  auto const& array() const
85  {
86  return m_y;
87  }
88  auto& array()
89  {
90  return m_y;
91  }
92 
98  Type& operator[](size_t index)
99  {
100  assert(index < static_cast<size_t>(m_y.size()));
101  return m_y[index];
102  }
103 
109  template <size_t Group>
111  {
112  static_assert((Group < num_groups) && (Group >= 0), "The template parameter Group should be valid.");
113  if constexpr (Group == 0) {
114  return 0;
115  }
116  else {
118  }
119  }
125  inline Eigen::VectorX<FP> get_compartments() const
126  {
127  return m_y.template cast<FP>();
128  }
129 
135  template <size_t Group>
136  FP get_group_total() const
137  {
138  return m_y.template cast<FP>()
139  .segment(get_first_index_of_group<Group>(), type_at_index_t<Group, LctStatesGroups>::Count)
140  .sum();
141  }
142 
147  FP get_total() const
148  {
149  return m_y.template cast<FP>().sum();
150  }
151 
165  {
166  bool corrected = false;
167  for (int i = 0; i < m_y.size(); i++) {
168  if (m_y[i] < 0.0) {
169  if (m_y[i] > -1e-10) {
170  log_warning("Constraint check: Compartment number {} changed from {} to {}", i, m_y[i], 0);
171  }
172  else {
173  log_error("Constraint check: Compartment number {} changed from {} to {}", i, m_y[i], 0);
174  }
175  m_y[i] = 0.;
176  corrected = true;
177  }
178  }
179  return corrected;
180  }
181 
186  bool check_constraints() const
187  {
188  if ((m_y < 0.0).any()) {
189  log_error("Constraint check: At least one compartment size is smaller {}.", 0);
190  return true;
191  }
192  return false;
193  }
194 
195 private:
200  template <size_t Group = 0>
201  [[nodiscard]] static size_t get_count()
202  {
203  size_t count = 0;
204  if constexpr (Group < num_groups) {
205  count = type_at_index_t<Group, LctStatesGroups>::Count + get_count<Group + 1>();
206  }
207  return count;
208  }
209 
210  InternalArrayType m_y{}; //< An array containing the number of people in the groups.
211 };
212 
213 } // namespace mio
214 
215 #endif // MIO_EPI_LCT_POPULATIONS_H
A class template for compartment populations of LCT models.
Definition: lct_populations.h:57
FP get_group_total() const
Returns the total population of a group.
Definition: lct_populations.h:136
auto & array()
Definition: lct_populations.h:88
FP get_total() const
Returns the total population of all compartments and groups.
Definition: lct_populations.h:147
InternalArrayType m_y
Definition: lct_populations.h:210
size_t get_first_index_of_group() const
Gets the first index of a group in the flat array.
Definition: lct_populations.h:110
static size_t get_count()
Sets recursively the total number of (sub-)compartments over all groups.
Definition: lct_populations.h:201
bool check_constraints() const
Checks whether all compartments have non-negative values and logs an error if constraint is not satis...
Definition: lct_populations.h:186
Type & operator[](size_t index)
Returns the entry of the array given a flat index.
Definition: lct_populations.h:98
size_t get_num_compartments() const
get_num_compartments Returns the number of compartments.
Definition: lct_populations.h:75
static constexpr size_t num_groups
Number of groups.
Definition: lct_populations.h:62
LctPopulations()
Default constructor.
Definition: lct_populations.h:66
bool apply_constraints()
Checks whether all compartments have non-negative values.
Definition: lct_populations.h:164
auto const & array() const
Returns a reference to the internally stored flat array.
Definition: lct_populations.h:84
Eigen::VectorX< FP > get_compartments() const
Returns an Eigen copy of the vector of populations.
Definition: lct_populations.h:125
Eigen::Array< Type, Eigen::Dynamic, 1 > InternalArrayType
Definition: lct_populations.h:60
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
typename type_at_index< Index, Types... >::type type_at_index_t
The type at the Index-th position in the list Types.
Definition: metaprogramming.h:118
void log_error(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:100
Collection of types. Each type is mapped to an index of type size_t.
Definition: type_list.h:32