ferram4 / Ferram-Aerospace-Research

Aerodynamics model for Kerbal Space Program
Other
239 stars 131 forks source link

NaN's from calling FARAPI.CalculateVesselAeroForces with high altitude values #216

Closed fat-lobyte closed 7 years ago

fat-lobyte commented 7 years ago

Hi! I'm the current maintainer of trajectories and decided to test your newest version of FAR (branch ksp_update at commit e9582884ebda8d077210563d63cd8d448fb9dd8c) with Trajectories. Unfortunately, I got quite a few NaN errors, which all look like this:

[FAR] NaN Prediction Section Error: Inputs: AtmDen: 0 Mach: Infinity Re: NaN Kn: NaN skin: NaN vel: (1799.7, 0.0, 1078.8) I debugged your code and it seems that for very high values of altitude such as 69998.6732426954 (so only 1.4m below the atmosphere ceiling), these values (in FARVesselAero.cs#319)

        pressure = (float)body.GetPressure(altitude);
        temperature = (float)body.GetTemperature(altitude);
        density = (float)body.GetDensity(pressure, temperature);
        speedOfSound = (float)body.GetSpeedOfSound(pressure, density);

Are all zero. This causes all kinds of infinities and NaN's.

Would it be possible for you to add a guard clause that checks for these zeros and simply returns a zero force vector, instead of printing NaN errors?

ferram4 commented 7 years ago

Oh, thanks! I was wondering where this was coming from; I didn't think it was something weird in those values.

Latest commit to ksp_update should have it fixed.

fat-lobyte commented 7 years ago

Hi, this was not enough to fix it unfortunately.

The guard clause that you added does not terminate the function but lets it continue Are you maybe just missing a return statement there? I compiled it with adding return; at FarVesselAero.cs#327 and that seems to do the trick.

ferram4 commented 7 years ago

Yep, forgot that. Added it and pushed the latest commit.