1

I am new to Dymola and I want to run a linearized model with initial conditions.

I know how to Linearize it. I can get the StateSpace object in Command window or get the dslin.mat. Now I want to run it with initial conditions. I found them in the dsin.txt file, but cant bring them together. Is there an implemented way or do I need to write it on my own?

Best regards, Axel

1 Answers1

3

You can use the block Modelica.Blocks.Continuous.StateSpace to build a model containing a state-space description, as shown below:

Model with state-space block from the MSL

The respective code is:

model StateSpaceModel
  Modelica.Blocks.Continuous.StateSpace sys annotation (Placement(transformation(extent={{-10,-10},{10,10}})));
  Modelica.Blocks.Sources.Step step(startTime=0.5) annotation (Placement(transformation(extent={{-60,-10},{-40,10}})));
equation 
  connect(step.y, sys.u[1]) annotation (Line(points={{-39,0},{-12,0}}, color={0,0,127}));
  annotation (uses(Modelica(version="4.0.0")));
end StateSpaceModel;

Additionally you can use a script (or a Modelica function) that does some work for you. More precisely, it

  • linearizes any suitable model. I've used the state-space model from the MSL itself, so you can be sure the result is correct.
  • translates the above model to be able to set the parameters from the command line
  • sets the parameters of the state-space block called sys. This includes the ones for the initial conditions in x_start
  • simulates the model with the new parameters
// Get state-space description of a model
ss = Modelica_LinearSystems2.ModelAnalysis.Linearize("Modelica.Blocks.Continuous.StateSpace");

// Translate custom example, set parameters to result of the above linearization, add initial conditions for states and simulate
translateModel("StateSpaceModel")
sys.A = ss.A;
sys.B = ss.B;
sys.C = ss.C; // in case of an error here, check if 'OutputCPUtime == false;'
sys.D = ss.D;
sys.x_start = ones(size(sys.A,1));
simulateModel("StateSpaceModel", resultFile="StateSpaceModel");
Markus A.
  • 6,300
  • 10
  • 26
  • Hey thanks for your answer, that helped me a lot. I have one further question: In your example, we initialize only with 1s. But how do i get the initial conditions from my original model, that i linearized? – Axel Schweiß Nov 22 '21 at 10:18
  • Depends a bit on how you have set those start values. Would something like this help? `w_str = ModelManagement.Structure.AST.Components.ComponentModifiers("Modelica.Mechanics.Rotational.Examples.CoupledClutches","J1.w.start");` This reads the modifier for the start value of the rotational in the component J1. You would still need to drop the `=` and convert to a string, but this should be possible with `w_start = Modelica.Utilities.Strings.scanReal(w_str[1],2);` – Markus A. Nov 22 '21 at 11:24