test.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * @file test/test.cpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2012-2016 ULCO http://www.univ-littoral.fr
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #define CATCH_CONFIG_MAIN
  22. #include <test/catch.hpp>
  23. #include <artis/kernel/AbstractAtomicModel.hpp>
  24. #include <artis/kernel/AbstractCoupledModel.hpp>
  25. #include <artis/kernel/Simulator.hpp>
  26. #include <artis/observer/Output.hpp>
  27. #include <artis/utils/Trace.hpp>
  28. struct GlobalParameters
  29. { };
  30. struct ModelParameters
  31. { };
  32. using namespace artis::kernel;
  33. using namespace artis::utils;
  34. template < typename T >
  35. using AtomicModel = AbstractAtomicModel < T, DoubleTime, ModelParameters >;
  36. class AModel : public AtomicModel < AModel >
  37. {
  38. public:
  39. enum externals { };
  40. enum internalsB { BX };
  41. enum internalsI { IX };
  42. enum internals { DX };
  43. AModel()
  44. {
  45. internalI(IX, &AModel::_ix);
  46. internalB(BX, &AModel::_bx);
  47. internal(DX, &AModel::_dx);
  48. }
  49. virtual ~AModel()
  50. { }
  51. void compute(double /* t */, bool /* update */)
  52. {
  53. ++_ix;
  54. _bx = not _bx;
  55. ++_dx;
  56. }
  57. void init(double /* t */, const ModelParameters& /* parameters */)
  58. {
  59. _ix = 0;
  60. _bx = false;
  61. _dx = 0.;
  62. }
  63. private:
  64. int _ix;
  65. bool _bx;
  66. double _dx;
  67. };
  68. class BModel : public AtomicModel < BModel >
  69. {
  70. public:
  71. enum externalsB { BX };
  72. enum externalsI { IX };
  73. enum externals { DX };
  74. enum internalsB { BY };
  75. enum internalsI { IY };
  76. enum internals { DY };
  77. BModel()
  78. {
  79. externalI(IX, &BModel::_ix);
  80. externalB(BX, &BModel::_bx);
  81. external(DX, &BModel::_dx);
  82. internalI(IY, &BModel::_iy);
  83. internalB(BY, &BModel::_by);
  84. internal(DY, &BModel::_dy);
  85. }
  86. virtual ~BModel()
  87. { }
  88. void compute(double /* t */, bool update)
  89. {
  90. if (update)
  91. std::cout << "UPDATE" << std::endl;
  92. _iy = _ix + 1;
  93. _by = not _bx;
  94. _dy = _dx + 1;
  95. }
  96. void init(double /* t */, const ModelParameters& /* parameters */)
  97. {
  98. _iy = 0;
  99. _by = false;
  100. _dy = 0.;
  101. }
  102. private:
  103. // externals
  104. int _ix;
  105. bool _bx;
  106. double _dx;
  107. // internals
  108. int _iy;
  109. bool _by;
  110. double _dy;
  111. };
  112. class RootModel :
  113. public artis::kernel::AbstractCoupledModel < RootModel,
  114. artis::utils::DoubleTime,
  115. ModelParameters,
  116. GlobalParameters >
  117. {
  118. public:
  119. enum submodels { A, B };
  120. RootModel()
  121. {
  122. S({ { A, &_a }, { B, &_b } });
  123. }
  124. virtual ~RootModel()
  125. { }
  126. void compute(double t, bool /* update */)
  127. {
  128. _a(t);
  129. OUT_I(t, &_a, AModel::IX) >> IN_I(t, &_b, BModel::IX);
  130. OUT_B(t, &_a, AModel::BX) >> IN_B(t, &_b, BModel::BX);
  131. OUT(t, &_a, AModel::DX) >> IN(t, &_b, BModel::DX);
  132. _b(t);
  133. }
  134. void init(double t, const ModelParameters& parameters)
  135. {
  136. _a.init(t, parameters);
  137. _b.init(t, parameters);
  138. }
  139. private:
  140. AModel _a;
  141. BModel _b;
  142. };
  143. typedef artis::kernel::Simulator < RootModel,
  144. artis::utils::DoubleTime,
  145. ModelParameters,
  146. GlobalParameters > Simulator;
  147. class AView : public artis::observer::View < artis::utils::DoubleTime,
  148. ModelParameters >
  149. {
  150. public:
  151. AView()
  152. {
  153. selectorI("A::I", { RootModel::A, AModel::IX });
  154. selectorB("A::B", { RootModel::A, AModel::BX });
  155. selector("A::D", { RootModel::A, AModel::DX });
  156. selectorI("B::I", { RootModel::B, BModel::IY });
  157. selectorB("B::B", { RootModel::B, BModel::BY });
  158. selector("B::D", { RootModel::B, BModel::DY });
  159. }
  160. virtual ~AView()
  161. { }
  162. };
  163. typedef artis::observer::Output < artis::utils::DoubleTime,
  164. ModelParameters > Output;
  165. TEST_CASE("Simulator_tests", "simple")
  166. {
  167. GlobalParameters globalParameters;
  168. ModelParameters modelParameters;
  169. ::Simulator simulator(new RootModel, globalParameters);
  170. simulator.attachView("Root", new AView);
  171. artis::utils::Trace < artis::utils::DoubleTime >::trace().clear();
  172. simulator.init(0, modelParameters);
  173. simulator.run(0, 10);
  174. ::Output output(simulator.observer());
  175. output();
  176. }