Simulator.hpp 6.7 KB

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