Commit 2d3a946c authored by Simon Spannagel's avatar Simon Spannagel
Browse files

Pulse: catch bad_alloc and handle

parent b3c5ca4e
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -145,7 +145,13 @@ void PulseTransferModule::run(Event* event) {

            // Generate pseudo-pulse:
            Pulse pulse(timestep_);
            try {
                pulse.addCharge(propagated_charge.getCharge(), propagated_charge.getLocalTime());
            } catch(const PulseBadAllocException& e) {
                LOG(ERROR) << e.what() << std::endl
                           << "Ignoring pulse contribution at time "
                           << Units::display(propagated_charge.getLocalTime(), {"ms", "us", "ns"});
            }
            pixel_pulse_map[pixel_index] += pulse;

            auto px = pixel_charge_map[pixel_index];
+7 −1
Original line number Diff line number Diff line
@@ -403,7 +403,13 @@ TransientPropagationModule::propagate(Event* event,

            // Create pulse if it doesn't exist. Store induced charge in the returned pulse iterator
            auto pixel_map_iterator = pixel_map.emplace(pixel_index, Pulse(timestep_));
            try {
                pixel_map_iterator.first->second.addCharge(induced, initial_time + runge_kutta.getTime());
            } catch(const PulseBadAllocException& e) {
                LOG(ERROR) << e.what() << std::endl
                           << "Ignoring pulse contribution at time "
                           << Units::display(initial_time + runge_kutta.getTime(), {"ms", "us", "ns"});
            }

            if(output_plots_) {
                potential_difference_->Fill(std::fabs(ramo - last_ramo));
+8 −5
Original line number Diff line number Diff line
@@ -8,7 +8,6 @@
 */

#include "Pulse.hpp"
#include "objects/exceptions.h"

#include <cmath>
#include <numeric>
@@ -21,11 +20,15 @@ void Pulse::addCharge(double charge, double time) {
    // For uninitialized pulses, store all charge in the first bin:
    auto bin = (initialized_ ? static_cast<size_t>(std::lround(time / bin_)) : 0);

    try {
        // Adapt pulse storage vector:
        if(bin >= this->size()) {
            this->resize(bin + 1);
        }
        this->at(bin) += charge;
    } catch(const std::bad_alloc& e) {
        PulseBadAllocException(bin + 1, time, e.what());
    }
}

int Pulse::getCharge() const {
+2 −0
Original line number Diff line number Diff line
@@ -14,6 +14,8 @@

#include <TObject.h>

#include "objects/exceptions.h"

namespace allpix {
    /**
     * @ingroup Objects
+26 −0
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@
#include <string>

#include "core/utils/exceptions.h"
#include "core/utils/text.h"
#include "core/utils/type.h"

namespace allpix {
@@ -62,6 +63,31 @@ namespace allpix {
            }
        }
    };

    /**
     * @ingroup Exceptions
     * @brief Indicates that a pulse object could not be allocated
     */
    class PulseBadAllocException : public RuntimeError {
    public:
        /**
         * @brief Constructs an error for a pulse which could not be extended to the requested size
         * @param bins Number of bins attempted to allocate
         * @param time Total integration time of the pulse
         * @param reason Reason why the allocation failed
         */
        explicit PulseBadAllocException(const size_t bins, const double time, const std::string& reason) {
            error_message_ = "Unable to allocate memory for pulse with ";
            error_message_ += allpix::to_string(bins);
            error_message_ += " bins and total duration of ";
            error_message_ += allpix::to_string(time);
            error_message_ += "ns";

            if(!reason.empty()) {
                error_message_ += ": " + reason;
            }
        }
    };
} // namespace allpix

#endif /* ALLPIX_OBJECT_EXCEPTIONS_H */