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
});\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
env.initial = false;\r
\r
ready = true;\r
+ started = false;\r
+ dirty = false;\r
\r
long endNanos = System.nanoTime();\r
\r
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
\r
public void setValue(String key, double value) {\r
env.setValue(key, value);\r
+ dirty = true;\r
}\r
\r
}\r
break;\r
case 37:\r
jj_consume_token(37);\r
+ jjtn000.op = "package";\r
break;\r
case 26:\r
jj_consume_token(26);\r
- jjtn000.op = "function";\r
+ jjtn000.op = "function";\r
break;\r
case 17:\r
jj_consume_token(17);\r
if (jj_scan_token(62)) return true;\r
Token xsp;\r
xsp = jj_scanpos;\r
- if (jj_3R_166()) jj_scanpos = xsp;\r
+ if (jj_3R_167()) jj_scanpos = xsp;\r
if (jj_scan_token(63)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_129() {\r
+ private boolean jj_3R_130() {\r
if (jj_scan_token(49)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_124() {\r
+ private boolean jj_3R_125() {\r
if (jj_scan_token(75)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_153() {\r
+ private boolean jj_3R_154() {\r
Token xsp;\r
xsp = jj_scanpos;\r
if (jj_scan_token(68)) jj_scanpos = xsp;\r
if (jj_scan_token(IDENT)) return true;\r
xsp = jj_scanpos;\r
- if (jj_3R_161()) jj_scanpos = xsp;\r
+ if (jj_3R_162()) jj_scanpos = xsp;\r
while (true) {\r
xsp = jj_scanpos;\r
- if (jj_3R_162()) { jj_scanpos = xsp; break; }\r
+ if (jj_3R_163()) { jj_scanpos = xsp; break; }\r
}\r
return false;\r
}\r
return false;\r
}\r
\r
- private boolean jj_3R_159() {\r
+ private boolean jj_3R_160() {\r
if (jj_scan_token(58)) return true;\r
return false;\r
}\r
return false;\r
}\r
\r
- private boolean jj_3R_150() {\r
+ private boolean jj_3R_151() {\r
if (jj_scan_token(84)) return true;\r
return false;\r
}\r
return false;\r
}\r
\r
- private boolean jj_3R_123() {\r
+ private boolean jj_3R_124() {\r
if (jj_scan_token(74)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_146() {\r
- if (jj_3R_155()) return true;\r
+ private boolean jj_3R_147() {\r
+ if (jj_3R_156()) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_164() {\r
+ private boolean jj_3R_165() {\r
if (jj_scan_token(70)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_145() {\r
+ private boolean jj_3R_146() {\r
if (jj_scan_token(66)) return true;\r
- if (jj_3R_154()) return true;\r
+ if (jj_3R_155()) return true;\r
if (jj_scan_token(67)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_144() {\r
+ private boolean jj_3R_145() {\r
if (jj_scan_token(62)) return true;\r
if (jj_3R_47()) return true;\r
if (jj_scan_token(63)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_142() {\r
- if (jj_3R_152()) return true;\r
+ private boolean jj_3R_143() {\r
+ if (jj_3R_153()) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_157() {\r
+ private boolean jj_3R_158() {\r
if (jj_scan_token(87)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_143() {\r
- if (jj_3R_153()) return true;\r
+ private boolean jj_3R_144() {\r
+ if (jj_3R_154()) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_134() {\r
+ private boolean jj_3R_135() {\r
Token xsp;\r
xsp = jj_scanpos;\r
- if (jj_3R_137()) {\r
- jj_scanpos = xsp;\r
if (jj_3R_138()) {\r
jj_scanpos = xsp;\r
if (jj_3R_139()) {\r
jj_scanpos = xsp;\r
if (jj_3R_146()) {\r
jj_scanpos = xsp;\r
+ if (jj_3R_147()) {\r
+ jj_scanpos = xsp;\r
if (jj_scan_token(35)) return true;\r
}\r
}\r
return false;\r
}\r
\r
- private boolean jj_3R_137() {\r
+ private boolean jj_3R_138() {\r
if (jj_scan_token(UNSIGNED_NUMBER)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_141() {\r
+ private boolean jj_3R_142() {\r
if (jj_scan_token(33)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_140() {\r
+ private boolean jj_3R_141() {\r
if (jj_scan_token(6)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_139() {\r
+ private boolean jj_3R_140() {\r
if (jj_scan_token(STRING)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_138() {\r
+ private boolean jj_3R_139() {\r
if (jj_scan_token(UNSIGNED_INTEGER)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_149() {\r
+ private boolean jj_3R_150() {\r
if (jj_scan_token(83)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_128() {\r
+ private boolean jj_3R_129() {\r
if (jj_scan_token(42)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_155() {\r
+ private boolean jj_3R_156() {\r
if (jj_scan_token(64)) return true;\r
- if (jj_3R_165()) return true;\r
+ if (jj_3R_166()) return true;\r
if (jj_scan_token(65)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_135() {\r
- if (jj_3R_147()) return true;\r
- if (jj_3R_134()) return true;\r
+ private boolean jj_3R_136() {\r
+ if (jj_3R_148()) return true;\r
+ if (jj_3R_135()) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_154() {\r
- if (jj_3R_163()) return true;\r
+ private boolean jj_3R_155() {\r
+ if (jj_3R_164()) return true;\r
Token xsp;\r
while (true) {\r
xsp = jj_scanpos;\r
- if (jj_3R_164()) { jj_scanpos = xsp; break; }\r
+ if (jj_3R_165()) { jj_scanpos = xsp; break; }\r
}\r
return false;\r
}\r
\r
- private boolean jj_3R_158() {\r
+ private boolean jj_3R_159() {\r
if (jj_3R_60()) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_133() {\r
+ private boolean jj_3R_134() {\r
if (jj_scan_token(79)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_152() {\r
+ private boolean jj_3R_153() {\r
Token xsp;\r
xsp = jj_scanpos;\r
- if (jj_3R_158()) {\r
- jj_scanpos = xsp;\r
if (jj_3R_159()) {\r
jj_scanpos = xsp;\r
- if (jj_3R_160()) return true;\r
+ if (jj_3R_160()) {\r
+ jj_scanpos = xsp;\r
+ if (jj_3R_161()) return true;\r
}\r
}\r
if (jj_3R_74()) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_122() {\r
+ private boolean jj_3R_123() {\r
if (jj_scan_token(73)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_147() {\r
+ private boolean jj_3R_148() {\r
Token xsp;\r
xsp = jj_scanpos;\r
- if (jj_3R_156()) {\r
+ if (jj_3R_157()) {\r
jj_scanpos = xsp;\r
- if (jj_3R_157()) return true;\r
+ if (jj_3R_158()) return true;\r
}\r
return false;\r
}\r
\r
- private boolean jj_3R_156() {\r
+ private boolean jj_3R_157() {\r
if (jj_scan_token(86)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_119() {\r
- if (jj_3R_134()) return true;\r
+ private boolean jj_3R_120() {\r
+ if (jj_3R_135()) return true;\r
Token xsp;\r
xsp = jj_scanpos;\r
- if (jj_3R_135()) jj_scanpos = xsp;\r
+ if (jj_3R_136()) jj_scanpos = xsp;\r
return false;\r
}\r
\r
- private boolean jj_3R_120() {\r
- if (jj_3R_136()) return true;\r
- if (jj_3R_119()) return true;\r
+ private boolean jj_3R_121() {\r
+ if (jj_3R_137()) return true;\r
+ if (jj_3R_120()) return true;\r
return false;\r
}\r
\r
private boolean jj_3R_104() {\r
- if (jj_3R_114()) return true;\r
+ if (jj_3R_115()) return true;\r
if (jj_3R_103()) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_113() {\r
- if (jj_3R_118()) return true;\r
- if (jj_3R_112()) return true;\r
+ private boolean jj_3R_114() {\r
+ if (jj_3R_119()) return true;\r
+ if (jj_3R_113()) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_136() {\r
+ private boolean jj_3R_137() {\r
Token xsp;\r
xsp = jj_scanpos;\r
- if (jj_3R_148()) {\r
- jj_scanpos = xsp;\r
if (jj_3R_149()) {\r
jj_scanpos = xsp;\r
if (jj_3R_150()) {\r
jj_scanpos = xsp;\r
- if (jj_3R_151()) return true;\r
+ if (jj_3R_151()) {\r
+ jj_scanpos = xsp;\r
+ if (jj_3R_152()) return true;\r
}\r
}\r
}\r
return false;\r
}\r
\r
- private boolean jj_3R_148() {\r
+ private boolean jj_3R_149() {\r
if (jj_scan_token(82)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_110() {\r
+ private boolean jj_3R_111() {\r
if (jj_3R_60()) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_112() {\r
- if (jj_3R_119()) return true;\r
+ private boolean jj_3R_113() {\r
+ if (jj_3R_120()) return true;\r
Token xsp;\r
while (true) {\r
xsp = jj_scanpos;\r
- if (jj_3R_120()) { jj_scanpos = xsp; break; }\r
+ if (jj_3R_121()) { jj_scanpos = xsp; break; }\r
}\r
return false;\r
}\r
\r
- private boolean jj_3R_131() {\r
+ private boolean jj_3R_132() {\r
if (jj_scan_token(56)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_118() {\r
+ private boolean jj_3R_119() {\r
Token xsp;\r
xsp = jj_scanpos;\r
- if (jj_3R_132()) {\r
- jj_scanpos = xsp;\r
if (jj_3R_133()) {\r
jj_scanpos = xsp;\r
+ if (jj_3R_134()) {\r
+ jj_scanpos = xsp;\r
if (jj_scan_token(80)) {\r
jj_scanpos = xsp;\r
if (jj_scan_token(81)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_132() {\r
+ private boolean jj_3R_133() {\r
if (jj_scan_token(78)) return true;\r
return false;\r
}\r
return false;\r
}\r
\r
- private boolean jj_3R_117() {\r
+ private boolean jj_3R_118() {\r
Token xsp;\r
xsp = jj_scanpos;\r
- if (jj_3R_130()) {\r
+ if (jj_3R_131()) {\r
jj_scanpos = xsp;\r
- if (jj_3R_131()) return true;\r
+ if (jj_3R_132()) return true;\r
}\r
return false;\r
}\r
\r
- private boolean jj_3R_130() {\r
+ private boolean jj_3R_131() {\r
if (jj_scan_token(32)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_111() {\r
- if (jj_3R_118()) return true;\r
+ private boolean jj_3R_112() {\r
+ if (jj_3R_119()) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_116() {\r
+ private boolean jj_3R_117() {\r
Token xsp;\r
xsp = jj_scanpos;\r
- if (jj_3R_127()) {\r
- jj_scanpos = xsp;\r
if (jj_3R_128()) {\r
jj_scanpos = xsp;\r
- if (jj_3R_129()) return true;\r
+ if (jj_3R_129()) {\r
+ jj_scanpos = xsp;\r
+ if (jj_3R_130()) return true;\r
}\r
}\r
return false;\r
}\r
\r
- private boolean jj_3R_127() {\r
+ private boolean jj_3R_128() {\r
if (jj_scan_token(5)) return true;\r
return false;\r
}\r
private boolean jj_3R_103() {\r
Token xsp;\r
xsp = jj_scanpos;\r
- if (jj_3R_111()) jj_scanpos = xsp;\r
- if (jj_3R_112()) return true;\r
+ if (jj_3R_112()) jj_scanpos = xsp;\r
+ if (jj_3R_113()) return true;\r
while (true) {\r
xsp = jj_scanpos;\r
- if (jj_3R_113()) { jj_scanpos = xsp; break; }\r
+ if (jj_3R_114()) { jj_scanpos = xsp; break; }\r
}\r
return false;\r
}\r
\r
- private boolean jj_3R_115() {\r
+ private boolean jj_3R_116() {\r
Token xsp;\r
xsp = jj_scanpos;\r
if (jj_scan_token(16)) {\r
return false;\r
}\r
\r
- private boolean jj_3R_114() {\r
+ private boolean jj_3R_115() {\r
Token xsp;\r
xsp = jj_scanpos;\r
- if (jj_3R_121()) {\r
- jj_scanpos = xsp;\r
if (jj_3R_122()) {\r
jj_scanpos = xsp;\r
if (jj_3R_123()) {\r
jj_scanpos = xsp;\r
if (jj_3R_125()) {\r
jj_scanpos = xsp;\r
- if (jj_3R_126()) return true;\r
+ if (jj_3R_126()) {\r
+ jj_scanpos = xsp;\r
+ if (jj_3R_127()) return true;\r
}\r
}\r
}\r
return false;\r
}\r
\r
- private boolean jj_3R_121() {\r
+ private boolean jj_3R_122() {\r
if (jj_scan_token(72)) return true;\r
return false;\r
}\r
return false;\r
}\r
\r
- private boolean jj_3R_109() {\r
+ private boolean jj_3R_110() {\r
Token xsp;\r
xsp = jj_scanpos;\r
- if (jj_3R_115()) jj_scanpos = xsp;\r
- xsp = jj_scanpos;\r
if (jj_3R_116()) jj_scanpos = xsp;\r
xsp = jj_scanpos;\r
if (jj_3R_117()) jj_scanpos = xsp;\r
+ xsp = jj_scanpos;\r
+ if (jj_3R_118()) jj_scanpos = xsp;\r
return false;\r
}\r
\r
}\r
\r
private boolean jj_3R_102() {\r
- if (jj_3R_109()) return true;\r
if (jj_3R_110()) return true;\r
+ if (jj_3R_111()) return true;\r
return false;\r
}\r
\r
return false;\r
}\r
\r
+ private boolean jj_3R_109() {\r
+ if (jj_scan_token(26)) return true;\r
+ return false;\r
+ }\r
+\r
private boolean jj_3R_106() {\r
if (jj_scan_token(7)) return true;\r
return false;\r
return false;\r
}\r
\r
- private boolean jj_3R_108() {\r
- if (jj_scan_token(26)) return true;\r
+ private boolean jj_3R_105() {\r
+ if (jj_scan_token(34)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_105() {\r
- if (jj_scan_token(34)) return true;\r
+ private boolean jj_3R_108() {\r
+ if (jj_scan_token(37)) return true;\r
return false;\r
}\r
\r
jj_scanpos = xsp;\r
if (jj_scan_token(38)) {\r
jj_scanpos = xsp;\r
- if (jj_scan_token(37)) {\r
- jj_scanpos = xsp;\r
if (jj_3R_108()) {\r
jj_scanpos = xsp;\r
+ if (jj_3R_109()) {\r
+ jj_scanpos = xsp;\r
if (jj_scan_token(17)) {\r
jj_scanpos = xsp;\r
if (jj_scan_token(95)) {\r
return false;\r
}\r
\r
- private boolean jj_3R_173() {\r
+ private boolean jj_3R_174() {\r
if (jj_3R_47()) return true;\r
return false;\r
}\r
return false;\r
}\r
\r
- private boolean jj_3R_162() {\r
+ private boolean jj_3R_163() {\r
if (jj_scan_token(68)) return true;\r
if (jj_scan_token(IDENT)) return true;\r
return false;\r
return false;\r
}\r
\r
- private boolean jj_3R_126() {\r
+ private boolean jj_3R_127() {\r
if (jj_scan_token(77)) return true;\r
return false;\r
}\r
return false;\r
}\r
\r
- private boolean jj_3R_172() {\r
+ private boolean jj_3R_173() {\r
if (jj_scan_token(69)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_168() {\r
+ private boolean jj_3R_169() {\r
if (jj_scan_token(71)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_170() {\r
+ private boolean jj_3R_171() {\r
Token xsp;\r
xsp = jj_scanpos;\r
- if (jj_3R_172()) {\r
+ if (jj_3R_173()) {\r
jj_scanpos = xsp;\r
- if (jj_3R_173()) return true;\r
+ if (jj_3R_174()) return true;\r
}\r
return false;\r
}\r
\r
- private boolean jj_3R_161() {\r
- if (jj_3R_167()) return true;\r
+ private boolean jj_3R_162() {\r
+ if (jj_3R_168()) return true;\r
return false;\r
}\r
\r
return false;\r
}\r
\r
- private boolean jj_3R_167() {\r
+ private boolean jj_3R_168() {\r
if (jj_scan_token(66)) return true;\r
- if (jj_3R_170()) return true;\r
+ if (jj_3R_171()) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_125() {\r
+ private boolean jj_3R_126() {\r
if (jj_scan_token(76)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_163() {\r
+ private boolean jj_3R_164() {\r
if (jj_3R_47()) return true;\r
Token xsp;\r
while (true) {\r
xsp = jj_scanpos;\r
- if (jj_3R_168()) { jj_scanpos = xsp; break; }\r
+ if (jj_3R_169()) { jj_scanpos = xsp; break; }\r
}\r
return false;\r
}\r
\r
- private boolean jj_3R_174() {\r
+ private boolean jj_3R_175() {\r
if (jj_scan_token(IDENT)) return true;\r
if (jj_scan_token(88)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_160() {\r
+ private boolean jj_3R_161() {\r
if (jj_scan_token(46)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_171() {\r
- if (jj_3R_174()) return true;\r
+ private boolean jj_3R_172() {\r
+ if (jj_3R_175()) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_151() {\r
+ private boolean jj_3R_152() {\r
if (jj_scan_token(85)) return true;\r
return false;\r
}\r
\r
- private boolean jj_3R_169() {\r
- if (jj_3R_171()) return true;\r
+ private boolean jj_3R_170() {\r
+ if (jj_3R_172()) return true;\r
return false;\r
}\r
\r
return false;\r
}\r
\r
- private boolean jj_3R_165() {\r
+ private boolean jj_3R_166() {\r
Token xsp;\r
xsp = jj_scanpos;\r
if (jj_3_10()) {\r
jj_scanpos = xsp;\r
- if (jj_3R_169()) return true;\r
+ if (jj_3R_170()) return true;\r
}\r
return false;\r
}\r
\r
- private boolean jj_3R_166() {\r
- if (jj_3R_165()) return true;\r
+ private boolean jj_3R_167() {\r
+ if (jj_3R_166()) return true;\r
return false;\r
}\r
\r