Parcourir la source

initial commit

Julien Dehos il y a 6 ans
commit
68cbf92f89
8 fichiers modifiés avec 537 ajouts et 0 suppressions
  1. 3 0
      .gitignore
  2. 5 0
      Makefile
  3. 32 0
      README.md
  4. 217 0
      models.hpp
  5. 16 0
      modules/artis
  6. 12 0
      oar.sh
  7. 109 0
      test-context.cpp
  8. 143 0
      test.cpp

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+.*~
+test.out
+test-context.out

+ 5 - 0
Makefile

@@ -0,0 +1,5 @@
+CXXFLAGS = -std=c++11 `pkg-config --cflags artis-1.0`
+LDFLAGS = `pkg-config --libs artis-1.0` -lboost_serialization
+all: test.out test-context.out
+%.out: %.cpp
+	$(CXX) $(CXXFLAGS) -o $@ $(LDFLAGS) $< 

+ 32 - 0
README.md

@@ -0,0 +1,32 @@
+
+```
+ml purge
+
+mkdir -p ~/tmp
+cd ~/tmp
+wget https://dl.bintray.com/boostorg/release/1.64.0/source/boost_1_64_0.tar.gz
+tar zxf boost_1_64_0.tar.gz
+cd boost_1_64_0
+./bootstrap.sh --prefix=${HOME}/opt/artis
+./b2 -j16 install
+
+cd ~/tmp
+git clone https://gogs.univ-littoral.fr/jdehos/test_artis
+cd test_artis
+cp -R modules ~/opt/artis/
+module use ~/opt/artis/modules
+module load artis 
+
+cd ~/tmp
+git clone https://gogs.univ-littoral.fr/devs-lab/artis
+mkdir -p artis/build
+cd artis/build
+BOOST_ROOT=${HOME}/opt/artis cmake -DCMAKE_INSTALL_PREFIX=${HOME}/opt/artis ..
+make -j16 install
+
+cd ~/tmp/test_artis
+make
+
+oarsub -S ./oar.sh
+```
+

+ 217 - 0
models.hpp

@@ -0,0 +1,217 @@
+/**
+ * @file test/models.hpp
+ * @author See the AUTHORS file
+ */
+
+/*
+ * Copyright (C) 2012-2017 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 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()
+    {
+        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, artis::utils::COMPUTE);
+        ::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, BY, DY, IZ };
+    enum states { N };
+
+    BModel()
+    {
+        // 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 */)
+    {
+        _iy = _ix + 1;
+        _by = not _bx;
+        _dy = _dx + 1;
+        ++_n;
+
+        ::Trace::trace() << ::TraceElement("B", t, artis::utils::COMPUTE);
+        ::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() : _a(new AModel), _b(new BModel)
+    {
+        // submodels
+        Submodels(((A, _a.get()), (B, _b.get())));
+
+        // internals
+        Internal(DX, &RootModel::_dx);
+        InternalsS(((IY, _b.get(), BModel::IY), (IZ, _b.get(), BModel::IZ)));
+        InternalS(BY, _b.get(), BModel::BY);
+
+        // states
+        States(int, ((N, &RootModel::_n)));
+        // State(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, artis::utils::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;
+
+    //internals
+    double _dx;
+
+    // states
+    int _n;
+};
+
+#endif

+ 16 - 0
modules/artis

@@ -0,0 +1,16 @@
+#%Module -*- tcl -*-
+
+proc ModulesHelp { } {
+        puts stderr "artis" 
+}
+
+module-whatis "ARTIS Discrete time Modelling and Simulation tools"
+
+set          ARTIS_ROOT       "$::env(HOME)/opt/artis"
+
+append-path  CPATH            $ARTIS_ROOT/include
+append-path  PATH             $ARTIS_ROOT/bin
+append-path  LD_LIBRARY_PATH  $ARTIS_ROOT/lib
+append-path  LIBRARY_PATH     $ARTIS_ROOT/lib
+append-path  PKG_CONFIG_PATH  $ARTIS_ROOT/lib/pkgconfig
+

+ 12 - 0
oar.sh

@@ -0,0 +1,12 @@
+#!/bin/sh
+
+#OAR -l /core=2,walltime=01:00:00
+
+. /nfs/opt/env/env.sh
+module use ~/opt/artis/modules
+module load artis 
+
+./test.out&
+./test-context.out
+wait
+

+ 109 - 0
test-context.cpp

@@ -0,0 +1,109 @@
+/**
+ * @file test/test-context.cpp
+ * @author See the AUTHORS file
+ */
+
+/*
+ * Copyright (C) 2012-2017 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/>.
+ */
+
+//#define CATCH_CONFIG_MAIN
+//#include <test/catch.hpp>
+
+#include "models.hpp"
+
+#include <artis/kernel/Simulator.hpp>
+#include <artis/utils/DateTime.hpp>
+
+#include <boost/archive/binary_oarchive.hpp>
+#include <boost/archive/binary_iarchive.hpp>
+
+#include <fstream>
+
+using namespace artis::kernel;
+
+typedef artis::kernel::Simulator < RootModel,
+                                   artis::utils::DoubleTime,
+                                   ModelParameters,
+                                   GlobalParameters > ASimulator;
+typedef artis::context::Context < artis::utils::DoubleTime > AContext;
+
+int main()
+{
+    GlobalParameters globalParameters;
+    ModelParameters modelParameters;
+
+    AContext context(artis::utils::DateTime::toJulianDayNumber("2016-1-1"),
+                     artis::utils::DateTime::toJulianDayNumber("2016-1-5"));
+
+    {
+        ASimulator simulator(new RootModel, globalParameters);
+
+        ::Trace::trace().clear();
+
+        simulator.init(artis::utils::DateTime::toJulianDayNumber("2016-1-1"),
+                       modelParameters);
+        simulator.run(context);
+
+        std::cout << ::Trace::trace().elements().to_string() << std::endl;
+
+        simulator.save(context);
+
+        std::ofstream os("state");
+        boost::archive::binary_oarchive oa(os);
+
+        oa << context;
+    }
+
+    std::cout << "==== PAUSE ====" << std::endl;
+
+    {
+        AContext new_context(context);
+        ASimulator simulator(new RootModel, globalParameters);
+
+        ::Trace::trace().clear();
+        new_context.begin(
+            artis::utils::DateTime::toJulianDayNumber("2016-1-6"));
+        new_context.end(
+            artis::utils::DateTime::toJulianDayNumber("2016-1-10"));
+        simulator.run(new_context);
+
+        std::cout << ::Trace::trace().elements().to_string() << std::endl;
+    }
+
+    std::cout << "==== PAUSE ====" << std::endl;
+
+    {
+        AContext new_context;
+        ASimulator simulator(new RootModel, globalParameters);
+
+        std::ifstream is("state");
+        boost::archive::binary_iarchive ia(is);
+
+        ia >> new_context;
+
+        ::Trace::trace().clear();
+        new_context.begin(
+            artis::utils::DateTime::toJulianDayNumber("2016-1-6"));
+        new_context.end(
+            artis::utils::DateTime::toJulianDayNumber("2016-1-10"));
+        simulator.run(new_context);
+
+        std::cout << ::Trace::trace().elements().to_string() << std::endl;
+    }
+
+    return 0;
+}

+ 143 - 0
test.cpp

@@ -0,0 +1,143 @@
+/**
+ * @file test/test.cpp
+ * @author See the AUTHORS file
+ */
+
+/*
+ * Copyright (C) 2012-2017 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/>.
+ */
+
+//#define CATCH_CONFIG_MAIN
+//#include <test/catch.hpp>
+
+#include "models.hpp"
+
+#include <artis/builder/Builder.hpp>
+#include <artis/builder/ModelFactory.hpp>
+#include <artis/kernel/Simulator.hpp>
+#include <artis/observer/Output.hpp>
+
+#include <artis/utils/DateTime.hpp>
+
+using namespace artis::kernel;
+using namespace artis::builder;
+
+using Creator = ObjectCreator < Model >;
+using Factory = ModelFactory < Model, int, Creator >;
+
+DECLARE_MODEL(AModel, ::Model, ::Factory);
+DECLARE_MODEL(BModel, ::Model, ::Factory);
+DECLARE_MODEL(RootModel, ::Model, ::Factory);
+
+typedef artis::kernel::Simulator < RootModel,
+                                   artis::utils::DoubleTime,
+                                   ModelParameters,
+                                   GlobalParameters > ASimulator;
+
+typedef artis::builder::Builder < ::Factory,
+                                  RootModel,
+                                  artis::utils::DoubleTime,
+                                  ModelParameters,
+                                  GlobalParameters > ABuilder;
+
+typedef artis::context::Context < artis::utils::DoubleTime > AContext;
+
+class AView : public artis::observer::View < artis::utils::DoubleTime,
+                                             ModelParameters >
+{
+public:
+    AView()
+    {
+        selector("A::I", INT, { RootModel::A, AModel::IX });
+        selector("A::B", BOOL, { RootModel::A, AModel::BX });
+        selector("A::D", DOUBLE, { RootModel::A, AModel::DX });
+        selector("B::I", INT, { RootModel::B, BModel::IY });
+        selector("B::B", BOOL, { RootModel::B, BModel::BY });
+        selector("B::D", DOUBLE, { RootModel::B, BModel::DY });
+    }
+
+    virtual ~AView()
+    { }
+};
+
+typedef artis::observer::Output < artis::utils::DoubleTime,
+                                  ModelParameters > AnOutput;
+
+//TEST_CASE("Simulator_tests", "simple")
+int main()
+{
+    GlobalParameters globalParameters;
+    ModelParameters modelParameters;
+
+    /**********************/
+
+    // AModel a;
+    // artis::kernel::Any v(&AModel::_ix);
+
+    // a.init(0, modelParameters);
+
+    // artis::kernel::to_value < AModel >(v, &a);
+
+    /**********************/
+
+    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\"} "         \
+                     "] }"                                           \
+                     "] }");
+
+    AContext context(artis::utils::DateTime::toJulianDayNumber("2016-1-1"),
+                     artis::utils::DateTime::toJulianDayNumber("2016-1-10"));
+    ASimulator simulator(builder.build(), globalParameters);
+
+    simulator.attachView("Root", new AView);
+
+    ::Trace::trace().clear();
+
+    simulator.init(0, modelParameters);
+    simulator.run(context);
+
+    std::cout << ::Trace::trace().elements().to_string() << std::endl;
+
+    AnOutput output(simulator.observer());
+
+    output();
+    return 0;
+}