Simulator.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * @file kernel/sss/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 SSS_SIMULATOR
  26. #define SSS_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 <artis-star/kernel/sss/Model.hpp>
  32. #include <cassert>
  33. namespace artis {
  34. namespace sss {
  35. template<class Time, class Dynamics,
  36. class Parameters = common::NoParameters>
  37. class Simulator : public common::Simulator<Time>,
  38. public sss::Model<Time>
  39. {
  40. typedef Simulator<Time, Dynamics, Parameters> type;
  41. public:
  42. Simulator(const std::string &name, const typename Time::type &time_step,
  43. const Parameters &parameters)
  44. :
  45. common::Model<Time>(name),
  46. common::Simulator<Time>(name),
  47. sss::Model<Time>(name),
  48. _dynamics(name, parameters),
  49. _time_step(time_step)
  50. {}
  51. ~Simulator()
  52. {}
  53. virtual bool is_atomic() const
  54. { return common::Simulator<Time>::is_atomic(); }
  55. virtual void restore(const common::context::State<Time> &state)
  56. {
  57. common::Simulator<Time>::restore(state);
  58. _dynamics.restore(state);
  59. }
  60. virtual void save(context::State <Time> &state) const
  61. {
  62. common::Simulator<Time>::save(state);
  63. _dynamics.save(state);
  64. }
  65. virtual std::string to_string(int level) const
  66. { return common::Simulator<Time>::to_string(level); }
  67. virtual void finish(const typename Time::type &t)
  68. {
  69. #ifndef WITH_TRACE
  70. (void) t;
  71. #endif
  72. #ifdef WITH_TRACE
  73. common::Trace<Time>::trace()
  74. << common::TraceElement<Time>(type::get_name(), t,
  75. common::FormalismType::SSS,
  76. common::FunctionType::FINISH,
  77. common::LevelType::FORMALISM);
  78. common::Trace<Time>::trace().flush();
  79. #endif
  80. }
  81. typename Time::type start(const typename Time::type &t)
  82. {
  83. #ifdef WITH_TRACE
  84. common::Trace<Time>::trace()
  85. << common::TraceElement<Time>(type::get_name(), t,
  86. common::FunctionType::I_MESSAGE)
  87. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  88. << type::_tn;
  89. common::Trace<Time>::trace().flush();
  90. #endif
  91. _dynamics.start(t);
  92. type::_tl = t;
  93. type::_tn = t;
  94. #ifdef WITH_TRACE
  95. common::Trace<Time>::trace()
  96. << common::TraceElement<Time>(type::get_name(), t,
  97. common::FunctionType::I_MESSAGE)
  98. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  99. << type::_tn;
  100. common::Trace<Time>::trace().flush();
  101. #endif
  102. return type::_tn;
  103. }
  104. void observation(std::ostream &file) const
  105. {
  106. _dynamics.observation(file);
  107. }
  108. void output(const typename Time::type &t)
  109. {
  110. #ifdef WITH_TRACE
  111. common::Trace<Time>::trace()
  112. << common::TraceElement<Time>(type::get_name(), t,
  113. common::FunctionType::OUTPUT) << ": BEFORE";
  114. common::Trace<Time>::trace().flush();
  115. #endif
  116. if (t == type::_tn) {
  117. common::Bag<Time> bag = _dynamics.lambda(t);
  118. if (not bag.empty()) {
  119. for (auto &event : bag) {
  120. event.set_model(this);
  121. }
  122. dynamic_cast < common::Coordinator<Time> * >(
  123. type::get_parent())->dispatch_events(bag, t);
  124. }
  125. }
  126. #ifdef WITH_TRACE
  127. common::Trace<Time>::trace()
  128. << common::TraceElement<Time>(type::get_name(), t,
  129. common::FunctionType::OUTPUT) << ": AFTER";
  130. common::Trace<Time>::trace().flush();
  131. #endif
  132. }
  133. void post_event(const typename Time::type &t,
  134. const common::ExternalEvent<Time> &event)
  135. {
  136. #ifndef WITH_TRACE
  137. (void) t;
  138. #endif
  139. #ifdef WITH_TRACE
  140. common::Trace<Time>::trace()
  141. << common::TraceElement<Time>(type::get_name(), t,
  142. common::FunctionType::POST_EVENT)
  143. << ": BEFORE => " << event.to_string();
  144. common::Trace<Time>::trace().flush();
  145. #endif
  146. type::add_event(event);
  147. #ifdef WITH_TRACE
  148. common::Trace<Time>::trace()
  149. << common::TraceElement<Time>(type::get_name(), t,
  150. common::FunctionType::POST_EVENT)
  151. << ": AFTER => " << event.to_string();
  152. common::Trace<Time>::trace().flush();
  153. #endif
  154. }
  155. typename Time::type transition(const typename Time::type &t)
  156. {
  157. #ifdef WITH_TRACE
  158. common::Trace<Time>::trace()
  159. << common::TraceElement<Time>(type::get_name(), t,
  160. common::FunctionType::S_MESSAGE)
  161. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  162. << type::_tn;
  163. common::Trace<Time>::trace().flush();
  164. #endif
  165. assert(t == type::_tn);
  166. if (type::is_marked()) {
  167. if (type::is_send()) {
  168. type::_tl = t;
  169. type::_tn = t + _time_step;
  170. }
  171. } else {
  172. _dynamics.transition(type::get_bag(), t);
  173. type::clear_bag();
  174. }
  175. #ifdef WITH_TRACE
  176. common::Trace<Time>::trace()
  177. << common::TraceElement<Time>(type::get_name(), t,
  178. common::FunctionType::S_MESSAGE)
  179. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  180. << type::_tn;
  181. common::Trace<Time>::trace().flush();
  182. #endif
  183. return type::_tn;
  184. }
  185. virtual void update_buffer(typename Time::type time)
  186. { _dynamics.update_buffer(time); }
  187. private :
  188. Dynamics _dynamics;
  189. typename Time::type _time_step;
  190. };
  191. }
  192. } // namespace artis sss
  193. #endif