]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Interpolate with WithLookup auxiliary
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 15 Feb 2010 12:33:21 +0000 (12:33 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 15 Feb 2010 12:33:21 +0000 (12:33 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@14108 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj
org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/WithLookupExpression.java

index 2dbca6f54c4433c6aae9b2316935a3c0538754b2..426ce0c5577d5cc2a55b2d47cc24a14c1842c023 100644 (file)
@@ -140,7 +140,7 @@ void primary() : {
   |   LOOKAHEAD( name() "(" ) name() function_call_args()\r
   | component_reference()\r
   // | "(" output_expression_list() ")"\r
-  // | "[" expression_list() { ";" expression_list() } "]"\r
+  | "[" expression_list() ( ";" expression_list() )* "]"\r
   //| "{" function_arguments() "}"\r
   | "end"\r
 }\r
@@ -187,4 +187,9 @@ void named_arguments() : {
 void named_argument() : {\r
 } {    \r
        <IDENT> "=" expression()\r
-}
\ No newline at end of file
+}\r
+\r
+void expression_list() : {\r
+} {\r
+    expression() ( "," expression() )*\r
+}\r
index a6259b14cf2124e511f61801dca89062b21d2076..280c59b6bc020bcf40c89c536b164ae3870abcd5 100644 (file)
@@ -5,13 +5,15 @@ import org.simantics.sysdyn.representation.IElement;
 import org.simantics.sysdyn.representation.Stock;\r
 import org.simantics.sysdyn.representation.Variable;\r
 import org.simantics.sysdyn.representation.expressions.IExpression;\r
+import org.simantics.sysdyn.representation.expressions.WithLookupExpression;\r
 \r
 public class ModelicaWriter {\r
 \r
     StringBuilder b = new StringBuilder();\r
     String app;\r
     boolean initialEquations = false;\r
-    \r
+    boolean insertInterpolate = false;\r
+\r
     public void write(Configuration conf) {\r
         conf.update();\r
         b.append("model ").append(conf.name).append('\n');\r
@@ -21,9 +23,10 @@ public class ModelicaWriter {
                 if(expr != null) {\r
                     app = expr.getDeclaration((Variable)element);\r
                     if (app != null) b.append(app);\r
+                    if (expr instanceof WithLookupExpression) insertInterpolate = true;\r
                 }\r
             }\r
-        \r
+\r
         for(IElement element : conf.elements)\r
             if(element instanceof Stock) {\r
                 IExpression expr = ((Stock)element).expression;\r
@@ -48,15 +51,36 @@ public class ModelicaWriter {
                 }\r
             }\r
         b.append("end ").append(conf.name).append(";\n");\r
+\r
+        if(insertInterpolate)\r
+            b.append(\r
+                    "\nfunction Interpolate\n" +\r
+                    "    input Real table[:, 2];\n" +\r
+                    "    input Real xp;\n" +\r
+                    "    output Real yp;\n" +\r
+                    "protected\n" +\r
+                    "    Integer i, sz;\n" +\r
+                    "algorithm\n" +\r
+                    "    sz := size(table,1);\n" +\r
+                    "    for i in 1:(sz-1) loop // assume monotonically increasing\n" +\r
+                    "    if ((i == 1 and xp <= table[i+1,1]) or\n" +\r
+                    "    (i == sz - 1) or\n" +\r
+                    "    (xp > table[i,1] and xp <= table[i+1,1])) then    // found appropriate segment\n" +\r
+                    "    yp := table[i,2] + (xp - table[i,1]) * (table[i+1,2] - table[i,2]) / (table[i+1,1] - table[i,1]);\n" +\r
+                    "    end if;\n" +\r
+                    "    end for;\n" +\r
+            " end Interpolate;\n");\r
+        \r
+\r
     }\r
-    \r
+\r
     public String escape(String name) {\r
         return name.replace(' ', '_');\r
     }\r
-    \r
+\r
     @Override\r
     public String toString() {\r
         return b.toString();\r
     }\r
-    \r
+\r
 }\r
index 0899b122ee36078908900f17cca8147679ff2510..56775723f229c6e9475ed5f60aa96b33718548ba 100644 (file)
@@ -1,9 +1,27 @@
 package org.simantics.sysdyn.representation.expressions;\r
 \r
 import org.simantics.objmap.annotations.GraphType;\r
+import org.simantics.objmap.annotations.RelatedValue;\r
+import org.simantics.sysdyn.representation.Variable;\r
 \r
 \r
 @GraphType("http://www.simantics.org/Sysdyn#WithLookupExpression")\r
 public class WithLookupExpression extends Expression {\r
 \r
+    @RelatedValue("http://www.simantics.org/Sysdyn#HasLookup")\r
+    private String lookupTable;\r
+    @RelatedValue("http://www.simantics.org/Sysdyn#HasEquation")\r
+    private String equation;\r
+\r
+    @Override\r
+    public String getDeclaration(Variable variable) {\r
+        return "    " + variable.getType() + " " + variable.getName() + ";\n";\r
+    }\r
+\r
+    @Override\r
+    public String getEquation(Variable variable) {\r
+        return \r
+        "    " + variable.getName() + " = Interpolate(table = " + lookupTable + ", xp = " + equation + ");\n";\r
+    }\r
+\r
 }\r