|
@@ -23,200 +23,22 @@
|
|
|
//#define CATCH_CONFIG_MAIN
|
|
|
//#include <test/catch.hpp>
|
|
|
|
|
|
-#include <artis/kernel/AbstractAtomicModel.hpp>
|
|
|
-#include <artis/kernel/AbstractCoupledModel.hpp>
|
|
|
-//#include <artis/kernel/Builder.hpp>
|
|
|
+#include <test/models.hpp>
|
|
|
+
|
|
|
+#include <artis/kernel/Builder.hpp>
|
|
|
#include <artis/kernel/ModelFactory.hpp>
|
|
|
#include <artis/kernel/Simulator.hpp>
|
|
|
#include <artis/observer/Output.hpp>
|
|
|
|
|
|
#include <artis/utils/DateTime.hpp>
|
|
|
-#include <artis/utils/Trace.hpp>
|
|
|
-
|
|
|
-#include <memory>
|
|
|
-
|
|
|
-struct GlobalParameters
|
|
|
-{ };
|
|
|
-
|
|
|
-struct ModelParameters
|
|
|
-{ };
|
|
|
|
|
|
using namespace artis::kernel;
|
|
|
-using namespace artis::utils;
|
|
|
|
|
|
-using Model = AbstractModel < DoubleTime, ModelParameters >;
|
|
|
using Creator = ObjectCreator < Model >;
|
|
|
using Factory = ModelFactory < Model, int, Creator >;
|
|
|
|
|
|
-using Trace = Trace < DoubleTime >;
|
|
|
-using TraceElement = TraceElement < DoubleTime >;
|
|
|
-
|
|
|
-template < typename T >
|
|
|
-using AtomicModel = AbstractAtomicModel < T, DoubleTime, ModelParameters >;
|
|
|
-
|
|
|
-template < typename T >
|
|
|
-using CoupledModel = AbstractCoupledModel < T, DoubleTime, ModelParameters,
|
|
|
- GlobalParameters >;
|
|
|
-
|
|
|
-class AModel : public AtomicModel < AModel >
|
|
|
-{
|
|
|
-public:
|
|
|
- enum externals { };
|
|
|
-
|
|
|
- enum internals { DX, BX, IX };
|
|
|
-
|
|
|
- AModel()
|
|
|
- {
|
|
|
- internal(IX, &AModel::_ix);
|
|
|
- internal(BX, &AModel::_bx);
|
|
|
- internal(DX, &AModel::_dx);
|
|
|
- }
|
|
|
-
|
|
|
- virtual ~AModel()
|
|
|
- { }
|
|
|
-
|
|
|
- void compute(double t, bool /* update */)
|
|
|
- {
|
|
|
- ++_ix;
|
|
|
- _bx = not _bx;
|
|
|
- ++_dx;
|
|
|
-
|
|
|
- ::Trace::trace() << ::TraceElement("A", t, COMPUTE);
|
|
|
- ::Trace::trace().flush();
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- void init(double /* t */, const ModelParameters& /* parameters */)
|
|
|
- {
|
|
|
- _ix = 0;
|
|
|
- _bx = false;
|
|
|
- _dx = 0.;
|
|
|
- }
|
|
|
-
|
|
|
-private:
|
|
|
- int _ix;
|
|
|
- bool _bx;
|
|
|
- double _dx;
|
|
|
-};
|
|
|
-
|
|
|
DECLARE_MODEL(AModel, ::Model, ::Factory);
|
|
|
-
|
|
|
-class BModel : public AtomicModel < BModel >
|
|
|
-{
|
|
|
-public:
|
|
|
- enum externals { IX, BX, DX };
|
|
|
- enum internals { IY, BY, DY };
|
|
|
- enum states { N };
|
|
|
-
|
|
|
- BModel()
|
|
|
- {
|
|
|
- // external(IX, &BModel::_ix);
|
|
|
- E < int >({ { IX, &BModel::_ix } });
|
|
|
-
|
|
|
- external(BX, &BModel::_bx);
|
|
|
- external(DX, &BModel::_dx);
|
|
|
-
|
|
|
- // internal(IY, &BModel::_iy);
|
|
|
- I < int >({ { IY, &BModel::_iy } });
|
|
|
-
|
|
|
- internal(BY, &BModel::_by);
|
|
|
- internal(DY, &BModel::_dy);
|
|
|
-
|
|
|
- S < int >({ { N, &BModel::_n } });
|
|
|
- }
|
|
|
-
|
|
|
- virtual ~BModel()
|
|
|
- { }
|
|
|
-
|
|
|
- void compute(double t, bool update)
|
|
|
- {
|
|
|
- if (update)
|
|
|
- std::cout << "UPDATE" << std::endl;
|
|
|
-
|
|
|
- _iy = _ix + 1;
|
|
|
- _by = not _bx;
|
|
|
- _dy = _dx + 1;
|
|
|
- ++_n;
|
|
|
-
|
|
|
- ::Trace::trace() << ::TraceElement("B", t, COMPUTE);
|
|
|
- ::Trace::trace().flush();
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- void init(double /* t */, const ModelParameters& /* parameters */)
|
|
|
- {
|
|
|
- _iy = 0;
|
|
|
- _by = false;
|
|
|
- _dy = 0.;
|
|
|
- _n = 0;
|
|
|
- }
|
|
|
-
|
|
|
-private:
|
|
|
- // externals
|
|
|
- int _ix;
|
|
|
- bool _bx;
|
|
|
- double _dx;
|
|
|
-
|
|
|
- // internals
|
|
|
- int _iy;
|
|
|
- bool _by;
|
|
|
- double _dy;
|
|
|
-
|
|
|
- // states
|
|
|
- int _n;
|
|
|
-};
|
|
|
-
|
|
|
DECLARE_MODEL(BModel, ::Model, ::Factory);
|
|
|
-
|
|
|
-class RootModel : public CoupledModel < RootModel >
|
|
|
-{
|
|
|
-public:
|
|
|
- enum submodels { A, B };
|
|
|
- enum states { N };
|
|
|
-
|
|
|
- RootModel() : _a(new AModel), _b(new BModel)
|
|
|
- {
|
|
|
- // submodels
|
|
|
- S({ { A, _a.get() }, { B, _b.get() } });
|
|
|
-
|
|
|
- // states
|
|
|
- S < int >({ { N, &RootModel::_n } });
|
|
|
- }
|
|
|
-
|
|
|
- virtual ~RootModel()
|
|
|
- { }
|
|
|
-
|
|
|
- void compute(double t, bool /* update */)
|
|
|
- {
|
|
|
- (*_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;
|
|
|
-
|
|
|
- ::Trace::trace() << ::TraceElement("ROOT", t, COMPUTE);
|
|
|
- ::Trace::trace().flush();
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- void init(double t, const ModelParameters& parameters)
|
|
|
- {
|
|
|
- _a->init(t, parameters);
|
|
|
- _b->init(t, parameters);
|
|
|
- _n = 0;
|
|
|
- }
|
|
|
-
|
|
|
-private:
|
|
|
- // submodels
|
|
|
- std::unique_ptr < AModel > _a;
|
|
|
- std::unique_ptr < BModel > _b;
|
|
|
-
|
|
|
- // states
|
|
|
- int _n;
|
|
|
-};
|
|
|
-
|
|
|
DECLARE_MODEL(RootModel, ::Model, ::Factory);
|
|
|
|
|
|
typedef artis::kernel::Simulator < RootModel,
|
|
@@ -224,11 +46,11 @@ typedef artis::kernel::Simulator < RootModel,
|
|
|
ModelParameters,
|
|
|
GlobalParameters > ASimulator;
|
|
|
|
|
|
-// typedef artis::kernel::Builder < ::Factory,
|
|
|
-// RootModel,
|
|
|
-// artis::utils::DoubleTime,
|
|
|
-// ModelParameters,
|
|
|
-// GlobalParameters > ABuilder;
|
|
|
+typedef artis::kernel::Builder < ::Factory,
|
|
|
+ RootModel,
|
|
|
+ artis::utils::DoubleTime,
|
|
|
+ ModelParameters,
|
|
|
+ GlobalParameters > ABuilder;
|
|
|
|
|
|
class AView : public artis::observer::View < artis::utils::DoubleTime,
|
|
|
ModelParameters >
|
|
@@ -251,183 +73,54 @@ public:
|
|
|
typedef artis::observer::Output < artis::utils::DoubleTime,
|
|
|
ModelParameters > AnOutput;
|
|
|
|
|
|
-struct NullModel : CoupledModel < NullModel >
|
|
|
-{ };
|
|
|
-
|
|
|
-template < typename T >
|
|
|
-struct TType
|
|
|
-{
|
|
|
- typedef T type;
|
|
|
-
|
|
|
- T* operator()()
|
|
|
- { return new T(); }
|
|
|
-};
|
|
|
-
|
|
|
-template < typename P, typename T, typename N /*, typename ST */ > // I, In, Ex, S >
|
|
|
-struct TCoupledModel
|
|
|
-{
|
|
|
- typename T::type* operator()(P* parent)
|
|
|
- {
|
|
|
- //T::type* m = T()();
|
|
|
- N n;
|
|
|
- // ST st;
|
|
|
-
|
|
|
- n(parent);
|
|
|
- //st(m);
|
|
|
- return T()();
|
|
|
- }
|
|
|
-};
|
|
|
-
|
|
|
-// template < T, N, I, ST, In, Ex >
|
|
|
-// class AtomicModel
|
|
|
-// {
|
|
|
-// };
|
|
|
-
|
|
|
-template < typename P >
|
|
|
-struct TNull
|
|
|
-{
|
|
|
- P* operator()()
|
|
|
- { return nullptr; }
|
|
|
-};
|
|
|
-
|
|
|
-template < typename T, typename X >
|
|
|
-struct TVariable
|
|
|
-{
|
|
|
- X T::* variable;
|
|
|
-};
|
|
|
-
|
|
|
-template < typename P, typename N = TNull < P > >
|
|
|
-struct TName
|
|
|
-{
|
|
|
- void operator()(P* parent)
|
|
|
- {
|
|
|
- if (parent) {
|
|
|
- N n;
|
|
|
-
|
|
|
- parent->submodel(0, n());
|
|
|
- }
|
|
|
- }
|
|
|
-};
|
|
|
-
|
|
|
-// template < typename M, typename N, typename I, typename T >
|
|
|
-// struct TState
|
|
|
-// {
|
|
|
-// void operator()(M* m)
|
|
|
-// {
|
|
|
-// N n;
|
|
|
-
|
|
|
-// }
|
|
|
-// };
|
|
|
-
|
|
|
-template < typename T, typename M >
|
|
|
-struct TBuilder
|
|
|
-{
|
|
|
- T* operator()()
|
|
|
- {
|
|
|
- M m;
|
|
|
-
|
|
|
- return m(nullptr);
|
|
|
- }
|
|
|
-};
|
|
|
-
|
|
|
-// constexpr int AModel::* p = AModel::_ix;
|
|
|
-
|
|
|
-// typedef TBuilder <
|
|
|
-// RootModel,
|
|
|
-// TCoupledModel <
|
|
|
-// NullModel,
|
|
|
-// TType < RootModel >,
|
|
|
-// TName < NullModel >/*,
|
|
|
-// TState <
|
|
|
-// RootModel,
|
|
|
-// TName < AModel::_ix >,
|
|
|
-// TID < AModel::IX >,
|
|
|
-// TType < int >
|
|
|
-// > */
|
|
|
-// >
|
|
|
-// > ABuilder;
|
|
|
-
|
|
|
-// typedef Builder <
|
|
|
-// CoupledModel <
|
|
|
-// Type < RootModel >,
|
|
|
-// Name < >,
|
|
|
-// ID < >,
|
|
|
-// States < >,
|
|
|
-// Internals < >,
|
|
|
-// Externals < >,
|
|
|
-// Submodels <
|
|
|
-// AtomicModel <
|
|
|
-// Type < AModel >,
|
|
|
-// Name < RootModel::_a >,
|
|
|
-// ID < A >,
|
|
|
-// States < >,
|
|
|
-// Internals <
|
|
|
-// Variable < Name < AModel::_ix >, ID < IX >, Type < int > >,
|
|
|
-// Variable < Name < AModel::_bx >, ID < BX >, Type < bool > >,
|
|
|
-// Variable < Name < AModel::_dx >, ID < DX >, Type < double > >
|
|
|
-// >,
|
|
|
-// Externals < >,
|
|
|
-// >,
|
|
|
-// AtomicModel <
|
|
|
-// Type < BModel >,
|
|
|
-// Name < RootModel::_b >,
|
|
|
-// ID < B >,
|
|
|
-// States < >,
|
|
|
-// Internals < >,
|
|
|
-// Externals < >,
|
|
|
-// >
|
|
|
-// >
|
|
|
-// >
|
|
|
-// > ABuilder;
|
|
|
-
|
|
|
//TEST_CASE("Simulator_tests", "simple")
|
|
|
int main()
|
|
|
{
|
|
|
GlobalParameters globalParameters;
|
|
|
ModelParameters modelParameters;
|
|
|
- // ABuilder builder;
|
|
|
-
|
|
|
- // ABuilder builder("{ \"type\": \"RootModel\", " \
|
|
|
- // "\"name\": \"root\", " \
|
|
|
- // "\"states\": [ " \
|
|
|
- // "{ \"name\":\"_i\", \"type\":\"int\"} " \
|
|
|
- // "]," \
|
|
|
- // "\"submodels\": [ " \
|
|
|
- // "{ \"type\": \"AModel\", " \
|
|
|
- // "\"name\": \"_a\", " \
|
|
|
- // "\"internals\": [" \
|
|
|
- // "{ \"name\":\"_ix\", \"type\":\"int\"}, " \
|
|
|
- // "{ \"name\":\"_bx\", \"type\":\"bool\"}, " \
|
|
|
- // "{ \"name\":\"_dx\", \"type\":\"double\"} " \
|
|
|
- // "] }, " \
|
|
|
- // "{ \"type\": \"BModel\", " \
|
|
|
- // "\"name\": \"_b\", " \
|
|
|
- // "\"internals\": [" \
|
|
|
- // "{ \"name\":\"_iy\", \"type\":\"int\"}, " \
|
|
|
- // "{ \"name\":\"_by\", \"type\":\"bool\"}, " \
|
|
|
- // "{ \"name\":\"_dy\", \"type\":\"double\"} " \
|
|
|
- // "], " \
|
|
|
- // "\"externals\": [" \
|
|
|
- // "{ \"name\":\"_ix\", \"type\":\"int\"}, " \
|
|
|
- // "{ \"name\":\"_bx\", \"type\":\"bool\"}, " \
|
|
|
- // "{ \"name\":\"_dx\", \"type\":\"double\"} " \
|
|
|
- // "], " \
|
|
|
- // "\"states\": [ " \
|
|
|
- // "{ \"name\":\"_n\", \"type\":\"int\"} " \
|
|
|
- // "] }" \
|
|
|
- // "] }");
|
|
|
- // ASimulator simulator(builder(), globalParameters);
|
|
|
-
|
|
|
- // simulator.attachView("Root", new AView);
|
|
|
-
|
|
|
- // ::Trace::trace().clear();
|
|
|
-
|
|
|
- // simulator.init(0, modelParameters);
|
|
|
- // simulator.run(artis::utils::DateTime::toJulianDayNumber(2016, 1, 1),
|
|
|
- // artis::utils::DateTime::toJulianDayNumber(2016, 1, 10));
|
|
|
-
|
|
|
- // AnOutput output(simulator.observer());
|
|
|
|
|
|
- // output();
|
|
|
+ ABuilder builder("{ \"type\": \"RootModel\", " \
|
|
|
+ "\"name\": \"root\", " \
|
|
|
+ "\"states\": [ " \
|
|
|
+ "{ \"name\":\"_i\", \"type\":\"int\"} " \
|
|
|
+ "]," \
|
|
|
+ "\"submodels\": [ " \
|
|
|
+ "{ \"type\": \"AModel\", " \
|
|
|
+ "\"name\": \"_a\", " \
|
|
|
+ "\"internals\": [" \
|
|
|
+ "{ \"name\":\"_ix\", \"type\":\"int\"}, " \
|
|
|
+ "{ \"name\":\"_bx\", \"type\":\"bool\"}, " \
|
|
|
+ "{ \"name\":\"_dx\", \"type\":\"double\"} " \
|
|
|
+ "] }, " \
|
|
|
+ "{ \"type\": \"BModel\", " \
|
|
|
+ "\"name\": \"_b\", " \
|
|
|
+ "\"internals\": [" \
|
|
|
+ "{ \"name\":\"_iy\", \"type\":\"int\"}, " \
|
|
|
+ "{ \"name\":\"_by\", \"type\":\"bool\"}, " \
|
|
|
+ "{ \"name\":\"_dy\", \"type\":\"double\"} " \
|
|
|
+ "], " \
|
|
|
+ "\"externals\": [" \
|
|
|
+ "{ \"name\":\"_ix\", \"type\":\"int\"}, " \
|
|
|
+ "{ \"name\":\"_bx\", \"type\":\"bool\"}, " \
|
|
|
+ "{ \"name\":\"_dx\", \"type\":\"double\"} " \
|
|
|
+ "], " \
|
|
|
+ "\"states\": [ " \
|
|
|
+ "{ \"name\":\"_n\", \"type\":\"int\"} " \
|
|
|
+ "] }" \
|
|
|
+ "] }");
|
|
|
+
|
|
|
+ ASimulator simulator(builder.build(), globalParameters);
|
|
|
+
|
|
|
+ simulator.attachView("Root", new AView);
|
|
|
+
|
|
|
+ ::Trace::trace().clear();
|
|
|
+
|
|
|
+ simulator.init(0, modelParameters);
|
|
|
+ simulator.run(artis::utils::DateTime::toJulianDayNumber("2016-1-1"),
|
|
|
+ artis::utils::DateTime::toJulianDayNumber("2016-1-10"));
|
|
|
+
|
|
|
+ AnOutput output(simulator.observer());
|
|
|
+
|
|
|
+ output();
|
|
|
return 0;
|
|
|
}
|