Fixed floating point exception in adiabatic conversion initialize!
#569
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
I was running some simulations where I saw the NaR warning during runtime. To debug exactly where this first appears, I switched on trapping for floating point exceptions.
In this calculation in
adiabatic_conversion.jl
:if
σ_levels_half[1]
is less than or equal to zero, then we would have a division by zero, or invalid arguments tolog
.This doesn't normally cause any bugs, because this simply returns
NaN
in the first element and in any of those cases the next line immediately throws away theNaN
and swaps it forlog(2)
.However, if we are trapping all floating point exceptions for debug purposes, the first expression would always raise e.g.
FE_DIVBYZERO
where we divide by zero, andFE_INVALID
where we take the logarithm of a negative number; even though any problems caused by these exceptions are already being treated in the next line.This means floating point exceptions caused by genuine bugs in model initialization that happen after
AdiabaticConversion
is initialized will be masked by this benign error. It is better if these lines are reordered so that no invalid operations are intentionally performed.To reproduce
In the REPL, run the following :
using SpeedyWeather
This throws an error with a stacktrace pointing to the changed lines.
Expected behaviour would be
FE_DIVBYZERO
andFE_INVALID
never being triggered intentionally where possible (otherwise it becomes very hard to debug occurrences of this happening unintentionally).Fix
Reorder the calculation so that rather than replacing the
NaN
after it is generated, it is never generated in the first place.