Skip to content

FGTurbine does not update the thruster inputs before calling FGThruster::Calculate#1259

Merged
bcoconni merged 1 commit intoJSBSim-Team:masterfrom
bcoconni:turbine_bug
Apr 15, 2025
Merged

FGTurbine does not update the thruster inputs before calling FGThruster::Calculate#1259
bcoconni merged 1 commit intoJSBSim-Team:masterfrom
bcoconni:turbine_bug

Conversation

@bcoconni
Copy link
Member

As the title says, the bug is silently triggered by the C130 model which has a creative combination of FGTurbine engines with FGPropeller.

<engine file="t56">
<feed>0</feed>
<thruster file="t56_prop">

Currently since FGTurbine does not call FGEngine::LoadThrusterInputs then FGThruster::in::SoundSpeed is not initialized.
void FGEngine::LoadThrusterInputs()
{
Thruster->in.TotalDeltaT = in.TotalDeltaT;
Thruster->in.H_agl = in.H_agl;
Thruster->in.PQRi = in.PQRi;
Thruster->in.AeroPQR = in.AeroPQR;
Thruster->in.AeroUVW = in.AeroUVW;
Thruster->in.Density = in.Density;
Thruster->in.Pressure = in.Pressure;
Thruster->in.Soundspeed = in.Soundspeed;

So when the variable HelicalTipMach is computed in.SoundSpeed contains garbage and is potentially zero which raises a numerical exception.
HelicalTipMach = sqrt(Vtip*Vtip + Vel*Vel) / in.Soundspeed;

Fortunately, the C130 model do not use the tables CP_MACH and CT_MACH hence the bug has no further consequences.

I have found the bug by chance while running the debugger which was halting at the line of code that computes HelicalTipMach and was complaining that a numerical exception was occurring. The bug fix is trivial: it just adds a call to FGEngine::LoadThrusterInputs before the call to FGThruster::Calculate.

@codecov
Copy link

codecov bot commented Apr 13, 2025

Codecov Report

Attention: Patch coverage is 0% with 1 line in your changes missing coverage. Please review.

Project coverage is 24.94%. Comparing base (13008f3) to head (2ee096c).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/models/propulsion/FGTurbine.cpp 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1259      +/-   ##
==========================================
- Coverage   24.95%   24.94%   -0.01%     
==========================================
  Files         169      169              
  Lines       19523    19524       +1     
==========================================
  Hits         4871     4871              
- Misses      14652    14653       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@seanmcleod70
Copy link
Contributor

Saw some interesting things while trying to reproduce this using Visual Studio.

Running a release build under the debugger, it was hit and miss, every 3rd-4th run would stop with a division by 0 exception.

Exception thrown at 0x00000001400D8F77 in JSBSim.exe: 0xC000008E: Floating-point division by zero (parameters: 0x0000000000000000, 0x0000000000001924).
Unhandled exception at 0x00000001400D8F77 in JSBSim.exe: 0xC000008E: Floating-point division by zero (parameters: 0x0000000000000000, 0x0000000000001924).

However running a debug build under the debugger I never saw a division by 0 exception. But did notice a very consistent pattern in the uninitialized structure.

image

Note the value of the uninitialized doubles.

Which is more than likely coming from the debug heap used by the debug build of MSVC.

https://www.nobugs.org/developer/win32/debug_crt_heap.html

Where the debug heap returns heap memory with a specific fill pattern which result in an unusual double value.

So at least for debug MSVC builds we could potentially have some asserts looking for these sorts of patterns of uninitialized doubles etc. in various of the heap based structures in JSBSim.

@bcoconni
Copy link
Member Author

@seanmcleod70, based on your investigations, could you confirm that this PR indeed fixes the reported bug ?

@bcoconni bcoconni added the bug label Apr 14, 2025
@seanmcleod70
Copy link
Contributor

Just confirmed that the debug heap in MSVC fills heap memory it returns with 0xCD, and that 8 bytes of that results in a double with a value of -6.2774385622041925e+66, so a good indicator of an uninitialized member. Also the stack seems to also be filled with 0xCD, so uninitialized local doubles also stand out.

I tested your proposed fix in both debug and release builds and it does the trick.

@bcoconni bcoconni merged commit 2006648 into JSBSim-Team:master Apr 15, 2025
29 checks passed
@bcoconni bcoconni deleted the turbine_bug branch April 15, 2025 16:27
@bcoconni
Copy link
Member Author

Just confirmed that the debug heap in MSVC fills heap memory it returns with 0xCD, and that 8 bytes of that results in a double with a value of -6.2774385622041925e+66, so a good indicator of an uninitialized member. Also the stack seems to also be filled with 0xCD, so uninitialized local doubles also stand out.

I have tried adding a test before the computation of HelicalTipMach on Windows in Debug mode:

+  char* p = reinterpret_cast<char*>(&in.Soundspeed);
+  assert(*p != 0xCD);
  HelicalTipMach = sqrt(Vtip*Vtip + Vel*Vel) / in.Soundspeed;

But the assertion did not fail. Not sure what I did wrong ?

I tested your proposed fix in both debug and release builds and it does the trick.

Thanks. PR merged !

@seanmcleod70
Copy link
Contributor

But the assertion did not fail. Not sure what I did wrong ?

Hmm, was also stumped for a bit.

  char* p = reinterpret_cast<char*>(&in.Soundspeed);
  char pp = *p;
  assert(*p != 0xCD);
*p != 0xCD
true
*p
0xcd 'Í'
*p != 0xcd
true
*p == 0xcd
false

pp
0xcd 'Í'
pp != 0xCD
true
pp == 0xcd
false
pp == (char)0xcd
true

It's a signed versus unsigned issue.

0xCD = 1100 1101 (205)

image

@bcoconni
Copy link
Member Author

Good catch 👍 ! Changing the type to unsigned char* fixed the problem.

bcoconni added a commit to bcoconni/jsbsim that referenced this pull request May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants