models.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * @file test/models.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2012-2017 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. #ifndef TEST_MODELS_HPP
  22. #define TEST_MODELS_HPP
  23. #include <artis/kernel/AbstractAtomicModel.hpp>
  24. #include <artis/kernel/AbstractCoupledModel.hpp>
  25. #include <artis/utils/Trace.hpp>
  26. #include <memory>
  27. struct GlobalParameters
  28. { };
  29. struct ModelParameters
  30. { };
  31. using Model = artis::kernel::AbstractModel < artis::utils::DoubleTime,
  32. ModelParameters >;
  33. using Trace = artis::utils::Trace < artis::utils::DoubleTime >;
  34. using TraceElement = artis::utils::TraceElement < artis::utils::DoubleTime >;
  35. template < typename T >
  36. using AtomicModel = artis::kernel::AbstractAtomicModel <
  37. T, artis::utils::DoubleTime, ModelParameters >;
  38. template < typename T >
  39. using CoupledModel = artis::kernel::AbstractCoupledModel <
  40. T, artis::utils::DoubleTime, ModelParameters, GlobalParameters >;
  41. class AModel : public AtomicModel < AModel >
  42. {
  43. public:
  44. enum externals { };
  45. enum internals { DX, BX, IX };
  46. AModel()
  47. {
  48. Internal(IX, &AModel::_ix);
  49. Internal(BX, &AModel::_bx);
  50. Internal(DX, &AModel::_dx);
  51. }
  52. virtual ~AModel()
  53. { }
  54. void compute(double t, bool /* update */)
  55. {
  56. ++_ix;
  57. _bx = not _bx;
  58. ++_dx;
  59. ::Trace::trace() << ::TraceElement("A", t, artis::utils::COMPUTE);
  60. ::Trace::trace().flush();
  61. }
  62. void init(double /* t */, const ModelParameters& /* parameters */)
  63. {
  64. _ix = 0;
  65. _bx = false;
  66. _dx = 0.;
  67. }
  68. //private:
  69. int _ix;
  70. bool _bx;
  71. double _dx;
  72. };
  73. class BModel : public AtomicModel < BModel >
  74. {
  75. public:
  76. enum externals { IX, BX, DX };
  77. enum internals { IY, BY, DY, IZ };
  78. enum states { N };
  79. BModel()
  80. {
  81. // external(IX, &BModel::_ix);
  82. Externals(int, (( IX, &BModel::_ix )));
  83. External(BX, &BModel::_bx);
  84. External(DX, &BModel::_dx);
  85. // internal(IY, &BModel::_iy);
  86. Internals(int, ( ( IY, &BModel::_iy ), ( IZ, &BModel::_iz ) ) );
  87. Internal(BY, &BModel::_by);
  88. Internal(DY, &BModel::_dy);
  89. States(int, ((N, &BModel::_n)));
  90. }
  91. virtual ~BModel()
  92. { }
  93. void compute(double t, bool /* update */)
  94. {
  95. _iy = _ix + 1;
  96. _by = not _bx;
  97. _dy = _dx + 1;
  98. ++_n;
  99. ::Trace::trace() << ::TraceElement("B", t, artis::utils::COMPUTE);
  100. ::Trace::trace().flush();
  101. }
  102. void init(double /* t */, const ModelParameters& /* parameters */)
  103. {
  104. _iy = 0;
  105. _by = false;
  106. _dy = 0.;
  107. _iz = 0;
  108. _n = 0;
  109. }
  110. private:
  111. // externals
  112. int _ix;
  113. bool _bx;
  114. double _dx;
  115. // internals
  116. int _iy;
  117. bool _by;
  118. double _dy;
  119. int _iz;
  120. // states
  121. int _n;
  122. };
  123. class RootModel : public CoupledModel < RootModel >
  124. {
  125. public:
  126. enum submodels { A, B };
  127. enum internals { IY, BY, DY, IZ, DX };
  128. enum states { N };
  129. RootModel() : _a(new AModel), _b(new BModel)
  130. {
  131. // submodels
  132. Submodels(((A, _a.get()), (B, _b.get())));
  133. // internals
  134. Internal(DX, &RootModel::_dx);
  135. InternalsS(((IY, _b.get(), BModel::IY), (IZ, _b.get(), BModel::IZ)));
  136. InternalS(BY, _b.get(), BModel::BY);
  137. // states
  138. States(int, ((N, &RootModel::_n)));
  139. // State(N, &RootModel::_n);
  140. }
  141. virtual ~RootModel()
  142. { }
  143. void compute(double t, bool /* update */)
  144. {
  145. (*_a)(t);
  146. _b->put < double >(t, BModel::DX, _a->get < double >(t, AModel::DX));
  147. _b->put < int >(t, BModel::IX, _a->get < int >(t, AModel::IX));
  148. _b->put < bool >(t, BModel::BX, _a->get < bool >(t, AModel::BX));
  149. (*_b)(t);
  150. ++_n;
  151. ::Trace::trace() << ::TraceElement("ROOT", t, artis::utils::COMPUTE);
  152. ::Trace::trace().flush();
  153. }
  154. void init(double t, const ModelParameters& parameters)
  155. {
  156. _a->init(t, parameters);
  157. _b->init(t, parameters);
  158. _n = 0;
  159. }
  160. private:
  161. // submodels
  162. std::unique_ptr < AModel > _a;
  163. std::unique_ptr < BModel > _b;
  164. //internals
  165. double _dx;
  166. // states
  167. int _n;
  168. };
  169. #endif