From: jkauttio Date: Thu, 24 Oct 2013 12:30:56 +0000 (+0000) Subject: Add a special "time" variable to the solver that always refers to the current simulat... X-Git-Tag: 1.8.1~224 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=5870c937177f4ee6b3ac0b5e4fb2789ee00f4a01;p=simantics%2Fsysdyn.git Add a special "time" variable to the solver that always refers to the current simulation time. refs #4488 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@28116 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Declaration.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Declaration.java index 6dd7532a..cb04716a 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Declaration.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Declaration.java @@ -1,14 +1,8 @@ package fi.semantum.sysdyn.solver; -import java.util.Arrays; - public class Declaration implements IExpression { public Variable variable; - -// public String name; -// public int[] subscripts; - public Object modification; public Declaration(Variable variable, Object modification) { diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java index f2ab66bc..d6beb34f 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java @@ -41,17 +41,17 @@ public class Environment implements IEnvironment, ISystem { public Model model; public final double step; - public double time = 0; + public double time; public boolean initial = true; public Object[] valueTable; HashMap> history = new HashMap>(); - public Environment(Model model, double step) { - + public Environment(Model model, double step, double start) { this.model = model; this.step = step; + this.time = start; model.functions.put("size", new Fn1() { @@ -287,8 +287,9 @@ public class Environment implements IEnvironment, ISystem { values.put(v.toString(), (Double)getValue(v.index(this))); } - // not completely sure if these should be included or what else should - // be included in the results + // NOTE: there is room for optimization as parameter values that do not + // change should only be obtained once (and parameter values that do + // change are (possibly) included in assignments or derivatives) for (ParameterDeclaration p : model.parameters) { Variable v = p.variable; values.put(v.toString(), (Double)getValue(v.index(this))); diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java index fbdc6cc6..55710051 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java @@ -269,6 +269,9 @@ public class Parser { if(n.jjtGetNumChildren() == 1) { return new Variable(currentFrame, n.op, (IExpression[])walk((SimpleNode)n.jjtGetChild(0), indent+2, model)); } else { + if ("time".equals(n.op)) { + return new TimeVariable(); + } return new Variable(currentFrame, n.op, null); } case relation: diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Solver.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Solver.java index dfcbcc89..b4e55d27 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Solver.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Solver.java @@ -14,11 +14,13 @@ public class Solver { private Object[] newValues; private double step; + private double start; private NodeCache cache; private boolean ready; public Solver() { step = 0.1; + start = 0; // is it okay to store the cache with the solver instance? cache = new NodeCache(); ready = false; @@ -29,12 +31,17 @@ public class Solver { ready = false; } + public void setStart(double start) { + this.start = start; + ready = false; + } + public void prepare(String input) throws Exception { LineReader reader = new LineReader(input, cache); reader.parse(); model = reader.model; - env = new Environment(model, step); + env = new Environment(model, step, start); int size = model.prepare(); @@ -136,6 +143,9 @@ public class Solver { Assignment[] assignments = model.assignmentArray; Assignment[] derivatives = model.derivativeArray; + // should probably be wrapped inside a method or something + env.time += env.step; + // Solve algebraic equations for(int i=0;i