From 5b873d1f4dd91cc17ac80e66ceeeb875337507d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag=20Br=C3=BCck?= Date: Mon, 1 Jul 2024 15:09:38 +0200 Subject: [PATCH] Updated to handle indirect parameter file reading --- .../MasterParameter/InitializeArray.mo | 13 +++++++++++++ .../MasterParameter/InitializeParameters.mo | 9 ++------- examples/MasterParameter/MasterParameter/ParSet.mo | 4 ++-- examples/MasterParameter/MasterParameter/Test.mo | 2 +- examples/MasterParameter/MasterParameter/package.mo | 8 ++++++-- .../MasterParameter/MasterParameter/package.order | 1 + examples/MasterParameter/masterfile.txt | 2 +- 7 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 examples/MasterParameter/MasterParameter/InitializeArray.mo diff --git a/examples/MasterParameter/MasterParameter/InitializeArray.mo b/examples/MasterParameter/MasterParameter/InitializeArray.mo new file mode 100644 index 0000000..4af3433 --- /dev/null +++ b/examples/MasterParameter/MasterParameter/InitializeArray.mo @@ -0,0 +1,13 @@ +within MasterParameter; +function InitializeArray "Reads initial values for vector parameter" + input String filename "File to read parameters from"; + output Real v[:] "Parameter vector from file"; +protected + Integer n=Modelica.Utilities.Streams.countLines(filename); + String data[:]=Modelica.Utilities.Streams.readFile(filename); +algorithm + v := fill(0.0, n); // Initialization needed to get the right size + for i in 1:n loop + v[i] := Modelica.Utilities.Strings.scanReal(data[i]); + end for; +end InitializeArray; diff --git a/examples/MasterParameter/MasterParameter/InitializeParameters.mo b/examples/MasterParameter/MasterParameter/InitializeParameters.mo index 3ded648..ff1d1a0 100644 --- a/examples/MasterParameter/MasterParameter/InitializeParameters.mo +++ b/examples/MasterParameter/MasterParameter/InitializeParameters.mo @@ -2,12 +2,7 @@ within MasterParameter; function InitializeParameters "Reads initial values for parameter set" input String filename "File to read parameters from"; output ParSet p "Parameter set from file"; -protected - Integer n=Modelica.Utilities.Streams.countLines(filename); - String data[:]=Modelica.Utilities.Streams.readFile(filename); algorithm - p.J := fill(0.0, n); // Initialization needed to get the right size - for i in 1:n loop - p.J[i] := Modelica.Utilities.Strings.scanReal(data[i]); - end for; + p.datafile :=Modelica.Utilities.Streams.readLine(filename, 1); + p.J := InitializeArray(p.datafile); end InitializeParameters; diff --git a/examples/MasterParameter/MasterParameter/ParSet.mo b/examples/MasterParameter/MasterParameter/ParSet.mo index 4432f1f..546c3b8 100644 --- a/examples/MasterParameter/MasterParameter/ParSet.mo +++ b/examples/MasterParameter/MasterParameter/ParSet.mo @@ -1,5 +1,5 @@ within MasterParameter; record ParSet "Simple parameter set with array data" - // Old fixed-sized array: Real J[4] = { 1.0, 1.0, 1.0, 1.0}; - Real J[:] "Component index and inertia" annotation(__Dymola_UnknownArray=true); + String datafile "Name of real parameter file"; + Real J[:] "Component inertia" annotation(__Dymola_UnknownArray=true); end ParSet; diff --git a/examples/MasterParameter/MasterParameter/Test.mo b/examples/MasterParameter/MasterParameter/Test.mo index 41e7305..ba4b68c 100644 --- a/examples/MasterParameter/MasterParameter/Test.mo +++ b/examples/MasterParameter/MasterParameter/Test.mo @@ -1,6 +1,6 @@ within MasterParameter; model Test "Use package parameter" - parameter MasterParameter.ParSet par=InitializeParameters("parameterfile.txt") + parameter MasterParameter.ParSet par=InitializeParameters("masterfile.txt") annotation (Evaluate=false); extends Modelica.Mechanics.Rotational.Examples.CoupledClutches( J1(J=par.J[1]), diff --git a/examples/MasterParameter/MasterParameter/package.mo b/examples/MasterParameter/MasterParameter/package.mo index 4ef11ea..4c59424 100644 --- a/examples/MasterParameter/MasterParameter/package.mo +++ b/examples/MasterParameter/MasterParameter/package.mo @@ -4,9 +4,13 @@ package MasterParameter "Demonstrate use of parameter file indirection" annotation (uses(Modelica(version="4.0.0")), Documentation(info="This package demonstrates a recent feature in Dymola, arrays with variable size used as global parameters. We have three parts: -- A parameter record with an array parameter, whoch size is not known at compile time. +- A parameter record comprising + - The name of the actual parameter data. + - An array parameter, which size is not known at compile time. - An initialization function that reads data from a file and initializes the parameter set. -- A test model that reads parameters from a file and sets components parameters accordingly. + - To do this the master file is read in order to get the name of the actual parameter data. + - Then the parameter data is read from the designated file. +- A test model that reads parameters from two files and sets components parameters accordingly. "), __Dymola_UserMetaData(MetaData(category="User Meta Data", value={{"Author","Dag Brück"},{"Company", "Dassault Systèmes AB"}}))); end MasterParameter; diff --git a/examples/MasterParameter/MasterParameter/package.order b/examples/MasterParameter/MasterParameter/package.order index bb0b76f..b7eee52 100644 --- a/examples/MasterParameter/MasterParameter/package.order +++ b/examples/MasterParameter/MasterParameter/package.order @@ -1,3 +1,4 @@ ParSet +InitializeArray InitializeParameters Test diff --git a/examples/MasterParameter/masterfile.txt b/examples/MasterParameter/masterfile.txt index 59ad90c..0b0c201 100644 --- a/examples/MasterParameter/masterfile.txt +++ b/examples/MasterParameter/masterfile.txt @@ -1 +1 @@ -parmeterfile.txt +parameterfile.txt