Simulator.hpp 2.6 KB

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