forked from OpenModelica/OMPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_linearization.py
More file actions
108 lines (98 loc) · 3.7 KB
/
test_linearization.py
File metadata and controls
108 lines (98 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import OMPython
import tempfile
import shutil
import unittest
import pathlib
import numpy as np
class Test_Linearization(unittest.TestCase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.tmp = pathlib.Path(tempfile.mkdtemp(prefix='tmpOMPython.tests'))
with open(self.tmp / "linearTest.mo", "w") as fout:
fout.write("""
model linearTest
Real x1(start=1);
Real x2(start=-2);
Real x3(start=3);
Real x4(start=-5);
parameter Real a=3,b=2,c=5,d=7,e=1,f=4;
equation
a*x1 = b*x2 -der(x1);
der(x2) + c*x3 + d*x1 = x4;
f*x4 - e*x3 - der(x3) = x1;
der(x4) = x1 + x2 + der(x3) + x4;
end linearTest;
""")
def __del__(self):
shutil.rmtree(self.tmp, ignore_errors=True)
def test_example(self):
filePath = (self.tmp / "linearTest.mo").as_posix()
mod = OMPython.ModelicaSystem(filePath, "linearTest")
[A, B, C, D] = mod.linearize()
expected_matrixA = [[-3, 2, 0, 0], [-7, 0, -5, 1], [-1, 0, -1, 4], [0, 1, -1, 5]]
assert A == expected_matrixA, f"Matrix does not match the expected value. Got: {A}, Expected: {expected_matrixA}"
assert B == [], f"Matrix does not match the expected value. Got: {B}, Expected: {[]}"
assert C == [], f"Matrix does not match the expected value. Got: {C}, Expected: {[]}"
assert D == [], f"Matrix does not match the expected value. Got: {D}, Expected: {[]}"
assert mod.getLinearInputs() == []
assert mod.getLinearOutputs() == []
assert mod.getLinearStates() == ["x1", "x2", "x3", "x4"]
def test_getters(self):
model_file = self.tmp / "pendulum.mo"
model_file.write_text("""
model Pendulum
Real phi(start=Modelica.Constants.pi, fixed=true);
Real omega(start=0, fixed=true);
input Real u1;
input Real u2;
output Real y1;
output Real y2;
parameter Real l = 1.2;
parameter Real g = 9.81;
equation
der(phi) = omega + u2;
der(omega) = -g/l * sin(phi);
y1 = y2 + 0.5*omega;
y2 = phi + u1;
end Pendulum;
""")
mod = OMPython.ModelicaSystem(model_file.as_posix(), "Pendulum", ["Modelica"])
d = mod.getLinearizationOptions()
assert isinstance(d, dict)
assert "startTime" in d
assert "stopTime" in d
assert mod.getLinearizationOptions(["stopTime", "startTime"]) == [d["stopTime"], d["startTime"]]
mod.setLinearizationOptions("stopTime=0.02")
assert mod.getLinearizationOptions("stopTime") == ["0.02"]
mod.setInputs(["u1=10", "u2=0"])
[A, B, C, D] = mod.linearize()
g = float(mod.getParameters("g")[0])
l = float(mod.getParameters("l")[0])
assert mod.getLinearInputs() == ["u1", "u2"]
assert mod.getLinearStates() == ["omega", "phi"]
assert mod.getLinearOutputs() == ["y1", "y2"]
assert np.isclose(A, [[0, g/l], [1, 0]]).all()
assert np.isclose(B, [[0, 0], [0, 1]]).all()
assert np.isclose(C, [[0.5, 1], [0, 1]]).all()
assert np.isclose(D, [[1, 0], [1, 0]]).all()
# test LinearizationResult
result = mod.linearize()
assert result[0] == A
assert result[1] == B
assert result[2] == C
assert result[3] == D
with self.assertRaises(KeyError):
result[4]
A2, B2, C2, D2 = result
assert A2 == A
assert B2 == B
assert C2 == C
assert D2 == D
assert result.n == 2
assert result.m == 2
assert result.p == 2
assert np.isclose(result.x0, [0, np.pi]).all()
assert np.isclose(result.u0, [10, 0]).all()
assert result.stateVars == ["omega", "phi"]
assert result.inputVars == ["u1", "u2"]
assert result.outputVars == ["y1", "y2"]