Simulator.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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-2019 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 {
  33. namespace dtss {
  34. template<class Time, class Dynamics, class Parameters>
  35. class Simulator;
  36. template<class Time, class Dynamics,
  37. class Parameters = common::NoParameters>
  38. class Context
  39. {
  40. typedef dtss::Simulator<Time, Dynamics, Parameters> Simulator;
  41. public:
  42. Context(const Parameters &parameters, Simulator *simulator)
  43. :
  44. _parameters(parameters), _simulator(simulator)
  45. {}
  46. virtual ~Context()
  47. {}
  48. const Parameters &parameters() const
  49. { return _parameters; }
  50. Simulator *simulator() const
  51. { return _simulator; }
  52. private:
  53. const Parameters &_parameters;
  54. Simulator *_simulator;
  55. };
  56. template<class Time, class Dynamics,
  57. class Parameters = common::NoParameters>
  58. class Simulator : public common::Simulator<Time>
  59. {
  60. typedef Simulator<Time, Dynamics, Parameters> type;
  61. public:
  62. Simulator(const std::string &name, const Parameters &parameters)
  63. :
  64. common::Model<Time>(name),
  65. common::Simulator<Time>(name),
  66. _dynamics(name, Context<Time, Dynamics, Parameters>(parameters, this)),
  67. _time_step(parameters.time_step)
  68. {}
  69. ~Simulator()
  70. {}
  71. virtual void restore(const common::context::State<Time> &state)
  72. {
  73. _dynamics.restore(state);
  74. }
  75. virtual void save(common::context::State<Time> &state) const
  76. {
  77. _dynamics.save(state);
  78. }
  79. virtual void finish(const typename Time::type &t)
  80. {
  81. #ifndef WITH_TRACE
  82. (void) t;
  83. #endif
  84. #ifdef WITH_TRACE
  85. common::Trace<Time>::trace()
  86. << common::TraceElement<Time>(type::get_name(), t,
  87. common::FormalismType::DTSS,
  88. common::FunctionType::FINISH,
  89. common::LevelType::FORMALISM);
  90. common::Trace<Time>::trace().flush();
  91. #endif
  92. }
  93. typename Time::type start(const typename Time::type &t)
  94. {
  95. #ifdef WITH_TRACE
  96. common::Trace<Time>::trace()
  97. << common::TraceElement<Time>(type::get_name(), t,
  98. common::FormalismType::DTSS,
  99. common::FunctionType::I_MESSAGE,
  100. common::LevelType::FORMALISM)
  101. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  102. << type::_tn;
  103. common::Trace<Time>::trace().flush();
  104. #endif
  105. _dynamics.start(t);
  106. type::_tl = t;
  107. type::_tn = t;
  108. #ifdef WITH_TRACE
  109. common::Trace<Time>::trace()
  110. << common::TraceElement<Time>(type::get_name(), t,
  111. common::FormalismType::DTSS,
  112. common::FunctionType::I_MESSAGE,
  113. common::LevelType::FORMALISM)
  114. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  115. << type::_tn;
  116. common::Trace<Time>::trace().flush();
  117. #endif
  118. return type::_tn;
  119. }
  120. typename Time::type lookahead(const typename Time::type &t) const
  121. {
  122. return _dynamics.lookahead(t);
  123. }
  124. common::Value observe(const typename Time::type &t,
  125. unsigned int index) const
  126. { return _dynamics.observe(t, index); }
  127. virtual std::string observable_name(unsigned int observable_index) const
  128. {
  129. return _dynamics.observable_name(observable_index);
  130. }
  131. void output(const typename Time::type &t)
  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. << ": BEFORE";
  140. common::Trace<Time>::trace().flush();
  141. #endif
  142. if (t == type::_tn) {
  143. common::Bag<Time> bag = _dynamics.lambda(t);
  144. if (not bag.empty()) {
  145. for (auto &event : bag) {
  146. event.set_model(this);
  147. }
  148. dynamic_cast < common::Coordinator<Time> * >(
  149. type::get_parent())->dispatch_events(bag, t);
  150. }
  151. }
  152. #ifdef WITH_TRACE
  153. common::Trace<Time>::trace()
  154. << common::TraceElement<Time>(type::get_name(), t,
  155. common::FormalismType::DTSS,
  156. common::FunctionType::LAMBDA,
  157. common::LevelType::FORMALISM)
  158. << ": AFTER";
  159. common::Trace<Time>::trace().flush();
  160. #endif
  161. }
  162. void post_event(const typename Time::type &t,
  163. const common::ExternalEvent<Time> &event)
  164. {
  165. #ifndef WITH_TRACE
  166. (void) t;
  167. #endif
  168. #ifdef WITH_TRACE
  169. common::Trace<Time>::trace()
  170. << common::TraceElement<Time>(type::get_name(), t,
  171. common::FormalismType::DTSS,
  172. common::FunctionType::POST_EVENT,
  173. common::LevelType::FORMALISM)
  174. << ": BEFORE => " << event.to_string();
  175. common::Trace<Time>::trace().flush();
  176. #endif
  177. type::add_event(event);
  178. #ifdef WITH_TRACE
  179. common::Trace<Time>::trace()
  180. << common::TraceElement<Time>(type::get_name(), t,
  181. common::FormalismType::DTSS,
  182. common::FunctionType::POST_EVENT,
  183. common::LevelType::FORMALISM)
  184. << ": AFTER => " << event.to_string();
  185. common::Trace<Time>::trace().flush();
  186. #endif
  187. }
  188. typename Time::type transition(const typename Time::type &t)
  189. {
  190. #ifdef WITH_TRACE
  191. common::Trace<Time>::trace()
  192. << common::TraceElement<Time>(type::get_name(), t,
  193. common::FormalismType::DTSS,
  194. common::FunctionType::S_MESSAGE,
  195. common::LevelType::FORMALISM)
  196. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  197. << type::_tn;
  198. common::Trace<Time>::trace().flush();
  199. #endif
  200. assert(t == type::_tn);
  201. _dynamics.transition(type::get_bag(), t);
  202. type::_tl = t;
  203. type::_tn = t + _time_step;
  204. type::clear_bag();
  205. #ifdef WITH_TRACE
  206. common::Trace<Time>::trace()
  207. << common::TraceElement<Time>(type::get_name(), t,
  208. common::FormalismType::DTSS,
  209. common::FunctionType::S_MESSAGE,
  210. common::LevelType::FORMALISM)
  211. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  212. << type::_tn;
  213. common::Trace<Time>::trace().flush();
  214. #endif
  215. return type::_tn;
  216. }
  217. const typename Time::type &time_step() const
  218. { return _time_step; }
  219. private :
  220. Dynamics _dynamics;
  221. typename Time::type _time_step;
  222. };
  223. }
  224. } // namespace artis dtss
  225. #endif