diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h
index 63783419b..8c612cca0 100644
--- a/McZapkie/MOVER.h
+++ b/McZapkie/MOVER.h
@@ -16,6 +16,7 @@ http://mozilla.org/MPL/2.0/.
#include "hamulce.h"
#include "couplerphysics.h"
#include "derailment.h"
+#include "collision.h"
#include "scripting/ladderlogic.h"
#include "vehicle/odometerreading.h"
@@ -176,6 +177,10 @@ enum DerailReason
COLLISION = 5, // powód wykolejenia: zderzenie z innym pojazdem
/// Trailing through a switch with a movable nose set the other way.
TRAILED_SWITCH = 6, // powód wykolejenia: rozprucie rozjazdu z ruchomym dziobem
+ /// Climbed onto the vehicle it hit, its end crushed (physics.derailment new).
+ COLLISION_OVERRIDE = 7, // najechanie
+ /// Pushed off the track by the force pressing its end (physics.derailment new).
+ COLLISION_PUSHED_OFF = 8, // wyciśnięcie z toru
};
/*wagi prawdopodobienstwa dla funkcji FuzzyLogic*/
@@ -1288,6 +1293,15 @@ struct TCoupling
// and the state of the coupling with the vehicle at this end, which that vehicle keeps alike on its own end
coupler_physics::end_parameters physics;
coupler_physics::pair_state physics_state;
+ // the collisions past the buffers (physics.derailment new), see collision.h: the optional keys of this end, the depth
+ // it's crushed to for good [m], and the hit of the pair in the step: whether either vehicle handled it already, its
+ // peak force [N] and how long that acted [s]
+ collision::end_definition collision;
+ double crush{0.0};
+ double closing{0.0}; // the velocity the buffers of the pair closed in at, at the start of the step [m/s]
+ bool hit_handled{false};
+ double hit_force{0.0};
+ double hit_duration{0.0};
int sounds{0}; // sounds emitted by the coupling devices
bool Render = false; /*ABu: czy rysowac jak zaczepiony sprzeg*/
@@ -2444,6 +2458,23 @@ class TMoverParameters
// how the vehicle left the rails by the criteria of derailment.h: upright, or on its side. none for any other
// derailment, which stops it as the legacy model does
derailment::outcome Derailed{derailment::outcome::none};
+ // physics.derailment new: what the vehicle is for the defaults of its crush and its buffers, the crush curve and the
+ // height of the buffers of an end, and how long the push of its ends held its wheels over the climbing limit
+ collision::vehicle_kind collision_kind() const;
+ collision::crush_curve crush_curve_at(int End) const;
+ double buffer_height_at(int End) const;
+ collision::pushed_monitor pushed_off_monitor;
+ // how the vehicle left the rails in a collision (physics.derailment new): overriding or pushed off, NONE otherwise
+ DerailReason CollisionDerail{NONE};
+ // physics.derailment new: the hit of the end on the stop of its buffers, harder than the steel takes, crushes the
+ // weaker end and may let one vehicle climb onto the other one, see collision.h. returns: whether it did, and the
+ // velocities took the hit; the legacy collision takes the rest
+ bool crush_hit(int End, double dt);
+ // the force pressing the ends pushes the vehicle off the track (physics.derailment new)
+ void check_pushed_off(double dt);
+ // the vehicle leaves the rails in a collision, the reason told in the log, and slides on as the criteria of
+ // derailment.h have it
+ void derail_in_collision(DerailReason Reason, std::string const &Why);
bool DirectionForward();
bool DirectionBackward(void); /*! kierunek ruchu*/
bool EIMDirectionChangeAllow(void) const;
diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp
index 2fa19e184..00a4b775c 100644
--- a/McZapkie/Mover.cpp
+++ b/McZapkie/Mover.cpp
@@ -1338,6 +1338,12 @@ void TMoverParameters::CollisionDetect(int const End, double const dt)
auto const &othercoupler{othervehicle->Couplers[otherend]};
auto const physical{coupler_physics_at(End)};
+ // physics.derailment new: past the buffers the ends crush (collision.h); a hit the steel takes stays the legacy one
+ auto const crushing{Global.derailment == global_settings::derailment_model::physical};
+ if (crushing && (thiscoupler.hit_handled || crush_hit(End, dt)))
+ {
+ return;
+ }
if (physical && false == coupler_stop_hit(End, *othervehicle, otherend))
{
return;
@@ -1373,7 +1379,8 @@ void TMoverParameters::CollisionDetect(int const End, double const dt)
}
}
- if (Global.crash_damage)
+ // the new model crushes the ends and derails the vehicles by the crush, see crush_hit()
+ if (Global.crash_damage && false == crushing)
{
// the physical model breaks the coupling and crushes the buffers by the force of the hit (coupler_stop_hit())
if (false == physical && -thiscoupler.Dist >= thiscoupler.DmaxB && FuzzyLogic(std::abs(CCF), 5.0 * (thiscoupler.FmaxC + 1.0), p_coupldmg))
@@ -1409,13 +1416,15 @@ void TMoverParameters::CollisionDetect(int const End, double const dt)
// integration of the next step (ComputeMovement()) would carry a quarter of an acceleration spike over into the
// velocity once more, and the vehicles would bounce off the stop faster than they hit it
auto const accelerationspike{physical ? 0.0 : 1.0};
- if (false == TestFlag(DamageFlag, dtrain_out))
+ // derailed the legacy way a vehicle is a wall, off the rails by the new criteria it slides on and takes the hit
+ auto const wall = [crushing](TMoverParameters const &Vehicle) { return TestFlag(Vehicle.DamageFlag, dtrain_out) && (false == crushing || Vehicle.Derailed == derailment::outcome::none); };
+ if (false == wall(*this))
{
auto const accelerationchange{(velocity - V) / dt};
AccS += accelerationchange * accelerationspike;
V = velocity;
}
- if (false == TestFlag(othervehicle->DamageFlag, dtrain_out))
+ if (false == wall(*othervehicle))
{
auto const othervehiclenewvelocity{othervehiclevelocity * otherdirection}; // back along the other vehicle's axis
auto const othervehicleaccelerationchange{(othervehiclenewvelocity - othervehicle->V) / dt};
@@ -1465,6 +1474,278 @@ void TMoverParameters::damage_coupler(int const End)
WriteLog("Bad driving: " + Name + " broke a coupler");
}
+// *************************************************************************************************
+// physics.derailment new: the collisions past the buffers, see collision.h
+// *************************************************************************************************
+collision::vehicle_kind TMoverParameters::collision_kind() const
+{
+ auto const powered{EngineType != TEngineType::None && EngineType != TEngineType::Dumb};
+ auto const unit{(TrainType & (dt_EZT | dt_DMU)) != 0};
+ auto const passengers{std::any_of(LoadAttributes.begin(), LoadAttributes.end(), [](auto const &Load) { return ToLower(Load.name).starts_with("passenger"); })};
+ // a coach heats its compartments; the crash zone is a matter of each end (crush_of())
+ return collision::kind_of(powered, unit || passengers || (false == powered && HeatingPower > 0.0), false);
+}
+
+collision::crush_curve TMoverParameters::crush_curve_at(int const End) const
+{
+ return collision::crush_of(Couplers[End].collision, collision_kind());
+}
+
+double TMoverParameters::buffer_height_at(int const End) const
+{
+ auto const loadshare{MaxLoad > 0.f ? LoadAmount / MaxLoad : 0.0};
+ return collision::buffer_height(Couplers[End].collision, collision_kind(), loadshare);
+}
+
+void TMoverParameters::derail_in_collision(DerailReason const Reason, std::string const &Why)
+{
+ if (false == SetFlag(DamageFlag, dtrain_out))
+ {
+ return;
+ }
+ // upright, it slides on the sleepers and keeps passing the forces on
+ Derailed = derailment::outcome::off_rails;
+ CollisionDerail = Reason;
+ EventFlag = true;
+ MainSwitch(false, range_t::local);
+ if (Vel < 5.0)
+ {
+ // HACK: prevent permanent axle spin in static vehicle after a collision
+ nrot = 0.0;
+ SlippingWheels = false;
+ }
+ // the brake hoses tear: the brake pipe vents and the rest of the train brakes in emergency
+ if (std::any_of(std::begin(Couplers), std::end(Couplers), [](auto const &Coupler) { return (Coupler.CouplingFlag & coupling::brakehose) == coupling::brakehose; }))
+ {
+ AlarmChainFlag = true;
+ }
+ derailment_monitor.reset();
+ pushed_off_monitor.reset();
+ ErrorLog(std::format("Bad driving: {} derailed in a collision, {}: {}", Name, Reason == COLLISION_OVERRIDE ? "overriding" : "pushed off the track", Why));
+}
+
+namespace
+{
+
+char const *end_name(int const End)
+{
+ return End == end::front ? "front" : "rear";
+}
+
+// the force pressing the end in, of the coupling or of the buffers touching [N]: forwards at the front pulls
+double compression_at(TCoupling const &Coupler, int const End)
+{
+ return std::max(0.0, (End == end::front ? -1.0 : 1.0) * Coupler.CForce);
+}
+
+// the steel parts of both ends in series [N/m]
+double steel_stiffness(TCoupling const &First, TCoupling const &Second)
+{
+ auto const first{std::max(First.physics.link_stiffness, 1.0)};
+ auto const second{std::max(Second.physics.link_stiffness, 1.0)};
+ return first * second / (first + second);
+}
+
+// the hit of the pair in the step, for the push off the track: the peak force and how long it acted
+void record_hit(TCoupling &Coupler, double const Force, double const Duration)
+{
+ Coupler.hit_force = std::max(Coupler.hit_force, Force);
+ Coupler.hit_duration = std::max(Coupler.hit_duration, Duration);
+}
+
+} // namespace
+
+bool TMoverParameters::crush_hit(int const End, double const dt)
+{
+ auto &thiscoupler{Couplers[End]};
+ auto *othervehicle{Neighbours[End].vehicle->MoverParameters};
+ auto const otherend{Neighbours[End].vehicle_end};
+ auto &othercoupler{othervehicle->Couplers[otherend]};
+
+ auto const physical{coupler_physics_at(End)};
+ // the buffers pressed all the way, not the draw gear pulled all the way, and the pair still closing in
+ auto const buffers{physical ? thiscoupler.physics_state.stop < 0 : thiscoupler.Dist < 0.0};
+ auto const closing{-CouplerVelocityDifference(End, *othervehicle, otherend) * DirF(End)};
+ if (false == Global.crash_damage || false == buffers || closing <= 0.0)
+ {
+ return false;
+ }
+ // a vehicle derailed the legacy way (the end of the track, say) stays where it is, whatever hits it
+ auto const wall{TestFlag(DamageFlag, dtrain_out) && Derailed == derailment::outcome::none};
+ auto const otherwall{TestFlag(othervehicle->DamageFlag, dtrain_out) && othervehicle->Derailed == derailment::outcome::none};
+ if (wall && otherwall)
+ {
+ return false;
+ }
+ constexpr double immovable{1e12};
+ auto const mass{wall ? immovable : TotalMass};
+ auto const othermass{otherwall ? immovable : othervehicle->TotalMass};
+ auto const loss{physical ? coupler_physics::solid_hit_loss(thiscoupler.physics, othercoupler.physics) : (thiscoupler.beta + othercoupler.beta) / 2.0};
+
+ auto const firstcurve{crush_curve_at(End)};
+ auto const secondcurve{othervehicle->crush_curve_at(otherend)};
+ auto const stiffness{steel_stiffness(thiscoupler, othercoupler)};
+ auto const springs{compression_at(thiscoupler, End)};
+ collision::hit_input input;
+ input.velocity = closing;
+ input.mass1 = mass;
+ input.mass2 = othermass;
+ input.restitution = std::sqrt(std::max(0.0, 1.0 - loss));
+ input.stiffness = stiffness;
+ input.springs = springs;
+ input.dt = dt;
+ input.start_velocity = thiscoupler.closing;
+ auto const hit{collision::hit({firstcurve, thiscoupler.crush}, {secondcurve, othercoupler.crush}, input)};
+ if (false == hit.crushing)
+ {
+ // 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))};
+ record_hit(thiscoupler, peak, duration);
+ record_hit(othercoupler, peak, duration);
+ return false;
+ }
+ // the other vehicle doesn't take it again in this step
+ thiscoupler.hit_handled = true;
+ othercoupler.hit_handled = true;
+
+ // the hit at the force the weaker end gives at pushes the vehicles apart: the velocities alone change, along this
+ // vehicle's axis as in CollisionDetect()
+ auto const otherdirection{DirPatch(End, otherend)};
+ auto const apart{End == end::front ? -1.0 : 1.0};
+ if (false == wall)
+ {
+ V += apart * hit.impulse / mass;
+ }
+ if (false == otherwall)
+ {
+ othervehicle->V -= apart * hit.impulse / othermass * otherdirection;
+ }
+ record_hit(thiscoupler, hit.force, hit.duration);
+ record_hit(othercoupler, hit.force, hit.duration);
+
+ auto const crushed = [&](TMoverParameters const &Vehicle, int const VehicleEnd, double const Before, double const After) {
+ if (Before < collision::crush_mark && After >= collision::crush_mark)
+ {
+ ErrorLog(std::format("Bad driving: {} and {} collided at {:.0f} km/h, the {} end of {} crushed", Name, othervehicle->Name, closing * 3.6, end_name(VehicleEnd), Vehicle.Name));
+ }
+ };
+ auto const before{thiscoupler.crush};
+ auto const otherbefore{othercoupler.crush};
+ thiscoupler.crush += hit.crush1;
+ othercoupler.crush += hit.crush2;
+ crushed(*this, End, before, thiscoupler.crush);
+ crushed(*othervehicle, otherend, otherbefore, othercoupler.crush);
+
+ // the crushed headstock tears off the draw gear, with the brake hose: both parts of the train brake in emergency.
+ // the coupler of an end not coupled is gone as well, it doesn't couple any more
+ if (thiscoupler.CouplingFlag != coupling::faux && std::max(thiscoupler.crush, othercoupler.crush) >= collision::draw_gear_crush)
+ {
+ auto const hose{(thiscoupler.CouplingFlag & coupling::brakehose) == coupling::brakehose};
+ damage_coupler(End);
+ if (hose)
+ {
+ othervehicle->AlarmChainFlag = true;
+ }
+ }
+ else if (thiscoupler.CouplingFlag == coupling::faux)
+ {
+ if (thiscoupler.crush >= collision::draw_gear_crush && false == thiscoupler.broken)
+ {
+ damage_coupler(End);
+ }
+ if (othercoupler.crush >= collision::draw_gear_crush && false == othercoupler.broken)
+ {
+ othervehicle->damage_coupler(otherend);
+ }
+ }
+
+ // the end crushed deep enough lets one vehicle climb onto the other one
+ collision::override_input climbing;
+ climbing.crush1 = thiscoupler.crush;
+ climbing.crush2 = othercoupler.crush;
+ climbing.mass1 = TotalMass - Mred;
+ climbing.mass2 = othervehicle->TotalMass - othervehicle->Mred;
+ climbing.height1 = buffer_height_at(End);
+ climbing.height2 = othervehicle->buffer_height_at(otherend);
+ climbing.anticlimbers = collision::anticlimber_of(thiscoupler.collision, collision_kind()) && collision::anticlimber_of(othercoupler.collision, othervehicle->collision_kind());
+ climbing.zone1 = firstcurve.densify;
+ climbing.zone2 = secondcurve.densify;
+ auto const climbed{collision::override_of(climbing)};
+ auto const why = [&](TMoverParameters const &Climber, TMoverParameters const &Other) {
+ return std::format("{} rode up onto {} at {:.0f} km/h, the ends crushed by {:.2f} m", Climber.Name, Other.Name, closing * 3.6, thiscoupler.crush + othercoupler.crush);
+ };
+ if (climbed.first)
+ {
+ derail_in_collision(COLLISION_OVERRIDE, why(*this, *othervehicle));
+ }
+ if (climbed.second)
+ {
+ othervehicle->derail_in_collision(COLLISION_OVERRIDE, why(*othervehicle, *this));
+ }
+ return true;
+}
+
+void TMoverParameters::check_pushed_off(double const dt)
+{
+ if (Global.derailment != global_settings::derailment_model::physical || false == Global.crash_damage || TestFlag(DamageFlag, dtrain_out) || false == TestFlag(CategoryFlag, 1) ||
+ DebugModeFlag)
+ {
+ pushed_off_monitor.reset();
+ return;
+ }
+ collision::pushed_vehicle vehicle;
+ vehicle.mass = TotalMass - Mred;
+ vehicle.axles = NAxles > 0 ? NAxles : 4;
+ vehicle.length = Dim.L;
+ vehicle.pivots = BDist;
+ vehicle.axle_base = ADist > 0.0 ? ADist : BDist;
+
+ auto holds{false};
+ auto duration{0.0};
+ auto worst{0.0};
+ auto worstforce{0.0};
+ auto worstend{static_cast(end::front)};
+ for (int end = end::front; end <= end::rear; ++end)
+ {
+ auto const *other{Neighbours[end].vehicle};
+ if (other == nullptr)
+ {
+ continue;
+ }
+ auto const &coupler{Couplers[end]};
+ collision::pushed_end pushed;
+ pushed.other_length = other->MoverParameters->Dim.L;
+ pushed.crush = coupler.crush;
+ // the steady push of the springs over the step, or the peak of a hit for as long as it lasted
+ for (auto const &[force, time] : {std::pair{compression_at(coupler, end), dt}, std::pair{coupler.hit_force, coupler.hit_duration}})
+ {
+ pushed.force = force;
+ auto const ratio{collision::pushed_ratio(vehicle, pushed, RunningShape.R)};
+ if (ratio < derailment::climbing_limit)
+ {
+ continue;
+ }
+ holds = true;
+ duration = std::max(duration, time);
+ if (ratio > worst)
+ {
+ worst = ratio;
+ worstforce = force;
+ worstend = end;
+ }
+ }
+ }
+ if (pushed_off_monitor.update(holds, duration))
+ {
+ derail_in_collision(COLLISION_PUSHED_OFF, std::format("{:.0f} kN pressing its {} end in, at {:.0f} km/h, curve radius {:.0f} m: Y/Q {:.2f}", worstforce / 1000.0, end_name(worstend), Vel,
+ std::abs(RunningShape.R), std::min(worst, 99.0)));
+ }
+}
+
namespace
{
@@ -1726,6 +2007,7 @@ double TMoverParameters::ComputeMovement(double dt, double dt1, const TTrackShap
for (int b = 0; b < 2; b++)
if (Couplers[b].CheckCollision)
CollisionDetect(b, dt); // zmienia niejawnie AccS, V !!!
+ check_pushed_off(dt);
} // liczone dL, predkosc i przyspieszenie
@@ -1788,7 +2070,8 @@ double TMoverParameters::FastComputeMovement(double dt, const TTrackShape & /*Sh
// simple mode skips calculation of vertical acceleration
AccVert = 0.0;
- if (true == TestFlag(DamageFlag, dtrain_out) && Vel < 1.0)
+ // a vehicle derailed by the physical criteria slides on with the friction of ComputeTotalForce()
+ if (true == TestFlag(DamageFlag, dtrain_out) && Derailed == derailment::outcome::none && Vel < 1.0)
{
V = 0.0;
AccS = 0.0;
@@ -1801,6 +2084,7 @@ double TMoverParameters::FastComputeMovement(double dt, const TTrackShape & /*Sh
for (int b = 0; b < 2; b++)
if (Couplers[b].CheckCollision)
CollisionDetect(b, dt); // zmienia niejawnie AccS, V !!!
+ check_pushed_off(dt);
} // liczone dL, predkosc i przyspieszenie
auto const d{(EngineType == TEngineType::WheelsDriven ? dL * CabActive : // na chwile dla testu
@@ -5456,8 +5740,15 @@ void TMoverParameters::ComputeTotalForce(double dt)
// doliczenie sił z innych pojazdów
for (int end = end::front; end <= end::rear; ++end)
{
+ // a new step: the hit of the pair not taken by either vehicle yet
+ Couplers[end].hit_handled = false;
+ Couplers[end].hit_force = 0.0;
+ Couplers[end].hit_duration = 0.0;
+ Couplers[end].closing = 0.0;
if (Neighbours[end].vehicle != nullptr)
{
+ // the velocities before the step, the forces of all the vehicles come before their movement
+ 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;
}
@@ -5674,7 +5965,10 @@ double TMoverParameters::CouplerForce(int const End, double dt)
auto const initialdistance{Neighbours[End].distance}; // odległość od sprzęgu sąsiada
auto const distancedelta{(End == end::front ? othervehiclemove - dMoveLen : dMoveLen - othervehiclemove)};
- auto const newdistance{initialdistance + 10.0 * distancedelta};
+ // the ends crushed for good (physics.derailment new, see collision.h) let the buffers meet that much closer
+ auto const crushed{thiscoupler.crush + othercoupler.crush};
+ auto const rawdistance{initialdistance + 10.0 * distancedelta};
+ auto const newdistance{rawdistance < 0.0 ? std::min(0.0, rawdistance + crushed) : rawdistance};
auto const dV{CouplerVelocityDifference(End, *othervehicle, otherend)};
auto const absdV{std::abs(dV)};
@@ -5819,6 +6113,11 @@ void TMoverParameters::automatic_coupling(int const End, TMoverParameters &Other
{
return;
}
+ // a coupler torn off by the crush of its end (physics.derailment new) doesn't catch
+ if (Global.derailment == global_settings::derailment_model::physical && (thiscoupler.broken || othercoupler.broken))
+ {
+ return;
+ }
// sprzeganie wagonow z samoczynnymi sprzegami
auto couplingtype{thiscoupler.AutomaticCouplingFlag & othercoupler.AutomaticCouplingFlag};
// potentially exclude incompatible control coupling
@@ -5898,7 +6197,7 @@ double TMoverParameters::coupler_force_physical(int const End, double const dt)
auto const previousgap{state.gap};
auto const previousdrawgear{state.draw_gear.deflection};
- auto const result{coupler_physics::update_pair(thiscoupler.physics, othercoupler.physics, state, {gap, gaprate, coupled, adapter, Global.crash_damage})};
+ auto const result{coupler_physics::update_pair(thiscoupler.physics, othercoupler.physics, state, {gap, gaprate, coupled, adapter, Global.crash_damage, thiscoupler.crush + othercoupler.crush})};
SetFlag(thiscoupler.sounds, coupler_sounds({gap, previousgap, gaprate, state.draw_gear.deflection, previousdrawgear, coupled, adapter, Vel > 1.0}));
@@ -5952,7 +6251,8 @@ bool TMoverParameters::coupler_stop_hit(int const End, TMoverParameters &Other,
auto const hit{coupler_physics::impact(thiscoupler.physics, othercoupler.physics, state, closing, TotalMass, Other.TotalMass)};
// the other vehicle keeps the state of the pair alike
othercoupler.physics_state.elongation = state.elongation;
- if (hit.broken || hit.crushed)
+ // with physics.derailment new the crush of the ends past the buffers is crush_hit()'s
+ if (hit.broken || (hit.crushed && Global.derailment != global_settings::derailment_model::physical))
{
damage_coupler(End);
}
@@ -11425,6 +11725,45 @@ void scale_strength(coupler_physics::link_strength &Link, double const Factor)
Link.ultimate *= Factor;
}
+// the optional keys of the collisions past the buffers (physics.derailment new, see collision.h), what's not given
+// comes from the kind of the vehicle:
+// FcrushPeak, FcrushPlateau [kN] the force the end starts to crush at, and keeps crushing at
+// CrushOnset, CrushDensify, CrushLimit [m] the crush the peak drops at, it packs down past, it can't go past
+// CrushHardening [1/m] the growth of the force past CrushDensify, to the plateau for each metre
+// CrashZone [yes/no] built to EN 15227: energy absorbers and anticlimbers; AntiClimber [yes/no] on its own
+// BufferHeight [m] of the empty vehicle above the rail
+// BufferCategory [A/B/C/L/P] the buffers of EN 15551 (P those of the coaches) in place of kB and DmaxB
+collision::end_definition collision_definition(std::string const &Line)
+{
+ collision::end_definition definition;
+ auto const number = [&Line](std::string const &Key, double const Scale) -> std::optional {
+ auto value{0.0};
+ if (extract_value(value, Key, Line, "") && value >= 0.0)
+ {
+ return value * Scale;
+ }
+ return std::nullopt;
+ };
+ auto const flag = [&Line](std::string const &Key) -> std::optional {
+ auto value{false};
+ if (extract_value(value, Key, Line, ""))
+ {
+ return value;
+ }
+ return std::nullopt;
+ };
+ definition.peak = number("FcrushPeak", 1000.0);
+ definition.plateau = number("FcrushPlateau", 1000.0);
+ definition.onset = number("CrushOnset", 1.0);
+ definition.densify = number("CrushDensify", 1.0);
+ definition.hardening = number("CrushHardening", 1.0);
+ definition.limit = number("CrushLimit", 1.0);
+ definition.crash_zone = flag("CrashZone");
+ definition.anticlimber = flag("AntiClimber");
+ definition.buffer_height = number("BufferHeight", 1.0);
+ return definition;
+}
+
} // namespace
void TMoverParameters::LoadFIZ_BuffCoupl(std::string const &line, int const Index)
@@ -11505,7 +11844,22 @@ void TMoverParameters::LoadFIZ_BuffCoupl(std::string const &line, int const Inde
thiscoupler->DmaxB = std::max(0.05, thiscoupler->FmaxC / thiscoupler->SpringKB);
}
+ // the buffers of a category of EN 15551 in place of the springs given
+ auto const categoryname{extract_value("BufferCategory", line)};
+ auto const category{categoryname.empty() ? std::nullopt : collision::buffer_category_of(categoryname.front())};
+ auto const buffered{thiscoupler->CouplerType != TCouplerType::NoCoupler && thiscoupler->CouplerType != TCouplerType::Bare && thiscoupler->CouplerType != TCouplerType::Articulated};
+ if (category && buffered)
+ {
+ auto const linear{collision::buffer_curve(*category, 0.0)};
+ thiscoupler->SpringKB = linear.stiffness;
+ thiscoupler->DmaxB = linear.travel;
+ }
thiscoupler->physics = coupler_physics_parameters(*thiscoupler, line);
+ if (category && buffered)
+ {
+ thiscoupler->physics.buffers = collision::buffer_curve(*category, thiscoupler->physics.buffers.progression);
+ }
+ thiscoupler->collision = collision_definition(line);
if (Index == 0)
{
diff --git a/tests/unit/test_collisions.cpp b/tests/unit/test_collisions.cpp
new file mode 100644
index 000000000..c689c709f
--- /dev/null
+++ b/tests/unit/test_collisions.cpp
@@ -0,0 +1,648 @@
+#include "stdafx.h"
+#include "doctest.h"
+#include "consist.h"
+#include "vehicle.h"
+#include "McZapkie/collision.h"
+#include "McZapkie/MOVER.h"
+#include "utilities/Globals.h"
+
+#include
+#include
+#include
+#include
+
+// collisions past the buffers with physics.derailment new (McZapkie/collision.h): the crush of the ends, the override,
+// the push off the track, and what they do in trains of real vehicles
+
+using test::consist;
+using test::definition;
+using test::vehicle;
+namespace cl = collision;
+
+namespace {
+
+constexpr double kmh{1.0 / 3.6};
+
+// the given model of derailment for the test, restored after it
+class model_setting {
+public:
+ explicit model_setting(global_settings::derailment_model Model) : m_previous{Global.derailment} { Global.derailment = Model; }
+ ~model_setting() { Global.derailment = m_previous; }
+ model_setting(model_setting const &) = delete;
+ model_setting &operator=(model_setting const &) = delete;
+
+private:
+ global_settings::derailment_model m_previous;
+};
+
+// the given setting of a global flag for the test, restored after it
+class flag_setting {
+public:
+ flag_setting(bool &Flag, bool Enabled) : m_flag{Flag}, m_previous{Flag} { m_flag = Enabled; }
+ ~flag_setting() { m_flag = m_previous; }
+ flag_setting(flag_setting const &) = delete;
+ flag_setting &operator=(flag_setting const &) = delete;
+
+private:
+ bool &m_flag;
+ bool m_previous;
+};
+
+// ---- the hit of two masses on the stop of their buffers, the way the vehicles take it
+
+struct pair_outcome {
+ double crush1{0.0};
+ double crush2{0.0};
+ double velocity1{0.0};
+ double velocity2{0.0};
+ double peak{0.0}; // force
+};
+
+// the first mass runs at the velocity into the second one standing, their buffers pressed all the way already; the
+// springs carry the given force while pressed, stepped by Dt as the vehicles are: the springs over the step first, the
+// hit after them
+pair_outcome hit_pair(cl::end_state First, cl::end_state Second, double const Mass1, double const Mass2, double const Velocity, double const Springs, double const Dt) {
+ pair_outcome result;
+ auto velocity1{Velocity};
+ auto velocity2{0.0};
+ for (int step = 0; step < 1000000; ++step) {
+ 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.mass1 = Mass1;
+ input.mass2 = Mass2;
+ input.restitution = 0.2;
+ input.stiffness = 1.4e8;
+ input.springs = Springs;
+ input.dt = Dt;
+ auto const hit{cl::hit(First, Second, input)};
+ if (false == hit.crushing) {
+ // elastic: bounced apart with the restitution
+ auto const reduced{Mass1 * Mass2 / (Mass1 + Mass2)};
+ auto const impulse{reduced * closing * (1.0 + input.restitution)};
+ velocity1 -= impulse / Mass1;
+ velocity2 += impulse / Mass2;
+ break;
+ }
+ velocity1 -= hit.impulse / Mass1;
+ velocity2 += hit.impulse / Mass2;
+ First.crush += hit.crush1;
+ Second.crush += hit.crush2;
+ result.peak = std::max(result.peak, hit.force);
+ if (hit.velocity <= 0.0) {
+ break;
+ }
+ }
+ result.crush1 = First.crush;
+ result.crush2 = Second.crush;
+ result.velocity1 = velocity1;
+ result.velocity2 = velocity2;
+ return result;
+}
+
+cl::crush_curve const wagon_curve{cl::default_crush(cl::vehicle_kind::wagon)};
+cl::crush_curve const locomotive_curve{cl::default_crush(cl::vehicle_kind::locomotive)};
+
+// ---- trains of real vehicles
+
+bool derailed(TMoverParameters const &Vehicle) {
+ return TestFlag(Vehicle.DamageFlag, dtrain_out);
+}
+
+double crush_of(TMoverParameters const &Vehicle) {
+ return std::max(Vehicle.Couplers[end::front].crush, Vehicle.Couplers[end::rear].crush);
+}
+
+// a mixed freight train as the PoC has it: every third wagon from the given one an empty two-axle 3WL (10.5 t), the
+// rest 430W coal wagons loaded full (80 t)
+definition mixed_wagon(int const Index) {
+ return definition::fiz(Index % 3 == 1 ? "3wl_v2/3wl" : "430w_v2/430w");
+}
+
+vehicle &add_mixed(consist &Train, int const Index, int const Coupling, double const Velocity, double const Gap) {
+ auto &wagon{Train.add(mixed_wagon(Index), 0, Coupling, Velocity, Gap)};
+ if (Index % 3 != 1) {
+ wagon->AssignLoad("coal", 59.5f);
+ wagon->ComputeMass();
+ }
+ return wagon;
+}
+
+struct crash_outcome {
+ int derailed{0};
+ int overridden{0};
+ int pushed{0};
+ int crushed{0}; // ends crushed past the mark
+ int broken{0}; // couplings torn off
+ double crush{0.0}; // the deepest crush of an end
+ bool locomotive{false}; // it derailed
+ std::string list; // what happened to which vehicle, for the messages
+};
+
+// the PoC of G14: a locomotive (EU07, 80 t) with 12 mixed wagons running into the rear of 8 mixed wagons standing
+// braked, in emergency, on a straight track or in a curve
+crash_outcome freight_crash(double const Speed, double const Radius, double const Dt = 0.01) {
+ test::track track;
+ track.radius = Radius;
+ consist train;
+ // the standing train, the wagon it's hit at last; its brakes charged, then in emergency
+ constexpr int standing{8};
+ for (int i = standing - 1; i >= 0; --i) {
+ add_mixed(train, i, consist::full_coupling, 0.0, 0.0).hold_brake_pipe(5.0);
+ }
+ train.run(90.0, track);
+ for (int i = 0; i < standing; ++i) {
+ train[i].hold_brake_pipe(0.0);
+ }
+ train.run(30.0, track);
+ // the train running into it, its locomotive leading, a metre short of the hit
+ train.add(definition::fiz("4e_v1/4e"), 0, coupling::faux, Speed, 1.0);
+ for (int i = 0; i < 12; ++i) {
+ add_mixed(train, i, consist::full_coupling, Speed, 0.0);
+ }
+ train.run(12.0, track, Dt);
+
+ crash_outcome result;
+ for (std::size_t i = 0; i < train.size(); ++i) {
+ auto const &vehicle{train[i].mover()};
+ auto const name{i < standing ? std::format("standing {}", standing - 1 - i) : i == standing ? std::string{"locomotive"} : std::format("running {}", i - standing - 1)};
+ for (auto const end : {end::front, end::rear}) {
+ if (vehicle.Couplers[end].crush >= cl::crush_mark) {
+ ++result.crushed;
+ }
+ }
+ result.crush = std::max(result.crush, crush_of(vehicle));
+ if (derailed(vehicle)) {
+ ++result.derailed;
+ result.overridden += vehicle.CollisionDerail == COLLISION_OVERRIDE;
+ result.pushed += vehicle.CollisionDerail == COLLISION_PUSHED_OFF;
+ result.locomotive = result.locomotive || i == standing;
+ result.list += std::format("{} ({:.1f} t) {}; ", name, vehicle.TotalMass / 1000.0, vehicle.CollisionDerail == COLLISION_OVERRIDE ? "overrode" : "pushed off");
+ }
+ else if (crush_of(vehicle) >= cl::crush_mark) {
+ result.list += std::format("{} crushed {:.2f} m; ", name, crush_of(vehicle));
+ }
+ }
+ for (std::size_t i = 0; i + 1 < train.size(); ++i) {
+ result.broken += train[i]->Couplers[end::rear].broken;
+ }
+ return result;
+}
+
+// a locomotive running into a loaded coal wagon standing free, both on the rails: the crush of the pair after the hit
+double locomotive_into_wagon(double const Speed, double const Dt) {
+ consist train;
+ auto &wagon{train.add(definition::fiz("430w_v2/430w"))};
+ wagon->AssignLoad("coal", 59.5f);
+ wagon->ComputeMass();
+ // behind the wagon, running forwards into its rear end
+ auto &locomotive{train.add(definition::fiz("4e_v1/4e"), 0, coupling::faux, Speed, 0.5)};
+ train.run(3.0, {}, Dt);
+ return wagon->Couplers[end::rear].crush + locomotive->Couplers[end::front].crush;
+}
+
+} // namespace
+
+// ---- the crush curve
+
+TEST_CASE("collisions: the crush curve, a peak, a plateau, packing down, and the limit") {
+ auto const &curve{wagon_curve};
+ CHECK(curve.force(0.0) == doctest::Approx(3.0e6));
+ CHECK(curve.force(0.049) == doctest::Approx(3.0e6));
+ CHECK(curve.force(0.05) == doctest::Approx(2.0e6));
+ CHECK(curve.force(0.79) == doctest::Approx(2.0e6));
+ // 2 / m past 0.8 m: a metre further it's three times the plateau
+ CHECK(curve.force(1.3) == doctest::Approx(2.0e6 * (1.0 + 2.0 * 0.5)));
+ CHECK_FALSE(std::isfinite(curve.force(1.5)));
+ // the energy is the area under it
+ CHECK(curve.energy(0.0, 0.05) == doctest::Approx(3.0e6 * 0.05));
+ CHECK(curve.energy(0.0, 0.8) == doctest::Approx(3.0e6 * 0.05 + 2.0e6 * 0.75));
+ CHECK(curve.energy(0.8, 1.3) == doctest::Approx(0.5 * (2.0e6 + 4.0e6) * 0.5));
+ CHECK(curve.energy(0.0, 0.3) + curve.energy(0.3, 1.0) == doctest::Approx(curve.energy(0.0, 1.0)));
+ // past the limit there's nothing more to crush
+ CHECK(curve.energy(1.5, 3.0) == 0.0);
+
+ // the kinds of vehicles; a crash zone gives a steady force with no peak
+ CHECK(locomotive_curve.peak > wagon_curve.peak);
+ auto const zone{cl::default_crush(cl::vehicle_kind::crash_zone)};
+ CHECK(zone.force(0.0) == doctest::Approx(zone.force(0.5)));
+ // the keys of the .fiz override the defaults of the kind
+ cl::end_definition given;
+ given.plateau = 1.0e6;
+ given.crash_zone = true;
+ auto const own{cl::crush_of(given, cl::vehicle_kind::coach)};
+ CHECK(own.plateau == doctest::Approx(1.0e6));
+ CHECK(own.densify == doctest::Approx(zone.densify));
+ CHECK(cl::anticlimber_of(given, cl::vehicle_kind::coach));
+ CHECK_FALSE(cl::anticlimber_of({}, cl::vehicle_kind::locomotive));
+}
+
+TEST_CASE("collisions: the buffers of EN 15551, their stroke and energy") {
+ for (auto const category : {cl::buffer_category::A, cl::buffer_category::B, cl::buffer_category::C, cl::buffer_category::L, cl::buffer_category::passenger}) {
+ auto const rated{cl::rating(category)};
+ for (auto const progression : {0.0, 0.5, 0.9}) {
+ auto const curve{cl::buffer_curve(category, progression)};
+ CHECK(curve.travel == doctest::Approx(rated.stroke));
+ // two buffers side by side
+ CHECK(curve.energy(curve.travel) == doctest::Approx(2.0 * rated.energy));
+ }
+ }
+ CHECK(cl::rating(cl::buffer_category::A).energy < cl::rating(cl::buffer_category::C).energy);
+ CHECK(cl::buffer_category_of('C') == cl::buffer_category::C);
+ CHECK_FALSE(cl::buffer_category_of('X').has_value());
+ // the triangle of category A: 2 x 30 kJ over 105 mm is about 1.1 MN at the end of the stroke, 570 kN a buffer
+ CHECK(cl::buffer_curve(cl::buffer_category::A, 0.0).end_force() == doctest::Approx(4.0 * 30.0e3 / 0.105));
+}
+
+// ---- the hit
+
+TEST_CASE("collisions: a hit the steel takes stays elastic, a harder one crushes the weaker end") {
+ cl::hit_input input;
+ input.mass1 = 80.0e3;
+ input.mass2 = 80.0e3;
+ input.stiffness = 1.4e8;
+ input.dt = 0.01;
+ // 40 t reduced: the steel takes 3 MN at 3 / sqrt(1.4e8 * 4e4) = 1.27 m/s
+ input.velocity = 1.0;
+ CHECK_FALSE(cl::hit({wagon_curve, 0.0}, {wagon_curve, 0.0}, input).crushing);
+ input.velocity = 5.0;
+ auto const hit{cl::hit({locomotive_curve, 0.0}, {wagon_curve, 0.0}, input)};
+ REQUIRE(hit.crushing);
+ // the wagon gives, the locomotive doesn't; at its peak force for the whole step
+ CHECK(hit.crush1 == 0.0);
+ CHECK(hit.crush2 > 0.0);
+ CHECK(hit.force == doctest::Approx(3.0e6));
+ CHECK(hit.impulse == doctest::Approx(3.0e6 * 0.01));
+ CHECK(hit.duration == doctest::Approx(0.01));
+ CHECK(hit.velocity == doctest::Approx(5.0 - 3.0e6 * 0.01 / 40.0e3));
+ // with the springs carrying a part of the force, the hit adds the rest
+ input.springs = 1.0e6;
+ auto const sprung{cl::hit({locomotive_curve, 0.0}, {wagon_curve, 0.0}, input)};
+ CHECK(sprung.impulse == doctest::Approx(2.0e6 * 0.01));
+ // two alike crush alike
+ input.springs = 0.0;
+ auto const alike{cl::hit({wagon_curve, 0.0}, {wagon_curve, 0.0}, input)};
+ CHECK(alike.crush1 == doctest::Approx(alike.crush2));
+ // a wall: the end crushed all the way doesn't give, the other one does
+ auto const wall{cl::hit({wagon_curve, 2.0}, {wagon_curve, 0.0}, input)};
+ CHECK(wall.crush1 == 0.0);
+ CHECK(wall.crush2 > 0.0);
+ // both crushed all the way: rigid, the legacy exchange
+ CHECK_FALSE(cl::hit({wagon_curve, 2.0}, {wagon_curve, 2.0}, input).crushing);
+}
+
+TEST_CASE("collisions: the crush doesn't depend on the step, and it takes the energy of the hit") {
+ struct pair {
+ char const *name;
+ double mass1;
+ double mass2;
+ double velocity;
+ double springs;
+ };
+ for (auto const &[name, mass1, mass2, velocity, springs] : {
+ pair{"80 t into 80 t at 25 km/h", 80.0e3, 80.0e3, 25.0 * kmh, 0.0},
+ pair{"80 t into 80 t at 40 km/h, the springs at 1 MN", 80.0e3, 80.0e3, 40.0 * kmh, 1.0e6},
+ pair{"80 t into 12 t at 40 km/h", 80.0e3, 12.0e3, 40.0 * kmh, 0.5e6},
+ pair{"300 t into 80 t at 35 km/h, past the densification", 300.0e3, 80.0e3, 35.0 * kmh, 0.0},
+ }) {
+ CAPTURE(name);
+ auto const fine{hit_pair({locomotive_curve, 0.0}, {wagon_curve, 0.0}, mass1, mass2, velocity, springs, 0.001)};
+ REQUIRE(fine.crush2 > 0.05);
+ CHECK(fine.crush1 == 0.0);
+ for (auto const dt : {0.005, 0.01, 0.05}) {
+ CAPTURE(dt);
+ auto const coarse{hit_pair({locomotive_curve, 0.0}, {wagon_curve, 0.0}, mass1, mass2, velocity, springs, dt)};
+ CHECK(coarse.crush2 == doctest::Approx(fine.crush2).epsilon(0.02));
+ // the momentum stays with the pair
+ CHECK(mass1 * coarse.velocity1 + mass2 * coarse.velocity2 == doctest::Approx(mass1 * velocity));
+ // never past the force the wagon gives at: its peak, or what it packed down to
+ CHECK(coarse.peak <= std::max(wagon_curve.peak, wagon_curve.force(coarse.crush2)) * (1.0 + 1e-9));
+ }
+ if (springs == 0.0) {
+ // the energy of the relative motion goes into the crush, but for what the steel takes back
+ auto const reduced{mass1 * mass2 / (mass1 + mass2)};
+ auto const kinetic{0.5 * reduced * velocity * velocity};
+ auto const crushforce{wagon_curve.force(fine.crush2)};
+ auto const elastic{crushforce * crushforce / (2.0 * 1.4e8)};
+ CHECK(wagon_curve.energy(0.0, fine.crush2) == doctest::Approx(kinetic - elastic).epsilon(0.01));
+ }
+ }
+}
+
+TEST_CASE("collisions: the crush of two pairs of masses at the speeds of the table") {
+ // 80 t on 80 t: the steel takes the hit up to 1.3 m/s past the buffers; at 25 km/h the two ends alike crush by
+ // 0.4 m between them, the 0.97 MJ at 3 MN to 0.05 m each and at 2 MN past it (the buffers aren't here)
+ auto const slow{hit_pair({wagon_curve, 0.0}, {wagon_curve, 0.0}, 80.0e3, 80.0e3, 1.2, 0.0, 0.01)};
+ CHECK(slow.crush1 + slow.crush2 == 0.0);
+ auto const hard{hit_pair({wagon_curve, 0.0}, {wagon_curve, 0.0}, 80.0e3, 80.0e3, 25.0 * kmh, 0.0, 0.01)};
+ CHECK(hard.crush1 + hard.crush2 > 0.2);
+ CHECK(hard.crush1 + hard.crush2 < 0.5);
+}
+
+// ---- override
+
+TEST_CASE("collisions: the lighter or the higher vehicle climbs onto the other one, both past a deeper crush") {
+ cl::override_input input;
+ input.mass1 = 80.0e3;
+ input.mass2 = 80.0e3;
+ input.height1 = 1.040;
+ input.height2 = 1.040;
+ SUBCASE("a shallow crush holds") {
+ input.crush1 = 0.3;
+ auto const result{cl::override_of(input)};
+ CHECK_FALSE(result.first);
+ CHECK_FALSE(result.second);
+ }
+ SUBCASE("alike: past 0.35 m they hold, past 0.6 m both climb") {
+ input.crush1 = 0.4;
+ CHECK_FALSE(cl::override_of(input).first);
+ input.crush2 = 0.25;
+ auto const result{cl::override_of(input)};
+ CHECK(result.first);
+ CHECK(result.second);
+ }
+ SUBCASE("the lighter one climbs past 0.35 m") {
+ input.mass1 = 12.0e3;
+ input.crush1 = 0.4;
+ auto const result{cl::override_of(input)};
+ CHECK(result.first);
+ CHECK_FALSE(result.second);
+ }
+ SUBCASE("40 mm higher, the higher one climbs") {
+ input.height2 = 1.000;
+ input.crush1 = 0.2;
+ input.crush2 = 0.2;
+ auto const result{cl::override_of(input)};
+ CHECK(result.first);
+ CHECK_FALSE(result.second);
+ }
+ SUBCASE("interlocked anticlimbers hold them until the crush zone of an end is used up") {
+ input.anticlimbers = true;
+ input.zone1 = 0.8;
+ input.zone2 = 0.8;
+ input.height2 = 1.000;
+ input.mass1 = 40.0e3;
+ input.crush1 = 0.5;
+ input.crush2 = 0.5;
+ auto const held{cl::override_of(input)};
+ CHECK_FALSE(held.first);
+ CHECK_FALSE(held.second);
+ input.crush1 = 0.85;
+ auto const spent{cl::override_of(input)};
+ CHECK(spent.first);
+ CHECK(spent.second);
+ }
+}
+
+// ---- pushed off the track
+
+TEST_CASE("collisions: an empty two-axle wagon between loaded ones in a curve of 150 m is pushed off past the safe push of EN 15566") {
+ // the vehicles of data/fiz: the 3WL empty (10.5 t, two axles 6 m apart), between 430W coal wagons
+ vehicle empty{definition::fiz("3wl_v2/3wl")};
+ vehicle loaded{definition::fiz("430w_v2/430w")};
+ cl::pushed_vehicle wagon;
+ wagon.mass = empty->TotalMass - empty->Mred;
+ wagon.axles = empty->NAxles;
+ wagon.length = empty->Dim.L;
+ wagon.pivots = empty->BDist;
+ wagon.axle_base = empty->ADist > 0.0 ? empty->ADist : empty->BDist;
+ REQUIRE(wagon.axles == 2);
+ cl::pushed_end end;
+ end.other_length = loaded->Dim.L;
+ auto const ratio = [&](double const Force, double const Radius) {
+ end.force = Force;
+ return cl::pushed_ratio(wagon, end, Radius);
+ };
+ // 250 kN on the pair of buffers in a curve of 150 m is safe (EN 15566, 4.2); 600 kN pushes it off
+ CHECK(ratio(250.0e3, 150.0) < 1.2);
+ CHECK(ratio(250.0e3, 150.0) > 0.5);
+ CHECK(ratio(600.0e3, 150.0) > 1.2);
+ // the wider the curve the more it takes; on a straight track the empty wagon lifts off past 1.5 MN: the force
+ // pitching it takes the load off its leading wheels
+ CHECK(ratio(600.0e3, 500.0) < ratio(600.0e3, 150.0));
+ CHECK(ratio(600.0e3, 0.0) < 1.2);
+ CHECK(ratio(1.5e6, 0.0) < 1.2);
+ CHECK(ratio(2.5e6, 0.0) > 1.2);
+ // a crushed end no longer holds the ends in line
+ end.crush = 0.5;
+ CHECK(ratio(1.5e6, 0.0) > 1.2);
+ end.crush = 0.0;
+
+ // the loaded wagon, four axles, takes it
+ cl::pushed_vehicle heavy;
+ heavy.mass = loaded->TotalMass - loaded->Mred + 59.5e3;
+ heavy.axles = loaded->NAxles;
+ heavy.length = loaded->Dim.L;
+ heavy.pivots = loaded->BDist;
+ heavy.axle_base = loaded->ADist;
+ end.force = 600.0e3;
+ CHECK(cl::pushed_ratio(heavy, end, 150.0) < 0.5);
+
+ // it has to hold for 0.05 s, however long the steps
+ cl::pushed_monitor monitor;
+ CHECK_FALSE(monitor.update(true, 0.03));
+ CHECK_FALSE(monitor.update(false, 0.01));
+ CHECK_FALSE(monitor.update(true, 0.03));
+ CHECK(monitor.update(true, 0.03));
+}
+
+// ---- the model of eu07.ini
+
+TEST_CASE("collisions: the legacy rule with physics.derailment old, the crush with new") {
+ flag_setting const crashdamage{Global.crash_damage, true};
+ struct collision {
+ char const *name;
+ char const *front;
+ char const *back;
+ double speed; // of the back one, running into the front one standing
+ bool frontout_old;
+ bool backout_old;
+ bool frontout_new;
+ bool backout_new;
+ };
+ // the legacy rule: over 15 km/h weighed by the ratio of the masses. the crush: the light wagon is crushed deep and
+ // climbs onto the locomotive at 60 km/h; two locomotives at 25 km/h crush their ends 0.1-0.3 m and stay on the rails
+ for (auto const &[name, front, back, speed, frontout_old, backout_old, frontout_new, backout_new] : {
+ collision{"locomotive into a wagon at 60", "3wl_v2/3wl", "4e_v1/4e", 60.0, true, false, true, false},
+ collision{"locomotive into a locomotive at 25", "4e_v1/4e", "4e_v1/4e", 25.0, true, true, false, false},
+ collision{"locomotive into a locomotive at 50", "4e_v1/4e", "4e_v1/4e", 50.0, true, true, true, true},
+ }) {
+ CAPTURE(name);
+ for (auto const model : {global_settings::derailment_model::old, global_settings::derailment_model::physical}) {
+ auto const physical{model == global_settings::derailment_model::physical};
+ CAPTURE(physical);
+ model_setting const setting{model};
+ consist train;
+ train.add(definition::fiz(front));
+ train.add(definition::fiz(back), 0, coupling::faux, speed, 20.0);
+ train.run(10.0);
+ CHECK(derailed(train[0].mover()) == (physical ? frontout_new : frontout_old));
+ CHECK(derailed(train[1].mover()) == (physical ? backout_new : backout_old));
+ auto const crush{train[0]->Couplers[end::rear].crush + train[1]->Couplers[end::front].crush};
+ if (physical) {
+ MESSAGE(name, ": crush ", crush, " m");
+ CHECK(crush > 0.05);
+ }
+ else {
+ CHECK(crush == 0.0);
+ }
+ }
+ }
+}
+
+TEST_CASE("collisions: the crush of a locomotive hitting a wagon doesn't depend on the step, nor on the model of the coupling") {
+ model_setting const physical{global_settings::derailment_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};
+ auto const fine{locomotive_into_wagon(30.0, 0.001)};
+ auto const game{locomotive_into_wagon(30.0, 0.01)};
+ auto const coarse{locomotive_into_wagon(30.0, 0.05)};
+ MESSAGE(std::format("30 km/h, couplerphysics {}: crush {:.3f} / {:.3f} / {:.3f} m with dt 0.001 / 0.01 / 0.05 s", physics, fine, game, coarse));
+ REQUIRE(fine > 0.05);
+ // the step of the game, and a step five times as long, which the buffers past the crush aren't made for
+ CHECK(game == doctest::Approx(fine).epsilon(0.1));
+ CHECK(coarse == doctest::Approx(fine).epsilon(0.5));
+ }
+ // the models of the coupling differ in their buffers, not past them
+ {
+ auto const legacy{locomotive_into_wagon(30.0, 0.01)};
+ flag_setting const couplerphysics{Global.coupler_physics, true};
+ flag_setting const everywhere{Global.coupler_physics_all, true};
+ CHECK(locomotive_into_wagon(30.0, 0.01) == doctest::Approx(legacy).epsilon(0.3));
+ }
+ // slow enough, the buffers take it all
+ CHECK(locomotive_into_wagon(5.0, 0.01) == 0.0);
+}
+
+TEST_CASE("collisions: automatic couplers hit too fast don't catch, the ends crush; torn off, they don't couple again") {
+ model_setting const physical{global_settings::derailment_model::physical};
+ flag_setting const crashdamage{Global.crash_damage, true};
+ auto const automatic{definition::load("wagon").set("BuffCoupl", "CType", "Automatic").set("BuffCoupl", "AutomaticFlag", coupling::coupler | coupling::brakehose)};
+ for (auto const speed : {20.0, 30.0, 45.0}) {
+ CAPTURE(speed);
+ vehicle moving{automatic, speed, "moving"};
+ vehicle standing{automatic, 0.0, "standing"};
+ test::pair pair{moving, end::front, standing, end::rear, 1.0};
+ auto coupled{false};
+ auto fast{false}; // coupled while closing in faster than the couplers catch
+ for (int i = 0; i < 1000; ++i) {
+ auto const closing{moving->CollisionVelocity(end::front, standing.mover(), end::rear)};
+ pair.step();
+ fast = fast || (pair.coupled() && false == coupled && closing > TMoverParameters::SafeCollisionVelocity);
+ coupled = coupled || pair.coupled();
+ }
+ auto const crush{moving->Couplers[end::front].crush + standing->Couplers[end::rear].crush};
+ MESSAGE(speed, " km/h: crush ", crush, " m, derailed ", derailed(moving.mover()), " ", derailed(standing.mover()), ", coupled later ", coupled);
+ CHECK_FALSE(fast);
+ CHECK(crush > 0.0);
+ // crushed past the draw gear, the coupler is gone; short of it, the wagons may roll together again slowly
+ // after the hit, and couple then
+ if (std::max(moving->Couplers[end::front].crush, standing->Couplers[end::rear].crush) >= cl::draw_gear_crush) {
+ CHECK_FALSE(coupled);
+ CHECK((moving->Couplers[end::front].broken || standing->Couplers[end::rear].broken));
+ }
+ // the two alike: neither climbs onto the other until the crush is deep enough for both to
+ CHECK(derailed(moving.mover()) == derailed(standing.mover()));
+ CHECK(derailed(moving.mover()) == (crush > cl::override_both));
+ }
+}
+
+TEST_CASE("collisions: a vehicle off the rails by the new model stays in the chain of forces") {
+ model_setting const physical{global_settings::derailment_model::physical};
+ flag_setting const crashdamage{Global.crash_damage, true};
+ consist train;
+ auto &wagon{train.add(definition::fiz("430w_v2/430w"))};
+ auto &locomotive{train.add(definition::fiz("4e_v1/4e"), 0, coupling::faux, 10.0, 0.5)};
+ wagon->derail_in_collision(COLLISION_PUSHED_OFF, "for the test");
+ REQUIRE(derailed(wagon.mover()));
+ auto const before{wagon->TotalMass * wagon->V + locomotive->TotalMass * locomotive->V};
+ // the hit is over within a second, the friction of the derailed wagon slows the pair down over it
+ train.run(1.0);
+ auto const after{wagon->TotalMass * wagon->V + locomotive->TotalMass * locomotive->V};
+ // pushed along, not a wall the locomotive bounces off
+ CHECK(wagon.speed() > 1.0);
+ CHECK(locomotive.speed() > 0.0);
+ CHECK(after < before);
+ CHECK(after > 0.5 * before);
+ // and it slides on with the friction of the ballast, stopping before long
+ train.run(10.0);
+ CHECK(std::abs(wagon.speed()) < 0.1);
+}
+
+TEST_CASE("collisions: a freight train running into a standing one, the cases of the PoC of G14") {
+ model_setting const physical{global_settings::derailment_model::physical};
+ flag_setting const crashdamage{Global.crash_damage, true};
+ struct crash {
+ double speed;
+ double radius;
+ };
+ for (auto const &[speed, radius] : {crash{10.0, 0.0}, crash{15.0, 0.0}, crash{15.0, 190.0}, crash{25.0, 0.0}, crash{25.0, 190.0}, crash{40.0, 0.0}, crash{40.0, 190.0}}) {
+ CAPTURE(speed);
+ CAPTURE(radius);
+ auto const result{freight_crash(speed, radius)};
+ MESSAGE(std::format("{:.0f} km/h, R {:.0f}: {} derailed ({} overrode, {} pushed off), {} ends crushed (deepest {:.2f} m), {} couplings broken: {}", speed, radius, result.derailed,
+ result.overridden, result.pushed, result.crushed, result.crush, result.broken, result.list));
+ if (speed <= 10.0) {
+ // the buffers take it
+ CHECK(result.derailed == 0);
+ CHECK(result.crushed == 0);
+ }
+ }
+}
+
+TEST_CASE("collisions: identical units built to EN 15227 at 36 km/h") {
+ model_setting const physical{global_settings::derailment_model::physical};
+ flag_setting const crashdamage{Global.crash_damage, true};
+ // two coaches of data/wagon.json (42.2 t) a unit, their ends crash zones with anticlimbers, unbraked
+ auto const unit = [](bool const Anticlimbers, double const Height) {
+ return definition::load("wagon").set("BuffCoupl", "CrashZone", "Yes").set("BuffCoupl", "AntiClimber", Anticlimbers ? "Yes" : "No").set("BuffCoupl", "BufferHeight", Height);
+ };
+ auto const crash = [&](bool const Anticlimbers, double const Speed, double const Offset) {
+ consist train;
+ train.add(unit(Anticlimbers, 1.040));
+ train.add(unit(Anticlimbers, 1.040));
+ train.add(unit(Anticlimbers, 1.040 + Offset), 0, coupling::faux, Speed, 1.0);
+ train.add(unit(Anticlimbers, 1.040 + Offset), 0, consist::full_coupling, Speed);
+ train.run(6.0);
+ struct {
+ double crush;
+ double inner;
+ int derailed;
+ bool higher;
+ } result{train[1]->Couplers[end::rear].crush + train[2]->Couplers[end::front].crush, std::max(crush_of(train[0].mover()), crush_of(train[3].mover())), 0,
+ derailed(train[2].mover())};
+ for (std::size_t i = 0; i < train.size(); ++i) {
+ result.derailed += derailed(train[i].mover());
+ }
+ return result;
+ };
+ SUBCASE("scenario 1: the crush stays in the zones, nothing climbs") {
+ auto const result{crash(true, 36.0, 0.04)};
+ MESSAGE("36 km/h, 40 mm offset: crush ", result.crush, " m, the far ends ", result.inner, " m");
+ CHECK(result.crush > 0.2);
+ CHECK(result.crush < 2.0 * 0.8);
+ CHECK(result.derailed == 0);
+ }
+ SUBCASE("the same units without the anticlimbers override") {
+ auto const result{crash(false, 36.0, 0.0)};
+ CHECK(result.derailed >= 2);
+ }
+ SUBCASE("slower, without the anticlimbers, the higher one climbs") {
+ auto const result{crash(false, 32.0, 0.04)};
+ MESSAGE("32 km/h, 40 mm offset: crush ", result.crush, " m");
+ CHECK(result.crush > cl::override_crush);
+ CHECK(result.crush < cl::override_both);
+ CHECK(result.higher);
+ CHECK(result.derailed == 1);
+ auto const held{crash(true, 32.0, 0.04)};
+ CHECK(held.derailed == 0);
+ }
+}
diff --git a/tests/unit/test_controls.cpp b/tests/unit/test_controls.cpp
index 4e5255ff4..f42ecd731 100644
--- a/tests/unit/test_controls.cpp
+++ b/tests/unit/test_controls.cpp
@@ -4,6 +4,7 @@
#include "drive.h"
#include "McZapkie/hamulce.h"
+#include "utilities/Globals.h"
#include "utilities/utilities.h"
// characterisation of the controls a driver and a crew use besides driving and braking: doors, direction and
@@ -495,6 +496,14 @@ TEST_CASE("controls: a freight wagon loaded and braked") {
}
TEST_CASE("controls: a locomotive running into a standing wagon") {
+ // the legacy collisions of physics.derailment old: derailed, the wagon is a wall (test_collisions.cpp has the new)
+ struct legacy {
+ global_settings::derailment_model previous{Global.derailment};
+ legacy() { Global.derailment = global_settings::derailment_model::old; }
+ ~legacy() { Global.derailment = previous; }
+ legacy(legacy const &) = delete;
+ legacy &operator=(legacy const &) = delete;
+ } const model;
for (auto const speed : {5.0, 15.0, 40.0}) {
CAPTURE(speed);
scenario scenario{std::format("controls/collision/{} kmh", static_cast(speed)), motion()};
diff --git a/tests/unit/test_couplers.cpp b/tests/unit/test_couplers.cpp
index bbb1c0dd9..c6a3f4a5d 100644
--- a/tests/unit/test_couplers.cpp
+++ b/tests/unit/test_couplers.cpp
@@ -49,6 +49,19 @@ private:
bool m_previous{Global.crash_damage};
};
+// the legacy collisions of physics.derailment old for the test, restored after it: the rule of the safe closing speed
+// weighed by the ratio of the masses (the crush of the ends of the new model is in test_collisions.cpp)
+class legacy_collisions {
+public:
+ legacy_collisions() { Global.derailment = global_settings::derailment_model::old; }
+ ~legacy_collisions() { Global.derailment = m_previous; }
+ legacy_collisions(legacy_collisions const &) = delete;
+ legacy_collisions &operator=(legacy_collisions const &) = delete;
+
+private:
+ global_settings::derailment_model m_previous{Global.derailment};
+};
+
} // namespace
// issue #86: automatic couplers used to catch at any closing speed, so the vehicles never collided nor derailed
@@ -202,6 +215,7 @@ TEST_CASE("couplers: a wagon rolling slowly into a standing one couples with it"
TEST_CASE("couplers: a wagon hitting a standing one too fast doesn't couple, nor once the hit has slowed it down") {
crash_damage_setting const crashdamage{true};
+ legacy_collisions const legacy;
auto moving{automatic_wagon(30.0, "moving")};
auto standing{automatic_wagon(0.0, "standing")};
test::pair pair{moving, end::front, standing, end::rear, 1.0};
@@ -268,6 +282,7 @@ TEST_CASE("couplers: the automatic coupling locked by a hit is unlocked once the
TEST_CASE("couplers: a hit a little too fast for the couplers bounces the vehicles apart, uncoupled and on the rails") {
crash_damage_setting const crashdamage{true};
+ legacy_collisions const legacy;
auto moving{automatic_wagon(20.0, "moving")};
auto standing{automatic_wagon(0.0, "standing")};
test::pair pair{moving, end::front, standing, end::rear, 1.0};
@@ -281,6 +296,7 @@ TEST_CASE("couplers: a hit a little too fast for the couplers bounces the vehicl
TEST_CASE("couplers: a hit too fast for the couplers derails the vehicles over the limit adjusted for their mass ratio") {
crash_damage_setting const crashdamage{true};
+ legacy_collisions const legacy;
SUBCASE("equal masses: both derail") {
auto moving{automatic_wagon(30.0, "moving")};
@@ -318,6 +334,7 @@ TEST_CASE("couplers: a hit too fast for the couplers derails the vehicles over t
TEST_CASE("couplers: wagons running head-on into each other couple only up to the safe closing speed") {
crash_damage_setting const crashdamage{true};
+ legacy_collisions const legacy;
SUBCASE("7 + 7 km/h, the couplers catch") {
auto first{automatic_wagon(7.0, "first")};
diff --git a/tests/unit/test_couplers_trains.cpp b/tests/unit/test_couplers_trains.cpp
index 926964dbe..e17a8f86d 100644
--- a/tests/unit/test_couplers_trains.cpp
+++ b/tests/unit/test_couplers_trains.cpp
@@ -3,6 +3,7 @@
#include "consist.h"
#include "scenario.h"
+#include "utilities/Globals.h"
#include "utilities/utilities.h"
// characterisation of the couplers between vehicles: coupling and uncoupling by hand (Attach, DettachStatus,
@@ -14,6 +15,22 @@ using test::definition;
using test::scenario;
using namespace test::channels;
+namespace {
+
+// the given model of derailment for the test, restored after it
+class model_setting {
+public:
+ explicit model_setting(global_settings::derailment_model Model) : m_previous{Global.derailment} { Global.derailment = Model; }
+ ~model_setting() { Global.derailment = m_previous; }
+ model_setting(model_setting const &) = delete;
+ model_setting &operator=(model_setting const &) = delete;
+
+private:
+ global_settings::derailment_model m_previous;
+};
+
+} // namespace
+
TEST_CASE("couplers: coupled by hand, touching or apart, of the same kind or not") {
consist train;
auto &locomotive{train.add(definition::fiz("4e_v1/4e")).mover()};
@@ -147,7 +164,9 @@ TEST_CASE("couplers: a coupler broken, with its brake hose torn off") {
TEST_CASE("couplers: collisions fast enough to derail") {
// a locomotive running into a standing wagon from behind, and a wagon running backwards into a standing
- // locomotive; over 15 km/h, weighed by the ratio of the masses, the vehicles derail
+ // locomotive; over 15 km/h, weighed by the ratio of the masses, the vehicles derail. the legacy rule of
+ // physics.derailment old: the new model crushes the ends (test_collisions.cpp)
+ model_setting const legacy{global_settings::derailment_model::old};
struct collision {
char const *name;
char const *frontpath;