]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Replaced interpolate function with a proper one
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 23 Aug 2010 12:19:29 +0000 (12:19 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 23 Aug 2010 12:19:29 +0000 (12:19 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@17470 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java

index 21aef064e583dfd1c4cb18da223a292f01cff5fe..7949b38d6a2a22899f2d1daf0a86ad04cb40178f 100644 (file)
@@ -41,20 +41,46 @@ public class ModelicaWriter {
             // THIS INTERPOLATE DOESN'T WORK PROPERLY\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
+                    "  input Real u \"in1;put value (first column of table)\";\n" +\r
+                    "  input Real table[:, :] \"table to be interpolated\";\n" +\r
+                    "  input Integer icol \"column of table to be interpolated\";\n" +\r
+                    "  output Real y \"interpolated input value (icol column of table)\";\n" +\r
+                    "protected \n" +\r
+                    "  Integer i;\n" +\r
+                    "  Integer n \"number of rows of table\";\n" +\r
+                    "  Real u1;\n" +\r
+                    "  Real u2;\n" +\r
+                    "  Real y1;\n" +\r
+                    "  Real y2;\n" +\r
+                    "algorithm \n" +\r
+                    "  n := size(table, 1);\n" +\r
+                    "  if n <= 1 then\n" +\r
+                    "          y := table[1, icol];\n" +\r
+                    "  else\n" +\r
+                    "          // Search interval\n" +\r
+                    "          if u <= table[1, 1] then\n" +\r
+                    "                  i := 1;\n" +\r
+                    "          else\n" +\r
+                    "                  i := 2;\n" +\r
+                    "                  while i < n and u > table[i, 1] loop\n" +\r
+                    "                          i := i + 1;\n" +\r
+                    "                  end while;\n" +\r
+                    "                  i := i - 1;\n" +\r
+                    "          end if;\n" +\r
+                      \r
+                    "          // Get interpolation data\n" +\r
+                    "          u1 := table[i, 1];\n" +\r
+                    "          u2 := table[i + 1, 1];\n" +\r
+                    "          y1 := table[i, icol];\n" +\r
+                    "          y2 := table[i + 1, icol];\n" +\r
+                    "          // Interpolate\n" +\r
+                    "          if u1 >= u2 then\n" +\r
+                    "                  y := y1;\n" +\r
+                    "                  // since not possible to throw error\n" +\r
+                    "          else\n" +\r
+                    "                  y := y1 + (y2 - y1)*(u - u1)/(u2 - u1);\n" +\r
+                    "          end if;\n" +\r
+                    "  end if;\n" +\r
             "end Interpolate;\n");\r
 \r
             b.append(\r