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