Derivative.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * @file kernel/qss/Derivative.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_DERIVATIVE
  26. #define QSS_DERIVATIVE
  27. #include <artis-star/kernel/pdevs/Dynamics.hpp>
  28. #include <artis-star/kernel/qss/Data.hpp>
  29. namespace artis {
  30. namespace qss {
  31. template<class Time, class Dyn, class Parameters = common::NoParameters>
  32. class Derivative : public artis::pdevs::Dynamics<Time, Dyn, Parameters> {
  33. typedef enum {
  34. INIT = 0, WAIT, RESPONSE
  35. } State;
  36. public:
  37. enum inputs {
  38. RESET = 0, IN
  39. };
  40. enum outputs {
  41. OUT = 0
  42. };
  43. enum states {
  44. STATE = 0, INPUT_NUMBER, OUTPUT_VALUE, LAST_OUTPUT
  45. };
  46. typedef Derivative<Time, Dyn, Parameters> type;
  47. Derivative(const std::string& name, const artis::pdevs::Context<Time, Dyn, Parameters>& context)
  48. :
  49. artis::pdevs::Dynamics<Time, Dyn, Parameters>(name, context),
  50. _external_number(0), _internal_number(0)
  51. {
  52. DECLARE_STATES(int,
  53. ((STATE, &type::_state)));
  54. DECLARE_STATES(unsigned int,
  55. ((INPUT_NUMBER, &type::_input_number)));
  56. DECLARE_STATES(double,
  57. ((OUTPUT_VALUE, &type::_output_value),
  58. (LAST_OUTPUT, &type::_output_value)));
  59. this->input_ports({{RESET, "reset"},
  60. {IN, "in"}});
  61. this->output_port({OUT, "out"});
  62. }
  63. virtual ~Derivative() { }
  64. int external(const std::string& name, double Dyn::* var)
  65. {
  66. ++_external_number;
  67. this->state_(LAST_OUTPUT + _external_number + 1, name, var);
  68. this->input_port({IN + _external_number, name});
  69. return IN + _external_number;
  70. }
  71. void internal(const std::string& name, double Dyn::* var)
  72. {
  73. assert(_internal_number == 0);
  74. ++_internal_number;
  75. this->state_(LAST_OUTPUT + 1, name, var);
  76. }
  77. virtual double compute() const = 0;
  78. virtual void dconf(const typename Time::type& t, typename Time::type e,
  79. const common::Bag<Time>& bag)
  80. {
  81. dint(t);
  82. dext(t, e, bag);
  83. }
  84. virtual void dint(const typename Time::type& /* time */)
  85. {
  86. if (_state == RESPONSE) {
  87. _last_output = _output_value;
  88. }
  89. _state = WAIT;
  90. }
  91. virtual void dext(const typename Time::type& t, typename Time::type e,
  92. const common::Bag<Time>& bag)
  93. {
  94. std::for_each(bag.begin(), bag.end(),
  95. [this, t, e](const common::ExternalEvent<Time>& event) {
  96. if (event.on_port(RESET)) {
  97. if (_input_number == 0) {
  98. _output_value = compute();
  99. _state = RESPONSE;
  100. } else {
  101. _state = INIT;
  102. }
  103. } else {
  104. IntegratorData data;
  105. event.data()(data);
  106. this->get((event.port_index() - 1) + LAST_OUTPUT + 1).put(
  107. dynamic_cast<Dyn*>(this),
  108. data.value);
  109. switch (_state) {
  110. case INIT:
  111. if (_input_number
  112. == this->state_number() - (LAST_OUTPUT + 1)) {
  113. _output_value = compute();
  114. _state = RESPONSE;
  115. }
  116. break;
  117. case WAIT:
  118. case RESPONSE:
  119. double value = compute();
  120. if (value != _last_output) {
  121. _output_value = value;
  122. _state = RESPONSE;
  123. } else {
  124. _state = WAIT;
  125. }
  126. }
  127. }
  128. });
  129. }
  130. virtual void start(const typename Time::type& /* time */)
  131. {
  132. _input_number = this->input_port_number() - 1;
  133. if (_input_number == 0) {
  134. _output_value = compute();
  135. _state = RESPONSE;
  136. } else {
  137. _state = INIT;
  138. }
  139. }
  140. virtual typename Time::type ta(const typename Time::type& /* time */)
  141. {
  142. switch (_state) {
  143. case INIT:
  144. return Time::infinity;
  145. case WAIT:
  146. return Time::infinity;
  147. case RESPONSE:
  148. return 0;
  149. }
  150. return Time::infinity;
  151. }
  152. virtual common::Bag<Time> lambda(const typename Time::type& /* time */) const
  153. {
  154. common::Bag<Time> msgs;
  155. switch (_state) {
  156. case INIT:
  157. break;
  158. case WAIT:
  159. break;
  160. case RESPONSE:
  161. const DerivativeData data = {_output_value};
  162. msgs.push_back(common::ExternalEvent<Time>(OUT, data));
  163. }
  164. return msgs;
  165. }
  166. virtual common::Value observe(const typename Time::type& /* t */,
  167. unsigned int /* index */) const
  168. {
  169. return common::Value();
  170. }
  171. private:
  172. unsigned int _external_number;
  173. unsigned int _internal_number;
  174. // state
  175. int _state;
  176. unsigned int _input_number;
  177. double _output_value;
  178. double _last_output;
  179. };
  180. }
  181. }
  182. #endif