Integrator.hpp 9.8 KB

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