temporal_hybrid_model.h Source File

CPP API: temporal_hybrid_model.h Source File
temporal_hybrid_model.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Julia Bicker, René 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 
21 #ifndef MIO_TEMPORAL_HYBRID_MODEL_H
22 #define MIO_TEMPORAL_HYBRID_MODEL_H
23 
24 #include <algorithm>
25 #include <functional>
26 #include <type_traits>
27 
28 #include "memilio/config.h"
29 
30 namespace mio
31 {
32 namespace hybrid
33 {
34 
42 template <class CurrentModel, class TargetModel>
43 void convert_model(const CurrentModel&, TargetModel&) = delete;
44 
54 template <class Model1, class Model2, class ResultType1, class ResultType2>
56 {
57 
58 public:
59  //Functions returning the result/current state of both models
60  using result1_function = std::function<ResultType1(const Model1&, ScalarType t)>;
61  using result2_function = std::function<ResultType2(const Model2&, ScalarType t)>;
62 
63  //Should return true when the simulation should be continued with the model that is not used currently i.e. a switch needs to be applied
65  std::function<bool(const ResultType1& state_model1, const ResultType2& state_model2, bool model1_used)>;
66 
77  TemporalHybridSimulation(Model1&& model1, Model2&& model2, const result1_function& result1,
78  const result2_function& result2, bool initially_use_model1, ScalarType t0 = 0,
79  ScalarType dt = 0.1)
80  : m_model1(std::move(model1))
81  , m_model2(std::move(model2))
82  , m_result1(result1)
83  , m_result2(result2)
84  , m_using_model1(initially_use_model1)
85  , m_t(t0)
86  , m_dt(dt)
87  {
88  }
89 
95  void advance(ScalarType tmax, const switching_condition& switch_model)
96  {
97  while (m_t < tmax) {
98  bool condition = switch_model(get_result_model1(), get_result_model2(), m_using_model1);
99  if (m_using_model1 &&
100  condition) { //currently model1 is used, but the condition to switch to model2 is fulfilled
102  m_using_model1 = false;
103  }
104  else if (
105  !m_using_model1 &&
106  condition) { //currently model2 is used, but the condition to switch is fulfilled i.e. we need to switch to model1
108  m_using_model1 = true;
109  }
110  //else{Switching condition is not fulfilled and currently used model is just advanced}
111  ScalarType next_step = std::min(m_dt, tmax - m_t);
112  if (m_using_model1) {
113  m_model1.advance(m_t + next_step);
114  }
115  else {
116  m_model2.advance(m_t + next_step);
117  }
118  m_t += next_step;
119  }
120  }
121 
126  ResultType1 get_result_model1() const
127  {
128  return m_result1(m_model1, m_t);
129  }
130 
135  ResultType2 get_result_model2() const
136  {
137  return m_result2(m_model2, m_t);
138  }
139 
143  const auto& get_model1() const
144  {
145  return m_model1;
146  }
147  auto& get_model1()
148  {
149  return m_model1;
150  }
151 
155  const auto& get_model2() const
156  {
157  return m_model2;
158  }
159  auto& get_model2()
160  {
161  return m_model2;
162  }
163 
167  const auto& using_model1() const
168  {
169  return m_using_model1;
170  }
171  auto& using_model1()
172  {
173  return m_using_model1;
174  }
175 
176 private:
177  Model1 m_model1;
178  Model2 m_model2;
184 };
185 
186 } //namespace hybrid
187 
188 } //namespace mio
189 
190 #endif //MIO_TEMPORAL_HYBRID_MODEL_H
A temporal-hybrid simulation.
Definition: temporal_hybrid_model.h:56
bool m_using_model1
Boolean specifying whether model 1 is currently used for simulation.
Definition: temporal_hybrid_model.h:181
TemporalHybridSimulation(Model1 &&model1, Model2 &&model2, const result1_function &result1, const result2_function &result2, bool initially_use_model1, ScalarType t0=0, ScalarType dt=0.1)
Create a temporal-hybrid simulation.
Definition: temporal_hybrid_model.h:77
Model1 m_model1
First model used for the simulation.
Definition: temporal_hybrid_model.h:177
auto & get_model1()
Definition: temporal_hybrid_model.h:147
ResultType1 get_result_model1() const
Get the result of model 1.
Definition: temporal_hybrid_model.h:126
const auto & get_model1() const
Returns first model used for the simulation.
Definition: temporal_hybrid_model.h:143
std::function< ResultType1(const Model1 &, ScalarType t)> result1_function
Definition: temporal_hybrid_model.h:60
const auto & using_model1() const
Returns whether the first model is currently used for simulation.
Definition: temporal_hybrid_model.h:167
ResultType2 get_result_model2() const
Get the result of model 2.
Definition: temporal_hybrid_model.h:135
std::function< ResultType2(const Model2 &, ScalarType t)> result2_function
Definition: temporal_hybrid_model.h:61
ScalarType m_dt
Step size with which the switching condition is checked.
Definition: temporal_hybrid_model.h:183
auto & get_model2()
Definition: temporal_hybrid_model.h:159
void advance(ScalarType tmax, const switching_condition &switch_model)
Advance simulation to tmax.
Definition: temporal_hybrid_model.h:95
ScalarType m_t
Current time step.
Definition: temporal_hybrid_model.h:182
result2_function m_result2
Result function of second model.
Definition: temporal_hybrid_model.h:180
const auto & get_model2() const
Returns second model used for the simulation.
Definition: temporal_hybrid_model.h:155
std::function< bool(const ResultType1 &state_model1, const ResultType2 &state_model2, bool model1_used)> switching_condition
Definition: temporal_hybrid_model.h:65
result1_function m_result1
Result function of first model.
Definition: temporal_hybrid_model.h:179
Model2 m_model2
Second model used for the simulation.
Definition: temporal_hybrid_model.h:178
auto & using_model1()
Definition: temporal_hybrid_model.h:171
double ScalarType
Configuration of memilio library.
Definition: memilio/config.h:30
static min_max_return_type< ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 >, ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > >::type min(const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &a, const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &b)
Definition: ad.hpp:2599
void convert_model(const dabm::Simulation< SingleWell< mio::osecir::InfectionState >> &current_model, smm::Simulation< ScalarType, mio::osecir::InfectionState > &target_model)
Definition: conversion_functions.cpp:38
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
Definition: io.h:94