Skip to content

Commit

Permalink
Updated to handle indirect parameter file reading
Browse files Browse the repository at this point in the history
  • Loading branch information
Dag Brück committed Jul 1, 2024
1 parent edf115f commit 5b873d1
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 13 deletions.
13 changes: 13 additions & 0 deletions examples/MasterParameter/MasterParameter/InitializeArray.mo
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 2 additions & 2 deletions examples/MasterParameter/MasterParameter/ParSet.mo
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion examples/MasterParameter/MasterParameter/Test.mo
Original file line number Diff line number Diff line change
@@ -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]),
Expand Down
8 changes: 6 additions & 2 deletions examples/MasterParameter/MasterParameter/package.mo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
1 change: 1 addition & 0 deletions examples/MasterParameter/MasterParameter/package.order
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ParSet
InitializeArray
InitializeParameters
Test
2 changes: 1 addition & 1 deletion examples/MasterParameter/masterfile.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
parmeterfile.txt
parameterfile.txt

0 comments on commit 5b873d1

Please sign in to comment.