Simulator.hpp 6.1 KB

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