stochastic_model.h Source File

CPP API: stochastic_model.h Source File
stochastic_model.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Rene Schmieding
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_STOCHASTIC_MODEL_H
21 #define MIO_COMPARTMENTS_STOCHASTIC_MODEL_H
22 
26 
27 namespace mio
28 {
29 
31 template <typename FP, class Comp, class Pop, class Params, class Flows = void>
33  : public std::conditional_t<std::is_same_v<Flows, void>, CompartmentalModel<FP, Comp, Pop, Params>,
34  FlowModel<FP, Comp, Pop, Params, Flows>>
35 {
36 public:
37  using Base = std::conditional_t<std::is_same_v<Flows, void>, CompartmentalModel<FP, Comp, Pop, Params>,
39  using Base::Base;
40 
57  virtual void get_noise(Eigen::Ref<const Eigen::VectorX<FP>> /*pop*/, Eigen::Ref<const Eigen::VectorX<FP>> /*y*/,
58  FP /*t*/, Eigen::Ref<Eigen::VectorX<FP>> /*noise*/) const {};
59 
66  auto white_noise(Eigen::Index size) const
67  {
68  return Eigen::VectorX<FP>::NullaryExpr(size, 1, [this]() {
70  });
71  }
72 
80  {
81  return FP{DistributionAdapter<std::normal_distribution<ScalarType>>::get_instance()(m_rng, 0.0, 1.0)};
82  }
83 
85  RandomNumberGenerator& get_rng() const
86  {
87  return m_rng;
88  }
89 
90 private:
91  mutable RandomNumberGenerator m_rng;
92 };
93 
100 template <class Model, typename FP>
102  requires(Model m, Eigen::Ref<const Eigen::VectorX<FP>> const_vref, Eigen::Ref<Eigen::VectorX<FP>> vref, FP t) {
103  requires IsCompartmentalModel<Model, FP>;
104  m.get_noise(const_vref, const_vref, t, vref);
105  };
106 
107 } // namespace mio
108 
109 #endif // MIO_COMPARTMENTS_STOCHASTIC_MODEL_H
A FlowModel is a CompartmentalModel defined by the flows between compartments.
Definition: flow_model.h:74
A CompartmentalModel with an additional get_noise member.
Definition: stochastic_model.h:35
RandomNumberGenerator & get_rng() const
Access the model's RNG.
Definition: stochastic_model.h:85
virtual void get_noise(Eigen::Ref< const Eigen::VectorX< FP >>, Eigen::Ref< const Eigen::VectorX< FP >>, FP, Eigen::Ref< Eigen::VectorX< FP >>) const
Calculate random changes to the population at a certain time point.
Definition: stochastic_model.h:57
RandomNumberGenerator m_rng
RNG for generating white noise in the get_noise implementation.
Definition: stochastic_model.h:91
std::conditional_t< std::is_same_v< Flows, void >, CompartmentalModel< FP, Comp, Pop, Params >, FlowModel< FP, Comp, Pop, Params, Flows > > Base
Definition: stochastic_model.h:38
auto white_noise(Eigen::Index size) const
Sample a vector of independent standard normal distributed values from the model's RNG.
Definition: stochastic_model.h:66
FP sample_standard_normal_distribution() const
Sample a standard normal distributed value from the model's RNG.
Definition: stochastic_model.h:79
The Model of the Simulation.
Definition: abm/model.h:54
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
requires(!std::is_trivial_v< T >) void BinarySerializerObject
Definition: binary_serializer.h:333
concept IsStochasticModel
Concept to check if a type is a valid stochastic model.
Definition: stochastic_model.h:101
CompartmentalModel is a template for a compartmental model for an array of initial populations and a ...
Definition: compartmental_model.h:59