Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/OrbitLaunchTests.h

Source at documentation-generation time. See Test catalog for fixture associations.

1#pragma once
2#include "../Modules/OrbitLaunch.h"
3
4Input LaunchFixture()
5{
6 auto in=BillEntryFixture();
7 in.massKg=124315.5859;in.fuelKg=26965;
8 in.entryForwardKN=in.entryReverseKN=in.entryUpKN=3003.7068;
9 in.entryDownKN=1398;in.entrySideKN=1427;in.thrustKN=in.entryForwardKN;in.flowKgS=2.7;
10 in.bodyDragAcceleration=1.05580505277*130827.7578/in.massKg;in.vneKms=.1526747;
11 SetE11PitchEnvelope(in);in.ascentConfiguration=true;
12 in.onGround=true;in.freeFlight=false;in.aglKm=0;
13 in.own={{0,in.radius+.3,0},{-in.spin*(in.radius+.3),0,0}};
14 in.entryVehicle.nose={0,0,1};in.bodyUp={0,1,0};in.entryVehicle.wind={};
15 in.entryVehicle.airVelocity={};in.entryVehicle.airIas=0;
16 in.targetName=L"TEST STATION";in.targetKind=TargetKind::Station;in.target={{0,in.radius+118,0},{0,0,std::sqrt(in.gm/(in.radius+118))}};
17 in.stationRate=std::sqrt(in.gm/std::pow(in.radius+118,3));
18 in.ascentAeroValid=true;
19 for(int j=0;j<73;++j) {
20 const double a=(-180+5*j)*Pi/180;
21 in.ascentAero[j]={a,E11Cl(a,false),E11Cd(a,false),1686.596*.09290304*E11Cl(-a-.5*Pi/180,true),
22 1686.596*.09290304*E11Cd(-a-.5*Pi/180,true)};
23 }
24 return in;
25}
26void LaunchTests()
27{
28 auto in=LaunchFixture();Request r;r.ascent=true;r.ascentOrbitSteering=true;r.arrivalEpoch=in.now+5400;
29 auto search=[&](const Input& input,const Request& request) {
30 LaunchPlanning::Search s;s.Begin(input,request);int iterations=0;
31 while(s.Pending()&&iterations++<2000) s.Work(256);
32 Check(!s.Pending(),"launch search terminates within fixed work bound");return s.Result();
33 };
34 const auto e=search(in,r);
35 Check(e.valid&&e.fuelEnough&&e.launchEpoch>=in.now&&e.insertionEpoch>e.launchEpoch&&e.insertionEpoch<e.meetEpoch,
36 "station MEET comparison predicts launch, insertion and transfer in time order");
37 Check(e.ascentFuel>0&&e.transferFuel>0&&e.deltaV>0&&e.earlierFuel>0&&e.laterFuel>0,
38 "selected MEET separates ascent and rendezvous fuel and shows neighboring deadlines");
39 Check(std::abs(e.earlierFuel-e.laterFuel)>1,"moving MEET changes the modeled time-energy tradeoff");
40 Check(e.evaluated>17&&e.evaluated<=29,"launch comparison refines its coarse departure samples with bounded work");
41 // Execute the selected ground wait, using the actual moving station state
42 // in current planet axes rather than a frozen target fixture.
43 auto waited=[&](double seconds) {
44 auto at=in;at.now+=seconds;
45 const auto target=TargetAt(in,seconds);
46 at.target={Rotate(target.r,{0,0,1},-in.spin*seconds),Rotate(target.v,{0,0,1},-in.spin*seconds)};
47 at.massKg-=seconds*in.idleFlowKgS;at.fuelKg-=seconds*in.idleFlowKgS;
48 at.bodyDragAcceleration=in.bodyDragAcceleration*in.massKg/at.massKg;
49 return at;
50 };
51 Executor armed;auto armedGuidance=armed.Begin(in,r,Solve(in,r),&e);
52 Check(armed.Active()&&armedGuidance.valid&&armedGuidance.ascentWaiting&&armedGuidance.phase==Phase::Wait&&
53 !armedGuidance.thrust&&!armedGuidance.attitude&&Norm(armedGuidance.accelerationCommand)==0,
54 "AUTO commits a future launch with no thrust or attitude commands");
55 Check(ManualAttitudeDisconnect(armedGuidance),"manual flight input cancels an armed ground wait");
56 const auto launchAt=armedGuidance.ascentLaunchEpoch;
57 auto before=waited(launchAt-in.now-.1);armedGuidance=armed.Step(before,r);
58 Check(armedGuidance.ascentWaiting&&armedGuidance.ascentLaunchEpoch==launchAt&&!armedGuidance.thrust,
59 "armed countdown retains its committed departure while the station moves");
60 auto atLaunch=waited(launchAt-in.now);armedGuidance=armed.Step(atLaunch,r);
61 Check(armedGuidance.valid&&armedGuidance.thrust&&armedGuidance.ascentLiftoff&&!armedGuidance.ascentWaiting,
62 "scheduled departure automatically enters the existing VTOL controller");
63 armed.Cancel();Check(!armed.Active()&&!armed.Step(atLaunch,r).thrust,"DOR off clears an armed departure");
64 for(int change=0;change<12;++change) {
65 armed.Begin(in,r,Solve(in,r),&e);auto altered=waited(1);auto settings=r;
66 if(change==0)altered.onGround=false;
67 if(change==1)altered.powered=false;
68 if(change==2)++altered.targetId;
69 if(change==3)altered.entryVehicle.airAge=1;
70 if(change==4)altered.ascentConfiguration=false;
71 if(change==5)settings.ascentOrbitSteering=!settings.ascentOrbitSteering;
72 if(change==6)altered.entryUpKN*=.9;
73 if(change==7)altered.entryVehicle.wind.x+=.02;
74 if(change==8)altered.bodyDragAcceleration*=2;
75 if(change==9)altered.target.r.x+=5;
76 if(change==10)altered.fuelKg=1;
77 if(change==11)++settings.revision;
78 const auto stopped=armed.Step(altered,settings);
79 Check(stopped.phase==Phase::Abort&&!stopped.valid&&!stopped.thrust&&!armed.Active(),
80 "ground wait cancels on manual departure, lost data, changed plan/configuration or forecast inputs");
81 }
82 auto pending=e;pending.pending=true;
83 auto planning=armed.Begin(in,r,Solve(in,r),&pending);
84 Check(armed.Active()&&planning.valid&&planning.ascentWaiting&&planning.ascentPlanning&&
85 !planning.thrust&&!planning.attitude,"arming a provisional candidate waits for the comparison without control output");
86 const auto comparingAt=waited(1);
87 planning=armed.Step(comparingAt,r,&pending);
88 Check(planning.ascentPlanning&&!planning.thrust&&Norm(planning.accelerationCommand)==0,
89 "unfinished comparison never starts the provisional launch");
90 auto finished=e;finished.checkedEpoch=comparingAt.now;
91 planning=armed.Step(comparingAt,r,&finished);
92 Check(planning.valid&&planning.ascentWaiting&&!planning.ascentPlanning&&planning.ascentLaunchEpoch==e.launchEpoch,
93 "armed comparison commits the completed recommendation without reengagement");
94 auto different=finished;different.launchEpoch+=60;
95 Check(armed.Step(waited(2),r,&different).ascentLaunchEpoch==e.launchEpoch,
96 "later estimates cannot move a committed launch");
97 armed.Begin(in,r,Solve(in,r),&pending);
98 auto noFit=e;noFit.valid=false;
99 Check(armed.Step(waited(1),r,&noFit).phase==Phase::Abort&&!armed.Active(),
100 "completed comparison with no solution cancels armed planning without launch");
101 armed.Begin(in,r,Solve(in,r),&pending);
102 auto moved=waited(1);moved.onGround=false;
103 Check(armed.Step(moved,r,&pending).phase==Phase::Abort,"manual departure cancels armed planning for explicit airborne pickup");
104 for(int invalid=1;invalid<5;++invalid) {
105 auto estimate=e;
106 if(invalid==1)estimate.checkedEpoch=in.now-31;
107 if(invalid==2)estimate.fuelEnough=false;
108 if(invalid==3)estimate.meetEpoch+=300;
109 if(invalid==4)estimate.launchEpoch=NAN;
110 const auto refused=armed.Begin(in,r,Solve(in,r),&estimate);
111 Check(refused.phase==Phase::Abort&&!armed.Active()&&!refused.thrust,"AUTO refuses stale, mismatched or fuel-short completed forecasts");
112 }
113 auto immediate=r;immediate.ascentWait=false;
114 Check(armed.Begin(in,immediate,Solve(in,immediate)).ascentLiftoff,"explicit NOW launches without a scheduled forecast");
115 auto soon=e;soon.launchEpoch=soon.epoch;
116 Check(armed.Begin(in,r,Solve(in,r),&soon).ascentLiftoff,"an immediate AUTO recommendation launches immediately");
117 armed.Begin(in,r,Solve(in,r),&e);
118 Check(armed.Step(waited(launchAt-in.now+2),r).phase==Phase::Abort,"missed launch deadline requires deliberate rearming");
119 auto pickup=in;pickup.onGround=false;pickup.freeFlight=true;
120 pickup.own.r=Unit(in.own.r)*(in.radius+10);pickup.own.v=Cross({0,0,in.spin},pickup.own.r)+Vec{0,.06,.18};
121 const auto pickedUp=armed.Begin(pickup,r,Solve(pickup,r));
122 Check(pickedUp.valid&&pickedUp.thrust&&pickedUp.ascentControl&&!pickedUp.ascentWaiting&&!pickedUp.ascentLiftoff,
123 "airborne AUTO engagement picks up measured climb immediately without a ground estimate");
124 auto selection=r;const auto rev=selection.revision;auto groundSnapshot=Solve(in,r);
125 Edit(selection,Row::AscentLaunch,1,groundSnapshot,in.now);
126 Check(!selection.ascentWait&&selection.revision>rev,"launch AUTO/NOW is an explicit plan edit");
127 BusState waitingDisplay;waitingDisplay.request=r;waitingDisplay.snapshot=Solve(in,r);waitingDisplay.launch=e;
128 waitingDisplay.guidance=armed.Begin(in,r,waitingDisplay.snapshot,&e);waitingDisplay.clock=in.now;
129 bool waitingLabel=false;
130 for(const auto& label:Readouts(waitingDisplay,Rows(r,Mode::Ascent),0,true))
131 if(label.text.find(L"LIFTOFF IN ")==0)waitingLabel=true;
132 Check(waitingLabel,"armed ground MFD shows the committed liftoff countdown");
133 waitingDisplay.launch=pending;
134 waitingDisplay.guidance=armed.Begin(in,r,waitingDisplay.snapshot,&pending);
135 bool provisionalLabel=false,planningLabel=false;
136 for(const auto& label:Readouts(waitingDisplay,Rows(r,Mode::Ascent),0,true)) {
137 provisionalLabel|=label.text==L"CANDIDATE / NOT COMMITTED";
138 planningLabel|=label.text==L"ARMED / COMPARING LAUNCHES";
139 Check(label.text.find(L"LIFTOFF IN ")!=0,"pending comparison has no committed countdown");
140 }
141 Check(provisionalLabel&&planningLabel,"armed planning distinguishes provisional numbers from a completed recommendation");
142 for(const auto row:Rows(r,Mode::Ascent)) {
143 const auto text=RowText(row,r,waitingDisplay.snapshot,in.now);
144 Check(text.find_first_not_of(L' ',text.find(L' '))==7,"ascent menu values align with LAUNCH");
145 }
146 waitingDisplay.launch=e;waitingDisplay.guidance=armed.Begin(in,r,waitingDisplay.snapshot,&e);
147 Preview(in,r,"launch-armed-wait",&waitingDisplay.guidance,&e);
148 double coarseTime=0,coarseFuel=0;
149 for(double dt:{.5,.25}) {
150 LaunchPlanning::Climb climb;climb.Begin(in,r,0);
151 int n=0;while(!climb.done&&n++<16000)climb.Step(dt);
152 std::wcout<<L"Launch timestep "<<dt<<L": "<<climb.status<<L" duration "<<climb.duration<<L" fuel "<<climb.fuel
153 <<L" h "<<Norm(climb.Current().own.r)-in.radius<<L"\n";
154 Check(climb.valid&&climb.duration>0&&climb.duration<1800&&climb.fuel>0&&climb.fuel<2000,
155 "launch model reaches insertion within the independent loaded E11 flight budget");
156 if(coarseTime>0)Check(std::abs(climb.duration-coarseTime)<coarseTime*.05&&std::abs(climb.fuel-coarseFuel)<coarseFuel*.05,
157 "halving the forecast timestep changes insertion time and fuel by less than five percent");
158 coarseTime=climb.duration;coarseFuel=climb.fuel;
159 const auto orbit=Describe(climb.insertion.own,in.gm);
160 Check(orbit.bound&&orbit.pe-in.radius>195&&orbit.ap-in.radius<205,"forecast actually achieves parking tolerance before quoting insertion");
161 }
162 Preview(in,r,"launch-meet-comparison",nullptr,&e);
163 auto highOrbit=r;highOrbit.ascentAltitudeKm=1000;highOrbit.arrivalEpoch=in.now+10800;
164 for(double dt:{.5,.25}) {
165 LaunchPlanning::Climb climb;climb.Begin(in,highOrbit,0);
166 int steps=0;while(!climb.done&&steps++<44000)climb.Step(dt);
167 std::cout<<"High parking forecast: "<<climb.duration<<"s fuel "<<climb.fuel<<"\n";
168 Check(climb.valid&&climb.duration>0&&climb.duration<6000,
169 "supported 1000 km ascent finishes at both forecast timesteps");
170 const auto orbit=Describe(climb.insertion.own,in.gm);
171 Check(orbit.bound&&orbit.pe-in.radius>995&&orbit.ap-in.radius<1005,
172 "high parking orbit meets insertion tolerance before it is reported ready");
173 }
174 const auto highEstimate=search(in,highOrbit);
175 Check(highEstimate.valid&&highEstimate.insertionEpoch>highEstimate.launchEpoch&&
176 highEstimate.insertionEpoch<highEstimate.meetEpoch,
177 "launch comparison plans the supported high parking orbit before MEET");
178 // Simulate a long background comparison without depending on host speed.
179 // Its forecast origin is old, but a future launch remains valid if the
180 // measured ground state still matches the propagated forecast inputs.
181 LaunchPlanning::Search slow;slow.Begin(in,highOrbit);slow.Work(8);
182 auto current=waited(45);slow.Update(current,highOrbit);
183 int remainingWork=0;while(slow.Pending()&&remainingWork++<5000)slow.Work(256);
184 auto checked=slow.Result();Ascent::LaunchGate currentGate;
185 Check(!checked.pending&&checked.valid&&checked.epoch==in.now&&checked.checkedEpoch==current.now&&
186 checked.launchEpoch>=current.now&&!currentGate.Arm(current,highOrbit,&checked),
187 "slow comparison preserves a current, armable future departure instead of expiring on completion");
188 current=waited(45.1);slow.Update(current,highOrbit);
189 Check(!slow.Pending()&&slow.Result().valid&&slow.Result().checkedEpoch==current.now,
190 "completed comparison stays available instead of restarting immediately because its origin is old");
191 auto expiredNow=checked;expiredNow.launchEpoch=expiredNow.epoch;
192 Check(currentGate.Arm(current,highOrbit,&expiredNow)!=nullptr,
193 "current input validation never revives an expired immediate departure");
194 current.target.r.x+=5;slow.Update(current,highOrbit);
195 Check(slow.Pending()&&!slow.Result().valid,"changed live station trajectory invalidates the checked forecast");
196 auto tooEarly=highOrbit;tooEarly.arrivalEpoch=in.now+3000;
197 LaunchPlanning::Climb late;late.Begin(in,tooEarly,0);
198 int lateSteps=0;while(!late.done&&lateSteps++<10000)late.Step();
199 Check(!late.valid&&std::wstring(late.status)==L"MEET BEFORE INSERTION"&&
200 late.Current().now<=tooEarly.arrivalEpoch-29.5,
201 "longer ascent forecast still stops at an impossible selected MEET");
202 BusState display;display.request=r;display.snapshot=Solve(in,r);display.launch=e;display.clock=in.now+3;
203 display.launch.launchEpoch=in.now;display.launch.epoch=in.now;
204 bool nowLabel=false;
205 for(const auto& label:Readouts(display,Rows(r,Mode::Ascent),0,false)) if(label.text==L"LAUNCH NOW")nowLabel=true;
206 Check(nowLabel,"immediate launch recommendation does not appear expired merely because computation took time");
207 auto early=r;early.arrivalEpoch=in.now+300;
208 Check(!search(in,early).valid,"impossible short MEET never advertises a launch solution");
209 auto limited=in;limited.fuelKg=e.ascentFuel+e.transferFuel*.5;
210 const auto shortfall=search(limited,r);
211 Check(shortfall.valid&&!shortfall.fuelEnough,"combined ascent and transfer fuel shortfall is exposed");
212 for(int kind=0;kind<8;++kind) {
213 auto bad=in;
214 if(kind==0)bad.entryVehicle.airAge=1;
215 if(kind==1)bad.entryUpKN=1;
216 if(kind==2)bad.dockedAtStation=true;
217 if(kind==3)bad.onGround=false;
218 if(kind==4)bad.targetKind=TargetKind::None;
219 if(kind==5)bad.ascentAero[10].cl=NAN;
220 if(kind==6)bad.thrustKN=0;
221 if(kind==7)bad.stationRate=0;
222 Check(!search(bad,r).valid,"invalid launch inputs never produce a fuel/timing promise");
223 }
224 auto rotated=in;
225 for(Vec* v:{&rotated.own.r,&rotated.own.v,&rotated.target.r,&rotated.target.v,&rotated.entryVehicle.nose,&rotated.bodyUp})
226 *v=Rotate(*v,{0,0,1},.7);
227 const auto rotation=search(rotated,r);
228 std::cout<<"Launch rotation: "<<e.launchEpoch<<" / "<<rotation.launchEpoch<<" fuel "<<e.ascentFuel+e.transferFuel
229 <<" / "<<rotation.ascentFuel+rotation.transferFuel<<" heading "<<e.heading<<" / "<<rotation.heading<<"\n";
230 Check(rotation.valid&&std::abs(rotation.launchEpoch-e.launchEpoch)<.01&&
231 std::abs(rotation.ascentFuel+rotation.transferFuel-e.ascentFuel-e.transferFuel)<.1&&std::abs(rotation.heading-e.heading)<1e-6,
232 "planet-frame rotation does not change the predicted launch or local true heading");
233 LaunchPlanning::Search bounded;bounded.Begin(in,r);bounded.Work(8);
234 Check(bounded.Pending()&&!bounded.Result().valid,"partial launch search remains explicitly pending");
235 auto other=in;other.targetId=42;other.entryUpKN=1;bounded.Update(other,r);
236 Check(!bounded.Pending()&&!bounded.Result().valid,"target/performance change clears stale recommendations");
237 Snapshot airborne;airborne.targetKind=TargetKind::Station;
238 const auto meet=r.arrivalEpoch;Edit(r,Row::Mode,1,airborne,in.now+2700);
239 Check(!r.ascent&&Objective(r,TargetKind::Station)==Mode::Rendezvous&&r.arrivalEpoch==meet,
240 "parking-to-rendezvous replan preserves the selected absolute MEET");
241 std::cout<<"Launch comparison: delay "<<e.launchEpoch-in.now<<" insertion "<<e.insertionEpoch-in.now
242 <<" fuel "<<e.ascentFuel<<" + "<<e.transferFuel<<" earlier "<<e.earlierFuel<<" later "<<e.laterFuel<<"\n";
243}
bool Unit(Vector3 &v)
ArrivalPreview Preview(const Input &in, const Request &r)
Plan Solve(const Input &in, const Request &r, bool geometry)
Definition OrbitEntry.h:332
State TargetAt(const Input &in, double seconds)
Definition OrbitModel.h:82
std::wstring RowText(Row row, const Request &r, const Snapshot &s, double now)
Mode Objective(const Request &r, TargetKind target)
Definition OrbitState.h:368
Vec Rotate(Vec v, Vec axis, double angle)
Definition OrbitMath.h:22
std::vector< Row > Rows(const Request &r, Mode mode, bool entry=false, bool ascent=false, bool recordingAllowed=false)
Definition OrbitState.h:414
Elements Describe(State s, double gm)
Definition OrbitMath.h:43
std::vector< Label > Readouts(const BusState &data, const std::vector< Row > &rows, int uiRow, bool dorActive, bool showKeys=true)
double Norm(Vec a)
Definition OrbitMath.h:18
void Edit(Request &r, Row row, int direction, const Snapshot &s, double now, bool circularize=false, bool recordingAllowed=false)
Definition OrbitState.h:438
Vec Cross(Vec a, Vec b)
Definition OrbitMath.h:17
bool ManualAttitudeDisconnect(const Guidance &g)
Definition OrbitState.h:355