Derivative.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. DECLARE_STATES(int,
  52. ((STATE, &type::_state)));
  53. DECLARE_STATES(unsigned int,
  54. ((INPUT_NUMBER, &type::_input_number)));
  55. DECLARE_STATES(double,
  56. ((OUTPUT_VALUE, &type::_output_value),
  57. (LAST_OUTPUT, &type::_output_value)));
  58. this->input_ports({{RESET, "reset"},
  59. {IN, "in"}});
  60. this->output_port({OUT, "out"});
  61. }
  62. virtual ~Derivative() {}
  63. int external(const std::string &name, double Dyn::* var) {
  64. ++_external_number;
  65. this->state_(LAST_OUTPUT + _external_number + 1, name, var);
  66. this->input_port({IN + _external_number, name});
  67. return IN + _external_number;
  68. }
  69. void internal(const std::string &name, double Dyn::* var) {
  70. assert(_internal_number == 0);
  71. ++_internal_number;
  72. this->state_(LAST_OUTPUT + 1, name, var);
  73. }
  74. virtual double compute() const = 0;
  75. virtual void dconf(const typename Time::type &t, typename Time::type e,
  76. const common::Bag<Time> &bag) {
  77. dint(t);
  78. dext(t, e, bag);
  79. }
  80. virtual void dint(const typename Time::type & /* time */) {
  81. if (_state == RESPONSE) {
  82. _last_output = _output_value;
  83. }
  84. _state = WAIT;
  85. }
  86. virtual void dext(const typename Time::type &t, typename Time::type e,
  87. const common::Bag<Time> &bag) {
  88. std::for_each(bag.begin(), bag.end(),
  89. [this, t, e](const common::ExternalEvent<Time> &event) {
  90. if (event.on_port(RESET)) {
  91. if (_input_number == 0) {
  92. _output_value = compute();
  93. _state = RESPONSE;
  94. } else {
  95. _state = INIT;
  96. }
  97. } else {
  98. IntegratorData data;
  99. event.data()(data);
  100. this->get((event.port_index() - 1) + LAST_OUTPUT + 1).put(
  101. dynamic_cast<Dyn *>(this),
  102. data.value);
  103. switch (_state) {
  104. case INIT:
  105. if (_input_number
  106. == this->state_number() - (LAST_OUTPUT + 1)) {
  107. _output_value = compute();
  108. _state = RESPONSE;
  109. }
  110. break;
  111. case WAIT:
  112. case RESPONSE:double value = compute();
  113. if (value != _last_output) {
  114. _output_value = value;
  115. _state = RESPONSE;
  116. } else {
  117. _state = WAIT;
  118. }
  119. }
  120. }
  121. });
  122. }
  123. virtual void start(const typename Time::type & /* time */) {
  124. _input_number = this->input_port_number() - 1;
  125. if (_input_number == 0) {
  126. _output_value = compute();
  127. _state = RESPONSE;
  128. } else {
  129. _state = INIT;
  130. }
  131. }
  132. virtual typename Time::type ta(const typename Time::type & /* time */) {
  133. switch (_state) {
  134. case INIT:return Time::infinity;
  135. case WAIT:return Time::infinity;
  136. case RESPONSE:return 0;
  137. }
  138. return Time::infinity;
  139. }
  140. virtual common::Bag<Time> lambda(const typename Time::type & /* time */) const {
  141. common::Bag<Time> msgs;
  142. switch (_state) {
  143. case INIT:break;
  144. case WAIT:break;
  145. case RESPONSE:const DerivativeData data = {_output_value};
  146. msgs.push_back(common::ExternalEvent<Time>(OUT, data));
  147. }
  148. return msgs;
  149. }
  150. virtual common::Value observe(const typename Time::type & /* t */,
  151. unsigned int /* index */) const {
  152. return common::Value();
  153. }
  154. private:
  155. unsigned int _external_number;
  156. unsigned int _internal_number;
  157. // state
  158. int _state;
  159. unsigned int _input_number;
  160. double _output_value;
  161. double _last_output;
  162. };
  163. }
  164. }
  165. #endif