]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/yasim-test.cpp
e63a9125ac55224f554a95bbf2b0408a454999e9
[flightgear.git] / src / FDM / YASim / yasim-test.cpp
1 #include <stdio.h>
2
3 #include <simgear/props/props.hxx>
4 #include <simgear/xml/easyxml.hxx>
5
6 #include "FGFDM.hpp"
7 #include "Atmosphere.hpp"
8 #include "Airplane.hpp"
9
10 using namespace yasim;
11
12 // Stubs.  Not needed by a batch program, but required to link.
13 bool fgSetFloat (const char * name, float val) { return false; }
14 bool fgSetBool(char const * name, bool val) { return false; }
15 bool fgGetBool(char const * name, bool def) { return false; }
16 SGPropertyNode* fgGetNode (const char * path, bool create) { return 0; }
17 SGPropertyNode* fgGetNode (const char * path, int i, bool create) { return 0; }
18 float fgGetFloat (const char * name, float defaultValue) { return 0; }
19 double fgGetDouble (const char * name, double defaultValue = 0.0) { return 0; }
20 bool fgSetDouble (const char * name, double defaultValue = 0.0) { return 0; }
21
22 static const float RAD2DEG = 57.2957795131;
23 static const float DEG2RAD = 0.0174532925199;
24 static const float KTS2MPS = 0.514444444444;
25
26
27 // Generate a graph of lift, drag and L/D against AoA at the specified
28 // speed and altitude.  The result is a space-separated file of
29 // numbers: "aoa lift drag LD" (aoa in degrees, lift and drag in
30 // G's).  You can use this in gnuplot like so (assuming the output is
31 // in a file named "dat":
32 //
33 // plot "dat" using 1:2 with lines title 'lift', \ 
34 //      "dat" using 1:3 with lines title 'drag', \ 
35 //      "dat" using 1:4 with lines title 'LD'
36 //
37 void yasim_graph(Airplane* a, float alt, float kts)
38 {
39     Model* m = a->getModel();
40     State s;
41
42     m->setAir(Atmosphere::getStdPressure(alt),
43               Atmosphere::getStdTemperature(alt),
44               Atmosphere::getStdDensity(alt));
45     m->getBody()->recalc();
46
47     for(int deg=-179; deg<=179; deg++) {
48         float aoa = deg * DEG2RAD;
49         Airplane::setupState(aoa, kts * KTS2MPS, 0 ,&s);
50         m->getBody()->reset();
51         m->initIteration();
52         m->calcForces(&s);
53
54         float acc[3];
55         m->getBody()->getAccel(acc);
56         Math::tmul33(s.orient, acc, acc);
57
58         float drag = acc[0] * (-1/9.8);
59         float lift = 1 + acc[2] * (1/9.8);
60
61         printf("%d %g %g %g\n", deg, lift, drag, lift/drag);
62     }
63 }
64
65 int usage()
66 {
67     fprintf(stderr, "Usage: yasim <ac.xml> [-g [-a alt] [-s kts]]\n");
68     return 1;
69 }
70
71 int main(int argc, char** argv)
72 {
73     FGFDM* fdm = new FGFDM();
74     Airplane* a = fdm->getAirplane();
75
76     if(argc < 2) return usage();
77
78     // Read
79     try {
80         string file = argv[1];
81         readXML(file, *fdm);
82     } catch (const sg_exception &e) {
83         printf("XML parse error: %s (%s)\n",
84                e.getFormattedMessage().c_str(), e.getOrigin().c_str());
85     }
86
87     // ... and run
88     a->compile();
89     if(a->getFailureMsg())
90         printf("SOLUTION FAILURE: %s\n", a->getFailureMsg());
91
92     if(!a->getFailureMsg() && argc > 2 && strcmp(argv[2], "-g") == 0) {
93         float alt = 5000, kts = 100;
94         for(int i=3; i<argc; i++) {
95             if     (strcmp(argv[i], "-a") == 0) alt = atof(argv[++i]);
96             else if(strcmp(argv[i], "-s") == 0) kts = atof(argv[++i]);
97             else return usage();
98         }
99         yasim_graph(a, alt, kts);
100     } else {
101         float aoa = a->getCruiseAoA() * RAD2DEG;
102         float tail = -1 * a->getTailIncidence() * RAD2DEG;
103         float drag = 1000 * a->getDragCoefficient();
104         float cg[3];
105         a->getModel()->getBody()->getCG(cg);
106         a->getModel()->getBody()->recalc();
107
108         float SI_inertia[9];
109         a->getModel()->getBody()->getInertiaMatrix(SI_inertia);
110         
111         printf("Solution results:");
112         printf("       Iterations: %d\n", a->getSolutionIterations());
113         printf(" Drag Coefficient: %f\n", drag);
114         printf("       Lift Ratio: %f\n", a->getLiftRatio());
115         printf("       Cruise AoA: %f\n", aoa);
116         printf("   Tail Incidence: %f\n", tail);
117         printf("Approach Elevator: %f\n", a->getApproachElevator());
118         printf("               CG: x:%.3f, y:%.3f, z:%.3f\n\n", cg[0], cg[1], cg[2]);
119         printf("  Inertia tensor : %.3f, %.3f, %.3f\n", SI_inertia[0], SI_inertia[1], SI_inertia[2]);
120         printf("        [kg*m^2]   %.3f, %.3f, %.3f\n", SI_inertia[3], SI_inertia[4], SI_inertia[5]);
121         printf("     Origo at CG   %.3f, %.3f, %.3f\n", SI_inertia[6], SI_inertia[7], SI_inertia[8]);
122     }
123     delete fdm;
124     return 0;
125 }