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
return elements.get(index);\r
}\r
\r
- public Collection<Object> elements() {\r
+ public List<Object> elements() {\r
return elements;\r
}\r
\r
\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
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
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
applyIC :: Variable -> String -> <WriteGraph> ()\r
deleteIC :: Variable -> String -> <WriteGraph> ()\r
\r
+ setPublishResults :: Variable -> Boolean -> <ReadGraph> ()\r
\ No newline at end of file
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
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
*/\r
@Override\r
public void updateSubscriptions() {\r
+ if(!publishResults) return;\r
for(VariableValueSubscription subscription : getListenerSnapshot())\r
subscription.update();\r
skippedVariableUpdate = false;\r
\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
\r
@Override\r
public void prepareToStep() throws Exception {\r
+ solver.aboutToRun();\r
}\r
\r
@Override\r