Skip to content

Commit

Permalink
Use imports consequently in Examples.StateSpace
Browse files Browse the repository at this point in the history
  • Loading branch information
tobolar committed May 14, 2024
1 parent 2f9b4de commit d81f4bd
Show file tree
Hide file tree
Showing 22 changed files with 338 additions and 467 deletions.
6 changes: 2 additions & 4 deletions Modelica_LinearSystems2/Examples/StateSpace/analysis.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ within Modelica_LinearSystems2.Examples.StateSpace;
function analysis "Example to check controllability of a state space system"
extends Modelica.Icons.Function;

import Modelica_LinearSystems2;
import Modelica_LinearSystems2.StateSpace;

input StateSpace ssi = Modelica_LinearSystems2.StateSpace(
input StateSpace ssi = StateSpace(
A=[-3,2,-3,4,5,6; 0,6,7,8,9,4; 0,2,3,0,78,6; 0,1,2,2,3,3; 0,13,34,0,0,1; 0,
0,0,-17,0,0],
B=[1,0; 0,1; 1,0; 0,1; 1,0; 0,1],
Expand Down Expand Up @@ -41,8 +40,7 @@ function analysis "Example to check controllability of a state space system"
output Boolean ok;

protected
StateSpace ss = if systemOnFile then
Modelica_LinearSystems2.StateSpace.Import.fromFile(fileName) else ssi;
StateSpace ss = if systemOnFile then StateSpace.Import.fromFile(fileName) else ssi;

algorithm
ok := false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function analysisControllability
import Modelica.Utilities.Streams.print;
import Modelica_LinearSystems2.Utilities.Types.StaircaseMethod;

input StateSpace ssi=Modelica_LinearSystems2.StateSpace(
input StateSpace ssi=StateSpace(
A=[1,0,0,0,0,0; 1,0,0,0,0,0; 0,2,3,0,78,6; 1,1,2,2,3,3; 10,13,34,0,0,1; 0,
0,0,2,0,0],
B=[0,0; 0,0; 0,0; 0,0; 1,0; 0,0],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function analysisControllablePoles
import Modelica_LinearSystems2.StateSpace;
import Modelica.Utilities.Streams.print;

input StateSpace ssi=Modelica_LinearSystems2.StateSpace(
input StateSpace ssi=StateSpace(
A=[1,0,0,0,0,0; 1,0,0,0,0,0; 0,2,3,0,78,6; 1,1,2,2,3,3; 10,13,34,0,0,1; 0,
0,0,2,0,0],
B=[0,0; 0,0; 0,0; 0,0; 1,0; 0,0],
Expand All @@ -25,8 +25,7 @@ function analysisControllablePoles
output Boolean ok;

protected
StateSpace ss = if systemOnFile then
Modelica_LinearSystems2.StateSpace.Import.fromFile(fileName) else ssi;
StateSpace ss = if systemOnFile then StateSpace.Import.fromFile(fileName) else ssi;
Real cPoles[:,2] "controllable poles";
Real ncPoles[:,2] "uncontrollable poles";

Expand Down
6 changes: 3 additions & 3 deletions Modelica_LinearSystems2/Examples/StateSpace/analysisDcGain.mo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function analysisDcGain "Example to compute dcGain of a state space system"
import Modelica_LinearSystems2.StateSpace;
import Modelica.Utilities.Streams.print;

input StateSpace ssi=Modelica_LinearSystems2.StateSpace(
input StateSpace ssi = StateSpace(
A=[1,0,0,0,0,0; 1,4,0,2,0,-1; 0,2,3,0,78,6; 1,1,2,2,3,3; 10,13,34,0,0,1; 3,
0,0,2,0,0],
B=[0,0; 0,0; 0,0; 0,0; 1,0; 0,0],
Expand All @@ -20,15 +20,15 @@ protected
Real K1[size(ssi.C,1), size(ssi.B,2)];

Real K2[2,1];
StateSpace ss2=Modelica_LinearSystems2.StateSpace(
StateSpace ss2 = StateSpace(
A=[0,0;
0,1],
B=[0;-2],
C=[1,0;0,1],
D=[0;0]);

Real K3[2,1];
StateSpace ss3=Modelica_LinearSystems2.StateSpace(
StateSpace ss3 = StateSpace(
A=[0,0;
0,1],
B=[5;-2],
Expand Down
109 changes: 49 additions & 60 deletions Modelica_LinearSystems2/Examples/StateSpace/analysisImpulseResponse.mo
Original file line number Diff line number Diff line change
@@ -1,60 +1,49 @@
within Modelica_LinearSystems2.Examples.StateSpace;
function analysisImpulseResponse "Impulse response example"
extends Modelica.Icons.Function;

import Modelica_LinearSystems2.StateSpace;

protected
Modelica_LinearSystems2.StateSpace sc=Modelica_LinearSystems2.StateSpace(
A=[-1,1; 0,-2],
B=[4,0; 0,2],
C=[2,0; 0,3],
D=[0,0; 0,0]);

Real t[:] "Time vector: (number of samples)";
Real x_continuous[:,size(sc.A, 1),size(sc.B, 2)]
"State trajectories: (number of samples) x (number of states) x (number of inputs)";

public
output Real y[:,size(sc.C, 1),size(sc.B, 2)]
"Output response: (number of samples) x (number of outputs) x (number of inuputs)";

algorithm
(y,t,x_continuous) := Modelica_LinearSystems2.StateSpace.Analysis.impulseResponse(sc=sc,dt=0.1,tSpan=5);

Modelica_LinearSystems2.Utilities.Plot.diagramVector({
Modelica_LinearSystems2.Utilities.Plot.Records.Diagram(
curve={
Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
x=t,
y=y[:,1,1],
legend="y1"),
Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
x=t,
y=y[:,2,1],
legend="y2")},
heading="Impulse response to u1",
xLabel="time [s]",
yLabel="y1, y2"),
Modelica_LinearSystems2.Utilities.Plot.Records.Diagram(
curve={
Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
x=t,
y=y[:,1,2],
legend="y1"),
Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
x=t,
y=y[:,2,2],
legend="y2")},
heading="Impulse response to u2",
xLabel="time [s]",
yLabel="y1, y2")});

annotation (
__Dymola_interactive=true,
Documentation(info="<html>
<p>
Computes and plots the impulse response of a state space system.
</p>
</html>"));
end analysisImpulseResponse;
within Modelica_LinearSystems2.Examples.StateSpace;
function analysisImpulseResponse "Impulse response example"
extends Modelica.Icons.Function;

import Modelica_LinearSystems2.StateSpace;
import Modelica_LinearSystems2.Utilities.Plot;

protected
StateSpace sc = StateSpace(
A=[-1,1; 0,-2],
B=[4,0; 0,2],
C=[2,0; 0,3],
D=[0,0; 0,0]);

Real t[:] "Time vector: (number of samples)";
Real x_continuous[:,size(sc.A, 1),size(sc.B, 2)]
"State trajectories: (number of samples) x (number of states) x (number of inputs)";

public
output Real y[:,size(sc.C, 1),size(sc.B, 2)]
"Output response: (number of samples) x (number of outputs) x (number of inuputs)";

algorithm
(y,t,x_continuous) := StateSpace.Analysis.impulseResponse(sc=sc,dt=0.1,tSpan=5);

Plot.diagramVector({
Plot.Records.Diagram(
curve={
Plot.Records.Curve(x=t, y=y[:,1,1], legend="y1"),
Plot.Records.Curve(x=t, y=y[:,2,1], legend="y2")},
heading="Impulse response to u1",
xLabel="time [s]",
yLabel="y1, y2"),
Plot.Records.Diagram(
curve={
Plot.Records.Curve(x=t, y=y[:,1,2], legend="y1"),
Plot.Records.Curve(x=t, y=y[:,2,2], legend="y2")},
heading="Impulse response to u2",
xLabel="time [s]",
yLabel="y1, y2")});

annotation (
__Dymola_interactive=true,
Documentation(info="<html>
<p>
Computes and plots the impulse response of a state space system.
</p>
</html>"));
end analysisImpulseResponse;
102 changes: 52 additions & 50 deletions Modelica_LinearSystems2/Examples/StateSpace/analysisInitialResponse.mo
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
within Modelica_LinearSystems2.Examples.StateSpace;
function analysisInitialResponse "Initial response example"
extends Modelica.Icons.Function;
import Modelica_LinearSystems2.StateSpace;

protected
Modelica_LinearSystems2.StateSpace sc=Modelica_LinearSystems2.StateSpace(
A=[-1,1; 0,-2],
B=[1,0; 0,1],
C=[1,0; 0,1],
D=[0,0; 0,0]);

Real t[:] "Time vector: (number of samples)";
Real x_continuous[:,2,1]
"State trajectories: (number of samples) x (number of states) x 1";
Real x0[2]=ones(2) "Initial state vector";

public
output Real y[:,size(sc.C, 1),1]
"Output response: (number of samples) x (number of outputs) x 1";

algorithm
(y,t,x_continuous) := Modelica_LinearSystems2.StateSpace.Analysis.initialResponse(x0=x0,sc=sc,dt=0.1,tSpan=5);

Modelica_LinearSystems2.Utilities.Plot.diagramVector({
Modelica_LinearSystems2.Utilities.Plot.Records.Diagram(
curve={
Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
x=t,
y=y[:,1,1],
legend="y1")},
heading="Initial response y1",
xLabel="time [s]"),
Modelica_LinearSystems2.Utilities.Plot.Records.Diagram(
curve={
Modelica_LinearSystems2.Utilities.Plot.Records.Curve(
x=t,
y=y[:,2,1],
legend="y2")},
heading="Initial response y2",
xLabel="time [s]")});

annotation (
__Dymola_interactive=true,
Documentation(info="<html>
<p>
Computes and plots the initial response of a state space system by given initial values.
</p>
</html>"));
end analysisInitialResponse;
within Modelica_LinearSystems2.Examples.StateSpace;
function analysisInitialResponse "Initial response example"
extends Modelica.Icons.Function;

import Modelica_LinearSystems2.StateSpace;
import Modelica_LinearSystems2.Utilities.Plot;

protected
StateSpace sc=StateSpace(
A=[-1,1; 0,-2],
B=[1,0; 0,1],
C=[1,0; 0,1],
D=[0,0; 0,0]);

Real t[:] "Time vector: (number of samples)";
Real x_continuous[:,2,1]
"State trajectories: (number of samples) x (number of states) x 1";
Real x0[2]=ones(2) "Initial state vector";

public
output Real y[:,size(sc.C, 1),1]
"Output response: (number of samples) x (number of outputs) x 1";

algorithm
(y,t,x_continuous) := StateSpace.Analysis.initialResponse(x0=x0,sc=sc,dt=0.1,tSpan=5);

Plot.diagramVector({
Plot.Records.Diagram(
curve={
Plot.Records.Curve(
x=t,
y=y[:,1,1],
legend="y1")},
heading="Initial response y1",
xLabel="time [s]"),
Plot.Records.Diagram(
curve={
Plot.Records.Curve(
x=t,
y=y[:,2,1],
legend="y2")},
heading="Initial response y2",
xLabel="time [s]")});

annotation (
__Dymola_interactive=true,
Documentation(info="<html>
<p>
Computes and plots the initial response of a state space system by given initial values.
</p>
</html>"));
end analysisInitialResponse;
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected
Complex Zeros[:];
Boolean ok;
algorithm
Zeros := Modelica_LinearSystems2.StateSpace.Analysis.invariantZeros(ss);
Zeros := StateSpace.Analysis.invariantZeros(ss);

if size(Zeros, 1) == 0 then
print("\nSystem\n "+String(zp)+"\nhas no invariant zeros\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function analysisObservability
import Modelica_LinearSystems2.StateSpace;
import Modelica.Utilities.Streams.print;

input StateSpace ssi=Modelica_LinearSystems2.StateSpace(
input StateSpace ssi = StateSpace(
A=[1,2,3,4,5,6; 5,6,7,8,9,4; 0,2,3,0,78,6; 1,1,2,2,3,3; 10,13,34,0,0,1; 0,0,0,2,0,1],
B=[1,0; 2,0; 0,1; 0,2; 0,0; 0,1],
C=[1,0,1,0,1,0; 0,1,0,0,0,0],
Expand All @@ -24,9 +24,7 @@ function analysisObservability
output Boolean ok;

protected
StateSpace ss = if systemOnFile then
Modelica_LinearSystems2.StateSpace.Import.fromFile(fileName) else ssi;

StateSpace ss = if systemOnFile then StateSpace.Import.fromFile(fileName) else ssi;
Boolean isObservable;
Modelica_LinearSystems2.Utilities.Types.StaircaseMethod method;
Boolean isSISO=StateSpace.Internal.isSISO(ss);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function analysisStairCase
input String matrixName="ABCD" "Name of the state space system matrix"
annotation(Dialog(group="system data definition",enable = systemOnFile));

input StateSpace ssi=Modelica_LinearSystems2.StateSpace(
input StateSpace ssi=StateSpace(
A=[1,2,3,4,5,6; 5,6,7,8,9,4; 0,2,3,0,78,6; 1,1,2,2,3,3; 10,13,34,0,0,1; 0,0,0,2,0,1],
B=[1; 2; 0; 0; 0; 0],
C=[1,0,1,0,1,0],
Expand All @@ -24,8 +24,7 @@ function analysisStairCase
output Boolean ok;

protected
StateSpace ss = if systemOnFile then
Modelica_LinearSystems2.StateSpace.Import.fromFile(fileName) else ssi;
StateSpace ss = if systemOnFile then StateSpace.Import.fromFile(fileName) else ssi;
StateSpace ss2 = StateSpace.Internal.transposeStateSpace(ss);
StateSpace ss3 = ss;
Boolean isControllable;
Expand Down
Loading

0 comments on commit d81f4bd

Please sign in to comment.