lct_infection_state.h Source File

CPP API: lct_infection_state.h Source File
lct_infection_state.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_INFECTION_STATE_H
21 #define MIO_EPI_LCT_INFECTION_STATE_H
22 
23 #include "memilio/config.h" // IWYU pragma: keep
24 #include "memilio/math/eigen.h" // IWYU pragma: keep
25 
26 #include <array>
27 
28 namespace mio
29 {
40 template <typename FP, class InfectionStates, size_t... Ns>
42 {
43 public:
44  using InfectionState = InfectionStates;
45  static_assert((size_t)InfectionState::Count == sizeof...(Ns),
46  "The number of the size_t's provided as template parameters must be "
47  "the same as the entry Count of InfectionState.");
48 
49  static_assert(((Ns > 0) && ...), "The number of subcompartments must be at least 1.");
50 
57  template <InfectionState State>
58  static constexpr size_t get_num_subcompartments()
59  {
60  static_assert(State < InfectionState::Count, "State must be a a valid InfectionState.");
61  return m_subcompartment_numbers[(size_t)State];
62  }
63 
73  template <InfectionState State>
74  static constexpr size_t get_first_index()
75  {
76  static_assert(State < InfectionState::Count, "State must be a a valid InfectionState.");
77  size_t index = 0;
78  for (size_t i = 0; i < (size_t)(State); i++) {
79  index = index + m_subcompartment_numbers[i];
80  }
81  return index;
82  }
83 
93  static Eigen::VectorX<FP> calculate_compartments(const Eigen::VectorX<FP>& subcompartments)
94  {
95  assert(subcompartments.rows() == Count);
96 
97  Eigen::VectorX<FP> compartments((Eigen::Index)InfectionState::Count);
98  // Use segment of the vector subcompartments of each InfectionState and sum up the values of subcompartments.
99  for (int i = 0; i < (Eigen::Index)InfectionState::Count; i++) {
100  auto State = static_cast<InfectionState>(i);
101  // first index of first subcompartment:
102  size_t index = 0;
103  for (size_t j = 0; j < (size_t)(State); j++) {
104  index = index + m_subcompartment_numbers[j];
105  }
106  // number of subcompartments:
107  size_t num_subcomp = m_subcompartment_numbers[(size_t)State];
108  compartments[i] = subcompartments.segment(index, num_subcomp).sum();
109  }
110 
111  return compartments;
112  }
113 
114  static constexpr size_t Count{(... + Ns)};
115 
116 private:
117  static constexpr const std::array<size_t, sizeof...(Ns)> m_subcompartment_numbers{
118  Ns...};
119 };
120 
121 } // namespace mio
122 
123 #endif
Provides the functionality to be able to work with subcompartments in an LCT model.
Definition: lct_infection_state.h:42
static constexpr const std::array< size_t, sizeof...(Ns)> m_subcompartment_numbers
Vector which defines the number of subcompartments for each infection state of InfectionState.
Definition: lct_infection_state.h:117
static constexpr size_t Count
Definition: lct_infection_state.h:114
static Eigen::VectorX< FP > calculate_compartments(const Eigen::VectorX< FP > &subcompartments)
Cumulates a vector with the number of individuals in each subcompartment (with subcompartments accord...
Definition: lct_infection_state.h:93
InfectionStates InfectionState
Definition: lct_infection_state.h:44
static constexpr size_t get_first_index()
Gets the index of the first subcompartment of an infection state.
Definition: lct_infection_state.h:74
static constexpr size_t get_num_subcompartments()
Gets the number of subcompartments in an infection state.
Definition: lct_infection_state.h:58
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