]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
refs #5042
authorvillberg <villberg@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 30 Jun 2014 06:41:30 +0000 (06:41 +0000)
committervillberg <villberg@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 30 Jun 2014 06:41:30 +0000 (06:41 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@29763 ac1ea38d-2e2b-0410-8846-a27921b304fc

fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ElementwiseProduct.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Solver.java
org.simantics.sysdyn/scl/Simantics/Sysdyn.scl
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/OldSysdynExperiment.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiments.java
org.simantics.sysdyn/src/org/simantics/sysdyn/solver/InternalSolver.java

index db00150642e150cdacd494d2bcb0dc169e418cd0..68a0504bf4eecfed8c8a037c65c15309d95daf88 100644 (file)
@@ -13,18 +13,23 @@ package fi.semantum.sysdyn.solver;
 import java.util.ArrayList;\r
 import java.util.Collection;\r
 import java.util.Iterator;\r
+import java.util.List;\r
 import java.util.Map;\r
 \r
 public class Array implements IExpression {\r
 \r
        public static final Array FULL = new Array();\r
        \r
-       private ArrayList<Object> elements = new ArrayList<Object>();\r
+       final private ArrayList<Object> elements;\r
 \r
        public Array() {\r
-               \r
+               elements = new ArrayList<Object>();\r
        }\r
        \r
+       public Array(int size) {\r
+               elements = new ArrayList<Object>(size);\r
+       }\r
+\r
        public Array addElement(Object element) {\r
                if(element instanceof Constant) addElement(((Constant)element).value);\r
                else elements.add(element);\r
@@ -70,7 +75,7 @@ public class Array implements IExpression {
                return elements.get(index);\r
        }\r
        \r
-       public Collection<Object> elements() {\r
+       public List<Object> elements() {\r
                return elements;\r
        }\r
        \r
index e3198cfeca802c842bbd872682e2bce778559873..f87529c53fe4e52cb2d59ec303b05f27fa0347b1 100644 (file)
@@ -12,6 +12,7 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Collection;\r
 import java.util.Iterator;\r
+import java.util.List;\r
 import java.util.Map;\r
 \r
 public class ElementwiseProduct implements IExpression {\r
@@ -36,26 +37,24 @@ public class ElementwiseProduct implements IExpression {
                if(left instanceof Array && right instanceof Array) {\r
                        Array la = (Array)left;\r
                        Array ra = (Array)right;\r
-                       Collection<Object> lae = la.elements();\r
-                       Collection<Object> rae = ra.elements();\r
+                       List<Object> lae = la.elements();\r
+                       List<Object> rae = ra.elements();\r
                        if(lae.size() != rae.size()) throw new UnsupportedOperationException();\r
-                       Iterator<Object> li = lae.iterator();\r
-                       Iterator<Object> ri = rae.iterator();\r
-                       Array result = new Array();\r
-                       for(int i=0;i<lae.size();i++) {\r
-                               double ld = (Double)li.next();\r
-                               double rd = (Double)ri.next();\r
+                       Array result = new Array(lae.size());\r
+                       int index = 0;\r
+                       for(Object o : lae) {\r
+                               Double ld = (Double)o;\r
+                               Double rd = (Double)rae.get(index++);\r
                                result.addElement(ld*rd);\r
                        }\r
                        return result;\r
                } else if (left instanceof Array && right instanceof Double) {\r
                        Array la = (Array)left;\r
                        Double rd = (Double)right;\r
-                       Collection<Object> lae = la.elements();\r
-                       Iterator<Object> li = lae.iterator();\r
-                       Array result = new Array();\r
-                       for(int i=0;i<lae.size();i++) {\r
-                               double ld = (Double)li.next();\r
+                       List<Object> lae = la.elements();\r
+                       Array result = new Array(lae.size());\r
+                       for(Object o : lae) {\r
+                               Double ld = (Double)o;\r
                                result.addElement(ld*rd);\r
                        }\r
                        return result;\r
index b65afaff5e7bbb9bc1599e823b01a92b6b033e07..e03b297deb789137e6c772064b6109ec14084ae4 100644 (file)
@@ -42,6 +42,9 @@ public class Solver {
        private NodeCache cache;\r
        private boolean ready;\r
        \r
+       private boolean dirty;\r
+       private boolean started;\r
+       \r
        public Solver() {\r
                defaultStep = 0.1;\r
                start = 0;\r
@@ -105,23 +108,27 @@ public class Solver {
                });\r
                \r
        }\r
+\r
+       private SimpleNode n;\r
+       private String codeCache = null;\r
        \r
        public void prepare(String input) throws Exception {\r
                \r
                long startNanos = System.nanoTime();\r
                \r
-//             LineReader reader = new LineReader(input, cache);\r
-//             reader.parse();\r
-//             \r
-//             model = reader.model;\r
+               if(!input.equals(codeCache)) {\r
                \r
-               StringReader reader = new StringReader(input);\r
-               ModelParser modelParser = new ModelParser(reader);\r
-               SimpleNode n = (SimpleNode)modelParser.stored_definition();\r
+                       StringReader reader = new StringReader(input);\r
+                       ModelParser modelParser = new ModelParser(reader);\r
+                       n = (SimpleNode)modelParser.stored_definition();\r
+               \r
+               }\r
+\r
            Parser parser = new Parser();\r
            model = new Model(new Globals(), "", false);\r
                parser.walk(n, 0, model);\r
-               \r
+               codeCache = input;\r
+\r
                env = new Environment(model, defaultStep, start);\r
                \r
                int size = model.prepare();\r
@@ -237,6 +244,8 @@ public class Solver {
                env.initial = false;\r
                \r
                ready = true;\r
+               started = false;\r
+               dirty = false;\r
                \r
                long endNanos = System.nanoTime();\r
                \r
@@ -252,15 +261,67 @@ public class Solver {
                return env.getValueArray();\r
        }\r
        \r
+       public void aboutToRun() {\r
+\r
+               if(!started && dirty) {\r
+                       \r
+                       // Solve all algebraic equations\r
+                       Assignment[] assignments = model.assignmentArray;\r
+                       for(int i=0;i<assignments.length;i++) {\r
+                               if(assignments[i].isConstant) continue;\r
+                               Object value = assignments[i].expression.evaluate(env);\r
+                               assignments[i].target.assign(env, assignments[i].subscripts, value);\r
+                       }\r
+                       for(Assignment ass : model.initials) {\r
+                               if(ass.isConstant) continue;\r
+                               Object value = ass.expression.evaluate(env);\r
+                               ass.target.assign(env, ass.subscripts, value);\r
+                       }\r
+\r
+                       // Solve initial values\r
+                       for(VariableDeclaration vd : model.variables) {\r
+                               try {\r
+                                       for(Argument arg : vd.modification.args) {\r
+                                               if(arg.name.endsWith("start")) {\r
+                                                       Object value = arg.modification.evaluate(env);\r
+                                                       if(vd.variable.base.dimension() == 1) {\r
+                                                               vd.variable.assign(env, null, value);\r
+                                                       } else {\r
+                                                               if(value instanceof Double) {\r
+                                                                       Array array = new Array();\r
+                                                                       for(int i=0;i<vd.variable.base.dimension();i++) array.addElement(value);\r
+                                                                       vd.variable.assign(env, null, array);\r
+                                                               } else {\r
+                                                                       throw new IllegalStateException();\r
+                                                               }\r
+                                                       }\r
+                                               }\r
+                                       }\r
+                               } catch (Exception e) {\r
+                                       if(PRINT_EXCEPTIONS) {\r
+                                               e.printStackTrace();\r
+                                               System.err.println("failed to assign " + vd.variable.toString());\r
+                                       }\r
+                               }\r
+                       }\r
+                       \r
+                       dirty = false;\r
+                       \r
+               }\r
+               \r
+       }\r
+       \r
        public void step() {\r
                \r
 //             printEnvironment();\r
 //             \r
 //             new Exception().printStackTrace();\r
-\r
+               \r
                // TODO: do something more sensible if the solver is not ready\r
                if (!ready) return;\r
                \r
+               started = true;\r
+\r
                Assignment[] assignments = model.assignmentArray;\r
                Assignment[] derivatives = model.derivativeArray;\r
                \r
@@ -307,6 +368,7 @@ public class Solver {
        \r
        public void setValue(String key, double value) {\r
                env.setValue(key, value);\r
+               dirty = true;\r
        }\r
        \r
 }\r
index 12e7ef77425a5d194c3d4fbcfc5e81ca4caf3173..b75a31c8ab9562f15469bfb10cb2bf6a863902f0 100644 (file)
@@ -105,4 +105,5 @@ importJava "org.simantics.sysdyn.manager.SysdynExperiments" where
     applyIC :: Variable -> String -> <WriteGraph> ()\r
     deleteIC :: Variable -> String -> <WriteGraph> ()\r
 \r
+    setPublishResults :: Variable -> Boolean -> <ReadGraph> ()\r
     
\ No newline at end of file
index 326e8cbe52702437ad9c2b18221c1ad08eefd1f1..d9218f28a68912b0cd1b411e03d0eacc8b662d1e 100644 (file)
@@ -86,6 +86,8 @@ public class OldSysdynExperiment extends SysdynExperiment {
     protected static String                         omcHome = null;\r
 \r
     public static OldSysdynExperiment                                  INSTANCE;\r
+       \r
+       boolean publishResults = true;\r
 \r
     public OldSysdynExperiment(Resource experiment, Resource model) {\r
         super(experiment, model);\r
@@ -822,6 +824,11 @@ public class OldSysdynExperiment extends SysdynExperiment {
     public void resultsChanged() {\r
        resultsChanged(false);\r
     }\r
+    \r
+    public void setPublishResults(boolean value) {\r
+       publishResults = value;\r
+       if(publishResults) resultsChanged(true);\r
+    }\r
 \r
     public void resultsChanged(boolean force) {\r
         long time = System.nanoTime();\r
@@ -841,6 +848,7 @@ public class OldSysdynExperiment extends SysdynExperiment {
      */\r
     @Override\r
     public void updateSubscriptions() {\r
+       if(!publishResults) return;\r
         for(VariableValueSubscription subscription : getListenerSnapshot())\r
             subscription.update();\r
         skippedVariableUpdate = false;\r
index 78d7902c86be3c7bd289a5d91347dafa52dad4c7..aaa959a5960c27a3921ebb800a5a1843c4450a6d 100644 (file)
@@ -35,6 +35,22 @@ import org.simantics.utils.DataContainer;
 \r
 public class SysdynExperiments {\r
 \r
+       public static void setPublishResults(ReadGraph graph, Variable variable, boolean value) throws DatabaseException {\r
+\r
+        final IProject project = Simantics.getProject();\r
+        if (project == null) return;\r
+\r
+        final IExperimentManager experimentManager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
+        if(experimentManager == null) return;\r
+        \r
+        IExperiment exp = experimentManager.getExperiment(variable.getName(graph));\r
+        if(exp instanceof OldSysdynExperiment) {\r
+               OldSysdynExperiment experiment = (OldSysdynExperiment)exp;\r
+               experiment.setPublishResults(value);\r
+        }\r
+\r
+       }\r
+       \r
     public static String activateExperiment(IProgressMonitor monitor, final IProject project, final IExperimentManager manager, final Resource experimentResource) {\r
        \r
        if(monitor == null) monitor = new NullProgressMonitor();\r
index 6835e191eb061b9a06a78ddb85c9d37882ac2f42..e7cd7bf152d82444d40581e567057b5a1996c8b2 100644 (file)
@@ -197,6 +197,7 @@ public class InternalSolver implements ISolver {
        \r
        @Override\r
        public void prepareToStep() throws Exception {\r
+               solver.aboutToRun();\r
        }\r
        \r
        @Override\r