Debian Codegraph @

src/FDM/AIWake/WakeMesh.cxx

raw symbols SCIP
1 // WakeMesh.cxx -- Mesh for the computation of a wing wake.
2 //
3 // Written by Bertrand Coconnier, started March 2017.
4 //
5 // Copyright (C) 2017 Bertrand Coconnier - [email protected]
6 //
7 // This program is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU General Public License as published by the Free Software
9 // Foundation; either version 2 of the License, or (at your option) any later
10 // version.
11 //
12 // This program is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 // details.
16 //
17 // You should have received a copy of the GNU General Public License along with
18 // this program; if not, write to the Free Software Foundation, Inc., 51
19 // Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 //
21 // $Id$
22
23 #include <vector>
24 #include <cmath>
25
26 #include <simgear/structure/SGSharedPtr.hxx>
27 #include <simgear/math/SGVec3.hxx>
28 #include <simgear/debug/logstream.hxx>
29
30 #include "WakeMesh.hxx"
31 extern "C" {
32 #include "../LaRCsim/ls_matrix.h"
33 }
34
35 WakeMesh::WakeMesh(double _span, double _chord, const std::string& aircraft_name)
36 : nelm(10), span(_span), chord(_chord)
37 {
38 double y1 = -0.5*span;
39 double ds = span / nelm;
40
41 for(int i=0; i<nelm; i++) {
42 double y2 = y1 + ds;
43 elements.push_back(new AeroElement(SGVec3d(-chord, y1, 0.),
44 SGVec3d(0., y1, 0.),
45 SGVec3d(0., y2, 0.),
46 SGVec3d(-chord, y2, 0.)));
47 y1 = y2;
48 }
49
50 influenceMtx = nr_matrix(1, nelm, 1, nelm);
51 Gamma = nr_matrix(1, nelm, 1, 1);
52
53 for (int i=0; i < nelm; ++i) {
54 SGVec3d normal = elements[i]->getNormal();
55 SGVec3d collPt = elements[i]->getCollocationPoint();
56
57 for (int j=0; j < nelm; ++j)
58 influenceMtx[i+1][j+1] = dot(elements[j]->getInducedVelocity(collPt),
59 normal);
60 }
61
62 // Compute the inverse matrix with the Gauss-Jordan algorithm
63 int ret = nr_gaussj(influenceMtx, nelm, nullptr, 0);
64 if (ret) {
65 // Something went wrong with the matrix inversion.
66
67 // 1. Nullify the influence matrix to disable the current aircraft wake.
68 for (int i=0; i < nelm; ++i) {
69 for (int j=0; j < nelm; ++j)
70 influenceMtx[i+1][j+1] = 0.0;
71 }
72 // 2. Report the issue in the log.
73 SG_LOG(SG_FLIGHT, SG_WARN,
74 "Failed to build wake mesh. " << aircraft_name << " ( span:"
75 << _span << ", chord:" << _chord << ") wake will be ignored.");
76 }
77 }
78
79 WakeMesh::~WakeMesh()
80 {
81 nr_free_matrix(influenceMtx, 1, nelm, 1, nelm);
82 nr_free_matrix(Gamma, 1, nelm, 1, 1);
83 }
84
85 double WakeMesh::computeAoA(double vel, double rho, double weight)
86 {
87 for (int i=1; i<=nelm; ++i) {
88 Gamma[i][1] = 0.0;
89 for (int k=1; k<=nelm; ++k)
90 Gamma[i][1] -= influenceMtx[i][k];
91 Gamma[i][1] *= vel;
92 }
93
94 // Compute the lift only. Velocities in the z direction are discarded
95 // because they only produce drag. This include the vertical component
96 // vel*sin(alpha) and the induced velocities on the bound vortex.
97 // This assumption is only valid for small angles.
98 SGVec3d f(0.,0.,0.);
99 SGVec3d v(-vel, 0.0, 0.0);
100
101 for (int i=0; i<nelm; ++i)
102 f += rho*Gamma[i+1][1]*cross(v, elements[i]->getBoundVortex());
103
104 double sinAlpha = -weight/f[2];
105
106 for (int i=1; i<=nelm; ++i)
107 Gamma[i][1] *= sinAlpha;
108
109 return asin(sinAlpha);
110 }
111
112 SGVec3d WakeMesh::getInducedVelocityAt(const SGVec3d& at) const
113 {
114 SGVec3d v(0., 0., 0.);
115
116 for (int i=0; i<nelm; ++i)
117 v += Gamma[i+1][1] * elements[i]->getInducedVelocity(at);
118
119 return v;
120 }