RootCoordinator.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @file RootCoordinator.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 COMMON_ROOT_COORDINATOR
  26. #define COMMON_ROOT_COORDINATOR
  27. #include <artis-star/common/context/Context.hpp>
  28. #include <artis-star/common/observer/Observer.hpp>
  29. #include <artis-star/common/observer/View.hpp>
  30. #include <artis-star/common/Parameters.hpp>
  31. #include <sstream>
  32. #include <string>
  33. namespace artis {
  34. namespace common {
  35. template<class Time, class Coordinator>
  36. class RootCoordinator {
  37. public :
  38. RootCoordinator(const common::context::Context<Time>& context,
  39. const std::string& root_name,
  40. const typename Coordinator::parameters_type& parameters,
  41. const typename Coordinator::graph_parameters_type& graph_parameters)
  42. :
  43. _root(root_name, parameters, graph_parameters), _observer(&_root),
  44. _t_max(context.end()), _tn(context.begin()) { }
  45. RootCoordinator(const common::context::Context<Time>& context,
  46. const std::string& root_name,
  47. const typename Coordinator::parameters_type& parameters)
  48. :
  49. _root(root_name, parameters, NoParameters()), _observer(&_root),
  50. _t_max(context.end()), _tn(context.begin()) { }
  51. RootCoordinator(const common::context::Context<Time>& context,
  52. const std::string& root_name)
  53. :
  54. _root(root_name, NoParameters(), NoParameters()), _observer(&_root),
  55. _t_max(context.end()), _tn(context.begin()) { }
  56. virtual ~RootCoordinator() { }
  57. void attachView(const std::string& name, observer::View<Time>* view)
  58. {
  59. _observer.attachView(name, view);
  60. }
  61. const observer::Observer<Time>& observer() const { return _observer; }
  62. void run(const common::context::Context<Time>& context)
  63. {
  64. // DSDE synchroniser
  65. // When receive (START,t)
  66. // send (START,0) to child
  67. // while (t N,child ≠ ∞) do
  68. // "Computes the output of all the models"
  69. // send (OUTPUT,t N,child ) to child
  70. // "Makes the transition"
  71. // send (TRANSITION,t N,child ,φ) to child
  72. // endWhile
  73. // end
  74. _observer.init();
  75. if (context.valid()) {
  76. _root.restore(context.state());
  77. _tn = _root.get_tn();
  78. } else {
  79. _tn = _root.start(_tn);
  80. }
  81. while (_tn <= _t_max) {
  82. typename Time::type next_tn;
  83. _root.output(_tn);
  84. next_tn = _root.transition(_tn);
  85. _observer.observe(_tn, _t_max < next_tn ? _t_max : next_tn);
  86. _tn = next_tn;
  87. }
  88. }
  89. void save(common::context::Context<Time>& context) const
  90. {
  91. _root.save(context.state());
  92. context.saved();
  93. }
  94. void switch_to_timed_observer(double step) { _observer.switch_to_timed_observer(step); }
  95. std::string to_string() const
  96. {
  97. std::ostringstream ss;
  98. ss << _root.to_string(0);
  99. return ss.str();
  100. }
  101. private :
  102. Coordinator _root;
  103. observer::Observer<Time> _observer;
  104. typename Time::type _t_max;
  105. typename Time::type _tn;
  106. };
  107. }
  108. } // namespace artis common
  109. #endif