AbstractCoupledModel.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /**
  2. * @file artis/kernel/AbstractCoupledModel.hpp
  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. #ifndef __ARTIS_KERNEL_ABSTRACT_COUPLED_MODEL_HPP
  22. #define __ARTIS_KERNEL_ABSTRACT_COUPLED_MODEL_HPP
  23. #include <artis/kernel/AbstractModel.hpp>
  24. #include <artis/kernel/Internals.hpp>
  25. #include <artis/kernel/States.hpp>
  26. #include <functional>
  27. #include <vector>
  28. namespace artis { namespace kernel {
  29. template < typename T, typename U, typename V, typename W >
  30. class AbstractCoupledModel : public AbstractModel < U, V >,
  31. public States < T, U >,
  32. public Internals < T, U >,
  33. public Externals < T, U >
  34. {
  35. typedef AbstractModel < U, V > type;
  36. typedef std::map < int, type* > Submodels;
  37. typedef std::map < int,
  38. std::vector < type* > > Setsubmodels;
  39. typedef std::map < int, std::pair < type*,
  40. int > > SubModelInternals;
  41. struct Var
  42. {
  43. unsigned int index;
  44. type* model;
  45. int sub_index;
  46. };
  47. public:
  48. AbstractCoupledModel()
  49. { }
  50. virtual ~AbstractCoupledModel()
  51. { }
  52. virtual void build(const W& /* parameters */)
  53. { }
  54. virtual void build(const W& /* parameters */,
  55. const std::string& /* json */)
  56. { }
  57. virtual bool check(typename U::type t) const
  58. { return Externals < T, U >::check(t); }
  59. virtual void compute(typename U::type t, bool update) = 0;
  60. virtual double get(typename U::type t, unsigned int index) const
  61. {
  62. typename AbstractCoupledModel::SubModelInternals::const_iterator it =
  63. submodel_internals.find(index);
  64. if (it == submodel_internals.end()) {
  65. return static_cast < const T* >(this)->*(
  66. Internals < T, U >::get(index));
  67. } else {
  68. return it->second.first->get(t, it->second.second);
  69. }
  70. }
  71. virtual int getI(typename U::type t, unsigned int index) const
  72. {
  73. typename AbstractCoupledModel::SubModelInternals::const_iterator it =
  74. submodel_internals.find(index);
  75. if (it == submodel_internals.end()) {
  76. return static_cast < const T* >(this)->*(
  77. Internals < T, U >::getI(index));
  78. } else {
  79. return it->second.first->getI(t, it->second.second);
  80. }
  81. }
  82. virtual bool getB(typename U::type t, unsigned int index) const
  83. {
  84. typename AbstractCoupledModel::SubModelInternals::const_iterator it =
  85. submodel_internals.find(index);
  86. if (it == submodel_internals.end()) {
  87. return static_cast < const T* >(this)->*(
  88. Internals < T, U >::getB(index));
  89. } else {
  90. return it->second.first->getB(t, it->second.second);
  91. }
  92. }
  93. virtual void init(typename U::type t, const V& parameters) = 0;
  94. bool is_computed(typename U::type t, unsigned int index) const
  95. {
  96. typename AbstractCoupledModel::SubModelInternals::const_iterator it =
  97. submodel_internals.find(index);
  98. if (it == submodel_internals.end()) {
  99. return false;
  100. } else {
  101. return it->second.first->is_computed(t, it->second.second);
  102. }
  103. }
  104. bool is_stable(typename U::type t) const
  105. {
  106. typename AbstractCoupledModel::SubModelInternals::const_iterator it =
  107. submodel_internals.begin();
  108. bool stable = true;
  109. while (it != submodel_internals.end() and stable) {
  110. stable = it->second.first->is_stable(t);
  111. ++it;
  112. }
  113. return stable;
  114. }
  115. virtual bool is_updated() const
  116. { return Externals < T, U >::updated; }
  117. virtual const Node < U >* get_submodel(unsigned int index) const
  118. {
  119. typename AbstractCoupledModel::Submodels::const_iterator it =
  120. submodels.find(index);
  121. if (it != submodels.end()) {
  122. return it->second;
  123. } else {
  124. return 0;
  125. }
  126. }
  127. virtual const Node < U >* get_submodel(unsigned int index,
  128. unsigned int rank) const
  129. {
  130. typename AbstractCoupledModel::Setsubmodels::const_iterator it =
  131. setsubmodels.find(index);
  132. if (it != setsubmodels.end() and it->second.size() > rank) {
  133. return it->second.at(rank);
  134. } else {
  135. return 0;
  136. }
  137. }
  138. virtual void restore(const State < U >& state)
  139. {
  140. typename AbstractCoupledModel::Submodels::const_iterator it =
  141. submodels.begin();
  142. while (it != submodels.end()) {
  143. it->second->restore(state.get_substate(it->first));
  144. ++it;
  145. }
  146. States < T, U >::restore(state);
  147. Internals < T, U >::restore(state);
  148. }
  149. virtual void save(State < U >& state) const
  150. {
  151. typename AbstractCoupledModel::Submodels::const_iterator it =
  152. submodels.begin();
  153. while (it != submodels.end()) {
  154. State < U > substate;
  155. it->second->save(substate);
  156. state.add_substate(it->first, substate);
  157. ++it;
  158. }
  159. States < T, U >::save(state);
  160. Internals < T, U >::save(state);
  161. }
  162. virtual void stable()
  163. { Externals < T, U >::updated = false; }
  164. protected:
  165. void change_internal(unsigned int index, double T::* var)
  166. {
  167. submodel_internals.erase(index);
  168. Internals < T, U >::internal(index, var);
  169. }
  170. void change_internal(unsigned int index, int T::* var)
  171. {
  172. submodel_internals.erase(index);
  173. Internals < T, U >::internalI(index, var);
  174. }
  175. void change_internal(unsigned int index, bool T::* var)
  176. {
  177. submodel_internals.erase(index);
  178. Internals < T, U >::internalB(index, var);
  179. }
  180. void internal(unsigned int index, double T::* var)
  181. { Internals < T, U >::internal(index, var); }
  182. void I(std::initializer_list < std::pair < unsigned int,
  183. bool T::* > > internals)
  184. { Internals < T, U >::I(internals); }
  185. void I(std::initializer_list < std::pair < unsigned int,
  186. int T::* > > internals)
  187. { Internals < T, U >::I(internals); }
  188. void I(std::initializer_list < std::pair < unsigned int,
  189. double T::* > > internals)
  190. { Internals < T, U >::I(internals); }
  191. void internal(unsigned int index, type* model, int sub_index)
  192. {
  193. submodel_internals[index] =
  194. std::pair < type*, int >(model, sub_index);
  195. }
  196. void I(std::initializer_list < Var > internals)
  197. {
  198. for (typename std::initializer_list < Var >::iterator it =
  199. internals.begin(); it != internals.end(); ++it) {
  200. submodel_internals[it->index] =
  201. std::pair < type*, int >(it->model, it->sub_index);
  202. }
  203. }
  204. void submodel(unsigned int index, type* model)
  205. { submodels[index] = model; }
  206. void S(std::initializer_list < std::pair < unsigned int,
  207. type* > > models)
  208. {
  209. for (typename std::initializer_list < std::pair < unsigned int,
  210. type* > >::iterator it =
  211. models.begin(); it != models.end(); ++it) {
  212. submodels[it->first] = it->second;
  213. }
  214. }
  215. void setsubmodel(unsigned int index, type* model)
  216. {
  217. if (setsubmodels.find(index) == setsubmodels.end()) {
  218. setsubmodels[index] = std::vector < type* >();
  219. }
  220. setsubmodels[index].push_back(model);
  221. }
  222. private:
  223. SubModelInternals submodel_internals;
  224. Submodels submodels;
  225. Setsubmodels setsubmodels;
  226. };
  227. template < typename T, typename U, typename V, typename W >
  228. struct IN_t
  229. {
  230. IN_t(AbstractCoupledModel < T, U, V, W >* model,
  231. unsigned int index) : model(model), index(index)
  232. { }
  233. AbstractCoupledModel < T, U, V, W >* model;
  234. unsigned int index;
  235. };
  236. template < typename T, typename U, typename V, typename W >
  237. struct OUT_SC_t
  238. {
  239. OUT_SC_t(double t, AbstractCoupledModel < T, U, V, W >* model,
  240. unsigned int index) : t(t), model(model), index(index)
  241. { }
  242. double t;
  243. AbstractCoupledModel < T, U, V, W >* model;
  244. unsigned int index;
  245. };
  246. template < typename T >
  247. struct OUT_A_t
  248. {
  249. OUT_A_t(double t, T value) : t(t), value(value)
  250. { }
  251. double t;
  252. T value;
  253. };
  254. template < typename T, typename U, typename V, typename W >
  255. IN_t < T, U, V, W > IN(AbstractCoupledModel < T, U, V, W >* model,
  256. unsigned int index)
  257. { return IN_t < T, U, V, W >(model, index); }
  258. template < typename T, typename U, typename V, typename W >
  259. OUT_SC_t < T, U, V, W > OUT(double t,
  260. AbstractCoupledModel < T, U, V, W >* model,
  261. unsigned int index)
  262. { return OUT_SC_t < T, U, V, W >(t, model, index); }
  263. template < typename T >
  264. OUT_A_t < T > OUT(double t, T value)
  265. { return OUT_A_t < T >(t, value); }
  266. template < typename T1, typename U1, typename V1, typename W1,
  267. typename T2, typename U2, typename V2, typename W2 >
  268. void operator>>(OUT_SC_t < T2, U2, V2, W2 > out, IN_t < T1, U1, V1, W1 > in)
  269. {
  270. if (out.model->is_computed(out.t, out.index)) {
  271. in.model->put(out.t, in.index, out.model->get(out.t, out.index));
  272. }
  273. }
  274. template < typename T, typename U, typename V, typename W, typename Z >
  275. void operator>>(OUT_A_t < Z > out, IN_t < T, U, V, W > in)
  276. { in.model->put(out.t, in.index, out.value); }
  277. } }
  278. #endif