From 88dcafee03657d14f465805ac5c288567eb6fd48 Mon Sep 17 00:00:00 2001 From: csendlinger Date: Tue, 18 Mar 2025 12:15:24 +0100 Subject: [PATCH] fix mu=-1 bug --- src/simulation/energy_binning.cpp | 7 ++++ src/simulation/velocity_distribution.cpp | 42 +++++++++++------------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/simulation/energy_binning.cpp b/src/simulation/energy_binning.cpp index 6c2a765..156e06b 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 5ce2687..003c700 100755 --- a/src/simulation/velocity_distribution.cpp +++ b/src/simulation/velocity_distribution.cpp @@ -180,28 +180,6 @@ CartesianVector VelocityDistributionFromDensityField::rollVelocity( double energy_eV = energy_dist(*engine); - // std::stringstream out; - // if(above) { - // out << "Above: "; - // } else { - // out << "Below: "; - // } - // out << "\n\tEnergyValues: " << energyValues; - // out << "\n\tBinBorders: ["; - // for(auto bb : energyBinBorders_) - // out << bb << ", "; - // out << "]"; - // out << "\n\tdrawn energy: " << energy_eV; - // out << "\n\tlowerIndexEnergyValues: " << energy_values.lbound(); - // out << "\n\tBeginIterator: " << *bordersBeginIterator; - // out << "\n\tEndIterator: " << *(bordersEndIterator - 1); - // out << "\n\tValueBeginIterator: " << *valuesBeginIterator; - // DEBUG_LEVEL_2(out.str()); - - // if (isnan(energy_eV)) { - // ERROR("energy value is nan!"); - // } - // draw pitch angle from DynamicDensityField blitz::Array pitchAngleValues = density_field_-> @@ -214,6 +192,26 @@ CartesianVector VelocityDistributionFromDensityField::rollVelocity( double pitchAngle = pitchAngle_dist(*engine); + +#ifdef debug + if((std::isnan(pitchAngle)) || (std::isnan(energy_eV))) { + std::stringstream out; + above ? out << "Above" : out << "Below"; + out << " threshold bin of " << E_th_bin; + out << "\npitchAngle: " << 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 const double theta = theta_distribution_(*engine)*2*M_PI;