MultiDerivative.hpp 7.7 KB

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