From 5bfc6fa16467b8d4853aa2c3eadc74c4283e1c00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag=20Br=C3=BCck?= Date: Fri, 28 Jun 2024 15:34:43 +0200 Subject: [PATCH] Added example with variable array parameter --- .../VariableParameter/InitializeParameters.mo | 13 +++++++++++++ .../VariableParameter/ParSet.mo | 5 +++++ .../VariableParameter/Test.mo | 18 ++++++++++++++++++ .../VariableParameter/package.mo | 13 +++++++++++++ .../VariableParameter/package.order | 3 +++ examples/VariableParameter/parameterfile.txt | 4 ++++ 6 files changed, 56 insertions(+) create mode 100644 examples/VariableParameter/VariableParameter/InitializeParameters.mo create mode 100644 examples/VariableParameter/VariableParameter/ParSet.mo create mode 100644 examples/VariableParameter/VariableParameter/Test.mo create mode 100644 examples/VariableParameter/VariableParameter/package.mo create mode 100644 examples/VariableParameter/VariableParameter/package.order create mode 100644 examples/VariableParameter/parameterfile.txt diff --git a/examples/VariableParameter/VariableParameter/InitializeParameters.mo b/examples/VariableParameter/VariableParameter/InitializeParameters.mo new file mode 100644 index 0000000..350b02f --- /dev/null +++ b/examples/VariableParameter/VariableParameter/InitializeParameters.mo @@ -0,0 +1,13 @@ +within VariableParameter; +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/VariableParameter/VariableParameter/ParSet.mo b/examples/VariableParameter/VariableParameter/ParSet.mo new file mode 100644 index 0000000..d347c54 --- /dev/null +++ b/examples/VariableParameter/VariableParameter/ParSet.mo @@ -0,0 +1,5 @@ +within VariableParameter; +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/VariableParameter/VariableParameter/Test.mo b/examples/VariableParameter/VariableParameter/Test.mo new file mode 100644 index 0000000..ce7ea38 --- /dev/null +++ b/examples/VariableParameter/VariableParameter/Test.mo @@ -0,0 +1,18 @@ +within VariableParameter; +model Test "Use package parameter" + parameter VariableParameter.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/VariableParameter/VariableParameter/package.mo b/examples/VariableParameter/VariableParameter/package.mo new file mode 100644 index 0000000..557856c --- /dev/null +++ b/examples/VariableParameter/VariableParameter/package.mo @@ -0,0 +1,13 @@ +within ; +package VariableParameter "Demonstrate use of global parameter record" + + 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 VariableParameter; diff --git a/examples/VariableParameter/VariableParameter/package.order b/examples/VariableParameter/VariableParameter/package.order new file mode 100644 index 0000000..bb0b76f --- /dev/null +++ b/examples/VariableParameter/VariableParameter/package.order @@ -0,0 +1,3 @@ +ParSet +InitializeParameters +Test diff --git a/examples/VariableParameter/parameterfile.txt b/examples/VariableParameter/parameterfile.txt new file mode 100644 index 0000000..f01c5c5 --- /dev/null +++ b/examples/VariableParameter/parameterfile.txt @@ -0,0 +1,4 @@ +0.8 +0.9 +1.1 +1.2