Simulator.hpp 7.1 KB

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