models.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @file tests/qss/models.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 TESTS_QSS_MODELS_HPP
  26. #define TESTS_QSS_MODELS_HPP 1
  27. #include <artis-star/common/time/DoubleTime.hpp>
  28. #include <artis-star/common/utils/Trace.hpp>
  29. #include <artis-star/kernel/pdevs/qss/Derivative.hpp>
  30. namespace artis {
  31. namespace tests {
  32. namespace qss {
  33. class Constant : public artis::pdevs::qss::Derivative<common::DoubleTime, Constant> {
  34. public:
  35. Constant(const std::string& name,
  36. const artis::pdevs::Context<common::DoubleTime, Constant, artis::common::NoParameters>& context)
  37. :
  38. artis::pdevs::qss::Derivative<common::DoubleTime, Constant>(name,
  39. context) { }
  40. ~Constant() override = default;
  41. double compute() const override { return 0.5; }
  42. };
  43. struct ParabolaParameters {
  44. double alpha;
  45. };
  46. class Parabola
  47. : public artis::pdevs::qss::Derivative<common::DoubleTime, Parabola, ParabolaParameters> {
  48. public:
  49. enum states {
  50. X = LAST_OUTPUT + 1
  51. };
  52. Parabola(const std::string& name,
  53. const artis::pdevs::Context<common::DoubleTime, Parabola, ParabolaParameters>& context)
  54. :
  55. artis::pdevs::qss::Derivative<common::DoubleTime, Parabola, ParabolaParameters>(
  56. name,
  57. context), _alpha(context.parameters().alpha)
  58. {
  59. DECLARE_STATES(double, ((X, &Parabola::_x)));
  60. }
  61. ~Parabola() override = default;
  62. double compute() const override { return _alpha * _x; }
  63. private:
  64. double _alpha;
  65. double _x;
  66. };
  67. struct PreyPredatorParameters {
  68. double a;
  69. double b;
  70. double d;
  71. double e;
  72. };
  73. class Predator
  74. : public artis::pdevs::qss::Derivative<common::DoubleTime, Predator, PreyPredatorParameters> {
  75. public:
  76. enum inputs {
  77. IN_X = IN + 1
  78. };
  79. enum states {
  80. Y = LAST_OUTPUT + 1, X
  81. };
  82. Predator(const std::string& name,
  83. const artis::pdevs::Context<common::DoubleTime, Predator, PreyPredatorParameters>& context)
  84. :
  85. artis::pdevs::qss::Derivative<common::DoubleTime, Predator, PreyPredatorParameters>(
  86. name,
  87. context), _b(context.parameters().b), _d(context.parameters().d),
  88. _e(context.parameters().e)
  89. {
  90. DECLARE_STATES(double, ((X, &Predator::_x), (Y, &Predator::_y)));
  91. input_port({IN_X, "in_x"});
  92. }
  93. ~Predator() override = default;
  94. double compute() const override { return _b * _d * _x * _y - _e * _y; }
  95. private:
  96. // parameters
  97. double _b;
  98. double _d;
  99. double _e;
  100. // state
  101. double _x;
  102. double _y;
  103. };
  104. class Prey
  105. : public artis::pdevs::qss::Derivative<common::DoubleTime, Prey, PreyPredatorParameters> {
  106. public:
  107. enum inputs {
  108. IN_Y = IN + 1
  109. };
  110. enum states {
  111. X = LAST_OUTPUT + 1, Y
  112. };
  113. Prey(const std::string& name,
  114. const artis::pdevs::Context<common::DoubleTime, Prey, PreyPredatorParameters>& context)
  115. :
  116. artis::pdevs::qss::Derivative<common::DoubleTime, Prey, PreyPredatorParameters>(
  117. name,
  118. context), _a(context.parameters().a), _b(context.parameters().b)
  119. {
  120. DECLARE_STATES(double, ((X, &Prey::_x), (Y, &Prey::_y)));
  121. input_port({IN_Y, "in_y"});
  122. }
  123. ~Prey() override = default;
  124. double compute() const override { return _a * _x - _b * _y * _x; }
  125. private:
  126. // parameters
  127. double _a;
  128. double _b;
  129. // state
  130. double _x;
  131. double _y;
  132. };
  133. }
  134. }
  135. } // namespace artis tests qss
  136. #endif