Simulator.hpp 6.6 KB

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