Skip to content

Commit

Permalink
Added example with variable array parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Dag Brück committed Jun 28, 2024
1 parent 74b5023 commit 5bfc6fa
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 5 additions & 0 deletions examples/VariableParameter/VariableParameter/ParSet.mo
Original file line number Diff line number Diff line change
@@ -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;
18 changes: 18 additions & 0 deletions examples/VariableParameter/VariableParameter/Test.mo
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 13 additions & 0 deletions examples/VariableParameter/VariableParameter/package.mo
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions examples/VariableParameter/VariableParameter/package.order
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ParSet
InitializeParameters
Test
4 changes: 4 additions & 0 deletions examples/VariableParameter/parameterfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0.8
0.9
1.1
1.2

0 comments on commit 5bfc6fa

Please sign in to comment.