Simulator.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * @file kernel/dtss/Simulator.hpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2022 ULCO http://www.univ-littoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #ifndef DTSS_SIMULATOR
  26. #define DTSS_SIMULATOR
  27. #include <artis-star/common/Coordinator.hpp>
  28. #include <artis-star/common/Parameters.hpp>
  29. #include <artis-star/common/Simulator.hpp>
  30. #include <artis-star/common/utils/Trace.hpp>
  31. #include <cassert>
  32. namespace artis::dtss {
  33. template<typename Time, typename Dynamics, typename Parameters>
  34. class Simulator;
  35. template<typename Time, typename Dynamics, typename Parameters = common::NoParameters>
  36. class Context {
  37. typedef dtss::Simulator<Time, Dynamics, Parameters> Simulator;
  38. public:
  39. Context(const Parameters &parameters, Simulator *simulator)
  40. :
  41. _parameters(parameters), _simulator(simulator) {}
  42. virtual ~Context() {}
  43. const Parameters &parameters() const { return _parameters; }
  44. Simulator *simulator() const { return _simulator; }
  45. private:
  46. const Parameters &_parameters;
  47. Simulator *_simulator;
  48. };
  49. template<typename Time, typename Dynamics, typename Parameters = common::NoParameters>
  50. class Simulator : public common::Simulator<Time> {
  51. typedef Simulator<Time, Dynamics, Parameters> type;
  52. public:
  53. Simulator(const std::string &name, const Parameters &parameters)
  54. :
  55. common::Model<Time>(name),
  56. common::Simulator<Time>(name),
  57. _dynamics(name, Context<Time, Dynamics, Parameters>(parameters, this)),
  58. _time_step(parameters.time_step) {}
  59. ~Simulator() {}
  60. virtual void restore(const common::context::State<Time> &state) {
  61. _dynamics.restore(state);
  62. }
  63. virtual void save(common::context::State<Time> &state) const {
  64. _dynamics.save(state);
  65. }
  66. virtual void finish(const typename Time::type &t) {
  67. #ifndef WITH_TRACE
  68. (void) t;
  69. #endif
  70. #ifdef WITH_TRACE
  71. common::Trace<Time>::trace()
  72. << common::TraceElement<Time>(type::get_name(), t,
  73. common::FormalismType::DTSS,
  74. common::FunctionType::FINISH,
  75. common::LevelType::FORMALISM);
  76. common::Trace<Time>::trace().flush();
  77. #endif
  78. }
  79. typename Time::type start(const typename Time::type &t) {
  80. #ifdef WITH_TRACE
  81. common::Trace<Time>::trace()
  82. << common::TraceElement<Time>(type::get_name(), t,
  83. common::FormalismType::DTSS,
  84. common::FunctionType::I_MESSAGE,
  85. common::LevelType::FORMALISM)
  86. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  87. << type::_tn;
  88. common::Trace<Time>::trace().flush();
  89. #endif
  90. _dynamics.start(t);
  91. type::_tl = t;
  92. type::_tn = t;
  93. #ifdef WITH_TRACE
  94. common::Trace<Time>::trace()
  95. << common::TraceElement<Time>(type::get_name(), t,
  96. common::FormalismType::DTSS,
  97. common::FunctionType::I_MESSAGE,
  98. common::LevelType::FORMALISM)
  99. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  100. << type::_tn;
  101. common::Trace<Time>::trace().flush();
  102. #endif
  103. return type::_tn;
  104. }
  105. typename Time::type lookahead(const typename Time::type &t) const {
  106. return _dynamics.lookahead(t);
  107. }
  108. common::event::Value observe(const typename Time::type &t,
  109. unsigned int index) const { return _dynamics.observe(t, index); }
  110. virtual std::string observable_name(unsigned int observable_index) const {
  111. return _dynamics.observable_name(observable_index);
  112. }
  113. void output(const typename Time::type &t) {
  114. #ifdef WITH_TRACE
  115. common::Trace<Time>::trace()
  116. << common::TraceElement<Time>(type::get_name(), t,
  117. common::FormalismType::DTSS,
  118. common::FunctionType::LAMBDA,
  119. common::LevelType::FORMALISM)
  120. << ": BEFORE";
  121. common::Trace<Time>::trace().flush();
  122. #endif
  123. if (t == type::_tn) {
  124. common::event::Bag<Time> bag = _dynamics.lambda(t);
  125. if (not bag.empty()) {
  126. for (auto &event: bag) {
  127. event.set_model(this);
  128. }
  129. dynamic_cast < common::Coordinator<Time> * >(
  130. type::get_parent())->dispatch_events(bag, t);
  131. }
  132. }
  133. #ifdef WITH_TRACE
  134. common::Trace<Time>::trace()
  135. << common::TraceElement<Time>(type::get_name(), t,
  136. common::FormalismType::DTSS,
  137. common::FunctionType::LAMBDA,
  138. common::LevelType::FORMALISM)
  139. << ": AFTER";
  140. common::Trace<Time>::trace().flush();
  141. #endif
  142. }
  143. void post_event(const typename Time::type &t,
  144. const common::event::ExternalEvent<Time> &event) {
  145. #ifndef WITH_TRACE
  146. (void) t;
  147. #endif
  148. #ifdef WITH_TRACE
  149. common::Trace<Time>::trace()
  150. << common::TraceElement<Time>(type::get_name(), t,
  151. common::FormalismType::DTSS,
  152. common::FunctionType::POST_EVENT,
  153. common::LevelType::FORMALISM)
  154. << ": BEFORE => " << event.to_string();
  155. common::Trace<Time>::trace().flush();
  156. #endif
  157. type::add_event(event);
  158. #ifdef WITH_TRACE
  159. common::Trace<Time>::trace()
  160. << common::TraceElement<Time>(type::get_name(), t,
  161. common::FormalismType::DTSS,
  162. common::FunctionType::POST_EVENT,
  163. common::LevelType::FORMALISM)
  164. << ": AFTER => " << event.to_string();
  165. common::Trace<Time>::trace().flush();
  166. #endif
  167. }
  168. typename Time::type transition(const typename Time::type &t) {
  169. #ifdef WITH_TRACE
  170. common::Trace<Time>::trace()
  171. << common::TraceElement<Time>(type::get_name(), t,
  172. common::FormalismType::DTSS,
  173. common::FunctionType::S_MESSAGE,
  174. common::LevelType::FORMALISM)
  175. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  176. << type::_tn;
  177. common::Trace<Time>::trace().flush();
  178. #endif
  179. assert(t == type::_tn);
  180. _dynamics.transition(type::get_bag(), t);
  181. type::_tl = t;
  182. type::_tn = t + _time_step;
  183. type::clear_bag();
  184. #ifdef WITH_TRACE
  185. common::Trace<Time>::trace()
  186. << common::TraceElement<Time>(type::get_name(), t,
  187. common::FormalismType::DTSS,
  188. common::FunctionType::S_MESSAGE,
  189. common::LevelType::FORMALISM)
  190. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  191. << type::_tn;
  192. common::Trace<Time>::trace().flush();
  193. #endif
  194. return type::_tn;
  195. }
  196. const typename Time::type &time_step() const { return _time_step; }
  197. private :
  198. Dynamics _dynamics;
  199. typename Time::type _time_step;
  200. };
  201. } // namespace artis dtss
  202. #endif