]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Fixed some bugs encountered in Strada-model
authorvillberg <villberg@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 28 Apr 2014 08:19:53 +0000 (08:19 +0000)
committervillberg <villberg@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 28 Apr 2014 08:19:53 +0000 (08:19 +0000)
refs #4765

git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@29369 ac1ea38d-2e2b-0410-8846-a27921b304fc

14 files changed:
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Addition.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Constant.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Division.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Model.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Multiplication.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Negation.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Solver.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/SolverUtils.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Subtraction.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Variable.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/VariableBase.java

index a407ca4e2dec1adcc04b73f440e49f2b88514341..b2ec695c4bd97acbd7103a4a5dd2512b354f201e 100644 (file)
@@ -33,6 +33,7 @@ public class Addition implements IExpression {
        public Object evaluate(IEnvironment environment) {\r
                Object o1 = exp1.evaluate(environment);\r
                Object o2 = exp2.evaluate(environment);\r
+               if(o1 == null || o2 == null) return null; \r
                if(o1 instanceof Double && o2 instanceof Double) {\r
                        return (Double)o1 + (Double)o2;\r
                }\r
index a14e5ace2a686aa6b85a4ae985a4bf79bd0c63b3..233838d9f949f0b479071ec94f973fb71974b527 100644 (file)
@@ -20,6 +20,8 @@ import fi.semantum.sysdyn.solver.SolverUtils.Slice;
 \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
 \r
        public Array() {\r
@@ -171,29 +173,57 @@ public class Array implements IExpression {
                return true;\r
        }\r
 \r
-       public Array slice(Slice[] indices) {\r
+       public Array slice(Array[] indices) {\r
                \r
                if(indices.length == 1) {\r
                        Array result = new Array();\r
-                       for(int i=indices[0].start;i<=indices[0].end;i++) {\r
-                               result.addElement(element(i));\r
+                       for(Object r : indices[0].elements) {\r
+                               Double d = (Double)r;\r
+                               int index = d.intValue();\r
+                               result.addElement(element(index));\r
                        }\r
                        return result;\r
                }\r
                \r
-               if(indices.length != 2) throw new IllegalStateException();\r
+               if(indices.length == 2) {\r
+                       if(indices[1] == Array.FULL) {\r
+                               Double d = (Double)indices[0].element(0);\r
+                               int index = d.intValue();\r
+                               return (Array)elements.get(index);\r
+                       } else {\r
+                               Array result = new Array();\r
+                               for(Object o : elements) {\r
+                                       Array a = (Array)o;\r
+                                       Double d = (Double)indices[1].element(0);\r
+                                       int index = d.intValue();\r
+                                       result.addElement(a.element(index));\r
+                               }\r
+                               return result;\r
+                       }\r
+               }\r
                \r
-               if(indices[1] == Slice.FULL) {\r
-                       return (Array)elements.get(indices[0].start);\r
-               } else {\r
+               if(indices.length == 3) {\r
                        Array result = new Array();\r
-                       for(Object o : elements) {\r
-                               Array a = (Array)o;\r
-                               result.addElement(a.element(indices[1].start));\r
+                       for(int i=0;i<indices[0].dimension();i++) {\r
+                               Double d0 = (Double)indices[0].element(i);\r
+                               int index0 = d0.intValue();\r
+                               Array a0 = (Array)element(index0);\r
+                               for(int j=0;j<indices[1].dimension();j++) {\r
+                                       Double d1 = (Double)indices[1].element(j);\r
+                                       int index1 = d1.intValue();\r
+                                       Array a1 = (Array)a0.element(index1);\r
+                                       for(int k=0;k<indices[2].dimension();k++) {\r
+                                               Double d2 = (Double)indices[2].element(k);\r
+                                               int index2 = d2.intValue();\r
+                                               result.addElement(a1.element(index2));\r
+                                       }\r
+                               }\r
                        }\r
                        return result;\r
                }\r
                \r
+               throw new IllegalStateException();\r
+               \r
        }\r
        \r
        public Array add(Array other) {\r
@@ -276,4 +306,19 @@ public class Array implements IExpression {
                }\r
        }\r
        \r
+       public static Array singleton(double value) {\r
+               Array result = new Array();\r
+               result.addElement(value);\r
+               return result;\r
+       }\r
+\r
+       public static Array slice(int start, int end) {\r
+               Array result = new Array();\r
+               for(int i=start;i<=end;i++) {\r
+                       double d = i;\r
+                       result.addElement(d);\r
+               }\r
+               return result;\r
+       }\r
+\r
 }\r
index 9fd58ddd0df7279a1443e1f22c30706b18a7e659..b93dc9898bc44e89670b7d616035576d7618fcd9 100644 (file)
@@ -22,7 +22,12 @@ public class Constant implements IExpression {
                try {\r
                        value = Double.parseDouble(name);\r
                } catch (NumberFormatException e) {\r
-                       value = name;\r
+                       if("true".equals(name))\r
+                               value = Boolean.TRUE;\r
+                       else if("false".equals(name))\r
+                               value = Boolean.FALSE;\r
+                       else\r
+                               value = name;\r
                }\r
        }\r
        \r
index 6b7658f20de06f4cea1fd128f15109fe553794ec..80fbaee4394f1bc5498c9249cc75732043fc97d5 100644 (file)
@@ -45,6 +45,7 @@ public class Division implements IExpression {
        public Object evaluate(IEnvironment environment) {\r
                Object left = exp1.evaluate(environment);\r
                Object right = exp2.evaluate(environment);\r
+               if(left == null || right == null) return null;\r
                if(left instanceof Double && right instanceof Double) return ((Double)left)/((Double)right);\r
                else if (left instanceof Array && right instanceof Double) return arrayDiv((Array)left, (Double)right);\r
                else if (left instanceof Double && right instanceof Array) return arrayDiv((Array)right, (Double)left);\r
index 1e1474087b83459f5546e51194c2acb2ced72856..849a8c99d68ccf4205f1b5007a6b92d8599c6828 100644 (file)
@@ -84,6 +84,15 @@ final public class Environment implements IEnvironment, ISystem {
                                return Double.valueOf(array.size(col.intValue()));\r
                        }\r
                        \r
+               });\r
+               model.functions.put("exp", new Fn1(2) {\r
+\r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Double x = (Double)environment.getValue(0);\r
+                               return Math.exp(x);\r
+                       }\r
+                       \r
                });\r
                model.functions.put("zidz", new Fn1(2) {\r
 \r
index 4aa58463633d37cf502f165bf9ce6e3a2378444d..8e084e989e540ed8940d554ff553249fba7708b8 100644 (file)
@@ -135,7 +135,6 @@ class Model implements IFrame {
                        }\r
                        \r
                        fn.setLocals(frame);\r
-\r
                        return fn.evaluate(frame, args.args.length);\r
                        \r
                }\r
index cc4724ab3fad0d7320b38951f79855778b76b5a8..fa37e77a54a4a321951892dd145b8f8cf8ba3cc5 100644 (file)
@@ -33,6 +33,7 @@ public class Multiplication implements IExpression {
        public Object evaluate(IEnvironment environment) {\r
                Object left = exp1.evaluate(environment);\r
                Object right = exp2.evaluate(environment);\r
+               if(left == null || right == null) return null;\r
                if(left instanceof Double && right instanceof Double) {\r
                        return ((Double)left)*((Double)right);\r
                } \r
index 15e9e2df8fa13057435be334767d431e21ecbd8a..c60bf13e6b7fce831146467e29d663fadd1f0bb2 100644 (file)
@@ -29,7 +29,9 @@ public class Negation implements IExpression {
     \r
        @Override\r
        public Object evaluate(IEnvironment environment) {\r
-               return -((Double)exp.evaluate(environment));\r
+               Double d = (Double)exp.evaluate(environment);\r
+               if(d == null) return null;\r
+               return -d;\r
        }\r
     \r
        @Override\r
index 175ea53cf0cae10453908f84e9dcb0ff96424b6b..bd6fc87d7fbac312df4b75ba44ab02c3e4a94034 100644 (file)
@@ -27,7 +27,7 @@ public class Parser {
        \r
 //     public IFrame currentFrame;\r
 \r
-       private int PRINT = 1;\r
+       private int PRINT = 0;\r
        \r
        public Object walk(SimpleNode n, int indent, IFrame model) {\r
                Object result = walk_(n, indent, model);\r
index 1a4aed8c9937fafe357a467d5ec678edf6b9ff24..93ac4fff6750f1a023f6d699f3e2cf0609c0ccb5 100644 (file)
@@ -212,8 +212,10 @@ public class Solver {
                                try {\r
                                        if(!ass.assigned) {\r
                                                Object value = ass.expression.evaluate(env);\r
-                                               ass.target.assign(env, ass.subscripts, value);\r
-                                               ass.assigned = true;\r
+                                               if(value != null) {\r
+                                                       ass.target.assign(env, ass.subscripts, value);\r
+                                                       ass.assigned = true;\r
+                                               }\r
                                        }\r
                                } catch (Exception e) {\r
                                        condition++;\r
@@ -257,7 +259,7 @@ public class Solver {
                        newValues[i] = derivatives[i].expression.evaluate(env);\r
                }\r
                for(int i=0;i<model.derivatives.size();i++) {\r
-                       derivatives[i].target.assign(env, assignments[i].subscripts, newValues[i]);\r
+                       derivatives[i].target.assign(env, derivatives[i].subscripts, newValues[i]);\r
                }\r
 \r
                // Increment time\r
index 731de2dcf83b3ff8b85719cc1b24cadfd6b2bcb5..05534e479a0f1409ff276e41366078517e8628dd 100644 (file)
@@ -31,28 +31,36 @@ public class SolverUtils {
                return dimensions != null && dimensions != VariableBase.UNINIT;\r
        }\r
        \r
-       public static Slice[] parseSubscripts(IEnvironment env, IExpression[] subscripts) {\r
-               Slice[] result = new Slice[subscripts.length];\r
+       public static Array[] parseSubscripts(IEnvironment env, IExpression[] subscripts) {\r
+               Array[] result = new Array[subscripts.length];\r
                for(int i=0;i<subscripts.length;i++) {\r
                        IExpression e = subscripts[i];\r
                        if(e instanceof ArraySliceExpression) {\r
                                ArraySliceExpression ase = (ArraySliceExpression)e;\r
                                Double start = (Double)ase.start.evaluate(env);\r
                                Double end = (Double)ase.end.evaluate(env);\r
-                               result[i] = new Slice(start.intValue(), end.intValue());\r
+                               result[i] = Array.slice(start.intValue()-1, end.intValue()-1);\r
+                       } else if (e instanceof Array) {\r
+                               Array array = (Array)e;\r
+                               Array r = new Array();\r
+                               for(Object o : array.elements()) {\r
+                                       Double d = (Double)o;\r
+                                       r.addElement(d-1);\r
+                               }\r
+                               result[i] = r;\r
                        } else {\r
                                Double v = (Double)e.evaluate(env);\r
                                int index = v.intValue()-1;\r
-                               if(index == -2) result[i] = Slice.FULL;\r
-                               else result[i] = new Slice(index,index);\r
+                               if(index == -2) result[i] = Array.FULL;\r
+                               else result[i] = Array.singleton(index);\r
                        }\r
                }\r
                return result;\r
        }\r
        \r
-       public static boolean isSlice(Slice[] subs) {\r
-               for(Slice s : subs) {\r
-                       if(s.start != s.end) return true;\r
+       public static boolean isSlice(Array[] subs) {\r
+               for(Array s : subs) {\r
+                       if(s.dimension() != 1) return true;\r
                }\r
                return false;\r
        }\r
index 79d1404caf5c64594da13de9b89e8819627bfef4..60a4306353cd404746255a5539981784532ba81d 100644 (file)
@@ -32,7 +32,10 @@ public class Subtraction implements IExpression {
     \r
        @Override\r
        public Object evaluate(IEnvironment environment) {\r
-               return ((Double)exp1.evaluate(environment)) - ((Double)exp2.evaluate(environment));\r
+               Double d1 = (Double)exp1.evaluate(environment);\r
+               Double d2 = (Double)exp2.evaluate(environment);\r
+               if(d1 == null || d2 == null) return null;\r
+               return d1-d2;\r
        }\r
        \r
        @Override\r
index 8144fba00de810baf85a3f4d9691845cc99620ea..a461894e9a748017b58df03b923a321b5dc2da07 100644 (file)
@@ -123,6 +123,8 @@ public class Variable implements IExpression {
        \r
        private void validateSize(IEnvironment env, IExpression[] subscripts, Object value) {\r
                \r
+               if(value == null) return;\r
+               \r
                if(base.isStoredAsArray()) return;\r
                \r
                int fullDimension = base.dimension();\r
@@ -134,6 +136,8 @@ public class Variable implements IExpression {
                        }\r
                } else {\r
                        if(subscripts == null) {\r
+                               if(base.dimensions.length == this.subscripts.length && value instanceof Double) \r
+                                       return;\r
                                if(!(value instanceof Array))\r
                                        throw new IllegalStateException();\r
                                Array arr = (Array)value;\r
@@ -146,6 +150,9 @@ public class Variable implements IExpression {
                                        if(!arr.validateDimensions(base.dimensions, 0))\r
                                                throw new IllegalStateException();\r
                                } else {\r
+                                       if(base.dimensions.length == subscripts.length) {\r
+                                               if(value instanceof Double) return;\r
+                                       }\r
                                        throw new IllegalStateException();\r
                                }\r
                        }\r
@@ -181,7 +188,9 @@ public class Variable implements IExpression {
        \r
        @Override\r
        public Object evaluate(IEnvironment environment) {\r
-               return base.evaluate(environment, subscripts);\r
+               Object result = base.evaluate(environment, subscripts);\r
+               if(result == null) System.err.println("returned null for " + base.name);\r
+               return result;\r
        }\r
 \r
        \r
index b00a48cf9c892c2670488ac5f43428e34462fa7e..183f9e25fc3edd0e3e58dec319e439fa8f812f81 100644 (file)
@@ -179,7 +179,7 @@ public class VariableBase {
                        if(dimension() > 1) {\r
                                if(subscripts != null) {\r
                                        \r
-                                       Slice[] sub = SolverUtils.parseSubscripts(environment, subscripts);\r
+                                       Array[] sub = SolverUtils.parseSubscripts(environment, subscripts);\r
                                        if(SolverUtils.isSlice(sub)) {\r
                                                Array arr = new Array();\r
                                                intoArray(environment, index, 0, arr);\r