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