Integrator.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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(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(typename Time::type t, typename Time::type e,
  127. const common::Bag<Time>& bag)
  128. {
  129. std::for_each(bag.begin(), bag.end(),
  130. [this, t, e](const common::ExternalEvent<Time>& event) {
  131. if (event.on_port(QUANTA)) {
  132. QuantifierData data;
  133. event.data()(data);
  134. _up_threshold = data.up;
  135. _down_threshold = data.down;
  136. if (_state == WAIT_FOR_QUANTA) {
  137. _state = RUNNING;
  138. }
  139. if (_state == WAIT_FOR_BOTH) {
  140. _state = WAIT_FOR_X_DOT;
  141. }
  142. } else if (event.on_port(X_DOT)) {
  143. DerivativeData data;
  144. event.data()(data);
  145. _archive_x_dot.push_back(data.x_dot);
  146. _archive_date.push_back(t);
  147. if (_state == WAIT_FOR_X_DOT) {
  148. _state = RUNNING;
  149. }
  150. if (_state == WAIT_FOR_BOTH) {
  151. _state = WAIT_FOR_QUANTA;
  152. }
  153. } else if (event.on_port(RESET)) {
  154. IntegratorData data;
  155. event.data()(data);
  156. _current_value = data.value;
  157. _state = INIT;
  158. }
  159. });
  160. if (_state == RUNNING) {
  161. _current_value = current_value(t);
  162. _expected_value = expected_value(t);
  163. }
  164. }
  165. virtual void start(typename Time::type /* time */)
  166. {
  167. _current_value = _init_value;
  168. _state = INIT;
  169. }
  170. virtual typename Time::type ta(typename Time::type /* time */)
  171. {
  172. double current_derivative;
  173. switch (_state) {
  174. case INIT:
  175. return 0;
  176. case RUNNING:
  177. assert(_archive_date.size() > 0);
  178. current_derivative = _archive_x_dot.back();
  179. if (current_derivative == 0) {
  180. return Time::infinity;
  181. }
  182. if (current_derivative > 0) {
  183. assert(_up_threshold - _current_value >= 0);
  184. return (_up_threshold - _current_value) / current_derivative;
  185. } else {
  186. assert(_down_threshold - _current_value <= 0);
  187. return (_down_threshold - _current_value) / current_derivative;
  188. }
  189. default:
  190. return Time::infinity;
  191. }
  192. }
  193. virtual common::Bag<Time> lambda(typename Time::type /* time */) const
  194. {
  195. common::Bag<Time> msgs;
  196. switch (_state) {
  197. case RUNNING: {
  198. const IntegratorData data = {_expected_value};
  199. msgs.push_back(common::ExternalEvent<Time>(OUT, data));
  200. break;
  201. }
  202. case INIT: {
  203. const IntegratorData data = {_current_value};
  204. msgs.push_back(common::ExternalEvent<Time>(OUT, data));
  205. break;
  206. }
  207. default:
  208. break;
  209. }
  210. return msgs;
  211. }
  212. virtual common::Value observe(const typename Time::type& /* t */,
  213. unsigned int index) const
  214. {
  215. switch (index) {
  216. case VALUE:
  217. return (double) (_current_value);
  218. default:
  219. return common::Value();
  220. }
  221. }
  222. private:
  223. double current_value(const typename Time::type& time) const
  224. {
  225. double val = _last_output_value;
  226. if (_archive_date.size() > 0) {
  227. for (size_t i = 0; i < _archive_date.size() - 1; i++) {
  228. val +=
  229. (_archive_date[i + 1] - _archive_date[i]) * _archive_x_dot[i];
  230. }
  231. val += (time - _archive_date.back()) * _archive_x_dot.back();
  232. }
  233. return val;
  234. }
  235. double expected_value(const typename Time::type& /* time */) const
  236. {
  237. double current_derivative = _archive_x_dot.back();
  238. if (current_derivative == 0) {
  239. return _current_value;
  240. } else if (current_derivative > 0) {
  241. return _up_threshold;
  242. }
  243. return _down_threshold;
  244. }
  245. int _state;
  246. typename Time::type _last_output_date;
  247. double _up_threshold;
  248. double _down_threshold;
  249. double _last_output_value;
  250. double _init_value;
  251. double _current_value;
  252. double _expected_value;
  253. std::vector<double> _archive_x_dot;
  254. std::vector<typename Time::type> _archive_date;
  255. };
  256. }
  257. }
  258. }
  259. #endif