Integrator.hpp 8.2 KB

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