Simulator.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * @file artis/kernel/Simulator.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2012-2017 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 { namespace kernel {
  29. template < typename T, typename U, typename V, typename W >
  30. class Simulator
  31. {
  32. public:
  33. Simulator(artis::kernel::AbstractCoupledModel < T, U, V, W >* model,
  34. W& parameters) : _model(model), _observer(model)
  35. { _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)
  39. { _model->build(parameters, json); }
  40. virtual ~Simulator()
  41. { delete _model; }
  42. void attachView(const std::string& name, observer::View < U, V >* view)
  43. { _observer.attachView(name, view); }
  44. void init(double time, const V& parameters)
  45. {
  46. _model->init(time, parameters);
  47. _observer.init();
  48. #ifdef WITH_TRACE
  49. _model->trace_element(utils::DoubleTime::null,
  50. utils::INIT);
  51. _model->trace_model(utils::DoubleTime::null,
  52. utils::INIT);
  53. #endif
  54. }
  55. const observer::Observer < U, V >& observer() const
  56. { return _observer; }
  57. void run(const context::Context < U >& context)
  58. {
  59. if (context.valid()) {
  60. _model->restore(context.state());
  61. }
  62. for (double t = context.begin(); t <= context.end(); t++) {
  63. (*_model)(t);
  64. _observer.observe(t);
  65. }
  66. }
  67. void save(context::Context < U >& context) const
  68. {
  69. _model->save(context.state());
  70. context.saved();
  71. }
  72. private:
  73. artis::kernel::AbstractCoupledModel < T, U, V, W >* _model;
  74. observer::Observer < U, V > _observer;
  75. };
  76. } }
  77. #endif