Derivative.hpp 5.9 KB

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