Simulator.hpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. utils::Trace < utils::DoubleTime >::trace()
  50. << utils::TraceElement < utils::DoubleTime >("KERNEL", 0,
  51. utils::INIT)
  52. << "GLOBAL VALUES - "
  53. << _model->path(_model);
  54. _model->trace_model(0);
  55. utils::Trace < utils::DoubleTime >::trace().flush();
  56. #endif
  57. }
  58. const observer::Observer < U, V >& observer() const
  59. { return _observer; }
  60. void run(const context::Context < U >& context)
  61. {
  62. if (context.valid()) {
  63. _model->restore(context.state());
  64. }
  65. for (double t = context.begin(); t <= context.end(); t++) {
  66. (*_model)(t);
  67. _observer.observe(t);
  68. }
  69. }
  70. void save(context::Context < U >& context) const
  71. {
  72. _model->save(context.state());
  73. context.saved();
  74. }
  75. private:
  76. artis::kernel::AbstractCoupledModel < T, U, V, W >* _model;
  77. observer::Observer < U, V > _observer;
  78. };
  79. } }
  80. #endif