Derivative.hpp 6.0 KB

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