MultiDerivative.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. * @file kernel/qss/MultiDerivative.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_MULTI_DERIVATIVE
  26. #define QSS_MULTI_DERIVATIVE
  27. #include <artis-star/kernel/pdevs/Dynamics.hpp>
  28. namespace artis::qss {
  29. template<typename Time, typename Dyn, typename Parameters = common::NoParameters>
  30. class MultiDerivative : public artis::pdevs::Dynamics<Time, Dyn, Parameters> {
  31. public:
  32. struct input {
  33. enum values {
  34. RESET = 0, INTERNAL = 1, EXTERNAL = 1000
  35. };
  36. };
  37. struct var {
  38. enum values {
  39. VALUE
  40. };
  41. };
  42. typedef MultiDerivative<Time, Dyn, Parameters> type;
  43. MultiDerivative(const std::string &name,
  44. const artis::pdevs::Context<Time, Dyn, Parameters> &context)
  45. :
  46. artis::pdevs::Dynamics<Time, Dyn, Parameters>(name, context),
  47. _external_number(0), _internal_number(0), _variable_number(0) {
  48. DECLARE_STATES(int,
  49. ((state::PHASE, &type::_phase)));
  50. DECLARE_STATES(unsigned int,
  51. ((state::INPUT_NUMBER, &type::_input_number)));
  52. DECLARE_STATES(std::vector < double > ,
  53. ((state::OUTPUT_VALUES, &type::_output_values),
  54. (state::LAST_OUTPUTS, &type::_last_outputs)));
  55. this->input_port({input::RESET, "reset"});
  56. }
  57. virtual ~MultiDerivative() {}
  58. int external(const std::string &name, double Dyn::* var) {
  59. this->state_(state::LAST_OUTPUTS + _variable_number + 1, name, var);
  60. this->input_port({input::EXTERNAL + _external_number, name});
  61. ++_variable_number;
  62. ++_external_number;
  63. return input::EXTERNAL + _external_number - 1;
  64. }
  65. void internal(const std::string &name, double Dyn::* var) {
  66. assert(_external_number == 0);
  67. this->state_(state::LAST_OUTPUTS + _variable_number + 1, name, var);
  68. this->input_port({input::INTERNAL + _internal_number, name});
  69. this->output_port({_internal_number, name});
  70. this->observable({var::VALUE + _internal_number, name});
  71. ++_variable_number;
  72. ++_internal_number;
  73. }
  74. virtual std::vector<double> compute() = 0;
  75. virtual void dconf(const typename Time::type &t, typename Time::type e,
  76. const common::event::Bag <Time> &bag) {
  77. dint(t);
  78. dext(t, e, bag);
  79. }
  80. virtual void dint(const typename Time::type & /* t */) {
  81. if (_phase == phase::RESPONSE) {
  82. _last_outputs.assign(_output_values.begin(), _output_values.end());
  83. }
  84. _phase = phase::WAIT;
  85. }
  86. virtual void dext(const typename Time::type &t, typename Time::type e,
  87. const common::event::Bag <Time> &bag) {
  88. std::for_each(bag.begin(), bag.end(),
  89. [this, t, e](const common::event::ExternalEvent <Time> &event) {
  90. if (event.on_port(input::RESET)) {
  91. _input_number = 0;
  92. _phase = phase::INIT;
  93. } else {
  94. IntegratorData data;
  95. event.data()(data);
  96. if (event.port_index() >= input::INTERNAL
  97. and event.port_index() < input::EXTERNAL) {
  98. this->get((event.port_index() - input::INTERNAL) + state::LAST_OUTPUTS + 1)
  99. .put(
  100. static_cast<Dyn *>(this), data.value);
  101. } else {
  102. assert(event.port_index() >= input::EXTERNAL);
  103. this->get((event.port_index() - input::EXTERNAL) + state::LAST_OUTPUTS + 1)
  104. .put(
  105. static_cast<Dyn *>(this), data.value);
  106. }
  107. switch (_phase) {
  108. case phase::INIT: {
  109. ++_input_number;
  110. if (_input_number == this->state_number() - (state::LAST_OUTPUTS + 1)) {
  111. std::vector<double> values = compute();
  112. assign_values(values);
  113. _phase = phase::RESPONSE;
  114. }
  115. break;
  116. }
  117. case phase::WAIT:
  118. case phase::RESPONSE: {
  119. std::vector<double> values = compute();
  120. if (is_different_values(values)) {
  121. assign_values(values);
  122. _phase = phase::RESPONSE;
  123. } else {
  124. _phase = phase::WAIT;
  125. }
  126. }
  127. }
  128. }
  129. });
  130. }
  131. virtual void start(const typename Time::type & /* time */) {
  132. _output_values = std::vector<double>(_internal_number);
  133. _last_outputs = std::vector<double>(_internal_number);
  134. _input_number = 0;
  135. _phase = phase::INIT;
  136. }
  137. virtual typename Time::type ta(const typename Time::type & /* time */) {
  138. switch (_phase) {
  139. case phase::INIT:
  140. return Time::infinity;
  141. case phase::WAIT:
  142. return Time::infinity;
  143. case phase::RESPONSE:
  144. return 0;
  145. }
  146. return Time::infinity;
  147. }
  148. virtual common::event::Bag <Time> lambda(const typename Time::type & /* time */) const {
  149. common::event::Bag <Time> msgs;
  150. switch (_phase) {
  151. case phase::INIT:
  152. break;
  153. case phase::WAIT:
  154. break;
  155. case phase::RESPONSE: {
  156. unsigned int index = 0;
  157. for (double value: _output_values) {
  158. if (value != _last_outputs[index]) {
  159. const DerivativeData data = {value};
  160. msgs.push_back(common::event::ExternalEvent<Time>(index, data));
  161. }
  162. ++index;
  163. }
  164. }
  165. }
  166. return msgs;
  167. }
  168. virtual common::event::Value observe(const typename Time::type & /* t */,
  169. unsigned int index) const {
  170. if (index >= var::VALUE and index <= var::VALUE + _internal_number) {
  171. return (double) (_output_values[index - var::VALUE]);
  172. } else {
  173. return common::event::Value();
  174. }
  175. }
  176. unsigned int variable_number() const { return _internal_number; }
  177. private:
  178. void assign_values(const std::vector<double> &values) {
  179. for (unsigned int index = 0; index < _internal_number; ++index) {
  180. _output_values[index] = values[index];
  181. }
  182. }
  183. bool is_different_values(const std::vector<double> &values) {
  184. bool different = false;
  185. unsigned int index = 0;
  186. while (not different and index < _internal_number) {
  187. if (_output_values[index] != values[index]) {
  188. different = true;
  189. } else {
  190. ++index;
  191. }
  192. }
  193. return different;
  194. }
  195. struct phase {
  196. enum values {
  197. INIT = 0, WAIT, RESPONSE
  198. };
  199. };
  200. struct state {
  201. enum values {
  202. PHASE = 0, INPUT_NUMBER, OUTPUT_VALUES, LAST_OUTPUTS
  203. };
  204. };
  205. unsigned int _external_number;
  206. unsigned int _internal_number;
  207. unsigned int _variable_number;
  208. // state
  209. int _phase;
  210. unsigned int _input_number;
  211. std::vector<double> _output_values;
  212. std::vector<double> _last_outputs;
  213. };
  214. }
  215. #endif