]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/Airplane.cpp
3d76fff548560e8259319cf1efaa38c6aa1d18fa
[flightgear.git] / src / FDM / YASim / Airplane.cpp
1 #include "Atmosphere.hpp"
2 #include "ControlMap.hpp"
3 #include "Gear.hpp"
4 #include "Math.hpp"
5 #include "Glue.hpp"
6 #include "RigidBody.hpp"
7 #include "Surface.hpp"
8 #include "Thruster.hpp"
9
10 #include "Airplane.hpp"
11 namespace yasim {
12
13 Airplane::Airplane()
14 {
15     _emptyWeight = 0;
16     _pilotPos[0] = _pilotPos[1] = _pilotPos[2] = 0;
17     _wing = 0;
18     _tail = 0;
19     _ballast = 0;
20     _cruiseP = 0;
21     _cruiseT = 0;
22     _cruiseSpeed = 0;
23     _cruiseWeight = 0;
24     _approachP = 0;
25     _approachT = 0;
26     _approachSpeed = 0;
27     _approachAoA = 0;
28     _approachWeight = 0;
29
30     _dragFactor = 1;
31     _liftRatio = 1;
32     _cruiseAoA = 0;
33     _tailIncidence = 0;
34 }
35
36 Airplane::~Airplane()
37 {
38     for(int i=0; i<_fuselages.size(); i++)
39         delete (Fuselage*)_fuselages.get(i);
40     for(int i=0; i<_tanks.size(); i++)
41         delete (Tank*)_tanks.get(i);
42     for(int i=0; i<_thrusters.size(); i++)
43         delete (ThrustRec*)_thrusters.get(i);
44     for(int i=0; i<_gears.size(); i++)
45         delete (GearRec*)_gears.get(i);
46     for(int i=0; i<_surfs.size(); i++)
47         delete (Surface*)_surfs.get(i);    
48 }
49
50 void Airplane::iterate(float dt)
51 {
52     _model.iterate();
53
54     // FIXME: Consume fuel
55 }
56
57 ControlMap* Airplane::getControlMap()
58 {
59     return &_controls;
60 }
61
62 Model* Airplane::getModel()
63 {
64     return &_model;
65 }
66
67 void Airplane::getPilotAccel(float* out)
68 {
69     State* s = _model.getState();
70
71     // Gravity
72     Glue::geodUp(s->pos, out);
73     Math::mul3(-9.8, out, out);
74
75     // The regular acceleration
76     float tmp[3];
77     Math::mul3(-1, s->acc, tmp);
78     Math::add3(tmp, out, out);
79
80     // Convert to aircraft coordinates
81     Math::vmul33(s->orient, out, out);
82
83     // FIXME: rotational & centripetal acceleration needed
84 }
85
86 void Airplane::setPilotPos(float* pos)
87 {
88     for(int i=0; i<3; i++) _pilotPos[i] = pos[i];
89 }
90
91 void Airplane::getPilotPos(float* out)
92 {
93     for(int i=0; i<3; i++) out[i] = _pilotPos[i];
94 }
95
96 int Airplane::numGear()
97 {
98     return _gears.size();
99 }
100
101 Gear* Airplane::getGear(int g)
102 {
103     return ((GearRec*)_gears.get(g))->gear;
104 }
105
106 void Airplane::setGearState(bool down, float dt)
107 {
108     for(int i=0; i<_gears.size(); i++) {
109         GearRec* gr = (GearRec*)_gears.get(i);
110         if(gr->time == 0) {
111             // Non-extensible
112             gr->gear->setExtension(1);
113             gr->surf->setXDrag(1);
114             gr->surf->setYDrag(1);
115             gr->surf->setZDrag(1);
116             continue;
117         }
118
119         float diff = dt / gr->time;
120         if(!down) diff = -diff;
121         float ext = gr->gear->getExtension() + diff;
122         if(ext < 0) ext = 0;
123         if(ext > 1) ext = 1;
124
125         gr->gear->setExtension(ext);
126         gr->surf->setXDrag(ext);
127         gr->surf->setYDrag(ext);
128         gr->surf->setZDrag(ext);
129     }
130 }
131
132 void Airplane::setApproach(float speed, float altitude)
133 {
134     // The zero AoA will become a calculated stall AoA in compile()
135     setApproach(speed, altitude, 0);
136 }
137
138 void Airplane::setApproach(float speed, float altitude, float aoa)
139 {
140     _approachSpeed = speed;
141     _approachP = Atmosphere::getStdPressure(altitude);
142     _approachT = Atmosphere::getStdTemperature(altitude);
143     _approachAoA = aoa;
144 }
145  
146 void Airplane::setCruise(float speed, float altitude)
147 {
148     _cruiseSpeed = speed;
149     _cruiseP = Atmosphere::getStdPressure(altitude);
150     _cruiseT = Atmosphere::getStdTemperature(altitude);
151     _cruiseAoA = 0;
152     _tailIncidence = 0;
153 }
154
155 void Airplane::addApproachControl(int control, float val)
156 {
157     Control* c = new Control();
158     c->control = control;
159     c->val = val;
160     _approachControls.add(c);
161 }
162
163 void Airplane::addCruiseControl(int control, float val)
164 {
165     Control* c = new Control();
166     c->control = control;
167     c->val = val;
168     _cruiseControls.add(c);
169 }
170
171 int Airplane::numTanks()
172 {
173     return _tanks.size();
174 }
175
176 float Airplane::getFuel(int tank)
177 {
178     return ((Tank*)_tanks.get(tank))->fill;
179 }
180
181 float Airplane::getFuelDensity(int tank)
182 {
183     return ((Tank*)_tanks.get(tank))->density;
184 }
185
186 void Airplane::setWeight(float weight)
187 {
188     _emptyWeight = weight;
189 }
190
191 void Airplane::setWing(Wing* wing)
192 {
193     _wing = wing;
194 }
195
196 void Airplane::setTail(Wing* tail)
197 {
198     _tail = tail;
199 }
200
201 void Airplane::addVStab(Wing* vstab)
202 {
203     _vstabs.add(vstab);
204 }
205
206 void Airplane::addFuselage(float* front, float* back, float width)
207 {
208     Fuselage* f = new Fuselage();
209     for(int i=0; i<3; i++) {
210         f->front[i] = front[i];
211         f->back[i]  = back[i];
212     }
213     f->width = width;
214     _fuselages.add(f);
215 }
216
217 int Airplane::addTank(float* pos, float cap, float density)
218 {
219     Tank* t = new Tank();
220     for(int i=0; i<3; i++) t->pos[i] = pos[i];
221     t->cap = cap;
222     t->fill = cap;
223     t->density = density;
224     t->handle = 0xffffffff;
225     return _tanks.add(t);
226 }
227
228 void Airplane::addGear(Gear* gear, float transitionTime)
229 {
230     GearRec* g = new GearRec();
231     g->gear = gear;
232     g->surf = 0;
233     g->time = transitionTime;
234     _gears.add(g);
235 }
236
237 void Airplane::addThruster(Thruster* thruster, float mass, float* cg)
238 {
239     ThrustRec* t = new ThrustRec();
240     t->thruster = thruster;
241     t->mass = mass;
242     for(int i=0; i<3; i++) t->cg[i] = cg[i];
243     _thrusters.add(t);
244 }
245
246 void Airplane::addBallast(float* pos, float mass)
247 {
248     _model.getBody()->addMass(mass, pos);
249     _ballast += mass;
250 }
251
252 int Airplane::addWeight(float* pos, float size)
253 {
254     WeightRec* wr = new WeightRec();
255     wr->handle = _model.getBody()->addMass(0, pos);
256
257     wr->surf = new Surface();
258     wr->surf->setPosition(pos);
259     wr->surf->setTotalDrag(size*size);
260     _model.addSurface(wr->surf);
261     _surfs.add(wr->surf);
262
263     return _weights.add(wr);
264 }
265
266 void Airplane::setWeight(int handle, float mass)
267 {
268     WeightRec* wr = (WeightRec*)_weights.get(handle);
269
270     _model.getBody()->setMass(wr->handle, mass);
271
272     // Kill the aerodynamic drag if the mass is exactly zero.  This is
273     // how we simulate droppable stores.
274     if(mass == 0) {
275         wr->surf->setXDrag(0);
276         wr->surf->setYDrag(0);
277         wr->surf->setZDrag(0);
278     } else {
279         wr->surf->setXDrag(1);
280         wr->surf->setYDrag(1);
281         wr->surf->setZDrag(1);
282     }
283 }
284
285 void Airplane::setFuelFraction(float frac)
286 {
287     for(int i=0; i<_tanks.size(); i++) {
288         Tank* t = (Tank*)_tanks.get(i);
289         _model.getBody()->setMass(t->handle, t->cap * frac);
290     }
291 }
292
293 float Airplane::getDragCoefficient()
294 {
295     return _dragFactor;
296 }
297
298 float Airplane::getLiftRatio()
299 {
300     return _liftRatio;
301 }
302
303 float Airplane::getCruiseAoA()
304 {
305     return _cruiseAoA;
306 }
307
308 float Airplane::getTailIncidence()
309 {
310     return _tailIncidence;
311 }
312
313 char* Airplane::getFailureMsg()
314 {
315     return _failureMsg;
316 }
317
318 int Airplane::getSolutionIterations()
319 {
320     return _solutionIterations;
321 }
322
323 void Airplane::setupState(float aoa, float speed, State* s)
324 {
325     float cosAoA = Math::cos(aoa);
326     float sinAoA = Math::sin(aoa);
327     s->orient[0] =  cosAoA; s->orient[1] = 0; s->orient[2] = sinAoA;
328     s->orient[3] =       0; s->orient[4] = 1; s->orient[5] =      0;
329     s->orient[6] = -sinAoA; s->orient[7] = 0; s->orient[8] = cosAoA;
330
331     s->v[0] = speed; s->v[1] = 0; s->v[2] = 0;
332
333     for(int i=0; i<3; i++)
334         s->pos[i] = s->rot[i] = s->acc[i] = s->racc[i] = 0;
335
336     // Put us 1m above the origin, or else the gravity computation in
337     // Model goes nuts
338     s->pos[2] = 1;
339 }
340
341 float Airplane::compileWing(Wing* w)
342 {
343     // Make sure it's initialized.  The surfaces will pop out with
344     // total drag coefficients equal to their areas, which is what we
345     // want.
346     w->compile();
347
348     float wgt = 0;
349     for(int i=0; i<w->numSurfaces(); i++) {
350         Surface* s = (Surface*)w->getSurface(i);
351         _model.addSurface(s);
352
353         float pos[3];
354         s->getPosition(pos);
355         _model.getBody()->addMass(w->getSurfaceWeight(i), pos);
356         wgt += w->getSurfaceWeight(i);
357     }
358     return wgt;
359 }
360
361 float Airplane::compileFuselage(Fuselage* f)
362 {
363     float wgt = 0;
364     float fwd[3];
365     Math::sub3(f->front, f->back, fwd);
366     float len = Math::mag3(fwd);
367     float wid = f->width;
368     int segs = (int)Math::ceil(len/wid);
369     float segWgt = len*wid/segs;
370     for(int j=0; j<segs; j++) {
371         float frac = (j+0.5) / segs;
372         float pos[3];
373         Math::mul3(frac, fwd, pos);
374         Math::add3(f->back, pos, pos);
375         _model.getBody()->addMass(segWgt, pos);
376         wgt += segWgt;
377
378         // Make a Surface too
379         Surface* s = new Surface();
380         s->setPosition(pos);
381         float sideDrag = len/wid;
382         s->setYDrag(sideDrag);
383         s->setZDrag(sideDrag);
384         s->setTotalDrag(segWgt);
385
386         // FIXME: fails for fuselages aligned along the Y axis
387         float o[9];
388         float *x=o, *y=o+3, *z=o+6; // nicknames for the axes
389         Math::unit3(fwd, x);
390         y[0] = 0; y[1] = 1; y[2] = 0;
391         Math::cross3(x, y, z);
392         s->setOrientation(o);
393
394         _model.addSurface(s);
395         _surfs.add(s);
396     }
397     return wgt;
398 }
399
400 // FIXME: should probably add a mass for the gear, too
401 void Airplane::compileGear(GearRec* gr)
402 {
403     Gear* g = gr->gear;
404
405     // Make a Surface object for the aerodynamic behavior
406     Surface* s = new Surface();
407     gr->surf = s;
408
409     // Put the surface at the half-way point on the gear strut, give
410     // it a drag coefficient equal to a square of the same dimension
411     // (gear are really draggy) and make it symmetric.  Assume that
412     // the "length" of the gear is 3x the compression distance
413     float pos[3], cmp[3];
414     g->getCompression(cmp);
415     float length = 3 * Math::mag3(cmp);
416     g->getPosition(pos);
417     Math::mul3(0.5, cmp, cmp);
418     Math::add3(pos, cmp, pos);
419
420     s->setPosition(pos);
421     s->setTotalDrag(length*length);
422
423     _model.addGear(g);
424     _model.addSurface(s);
425     _surfs.add(s);
426 }
427
428 void Airplane::compile()
429 {
430     double ground[3];
431     ground[0] = 0; ground[1] = 0; ground[2] = 1;
432     _model.setGroundPlane(ground, -100000);
433
434     RigidBody* body = _model.getBody();
435     int firstMass = body->numMasses();
436
437     // Generate the point masses for the plane.  Just use unitless
438     // numbers for a first pass, then go back through and rescale to
439     // make the weight right.
440     float aeroWgt = 0;
441
442     // The Wing objects
443     aeroWgt += compileWing(_wing);
444     aeroWgt += compileWing(_tail);
445     for(int i=0; i<_vstabs.size(); i++) {
446         aeroWgt += compileWing((Wing*)_vstabs.get(i)); 
447     }
448     
449     // The fuselage(s)
450     for(int i=0; i<_fuselages.size(); i++) {
451         aeroWgt += compileFuselage((Fuselage*)_fuselages.get(i));
452     }
453
454     // Count up the absolute weight we have
455     float nonAeroWgt = _ballast;
456     for(int i=0; i<_thrusters.size(); i++)
457         nonAeroWgt += ((ThrustRec*)_thrusters.get(i))->mass;
458
459     // Rescale to the specified empty weight
460     float wscale = (_emptyWeight-nonAeroWgt)/aeroWgt;
461     for(int i=firstMass; i<body->numMasses(); i++)
462         body->setMass(i, body->getMass(i)*wscale);
463
464     // Add the thruster masses
465     for(int i=0; i<_thrusters.size(); i++) {
466         ThrustRec* t = (ThrustRec*)_thrusters.get(i);
467         body->addMass(t->mass, t->cg);
468     }
469
470     // Add the tanks, empty for now.
471     float totalFuel = 0;
472     for(int i=0; i<_tanks.size(); i++) { 
473         Tank* t = (Tank*)_tanks.get(i); 
474         t->handle = body->addMass(0, t->pos);
475         totalFuel += t->cap;
476     }
477     _cruiseWeight = _emptyWeight + totalFuel*0.5;
478     _approachWeight = _emptyWeight + totalFuel*0.2;
479
480     body->recalc();
481
482     // Add surfaces for the landing gear.
483     for(int i=0; i<_gears.size(); i++)
484         compileGear((GearRec*)_gears.get(i));
485
486     // The Thruster objects
487     for(int i=0; i<_thrusters.size(); i++) {
488         ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
489         tr->handle = _model.addThruster(tr->thruster);
490     }
491
492     // Ground effect
493     float gepos[3];
494     float gespan = _wing->getGroundEffect(gepos);
495     _model.setGroundEffect(gepos, gespan, .3);
496
497     solveGear();
498     solve();
499
500     // Drop the gear (use a really big dt)
501     setGearState(true, 1000000);
502 }
503
504 void Airplane::solveGear()
505 {
506     float cg[3], pos[3];
507     _model.getBody()->getCG(cg);
508
509     // Calculate spring constant weightings for the gear.  Weight by
510     // the inverse of the distance to the c.g. in the XY plane, which
511     // should be correct for most gear arrangements.  Add 50cm of
512     // "buffer" to keep things from blowing up with aircraft with a
513     // single gear very near the c.g. (AV-8, for example).
514     float total = 0;
515     for(int i=0; i<_gears.size(); i++) {
516         GearRec* gr = (GearRec*)_gears.get(i);
517         Gear* g = gr->gear;
518         g->getPosition(pos);
519         Math::sub3(cg, pos, pos);
520         gr->wgt = 1/(0.5+Math::sqrt(pos[0]*pos[0] + pos[1]*pos[1]));
521         total += gr->wgt;
522     }
523
524     // Renormalize so they sum to 1
525     for(int i=0; i<_gears.size(); i++)
526         ((GearRec*)_gears.get(i))->wgt /= total;
527     
528     // The force at max compression should be sufficient to stop a
529     // plane moving downwards at 3x the approach descent rate.  Assume
530     // a 3 degree approach.
531     float descentRate = 3*_approachSpeed/19.1;
532
533     // Spread the kinetic energy according to the gear weights.  This
534     // will results in an equal compression fraction (not distance) of
535     // each gear.
536     float energy = 0.5*_approachWeight*descentRate*descentRate;
537
538     for(int i=0; i<_gears.size(); i++) {
539         GearRec* gr = (GearRec*)_gears.get(i);
540         float e = energy * gr->wgt;
541         float comp[3];
542         gr->gear->getCompression(comp);
543         float len = Math::mag3(comp);
544
545         // Energy in a spring: e = 0.5 * k * len^2
546         float k = 2 * e / (len*len);
547
548         gr->gear->setSpring(k);
549
550         // Critically damped (too damped, too!)
551         gr->gear->setDamping(2*Math::sqrt(k*_approachWeight*gr->wgt));
552
553         // These are pretty generic
554         gr->gear->setStaticFriction(0.8);
555         gr->gear->setDynamicFriction(0.7);
556     }
557 }
558
559 void Airplane::stabilizeThrust()
560 {
561     for(int i=0; i<_thrusters.size(); i++)
562         _model.getThruster(i)->stabilize();
563 }
564
565 void Airplane::runCruise()
566 {
567     setupState(_cruiseAoA, _cruiseSpeed, &_cruiseState);
568     _model.setState(&_cruiseState);
569     _model.setAir(_cruiseP, _cruiseT);
570
571     // The control configuration
572     _controls.reset();
573     for(int i=0; i<_cruiseControls.size(); i++) {
574         Control* c = (Control*)_cruiseControls.get(i);
575         _controls.setInput(c->control, c->val);
576     }
577     _controls.applyControls();
578
579     // The local wind
580     float wind[3];
581     Math::mul3(-1, _cruiseState.v, wind);
582     Math::vmul33(_cruiseState.orient, wind, wind);
583  
584     // Gear are up (if they're non-retractable, this is a noop)
585     setGearState(false, 100000);
586     
587     // Cruise is by convention at 50% tank capacity
588     setFuelFraction(0.5);
589    
590     // Set up the thruster parameters and iterate until the thrust
591     // stabilizes.
592     for(int i=0; i<_thrusters.size(); i++) {
593         Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
594         t->setWind(wind);
595         t->setAir(_cruiseP, _cruiseT);
596     }
597     stabilizeThrust();
598
599     // Precompute thrust in the model, and calculate aerodynamic forces
600     _model.getBody()->reset();
601     _model.initIteration();
602     _model.calcForces(&_cruiseState);
603 }
604
605 void Airplane::runApproach()
606 {
607     setupState(_approachAoA, _approachSpeed, &_approachState);
608     _model.setState(&_approachState);
609     _model.setAir(_approachP, _approachT);
610
611     // The control configuration
612     _controls.reset();
613     for(int i=0; i<_approachControls.size(); i++) {
614         Control* c = (Control*)_approachControls.get(i);
615         _controls.setInput(c->control, c->val);
616     }
617     _controls.applyControls();
618
619     // The local wind
620     float wind[3];
621     Math::mul3(-1, _approachState.v, wind);
622     Math::vmul33(_approachState.orient, wind, wind);
623     
624     // Approach is by convention at 20% tank capacity
625     setFuelFraction(0.2);
626
627     // Gear are down
628     setGearState(true, 100000);
629
630     // Run the thrusters until they get to a stable setting.  FIXME:
631     // this is lots of wasted work.
632     for(int i=0; i<_thrusters.size(); i++) {
633         Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
634         t->setWind(wind);
635         t->setAir(_approachP, _approachT);
636     }
637     stabilizeThrust();
638
639     // Precompute thrust in the model, and calculate aerodynamic forces
640     _model.getBody()->reset();
641     _model.initIteration();
642     _model.calcForces(&_approachState);
643 }
644
645 void Airplane::applyDragFactor(float factor)
646 {
647     float applied = Math::sqrt(factor);
648     _dragFactor *= applied;
649     _wing->setDragScale(_wing->getDragScale() * applied);
650     _tail->setDragScale(_tail->getDragScale() * applied);
651     for(int i=0; i<_vstabs.size(); i++) {
652         Wing* w = (Wing*)_vstabs.get(i);
653         w->setDragScale(w->getDragScale() * applied);
654     }
655     for(int i=0; i<_surfs.size(); i++) {
656         Surface* s = (Surface*)_surfs.get(i);
657         s->setTotalDrag(s->getTotalDrag() * applied);
658     }
659 }
660
661 void Airplane::applyLiftRatio(float factor)
662 {
663     float applied = Math::sqrt(factor);
664     _liftRatio *= applied;
665     _wing->setLiftRatio(_wing->getLiftRatio() * applied);
666     _tail->setLiftRatio(_tail->getLiftRatio() * applied);
667     for(int i=0; i<_vstabs.size(); i++) {
668         Wing* w = (Wing*)_vstabs.get(i);
669         w->setLiftRatio(w->getLiftRatio() * applied);
670     }
671 }
672
673 float Airplane::clamp(float val, float min, float max)
674 {
675     if(val < min) return min;
676     if(val > max) return max;
677     return val;
678 }
679
680 float Airplane::normFactor(float f)
681 {
682     if(f < 0) f = -f;
683     if(f < 1) f = 1/f;
684     return f;
685 }
686
687 void Airplane::solve()
688 {
689     static const float ARCMIN = 0.0002909;
690
691     float tmp[3];
692     _solutionIterations = 0;
693     _failureMsg = 0;
694     while(1) {
695         if(_solutionIterations++ > 10000) {
696             _failureMsg = "Solution failed to converge after 10000 iterations";
697             return;
698         }
699
700         // Run an iteration at cruise, and extract the needed numbers:
701         runCruise();
702
703         _model.getThrust(tmp);
704         float thrust = tmp[0];
705
706         _model.getBody()->getAccel(tmp);
707         float xforce = _cruiseWeight * tmp[0];
708         float clift0 = _cruiseWeight * tmp[2];
709
710         _model.getBody()->getAngularAccel(tmp);
711         float pitch0 = tmp[1];
712
713         // Run an approach iteration, and do likewise
714         runApproach();
715
716         _model.getBody()->getAccel(tmp);
717         float alift = _approachWeight * tmp[2];
718
719         // Modify the cruise AoA a bit to get a derivative
720         _cruiseAoA += ARCMIN;
721         runCruise();
722         _cruiseAoA -= ARCMIN;
723
724         _model.getBody()->getAccel(tmp);
725         float clift1 = _cruiseWeight * tmp[2];
726
727         // Do the same with the tail incidence
728         _tail->setIncidence(_tailIncidence + ARCMIN);
729         runCruise();
730         _tail->setIncidence(_tailIncidence);
731
732         _model.getBody()->getAngularAccel(tmp);
733         float pitch1 = tmp[1];
734
735         // Now calculate:
736         float awgt = 9.8 * _approachWeight;
737
738         float dragFactor = thrust / (thrust-xforce);
739         float liftFactor = awgt / (awgt+alift);
740         float aoaDelta = -clift0 * (ARCMIN/(clift1-clift0));
741         float tailDelta = -pitch0 * (ARCMIN/(pitch1-pitch0));
742
743         // Sanity:
744         if(dragFactor <= 0) {
745             _failureMsg = "Zero or negative drag adjustment.";
746             return;
747         } else if(liftFactor <= 0) {
748             _failureMsg = "Zero or negative lift adjustment.";
749             return;
750         }
751
752         // And apply:
753         applyDragFactor(dragFactor);
754         applyLiftRatio(liftFactor);
755
756         // DON'T do the following until the above are sane
757         if(normFactor(dragFactor) > 1.1
758            || normFactor(liftFactor) > 1.1)
759         {
760             continue;
761         }
762
763         // OK, now we can adjust the minor variables
764         _cruiseAoA += 0.5*aoaDelta;
765         _tailIncidence += 0.5*tailDelta;
766         
767         _cruiseAoA = clamp(_cruiseAoA, -.174, .174);
768         _tailIncidence = clamp(_tailIncidence, -.174, .174);
769
770         if(dragFactor < 1.00001 && liftFactor < 1.00001 &&
771            aoaDelta < .000017   && tailDelta < .000017)
772         {
773             break;
774         }
775     }
776
777     if(_dragFactor < 1e-06 || _dragFactor > 1e6) {
778         _failureMsg = "Drag factor beyond reasonable bounds.";
779         return;
780     } else if(_liftRatio < 1e-04 || _liftRatio > 1e4) {
781         _failureMsg = "Lift ratio beyond reasonable bounds.";
782         return;
783     } else if(Math::abs(_cruiseAoA) >= .174) {
784         _failureMsg = "Cruise AoA > 10 degrees";
785         return;
786     } else if(Math::abs(_tailIncidence) >= .174) {
787         _failureMsg = "Tail incidence > 10 degrees";
788         return;
789     }
790 }
791 }; // namespace yasim