Simulator.hpp 9.2 KB

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