From 0f61567bcb84cc809618ebe5f7e1aeae1ddebded Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Mon, 16 Nov 2026 00:26:18 +0100 Subject: [PATCH] fix(physics): only the energy of a hit past the buffers of EN 15551 crushes the ends Found running every scenario of the install with physics.collisions new: two locomotives (SU45 117 t, SM42 84 t) put down by calkowo_v2_kontenera inside each other crushed both their ends at 5 km/h right after the start, deeper hit after hit, until both overrode (0.60 m); in baltyk_sdm the coaches of a train pushed into a vehicle derailed at the end of the track (a wall) crushed at 5-8 km/h. Some 50 kJ of relative motion, which the buffers take in with room to spare. The crush went by the closing velocity at the stop of the buffers alone: buffers pressed all the way from the start of the contact (put down overlapping, or knocked onto each other by a hit elsewhere) left the whole hit to the underframes, every step anew. Each pair now counts the energy of its contact (collision::contact_energy): what the springs of the buffers take in while they're pressed and the pair closes in, kept down to what they hold once they spring back, reset when they part. A hit may crush only what it brings to the stop past what the buffers of EN 15551 of both ends would take in, by their BufferCategory (the key now also gives that energy) or 30 kJ a buffer by default: budget = 1/2 mu v^2 - max(0, capacity - absorbed) collision::hit() stops the crush once its work reaches the budget, the rest springs back. The crush of a whole hit thus never takes more than the hit brought past the buffers; a train behind pushing the pair on brings in more through the velocity of the next step. The monitor goes by the same budget. The log of a crush tells the energies. Pressing alone, a locomotive pushing a braked train at a stand, and vehicles put down overlapping don't crush. The crash cases keep crushing: the hits of two masses, EU07 into a 430W, the EN 15227 units at 36 km/h and the 15 km/h PoC cases as before; at 25-40 km/h fewer ends deep in the trains crush (the buffers of the couplings there take in their energy). --- McZapkie/MOVER.h | 2 + McZapkie/Mover.cpp | 23 ++- McZapkie/collision.cpp | 74 ++++++- McZapkie/collision.h | 65 +++++- tests/unit/test_collisions.cpp | 354 +++++++++++++++++++++++++++++++++ 5 files changed, 503 insertions(+), 15 deletions(-) diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index 7e708d5c8..a2ae1368c 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -1304,6 +1304,8 @@ struct TCoupling double hit_duration{0.0}; // physics.collisions monitor: the log told of the crush of this hit of the pair already bool crush_monitored{false}; + // the energy the contact of the pair brought to its buffers, for the budget of the crush + collision::contact_energy contact; int sounds{0}; // sounds emitted by the coupling devices bool Render = false; /*ABu: czy rysowac jak zaczepiony sprzeg*/ diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index f33bbb8ed..1efeb597c 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -1623,6 +1623,11 @@ bool TMoverParameters::crush_hit(int const End, double const dt) // the monitor takes the whole hit at once, over a second, longer than the crush of any hit lasts input.dt = monitor ? 1.0 : dt; input.start_velocity = thiscoupler.closing; + // the crush takes only what the hit brings past what the buffers of EN 15551 of both ends take in + auto const reduced{mass * othermass / (mass + othermass)}; + auto const capacity{collision::buffer_capacity(thiscoupler.collision, collision_kind()) + collision::buffer_capacity(othercoupler.collision, othervehicle->collision_kind())}; + auto const hitvelocity{std::max(closing, thiscoupler.closing)}; + input.budget = thiscoupler.contact.budget(reduced, hitvelocity, capacity); auto const hit{collision::hit({firstcurve, thiscoupler.crush}, {secondcurve, othercoupler.crush}, input)}; // the override, by the crush of both ends auto const climbing_of = [&](double const Crush1, double const Crush2) { @@ -1664,7 +1669,6 @@ bool TMoverParameters::crush_hit(int const End, double const dt) { // the steel takes it, the legacy collision follows; its peak, under the force an end gives at, for the push // off the track - auto const reduced{mass * othermass / (mass + othermass)}; auto const weaker{std::min(firstcurve.force(thiscoupler.crush), secondcurve.force(othercoupler.crush))}; auto const peak{std::min(springs + coupler_physics::impact_force(closing, stiffness, mass, othermass), weaker)}; auto const duration{std::min(dt, (1.0 + input.restitution) * std::sqrt(reduced / stiffness))}; @@ -1691,13 +1695,19 @@ bool TMoverParameters::crush_hit(int const End, double const dt) record_hit(thiscoupler, hit.force, hit.duration); record_hit(othercoupler, hit.force, hit.duration); + // the energy the crush took, for both vehicles' count of the contact + thiscoupler.contact.crushed(hit.work); + othercoupler.contact.crushed(hit.work); // what the log tells of the pair, for the analysis of a crush + auto const kinetic{0.5 * reduced * hitvelocity * hitvelocity}; auto const pair = [&]() { return std::format("{} {:.1f} t {}, {} end crushed {:.2f} m, gives at {:.0f} kN; {} {:.1f} t {}, {} end crushed {:.2f} m, gives at {:.0f} kN; force {:.0f} kN, springs {:.0f} kN, " - "closing at {:.1f} km/h at the start of the step", + "closing at {:.1f} km/h at the start of the step; energy {:.0f} kJ of the motion and {:.0f} kJ in the buffers, past the {:.0f} kJ they take in, " + "{:.0f} kJ crushed in the contact", Name, TotalMass / 1000.0, kind_name(collision_kind()), end_name(End), thiscoupler.crush, firstcurve.force(thiscoupler.crush) / 1000.0, othervehicle->Name, othervehicle->TotalMass / 1000.0, kind_name(othervehicle->collision_kind()), end_name(otherend), othercoupler.crush, - secondcurve.force(othercoupler.crush) / 1000.0, hit.force / 1000.0, springs / 1000.0, input.start_velocity * 3.6); + secondcurve.force(othercoupler.crush) / 1000.0, hit.force / 1000.0, springs / 1000.0, input.start_velocity * 3.6, kinetic / 1000.0, + thiscoupler.contact.absorbed() / 1000.0, capacity / 1000.0, thiscoupler.contact.crush_work() / 1000.0); }; auto const crushed = [&](TMoverParameters const &Vehicle, int const VehicleEnd, double const Before, double const After) { if (Before < collision::crush_mark && After >= collision::crush_mark) @@ -5819,9 +5829,15 @@ void TMoverParameters::ComputeTotalForce(double dt) Couplers[end].closing = -CouplerVelocityDifference(end, *Neighbours[end].vehicle->MoverParameters, Neighbours[end].vehicle_end) * DirF(end); Couplers[end].CForce = CouplerForce(end, dt); FTrain += Couplers[end].CForce; + // what the buffers of the pair take in, for the budget of the crush (physics.collisions new and monitor) + auto &coupler{Couplers[end]}; + coupler.contact.update(coupler.Dist < 0.0, coupler.CheckCollision, coupler.closing, compression_at(coupler, end), std::max(0.0, -coupler.Dist), dt); } else + { Couplers[end].CForce = 0; + Couplers[end].contact.reset(); + } } FStand += Fb; @@ -11928,6 +11944,7 @@ void TMoverParameters::LoadFIZ_BuffCoupl(std::string const &line, int const Inde thiscoupler->physics.buffers = collision::buffer_curve(*category, thiscoupler->physics.buffers.progression); } thiscoupler->collision = collision_definition(line); + thiscoupler->collision.buffers = category; if (Index == 0) { diff --git a/McZapkie/collision.cpp b/McZapkie/collision.cpp index 60bcf4a41..0abdfdd3b 100644 --- a/McZapkie/collision.cpp +++ b/McZapkie/collision.cpp @@ -156,6 +156,51 @@ std::optional buffer_category_of(char const Name) { } } +buffer_category default_buffers(vehicle_kind const Kind) { + return Kind == vehicle_kind::coach ? buffer_category::passenger : buffer_category::A; +} + +double buffer_capacity(end_definition const &Definition, vehicle_kind const Kind) { + // two buffers side by side + return 2.0 * rating(Definition.buffers.value_or(default_buffers(Kind))).energy; +} + +void contact_energy::update(bool const Pressed, bool const Solid, double const Closing, double const Force, double const Compression, double const Dt) { + if (false == Pressed) { + // the buffers apart, the contact is over + reset(); + return; + } + if (Solid) { + // pressed all the way, the buffers take in nothing more + return; + } + auto const force{std::max(Force, 0.0)}; + if (Closing > 0.0) { + m_absorbed += force * Closing * std::max(Dt, 0.0); + } + else if (Closing < 0.0) { + // springing back, they hold no more than their springs pressed in so far + m_absorbed = std::min(m_absorbed, 0.5 * force * std::max(Compression, 0.0)); + } +} + +void contact_energy::crushed(double const Work) { + m_crushed += std::max(Work, 0.0); +} + +double contact_energy::budget(double const Mass, double const Closing, double const Capacity) const { + auto const closing{std::max(Closing, 0.0)}; + // what the buffers of EN 15551 would still take in: the springs of the .fiz taking in more leave no more energy past + // the buffers than the hit has + return 0.5 * std::max(Mass, 0.0) * closing * closing - std::max(0.0, Capacity - m_absorbed); +} + +void contact_energy::reset() { + m_absorbed = 0.0; + m_crushed = 0.0; +} + hit_result hit(end_state const &First, end_state const &Second, hit_input const &Input) { hit_result result; if (Input.velocity <= 0.0 || Input.mass1 <= 0.0 || Input.mass2 <= 0.0 || Input.dt <= 0.0) { @@ -177,7 +222,7 @@ hit_result hit(end_state const &First, end_state const &Second, hit_input const auto const elastic_limit = [&](double const Force) { return std::max(0.0, Force - springs) / root; }; { auto const force{std::min(First.curve.force(crush1), Second.curve.force(crush2))}; - if (false == std::isfinite(force) || velocity <= elastic_limit(force)) { + if (false == std::isfinite(force) || velocity <= elastic_limit(force) || false == (Input.budget > 0.0)) { // the steel takes it all: the legacy elastic exchange return result; } @@ -189,6 +234,8 @@ hit_result hit(end_state const &First, end_state const &Second, hit_input const auto time{0.0}; auto rebound{false}; auto crushtime{0.0}; + auto work{0.0}; + auto cap{0.0}; // the force the crush went at last while (Input.dt - time > 1e-12) { auto const force1{First.curve.force(crush1)}; auto const force2{Second.curve.force(crush2)}; @@ -205,7 +252,19 @@ hit_result hit(end_state const &First, end_state const &Second, hit_input const step = (velocity - limit) * mass / force; change = velocity - limit; } - auto const crush{(velocity - 0.5 * change) * step}; + auto crush{(velocity - 0.5 * change) * step}; + auto spent{false}; + if (work + force * crush > Input.budget) { + // the energy of the hit past the buffers runs out in the slice: the crush goes as far as it takes, the + // buffers of EN 15551 would take in the rest + auto const share{std::clamp((Input.budget - work) / std::max(force * crush, 1e-12), 0.0, 1.0)}; + step *= share; + change *= share; + crush *= share; + spent = true; + } + work += force * crush; + cap = force; // the weaker end gives, both alike when they're alike if (std::abs(force1 - force2) <= 1e-9 * force) { crush1 += 0.5 * crush; @@ -221,14 +280,20 @@ hit_result hit(end_state const &First, end_state const &Second, hit_input const time += step; crushtime += step; result.force = std::max(result.force, force); + if (spent) { + result.spent = true; + rebound = true; + break; + } } auto after{velocity}; auto duration{crushtime}; if (rebound) { // the steel springs back with the restitution, the springs push the pair apart over the rest of the step - // under the force the weaker end gives at, as the slices stopped there - result.force = std::max(result.force, springs + velocity * root); + // under the force the weaker end gives at, as the slices stopped there. a crush stopped at the budget: the + // buffers of EN 15551 would have taken in the rest, its force no more than what crushes the end + result.force = std::max(result.force, result.spent ? std::min(springs + velocity * root, cap) : springs + velocity * root); if (velocity > 0.0) { duration += (1.0 + restitution) * velocity * mass / std::max(velocity * root, 1.0); } @@ -239,6 +304,7 @@ hit_result hit(end_state const &First, end_state const &Second, hit_input const result.duration = std::min(duration, Input.dt); result.crush1 = crush1 - First.crush; result.crush2 = crush2 - Second.crush; + result.work = work; return result; } diff --git a/McZapkie/collision.h b/McZapkie/collision.h index b51fc6561..4964759af 100644 --- a/McZapkie/collision.h +++ b/McZapkie/collision.h @@ -9,6 +9,7 @@ http://mozilla.org/MPL/2.0/. #pragma once +#include #include #include "couplerphysics.h" @@ -17,7 +18,8 @@ http://mozilla.org/MPL/2.0/. // TMoverParameters::CollisionDetect()), as plain functions of the vehicles and the hit: // // crush: once the buffers of the pair are pressed all the way, the underframes carry the rest of the hit, up to the -// force the weaker end gives at. past it the end crushes for good: a short peak of force, then a lower plateau as +// force the weaker end gives at. only the energy the hit brings past what the buffers of EN 15551 of both ends take +// in can crush them (contact_energy): a hit the buffers would take, or the push of a train at a stand, never does. past it the end crushes for good: a short peak of force, then a lower plateau as // the structure folds, then it packs down and stiffens (the force-crush curves of the full scale tests of the FRA: // Tyrell et al., IMECE2002-33247). the hit is an impulse limited to that force over the step, so the force never // goes past it and the depth of the crush is the same with any step; the depth stays with the end @@ -64,6 +66,15 @@ enum class vehicle_kind { // do (2 MN of the crash buffers in service) crush_curve default_crush(vehicle_kind Kind); +// the buffers of EN 15551, by their category +enum class buffer_category { + A, // 105 mm, 30 kJ + B, // 105 mm, 50 kJ + C, // 105 mm, 70 kJ + L, // the long stroke, 150 mm + passenger // 110 mm, of the coaches +}; + // the optional keys of the .fiz for an end (BuffCoupl): what's not given comes from the kind of the vehicle struct end_definition { std::optional peak; // FcrushPeak [N] @@ -75,6 +86,7 @@ struct end_definition { std::optional crash_zone; // CrashZone: built to EN 15227 std::optional anticlimber; // AntiClimber std::optional buffer_height; // BufferHeight, of the empty vehicle above the rail [m] + std::optional buffers; // BufferCategory }; vehicle_kind kind_of(bool Powered, bool Passenger, bool CrashZone); crush_curve crush_of(end_definition const &Definition, vehicle_kind Kind); @@ -85,13 +97,6 @@ double buffer_height(end_definition const &Definition, vehicle_kind Kind, double // ---- the buffers, EN 15551 -enum class buffer_category { - A, // 105 mm, 30 kJ - B, // 105 mm, 50 kJ - C, // 105 mm, 70 kJ - L, // the long stroke, 150 mm - passenger // 110 mm, of the coaches -}; struct buffer_rating { double stroke{0.105}; // [m] double energy{30.0e3}; // dynamic energy of one buffer [J] @@ -101,6 +106,45 @@ buffer_rating rating(buffer_category Category); // over the stroke coupler_physics::spring_curve buffer_curve(buffer_category Category, double Progression); std::optional buffer_category_of(char Name); +// the buffers of an end without the category given: of category A, those of a coach of its own category (EN 15551, +// 30 kJ a buffer either way) +buffer_category default_buffers(vehicle_kind Kind); +// the energy the two buffers of an end take in over their stroke, of the category given or the default of the kind [J] +double buffer_capacity(end_definition const &Definition, vehicle_kind Kind); + +// ---- the energy of a hit, for the crush + +// the energy budget of the crush in a contact of two ends. what a hit brings to the stop of the buffers is the kinetic +// energy of the relative motion of the pair there, and what its buffers took in on the way (the springs of the .fiz, +// which may take less than the buffers of EN 15551 would). the buffers of EN 15551 of both ends take in their +// capacity of it first, the crush may take only the rest: +// budget = 1/2 mu v^2 - max(0, capacity - absorbed) +// over the steps of a crush the crush done already is in neither term, so the crush of the whole hit never takes more +// than the hit brought past the buffers. the train behind the pair pushing it on through the crush brings in more: +// it's in the velocity the pair closes in with in the next step. the buffers take in in the stroke under way: the +// buffers pressed and the pair closing in. springing back they keep what they still hold, so a train pushing at a +// stand never builds up what it feeds into their damping. the buffers pressed all the way from the start of the +// contact took in nothing: vehicles put down inside each other, or knocked onto each other by a hit elsewhere, bring +// in only the kinetic energy they close in with +class contact_energy { +public: + // the step of the pair: whether its buffers press, and all the way; the closing velocity [m/s] and the force + // pressing the buffers [N] at the start of the step; how far they're pressed in [m]; the length of the step [s] + void update(bool Pressed, bool Solid, double Closing, double Force, double Compression, double Dt); + // the crush of the hit took the energy [J] + void crushed(double Work); + // the energy the crush may take now, closing in at the velocity [m/s] with the reduced mass [kg], the buffers of + // both ends taking in the capacity [J]; zero or less, the hit stays elastic + double budget(double Mass, double Closing, double Capacity) const; + // what the buffers took in in the stroke under way [J], and the crush in the contact + double absorbed() const { return m_absorbed; } + double crush_work() const { return m_crushed; } + void reset(); + +private: + double m_absorbed{0.0}; + double m_crushed{0.0}; +}; // ---- the hit past the buffers @@ -121,6 +165,9 @@ struct hit_input { // the closing velocity the pair started the step with, before the forces of the step [m/s]; without it (0), the // velocity with what the springs took off it over the step double start_velocity{0.0}; + // the energy the crush may take at most [J] (contact_energy::budget()); none left, the hit stays elastic, and the + // crush stops once it took it, the rest springs back as the buffers would have taken it + double budget{std::numeric_limits::infinity()}; }; struct hit_result { @@ -131,6 +178,8 @@ struct hit_result { double duration{0.0}; // how long that force acted over the step [s] double crush1{0.0}; // added to the end of the first vehicle [m] double crush2{0.0}; // of the second one + double work{0.0}; // the crush took [J] + bool spent{false}; // the crush stopped at the budget }; // the hit of the step: the steel takes the hit elastically up to the force the weaker end gives at, past that the end diff --git a/tests/unit/test_collisions.cpp b/tests/unit/test_collisions.cpp index c08c714dc..0aa7cb037 100644 --- a/tests/unit/test_collisions.cpp +++ b/tests/unit/test_collisions.cpp @@ -1,6 +1,7 @@ #include "stdafx.h" #include "doctest.h" #include "consist.h" +#include "drive.h" #include "vehicle.h" #include "McZapkie/collision.h" #include "McZapkie/MOVER.h" @@ -206,6 +207,49 @@ double locomotive_into_wagon(double const Speed, double const Dt) { return wagon->Couplers[end::rear].crush + locomotive->Couplers[end::front].crush; } +// the energy the crush of both ends of the pair took, by their curves [J] +double crush_energy(TMoverParameters const &First, int const FirstEnd, TMoverParameters const &Second, int const SecondEnd) { + return First.crush_curve_at(FirstEnd).energy(0.0, First.Couplers[FirstEnd].crush) + Second.crush_curve_at(SecondEnd).energy(0.0, Second.Couplers[SecondEnd].crush); +} + +// what the buffers of EN 15551 of the two ends take in [J] +double capacity_of(TMoverParameters const &First, int const FirstEnd, TMoverParameters const &Second, int const SecondEnd) { + return cl::buffer_capacity(First.Couplers[FirstEnd].collision, First.collision_kind()) + cl::buffer_capacity(Second.Couplers[SecondEnd].collision, Second.collision_kind()); +} + +// the kinetic energy of the relative motion of two vehicles closing in at the velocity [J] +double relative_energy(TMoverParameters const &First, TMoverParameters const &Second, double const Velocity) { + auto const mass{First.TotalMass * Second.TotalMass / (First.TotalMass + Second.TotalMass)}; + return 0.5 * mass * Velocity * Velocity; +} + +struct contact_outcome { + double crush{0.0}; // of both ends [m] + double energy{0.0}; // the crush took [J] + double available{0.0}; // the kinetic energy of the hit past the buffers of EN 15551 [J] + bool derailed{false}; // either vehicle +}; + +// the second vehicle runs into the rear end of the first one standing, Gap metres short of the hit (negative: the +// buffers pressed that far already), unbraked and alone on the track +contact_outcome contact(definition const &Front, definition const &Back, double const Speed, double const Gap, double const Seconds = 5.0, double const Dt = 0.01) { + consist train; + auto &front{train.add(Front)}; + auto &back{train.add(Back, 0, coupling::faux, Speed, Gap)}; + train.run(Seconds, {}, Dt); + contact_outcome result; + result.crush = front->Couplers[end::rear].crush + back->Couplers[end::front].crush; + result.energy = crush_energy(front.mover(), end::rear, back.mover(), end::front); + result.available = std::max(0.0, relative_energy(front.mover(), back.mover(), Speed * kmh) - capacity_of(front.mover(), end::rear, back.mover(), end::front)); + result.derailed = derailed(front.mover()) || derailed(back.mover()); + return result; +} + +// a coach of data/wagon.json of the given mass without its rotating masses +definition coach(double const Mass) { + return definition::load("wagon").set("Param", "M", Mass); +} + } // namespace // ---- the crush curve @@ -702,3 +746,313 @@ TEST_CASE("collisions: identical units built to EN 15227 at 36 km/h") { CHECK(held.derailed == 0); } } + +// ---- the energy of the hit: what the buffers of EN 15551 take in never crushes (found running every scenario of the +// install: calkowo_v2_kontenera, baltyk_sdm) + +TEST_CASE("collisions: the buffers of EN 15551 of an end take in their energy, of the category given or by the kind") { + CHECK(cl::buffer_capacity({}, cl::vehicle_kind::wagon) == doctest::Approx(60.0e3)); + CHECK(cl::buffer_capacity({}, cl::vehicle_kind::locomotive) == doctest::Approx(60.0e3)); + CHECK(cl::buffer_capacity({}, cl::vehicle_kind::coach) == doctest::Approx(60.0e3)); + CHECK(cl::default_buffers(cl::vehicle_kind::coach) == cl::buffer_category::passenger); + cl::end_definition category_c; + category_c.buffers = cl::buffer_category::C; + CHECK(cl::buffer_capacity(category_c, cl::vehicle_kind::wagon) == doctest::Approx(140.0e3)); + // the key of the .fiz + vehicle wagon{definition::load("wagon").set("BuffCoupl", "BufferCategory", "B")}; + REQUIRE(wagon->Couplers[end::front].collision.buffers.has_value()); + CHECK(*wagon->Couplers[end::front].collision.buffers == cl::buffer_category::B); + CHECK(cl::buffer_capacity(wagon->Couplers[end::rear].collision, wagon->collision_kind()) == doctest::Approx(100.0e3)); +} + +TEST_CASE("collisions: the budget of a contact, by what the hit brings to the buffers") { + cl::contact_energy contact; + // not touching: nothing + contact.update(false, false, 1.0, 0.0, 0.0, 0.01); + CHECK(contact.absorbed() == 0.0); + // pressed in, closing: the springs take in their work + contact.update(true, false, 1.0, 1.0e6, 0.05, 0.01); + CHECK(contact.absorbed() == doctest::Approx(10.0e3)); + // pressed all the way, they take in nothing more + contact.update(true, true, 1.0, 1.4e6, 0.3, 0.01); + CHECK(contact.absorbed() == doctest::Approx(10.0e3)); + // the kinetic energy and what the buffers took, past their capacity + CHECK(contact.budget(40.0e3, 2.0, 60.0e3) == doctest::Approx(0.5 * 40.0e3 * 4.0 + 10.0e3 - 60.0e3)); + // the springs of the .fiz taking in more than the buffers of EN 15551: no more than the kinetic energy + CHECK(contact.budget(40.0e3, 2.0, 5.0e3) == doctest::Approx(0.5 * 40.0e3 * 4.0)); + // springing back they hold no more than their springs pressed in + contact.update(true, false, -0.5, 0.2e6, 0.02, 0.01); + CHECK(contact.absorbed() == doctest::Approx(0.5 * 0.2e6 * 0.02)); + // a train pushing at a stand: the oscillation of the pair never builds the energy up + for (int i = 0; i < 6000; ++i) { + auto const closing{0.05 * std::sin(i * 0.3)}; + contact.update(true, false, closing, 0.3e6 + 1.0e6 * closing, 0.03, 0.01); + } + CHECK(contact.absorbed() < 0.5 * 0.35e6 * 0.03 + 0.35e6 * 0.05 * 0.01 * 12.0); + // parted: a new contact + contact.crushed(5.0e3); + CHECK(contact.crush_work() == doctest::Approx(5.0e3)); + contact.update(false, false, 0.0, 0.0, 0.0, 0.01); + CHECK(contact.absorbed() == 0.0); + CHECK(contact.crush_work() == 0.0); +} + +TEST_CASE("collisions: the crush of a hit takes no more than its energy past the buffers, with any step") { + // two masses at the stop of their buffers from the start (nothing taken in by the springs), the budget of the + // crush taken anew in every step of the hit, as the vehicles take it + struct masses { + double mass1; + double mass2; + }; + constexpr double capacity{120.0e3}; + constexpr double springs{1.2e6}; + for (auto const &[mass1, mass2] : {masses{80.0e3, 80.0e3}, masses{117.0e3, 84.0e3}, masses{300.0e3, 20.0e3}, masses{36.0e3, 41.6e3}}) { + auto const reduced{mass1 * mass2 / (mass1 + mass2)}; + for (auto const speed : {2.0, 5.0, 8.0, 12.0, 20.0, 30.0, 45.0, 60.0}) { + for (auto const dt : {0.001, 0.003, 0.01, 0.02, 0.03}) { + CAPTURE(mass1); + CAPTURE(mass2); + CAPTURE(speed); + CAPTURE(dt); + cl::end_state first{locomotive_curve, 0.0}; + cl::end_state second{wagon_curve, 0.0}; + auto velocity1{speed * kmh}; + auto velocity2{0.0}; + auto work{0.0}; + for (int step = 0; step < 100000; ++step) { + auto const start{velocity1 - velocity2}; + velocity1 -= springs * dt / mass1; + velocity2 += springs * dt / mass2; + auto const closing{velocity1 - velocity2}; + if (closing <= 0.0) { + break; + } + cl::hit_input input; + input.velocity = closing; + input.start_velocity = start; + input.mass1 = mass1; + input.mass2 = mass2; + input.stiffness = 1.4e8; + input.springs = springs; + input.dt = dt; + input.budget = 0.5 * reduced * start * start - capacity; + auto const hit{cl::hit(first, second, input)}; + if (false == hit.crushing) { + break; + } + CHECK(hit.work <= input.budget * (1.0 + 1e-9) + 1e-6); + work += hit.work; + first.crush += hit.crush1; + second.crush += hit.crush2; + velocity1 -= hit.impulse / mass1; + velocity2 += hit.impulse / mass2; + if (hit.velocity <= 0.0) { + break; + } + } + auto const available{std::max(0.0, 0.5 * reduced * (speed * kmh) * (speed * kmh) - capacity)}; + auto const crushed{locomotive_curve.energy(0.0, first.crush) + wagon_curve.energy(0.0, second.crush)}; + CHECK(work <= available + 1.0); + CHECK(crushed <= available * 1.001 + 1.0); + if (available <= 0.0) { + CHECK(first.crush + second.crush == 0.0); + } + } + } + } +} + +TEST_CASE("collisions: the crush of two vehicles takes no more than the energy of the hit past the buffers, with any step") { + model_setting const physical{global_settings::collision_model::physical}; + flag_setting const crashdamage{Global.crash_damage, true}; + struct pair_of { + char const *name; + definition front; + definition back; + }; + for (auto const &[name, front, back] : {pair_of{"EU07 into EU07", definition::fiz("4e_v1/4e"), definition::fiz("4e_v1/4e")}, + pair_of{"SU45 into SM42", definition::fiz("sm42_v1/6d1"), definition::fiz("su45_v2/301d")}, + pair_of{"EU07 into a wagon", definition::fiz("430w_v2/430w"), definition::fiz("4e_v1/4e")}}) { + for (auto const speed : {2.0, 5.0, 10.0, 15.0, 20.0, 30.0, 45.0, 60.0}) { + for (auto const dt : {0.001, 0.01, 0.03}) { + CAPTURE(name); + CAPTURE(speed); + CAPTURE(dt); + auto const result{contact(front, back, speed, 0.5, 4.0, dt)}; + // the explicit steps of the springs before the stop rate what they took in a little off. at 0.03 s, 0.5 m a + // step at 60 km/h, the legacy movement of the vehicles past the stop gives the hit energy of its own (the + // crush goes past what it is at 0.01 s and 0.001 s): the budget doesn't bind there, every step of the hit + // is within it (the test of the hit of two masses above), the same crush as before the budget + CHECK(result.energy <= result.available * (dt > 0.02 ? 1.15 : 1.05) + 5.0e3); + if (result.available <= 0.0) { + CHECK(result.crush == 0.0); + } + } + } + } +} + +TEST_CASE("collisions: two locomotives (117 t and 84 t) touching at walking speed don't crush (calkowo_v2_kontenera)") { + model_setting const physical{global_settings::collision_model::physical}; + flag_setting const crashdamage{Global.crash_damage, true}; + auto const su45{definition::fiz("su45_v2/301d")}; + auto const sm42{definition::fiz("sm42_v1/6d1")}; + { + vehicle check{su45}; + CHECK(check->TotalMass == doctest::Approx(117.0e3).epsilon(0.02)); + } + for (auto const physics : {false, true}) { + CAPTURE(physics); + flag_setting const couplerphysics{Global.coupler_physics, physics}; + flag_setting const everywhere{Global.coupler_physics_all, physics}; + for (auto const speed : {5.0, 5.8, 6.0}) { + CAPTURE(speed); + // a hit from a metre away + { + auto const result{contact(sm42, su45, speed, 1.0)}; + CHECK(result.crush == 0.0); + CHECK_FALSE(result.derailed); + } + // put down by the scenery with the buffers pressed in, up to past their stop, closing in: the log of the run + // had 5.8 km/h at the stop, both ends crushed, deeper hit after hit until both overrode + for (auto const overlap : {0.1, 0.4, 1.0}) { + CAPTURE(overlap); + auto const result{contact(sm42, su45, speed, -overlap, 10.0)}; + CHECK(result.crush == 0.0); + CHECK_FALSE(result.derailed); + } + } + } +} + +TEST_CASE("collisions: two coaches (36 t and 41.6 t) at 8 km/h don't crush, nor run into a wall (baltyk_sdm)") { + model_setting const physical{global_settings::collision_model::physical}; + flag_setting const crashdamage{Global.crash_damage, true}; + auto const light{coach(32.0e3)}; + auto const heavy{coach(37.6e3)}; + { + vehicle check{light}; + MESSAGE("the coaches: ", check->TotalMass / 1000.0, " t and ", vehicle{heavy}->TotalMass / 1000.0, " t"); + } + for (auto const speed : {5.0, 8.0}) { + CAPTURE(speed); + // a hit from a metre away, and one with the buffers pressed all the way already, as the coach knocked onto the + // other one by the hit of the train on the vehicle derailed at the end of the track + for (auto const gap : {1.0, -0.25}) { + CAPTURE(gap); + auto const result{contact(light, heavy, speed, gap)}; + CHECK(result.crush == 0.0); + CHECK_FALSE(result.derailed); + } + } + // the vehicle derailed the legacy way is a wall that the coach runs into + for (auto const gap : {1.0, -0.25}) { + CAPTURE(gap); + consist train; + auto &wall{train.add(light)}; + wall->Derail(END_OF_TRACK); + REQUIRE(derailed(wall.mover())); + auto &running{train.add(heavy, 0, coupling::faux, 5.0, gap)}; + train.run(5.0); + CHECK(wall->Couplers[end::rear].crush + running->Couplers[end::front].crush == 0.0); + CHECK(running->CollisionDerail == NONE); + } +} + +TEST_CASE("collisions: a locomotive pushing a standing braked train at full power doesn't crush") { + model_setting const physical{global_settings::collision_model::physical}; + flag_setting const crashdamage{Global.crash_damage, true}; + test::track const line{0.15, 0.0, test::catenary_3kv}; + // the diesel-electric keeps its full power at a stand (an electric one trips its overload relays there) + for (auto const *path : {"su45_v2/301d"}) { + CAPTURE(path); + consist train; + // four loaded coal wagons, braked in emergency once their brakes are charged + constexpr int wagons{4}; + for (int i = 0; i < wagons; ++i) { + add_mixed(train, 0, consist::full_coupling, 0.0, 0.0).hold_brake_pipe(5.0); + } + train.run(90.0, line); + for (int i = 0; i < wagons; ++i) { + train[i].hold_brake_pipe(0.0); + } + train.run(30.0, line); + // the locomotive behind them, touching, driven forwards into them at full power + auto &locomotive{train.add(definition::fiz(path), 1, coupling::faux, 0.0, 0.0)}; + test::driven const driver{locomotive.mover(), locomotive.mover(), locomotive.mover(), [&train](double const Seconds, test::track const &Track) { train.run(Seconds, Track); }}; + REQUIRE(test::start_up(driver, line)); + test::release_brakes(locomotive.mover()); + for (int i = 0; i < 3 && locomotive->DirActive <= 0; ++i) { + locomotive->DirectionForward(); + } + test::drive(driver, line, 10.0); + auto pushed{0.0}; + auto seconds{0}; // pushing with 100 kN or more + auto const push = [&]() { + auto const force{std::max(0.0, -locomotive->Couplers[end::front].CForce)}; + pushed = std::max(pushed, force); + seconds += force >= 100.0e3; + }; + // the line contactors close at the first position, once their delay and the pressure switch let them + locomotive->IncMainCtrl(1); + for (int i = 0; i < 90 && false == locomotive->StLinFlag; ++i) { + test::drive(driver, line, 1.0); + } + for (int i = 0; i < 60 && false == locomotive->IsMainCtrlMaxPowerPos(); ++i) { + locomotive->IncMainCtrl(1); + test::drive(driver, line, 1.0); + push(); + } + for (int i = 0; i < 60; ++i) { + test::drive(driver, line, 1.0); + push(); + } + MESSAGE(std::format("{}: pushing at up to {:.0f} kN, {} s at 100 kN or more, at {:.1f} km/h at the end", path, pushed / 1000.0, seconds, locomotive.speed())); + CHECK(pushed > 200.0e3); + CHECK(seconds >= 55); + for (std::size_t i = 0; i < train.size(); ++i) { + CAPTURE(i); + CHECK(crush_of(train[i].mover()) == 0.0); + CHECK_FALSE(derailed(train[i].mover())); + } + } +} + +TEST_CASE("collisions: vehicles put down with their buffers pressed in don't crush") { + model_setting const physical{global_settings::collision_model::physical}; + flag_setting const crashdamage{Global.crash_damage, true}; + for (auto const physics : {false, true}) { + CAPTURE(physics); + flag_setting const couplerphysics{Global.coupler_physics, physics}; + flag_setting const everywhere{Global.coupler_physics_all, physics}; + for (auto const overlap : {0.1, 0.3, 0.6}) { + CAPTURE(overlap); + for (auto const &[front, back] : {std::pair{definition::fiz("sm42_v1/6d1"), definition::fiz("su45_v2/301d")}, std::pair{coach(32.0e3), coach(37.6e3)}, + std::pair{definition::fiz("3wl_v2/3wl"), definition::fiz("4e_v1/4e")}}) { + auto const result{contact(front, back, 0.0, -overlap, 10.0)}; + CHECK(result.crush == 0.0); + CHECK_FALSE(result.derailed); + } + } + } +} + +TEST_CASE("collisions: the monitor goes by the energy of the hit as well") { + flag_setting const crashdamage{Global.crash_damage, true}; + model_setting const monitor{global_settings::collision_model::monitor}; + // a hit the buffers take in: the monitor tells of nothing + for (auto const gap : {1.0, -0.4}) { + CAPTURE(gap); + consist train; + train.add(definition::fiz("sm42_v1/6d1")); + train.add(definition::fiz("su45_v2/301d"), 0, coupling::faux, 5.8, gap); + train.run(5.0); + CHECK(train[0]->crush_reports + train[1]->crush_reports == 0); + } + // one past them it does + consist train; + train.add(definition::fiz("4e_v1/4e")); + train.add(definition::fiz("4e_v1/4e"), 0, coupling::faux, 25.0, 20.0); + train.run(10.0); + CHECK(train[0]->crush_reports + train[1]->crush_reports == 1); +}