Derivative.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. IN = 0
  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_port({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(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(typename Time::type /* time */)
  85. {
  86. if (_state == RESPONSE) {
  87. _last_output = _output_value;
  88. }
  89. _state = WAIT;
  90. }
  91. virtual void dext(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. IntegratorData data;
  97. event.data()(data);
  98. this->get(event.port_index() + LAST_OUTPUT + 1).put(
  99. dynamic_cast<Dyn*>(this),
  100. data.value);
  101. switch (_state) {
  102. case INIT:
  103. if (_input_number == this->state_number() - (LAST_OUTPUT + 1)) {
  104. _output_value = compute();
  105. _state = RESPONSE;
  106. }
  107. break;
  108. case WAIT:
  109. case RESPONSE:
  110. double value = compute();
  111. if (value != _last_output) {
  112. _output_value = value;
  113. _state = RESPONSE;
  114. } else {
  115. _state = WAIT;
  116. }
  117. }
  118. });
  119. }
  120. virtual void start(typename Time::type /* time */)
  121. {
  122. _input_number = this->input_port_number();
  123. if (_input_number == 0) {
  124. _output_value = compute();
  125. _state = RESPONSE;
  126. } else {
  127. _state = INIT;
  128. }
  129. }
  130. virtual typename Time::type ta(typename Time::type /* time */)
  131. {
  132. switch (_state) {
  133. case INIT:
  134. return Time::infinity;
  135. case WAIT:
  136. return Time::infinity;
  137. case RESPONSE:
  138. return 0;
  139. }
  140. return Time::infinity;
  141. }
  142. virtual common::Bag<Time> lambda(typename Time::type /* time */) const
  143. {
  144. common::Bag<Time> msgs;
  145. switch (_state) {
  146. case INIT:
  147. break;
  148. case WAIT:
  149. break;
  150. case RESPONSE:
  151. const DerivativeData data = {_output_value};
  152. msgs.push_back(common::ExternalEvent<Time>(OUT, data));
  153. }
  154. return msgs;
  155. }
  156. virtual common::Value observe(const typename Time::type& /* t */,
  157. unsigned int /* index */) const
  158. {
  159. return common::Value();
  160. }
  161. private:
  162. unsigned int _external_number;
  163. unsigned int _internal_number;
  164. // state
  165. int _state;
  166. unsigned int _input_number;
  167. double _output_value;
  168. double _last_output;
  169. };
  170. }
  171. }
  172. }
  173. #endif