From d17602e1fa837d2b31628b18b83455c4cda15b5e Mon Sep 17 00:00:00 2001 From: csendlinger Date: Sat, 15 Mar 2025 09:09:21 +0100 Subject: [PATCH] Fixed bug with mu=-1; added nan-check in drawing velocity from density if DO_DEBUG is true in the config file --- src/simulation/energy_binning.cpp | 7 +++++++ src/simulation/velocity_distribution.cpp | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/simulation/energy_binning.cpp b/src/simulation/energy_binning.cpp index 6c2a765..fdfcf20 100755 --- a/src/simulation/energy_binning.cpp +++ b/src/simulation/energy_binning.cpp @@ -70,6 +70,13 @@ int PitchAngleBinning::bin(double pitch_angle) const { auto const it = std::lower_bound(pitch_angle_bin_borders_.begin(), pitch_angle_bin_borders_.end(), pitch_angle); + // otherwise the valid mu-value of -1 is assigned to the underflow + // bin which can lead to a segfault in the drawing of the pitch + // angle in "velocity_distribution.cpp" + if(!(pitch_angle < pitch_angle_bin_borders_.front()) && + (it == pitch_angle_bin_borders_.begin())) { + return (it - pitch_angle_bin_borders_.begin()); + } return (it - pitch_angle_bin_borders_.begin()) - 1; } diff --git a/src/simulation/velocity_distribution.cpp b/src/simulation/velocity_distribution.cpp index 12e53b8..6030a03 100755 --- a/src/simulation/velocity_distribution.cpp +++ b/src/simulation/velocity_distribution.cpp @@ -5,6 +5,7 @@ #include "simulation/velocity_distribution_function.h" #include "simulation/density_field.h" #include "simulation/electromagnetic_field.h" +#include "simulation/errors.h" // utility function returning a unit vector perpendicular to the given // (cartesian) TinyVector @@ -119,7 +120,24 @@ CartesianVector VelocityDistributionFromDensityField::rollVelocity( pitchAngleBinBorders_.end(), pitchAngleValues.begin()); - double pitchAngle = pitchAngle_dist(*engine); + double pitchAngle = pitchAngle_dist(*engine); + +#ifdef debug + if((std::isnan(pitchAngle)) || (std::isnan(energy_eV))) { + std::stringstream out; + out << "pitchAngle: " << pitchAngle; + out << "\nenergy value: " << energy_eV; + out << "\nenergy values: " << energyValues; + out << "\npitch angle values: " << pitchAngleValues; + out << "\nenergy bin borders: [ "; + for(auto element : energyBinBorders_) { + out << element << " "; + } + out << "]"; + + ERROR(out.str()); + } +#endif // construct the velocity