Simulator.hpp 2.9 KB

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