Explorar el Código

Add new methods for code simplifications

Eric Ramat hace 8 años
padre
commit
b566501363

+ 28 - 0
src/artis/kernel/AbstractAtomicModel.hpp

@@ -24,6 +24,7 @@
 #define __ARTIS_KERNEL_ABSTRACT_ATOMIC_MODEL_HPP
 
 #include <artis/kernel/AbstractModel.hpp>
+#include <artis/kernel/AbstractCoupledModel.hpp>
 #include <artis/kernel/Internals.hpp>
 #include <artis/kernel/States.hpp>
 #include <artis/utils/Exception.hpp>
@@ -168,6 +169,33 @@ private:
     // std::vector < std::vector < double > T::* > externalsV;
 };
 
+template < typename T, typename U, typename V >
+struct OUT_SA_t
+{
+    OUT_SA_t(double t, AbstractAtomicModel < T, U, V >* model,
+             unsigned int index) : t(t), model(model), index(index)
+    { }
+
+    double t;
+    AbstractAtomicModel < T, U, V >* model;
+    unsigned int index;
+};
+
+template < typename T, typename U, typename V >
+OUT_SA_t < T, U, V > OUT(double t,
+                            AbstractAtomicModel < T, U, V >* model,
+                            unsigned int index)
+{ return OUT_SA_t < T, U, V >(t, model, index); }
+
+template < typename T1, typename U1, typename V1, typename W1,
+           typename T2, typename U2, typename V2 >
+void operator>>(OUT_SA_t < T2, U2, V2 > out, IN_t < T1, U1, V1, W1 > in)
+{
+    if (out.model->is_computed(out.t, out.index)) {
+        in.model->put(out.t, in.index, out.model->get(out.t, out.index));
+    }
+}
+
 } }
 
 #endif

+ 99 - 0
src/artis/kernel/AbstractCoupledModel.hpp

@@ -45,6 +45,13 @@ class AbstractCoupledModel : public AbstractModel < U, V >,
     typedef std::map < int, std::pair < type*,
                                         int > > SubModelInternals;
 
+    struct Var
+    {
+        unsigned int index;
+        type* model;
+        int sub_index;
+    };
+
 public:
     AbstractCoupledModel()
     { }
@@ -212,15 +219,46 @@ protected:
     void internal(unsigned int index, double T::* var)
     { Internals < T, U >::internal(index, var); }
 
+    void I(std::initializer_list < std::pair < unsigned int,
+           bool T::* > > internals)
+    { Internals < T, U >::I(internals); }
+
+    void I(std::initializer_list < std::pair < unsigned int,
+           int T::* > > internals)
+    { Internals < T, U >::I(internals); }
+
+    void I(std::initializer_list < std::pair < unsigned int,
+           double T::* > > internals)
+    { Internals < T, U >::I(internals); }
+
     void internal(unsigned int index, type* model, int sub_index)
     {
         submodel_internals[index] =
             std::pair < type*, int >(model, sub_index);
     }
 
+    void I(std::initializer_list < Var > internals)
+    {
+        for (typename std::initializer_list < Var >::iterator it =
+                 internals.begin(); it != internals.end(); ++it) {
+            submodel_internals[it->index] =
+                std::pair < type*, int >(it->model, it->sub_index);
+        }
+    }
+
     void submodel(unsigned int index, type* model)
     { submodels[index] = model; }
 
+    void S(std::initializer_list < std::pair <  unsigned int,
+           type* > > models)
+    {
+        for (typename std::initializer_list < std::pair <  unsigned int,
+                 type* > >::iterator it =
+                 models.begin(); it != models.end(); ++it) {
+            submodels[it->first] = it->second;
+        }
+    }
+
     void setsubmodel(unsigned int index, type* model)
     {
         if (setsubmodels.find(index) == setsubmodels.end()) {
@@ -235,6 +273,67 @@ private:
     Setsubmodels setsubmodels;
 };
 
+template < typename T, typename U, typename V, typename W >
+struct IN_t
+{
+    IN_t(AbstractCoupledModel < T, U, V, W >* model,
+         unsigned int index) : model(model), index(index)
+    { }
+
+    AbstractCoupledModel < T, U, V, W >* model;
+    unsigned int index;
+};
+
+template < typename T, typename U, typename V, typename W >
+struct OUT_SC_t
+{
+    OUT_SC_t(double t, AbstractCoupledModel < T, U, V, W >* model,
+             unsigned int index) : t(t), model(model), index(index)
+    { }
+
+    double t;
+    AbstractCoupledModel < T, U, V, W >* model;
+    unsigned int index;
+};
+
+template < typename T >
+struct OUT_A_t
+{
+    OUT_A_t(double t, T value) : t(t), value(value)
+    { }
+
+    double t;
+    T value;
+};
+
+template < typename T, typename U, typename V, typename W >
+IN_t < T, U, V, W > IN(AbstractCoupledModel < T, U, V, W >* model,
+                       unsigned int index)
+{ return IN_t < T, U, V, W >(model, index); }
+
+template < typename T, typename U, typename V, typename W >
+OUT_SC_t < T, U, V, W > OUT(double t,
+                            AbstractCoupledModel < T, U, V, W >* model,
+                            unsigned int index)
+{ return OUT_SC_t < T, U, V, W >(t, model, index); }
+
+template < typename T >
+OUT_A_t < T > OUT(double t, T value)
+{ return OUT_A_t < T >(t, value); }
+
+template < typename T1, typename U1, typename V1, typename W1,
+           typename T2, typename U2, typename V2, typename W2 >
+void operator>>(OUT_SC_t < T2, U2, V2, W2 > out, IN_t < T1, U1, V1, W1 > in)
+{
+    if (out.model->is_computed(out.t, out.index)) {
+        in.model->put(out.t, in.index, out.model->get(out.t, out.index));
+    }
+}
+
+template < typename T, typename U, typename V, typename W, typename Z >
+void operator>>(OUT_A_t < Z > out, IN_t < T, U, V, W > in)
+{ in.model->put(out.t, in.index, out.value); }
+
 } }
 
 #endif

+ 52 - 10
src/artis/kernel/Externals.hpp

@@ -31,6 +31,11 @@ namespace artis { namespace kernel {
 template < typename T, typename U >
 class Externals
 {
+    struct Var
+    {
+
+    };
+
 public:
     Externals() : updated(false)
     { }
@@ -41,17 +46,56 @@ public:
     bool check(typename U::type t) const
     {
         bool OK = true;
-        // typename std::vector <
-        //     std::pair < double,
-        //                 double T::* > >::const_iterator it = externalsD.begin();
-
-        // while (it != externalsD.end() and OK) {
-        //     OK = it->first == t;
-        //     ++it;
-        // }
+        typename std::vector <
+            std::pair < double,
+                        double T::* > >::const_iterator it = externalsD.begin();
+
+        while (it != externalsD.end() and OK) {
+            OK = it->first == t;
+            ++it;
+        }
         return OK;
     }
 
+    void E(std::initializer_list < std::pair < unsigned int,
+           bool T::* > > externals)
+    {
+        for (typename std::initializer_list < std::pair < unsigned int,
+                 bool T::* > >::iterator it =
+                 externals.begin(); it != externals.end(); ++it) {
+            if (externalsB.size() <= it->first) {
+                externalsB.resize(it->first + 1);
+            }
+            externalsB[it->first] = std::make_pair(-1, it->second);
+        }
+    }
+
+    void E(std::initializer_list < std::pair < unsigned int,
+           int T::* > > externals)
+    {
+        for (typename std::initializer_list < std::pair < unsigned int,
+                 int T::* > >::iterator it =
+                 externals.begin(); it != externals.end(); ++it) {
+            if (externalsI.size() <= it->first) {
+                externalsI.resize(it->first + 1);
+            }
+            externalsI[it->first] = std::make_pair(-1, it->second);
+        }
+    }
+
+    void E(std::initializer_list < std::pair < unsigned int,
+           double T::* > > externals)
+    {
+        for (typename std::initializer_list < std::pair < unsigned int,
+                 double T::* > >::iterator it =
+                 externals.begin(); it != externals.end(); ++it) {
+            if (externalsD.size() <= it->first) {
+                externalsD.resize(it->first + 1);
+            }
+            externalsD[it->first] = std::make_pair(-1, it->second);
+        }
+    }
+
     void externalB(unsigned int index, bool T::* var)
     {
         if (externalsB.size() <= index) {
@@ -126,8 +170,6 @@ public:
 
 protected:
     bool updated;
-
-private:
     std::vector < std::pair < typename U::type, bool T::* > > externalsB;
     std::vector < std::pair < typename U::type, int T::* > > externalsI;
     std::vector < std::pair < typename U::type, double T::* > > externalsD;

+ 39 - 0
src/artis/kernel/Internals.hpp

@@ -47,6 +47,45 @@ public:
     double T::* get(unsigned int index) const
     { return internalsD.at(index); }
 
+    void I(std::initializer_list < std::pair < unsigned int,
+           bool T::* > > internals)
+    {
+        for (typename std::initializer_list < std::pair < unsigned int,
+                 bool T::* > >::iterator it =
+                 internals.begin(); it != internals.end(); ++it) {
+            if (internalsB.size() <= it->first) {
+                internalsB.resize(it->first + 1);
+            }
+            internalsB[it->first] = it->second;
+        }
+    }
+
+    void I(std::initializer_list < std::pair < unsigned int,
+           int T::* > > internals)
+    {
+        for (typename std::initializer_list < std::pair < unsigned int,
+                 int T::* > >::iterator it =
+                 internals.begin(); it != internals.end(); ++it) {
+            if (internalsI.size() <= it->first) {
+                internalsI.resize(it->first + 1);
+            }
+            internalsI[it->first] = it->second;
+        }
+    }
+
+    void I(std::initializer_list < std::pair < unsigned int,
+           double T::* > > internals)
+    {
+        for (typename std::initializer_list < std::pair < unsigned int,
+                 double T::* > >::iterator it =
+                 internals.begin(); it != internals.end(); ++it) {
+            if (internalsD.size() <= it->first) {
+                internalsD.resize(it->first + 1);
+            }
+            internalsD[it->first] = it->second;
+        }
+    }
+
     void internalB(unsigned int index, bool T::* var)
     {
         if (internalsB.size() <= index) {

+ 5 - 5
src/test/test.cpp

@@ -57,14 +57,14 @@ public:
     virtual ~AModel()
     { }
 
-    void compute(double t, bool update)
+    void compute(double /* t */, bool /* update */)
     {
         ++_ix;
         _bx = not _bx;
         ++_dx;
     }
 
-    void init(double t, const ModelParameters& parameters)
+    void init(double /* t */, const ModelParameters& /* parameters */)
     {
         _ix = 0;
         _bx = false;
@@ -101,7 +101,7 @@ public:
     virtual ~BModel()
     { }
 
-    void compute(double t, bool update)
+    void compute(double /* t */, bool update)
     {
 
         if (update)
@@ -112,7 +112,7 @@ public:
         _dy = _dx + 1;
     }
 
-    void init(double t, const ModelParameters& parameters)
+    void init(double /* t */, const ModelParameters& /* parameters */)
     {
         _iy = 0;
         _by = false;
@@ -149,7 +149,7 @@ public:
     virtual ~RootModel()
     { }
 
-    void compute(double t, bool update)
+    void compute(double t, bool /* update */)
     {
         _a(t);
         _b.put(t, BModel::IX, _a.getI(t, AModel::IX));