compartmental_model.h Source File

CPP API: compartmental_model.h Source File
compartmental_model.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Jan Kleinert, Daniel Abele
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_COMPARTMENTS_COMPARTMENTAL_MODEL_H
21 #define MIO_COMPARTMENTS_COMPARTMENTAL_MODEL_H
22 
23 #include "memilio/config.h" // IWYU pragma: keep
24 #include "memilio/math/eigen.h" // IWYU pragma: keep
25 
26 #include <concepts>
27 
28 namespace mio
29 {
30 
32 template <class T>
33 concept HasCheckConstraints = requires(T t) {
34  { t.check_constraints() } -> std::same_as<bool>;
35 };
36 
38 template <class T>
39 concept HasApplyConstraints = requires(T t) {
40  { t.apply_constraints() } -> std::same_as<bool>;
41 };
42 
58 template <typename FP, class Comp, class Pop, class Params>
60 public:
61  using Compartments = Comp;
62  using Populations = Pop;
63  using ParameterSet = Params;
68  : populations{std::move(po)}
69  , parameters{pa}
70  {
71  }
72 
77  virtual ~CompartmentalModel() = default;
78 
79  // REMARK: Not pure virtual for easier java/python bindings.
80  virtual void get_derivatives(Eigen::Ref<const Eigen::VectorX<FP>> /*pop*/,
81  Eigen::Ref<const Eigen::VectorX<FP>> /*y*/, FP /*t*/,
82  Eigen::Ref<Eigen::VectorX<FP>> /*dydt*/) const {};
83 
104  void eval_right_hand_side(Eigen::Ref<const Eigen::VectorX<FP>> pop, Eigen::Ref<const Eigen::VectorX<FP>> y, FP t,
105  Eigen::Ref<Eigen::VectorX<FP>> dydt) const
106  {
107  dydt.setZero();
108  this->get_derivatives(pop, y, t, dydt);
109  }
110 
116  Eigen::VectorX<FP> get_initial_values() const
117  {
118  return populations.get_compartments();
119  }
120 
132  {
133  if constexpr (HasApplyConstraints<ParameterSet>) {
134  // use bitwise instead of logical or, so that both apply_constraint functions are called
135  return ((int)parameters.apply_constraints() | (int)populations.apply_constraints());
136  }
137  else {
138  return populations.check_constraints();
139  }
140  }
141 
146  bool check_constraints() const
147  {
148  if constexpr (HasCheckConstraints<ParameterSet>) {
149  return (parameters.check_constraints() || populations.check_constraints());
150  }
151  else {
152  return populations.check_constraints();
153  }
154  }
155 
158 };
159 
166 template <class Model, typename FP>
168  requires(Model m, Eigen::Ref<const Eigen::VectorX<FP>> pop_y, Eigen::Ref<Eigen::VectorX<FP>> dydt, FP t) {
169  { m.get_initial_values() } -> std::convertible_to<Eigen::VectorX<FP>>;
170  m.eval_right_hand_side(pop_y, pop_y, t, dydt);
171  };
172 
173 } // namespace mio
174 
175 #endif // MIO_COMPARTMENTS_COMPARTMENTAL_MODEL_H
The Model of the Simulation.
Definition: abm/model.h:54
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
requires(!std::is_trivial_v< T >) void BinarySerializerObject
Definition: binary_serializer.h:333
concept HasCheckConstraints
Check that the given type has a check_constraints member function.
Definition: compartmental_model.h:33
concept HasApplyConstraints
Check that the given type has an apply_constraints member function.
Definition: compartmental_model.h:39
concept IsCompartmentalModel
Concept to check if a type is a valid compartment model.
Definition: compartmental_model.h:167
Definition: io.h:94
CompartmentalModel is a template for a compartmental model for an array of initial populations and a ...
Definition: compartmental_model.h:59
CompartmentalModel(const CompartmentalModel &)=default
bool apply_constraints()
Checks whether the model satisfies all constraints.
Definition: compartmental_model.h:131
Populations populations
Definition: compartmental_model.h:156
bool check_constraints() const
Checks that the model satisfies all constraints (e.g.
Definition: compartmental_model.h:146
CompartmentalModel(CompartmentalModel &&)=default
CompartmentalModel(Populations const &po, ParameterSet const &pa)
CompartmentalModel default constructor.
Definition: compartmental_model.h:67
Pop Populations
Definition: compartmental_model.h:62
Params ParameterSet
Definition: compartmental_model.h:63
Eigen::VectorX< FP > get_initial_values() const
Get the initial conditions for the ODE dydt = f(y, t).
Definition: compartmental_model.h:116
ParameterSet parameters
Definition: compartmental_model.h:157
Comp Compartments
Definition: compartmental_model.h:61
void eval_right_hand_side(Eigen::Ref< const Eigen::VectorX< FP >> pop, Eigen::Ref< const Eigen::VectorX< FP >> y, FP t, Eigen::Ref< Eigen::VectorX< FP >> dydt) const
This function evaluates the right-hand-side f of the ODE dydt = f(y, t).
Definition: compartmental_model.h:104
CompartmentalModel & operator=(CompartmentalModel &&)=default
virtual void get_derivatives(Eigen::Ref< const Eigen::VectorX< FP >>, Eigen::Ref< const Eigen::VectorX< FP >>, FP, Eigen::Ref< Eigen::VectorX< FP >>) const
Definition: compartmental_model.h:80
virtual ~CompartmentalModel()=default
CompartmentalModel & operator=(const CompartmentalModel &)=default