From c7bae80f2d94af237ae36d12df714cff2d75b992 Mon Sep 17 00:00:00 2001 From: lempinen Date: Mon, 23 Aug 2010 12:19:29 +0000 Subject: [PATCH] Replaced interpolate function with a proper one git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@17470 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/modelica/ModelicaWriter.java | 54 ++++++++++++++----- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java index 21aef064..7949b38d 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java @@ -41,20 +41,46 @@ public class ModelicaWriter { // THIS INTERPOLATE DOESN'T WORK PROPERLY b.append( "\nfunction Interpolate\n" + - " input Real table[:, 2];\n" + - " input Real xp;\n" + - " output Real yp;\n" + - "protected\n" + - " Integer i, sz;\n" + - "algorithm\n" + - " sz := size(table,1);\n" + - " for i in 1:(sz-1) loop // assume monotonically increasing\n" + - " if ((i == 1 and xp <= table[i+1,1]) or\n" + - " (i == sz - 1) or\n" + - " (xp > table[i,1] and xp <= table[i+1,1])) then // found appropriate segment\n" + - " yp := table[i,2] + (xp - table[i,1]) * (table[i+1,2] - table[i,2]) / (table[i+1,1] - table[i,1]);\n" + - " end if;\n" + - " end for;\n" + + " input Real u \"in1;put value (first column of table)\";\n" + + " input Real table[:, :] \"table to be interpolated\";\n" + + " input Integer icol \"column of table to be interpolated\";\n" + + " output Real y \"interpolated input value (icol column of table)\";\n" + + "protected \n" + + " Integer i;\n" + + " Integer n \"number of rows of table\";\n" + + " Real u1;\n" + + " Real u2;\n" + + " Real y1;\n" + + " Real y2;\n" + + "algorithm \n" + + " n := size(table, 1);\n" + + " if n <= 1 then\n" + + " y := table[1, icol];\n" + + " else\n" + + " // Search interval\n" + + " if u <= table[1, 1] then\n" + + " i := 1;\n" + + " else\n" + + " i := 2;\n" + + " while i < n and u > table[i, 1] loop\n" + + " i := i + 1;\n" + + " end while;\n" + + " i := i - 1;\n" + + " end if;\n" + + + " // Get interpolation data\n" + + " u1 := table[i, 1];\n" + + " u2 := table[i + 1, 1];\n" + + " y1 := table[i, icol];\n" + + " y2 := table[i + 1, icol];\n" + + " // Interpolate\n" + + " if u1 >= u2 then\n" + + " y := y1;\n" + + " // since not possible to throw error\n" + + " else\n" + + " y := y1 + (y2 - y1)*(u - u1)/(u2 - u1);\n" + + " end if;\n" + + " end if;\n" + "end Interpolate;\n"); b.append( -- 2.47.1