LCOV - code coverage report
Current view: top level - models/abm - simulation.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 22 22 100.0 %
Date: 2025-02-17 13:46:44 Functions: 12 15 80.0 %

          Line data    Source code
       1             : /* 
       2             : * Copyright (C) 2020-2025 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             : template <class M = Model>
      36             : class Simulation
      37             : {
      38             : 
      39             : public:
      40             :     /**
      41             :      * @brief Create a simulation.
      42             :      * @param[in] t0 The starting time of the Simulation.
      43             :      * @param[in] model The Model to simulate.
      44             :      */
      45          33 :     Simulation(TimePoint t0, M&& model)
      46          33 :         : m_model(std::move(model))
      47          33 :         , m_t(t0)
      48          33 :         , m_dt(hours(1))
      49             :     {
      50          33 :     }
      51             : 
      52             :     /**
      53             :      * @brief Create a Simulation with an empty Model.
      54             :      * Model needs to be filled later.
      55             :      * @see Simulation::get_model
      56             :      * @param[in] t0 The starting time of the Simulation.
      57             :      */
      58             :     Simulation(TimePoint t0, size_t num_agegroups)
      59             :         : Simulation(t0, M(num_agegroups))
      60             :     {
      61             :     }
      62             : 
      63             :     /** 
      64             :      * @brief Run the Simulation from the current time to tmax.
      65             :      * @param[in] tmax Time to stop.
      66             :      * @param[in] history History object to log data of the Simulation.
      67             :      */
      68             :     template <typename... History>
      69          42 :     void advance(TimePoint tmax, History&... history)
      70             :     {
      71             :         //log initial system state
      72          42 :         (history.log(*this), ...);
      73         994 :         while (m_t < tmax) {
      74         952 :             evolve_model(tmax);
      75         952 :             (history.log(*this), ...);
      76             :         }
      77          42 :     }
      78             : 
      79             :     /**
      80             :      * @brief Get the current time of the Simulation.
      81             :      */
      82        3394 :     TimePoint get_time() const
      83             :     {
      84        3394 :         return m_t;
      85             :     }
      86             : 
      87             :     /**
      88             :      * @brief Get the Model that this Simulation evolves.
      89             :      */
      90          70 :     M& get_model()
      91             :     {
      92          70 :         return m_model;
      93             :     }
      94       28404 :     const M& get_model() const
      95             :     {
      96       28404 :         return m_model;
      97             :     }
      98             : 
      99             : private:
     100             :     void store_result_at(TimePoint t);
     101         952 :     void evolve_model(TimePoint tmax)
     102             :     {
     103         952 :         auto dt = std::min(m_dt, tmax - m_t);
     104         952 :         m_model.evolve(m_t, dt);
     105         952 :         m_t += m_dt;
     106         952 :     }
     107             : 
     108             :     M m_model; ///< The Model to simulate.
     109             :     TimePoint m_t; ///< The current TimePoint of the Simulation.
     110             :     TimeSpan m_dt; ///< The length of the time steps.
     111             : };
     112             : 
     113             : } // namespace abm
     114             : } // namespace mio
     115             : 
     116             : #endif

Generated by: LCOV version 1.14