123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- /**
- * @file test/models.hpp
- * @author See the AUTHORS file
- */
- /*
- * Copyright (C) 2012-2019 ULCO http://www.univ-littoral.fr
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #ifndef TEST_MODELS_HPP
- #define TEST_MODELS_HPP
- #include <artis/kernel/AbstractAtomicModel.hpp>
- #include <artis/kernel/AbstractCoupledModel.hpp>
- #include <artis/utils/Trace.hpp>
- #include <memory>
- struct GlobalParameters {
- };
- struct ModelParameters {
- };
- using Model = artis::kernel::AbstractModel<artis::utils::DoubleTime,
- ModelParameters>;
- using Models = artis::kernel::AbstractModels<artis::utils::DoubleTime,
- ModelParameters>;
- using Trace = artis::utils::Trace<artis::utils::DoubleTime>;
- using TraceElement = artis::utils::TraceElement<artis::utils::DoubleTime>;
- template<typename T>
- using AtomicModel = artis::kernel::AbstractAtomicModel<
- T, artis::utils::DoubleTime, ModelParameters>;
- template<typename T>
- using CoupledModel = artis::kernel::AbstractCoupledModel<
- T, artis::utils::DoubleTime, ModelParameters, GlobalParameters>;
- class AModel : public AtomicModel<AModel> {
- public:
- enum externals {
- };
- enum internals {
- DX, BX, IX
- };
- AModel(Models = Models())
- {
- Internal(IX, &AModel::_ix);
- Internal(BX, &AModel::_bx);
- Internal(DX, &AModel::_dx);
- }
- virtual ~AModel() { }
- void compute(double t, bool /* update */)
- {
- ::Trace::trace() << ::TraceElement("A", t, artis::utils::COMPUTE)
- << "Start";
- ::Trace::trace().flush();
- ++_ix;
- _bx = not _bx;
- ++_dx;
- ::Trace::trace() << ::TraceElement("A", t, artis::utils::COMPUTE)
- << "Stop";
- ::Trace::trace().flush();
- }
- void init(double /* t */, const ModelParameters& /* parameters */)
- {
- _ix = 0;
- _bx = false;
- _dx = 0.;
- }
- //private:
- int _ix;
- bool _bx;
- double _dx;
- };
- class BModel : public AtomicModel<BModel> {
- public:
- enum externals {
- IX, BX, DX
- };
- enum internals {
- IY, IZ, BY, DY
- };
- enum states {
- N
- };
- BModel(Models = Models())
- {
- // external(IX, &BModel::_ix);
- Externals(int, ((IX, &BModel::_ix)));
- External(BX, &BModel::_bx);
- External(DX, &BModel::_dx);
- // internal(IY, &BModel::_iy);
- Internals(int, ((IY, &BModel::_iy), (IZ, &BModel::_iz)));
- Internal(BY, &BModel::_by);
- Internal(DY, &BModel::_dy);
- States(int, ((N, &BModel::_n)));
- }
- virtual ~BModel() { }
- void compute(double t, bool /* update */)
- {
- ::Trace::trace() << ::TraceElement("B", t, artis::utils::COMPUTE)
- << "Start";
- ::Trace::trace().flush();
- _iy = _ix + 1;
- _by = not _bx;
- _dy = _dx + 1;
- ++_n;
- ::Trace::trace() << ::TraceElement("B", t, artis::utils::COMPUTE)
- << "Stop";
- ::Trace::trace().flush();
- }
- void init(double /* t */, const ModelParameters& /* parameters */)
- {
- _iy = 0;
- _by = false;
- _dy = 0.;
- _iz = 0;
- _n = 0;
- }
- private:
- // externals
- int _ix;
- bool _bx;
- double _dx;
- // internals
- int _iy;
- bool _by;
- double _dy;
- int _iz;
- // states
- int _n;
- };
- class RootModel : public CoupledModel<RootModel> {
- public:
- enum submodels {
- A, B
- };
- enum internals {
- IY, BY, DY, IZ, DX
- };
- enum states {
- N
- };
- RootModel(Models submodels)
- :
- _a(dynamic_cast < AModel* >(submodels[0])),
- _b(dynamic_cast < BModel* >(submodels[1]))
- {
- // submodels
- Submodels(((A, _a), (B, _b)));
- // internals
- Internal(DX, &RootModel::_dx);
- InternalsS(((IY, _b, BModel::IY), (IZ, _b, BModel::IZ)));
- InternalS(BY, _b, BModel::BY);
- // states
- States(int, ((N, &RootModel::_n)));
- // State(N, &RootModel::_n);
- }
- RootModel()
- :_a(new AModel), _b(new BModel)
- {
- // submodels
- Submodels(((A, _a), (B, _b)));
- // internals
- Internal(DX, &RootModel::_dx);
- InternalsS(((IY, _b, BModel::IY), (IZ, _b, BModel::IZ)));
- InternalS(BY, _b, BModel::BY);
- // states
- States(int, ((N, &RootModel::_n)));
- // State(N, &RootModel::_n);
- }
- virtual ~RootModel()
- {
- delete _a;
- delete _b;
- }
- void compute(double t, bool /* update */)
- {
- ::Trace::trace() << ::TraceElement(this->path(this), t,
- artis::utils::COMPUTE)
- << "Start";
- ::Trace::trace().flush();
- (*_a)(t);
- _b->put<double>(t, BModel::DX, _a->get<double>(t, AModel::DX));
- _b->put<int>(t, BModel::IX, _a->get<int>(t, AModel::IX));
- _b->put<bool>(t, BModel::BX, _a->get<bool>(t, AModel::BX));
- (*_b)(t);
- ++_n;
- this->trace_element(t, artis::utils::COMPUTE, "Stop");
- }
- void init(double t, const ModelParameters& parameters)
- {
- _a->init(t, parameters);
- _b->init(t, parameters);
- _n = 0;
- _dx = 0;
- }
- private:
- // submodels
- AModel* _a;
- BModel* _b;
- //internals
- double _dx;
- // states
- int _n;
- };
- #endif
|