|
@@ -20,18 +20,21 @@
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
*/
|
|
|
|
|
|
-#define CATCH_CONFIG_MAIN
|
|
|
-#include <test/catch.hpp>
|
|
|
+//#define CATCH_CONFIG_MAIN
|
|
|
+//#include <test/catch.hpp>
|
|
|
|
|
|
#include <artis/kernel/AbstractAtomicModel.hpp>
|
|
|
#include <artis/kernel/AbstractCoupledModel.hpp>
|
|
|
-#include <artis/kernel/Builder.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
|
|
|
{ };
|
|
|
|
|
@@ -42,9 +45,19 @@ 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:
|
|
@@ -62,11 +75,15 @@ public:
|
|
|
virtual ~AModel()
|
|
|
{ }
|
|
|
|
|
|
- void compute(double /* t */, bool /* update */)
|
|
|
+ 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 */)
|
|
@@ -82,6 +99,8 @@ private:
|
|
|
double _dx;
|
|
|
};
|
|
|
|
|
|
+DECLARE_MODEL(AModel, ::Model, ::Factory);
|
|
|
+
|
|
|
class BModel : public AtomicModel < BModel >
|
|
|
{
|
|
|
public:
|
|
@@ -109,7 +128,7 @@ public:
|
|
|
virtual ~BModel()
|
|
|
{ }
|
|
|
|
|
|
- void compute(double /* t */, bool update)
|
|
|
+ void compute(double t, bool update)
|
|
|
{
|
|
|
if (update)
|
|
|
std::cout << "UPDATE" << std::endl;
|
|
@@ -118,6 +137,10 @@ public:
|
|
|
_by = not _bx;
|
|
|
_dy = _dx + 1;
|
|
|
++_n;
|
|
|
+
|
|
|
+ ::Trace::trace() << ::TraceElement("B", t, COMPUTE);
|
|
|
+ ::Trace::trace().flush();
|
|
|
+
|
|
|
}
|
|
|
|
|
|
void init(double /* t */, const ModelParameters& /* parameters */)
|
|
@@ -143,20 +166,18 @@ private:
|
|
|
int _n;
|
|
|
};
|
|
|
|
|
|
-class RootModel :
|
|
|
- public artis::kernel::AbstractCoupledModel < RootModel,
|
|
|
- artis::utils::DoubleTime,
|
|
|
- ModelParameters,
|
|
|
- GlobalParameters >
|
|
|
+DECLARE_MODEL(BModel, ::Model, ::Factory);
|
|
|
+
|
|
|
+class RootModel : public CoupledModel < RootModel >
|
|
|
{
|
|
|
public:
|
|
|
enum submodels { A, B };
|
|
|
enum states { N };
|
|
|
|
|
|
- RootModel()
|
|
|
+ RootModel() : _a(new AModel), _b(new BModel)
|
|
|
{
|
|
|
// submodels
|
|
|
- S({ { A, &_a }, { B, &_b } });
|
|
|
+ S({ { A, _a.get() }, { B, _b.get() } });
|
|
|
|
|
|
// states
|
|
|
S < int >({ { N, &RootModel::_n } });
|
|
@@ -167,41 +188,47 @@ public:
|
|
|
|
|
|
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);
|
|
|
+ (*_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);
|
|
|
+ _a->init(t, parameters);
|
|
|
+ _b->init(t, parameters);
|
|
|
_n = 0;
|
|
|
}
|
|
|
|
|
|
private:
|
|
|
// submodels
|
|
|
- AModel _a;
|
|
|
- BModel _b;
|
|
|
+ std::unique_ptr < AModel > _a;
|
|
|
+ std::unique_ptr < BModel > _b;
|
|
|
|
|
|
// states
|
|
|
int _n;
|
|
|
};
|
|
|
|
|
|
+DECLARE_MODEL(RootModel, ::Model, ::Factory);
|
|
|
+
|
|
|
typedef artis::kernel::Simulator < RootModel,
|
|
|
artis::utils::DoubleTime,
|
|
|
ModelParameters,
|
|
|
GlobalParameters > ASimulator;
|
|
|
|
|
|
-typedef artis::kernel::Builder < 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 >
|
|
@@ -224,26 +251,183 @@ public:
|
|
|
typedef artis::observer::Output < artis::utils::DoubleTime,
|
|
|
ModelParameters > AnOutput;
|
|
|
|
|
|
-TEST_CASE("Simulator_tests", "simple")
|
|
|
+struct NullModel : CoupledModel < NullModel >
|
|
|
+{ };
|
|
|
+
|
|
|
+template < typename T >
|
|
|
+struct TType
|
|
|
{
|
|
|
- GlobalParameters globalParameters;
|
|
|
- ModelParameters modelParameters;
|
|
|
- ABuilder builder("{ \"type\": \"RootModel\", " \
|
|
|
- "\"submodels\": [ " \
|
|
|
- "{ \"type\": \"AModel\", \"internals\": [] }, " \
|
|
|
- "{ \"type\": \"BModel\", \"internals\": [] } " \
|
|
|
- "] }");
|
|
|
- ASimulator simulator(new RootModel, globalParameters);
|
|
|
+ typedef T type;
|
|
|
|
|
|
- simulator.attachView("Root", new AView);
|
|
|
+ T* operator()()
|
|
|
+ { return new T(); }
|
|
|
+};
|
|
|
|
|
|
- artis::utils::Trace < artis::utils::DoubleTime >::trace().clear();
|
|
|
+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;
|
|
|
|
|
|
- simulator.init(0, modelParameters);
|
|
|
- simulator.run(artis::utils::DateTime::toJulianDayNumber(2016, 1, 1),
|
|
|
- artis::utils::DateTime::toJulianDayNumber(2016, 1, 10));
|
|
|
+ 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;
|
|
|
|
|
|
- AnOutput output(simulator.observer());
|
|
|
+ return m(nullptr);
|
|
|
+ }
|
|
|
+};
|
|
|
|
|
|
- output();
|
|
|
+// 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();
|
|
|
+ return 0;
|
|
|
}
|