From 8d1698d21afe5eaca909120ae55ed5454369443b Mon Sep 17 00:00:00 2001
From: andy <andy>
Date: Wed, 18 Feb 2004 15:36:35 +0000
Subject: [PATCH] Added a <solve-weight> subtag of the approach/cruise
 parameters that can be used to set the variable weights to specific values
 for the solution.

---
 src/FDM/YASim/Airplane.cpp | 27 ++++++++++++++++++++++++++-
 src/FDM/YASim/Airplane.hpp |  6 ++++++
 src/FDM/YASim/FGFDM.cpp    |  4 ++++
 3 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/src/FDM/YASim/Airplane.cpp b/src/FDM/YASim/Airplane.cpp
index bb95ae9ba..ba1674b0c 100644
--- a/src/FDM/YASim/Airplane.cpp
+++ b/src/FDM/YASim/Airplane.cpp
@@ -66,6 +66,8 @@ Airplane::~Airplane()
 	delete (Surface*)_surfs.get(i);    
     for(i=0; i<_contacts.size(); i++)
         delete[] (float*)_contacts.get(i);
+    for(i=0; i<_solveWeights.size(); i++)
+        delete[] (SolveWeight*)_solveWeights.get(i);
 }
 
 void Airplane::iterate(float dt)
@@ -212,6 +214,15 @@ void Airplane::addCruiseControl(int control, float val)
     _cruiseControls.add(c);
 }
 
+void Airplane::addSolutionWeight(bool approach, int idx, float wgt)
+{
+    SolveWeight* w = new SolveWeight();
+    w->approach = approach;
+    w->idx = idx;
+    w->wgt = wgt;
+    _solveWeights.add(w);
+}
+
 int Airplane::numTanks()
 {
     return _tanks.size();
@@ -755,6 +766,18 @@ void Airplane::stabilizeThrust()
 	_model.getThruster(i)->stabilize();
 }
 
+void Airplane::setupWeights(bool isApproach)
+{
+    int i;
+    for(i=0; i<_weights.size(); i++)
+        setWeight(i, 0);
+    for(i=0; i<_solveWeights.size(); i++) {
+        SolveWeight* w = (SolveWeight*)_solveWeights.get(i);
+        if(w->approach == isApproach)
+            setWeight(w->idx, w->wgt);
+    }
+}
+
 void Airplane::runCruise()
 {
     setupState(_cruiseAoA, _cruiseSpeed, &_cruiseState);
@@ -777,6 +800,7 @@ void Airplane::runCruise()
     Math::vmul33(_cruiseState.orient, wind, wind);
  
     setFuelFraction(_cruiseFuel);
+    setupWeights(false);
    
     // Set up the thruster parameters and iterate until the thrust
     // stabilizes.
@@ -818,9 +842,10 @@ void Airplane::runApproach()
     Math::mul3(-1, _approachState.v, wind);
     Math::vmul33(_approachState.orient, wind, wind);
     
-    // Approach is by convention at 20% tank capacity
     setFuelFraction(_approachFuel);
 
+    setupWeights(true);
+
     // Run the thrusters until they get to a stable setting.  FIXME:
     // this is lots of wasted work.
     for(i=0; i<_thrusters.size(); i++) {
diff --git a/src/FDM/YASim/Airplane.hpp b/src/FDM/YASim/Airplane.hpp
index bc35a9b30..763e8d5ea 100644
--- a/src/FDM/YASim/Airplane.hpp
+++ b/src/FDM/YASim/Airplane.hpp
@@ -55,6 +55,8 @@ public:
     void addApproachControl(int control, float val);
     void addCruiseControl(int control, float val);
 
+    void addSolutionWeight(bool approach, int idx, float wgt);
+
     int numGear();
     Gear* getGear(int g);
 
@@ -86,6 +88,7 @@ private:
 	               int handle; float cg[3]; float mass; };
     struct Control { int control; float val; };
     struct WeightRec { int handle; Surface* surf; };
+    struct SolveWeight { bool approach; int idx; float wgt; };
 
     void runCruise();
     void runApproach();
@@ -104,6 +107,7 @@ private:
     void compileContactPoints();
     float normFactor(float f);
     void updateGearState();
+    void setupWeights(bool isApproach);
 
     Model _model;
     ControlMap _controls;
@@ -127,6 +131,8 @@ private:
 
     Vector _rotors;
 
+    Vector _solveWeights;
+
     Vector _cruiseControls;
     State _cruiseState;
     float _cruiseP;
diff --git a/src/FDM/YASim/FGFDM.cpp b/src/FDM/YASim/FGFDM.cpp
index f1d489708..234a25c1a 100644
--- a/src/FDM/YASim/FGFDM.cpp
+++ b/src/FDM/YASim/FGFDM.cpp
@@ -125,6 +125,10 @@ void FGFDM::startElement(const char* name, const XMLAttributes &atts)
 	float alt = attrf(a, "alt") * FT2M;
 	_airplane.setCruise(spd, alt, attrf(a, "fuel", 0.5));
 	_cruiseCurr = true;
+    } else if(eq(name, "solve-weight")) {
+        int idx = attri(a, "idx");
+        float wgt = attrf(a, "weight") * LBS2KG;
+        _airplane.addSolutionWeight(!_cruiseCurr, idx, wgt);
     } else if(eq(name, "cockpit")) {
 	v[0] = attrf(a, "x");
 	v[1] = attrf(a, "y");