-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunittest_hohmann_transfer
128 lines (106 loc) · 5.38 KB
/
unittest_hohmann_transfer
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
classdef HohmannTransferTest < matlab.unittest.TestCase
% Test class for HohmannTransfer implementation
properties (Constant)
% Test tolerance for floating-point comparisons
EPSILON = 1e-10
% Common orbital altitudes (km)
LEO_ALTITUDE = 400 % Low Earth Orbit
MEO_ALTITUDE = 20200 % Medium Earth Orbit (GPS)
GEO_ALTITUDE = 35786 % Geostationary Orbit
end
properties
% Test objects
transferLEOtoGEO
transferLEOtoMEO
end
methods(TestMethodSetup)
function createTestObjects(testCase)
% Create test objects before each test
testCase.transferLEOtoGEO = HohmannTransfer(testCase.LEO_ALTITUDE, testCase.GEO_ALTITUDE);
testCase.transferLEOtoMEO = HohmannTransfer(testCase.LEO_ALTITUDE, testCase.MEO_ALTITUDE);
end
end
methods(Test)
function testConstructorValidation(testCase)
% Test input validation
testCase.verifyError(@() HohmannTransfer(-100, 1000), 'MATLAB:validators:mustBePositive');
testCase.verifyError(@() HohmannTransfer(0, 1000), 'MATLAB:validators:mustBePositive');
testCase.verifyError(@() HohmannTransfer(1000, -100), 'MATLAB:validators:mustBePositive');
testCase.verifyError(@() HohmannTransfer(1000, 0), 'MATLAB:validators:mustBePositive');
testCase.verifyError(@() HohmannTransfer('invalid', 1000), 'MATLAB:validators:mustBeNumeric');
end
function testOrbitalParameters(testCase)
% Test orbital radius calculations
transfer = testCase.transferLEOtoGEO;
R_EARTH = 6378.137;
expectedR1 = testCase.LEO_ALTITUDE + R_EARTH;
expectedR2 = testCase.GEO_ALTITUDE + R_EARTH;
expectedSMA = (expectedR1 + expectedR2) / 2;
testCase.verifyEqual(transfer.r1, expectedR1, 'AbsTol', testCase.EPSILON);
testCase.verifyEqual(transfer.r2, expectedR2, 'AbsTol', testCase.EPSILON);
testCase.verifyEqual(transfer.a_transfer, expectedSMA, 'AbsTol', testCase.EPSILON);
end
function testDeltaVCalculations(testCase)
% Test delta-V calculations for LEO to GEO transfer
transfer = testCase.transferLEOtoGEO;
MU = 398600.4418;
% Manual calculation of expected values
r1 = transfer.r1;
r2 = transfer.r2;
a = transfer.a_transfer;
v1 = sqrt(MU/r1);
v2 = sqrt(MU/r2);
vt1 = sqrt(MU * (2/r1 - 1/a));
vt2 = sqrt(MU * (2/r2 - 1/a));
expectedDeltaV1 = vt1 - v1;
expectedDeltaV2 = v2 - vt2;
expectedTotalDeltaV = expectedDeltaV1 + expectedDeltaV2;
% Verify calculations
testCase.verifyEqual(transfer.delta_v_departure, expectedDeltaV1, 'AbsTol', testCase.EPSILON);
testCase.verifyEqual(transfer.delta_v_arrival, expectedDeltaV2, 'AbsTol', testCase.EPSILON);
testCase.verifyEqual(transfer.delta_v_total, expectedTotalDeltaV, 'AbsTol', testCase.EPSILON);
end
function testTransferTime(testCase)
% Test transfer time calculation
transfer = testCase.transferLEOtoGEO;
MU = 398600.4418;
expectedTime = pi * sqrt(transfer.a_transfer^3 / MU);
testCase.verifyEqual(transfer.transfer_time, expectedTime, 'AbsTol', testCase.EPSILON);
end
function testEdgeCases(testCase)
% Test edge cases with very small and large altitude differences
% Small altitude change
smallTransfer = HohmannTransfer(400, 401);
testCase.verifyGreaterThan(smallTransfer.delta_v_total, 0);
testCase.verifyLessThan(smallTransfer.delta_v_total, 0.1); % Should be very small
% Large altitude change
largeTransfer = HohmannTransfer(400, 100000);
testCase.verifyGreaterThan(largeTransfer.delta_v_total, 0);
testCase.verifyGreaterThan(largeTransfer.transfer_time, 0);
end
function testVisualization(testCase)
% Test that visualization runs without errors
transfer = testCase.transferLEOtoGEO;
% Verify no errors in visualization
try
fig = figure('Visible', 'off'); % Create invisible figure
transfer.visualizeTransfer();
close(fig);
testCase.verifyTrue(true); % If we get here, no errors occurred
catch
testCase.verifyTrue(false, 'Visualization failed');
end
end
function testPhysicalConsistency(testCase)
% Test physical consistency of the results
transfer = testCase.transferLEOtoMEO;
% Verify basic physical constraints
testCase.verifyGreaterThan(transfer.delta_v_total, 0);
testCase.verifyGreaterThan(transfer.transfer_time, 0);
% Verify that total delta-V is less than direct transfer
MU = 398600.4418;
directDeltaV = abs(sqrt(MU/transfer.r2) - sqrt(MU/transfer.r1));
testCase.verifyLessThan(transfer.delta_v_total, directDeltaV);
end
end
end