Integrator.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 = 1, X_DOT
  48. };
  49. enum outputs {
  50. OUT = 1
  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 >, ((ARCHIVE_X_DOT, &Integrator<Time>::_archive_x_dot)));
  83. DECLARE_STATES(std::vector <typename Time::type >, ((ARCHIVE_DATE, &Integrator<Time>::_archive_date)));
  84. this->input_ports({
  85. {QUANTA, "quanta"},
  86. {X_DOT, "x_dot"}});
  87. this->output_port({OUT, "out"});
  88. this->observable({VALUE, "value"});
  89. _init_value = context.parameters().x_0;
  90. }
  91. virtual ~Integrator() { }
  92. virtual void dconf(typename Time::type t, typename Time::type e,
  93. const common::Bag<Time>& bag)
  94. {
  95. dint(t);
  96. dext(t, e, bag);
  97. }
  98. virtual void dint(typename Time::type time)
  99. {
  100. switch (_state) {
  101. case RUNNING: {
  102. double last_derivative_value = _archive_x_dot.back();
  103. _last_output_value = _expected_value;
  104. _last_output_date = time;
  105. _archive_x_dot.clear();
  106. _archive_date.clear();
  107. _archive_x_dot.push_back(last_derivative_value);
  108. _archive_date.push_back(time);
  109. _current_value = _expected_value;
  110. _state = WAIT_FOR_QUANTA;
  111. break;
  112. }
  113. case INIT: {
  114. _state = WAIT_FOR_BOTH;
  115. _last_output_value = _current_value;
  116. _last_output_date = time;
  117. break;
  118. }
  119. default:
  120. assert(false);
  121. }
  122. }
  123. virtual void dext(typename Time::type t, typename Time::type e,
  124. const common::Bag<Time>& bag)
  125. {
  126. std::for_each(bag.begin(), bag.end(),
  127. [this, t, e](const common::ExternalEvent<Time>& event) {
  128. if (event.on_port(QUANTA)) {
  129. QuantifierData data;
  130. event.data()(data);
  131. _up_threshold = data.up;
  132. _down_threshold = data.down;
  133. if (_state == WAIT_FOR_QUANTA) {
  134. _state = RUNNING;
  135. }
  136. if (_state == WAIT_FOR_BOTH) {
  137. _state = WAIT_FOR_X_DOT;
  138. }
  139. }
  140. if (event.on_port(X_DOT)) {
  141. DerivativeData data;
  142. event.data()(data);
  143. _archive_x_dot.push_back(data.x_dot);
  144. _archive_date.push_back(t);
  145. if (_state == WAIT_FOR_X_DOT) {
  146. _state = RUNNING;
  147. }
  148. if (_state == WAIT_FOR_BOTH) {
  149. _state = WAIT_FOR_QUANTA;
  150. }
  151. }
  152. });
  153. if (_state == RUNNING) {
  154. _current_value = current_value(t);
  155. _expected_value = expected_value(t);
  156. }
  157. }
  158. virtual typename Time::type start(typename Time::type /* time */)
  159. {
  160. _current_value = _init_value;
  161. _state = INIT;
  162. return 0;
  163. }
  164. virtual typename Time::type ta(typename Time::type /* time */)
  165. {
  166. double current_derivative;
  167. switch (_state) {
  168. case RUNNING:
  169. assert(_archive_date.size() > 0);
  170. current_derivative = _archive_x_dot.back();
  171. if (current_derivative == 0) {
  172. return Time::infinity;
  173. }
  174. if (current_derivative > 0) {
  175. assert(_up_threshold - _current_value >= 0);
  176. return (_up_threshold - _current_value) / current_derivative;
  177. } else {
  178. assert(_down_threshold - _current_value <= 0);
  179. return (_down_threshold - _current_value) / current_derivative;
  180. }
  181. default:
  182. return Time::infinity;
  183. }
  184. }
  185. virtual common::Bag<Time> lambda(typename Time::type /* time */) const
  186. {
  187. common::Bag<Time> msgs;
  188. switch (_state) {
  189. case RUNNING: {
  190. const IntegratorData data = {_expected_value};
  191. msgs.push_back(common::ExternalEvent<Time>(OUT, data));
  192. break;
  193. }
  194. case INIT: {
  195. const IntegratorData data = {_current_value};
  196. msgs.push_back(common::ExternalEvent<Time>(OUT, data));
  197. break;
  198. }
  199. default:
  200. break;
  201. }
  202. return msgs;
  203. }
  204. virtual common::Value observe(const typename Time::type& /* t */,
  205. unsigned int index) const
  206. {
  207. switch (index) {
  208. case VALUE:
  209. return (double) (_current_value);
  210. default:
  211. return common::Value();
  212. }
  213. }
  214. private:
  215. double current_value(const typename Time::type& time) const
  216. {
  217. double val = _last_output_value;
  218. if (_archive_date.size() > 0) {
  219. for (size_t i = 0; i < _archive_date.size() - 1; i++) {
  220. val +=
  221. (_archive_date[i + 1] - _archive_date[i]) * _archive_x_dot[i];
  222. }
  223. val += (time - _archive_date.back()) * _archive_x_dot.back();
  224. }
  225. return val;
  226. }
  227. double expected_value(const typename Time::type& /* time */) const
  228. {
  229. double current_derivative = _archive_x_dot.back();
  230. if (current_derivative == 0) {
  231. return _current_value;
  232. } else if (current_derivative > 0) {
  233. return _up_threshold;
  234. }
  235. return _down_threshold;
  236. }
  237. int _state;
  238. typename Time::type _last_output_date;
  239. double _up_threshold;
  240. double _down_threshold;
  241. double _last_output_value;
  242. double _init_value;
  243. double _current_value;
  244. double _expected_value;
  245. std::vector<double> _archive_x_dot;
  246. std::vector<typename Time::type> _archive_date;
  247. };
  248. }
  249. }
  250. }
  251. #endif