Skip to content

Optimizations #1194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/input_output/FGGroundCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ double FGDefaultGroundCallback::GetAGLevel(double t, const FGLocation& loc,
return l.GetGeodAltitude() - mTerrainElevation;
}

double FGDefaultGroundCallback::GetAGLevel(const FGLocation& loc) const
{
FGLocation l = loc;
l.SetEllipse(a, b);
return l.GetGeodAltitude() - mTerrainElevation;
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

} // namespace JSBSim
9 changes: 9 additions & 0 deletions src/input_output/FGGroundCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@
FGColumnVector3& w) const
{ return GetAGLevel(time, location, contact, normal, v, w); }

virtual double GetAGLevel(const FGLocation& location) const

Check warning on line 98 in src/input_output/FGGroundCallback.h

View check run for this annotation

Codecov / codecov/patch

src/input_output/FGGroundCallback.h#L98

Added line #L98 was not covered by tests
{
FGLocation lDummy;
FGColumnVector3 vDummy;
return GetAGLevel(location, lDummy, vDummy, vDummy, vDummy);
}

Check warning on line 103 in src/input_output/FGGroundCallback.h

View check run for this annotation

Codecov / codecov/patch

src/input_output/FGGroundCallback.h#L100-L103

Added lines #L100 - L103 were not covered by tests

/** Set the terrain elevation.
Only needs to be implemented if JSBSim should be allowed
to modify the local terrain radius (see the default implementation)
Expand Down Expand Up @@ -133,6 +140,8 @@
FGColumnVector3& normal, FGColumnVector3& v,
FGColumnVector3& w) const override;

double GetAGLevel(const FGLocation& location) const override;

void SetTerrainElevation(double h) override
{ mTerrainElevation = h; }

Expand Down
8 changes: 0 additions & 8 deletions src/math/FGColumnVector3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ INCLUDES
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cmath>

using namespace std;

Expand Down Expand Up @@ -106,13 +105,6 @@ FGColumnVector3& FGColumnVector3::operator/=(const double scalar)

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

double FGColumnVector3::Magnitude(void) const
{
return sqrt( data[0]*data[0] + data[1]*data[1] + data[2]*data[2] );
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

FGColumnVector3& FGColumnVector3::Normalize(void)
{
double Mag = Magnitude();
Expand Down
7 changes: 6 additions & 1 deletion src/math/FGColumnVector3.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ SENTRY
INCLUDES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

#include <cmath>
#include <iosfwd>
#include <string>

Expand Down Expand Up @@ -236,7 +237,11 @@ class JSBSIM_API FGColumnVector3

/** Length of the vector.
Compute and return the euclidean norm of this vector. */
double Magnitude(void) const;
double Magnitude(void) const { return std::sqrt(SqrMagnitude()); }

/** Square of the length of the vector.
Compute and return the square of the euclidean norm of this vector. */
double SqrMagnitude(void) const { return data[0]*data[0] + data[1]*data[1] + data[2]*data[2]; }

/** Length of the vector in a coordinate axis plane.
Compute and return the euclidean norm of this vector projected into
Expand Down
14 changes: 14 additions & 0 deletions src/math/FGLocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ FGLocation::FGLocation(const FGColumnVector3& lv)

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

FGLocation::FGLocation(const FGColumnVector3& lv, double semimajor, double semiminor)
: mECLoc(lv), mCacheValid(false)
{
SetEllipse(semimajor, semiminor);

mLon = mLat = mRadius = 0.0;
mGeodLat = GeodeticAltitude = 0.0;

mTl2ec.InitMatrix();
mTec2l.InitMatrix();
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

FGLocation::FGLocation(const FGLocation& l)
: mECLoc(l.mECLoc), mCacheValid(l.mCacheValid)
{
Expand Down
25 changes: 23 additions & 2 deletions src/math/FGLocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,16 @@
(X,Y,Z) contained in the input FGColumnVector3. Distances are in feet,
the position is expressed in the ECEF frame.
@param lv vector that contain the cartesian coordinates*/
FGLocation(const FGColumnVector3& lv);
explicit FGLocation(const FGColumnVector3& lv);

/** Constructor to initialize the location with the cartesian coordinates
(X,Y,Z) contained in the input FGColumnVector3. Distances are in feet,
the position is expressed in the ECEF frame. Sets the semimajor and
semiminor axis lengths for this planet as if SetEllipse() was called.
@param lv vector that contain the cartesian coordinates
@param semimajor planet semi-major axis in ft.
@param semiminor planet semi-minor axis in ft.*/
FGLocation(const FGColumnVector3& lv, double semimajor, double semiminor);

/** Copy constructor. */
FGLocation(const FGLocation& l);
Expand Down Expand Up @@ -324,7 +333,7 @@
@param lvec Vector in the local horizontal coordinate frame
@return The location in the earth centered and fixed frame */
FGLocation LocalToLocation(const FGColumnVector3& lvec) const {
ComputeDerived(); return mTl2ec*lvec + mECLoc;
ComputeDerived(); return FGLocation(mTl2ec*lvec + mECLoc);
}

/** Conversion from a location in the earth centered and fixed frame
Expand Down Expand Up @@ -416,6 +425,12 @@
return *this;
}

const FGLocation& operator+=(const FGColumnVector3& l) {
mCacheValid = false;
mECLoc += l;
return *this;
}

/** This operator substracts the ECEF position vectors.
The cartesian coordinates of the supplied vector (right side) are
substracted from the ECEF position vector on the left side of the
Expand All @@ -426,6 +441,12 @@
return *this;
}

const FGLocation& operator-=(const FGColumnVector3& l) {
mCacheValid = false;
mECLoc -= l;
return *this;

Check warning on line 447 in src/math/FGLocation.h

View check run for this annotation

Codecov / codecov/patch

src/math/FGLocation.h#L444-L447

Added lines #L444 - L447 were not covered by tests
}

/** This operator scales the ECEF position vector.
The cartesian coordinates of the ECEF position vector on the left side of
the equality are scaled by the supplied value (right side), and a
Expand Down
10 changes: 10 additions & 0 deletions src/models/FGAtmosphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ double FGAtmosphere::ValidateTemperature(double t, const string& msg, bool quiet

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void FGAtmosphere::GetAltitudeDependentValues(AltitudeDependentValues& values, double altitude) const
{
values.Temperature = GetTemperature(altitude);
values.Pressure = GetPressure(altitude);
values.Density = (values.Pressure / (Reng * values.Temperature));
values.SoundSpeed = sqrt(SHRatio * Reng * values.Temperature);
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void FGAtmosphere::Calculate(double altitude)
{
FGPropertyNode* node = PropertyManager->GetNode();
Expand Down
9 changes: 9 additions & 0 deletions src/models/FGAtmosphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ class JSBSIM_API FGAtmosphere : public FGModel {
/// Enums for specifying pressure units.
enum ePressure {eNoPressUnit=0, ePSF, eMillibars, ePascals, eInchesHg};

typedef struct AltitudeDependentValues {
double Temperature = 0.0;
double Pressure = 0.0;
double Density = 0.0;
double SoundSpeed = 0.0;
} AltitudeDependentValues;

/// Constructor
FGAtmosphere(FGFDMExec*);

Expand Down Expand Up @@ -206,6 +213,8 @@ class JSBSIM_API FGAtmosphere : public FGModel {

virtual double GetPressureAltitude() const {return PressureAltitude;}

virtual void GetAltitudeDependentValues(AltitudeDependentValues& values, double altitude) const;

struct Inputs {
double altitudeASL;
double GeodLatitudeDeg;
Expand Down
5 changes: 1 addition & 4 deletions src/models/FGInertial.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ class JSBSIM_API FGInertial : public FGModel {
@param location Location at which the AGL is evaluated.
@see SetGroundCallback */
double GetAltitudeAGL(const FGLocation& location) const {
FGLocation lDummy;
FGColumnVector3 vDummy;
return GroundCallback->GetAGLevel(location, lDummy, vDummy, vDummy,
vDummy);
return GroundCallback->GetAGLevel(location);
}

/** Set the altitude above ground level.
Expand Down
22 changes: 18 additions & 4 deletions tests/unit_tests/FGAtmosphereTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,24 @@ class FGAtmosphereTest : public CxxTest::TestSuite
const double nu0 = mu0/rho0;

for(double h=-1000.0; h<10000; h+= 1000) {
double T = T0 + 0.1*h;
double P = P0 + 1.0*h;
const double T = T0 + 0.1*h;
const double P = P0 + 1.0*h;
const double rho = P/(R*T);
const double a = sqrt(gama*R*T);

FGAtmosphere::AltitudeDependentValues adv;
atm.GetAltitudeDependentValues(adv, h);
TS_ASSERT_DELTA(adv.Temperature, T, epsilon);
TS_ASSERT_DELTA(adv.Pressure, P, epsilon);
TS_ASSERT_DELTA(adv.Density, rho, epsilon);
TS_ASSERT_DELTA(adv.SoundSpeed, a, epsilon);

FGAtmosphere::AltitudeDependentValues adv_0;
atm.GetAltitudeDependentValues(adv_0, 0.0);
TS_ASSERT_EQUALS(adv_0.Temperature, T0);
TS_ASSERT_EQUALS(adv_0.Pressure, P0);
TS_ASSERT_DELTA(adv_0.Density, rho0, epsilon);
TS_ASSERT_DELTA(adv_0.SoundSpeed, a0, epsilon);

TS_ASSERT_DELTA(atm.GetTemperature(h), T, epsilon);
TS_ASSERT_EQUALS(atm.GetTemperature(0.0), T0);
Expand All @@ -160,11 +176,9 @@ class FGAtmosphereTest : public CxxTest::TestSuite
TS_ASSERT_DELTA(atm.GetPressure(h), P, epsilon);
TS_ASSERT_EQUALS(atm.GetPressure(0.0), P0);

double rho = P/(R*T);
TS_ASSERT_DELTA(atm.GetDensity(h), rho, epsilon);
TS_ASSERT_DELTA(atm.GetDensity(0.0), rho0, epsilon);

double a = sqrt(gama*R*T);
TS_ASSERT_DELTA(atm.GetSoundSpeed(h), a, epsilon);
TS_ASSERT_DELTA(atm.GetSoundSpeed(0.0), a0, epsilon);

Expand Down
18 changes: 18 additions & 0 deletions tests/unit_tests/FGColumnVector3Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,24 @@ class FGColumnVector3Test : public CxxTest::TestSuite
TS_ASSERT_EQUALS(Z(3), 1.0);
}

void testSqrMagnitude(void) {
JSBSim::FGColumnVector3 v0;
JSBSim::FGColumnVector3 v(3.0, 4.0, 0.0);

TS_ASSERT_EQUALS(v0.SqrMagnitude(), 0.0);
TS_ASSERT_EQUALS(v.SqrMagnitude(), 25.0);
TS_ASSERT_EQUALS(DotProduct(v,v), v.SqrMagnitude());

// Verify that the operands are not modified
TS_ASSERT_EQUALS(v0(1), 0.0);
TS_ASSERT_EQUALS(v0(2), 0.0);
TS_ASSERT_EQUALS(v0(3), 0.0);

TS_ASSERT_EQUALS(v(1), 3.0);
TS_ASSERT_EQUALS(v(2), 4.0);
TS_ASSERT_EQUALS(v(3), 0.0);
}

void testMagnitude(void) {
JSBSim::FGColumnVector3 v0;
JSBSim::FGColumnVector3 v(3.0, 4.0, 0.0);
Expand Down
12 changes: 10 additions & 2 deletions tests/unit_tests/FGGroundCallbackTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class FGGroundCallbackTest : public CxxTest::TestSuite
double lat_rad = lat*M_PI/180.;
loc = FGLocation(lon_rad, lat_rad, RadiusReference);
double agl = cb->GetAGLevel(loc, contact, normal, v, w);
double agl_2 = cb->GetAGLevel(loc);
TS_ASSERT_EQUALS(agl, agl_2);
TS_ASSERT_DELTA(0.0, agl, 1e-8);
TS_ASSERT_VECTOR_EQUALS(v, zero);
TS_ASSERT_VECTOR_EQUALS(w, zero);
Expand Down Expand Up @@ -88,6 +90,8 @@ class FGGroundCallbackTest : public CxxTest::TestSuite
double lat_rad = lat*M_PI/180.;
loc = FGLocation(lon_rad, lat_rad, RadiusReference+h);
double agl = cb->GetAGLevel(loc, contact, normal, v, w);
double agl_2 = cb->GetAGLevel(loc);
TS_ASSERT_EQUALS(agl, agl_2);
TS_ASSERT_DELTA(h/agl, 1.0, epsilon*100.);
TS_ASSERT_VECTOR_EQUALS(v, zero);
TS_ASSERT_VECTOR_EQUALS(w, zero);
Expand Down Expand Up @@ -129,6 +133,8 @@ class FGGroundCallbackTest : public CxxTest::TestSuite
double lat_rad = lat*M_PI/180.;
loc = FGLocation(lon_rad, lat_rad, RadiusReference+h);
double agl = cb->GetAGLevel(loc, contact, normal, v, w);
double agl_2 = cb->GetAGLevel(loc);
TS_ASSERT_EQUALS(agl, agl_2);
TS_ASSERT_DELTA((h-elevation)/agl, 1.0, epsilon*100.);
TS_ASSERT_VECTOR_EQUALS(v, zero);
TS_ASSERT_VECTOR_EQUALS(w, zero);
Expand Down Expand Up @@ -195,13 +201,15 @@ class FGGroundCallbackTest : public CxxTest::TestSuite
double lat_rad = lat*M_PI/180.;
loc.SetPositionGeodetic(lon_rad, lat_rad, h);
double agl = cb->GetAGLevel(loc, contact, normal, v, w);
double agl_2 = cb->GetAGLevel(loc);
TS_ASSERT_EQUALS(agl, agl_2);
TS_ASSERT_DELTA(h, agl, 1e-8);
TS_ASSERT_VECTOR_EQUALS(v, zero);
TS_ASSERT_VECTOR_EQUALS(w, zero);
TS_ASSERT_DELTA(normal(1), cos(lat_rad)*cos(lon_rad), epsilon);
TS_ASSERT_DELTA(normal(2), cos(lat_rad)*sin(lon_rad), epsilon);
TS_ASSERT_DELTA(normal(3), sin(lat_rad), epsilon);
FGColumnVector3 vLoc = loc-h*normal;
FGColumnVector3 vLoc = FGColumnVector3(loc)-h*normal;
FGColumnVector3 vContact = contact;
TS_ASSERT_DELTA(vLoc(1), vContact(1), 1e-7);
TS_ASSERT_DELTA(vLoc(2), vContact(2), 1e-7);
Expand Down Expand Up @@ -235,7 +243,7 @@ class FGGroundCallbackTest : public CxxTest::TestSuite
TS_ASSERT_DELTA(normal(1), cos(lat_rad)*cos(lon_rad), epsilon);
TS_ASSERT_DELTA(normal(2), cos(lat_rad)*sin(lon_rad), epsilon);
TS_ASSERT_DELTA(normal(3), sin(lat_rad), epsilon);
FGColumnVector3 vLoc = loc-(h-elevation)*normal;
FGColumnVector3 vLoc = FGColumnVector3(loc)-(h-elevation)*normal;
FGColumnVector3 vContact = contact;
TS_ASSERT_DELTA(vLoc(1), vContact(1), 1e-7);
TS_ASSERT_DELTA(vLoc(2), vContact(2), 1e-7);
Expand Down
3 changes: 3 additions & 0 deletions tests/unit_tests/FGLocationTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ class FGLocationTest : public CxxTest::TestSuite

lv3.SetEllipse(1., 1.);
CheckLocation(lv3, v);

JSBSim::FGLocation lv4(v, 1., 1.);
CheckLocation(lv3, v);
}

void testCopyConstructor() {
Expand Down
Loading