Derivative.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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, WAIT, RESPONSE
  36. } State;
  37. public:
  38. enum inputs {
  39. IN
  40. };
  41. enum outputs {
  42. OUT
  43. };
  44. enum states {
  45. STATE, 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. {
  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_port({IN, "in"});
  60. this->output_port({OUT, "out"});
  61. }
  62. virtual ~Derivative() { }
  63. virtual double compute() const = 0;
  64. virtual void dconf(typename Time::type t, typename Time::type e,
  65. const common::Bag<Time>& bag)
  66. {
  67. dint(t);
  68. dext(t, e, bag);
  69. }
  70. virtual void dint(typename Time::type /* time */)
  71. {
  72. if (_state == RESPONSE) {
  73. _last_output = _output_value;
  74. }
  75. _state = WAIT;
  76. }
  77. virtual void dext(typename Time::type t, typename Time::type e,
  78. const common::Bag<Time>& bag)
  79. {
  80. std::for_each(bag.begin(), bag.end(),
  81. [this, t, e](const common::ExternalEvent<Time>& event) {
  82. IntegratorData data;
  83. event.data()(data);
  84. this->get(event.port_index() + 4).put(dynamic_cast<Dyn*>(this),
  85. data.value);
  86. switch (_state) {
  87. case INIT:
  88. if (_input_number == this->state_number() - 4) {
  89. _output_value = compute();
  90. _state = RESPONSE;
  91. }
  92. break;
  93. case WAIT:
  94. case RESPONSE:
  95. double value = compute();
  96. if (value != _last_output) {
  97. _output_value = value;
  98. _state = RESPONSE;
  99. } else {
  100. _state = WAIT;
  101. }
  102. }
  103. });
  104. }
  105. virtual typename Time::type
  106. start(typename Time::type /* time */)
  107. {
  108. _input_number = this->input_port_number();
  109. if (_input_number == 0) {
  110. _output_value = compute();
  111. _state = RESPONSE;
  112. return 0;
  113. } else {
  114. _state = INIT;
  115. return Time::infinity;
  116. }
  117. }
  118. virtual typename Time::type ta(typename Time::type /* time */)
  119. {
  120. switch (_state) {
  121. case INIT:
  122. return Time::infinity;
  123. case WAIT:
  124. return Time::infinity;
  125. case RESPONSE:
  126. return 0;
  127. }
  128. return Time::infinity;
  129. }
  130. virtual common::Bag<Time> lambda(typename Time::type /* time */) const
  131. {
  132. common::Bag<Time> msgs;
  133. switch (_state) {
  134. case INIT:
  135. break;
  136. case WAIT:
  137. break;
  138. case RESPONSE:
  139. const DerivativeData data = {_output_value};
  140. msgs.push_back(common::ExternalEvent<Time>(OUT, data));
  141. }
  142. return msgs;
  143. }
  144. virtual common::Value observe(const typename Time::type& /* t */,
  145. unsigned int /* index */) const
  146. {
  147. return common::Value();
  148. }
  149. private:
  150. int _state;
  151. unsigned int _input_number;
  152. double _output_value;
  153. double _last_output;
  154. };
  155. }
  156. }
  157. }
  158. #endif