-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathpassivity_test.py
More file actions
140 lines (101 loc) · 3.38 KB
/
passivity_test.py
File metadata and controls
140 lines (101 loc) · 3.38 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'''
Author: Mark Yeatman
Date: May 30, 2022
'''
import pytest
import numpy
from control import ss, passivity, tf, sample_system, parallel, feedback
from control.exception import ControlArgument, ControlDimension
pytestmark = pytest.mark.cvxopt
def test_ispassive_ctime():
A = numpy.array([[0, 1], [-2, -2]])
B = numpy.array([[0], [1]])
C = numpy.array([[-1, 2]])
D = numpy.array([[1.5]])
sys = ss(A, B, C, D)
# happy path is passive
assert(passivity.ispassive(sys))
# happy path not passive
D = -D
sys = ss(A, B, C, D)
assert(not passivity.ispassive(sys))
def test_ispassive_dtime():
A = numpy.array([[0, 1], [-2, -2]])
B = numpy.array([[0], [1]])
C = numpy.array([[-1, 2]])
D = numpy.array([[1.5]])
sys = ss(A, B, C, D)
sys = sample_system(sys, 1, method='bilinear')
assert(passivity.ispassive(sys))
def test_passivity_indices_ctime():
sys = tf([1, 1, 5, 0.1], [1, 2, 3, 4])
iff_index = passivity.get_input_ff_index(sys)
ofb_index = passivity.get_output_fb_index(sys)
assert(isinstance(ofb_index, float))
sys_ff = parallel(sys, -iff_index)
sys_fb = feedback(sys, ofb_index, sign=1)
assert(sys_ff.ispassive())
assert(sys_fb.ispassive())
sys_ff = parallel(sys, -iff_index-1e-6)
sys_fb = feedback(sys, ofb_index+1e-6, sign=1)
assert(not sys_ff.ispassive())
assert(not sys_fb.ispassive())
def test_passivity_indices_dtime():
sys = tf([1, 1, 5, 0.1], [1, 2, 3, 4])
sys = sample_system(sys, Ts=0.1)
iff_index = passivity.get_input_ff_index(sys)
ofb_index = passivity.get_output_fb_index(sys)
assert(isinstance(iff_index, float))
sys_ff = parallel(sys, -iff_index)
sys_fb = feedback(sys, ofb_index, sign=1)
assert(sys_ff.ispassive())
assert(sys_fb.ispassive())
sys_ff = parallel(sys, -iff_index-1e-2)
sys_fb = feedback(sys, ofb_index+1e-2, sign=1)
assert(not sys_ff.ispassive())
assert(not sys_fb.ispassive())
def test_system_dimension():
A = numpy.array([[0, 1], [-2, -2]])
B = numpy.array([[0], [1]])
C = numpy.array([[-1, 2], [0, 1]])
D = numpy.array([[1.5], [1]])
sys = ss(A, B, C, D)
with pytest.raises(ControlDimension):
passivity.ispassive(sys)
A_d = numpy.array([[-2, 0], [0, 0]])
A = numpy.array([[-3, 0], [0, -2]])
B = numpy.array([[0], [1]])
C = numpy.array([[-1, 2]])
D = numpy.array([[1.5]])
@pytest.mark.parametrize(
"system_matrices, expected",
[((A, B, C, D*0), True),
((A_d, B, C, D), True),
((A, B*0, C*0, D), True),
((A*0, B, C, D), True),
((A*0, B*0, C*0, D*0), True)])
def test_ispassive_edge_cases(system_matrices, expected):
sys = ss(*system_matrices)
assert passivity.ispassive(sys) == expected
def test_rho_and_nu_are_none():
A = numpy.array([[0]])
B = numpy.array([[0]])
C = numpy.array([[0]])
D = numpy.array([[0]])
sys = ss(A, B, C, D)
with pytest.raises(ControlArgument):
passivity.solve_passivity_LMI(sys)
def test_transfer_function():
sys = tf([1], [1, 2])
assert(passivity.ispassive(sys))
sys = tf([1], [1, -2])
assert(not passivity.ispassive(sys))
def test_oo_style():
A = numpy.array([[0, 1], [-2, -2]])
B = numpy.array([[0], [1]])
C = numpy.array([[-1, 2]])
D = numpy.array([[1.5]])
sys = ss(A, B, C, D)
assert(sys.ispassive())
sys = tf([1], [1, 2])
assert(sys.ispassive())