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
\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
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
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
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
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
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
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
--- /dev/null
+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
interval = step;\r
\r
solver.setStep(step);\r
+ solver.setStart(start);\r
}\r
\r
@Override\r