Simulator.hpp 5.4 KB

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