Integrator.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /**
  2. * @file kernel/qss/Integrator.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-2022 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 QSS_INTEGRATOR
  26. #define QSS_INTEGRATOR
  27. #include <artis-star/kernel/pdevs/Dynamics.hpp>
  28. #include <artis-star/kernel/qss/Data.hpp>
  29. namespace artis::qss {
  30. struct IntegratorParameters {
  31. double x_0;
  32. };
  33. template<typename Time>
  34. class Integrator
  35. : public artis::pdevs::Dynamics<Time, Integrator<Time>, IntegratorParameters> {
  36. public:
  37. struct input {
  38. enum values {
  39. QUANTA, X_DOT, RESET
  40. };
  41. };
  42. struct output {
  43. enum values {
  44. OUT
  45. };
  46. };
  47. struct var {
  48. enum values {
  49. VALUE
  50. };
  51. };
  52. Integrator(const std::string &name,
  53. const artis::pdevs::Context<Time, Integrator<Time>, IntegratorParameters> &context)
  54. :
  55. artis::pdevs::Dynamics<Time, Integrator<Time>, IntegratorParameters>(name,
  56. context) {
  57. DECLARE_STATES(int, ((state::PHASE, &Integrator<Time>::_phase)));
  58. DECLARE_STATES(typename Time::type,
  59. ((state::LAST_OUT_DATE, &Integrator<Time>::_last_output_date)));
  60. DECLARE_STATES(double, ((state::UP_THRESHOLD, &Integrator<Time>::_up_threshold),
  61. (state::DOWN_THRESHOLD, &Integrator<Time>::_down_threshold),
  62. (state::LAST_OUT_VALUE, &Integrator<Time>::_last_output_value),
  63. (state::INIT_VALUE, &Integrator<Time>::_init_value),
  64. (state::CURRENT_VALUE, &Integrator<Time>::_current_value),
  65. (state::EXPECTED_VALUE, &Integrator<Time>::_expected_value)));
  66. DECLARE_STATES(std::vector < double > ,
  67. ((state::ARCHIVE_X_DOT, &Integrator<Time>::_archive_x_dot)));
  68. DECLARE_STATES(std::vector < typename Time::type > ,
  69. ((state::ARCHIVE_DATE, &Integrator<Time>::_archive_date)));
  70. this->input_ports({
  71. {input::QUANTA, "quanta"},
  72. {input::X_DOT, "x_dot"},
  73. {input::RESET, "reset"}});
  74. this->output_port({output::OUT, "out"});
  75. this->observable({var::VALUE, "value"});
  76. _init_value = context.parameters().x_0;
  77. }
  78. virtual ~Integrator() {}
  79. virtual void dconf(typename Time::type t, typename Time::type e,
  80. const common::event::Bag <Time> &bag) {
  81. dint(t);
  82. dext(t, e, bag);
  83. }
  84. virtual void dint(const typename Time::type &time) {
  85. switch (_phase) {
  86. case phase::RUNNING: {
  87. double last_derivative_value = _archive_x_dot.back();
  88. _last_output_value = _expected_value;
  89. _last_output_date = time;
  90. _archive_x_dot.clear();
  91. _archive_date.clear();
  92. _archive_x_dot.push_back(last_derivative_value);
  93. _archive_date.push_back(time);
  94. _current_value = _expected_value;
  95. _phase = phase::WAIT_FOR_QUANTA;
  96. break;
  97. }
  98. case phase::INIT: {
  99. _phase = phase::WAIT_FOR_BOTH;
  100. _last_output_value = _current_value;
  101. _last_output_date = time;
  102. break;
  103. }
  104. default:
  105. assert(false);
  106. }
  107. }
  108. virtual void dext(const typename Time::type &t, const typename Time::type &e,
  109. const common::event::Bag <Time> &bag) {
  110. bool reset = false;
  111. std::for_each(bag.begin(), bag.end(),
  112. [this, t, e, &reset](const common::event::ExternalEvent <Time> &event) {
  113. if (event.on_port(input::QUANTA)) {
  114. QuantifierData data;
  115. event.data()(data);
  116. _up_threshold = data.up;
  117. _down_threshold = data.down;
  118. if (_phase == phase::WAIT_FOR_QUANTA) {
  119. _phase = phase::RUNNING;
  120. }
  121. if (_phase == phase::WAIT_FOR_BOTH) {
  122. _phase = phase::WAIT_FOR_X_DOT;
  123. }
  124. } else if (event.on_port(input::X_DOT)) {
  125. DerivativeData data;
  126. event.data()(data);
  127. _archive_x_dot.push_back(data.x_dot);
  128. _archive_date.push_back(t);
  129. if (_phase == phase::WAIT_FOR_X_DOT) {
  130. _phase = phase::RUNNING;
  131. }
  132. if (_phase == phase::WAIT_FOR_BOTH) {
  133. _phase = phase::WAIT_FOR_QUANTA;
  134. }
  135. } else if (event.on_port(input::RESET)) {
  136. IntegratorData data;
  137. event.data()(data);
  138. _current_value = data.value;
  139. reset = true;
  140. _archive_x_dot.clear();
  141. _archive_date.clear();
  142. }
  143. });
  144. if (reset) {
  145. _phase = phase::INIT;
  146. } else {
  147. if (_phase == phase::RUNNING) {
  148. _current_value = current_value(t);
  149. _expected_value = expected_value(t);
  150. }
  151. }
  152. }
  153. virtual void start(const typename Time::type & /* time */) {
  154. _current_value = _init_value;
  155. _phase = phase::INIT;
  156. }
  157. virtual typename Time::type ta(const typename Time::type & /* time */) {
  158. double current_derivative;
  159. switch (_phase) {
  160. case phase::INIT:
  161. return 0;
  162. case phase::RUNNING:
  163. assert(_archive_date.size() > 0);
  164. current_derivative = _archive_x_dot.back();
  165. if (current_derivative == 0) {
  166. return Time::infinity;
  167. }
  168. if (current_derivative > 0) {
  169. assert(_up_threshold - _current_value >= 0);
  170. return (_up_threshold - _current_value) / current_derivative;
  171. } else {
  172. assert(_down_threshold - _current_value <= 0);
  173. return (_down_threshold - _current_value) / current_derivative;
  174. }
  175. default:
  176. return Time::infinity;
  177. }
  178. }
  179. virtual common::event::Bag <Time> lambda(const typename Time::type & /* time */) const {
  180. common::event::Bag <Time> msgs;
  181. switch (_phase) {
  182. case phase::RUNNING: {
  183. const IntegratorData data = {_expected_value};
  184. msgs.push_back(common::event::ExternalEvent<Time>(output::OUT, data));
  185. break;
  186. }
  187. case phase::INIT: {
  188. const IntegratorData data = {_current_value};
  189. msgs.push_back(common::event::ExternalEvent<Time>(output::OUT, data));
  190. break;
  191. }
  192. default:
  193. break;
  194. }
  195. return msgs;
  196. }
  197. virtual common::event::Value observe(const typename Time::type & /* t */,
  198. unsigned int index) const {
  199. switch (index) {
  200. case var::VALUE:
  201. return (double) (_current_value);
  202. default:
  203. return common::event::Value();
  204. }
  205. }
  206. private:
  207. double current_value(const typename Time::type &time) const {
  208. double val = _last_output_value;
  209. if (_archive_date.size() > 0) {
  210. for (size_t i = 0; i < _archive_date.size() - 1; i++) {
  211. val +=
  212. (_archive_date[i + 1] - _archive_date[i]) * _archive_x_dot[i];
  213. }
  214. val += (time - _archive_date.back()) * _archive_x_dot.back();
  215. }
  216. return val;
  217. }
  218. double expected_value(const typename Time::type & /* time */) const {
  219. double current_derivative = _archive_x_dot.back();
  220. if (current_derivative == 0) {
  221. return _current_value;
  222. } else if (current_derivative > 0) {
  223. return _up_threshold;
  224. }
  225. return _down_threshold;
  226. }
  227. struct phase {
  228. enum values {
  229. INIT,
  230. WAIT_FOR_QUANTA,
  231. WAIT_FOR_X_DOT,
  232. WAIT_FOR_BOTH,
  233. RUNNING
  234. };
  235. };
  236. struct state {
  237. enum values {
  238. PHASE,
  239. LAST_OUT_DATE,
  240. UP_THRESHOLD,
  241. DOWN_THRESHOLD,
  242. LAST_OUT_VALUE,
  243. INIT_VALUE,
  244. CURRENT_VALUE,
  245. EXPECTED_VALUE,
  246. ARCHIVE_X_DOT,
  247. ARCHIVE_DATE
  248. };
  249. };
  250. int _phase;
  251. typename Time::type _last_output_date;
  252. double _up_threshold;
  253. double _down_threshold;
  254. double _last_output_value;
  255. double _init_value;
  256. double _current_value;
  257. double _expected_value;
  258. std::vector<double> _archive_x_dot;
  259. std::vector<typename Time::type> _archive_date;
  260. };
  261. }
  262. #endif