Line data Source code
1 : /* 2 : * Copyright (C) 2020-2024 MEmilio 3 : * 4 : * Authors: Daniel Abele, Khoa Nguyen 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_ABM_SIMULATION_H 21 : #define MIO_ABM_SIMULATION_H 22 : 23 : #include "abm/model.h" 24 : #include "abm/time.h" 25 : #include "memilio/io/history.h" 26 : 27 : namespace mio 28 : { 29 : namespace abm 30 : { 31 : 32 : /** 33 : * @brief Run the Simulation in discrete steps, evolve the Model and report results. 34 : */ 35 : class Simulation 36 : { 37 : 38 : public: 39 : /** 40 : * @brief Create a simulation. 41 : * @param[in] t0 The starting time of the Simulation. 42 : * @param[in] model The Model to simulate. 43 : */ 44 : Simulation(TimePoint t0, Model&& model); 45 : 46 : /** 47 : * @brief Create a Simulation with an empty Model. 48 : * Model needs to be filled later. 49 : * @see Simulation::get_model 50 : * @param[in] t0 The starting time of the Simulation. 51 : */ 52 : Simulation(TimePoint t0, size_t num_agegroups) 53 : : Simulation(t0, Model(num_agegroups)) 54 : { 55 : } 56 : 57 : /** 58 : * @brief Run the Simulation from the current time to tmax. 59 : * @param[in] tmax Time to stop. 60 : * @param[in] history History object to log data of the Simulation. 61 : */ 62 : template <typename... History> 63 18 : void advance(TimePoint tmax, History&... history) 64 : { 65 : //log initial system state 66 18 : (history.log(*this), ...); 67 684 : while (m_t < tmax) { 68 666 : evolve_model(tmax); 69 666 : (history.log(*this), ...); 70 : } 71 18 : } 72 : 73 : /** 74 : * @brief Get the current time of the Simulation. 75 : */ 76 3393 : TimePoint get_time() const 77 : { 78 3393 : return m_t; 79 : } 80 : 81 : /** 82 : * @brief Get the Model that this Simulation evolves. 83 : */ 84 9 : Model& get_model() 85 : { 86 9 : return m_model; 87 : } 88 28404 : const Model& get_model() const 89 : { 90 28404 : return m_model; 91 : } 92 : 93 : private: 94 : void store_result_at(TimePoint t); 95 : void evolve_model(TimePoint tmax); 96 : 97 : Model m_model; ///< The Model to simulate. 98 : TimePoint m_t; ///< The current TimePoint of the Simulation. 99 : TimeSpan m_dt; ///< The length of the time steps. 100 : }; 101 : 102 : } // namespace abm 103 : } // namespace mio 104 : 105 : #endif