From a21eb396b9f923daa717c916b8e1fbf904eb28ac Mon Sep 17 00:00:00 2001 From: villberg Date: Mon, 31 Mar 2014 11:16:46 +0000 Subject: [PATCH] Getting closer refs #4765 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@29221 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../META-INF/MANIFEST.MF | 3 +- .../fi/semantum/sysdyn/solver/Addition.java | 7 ++ .../src/fi/semantum/sysdyn/solver/And.java | 8 ++ .../semantum/sysdyn/solver/Application.java | 9 +++ .../fi/semantum/sysdyn/solver/Argument.java | 8 ++ .../src/fi/semantum/sysdyn/solver/Array.java | 12 +++ .../sysdyn/solver/ArraySliceExpression.java | 9 +++ .../fi/semantum/sysdyn/solver/Assignment.java | 2 +- .../fi/semantum/sysdyn/solver/Constant.java | 7 ++ .../semantum/sysdyn/solver/Declaration.java | 8 ++ .../fi/semantum/sysdyn/solver/Derivate.java | 14 +++- .../fi/semantum/sysdyn/solver/Derivative.java | 5 ++ .../fi/semantum/sysdyn/solver/Division.java | 9 +++ .../sysdyn/solver/ElementwiseProduct.java | 9 +++ .../src/fi/semantum/sysdyn/solver/Equals.java | 9 +++ .../fi/semantum/sysdyn/solver/ForArray.java | 13 +++ .../sysdyn/solver/GreaterOrEqualThan.java | 9 +++ .../semantum/sysdyn/solver/GreaterThan.java | 9 +++ .../semantum/sysdyn/solver/IExpression.java | 5 ++ .../fi/semantum/sysdyn/solver/IfThenElse.java | 10 +++ .../sysdyn/solver/LessOrEqualThan.java | 9 +++ .../fi/semantum/sysdyn/solver/LessThan.java | 9 +++ .../sysdyn/solver/Multiplication.java | 9 +++ .../fi/semantum/sysdyn/solver/Negation.java | 8 ++ .../fi/semantum/sysdyn/solver/NotEquals.java | 9 +++ .../sysdyn/solver/NullModification.java | 5 ++ .../src/fi/semantum/sysdyn/solver/Or.java | 8 ++ .../sysdyn/solver/ParameterDeclaration.java | 5 ++ .../src/fi/semantum/sysdyn/solver/Solver.java | 80 ++++++++++++++++--- .../semantum/sysdyn/solver/Subtraction.java | 9 +++ .../semantum/sysdyn/solver/TimeVariable.java | 7 ++ .../fi/semantum/sysdyn/solver/Variable.java | 7 ++ .../semantum/sysdyn/solver/VariableBase.java | 2 - .../sysdyn/solver/VariableDeclaration.java | 7 ++ 34 files changed, 325 insertions(+), 14 deletions(-) diff --git a/fi.semantum.sysdyn.solver/META-INF/MANIFEST.MF b/fi.semantum.sysdyn.solver/META-INF/MANIFEST.MF index 7f8fe97d..3256e513 100644 --- a/fi.semantum.sysdyn.solver/META-INF/MANIFEST.MF +++ b/fi.semantum.sysdyn.solver/META-INF/MANIFEST.MF @@ -7,7 +7,8 @@ Bundle-Activator: fi.semantum.sysdyn.solver.Activator Bundle-Vendor: Semantum Oy Require-Bundle: org.eclipse.core.runtime, org.simantics.utils;bundle-version="1.1.0", - org.simantics.databoard;bundle-version="0.6.5" + org.simantics.databoard;bundle-version="0.6.5", + org.simantics.utils.datastructures;bundle-version="1.1.0" Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Bundle-ActivationPolicy: lazy Export-Package: fi.semantum.sysdyn.solver diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Addition.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Addition.java index 27ee6841..a407ca4e 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Addition.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Addition.java @@ -61,4 +61,11 @@ public class Addition implements IExpression { return this; } + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + exp1.accept(visitor); + exp2.accept(visitor); + } + } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/And.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/And.java index d77a191c..dc3ab53d 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/And.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/And.java @@ -13,6 +13,8 @@ package fi.semantum.sysdyn.solver; import java.util.ArrayList; import java.util.Map; +import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor; + public class And implements IExpression { public IExpression[] exps; @@ -59,4 +61,10 @@ public class And implements IExpression { return this; } + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + for(IExpression exp : exps) exp.accept(visitor); + } + } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Application.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Application.java index c9c2282c..a59ece28 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Application.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Application.java @@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver; import java.util.Map; +import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor; + public class Application implements IExpression { public String name; @@ -48,4 +50,11 @@ public class Application implements IExpression { return this; } + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + for(Argument a : args.args) + a.modification.accept(visitor); + } + } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Argument.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Argument.java index 3a7013f4..2712ab90 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Argument.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Argument.java @@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver; import java.util.Map; +import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor; + public class Argument implements IExpression { public String name; @@ -47,4 +49,10 @@ public class Argument implements IExpression { return this; } + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + modification.accept(visitor); + } + } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java index 72fcd405..45449362 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java @@ -15,6 +15,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.Map; +import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor; import fi.semantum.sysdyn.solver.SolverUtils.Slice; public class Array implements IExpression { @@ -227,4 +228,15 @@ public class Array implements IExpression { return result; } + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + for(int i=0;i elements = new ArrayList(); @@ -110,4 +112,15 @@ public class ForArray implements IExpression { return this; } + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + for(int i=0;i copies); + public void accept(ExpressionVisitor visitor); } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/IfThenElse.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/IfThenElse.java index 2176faf4..a95c99e1 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/IfThenElse.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/IfThenElse.java @@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver; import java.util.Map; +import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor; + public class IfThenElse implements IExpression { public IExpression exp; @@ -56,4 +58,12 @@ public class IfThenElse implements IExpression { return this; } + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + exp.accept(visitor); + t.accept(visitor); + e.accept(visitor); + } + } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/LessOrEqualThan.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/LessOrEqualThan.java index 7c2f2870..b9ab757e 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/LessOrEqualThan.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/LessOrEqualThan.java @@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver; import java.util.Map; +import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor; + public class LessOrEqualThan implements IExpression { public IExpression exp1; @@ -51,4 +53,11 @@ public class LessOrEqualThan implements IExpression { return this; } + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + exp1.accept(visitor); + exp2.accept(visitor); + } + } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/LessThan.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/LessThan.java index 9a808538..db798083 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/LessThan.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/LessThan.java @@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver; import java.util.Map; +import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor; + public class LessThan implements IExpression { public IExpression exp1; @@ -51,4 +53,11 @@ public class LessThan implements IExpression { return this; } + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + exp1.accept(visitor); + exp2.accept(visitor); + } + } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Multiplication.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Multiplication.java index 00397a37..cc4724ab 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Multiplication.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Multiplication.java @@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver; import java.util.Map; +import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor; + public class Multiplication implements IExpression { public IExpression exp1; @@ -62,4 +64,11 @@ public class Multiplication implements IExpression { return this; } + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + exp1.accept(visitor); + exp2.accept(visitor); + } + } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Negation.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Negation.java index cdf125ba..15e9e2df 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Negation.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Negation.java @@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver; import java.util.Map; +import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor; + public class Negation implements IExpression { public IExpression exp; @@ -46,4 +48,10 @@ public class Negation implements IExpression { return this; } + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + exp.accept(visitor); + } + } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/NotEquals.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/NotEquals.java index 41ca66a5..5c5499ce 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/NotEquals.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/NotEquals.java @@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver; import java.util.Map; +import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor; + public class NotEquals implements IExpression { public IExpression exp1; @@ -49,4 +51,11 @@ public class NotEquals implements IExpression { return this; } + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + exp1.accept(visitor); + exp2.accept(visitor); + } + } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/NullModification.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/NullModification.java index 287d6180..cbafd0d9 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/NullModification.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/NullModification.java @@ -23,5 +23,10 @@ public class NullModification implements IExpression { public IExpression rewrite(IFrame frame, Map copies) { return this; } + + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + } } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Or.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Or.java index e08c3282..375c9240 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Or.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Or.java @@ -13,6 +13,8 @@ package fi.semantum.sysdyn.solver; import java.util.ArrayList; import java.util.Map; +import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor; + public class Or implements IExpression { public IExpression[] exps; @@ -59,4 +61,10 @@ public class Or implements IExpression { return this; } + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + for(IExpression exp : exps) exp.accept(visitor); + } + } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ParameterDeclaration.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ParameterDeclaration.java index 1aa811f7..72cbab5e 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ParameterDeclaration.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ParameterDeclaration.java @@ -49,4 +49,9 @@ public class ParameterDeclaration implements IExpression { throw new UnsupportedOperationException(); } + @Override + public void accept(ExpressionVisitor visitor) { + throw new UnsupportedOperationException(); + } + } 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 bcd050d4..ca56e4bb 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 @@ -13,7 +13,15 @@ package fi.semantum.sysdyn.solver; import java.io.StringReader; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.simantics.utils.datastructures.MapList; + +import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor; import fi.semantum.sysdyn.solver.Model.Globals; import fi.semantum.sysdyn.solver.parser.ModelParser; import fi.semantum.sysdyn.solver.parser.SimpleNode; @@ -27,6 +35,7 @@ public class Solver { private Environment env; private Object[] newValues; + private Object[] derivatives; private double step; private double start; @@ -51,6 +60,52 @@ public class Solver { ready = false; } + private void sortAssignments() { + + final MapList asses = new MapList(); + for(Assignment ass : model.assignments) asses.add(ass.target.base.name, ass); + + final List found = new ArrayList(); + final Set visited = new HashSet(); + + ExpressionVisitor sortVisitor = new ExpressionVisitor() { + @Override + public void visit(IExpression expression) { + if(expression instanceof Variable) { + Variable var = (Variable)expression; + if(visited.add(var.base.name)) { + for(Assignment ass : asses.getValues(var.base.name)) { + ass.expression.accept(this); + if(ass.subscripts != null) + for(IExpression e : ass.subscripts) e.accept(this); + } + found.add(var.base); + } + } + } + }; + + // Sort assignments + for(Assignment ass : model.assignments) { + ass.target.accept(sortVisitor); + } + + Collections.sort(model.assignments, new Comparator() { + + @Override + public int compare(Assignment o1, Assignment o2) { + int i1 = found.indexOf(o1.target.base); + int i2 = found.indexOf(o2.target.base); + if(i1 < i2) return -1; + else if (i1 > i2) return 1; + return 0; + } + + + }); + + } + public void prepare(String input) throws Exception { long startNanos = System.nanoTime(); @@ -79,11 +134,14 @@ public class Solver { env.valueTable = new Object[size+STACK_SIZE]; + sortAssignments(); + model.assignmentArray = model.assignments.toArray(new Assignment[model.assignments.size()]); model.derivativeArray = model.derivatives.toArray(new Assignment[model.derivatives.size()]); model.parameterArray = model.parameters.toArray(new ParameterDeclaration[model.parameters.size()]); newValues = new Object[Math.max(model.assignments.size(),model.derivatives.size())]; + derivatives = new Object[model.derivatives.size()]; int condition = 1; int loops = 0; @@ -185,6 +243,8 @@ public class Solver { return env.getValueArray(); } + + public void step() { // TODO: do something more sensible if the solver is not ready if (!ready) return; @@ -192,10 +252,19 @@ public class Solver { Assignment[] assignments = model.assignmentArray; Assignment[] derivatives = model.derivativeArray; + // first we evaluate, integrate and assign derivatives at previous time step + for(int i=0;i