Simulator.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @file artis/kernel/Simulator.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2012-2019 ULCO http://www.univ-littoral.fr
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef ARTIS_KERNEL_SIMULATOR_HPP
  22. #define ARTIS_KERNEL_SIMULATOR_HPP
  23. #include <artis/kernel/AbstractCoupledModel.hpp>
  24. #include <artis/context/Context.hpp>
  25. #include <artis/observer/Observer.hpp>
  26. #include <artis/observer/View.hpp>
  27. #include <artis/utils/DoubleTime.hpp>
  28. namespace artis {
  29. namespace kernel {
  30. template<typename T, typename U, typename V, typename W>
  31. class Simulator {
  32. public:
  33. Simulator(artis::kernel::AbstractCoupledModel<T, U, V, W>* model,
  34. W& parameters)
  35. :_model(model), _observer(model) { _model->build(parameters); }
  36. Simulator(artis::kernel::AbstractCoupledModel<T, U, V, W>* model,
  37. W& parameters, const std::string& json)
  38. :_model(model), _observer(model) { _model->build(parameters, json); }
  39. virtual ~Simulator() { delete _model; }
  40. void attachView(const std::string& name, observer::View<U, V>* view) { _observer.attachView(name, view); }
  41. void init(double time, const V& parameters)
  42. {
  43. _model->init(time, parameters);
  44. _observer.init();
  45. #ifdef WITH_TRACE
  46. _model->trace_model(true,
  47. utils::DoubleTime::null,
  48. utils::INIT);
  49. #endif
  50. }
  51. const observer::Observer<U, V>& observer() const { return _observer; }
  52. void run(const context::Context<U>& context)
  53. {
  54. if (context.valid()) {
  55. _model->restore(context.state());
  56. }
  57. for (double t = context.begin(); t <= context.end(); t++) {
  58. (*_model)(t);
  59. _observer.observe(t);
  60. }
  61. }
  62. void save(context::Context<U>& context) const
  63. {
  64. _model->save(context.state());
  65. context.saved();
  66. }
  67. private:
  68. artis::kernel::AbstractCoupledModel<T, U, V, W>* _model;
  69. observer::Observer<U, V> _observer;
  70. };
  71. }
  72. }
  73. #endif