diff --git a/physics/bufferstop.cpp b/physics/bufferstop.cpp index d1222cb93..6d05fb2ca 100644 --- a/physics/bufferstop.cpp +++ b/physics/bufferstop.cpp @@ -357,7 +357,7 @@ contact_result contact(definition const &Stop, state &Stop_state, contact_input auto penetration{Input.penetration}; auto vehiclecrush{Input.vehicle_end.crush}; auto time{0.0}; - auto final{outcome::none}; + auto ending{outcome::none}; while (Input.dt - time > 1e-12) { auto const step{std::min(length, Input.dt - time)}; auto deflection{penetration - (now.absorber + now.slid + now.crush + vehiclecrush)}; @@ -409,7 +409,7 @@ contact_result contact(definition const &Stop, state &Stop_state, contact_input if (now.slid >= Stop.runout - 1e-9) { // slid off the end of its run-out: it goes on with the vehicle, holding nothing now.overrun = true; - final = outcome::overrun; + ending = outcome::overrun; } } } @@ -453,11 +453,11 @@ contact_result contact(definition const &Stop, state &Stop_state, contact_input result.climbed = true; result.work += climbed.work; velocity = climbed.velocity; - final = climbed.result; + ending = climbed.result; } else if (now.crush >= Stop.demolish) { now.demolished = true; - final = outcome::demolished; + ending = outcome::demolished; } break; } @@ -483,12 +483,12 @@ contact_result contact(definition const &Stop, state &Stop_state, contact_input result.climbed = true; result.work += climbed.work; velocity = climbed.velocity; - final = climbed.result; + ending = climbed.result; break; } if (now.crush >= Stop.demolish) { now.demolished = true; - final = outcome::demolished; + ending = outcome::demolished; break; } } @@ -501,8 +501,8 @@ contact_result contact(definition const &Stop, state &Stop_state, contact_input result.impulse = Input.mass * (Input.closing - velocity); result.vehicle_crush = vehiclecrush - Input.vehicle_end.crush; now.peak_force = std::max(now.peak_force, result.force); - if (final != outcome::none) { - result.result = final; + if (ending != outcome::none) { + result.result = ending; } else if (result.touched) { result.result = now.crush >= collision::crush_mark ? outcome::damaged : outcome::held; diff --git a/physics/components/bufferstops.cpp b/physics/components/bufferstops.cpp index d370d1729..9266bbdd5 100644 --- a/physics/components/bufferstops.cpp +++ b/physics/components/bufferstops.cpp @@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/. #include "utilities/Globals.h" #include "utilities/Logs.h" #include +#include #include #include @@ -116,7 +117,8 @@ void physics::Vehicle::buffer_stop_contact(double const dt) auto would{bufferstop::outcome::none}; auto vehiclecrush{0.0}; input.dt = monitor_step; - for (auto time = 0.0; time < monitor_time; time += monitor_step) + auto const steps{static_cast(std::ceil(monitor_time / monitor_step - 1e-9))}; + for (auto index{0}; index < steps; ++index) { auto const step{bufferstop::contact(installation.stop, state, input)}; would = bufferstop::worse(would, step.result); diff --git a/tests/unit/test_bufferstop.cpp b/tests/unit/test_bufferstop.cpp index fb3cae074..dc90106b3 100644 --- a/tests/unit/test_bufferstop.cpp +++ b/tests/unit/test_bufferstop.cpp @@ -60,7 +60,8 @@ run_result run(bs::definition const &Stop, bs::contact_input Input, double const Input.closing = Speed; Input.penetration = -0.01; auto touched{false}; - for (auto time = 0.0; time < 120.0; time += Input.dt) { + auto const steps{static_cast(std::ceil(120.0 / Input.dt))}; + for (auto index{0}; index < steps; ++index) { auto const step{bs::contact(Stop, result.stop, Input)}; result.worst = bs::worse(result.worst, step.result); result.peak = std::max(result.peak, step.force); @@ -87,7 +88,8 @@ run_result run(bs::definition const &Stop, bs::contact_input Input, double const // the lowest speed [km/h], in steps of a quarter, the vehicle rides up the stop at std::optional ride_up_speed(bs::definition const &Stop, bs::contact_input const &Input) { - for (auto speed = 4.0; speed <= 20.0; speed += 0.25) { + for (auto quarter{16}; quarter <= 80; ++quarter) { + auto const speed{0.25 * quarter}; if (run(Stop, Input, speed * kmh).climbed) { return speed; } @@ -153,7 +155,8 @@ TEST_CASE("bufferstop: the brake elements of a sliding stop hold less the longer // over the 8 m of its run-out the 1848 kJ of the calculation of Rawie 10 EB auto work{0.0}; constexpr double step{0.001}; - for (auto slid = 0.5 * step; slid < 8.0; slid += step) { + for (auto index{0}; index < 8000; ++index) { + auto const slid{(index + 0.5) * step}; work += bs::friction_force(stop, slid) * step; } CHECK(work == doctest::Approx(1848.0e3).epsilon(0.001)); diff --git a/vehicle/dynamic/bufferstop.cpp b/vehicle/dynamic/bufferstop.cpp index eeaa40a86..f8ff7696d 100644 --- a/vehicle/dynamic/bufferstop.cpp +++ b/vehicle/dynamic/bufferstop.cpp @@ -21,6 +21,8 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include +#include namespace { @@ -37,9 +39,9 @@ void TDynamicObject::update_track_ends() { for (auto const end : VehicleEnds) { auto &contact{physics->TrackEnds[end]}; // the scenery is told of the hit of the frame before, on the main thread - if (contact.pending) { - simulation::State.report_buffer_stop(*this, *contact.pending); - contact.pending.reset(); + // taken out first: what the scenery does with it can't reach the one held here + if (auto const notice{std::exchange(contact.pending, std::nullopt)}; notice.has_value()) { + simulation::State.report_buffer_stop(*this, *notice); } } auto const active{simulation::Paths.buffer_stops() > 0 && Global.buffer_stops != global_settings::bufferstop_mode::off &&