Simulator.hpp 6.1 KB

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