From edf115f4392c7020fabab53c8a46ed9d764db236 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag=20Br=C3=BCck?= Date: Mon, 1 Jul 2024 14:46:50 +0200 Subject: [PATCH] Initial version of file indirection --- .../MasterParameter/InitializeParameters.mo | 13 +++++++++++++ .../MasterParameter/MasterParameter/ParSet.mo | 5 +++++ .../MasterParameter/MasterParameter/Test.mo | 18 ++++++++++++++++++ .../MasterParameter/MasterParameter/package.mo | 12 ++++++++++++ .../MasterParameter/package.order | 3 +++ examples/MasterParameter/masterfile.txt | 1 + examples/MasterParameter/parameterfile.txt | 4 ++++ 7 files changed, 56 insertions(+) create mode 100644 examples/MasterParameter/MasterParameter/InitializeParameters.mo create mode 100644 examples/MasterParameter/MasterParameter/ParSet.mo create mode 100644 examples/MasterParameter/MasterParameter/Test.mo create mode 100644 examples/MasterParameter/MasterParameter/package.mo create mode 100644 examples/MasterParameter/MasterParameter/package.order create mode 100644 examples/MasterParameter/masterfile.txt create mode 100644 examples/MasterParameter/parameterfile.txt diff --git a/examples/MasterParameter/MasterParameter/InitializeParameters.mo b/examples/MasterParameter/MasterParameter/InitializeParameters.mo new file mode 100644 index 0000000..3ded648 --- /dev/null +++ b/examples/MasterParameter/MasterParameter/InitializeParameters.mo @@ -0,0 +1,13 @@ +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; +end InitializeParameters; diff --git a/examples/MasterParameter/MasterParameter/ParSet.mo b/examples/MasterParameter/MasterParameter/ParSet.mo new file mode 100644 index 0000000..4432f1f --- /dev/null +++ b/examples/MasterParameter/MasterParameter/ParSet.mo @@ -0,0 +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); +end ParSet; diff --git a/examples/MasterParameter/MasterParameter/Test.mo b/examples/MasterParameter/MasterParameter/Test.mo new file mode 100644 index 0000000..41e7305 --- /dev/null +++ b/examples/MasterParameter/MasterParameter/Test.mo @@ -0,0 +1,18 @@ +within MasterParameter; +model Test "Use package parameter" + parameter MasterParameter.ParSet par=InitializeParameters("parameterfile.txt") + annotation (Evaluate=false); + extends Modelica.Mechanics.Rotational.Examples.CoupledClutches( + J1(J=par.J[1]), + J2(J=par.J[2]), + J3(J=par.J[3]), + J4(J=par.J[4])); + annotation ( + Documentation(figures={Figure( + title="Plot", + preferred=true, + plots={Plot( + curves={Curve(x=time, y=J1.w),Curve(x=time, y=J2.w),Curve(x=time, y=J3.w),Curve(x=time, y=J4.w)}, + y=Axis(label="", unit="rad/s"))}, + caption="Plot rotational speeds")})); +end Test; diff --git a/examples/MasterParameter/MasterParameter/package.mo b/examples/MasterParameter/MasterParameter/package.mo new file mode 100644 index 0000000..4ef11ea --- /dev/null +++ b/examples/MasterParameter/MasterParameter/package.mo @@ -0,0 +1,12 @@ +within ; +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. +- 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. +"), __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 new file mode 100644 index 0000000..bb0b76f --- /dev/null +++ b/examples/MasterParameter/MasterParameter/package.order @@ -0,0 +1,3 @@ +ParSet +InitializeParameters +Test diff --git a/examples/MasterParameter/masterfile.txt b/examples/MasterParameter/masterfile.txt new file mode 100644 index 0000000..59ad90c --- /dev/null +++ b/examples/MasterParameter/masterfile.txt @@ -0,0 +1 @@ +parmeterfile.txt diff --git a/examples/MasterParameter/parameterfile.txt b/examples/MasterParameter/parameterfile.txt new file mode 100644 index 0000000..f01c5c5 --- /dev/null +++ b/examples/MasterParameter/parameterfile.txt @@ -0,0 +1,4 @@ +0.8 +0.9 +1.1 +1.2