Integrator.hpp 12 KB

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