Derivative.hpp 7.9 KB

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