Integrator.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. {
  33. double x_0;
  34. };
  35. template<class Time>
  36. class Integrator
  37. : public artis::pdevs::Dynamics<Time, Integrator<Time>, IntegratorParameters>
  38. {
  39. public:
  40. struct input
  41. {
  42. enum values
  43. {
  44. QUANTA, X_DOT, RESET
  45. };
  46. };
  47. struct output
  48. {
  49. enum values
  50. {
  51. OUT
  52. };
  53. };
  54. struct var
  55. {
  56. enum values
  57. {
  58. VALUE
  59. };
  60. };
  61. Integrator(const std::string &name,
  62. const artis::pdevs::Context<Time, Integrator<Time>, IntegratorParameters> &context)
  63. :
  64. artis::pdevs::Dynamics<Time, Integrator<Time>, IntegratorParameters>(name,
  65. context)
  66. {
  67. DECLARE_STATES(int, ((state::PHASE, &Integrator<Time>::_phase)));
  68. DECLARE_STATES(typename Time::type,
  69. ((state::LAST_OUT_DATE, &Integrator<Time>::_last_output_date)));
  70. DECLARE_STATES(double, ((state::UP_THRESHOLD, &Integrator<Time>::_up_threshold),
  71. (state::DOWN_THRESHOLD, &Integrator<Time>::_down_threshold),
  72. (state::LAST_OUT_VALUE, &Integrator<Time>::_last_output_value),
  73. (state::INIT_VALUE, &Integrator<Time>::_init_value),
  74. (state::CURRENT_VALUE, &Integrator<Time>::_current_value),
  75. (state::EXPECTED_VALUE, &Integrator<Time>::_expected_value)));
  76. DECLARE_STATES(std::vector<double>,
  77. ((state::ARCHIVE_X_DOT, &Integrator<Time>::_archive_x_dot)));
  78. DECLARE_STATES(std::vector<typename Time::type>,
  79. ((state::ARCHIVE_DATE, &Integrator<Time>::_archive_date)));
  80. this->input_ports({
  81. {input::QUANTA, "quanta"},
  82. {input::X_DOT, "x_dot"},
  83. {input::RESET, "reset"}});
  84. this->output_port({output::OUT, "out"});
  85. this->observable({var::VALUE, "value"});
  86. _init_value = context.parameters().x_0;
  87. }
  88. virtual ~Integrator()
  89. {}
  90. virtual void dconf(typename Time::type t, typename Time::type e,
  91. const common::Bag<Time> &bag)
  92. {
  93. dint(t);
  94. dext(t, e, bag);
  95. }
  96. virtual void dint(const typename Time::type &time)
  97. {
  98. switch (_phase) {
  99. case phase::RUNNING: {
  100. double last_derivative_value = _archive_x_dot.back();
  101. _last_output_value = _expected_value;
  102. _last_output_date = time;
  103. _archive_x_dot.clear();
  104. _archive_date.clear();
  105. _archive_x_dot.push_back(last_derivative_value);
  106. _archive_date.push_back(time);
  107. _current_value = _expected_value;
  108. _phase = phase::WAIT_FOR_QUANTA;
  109. break;
  110. }
  111. case phase::INIT: {
  112. _phase = phase::WAIT_FOR_BOTH;
  113. _last_output_value = _current_value;
  114. _last_output_date = time;
  115. break;
  116. }
  117. default:assert(false);
  118. }
  119. }
  120. virtual void dext(const typename Time::type &t, const typename Time::type &e,
  121. const common::Bag<Time> &bag)
  122. {
  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(input::QUANTA)) {
  127. QuantifierData data;
  128. event.data()(data);
  129. _up_threshold = data.up;
  130. _down_threshold = data.down;
  131. if (_phase == phase::WAIT_FOR_QUANTA) {
  132. _phase = phase::RUNNING;
  133. }
  134. if (_phase == phase::WAIT_FOR_BOTH) {
  135. _phase = phase::WAIT_FOR_X_DOT;
  136. }
  137. } else if (event.on_port(input::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 (_phase == phase::WAIT_FOR_X_DOT) {
  143. _phase = phase::RUNNING;
  144. }
  145. if (_phase == phase::WAIT_FOR_BOTH) {
  146. _phase = phase::WAIT_FOR_QUANTA;
  147. }
  148. } else if (event.on_port(input::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. _phase = phase::INIT;
  159. } else {
  160. if (_phase == phase::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. {
  168. _current_value = _init_value;
  169. _phase = phase::INIT;
  170. }
  171. virtual typename Time::type ta(const typename Time::type & /* time */)
  172. {
  173. double current_derivative;
  174. switch (_phase) {
  175. case phase::INIT:return 0;
  176. case phase::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:return Time::infinity;
  190. }
  191. }
  192. virtual common::Bag<Time> lambda(const typename Time::type & /* time */) const
  193. {
  194. common::Bag<Time> msgs;
  195. switch (_phase) {
  196. case phase::RUNNING: {
  197. const IntegratorData data = {_expected_value};
  198. msgs.push_back(common::ExternalEvent<Time>(output::OUT, data));
  199. break;
  200. }
  201. case phase::INIT: {
  202. const IntegratorData data = {_current_value};
  203. msgs.push_back(common::ExternalEvent<Time>(output::OUT, data));
  204. break;
  205. }
  206. default:break;
  207. }
  208. return msgs;
  209. }
  210. virtual common::Value observe(const typename Time::type & /* t */,
  211. unsigned int index) const
  212. {
  213. switch (index) {
  214. case var::VALUE:return (double) (_current_value);
  215. default:return common::Value();
  216. }
  217. }
  218. private:
  219. double current_value(const typename Time::type &time) const
  220. {
  221. double val = _last_output_value;
  222. if (_archive_date.size() > 0) {
  223. for (size_t i = 0; i < _archive_date.size() - 1; i++) {
  224. val +=
  225. (_archive_date[i + 1] - _archive_date[i]) * _archive_x_dot[i];
  226. }
  227. val += (time - _archive_date.back()) * _archive_x_dot.back();
  228. }
  229. return val;
  230. }
  231. double expected_value(const typename Time::type & /* time */) const
  232. {
  233. double current_derivative = _archive_x_dot.back();
  234. if (current_derivative == 0) {
  235. return _current_value;
  236. } else if (current_derivative > 0) {
  237. return _up_threshold;
  238. }
  239. return _down_threshold;
  240. }
  241. struct phase
  242. {
  243. enum values
  244. {
  245. INIT,
  246. WAIT_FOR_QUANTA,
  247. WAIT_FOR_X_DOT,
  248. WAIT_FOR_BOTH,
  249. RUNNING
  250. };
  251. };
  252. struct state
  253. {
  254. enum values
  255. {
  256. PHASE,
  257. LAST_OUT_DATE,
  258. UP_THRESHOLD,
  259. DOWN_THRESHOLD,
  260. LAST_OUT_VALUE,
  261. INIT_VALUE,
  262. CURRENT_VALUE,
  263. EXPECTED_VALUE,
  264. ARCHIVE_X_DOT,
  265. ARCHIVE_DATE
  266. };
  267. };
  268. int _phase;
  269. typename Time::type _last_output_date;
  270. double _up_threshold;
  271. double _down_threshold;
  272. double _last_output_value;
  273. double _init_value;
  274. double _current_value;
  275. double _expected_value;
  276. std::vector<double> _archive_x_dot;
  277. std::vector<typename Time::type> _archive_date;
  278. };
  279. }
  280. }
  281. #endif