Derivative.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. public:
  35. enum inputs {
  36. IN = 1
  37. };
  38. enum outputs {
  39. OUT = 1
  40. };
  41. Derivative(const std::string& name, const Context<Time, Dyn, Parameters>& context)
  42. :
  43. artis::pdevs::Dynamics<Time, Dyn, Parameters>(name, context)
  44. {
  45. this->input_port({IN, "in"});
  46. this->output_port({OUT, "out"});
  47. }
  48. virtual ~Derivative() { }
  49. virtual double compute() const = 0;
  50. virtual void dconf(typename Time::type t, typename Time::type e,
  51. const common::Bag<Time>& bag)
  52. {
  53. dint(t);
  54. dext(t, e, bag);
  55. }
  56. virtual void dint(typename Time::type /* time */)
  57. {
  58. if (_state == RESPONSE) {
  59. _last_output = _output_value;
  60. }
  61. _state = WAIT;
  62. }
  63. virtual void dext(typename Time::type t, typename Time::type e,
  64. const common::Bag<Time>& bag)
  65. {
  66. std::for_each(bag.begin(), bag.end(),
  67. [this, t, e](const common::ExternalEvent<Time>& event) {
  68. IntegratorData data;
  69. event.data()(data);
  70. _inputs[event.port_index()] = data.value;
  71. switch (_state) {
  72. case INIT:
  73. if (_inputs.size() == _input_number) {
  74. _output_value = compute();
  75. _state = RESPONSE;
  76. }
  77. break;
  78. case WAIT:
  79. case RESPONSE:
  80. double value = compute();
  81. if (value != _last_output) {
  82. _output_value = value;
  83. _state = RESPONSE;
  84. } else {
  85. _state = WAIT;
  86. }
  87. }
  88. });
  89. }
  90. virtual typename Time::type
  91. start(typename Time::type /* time */)
  92. {
  93. _input_number = this->input_port_number();
  94. if (_input_number == 0) {
  95. _output_value = compute();
  96. _state = RESPONSE;
  97. return 0;
  98. } else {
  99. _state = INIT;
  100. return Time::infinity;
  101. }
  102. }
  103. virtual typename Time::type ta(typename Time::type /* time */)
  104. {
  105. switch (_state) {
  106. case INIT:
  107. return Time::infinity;
  108. case WAIT:
  109. return Time::infinity;
  110. case RESPONSE:
  111. return 0;
  112. }
  113. return Time::infinity;
  114. }
  115. virtual common::Bag<Time> lambda(typename Time::type /* time */) const
  116. {
  117. common::Bag<Time> msgs;
  118. switch (_state) {
  119. case INIT:
  120. break;
  121. case WAIT:
  122. break;
  123. case RESPONSE:
  124. const DerivativeData data = {_output_value};
  125. msgs.push_back(common::ExternalEvent<Time>(OUT, data));
  126. }
  127. return msgs;
  128. }
  129. virtual common::Value observe(const typename Time::type& /* t */,
  130. unsigned int /* index */) const
  131. {
  132. return common::Value();
  133. }
  134. double input(unsigned int index) const {
  135. return _inputs.at(index);
  136. }
  137. private:
  138. typedef enum {
  139. INIT, WAIT, RESPONSE
  140. } State;
  141. State _state;
  142. unsigned int _input_number;
  143. std::map<unsigned int, double> _inputs;
  144. double _output_value;
  145. double _last_output;
  146. };
  147. }
  148. }
  149. }
  150. #endif