]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Add a special "time" variable to the solver that always refers to the current simulat...
authorjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 24 Oct 2013 12:30:56 +0000 (12:30 +0000)
committerjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 24 Oct 2013 12:30:56 +0000 (12:30 +0000)
refs #4488

git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@28116 ac1ea38d-2e2b-0410-8846-a27921b304fc

fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Declaration.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Solver.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/TimeVariable.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/solver/InternalSolver.java

index 6dd7532ab846641e3c67ce478249dac7c1b80980..cb04716a76970b2d526f1bcaa5d5ae2818f5e15e 100644 (file)
@@ -1,14 +1,8 @@
 package fi.semantum.sysdyn.solver;\r
 \r
-import java.util.Arrays;\r
-\r
 public class Declaration implements IExpression {\r
 \r
        public Variable variable;\r
-       \r
-//     public String name;\r
-//     public int[] subscripts;\r
-       \r
        public Object modification;\r
        \r
        public Declaration(Variable variable, Object modification) {\r
index f2ab66bcc90649884869bf8edc33622f4ee8c65d..d6beb34f4cb490287dbff7c1603d547d9a260dca 100644 (file)
@@ -41,17 +41,17 @@ public class Environment implements IEnvironment, ISystem {
        \r
        public Model model;\r
        public final double step;\r
-       public double time = 0;\r
+       public double time;\r
        public boolean initial = true;\r
        \r
        public Object[] valueTable;\r
 \r
        HashMap<String,TreeMap<Double,Double>> history = new HashMap<String,TreeMap<Double,Double>>();\r
 \r
-       public Environment(Model model, double step) {\r
-               \r
+       public Environment(Model model, double step, double start) {\r
                this.model = model;\r
                this.step = step;\r
+               this.time = start;\r
                \r
                model.functions.put("size", new Fn1() {\r
 \r
@@ -287,8 +287,9 @@ public class Environment implements IEnvironment, ISystem {
                        values.put(v.toString(), (Double)getValue(v.index(this)));\r
                }\r
                \r
-               // not completely sure if these should be included or what else should\r
-               // be included in the results\r
+               // NOTE: there is room for optimization as parameter values that do not\r
+               // change should only be obtained once (and parameter values that do\r
+               // change are (possibly) included in assignments or derivatives)\r
                for (ParameterDeclaration p : model.parameters) {\r
                        Variable v = p.variable;\r
                        values.put(v.toString(), (Double)getValue(v.index(this)));\r
index fbdc6cc6473e60a61778fd11540d491db5a7cdc0..55710051402ebab386fff08bed2c06c7945cd4e0 100644 (file)
@@ -269,6 +269,9 @@ public class Parser {
                        if(n.jjtGetNumChildren() == 1) {\r
                                return new Variable(currentFrame, n.op, (IExpression[])walk((SimpleNode)n.jjtGetChild(0), indent+2, model));\r
                        } else {\r
+                               if ("time".equals(n.op)) {\r
+                                       return new TimeVariable();\r
+                               }\r
                                return new Variable(currentFrame, n.op, null);\r
                        }\r
                case relation:\r
index dfcbcc893a6a0147ff5aa4ebd563268c5c8cb0d2..b4e55d27c44ec964c91d4323c8f6ca41eb6065c1 100644 (file)
@@ -14,11 +14,13 @@ public class Solver {
        private Object[] newValues;\r
        \r
        private double step;\r
+       private double start;\r
        private NodeCache cache;\r
        private boolean ready;\r
        \r
        public Solver() {\r
                step = 0.1;\r
+               start = 0;\r
                // is it okay to store the cache with the solver instance?\r
                cache = new NodeCache();\r
                ready = false;\r
@@ -29,12 +31,17 @@ public class Solver {
                ready = false;\r
        }\r
        \r
+       public void setStart(double start) {\r
+               this.start = start;\r
+               ready = false;\r
+       }\r
+       \r
        public void prepare(String input) throws Exception {\r
                LineReader reader = new LineReader(input, cache);\r
                reader.parse();\r
                \r
                model = reader.model;\r
-               env = new Environment(model, step);\r
+               env = new Environment(model, step, start);\r
                \r
                int size = model.prepare();\r
                \r
@@ -136,6 +143,9 @@ public class Solver {
                Assignment[] assignments = model.assignmentArray;\r
                Assignment[] derivatives = model.derivativeArray;\r
                \r
+               // should probably be wrapped inside a method or something\r
+               env.time += env.step;\r
+               \r
                // Solve algebraic equations\r
                for(int i=0;i<assignments.length;i++) {\r
                        newValues[i] = assignments[i].expression.evaluate(env);\r
@@ -151,7 +161,6 @@ public class Solver {
                for(int i=0;i<model.derivatives.size();i++) {\r
                        derivatives[i].target.assign(env, newValues[i]);\r
                }\r
-               \r
        }\r
        \r
        // TODO: implement some on the fly parameter change stuff for different experiment types\r
diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/TimeVariable.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/TimeVariable.java
new file mode 100644 (file)
index 0000000..4049f7f
--- /dev/null
@@ -0,0 +1,16 @@
+package fi.semantum.sysdyn.solver;\r
+\r
+public class TimeVariable implements IExpression {\r
+\r
+       @Override\r
+       public Object evaluate(IEnvironment environment) {\r
+               // possibly a bit ugly\r
+               return environment.getSystem().time();\r
+       }\r
+\r
+       @Override\r
+       public String toString() {\r
+               return "time";\r
+       }\r
+       \r
+}\r
index 66e18ee64ffc861574bf49fd0d0c422a4320f78e..db1f4eb92722193b86abbf478ada062b0952d689 100644 (file)
@@ -85,6 +85,7 @@ public class InternalSolver implements ISolver {
                        interval = step;\r
                \r
                solver.setStep(step);\r
+               solver.setStart(start);\r
        }\r
 \r
        @Override\r