model.h Source File

CPP API: model.h Source File
smm/model.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: René Schmieding, Julia Bicker, Kilian Volmer
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 MIO_SMM_MODEL_H
22 #define MIO_SMM_MODEL_H
23 
24 #include "memilio/utils/index.h"
26 #include "smm/parameters.h"
30 #include <utility>
31 
32 namespace mio
33 {
34 namespace smm
35 {
36 
38 template <class Status, class Region>
39 using PopulationIndex = decltype(concatenate_indices(std::declval<Region>(), std::declval<Status>()));
40 
53 template <typename FP, class Comp, class Status = Comp, class Region = mio::regions::Region>
54 class Model : public mio::CompartmentalModel<FP, Comp, mio::Populations<FP, PopulationIndex<Status, Region>>,
55  ParametersBase<FP, Status, Region>>
56 {
59  static_assert(!Base::Populations::Index::has_duplicates, "Do not use the same Index tag multiple times!");
60 
61 public:
62  Model(Status status_dimensions, Region region_dimensions)
63  : Base(typename Base::Populations(concatenate_indices(region_dimensions, status_dimensions)),
64  typename Base::ParameterSet())
65  {
66  }
67 
74  FP evaluate(const AdoptionRate<FP, Status, Region>& rate, const Eigen::VectorX<FP>& x) const
75  {
76  const auto& pop = this->populations;
77  const auto source = pop.get_flat_index({rate.region, rate.from});
78  // determine order and calculate rate
79  if (rate.influences.size() == 0) { // first order adoption
80  return rate.factor * x[source];
81  }
82  else { // second order adoption
83  FP N = 0;
84  for (auto status : make_index_range(reduce_index<Status>(this->populations.size()))) {
85  N += x[pop.get_flat_index({rate.region, status})];
86  }
87  // accumulate influences
88  FP influences = 0.0;
89  for (size_t i = 0; i < rate.influences.size(); i++) {
90  influences +=
91  rate.influences[i].factor * x[pop.get_flat_index({rate.region, rate.influences[i].status})];
92  }
93  return (N > 0) ? (rate.factor * x[source] * influences / N) : 0;
94  }
95  }
96 
103  FP evaluate(const TransitionRate<FP, Status, Region>& rate, const Eigen::VectorX<FP>& x) const
104  {
105  const auto source = this->populations.get_flat_index({rate.from, rate.status});
106  return rate.factor * x[source];
107  }
108 
113  mio::RandomNumberGenerator& get_rng()
114  {
115  return m_rng;
116  }
117 
118 private:
119  mio::RandomNumberGenerator m_rng;
120 };
121 
122 } //namespace smm
123 
124 } // namespace mio
125 
126 #endif
mio::Index< Tag > size() const
returns the size along the dimension provided as template parameter
Definition: custom_index_array.h:204
size_t get_flat_index(Index const &index) const
get_flat_index returns the flat index into the stored array, given the indices of each category
Definition: custom_index_array.h:309
a set of parameters defined at compile time
Definition: parameter_set.h:205
A class template for compartment populations.
Definition: populations.h:55
Stochastic Metapopulation Model.
Definition: smm/model.h:56
FP evaluate(const AdoptionRate< FP, Status, Region > &rate, const Eigen::VectorX< FP > &x) const
Calculate the current rate of the given adoption.
Definition: smm/model.h:74
Model(Status status_dimensions, Region region_dimensions)
Definition: smm/model.h:62
FP evaluate(const TransitionRate< FP, Status, Region > &rate, const Eigen::VectorX< FP > &x) const
Calculate the current rate of the given spatial transition.
Definition: smm/model.h:103
mio::RandomNumberGenerator & get_rng()
Get the RandomNumberGenerator used by this Model for random events.
Definition: smm/model.h:113
mio::RandomNumberGenerator m_rng
Model's random number generator.
Definition: smm/model.h:119
decltype(concatenate_indices(std::declval< Region >(), std::declval< Status >())) PopulationIndex
The Index type used to define the SMM subpopulations.
Definition: smm/model.h:39
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
Range< MultiIndexIterator< Index< Categories... > > > make_index_range(Index< Categories... > dimensions)
Construct a range that can be used to iterate over all MultiIndices in the given dimensions.
Definition: index_range.h:168
auto i
Definition: io.h:809
decltype(auto) concatenate_indices(IndexArgs &&... args)
Combine several Indexs into one MultiIndex.
Definition: index.h:355
Struct defining a possible status adoption in a Model based on Poisson Processes.
Definition: adoption_rate.h:49
Status from
Definition: adoption_rate.h:50
Region region
Definition: adoption_rate.h:52
std::vector< Influence< FP, Status > > influences
Definition: adoption_rate.h:54
FP factor
Definition: adoption_rate.h:53
CompartmentalModel is a template for a compartmental model for an array of initial populations and a ...
Definition: compartmental_model.h:59
Struct defining a possible regional transition in a Model based on Poisson Processes.
Definition: smm/parameters.h:56
FP factor
Definition: smm/parameters.h:60
Status status
Definition: smm/parameters.h:57
Region from
Definition: smm/parameters.h:58