testSs.m 5.89 KB
Newer Older
1
2
3
4
5
function [tests] = testSs()
%TESTMISC Summary of this function goes here
%   Detailed explanation goes here
tests = functiontests(localfunctions());
end
6
7
8
9
10
11
12
13
14
15

function testErrorSignal(tc)
simOriginal.x = [2; 2];
simOriginal.u = 1;
simReference.x = [1; 1];
simError = misc.ss.errorSignal(simOriginal, simReference);
tc.verifyEqual(simError.x, [1; 1]);
tc.verifyEqual(fieldnames(simError), {'x'});
end

16
17
18
19
20
21
22
23
function testAdd2SignalName(tc)
mySys = misc.ss.add2signalName(ss(1, 1, [1; 1], [], 'OutputName', {'a', 'b'}, ...
		'InputName', {'c'}), ...
	'OutputName', 'front', 'ode.');
mySys = misc.ss.add2signalName(mySys, 'InputName', 'back', '.ode');
tc.verifyEqual(mySys.OutputName, {'ode.a'; 'ode.b'});
tc.verifyEqual(mySys.InputName, {'c.ode'});
end
24

25
function testCombineInputSignals(tc)
26
myStateSpace = ss(-1, [1, 2, 3], 1, [], 'InputName', {'control', 'disturbance', 'my.fault'});
27
28
29
t = linspace(0, 4, 201);
disturbanceSignal = quantity.Symbolic(sin(sym('t')), 'grid', t, ...
	'variable', sym('t'), 'name', 'disturbance');
30
31
32
33
faultSignal = quantity.Symbolic(sin(sym('t')), 'grid', t, ...
	'variable', sym('t'), 'name', 'my.fault');

% case 1: only disturbance
34
35
36
37
38
u = misc.ss.combineInputSignals(myStateSpace, t, 'disturbance', disturbanceSignal);
y = quantity.Discrete(lsim(myStateSpace, u.on(), t), 'grid', t, ...
	'gridName', 't', 'name', 'y');
odeResiduum = - y.diff(1, 't') - y + 2*disturbanceSignal;
tc.verifyEqual(odeResiduum.abs.median(), 0, 'AbsTol', 5e-4);
39
40
41
42
43
44
45
46

% case 2: fault and disturbance
u2 = misc.ss.combineInputSignals(myStateSpace, t, 'disturbance', disturbanceSignal, ...
	'my.fault', faultSignal);
y2 = quantity.Discrete(lsim(myStateSpace, u2.on(), t), 'grid', t, ...
	'gridName', 't', 'name', 'y');
odeResiduum = - y2.diff(1, 't') - y2 + 2*disturbanceSignal + 3*faultSignal;
tc.verifyEqual(odeResiduum.abs.median(), 0, 'AbsTol', 5e-4);
47
48
end

49
50
51
52
53
54
function testRemoveEnumeration(tc)
tc.verifyEqual(misc.ss.removeEnumeration({'a(1)', 'a(2)', 'b'}), {'a', 'a', 'b'});
tc.verifyEqual(misc.ss.removeEnumeration({'a(1)', 'a(2)', 'b'}, false), {'a', 'a', 'b'});
tc.verifyEqual(misc.ss.removeEnumeration({'a(1)', 'a(2)', 'b'}, true), {'a', 'b'});
end

55
56
57
58
59
60
61
62
63
64
65
66
67
function testRemoveInputOfOutput(tc)
mySimulationModelOriginal = ss(1, [1, 2, 3], [1; 2; 3], [1, 0, 0; 0, 0, 0; 0, 0, 0], ...
	'OutputName', {'x', 'y', 'z'}, 'InputName', {'u(1)', 'u(2)', 'a'});
mySimulationModel = misc.ss.addInput2Output(mySimulationModelOriginal);
mySimulationModel = misc.ss.removeInputOfOutput(mySimulationModel);

for myProp = {'A', 'B', 'C', 'D', 'Ts', 'InputName', 'OutputName', 'Name'}
	tc.verifyEqual(mySimulationModelOriginal.(myProp{1}), mySimulationModel.(myProp{1}), ...
		['The property ', myProp{1}, ' is faulty']);
end

end % testRemoveInputOfOutput

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
function testAddInput2Output(testCase)
% first example
mySimulationModelOriginal = ss(1, [1, 2, 3], [1; 2; 3], [1, 0, 0; 0, 0, 0; 0, 0, 0], ...
	'OutputName', {'x', 'y', 'z'}, 'InputName', {'u(1)', 'u(2)', 'a'});
mySimulationModelModified = misc.ss.addInput2Output(mySimulationModelOriginal);
verifyAddInput2Output(testCase, mySimulationModelModified, mySimulationModelOriginal);

% second example
mySimulationModelOriginal2 = ss(1, [1, 2, 3], [1; 2; 3], [1, 0, 0; 0, 0, 0; 0, 0, 0], ...
	'OutputName', {'x(1)', 'x(2)', 'y'}, 'InputName', {'u(1)', 'u(2)', 'y'});
mySimulationModelModified2 = misc.ss.addInput2Output(mySimulationModelOriginal2);
verifyAddInput2Output(testCase, mySimulationModelModified2, mySimulationModelOriginal2);

function testCase = verifyAddInput2Output(testCase, ssModified, ssOriginal)
	testCase.verifyEqual(ssModified.A, ssOriginal.A);
	testCase.verifyEqual(ssModified.B, ssOriginal.B);
	resultingOutputNames = unique([ssOriginal.OutputName; ssOriginal.InputName], 'stable');
	testCase.verifyEqual(ssModified.C, ...
		[ssOriginal.C; zeros(numel(resultingOutputNames)-size(ssOriginal.C, 1), size(ssOriginal.A, 1))]);
	testCase.verifyEqual(ssModified.D(1:size(ssOriginal.C, 1), :), ssOriginal.D);
	testCase.verifyEqual(size(ssModified.D), [numel(resultingOutputNames), size(ssOriginal.B, 2)]);
	for it = 1 : size(ssOriginal.B, 2)
		thisInputName = ssOriginal.InputName{it};
		if ~any(strcmp(ssOriginal.OutputName, thisInputName))
			testCase.verifyEqual(...
				ssModified.D(strcmp(ssModified.OutputName, thisInputName), ...
					strcmp(ssModified.InputName, thisInputName)), 1);
		end
	end
	testCase.verifyEqual(ssModified.InputName, ssOriginal.InputName);
	testCase.verifyEqual(ssModified.OutputName, resultingOutputNames);
end

101
end % testAddInput2Output
102
103
104
105
106
107

function testChangeSignalName(testCase)
mySimulationModel = ss(1, 1, [1; 2; 3], []);
mySimulationModel = misc.ss.setSignalName(mySimulationModel, 'output', {'a', 'b'}, {2, 1});
mySimulationModel = misc.ss.changeSignalName(mySimulationModel, {'a'}, {'c'});
testCase.verifyEqual(mySimulationModel.OutputName, {'c(1)'; 'c(2)'; 'b'})
108
end % testChangeSignalName
109
110
111
112
113
114
115

function testSetSignalName(testCase)
mySimulationModel = ss(1, [1, 0], [1; 2; 3], []);
mySimulationModel = misc.ss.setSignalName(mySimulationModel, 'output', {'a', 'b'}, {2, 1});
mySimulationModel = misc.ss.setSignalName(mySimulationModel, 'input', {'c'}, {2});
testCase.verifyEqual(mySimulationModel.OutputName, {'a(1)'; 'a(2)'; 'b'})
testCase.verifyEqual(mySimulationModel.InputName, {'c(1)'; 'c(2)'})
116
end % testSetSignalName
117
118
119
120
121
122
123
124
125
126
127
128

function testSimulationOuput2Quantity(testCase)
mySimulationModelA = ss(1, 1, [1; 2], [], 'OutputName', {'a'}, 'InputName', 'u');
mySimulationModelB = ss(1, 1, [1; 2], [], 'OutputName', {'b'}, 'InputName', 'u');
mySimulationModel = append(mySimulationModelA, mySimulationModelB);
myIc = 0.5*ones(size(mySimulationModel.A, 1), 1);
time = linspace(0, 1, 51);
myInput = zeros(numel(time), size(mySimulationModel, 2));
simulationOutputArray = lsim(mySimulationModel, myInput, time, myIc);
simulationOutputQuantity = misc.ss.simulationOutput2Quantity( ...
					simulationOutputArray, time, mySimulationModel.OutputName);
testCase.verifyEqual(simulationOutputQuantity.a.on(), simulationOutputQuantity.b.on());
129
end % testSimulationOuput2Quantity