47 coast=(std::max)(coast,align);
49 Plan p;p.planningOnly=
true;p.performance=
true;p.start=coast;p.end=r.
arrivalEpoch-in.
now;
50 p.surface.route=route;p.entry.active=
true;p.entry.coastSeconds=coast;
52 p.surface.reason=L
"NO ENTRY PROFILE";
54 if(r.
final!=
Final::Hold&&!p.arrival.valid) {p.surface.reason=p.arrival.status;
return p;}
55 const double duration=p.end-coast;
56 if(duration<30)
return p;
59 const Vec endpoint=p.arrival.valid?p.arrival.gate:in.
target.
r;
60 const Vec destination=
Unit(
Rotate(endpoint,{0,0,1},in.
spin*p.end));
61 Vec normal=p.arrival.valid&&
Norm(p.arrival.normal)>.5?
Rotate(p.arrival.normal,{0,0,1},in.
spin*p.end):
Unit(
Cross(departure.r,destination));
63 if(
Norm(normal)<.5)
return p;
64 auto glideInput=in;glideInput.
target.
r=endpoint;
67 p.entry.glideKm=runway?descent.range-p.arrival.distanceKm:
GlideRange(glideInput,r);
68 for(
auto& point:p.arrival.path) {point.r=
Rotate(point.r,{0,0,1},in.
spin*p.end);point.seconds=p.end;}
69 if(!(p.entry.glideKm>0)||p.entry.glideKm>in.
radius*
Pi*.5) {
70 p.surface.reason=L
"GLIDE DATA UNAVAILABLE";
return p;
73 const Vec up=
Rotate(destination,normal,-p.entry.glideKm/in.
radius);
74 const Vec tangent=
Unit(
Cross(normal,up)),position=up*radius;
75 const double rate=in.
spin;
76 if(runway) p.entry.handoffFpa=-std::atan(descent.Slope(descent.range));
77 const Vec direction=tangent*std::cos(p.entry.handoffFpa)+up*std::sin(p.entry.handoffFpa);
79 const double tail=
Dot(wind,direction),cross2=(std::max)(0.0,
Dot(wind,wind)-tail*tail);
82 if(groundSpeed<=.010) {p.surface.reason=L
"HANDOFF HEADWIND";
return p;}
85 const Vec ground=direction*groundSpeed;
86 const Vec velocity=
Cross({0,0,rate},position)+ground;
87 const double horizontal=
Dot(ground,tangent),vertical=
Dot(ground,up);
90 const Vec acceleration=
Cross({0,0,rate},
Cross({0,0,rate},position))+
91 Cross({0,0,rate},ground)*2-up*(horizontal*horizontal/radius)+tangent*(horizontal*vertical/radius);
92 const Kinematic a{departure,
Gravity(departure.r,in.
gm)},b{{position,velocity},acceleration};
94 p.entry.departure=departure;p.entry.departureAcceleration=a.acceleration;p.entry.handoffAcceleration=acceleration;
95 bool transferClear=
true;
96 if(shape.end>shape.start) {
97 const double first=duration*shape.start,last=duration*shape.end;
98 if(first<30||duration-last<30||last-first<10)
return p;
101 c.
r=
Unit(c.r)*(
Norm(c.r)+shape.heightKm);
102 d.r=
Unit(d.r)*(
Norm(d.r)+shape.heightKm);
103 if(!
Lambert(c.r,d.r,last-first,in.
gm,
false,c.v,d.v))
return p;
104 p.entry.transferCoastStart=coast+first;p.entry.transferCoastEnd=coast+last;
105 p.entry.transferCoastDeparture=c;p.entry.transferCoastArrival=d;
109 bool clear=transferClear,speedFits=
true,axisFits=
true,loadFits=
true,skipFits=
true,entered=
false;
110 p.entry.loadChecked=p.entry.skipChecked=
true;
113 auto retain=[&](
const EntrySample& s,
bool isCoast) {
114 if(!geometry)
return;
115 p.entry.samples.push_back(s);
116 (isCoast?p.coast:p.path).push_back({s.state.r,s.seconds});
119 for(
int j=0;j<=32;++j) {
120 const double t=coast*j/32;
State s;
124 minimum=(std::min)(minimum,alt);
125 const double spent=in.
idleFlowKgS*coast/32*(j==0||j==32?.5:1);
126 p.fuel+=spent;mass-=spent;
129 p.entry.peakAftC=(std::max)(p.entry.peakAftC,aft);
130 p.entry.peakIasFraction=(std::max)(p.entry.peakIasFraction,in.
vneKms>0?air.ias/in.
vneKms:0);
132 p.surface.peakQ=(std::max)(p.surface.peakQ,air.q);
133 if(coast>0) retain({t,mass,p.fuel,air.q,
Norm(air.velocity),0,s,
Unit(air.velocity),{},
Gravity(s.r,in.
gm),aft,air.ias},
true);
135 if(geometry) p.handoffs.push_back({departure.r,coast});
136 const int count=(std::max)(160,
int(std::ceil(duration/4)));
137 std::vector<double> times;
138 for(
int j=0;j<=count;++j) times.push_back(duration*j/count);
142 for(
double join:{p.entry.transferCoastStart-coast,p.entry.transferCoastEnd-coast})
144 times.push_back((std::clamp)(join+offset,0.0,duration));
145 std::sort(times.begin(),times.end());
146 times.erase(std::unique(times.begin(),times.end()),times.end());
148 double previousDemand=0;Vec previousNose;
149 p.entry.entrySeconds=p.end;
150 for(
size_t j=0;j<times.size();++j) {
151 const double t=times[j],dt=j?t-times[j-1]:0;
152 const double weight=((j+1<times.size()?times[j+1]:t)-(j?times[j-1]:t))*.5;
154 const double altitude=
Norm(k.state.r)-in.
radius;
157 minimum=(std::min)(minimum,altitude);
158 p.surface.altitudeKm=(std::max)(p.surface.altitudeKm,altitude);
160 const double speed=
Norm(air.velocity),q=air.q;
162 const double best=demand.peak,bestCost=demand.cost;
163 const double bestReverse=(std::max)(0.0,-demand.main),bestNormal=(std::max)(std::abs(demand.normal),std::abs(demand.lateral));
164 const double bestMain=demand.main;
170 axisFits=axisFits&&best<=.02;
172 const Vec nose=demand.nose,thrust=demand.force;
174 const Vec required=(k.acceleration-
Gravity(k.state.r,in.
gm))*mass;
176 p.entry.peakLoadFraction=(std::max)(p.entry.peakLoadFraction,load);
180 if(j>0) p.entry.peakSlew=(std::max)(p.entry.peakSlew,
Angle(previousNose,nose)/dt);
185 previousDemand=bestMain;
186 p.surface.peakThrust=(std::max)(p.surface.peakThrust,best);
187 p.entry.peakReverse=(std::max)(p.entry.peakReverse,bestReverse);
188 p.entry.peakNormal=(std::max)(p.entry.peakNormal,bestNormal);
189 p.surface.peakQ=(std::max)(p.surface.peakQ,q);
190 p.entry.peakLoadG=(std::max)(p.entry.peakLoadG,
Norm(required)/(mass*.00980665));
191 p.entry.peakIasFraction=(std::max)(p.entry.peakIasFraction,in.
vneKms>0?air.ias/in.
vneKms:0);
194 p.entry.peakAftC=(std::max)(p.entry.peakAftC,aft);
196 p.entry.entrySeconds=coast+t;
197 if(geometry) p.handoffs.push_back({k.state.r,coast+t});
199 p.deltaV+=
Norm(thrust)/mass*weight;
200 p.entry.dragDeltaV+=bestDrag/mass*weight;
204 p.fuel+=spent;mass=(std::max)(in.
massKg*.05,mass-spent);
205 retain({coast+t,mass,p.fuel,q,speed,best,k.state,nose,thrust,k.acceleration,aft,air.ias},
false);
207 p.entry.atmosphericSeconds=p.end-p.entry.entrySeconds;
208 if(geometry) p.handoffs.push_back({position,p.end});
209 p.clearance={
true,minimum,0};p.surface.available=clear;
210 p.fuelEnough=p.surface.fuelEnough=p.fuel<=in.
fuelKg;
211 p.surface.fuel=p.fuel;p.surface.deltaV=p.deltaV;
214 const bool slewFits=p.entry.peakSlew<=2*
Pi/180;
216 auto handoffInput=in;handoffInput.
massKg=mass;handoffInput.entryVehicle.aftC=aft;
219 p.valid=p.surface.fits=clear&&axisFits&&loadFits&&skipFits&&aircraftFits&&speedFits&&heatFits&&slewFits&&p.surface.peakThrust<=reserve&&p.fuelEnough;
221 p.surface.reason=!clear?L
"ENTRY GEOMETRY":!loadFits?L
"LOAD RESERVE":!skipFits?L
"SKIP / OVERSHOOT":!speedFits?L
"IAS RESERVE":!heatFits?L
"AFT RESERVE":
222 !slewFits?L
"ATTITUDE RATE":p.surface.peakThrust>reserve?L
"THRUST RESERVE":!axisFits?L
"SPOOL LIMIT":
223 !p.fuelEnough?L
"FUEL LIMIT":!aircraftFits?L
"A/F HANDOFF ENVELOPE":p.planningOnly?p.executionBlock:L
"ENTRY READY";