Simulator.hpp 5.9 KB

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