models.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 Models = artis::kernel::AbstractModels < artis::utils::DoubleTime,
  34. ModelParameters >;
  35. using Trace = artis::utils::Trace < artis::utils::DoubleTime >;
  36. using TraceElement = artis::utils::TraceElement < artis::utils::DoubleTime >;
  37. template < typename T >
  38. using AtomicModel = artis::kernel::AbstractAtomicModel <
  39. T, artis::utils::DoubleTime, ModelParameters >;
  40. template < typename T >
  41. using CoupledModel = artis::kernel::AbstractCoupledModel <
  42. T, artis::utils::DoubleTime, ModelParameters, GlobalParameters >;
  43. class AModel : public AtomicModel < AModel >
  44. {
  45. public:
  46. enum externals { };
  47. enum internals { DX, BX, IX };
  48. AModel(Models = Models())
  49. {
  50. Internal(IX, &AModel::_ix);
  51. Internal(BX, &AModel::_bx);
  52. Internal(DX, &AModel::_dx);
  53. }
  54. virtual ~AModel()
  55. { }
  56. void compute(double t, bool /* update */)
  57. {
  58. ::Trace::trace() << ::TraceElement("A", t, artis::utils::COMPUTE)
  59. << "Start";
  60. ::Trace::trace().flush();
  61. ++_ix;
  62. _bx = not _bx;
  63. ++_dx;
  64. ::Trace::trace() << ::TraceElement("A", t, artis::utils::COMPUTE)
  65. << "Stop";
  66. ::Trace::trace().flush();
  67. }
  68. void init(double /* t */, const ModelParameters& /* parameters */)
  69. {
  70. _ix = 0;
  71. _bx = false;
  72. _dx = 0.;
  73. }
  74. private:
  75. int _ix;
  76. bool _bx;
  77. double _dx;
  78. };
  79. class BModel : public AtomicModel < BModel >
  80. {
  81. public:
  82. enum externals { IX, BX, DX };
  83. enum internals { IY, IZ, BY, DY };
  84. enum states { N };
  85. BModel(Models = Models())
  86. {
  87. // external(IX, &BModel::_ix);
  88. Externals(int, (( IX, &BModel::_ix )));
  89. External(BX, &BModel::_bx);
  90. External(DX, &BModel::_dx);
  91. // internal(IY, &BModel::_iy);
  92. Internals(int, ( ( IY, &BModel::_iy ), ( IZ, &BModel::_iz ) ) );
  93. Internal(BY, &BModel::_by);
  94. Internal(DY, &BModel::_dy);
  95. States(int, ((N, &BModel::_n)));
  96. }
  97. virtual ~BModel()
  98. { }
  99. void compute(double t, bool /* update */)
  100. {
  101. ::Trace::trace() << ::TraceElement("B", t, artis::utils::COMPUTE)
  102. << "Start";
  103. ::Trace::trace().flush();
  104. _iy = _ix + 1;
  105. _by = not _bx;
  106. _dy = _dx + 1;
  107. ++_n;
  108. ::Trace::trace() << ::TraceElement("B", t, artis::utils::COMPUTE)
  109. << "Stop";
  110. ::Trace::trace().flush();
  111. }
  112. void init(double /* t */, const ModelParameters& /* parameters */)
  113. {
  114. _iy = 0;
  115. _by = false;
  116. _dy = 0.;
  117. _iz = 0;
  118. _n = 0;
  119. }
  120. private:
  121. // externals
  122. int _ix;
  123. bool _bx;
  124. double _dx;
  125. // internals
  126. int _iy;
  127. bool _by;
  128. double _dy;
  129. int _iz;
  130. // states
  131. int _n;
  132. };
  133. class RootModel : public CoupledModel < RootModel >
  134. {
  135. public:
  136. enum submodels { A, B };
  137. enum internals { IY, BY, DY, IZ, DX };
  138. enum states { N };
  139. RootModel(Models submodels) :
  140. _a(dynamic_cast < AModel* >(submodels[0])),
  141. _b(dynamic_cast < BModel* >(submodels[1]))
  142. {
  143. // submodels
  144. Submodels(((A, _a), (B, _b)));
  145. // internals
  146. Internal(DX, &RootModel::_dx);
  147. InternalsS(((IY, _b, BModel::IY), (IZ, _b, BModel::IZ)));
  148. InternalS(BY, _b, BModel::BY);
  149. // states
  150. States(int, ((N, &RootModel::_n)));
  151. // State(N, &RootModel::_n);
  152. }
  153. RootModel() : _a(new AModel), _b(new BModel)
  154. {
  155. // submodels
  156. Submodels(((A, _a), (B, _b)));
  157. // internals
  158. Internal(DX, &RootModel::_dx);
  159. InternalsS(((IY, _b, BModel::IY), (IZ, _b, BModel::IZ)));
  160. InternalS(BY, _b, BModel::BY);
  161. // states
  162. States(int, ((N, &RootModel::_n)));
  163. // State(N, &RootModel::_n);
  164. }
  165. virtual ~RootModel()
  166. {
  167. delete _a;
  168. delete _b;
  169. }
  170. void compute(double t, bool /* update */)
  171. {
  172. ::Trace::trace() << ::TraceElement(this->path(this), t,
  173. artis::utils::COMPUTE)
  174. << "Start";
  175. ::Trace::trace().flush();
  176. (*_a)(t);
  177. _b->put < double >(t, BModel::DX, _a->get < double >(t, AModel::DX));
  178. _b->put < int >(t, BModel::IX, _a->get < int >(t, AModel::IX));
  179. _b->put < bool >(t, BModel::BX, _a->get < bool >(t, AModel::BX));
  180. (*_b)(t);
  181. ++_n;
  182. this->trace_element(t, artis::utils::COMPUTE, "Stop");
  183. }
  184. void init(double t, const ModelParameters& parameters)
  185. {
  186. _a->init(t, parameters);
  187. _b->init(t, parameters);
  188. _n = 0;
  189. _dx = 0;
  190. }
  191. private:
  192. // submodels
  193. AModel* _a;
  194. BModel* _b;
  195. //internals
  196. double _dx;
  197. // states
  198. int _n;
  199. };
  200. #endif