Axel Schweiß
Axel Schweiß

Reputation: 11

Modelica/Dymola Run Linearized Model with Initial Values

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

Upvotes: 1

Views: 262

Answers (1)

Markus A.
Markus A.

Reputation: 7535

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");

Upvotes: 3

Related Questions