diff --git a/KeyWordIO/Examples/ReadReal_m.mo b/KeyWordIO/Examples/ReadReal_m.mo new file mode 100644 index 0000000..bcdcf4c --- /dev/null +++ b/KeyWordIO/Examples/ReadReal_m.mo @@ -0,0 +1,22 @@ +within KeyWordIO.Examples; +model ReadReal_m "Reads real parameters from m-file" + extends Modelica.Icons.Example; + import Modelica.Units.SI; + parameter String inputFileName = Modelica.Utilities.Files.loadResource("modelica://KeyWordIO/Resources/m/setParameters.m"); + parameter SI.Resistance R0=KeyWordIO.readReal_m(inputFileName, "data.R0"); + parameter SI.Resistance R1=KeyWordIO.readReal_m(inputFileName, "data.R1"); + parameter SI.Resistance R2=KeyWordIO.readReal_m(inputFileName, "data.R2"); + parameter SI.AngularVelocity w0(displayUnit="rpm")=KeyWordIO.readReal_m(inputFileName, "data.w0"); + annotation (experiment(StopTime = 1, Interval = 1E-3), Documentation(info=" +
+To store parameters for usage in Modelica and in Matlab/Simulink only once; +
+
Anton Haumer
Technical Consulting & Electrical Engineering
-3423 St. Andrae-Woerdern, Austria
+93049 Regensburg, Germany
email: a.haumer@haumer.at
\\0
warning in OpenModelica,
diff --git a/KeyWordIO/package.mo b/KeyWordIO/package.mo
index 8449786..4e5b732 100644
--- a/KeyWordIO/package.mo
+++ b/KeyWordIO/package.mo
@@ -2,48 +2,8 @@ within ;
package KeyWordIO "Read and write data files with key words"
extends Modelica.Icons.Package;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
annotation (
- version="0.10.X", versionDate = "2021-XX-XX",
+ version="0.11.X", versionDate = "2021-XX-XX",
uses(Modelica(version="4.0.0")),
conversion(from(version={"0.9.0", "0.8.0", "0.7.0", "0.6.0"},
script="modelica://KeyWordIO/Resources/Scripts/Conversion/ConvertFromKeyWordIO_before_0.9.0.mos")));
diff --git a/KeyWordIO/package.order b/KeyWordIO/package.order
index ad9ca4c..c1c9540 100644
--- a/KeyWordIO/package.order
+++ b/KeyWordIO/package.order
@@ -1,6 +1,7 @@
UsersGuide
Examples
readRealParameter
+readReal_m
readRealCSV
readStringCSV
readLineWithoutCache
diff --git a/KeyWordIO/readReal_m.mo b/KeyWordIO/readReal_m.mo
new file mode 100644
index 0000000..08597c1
--- /dev/null
+++ b/KeyWordIO/readReal_m.mo
@@ -0,0 +1,79 @@
+within KeyWordIO;
+function readReal_m "Read the value of a Real parameter from m-file"
+ extends Modelica.Icons.Function;
+ import Modelica.Utilities.*;
+ input String fileName "Name of file";
+ input String name "Name of parameter";
+ input Boolean cache = false "Read file before compiling, if true";
+ output Real result "Actual value of parameter on file";
+protected
+ Integer iline = 1;
+ String line;
+ Integer index;
+ Integer lenIdent;
+ String message = "in file \"" + fileName + "\" on line ";
+ Boolean found = false;
+ Boolean endOfFile = false;
+algorithm
+ (line, endOfFile) :=KeyWordIO.readLine(fileName, iline, cache);
+ while not found and not endOfFile loop
+ if Strings.isEqual(Strings.substring(line,1,2),"//") or
+ Strings.isEqual(Strings.substring(line,1,1),"%") or
+ Strings.isEqual(Strings.substring(line,1,1)," ") then
+ //skip comment lines (even if comment has trailing blanks before "//" or "%")
+ iline := iline + 1;
+ else
+ index := Strings.find(line, "=", 1); //get position of "="
+ if index <= 0 then //no expression
+ Streams.error("Erroneous expression without =" + message + String(iline));
+ else
+ //strip trailing blanks of identifier part before "="
+ lenIdent := Strings.find(line," ",1);
+ lenIdent := if lenIdent <=0 then index else min(index, lenIdent);
+ if not Strings.isEqual(Strings.substring(line, 1, lenIdent - 1), name) then
+ //identifier <> name: skip line
+ iline := iline + 1;
+ found := false;
+ else
+ //identifier == name: evaluate expression
+ //check for mandatory ";" after formula before optional comments
+ if Strings.find(line, ";", index+1) <=0 then
+ Streams.error("Expression not terminated by ;" + message + String(iline));
+ else
+ (result, index) := KeyWordIO.Strings.expression(line, index+1, message + String(iline));
+ found := true;
+ end if;
+ end if;
+ end if;
+ end if;
+ (line, endOfFile) :=KeyWordIO.readLine(fileName, iline, cache);
+ end while;
+ if not found then
+ Streams.error("Parameter \"" + name + "\" not found in file \"" + fileName + "\"");
+ end if;
+ annotation(Documentation(info="
+++result = readReal_m(fileName, name); +
+This function demonstrates how a function can be implemented +that reads the value of a parameter from a Matlab script-file. +The idea is to keep parameters in Modelica and in Matlab/Simulink up to date. +The function performs the following actions: +
+