Simulator.hpp 7.6 KB

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