models.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. ::Trace::trace() << ::TraceElement("A", t, artis::utils::COMPUTE)
  57. << "Start";
  58. ::Trace::trace().flush();
  59. ++_ix;
  60. _bx = not _bx;
  61. ++_dx;
  62. ::Trace::trace() << ::TraceElement("A", t, artis::utils::COMPUTE)
  63. << "Stop";
  64. ::Trace::trace().flush();
  65. }
  66. void init(double /* t */, const ModelParameters& /* parameters */)
  67. {
  68. _ix = 0;
  69. _bx = false;
  70. _dx = 0.;
  71. }
  72. private:
  73. int _ix;
  74. bool _bx;
  75. double _dx;
  76. };
  77. class BModel : public AtomicModel < BModel >
  78. {
  79. public:
  80. enum externals { IX, BX, DX };
  81. enum internals { IY, IZ, BY, DY };
  82. enum states { N };
  83. BModel()
  84. {
  85. // external(IX, &BModel::_ix);
  86. Externals(int, (( IX, &BModel::_ix )));
  87. External(BX, &BModel::_bx);
  88. External(DX, &BModel::_dx);
  89. // internal(IY, &BModel::_iy);
  90. Internals(int, ( ( IY, &BModel::_iy ), ( IZ, &BModel::_iz ) ) );
  91. Internal(BY, &BModel::_by);
  92. Internal(DY, &BModel::_dy);
  93. States(int, ((N, &BModel::_n)));
  94. }
  95. virtual ~BModel()
  96. { }
  97. void compute(double t, bool /* update */)
  98. {
  99. ::Trace::trace() << ::TraceElement("B", t, artis::utils::COMPUTE)
  100. << "Start";
  101. ::Trace::trace().flush();
  102. _iy = _ix + 1;
  103. _by = not _bx;
  104. _dy = _dx + 1;
  105. ++_n;
  106. ::Trace::trace() << ::TraceElement("B", t, artis::utils::COMPUTE)
  107. << "Stop";
  108. ::Trace::trace().flush();
  109. }
  110. void init(double /* t */, const ModelParameters& /* parameters */)
  111. {
  112. _iy = 0;
  113. _by = false;
  114. _dy = 0.;
  115. _iz = 0;
  116. _n = 0;
  117. }
  118. private:
  119. // externals
  120. int _ix;
  121. bool _bx;
  122. double _dx;
  123. // internals
  124. int _iy;
  125. bool _by;
  126. double _dy;
  127. int _iz;
  128. // states
  129. int _n;
  130. };
  131. class RootModel : public CoupledModel < RootModel >
  132. {
  133. public:
  134. enum submodels { A, B };
  135. enum internals { IY, BY, DY, IZ, DX };
  136. enum states { N };
  137. RootModel() : _a(new AModel), _b(new BModel)
  138. {
  139. // submodels
  140. Submodels(((A, _a.get()), (B, _b.get())));
  141. // internals
  142. Internal(DX, &RootModel::_dx);
  143. InternalsS(((IY, _b.get(), BModel::IY), (IZ, _b.get(), BModel::IZ)));
  144. InternalS(BY, _b.get(), BModel::BY);
  145. // states
  146. States(int, ((N, &RootModel::_n)));
  147. // State(N, &RootModel::_n);
  148. }
  149. virtual ~RootModel()
  150. { }
  151. void compute(double t, bool /* update */)
  152. {
  153. ::Trace::trace() << ::TraceElement(this->path(this), t, artis::utils::COMPUTE)
  154. << "Start";
  155. ::Trace::trace().flush();
  156. (*_a)(t);
  157. _b->put < double >(t, BModel::DX, _a->get < double >(t, AModel::DX));
  158. _b->put < int >(t, BModel::IX, _a->get < int >(t, AModel::IX));
  159. _b->put < bool >(t, BModel::BX, _a->get < bool >(t, AModel::BX));
  160. (*_b)(t);
  161. ++_n;
  162. this->trace_element(t, artis::utils::COMPUTE, "Stop");
  163. }
  164. void init(double t, const ModelParameters& parameters)
  165. {
  166. _a->init(t, parameters);
  167. _b->init(t, parameters);
  168. _n = 0;
  169. _dx = 0;
  170. }
  171. private:
  172. // submodels
  173. std::unique_ptr < AModel > _a;
  174. std::unique_ptr < BModel > _b;
  175. //internals
  176. double _dx;
  177. // states
  178. int _n;
  179. };
  180. #endif