diff --git a/physics/components/body.cpp b/physics/components/body.cpp index 0bfd38cbf..a65ff1178 100644 --- a/physics/components/body.cpp +++ b/physics/components/body.cpp @@ -918,7 +918,10 @@ void physics::Vehicle::ComputeTotalForce(double dt) // the velocities before the step, the forces of all the vehicles come before their movement Couplers[end].closing = -CouplerVelocityDifference(end, *Neighbours[end].vehicle->physics, Neighbours[end].FacingEnd()) * DirF(end); Couplers[end].CForce = CouplerForce(end, dt); - FTrain += Couplers[end].CForce; + // off the track (physics.derailment new) the vehicles are apart in another direction than along the axis of + // this one: of the force between them, along it the share the gap goes by, which for ends gone past each + // other is the other way. the force as a whole stays in CForce, for the motion off the track + FTrain += Couplers[end].CForce * (Neighbours[end].off_track ? coupler_gap_change(end, 1.0, 0.0) * DirF(end) : 1.0); // 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); diff --git a/physics/components/couplers.cpp b/physics/components/couplers.cpp index 2ee5f8ec3..1633375a6 100644 --- a/physics/components/couplers.cpp +++ b/physics/components/couplers.cpp @@ -490,6 +490,19 @@ int coupler_sounds(coupler_sound_input const &Input) } // namespace +double physics::Vehicle::coupler_gap_change(VehicleEnd const End, double const Own, double const Other) const +{ + auto const &neighbour{Neighbours[End]}; + if (neighbour.off_track) + { + return neighbour.own_share * Own + neighbour.other_share * Other; + } + // the gap opens up when the front end falls behind the vehicle ahead of it, or the rear end runs ahead of the one + // behind; the other vehicle's move turned around when the vehicles face each other + auto const other{Other * DirPatch(End, neighbour.FacingEnd())}; + return End == VehicleEnd::front ? other - Own : Own - other; +} + double physics::Vehicle::coupler_force_physical(VehicleEnd const End, double const dt) { auto &thiscoupler{Couplers[End]}; @@ -498,11 +511,12 @@ double physics::Vehicle::coupler_force_physical(VehicleEnd const End, double con auto &othercoupler{othervehicle->Couplers[otherend]}; auto &state{thiscoupler.physics_state}; - // the gap as it is now: the distance found at the start of the frame, and the movement of both vehicles since - auto const othervehiclemove{othervehicle->moveLength * DirPatch(End, otherend)}; - auto const gap{Neighbours[End].distance + (End == VehicleEnd::front ? othervehiclemove - moveLength : moveLength - othervehiclemove)}; - // the gap opens up when the front end falls behind the vehicle ahead of it, or the rear end runs ahead of the one behind - auto const gaprate{CouplerVelocityDifference(End, *othervehicle, otherend) * DirF(End)}; + // the gap as it is now: the distance found at the start of the frame, and the movement of both vehicles since. + // off the track the ends of a pair may have gone past each other, where a move along the track that would close + // them on it opens the gap; taken as along the track, the draw gear unloaded while the gap grew far past its travel, + // and the coupling neither held nor broke + auto const gap{Neighbours[End].distance + coupler_gap_change(End, moveLength, othervehicle->moveLength)}; + auto const gaprate{coupler_gap_change(End, travelSpeed, othervehicle->travelSpeed)}; auto const coupled{thiscoupler.CouplingFlag != Coupling::none}; auto const adapter{thiscoupler.has_adapter() || othercoupler.has_adapter()}; @@ -554,7 +568,7 @@ bool physics::Vehicle::coupler_stop_hit(VehicleEnd const End, physics::Vehicle & auto &state{thiscoupler.physics_state}; // the draw gear went solid with the gap opening up, the buffers with it closing - auto const gaprate{CouplerVelocityDifference(End, Other, OtherEnd) * DirF(End)}; + auto const gaprate{coupler_gap_change(End, travelSpeed, Other.travelSpeed)}; auto const closing{state.stop > 0 ? gaprate : -gaprate}; if (state.stop == 0 || closing <= 0.0) { diff --git a/physics/vehicle.h b/physics/vehicle.h index 1a162d79b..ddefd28ee 100644 --- a/physics/vehicle.h +++ b/physics/vehicle.h @@ -1477,6 +1477,10 @@ class Vehicle bool coupler_physics_at(VehicleEnd const End) const; // the force of the coupling at the end by the physical model (couplerphysics in eu07.ini), see couplerphysics.h double coupler_force_physical(VehicleEnd const End, double const dt); + // how much the distance to the vehicle at the End grows [m, or m/s] when this vehicle moves (Own) and the other one + // moves (Other) along their axes: along a track the difference of the two moves; with either vehicle off the + // track (Neighbours[End].off_track) by the shares of them the ends have in the direction they are apart in + double coupler_gap_change(VehicleEnd const End, double const Own, double const Other) const; // with the physical model: whether the vehicles at the end hit the stop of their springs at the last update and // still close in on it, so that they collide; a hit hard enough breaks the coupling or crushes the buffers bool coupler_stop_hit(VehicleEnd const End, physics::Vehicle &Other, VehicleEnd const OtherEnd); diff --git a/physics/vehicletypes.h b/physics/vehicletypes.h index dc83b6d88..c8db5e270 100644 --- a/physics/vehicletypes.h +++ b/physics/vehicletypes.h @@ -812,6 +812,12 @@ struct neighbour_data TDynamicObject *vehicle{nullptr}; // detected obstacle int vehicle_end{-1}; // facing end of the obstacle float distance{10000.f}; // distance to the obstacle // NOTE: legacy value. TBD, TODO: use standard -1 instead? + // physics.derailment new, either vehicle off the track: the distance is between the ends where they are, and it + // changes with the moves of the vehicles (along their own axes, as V() and MoveLength() go) by these shares of them, + // rather than as along a track. see physics::Vehicle::coupler_gap_change() + bool off_track{false}; + double own_share{0.0}; + double other_share{0.0}; // the facing end of the obstacle there is VehicleEnd FacingEnd() const diff --git a/tests/unit/test_braking_curve.cpp b/tests/unit/test_braking_curve.cpp index 6bcf92fc7..f542ec27c 100644 --- a/tests/unit/test_braking_curve.cpp +++ b/tests/unit/test_braking_curve.cpp @@ -228,3 +228,19 @@ TEST_CASE("braking curve: a train gets the demand it got before the curve moved CHECK(braking_curve::demand(approach, &table) == doctest::Approx(expected).epsilon(1e-7)); } + +TEST_CASE("braking curve: down a gradient a train at walking pace in the last metres before its stop stops there") { + // EU07 alone down 20 per mille to the signal gr_sem (eu07_td_downgrade_w4_signal): 3.5 km/h 1.3 m short of the + // middle of the stopping window, the curve asked for next to no braking; the independent brake let off for it, the + // gradient took the locomotive again, and it crept on and braked again three times, for some 12 s + auto constexpr descent{0.196}; // [m/s2] 20 per mille + CHECK(braking_curve::stops_where_it_is(3.5, 1.3, descent)); + // past the point it aims at, it doesn't creep on half a metre at a time either + CHECK(braking_curve::stops_where_it_is(0.2, -1.2, descent)); + // faster, or further out, it follows the curve down to the point + CHECK_FALSE(braking_curve::stops_where_it_is(5.8, 2.6, descent)); + CHECK_FALSE(braking_curve::stops_where_it_is(3.5, 10.0, descent)); + // on the flat and uphill the gradient doesn't take the train when the brake lets off + CHECK_FALSE(braking_curve::stops_where_it_is(3.5, 1.3, 0.0)); + CHECK_FALSE(braking_curve::stops_where_it_is(3.5, 1.3, -0.196)); +} diff --git a/tests/unit/test_coupler_physics.cpp b/tests/unit/test_coupler_physics.cpp index 1c8cea768..520a985b4 100644 --- a/tests/unit/test_coupler_physics.cpp +++ b/tests/unit/test_coupler_physics.cpp @@ -7,6 +7,7 @@ #include #include +#include // the physical model of the couplings (physics/couplerphysics.h, couplerphysics in eu07.ini): progressive buffers and // draw gear with friction hysteresis, pressed in series, the play of the coupling, its yield and its breaking @@ -336,6 +337,63 @@ TEST_CASE("coupler physics: a coupling with play pulls only once the play is tak CHECK(front.speed() < 2.5); } +TEST_CASE("coupler physics: off the track, ends gone past each other pull the coupling taut, it holds or breaks") { + // eu07_td_off_track_buckle: the locomotive lay at rest beside the track, the coach coupled to it ran on past it along + // the rails. the gap between their ends grew with the run of the coach, but the model took the run along the track, + // as closing the gap: the draw gear unloaded, the coupling carried 0 N while the gap grew to 30 m, and it never broke + flag_setting const physics{Global.coupler_physics, true}; + flag_setting const everywhere{Global.coupler_physics_all, true}; + auto running{loose_wagon(0.05, 30.0, "running")}; + auto lying{loose_wagon(0.05, 0.0, "lying")}; + join(running, VehicleEnd::front, lying, VehicleEnd::rear); + test::pair pair{running, VehicleEnd::front, lying, VehicleEnd::rear, 0.0}; + running.hold_brake_pipe(5.0); + lying.hold_brake_pipe(5.0); + // as TDynamicObject::update_neighbours() measures the ends where they are: gone past each other, the run of the + // running wagon forwards opens the gap, the other one running after it closes it. from where the pair stands at + // the first step, the ends at one place + std::optional start; + auto const gap = [&]() { return (running->Loc().X - lying->Loc().X) - start.value_or(running->Loc().X - lying->Loc().X); }; + pair.neighbours_found = [&]() { + if (false == start.has_value()) { + start = running->Loc().X - lying->Loc().X; + } + auto const gap{(running->Loc().X - lying->Loc().X) - *start}; + auto &fromrunning{running->Neighbours[VehicleEnd::front]}; + fromrunning.distance = static_cast(gap); + fromrunning.off_track = true; + fromrunning.own_share = 1.0; + fromrunning.other_share = -1.0; + auto &fromlying{lying->Neighbours[VehicleEnd::rear]}; + fromlying.distance = static_cast(gap); + fromlying.off_track = true; + fromlying.own_share = -1.0; + fromlying.other_share = 1.0; + }; + + SUBCASE("with crash damage, the jerk breaks it") { + flag_setting const crashdamage{Global.crash_damage, true}; + pair.run(1.0); + CHECK_FALSE(pair.coupled()); + CHECK((coupler_damaged(running) || coupler_damaged(lying))); + } + SUBCASE("without it, the coupling holds: the wagon running past is held back, the gap stays within the draw gear") { + flag_setting const crashdamage{Global.crash_damage, false}; + auto pulled{false}; + auto widest{0.0}; + for (int i = 0; i < 300; ++i) { + pair.step(); + pulled = pulled || running->Couplers[VehicleEnd::front].CForce != 0.0; + widest = std::max(widest, gap()); + } + CHECK(pulled); + CHECK(pair.coupled()); + // it ran 25 m on past the other one, the coupling carrying nothing + CHECK(widest < 0.5); + CHECK(running.speed() == doctest::Approx(lying.speed()).epsilon(0.2)); + } +} + TEST_CASE("coupler physics: buffers hit at a walking pace give back less than they took") { flag_setting const physics{Global.coupler_physics, true}; flag_setting const everywhere{Global.coupler_physics_all, true}; diff --git a/tests/unit/test_utilities.cpp b/tests/unit/test_utilities.cpp index 1b12f1b9a..d9309fa46 100644 --- a/tests/unit/test_utilities.cpp +++ b/tests/unit/test_utilities.cpp @@ -7,6 +7,7 @@ #include "utilities/Globals.h" #include "utilities/utilities.h" +#include "utilities/uuid.hpp" #include #include @@ -297,6 +298,21 @@ TEST_CASE("utilities: a version 4 uuid is drawn from the random numbers of the s CHECK(generate_uuid_v4() == first); } +TEST_CASE("utilities: the uuid of a scene node takes none of the random numbers of the simulation") { + // a node added to the scenery (or a memory cell the program makes before it) left the numbers drawn after it as + // they were: the physics of a run of a seed doesn't depend on how many nodes the scenery has + Global.random_engine.seed(11); + auto const before{Global.random_engine}; // NOSONAR: a copy of the simulation's engine to compare with; nothing secret is drawn + auto const first{UID::random()}; + auto const second{UID::random()}; + CHECK(Global.random_engine == before); + CHECK(first != second); + // the version and the variant of a version 4 uuid + CHECK((first.bytes[6] >> 4) == 4); + CHECK((first.bytes[8] >> 6) == 2); + CHECK(first.to_string().size() == 36); +} + TEST_CASE("utilities: a failure is drawn only over the threshold, the likelier the less the value is over it") { debug_mode const mode{false}; for (auto draw{0}; draw < 50; ++draw) { diff --git a/tests/unit/vehicle.cpp b/tests/unit/vehicle.cpp index 67f275c9a..c90eb8d81 100644 --- a/tests/unit/vehicle.cpp +++ b/tests/unit/vehicle.cpp @@ -326,6 +326,9 @@ void pair::find_neighbours() { otherPhysics.Couplers[other.end].adapter_length); physics.Neighbours[Opposite(self.end)] = neighbour_data(); } + if (neighbours_found) { + neighbours_found(); + } } void pair::step(track const &Track, double Dt) { diff --git a/tests/unit/vehicle.h b/tests/unit/vehicle.h index efb73f137..36b076cb3 100644 --- a/tests/unit/vehicle.h +++ b/tests/unit/vehicle.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -157,6 +158,10 @@ public: CouplingSet coupling() const; bool coupled() const { return false == coupling().Empty(); } + // called after each finding of the neighbours, which it may change: stands in for the ends of vehicles off the track + // meeting where they are (TDynamicObject::update_neighbours() with physics.derailment new), not along the track + std::function neighbours_found; + private: struct placement { vehicle *owner{nullptr}; diff --git a/utilities/uuid.hpp b/utilities/uuid.hpp index f68ad49bb..462169874 100644 --- a/utilities/uuid.hpp +++ b/utilities/uuid.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "utilities/utilities.h" #include "utilities/Globals.h" @@ -14,10 +15,17 @@ public: std::array bytes; + // the uuid of each scene node, which only the editor goes by. drawn from an engine of its own: from the one of the + // simulation (Global.random_engine) each node made took two of the numbers the run's seed gives, and a node added to + // the scenery, or made by the program before it (a memory cell of the scripting interface), changed every random + // number drawn after it, the physics of the vehicles among them static UID random() { + static std::mt19937 engine(static_cast(true_random_seed())); // NOSONAR: an identifier, not anything security-relevant + static std::mutex mutex; UID u; - uint64_t a = Global.random_engine(); - uint64_t b = Global.random_engine(); + std::scoped_lock lock(mutex); + uint64_t a = engine(); + uint64_t b = engine(); for (int i = 0; i < 8; ++i) u.bytes[i] = uint8_t(a >> (i * 8) & 0xFF); for (int i = 0; i < 8; ++i) u.bytes[8 + i] = uint8_t(b >> (i * 8) & 0xFF); diff --git a/vehicle/DynObj.h b/vehicle/DynObj.h index 5e1f86745..adf70bb18 100644 --- a/vehicle/DynObj.h +++ b/vehicle/DynObj.h @@ -806,9 +806,9 @@ private: derail_motion::formation track_bed() const; glm::dvec3 track_front() const; int outward_side() const; - // the couplings of this vehicle off the track pull the vehicles coupled to it off the rails sideways, the step - // having travelled the distance - void pull_neighbours( double Distance ); + // the couplings of this vehicle off the track pull the vehicles coupled to it off the rails sideways, over the step + // of the time [s] + void pull_neighbours( double Time ); // the scraping of the vehicle off the track, until it stops, and its state to the log every second void update_off_track( double Deltatime ); diff --git a/vehicle/ai/speed.cpp b/vehicle/ai/speed.cpp index eccf943ee..a0d22367b 100644 --- a/vehicle/ai/speed.cpp +++ b/vehicle/ai/speed.cpp @@ -576,6 +576,12 @@ TController::adjust_desired_speed_for_final_approach( double const Vel ) { AccDesired = std::min( 0.0, AccPreferred ); VelDesired = min_speed( VelDesired, uphill_coasting_speed( ActualProximityDist - stopping_margin(), fAccGravity ) ); } + if( ( BrakeSystem == TBrakeSystem::Pneumatic ) + && ( braking_curve::stops_where_it_is( Vel, ActualProximityDist - stopping_margin(), fAccGravity ) ) ) { + // down a gradient at walking pace in the last metres before the point it aims at, or past it: it stops here, + // rather than lets off the brake to creep on, which lets the gradient take it again + VelDesired = 0.0; + } } void diff --git a/vehicle/brakingcurve.cpp b/vehicle/brakingcurve.cpp index 1cd1397aa..f28439ed0 100644 --- a/vehicle/brakingcurve.cpp +++ b/vehicle/brakingcurve.cpp @@ -87,4 +87,12 @@ double demand(approach const &Approach, brake_table const *Pneumatictable) { return shaped_demand(velocity, Approach.target, room + 0.1 / 25.92, Approach.gravity, *table); } +bool stops_where_it_is(double const Velocity, double const Room, double const Gravity) { + + constexpr double walking_pace{5.0}; // [km/h] + constexpr double last_metres{2.0}; // [m] + constexpr double descent{0.025}; // [m/s2] about 2.5 per mille down, as the AI takes a gradient going downhill + return (Gravity > descent) && (Velocity < walking_pace) && (Room < last_metres); +} + } // namespace braking_curve diff --git a/vehicle/brakingcurve.h b/vehicle/brakingcurve.h index 3b49597e5..ab8371827 100644 --- a/vehicle/brakingcurve.h +++ b/vehicle/brakingcurve.h @@ -57,4 +57,11 @@ bool brake_acting(double Trainbrakeposition, double Localbrakeposition); // zero, as in shunting) it is the constant deceleration over the distance left double demand(approach const &Approach, brake_table const *Pneumatictable); +// whether a train in the final approach to a stop, down a gradient, stops where it is rather than creeps on to the +// point it aims at: down to walking pace (Velocity [km/h]) within the last metres short of that point, or past it +// (Room [m], negative past it), on a descent (Gravity [m/s2], positive downhill). there the demand of the curve is +// next to nothing; the pneumatic brake let off for it lets the gradient take the train again, and it crept on and +// braked again, a release and an application at a time, for many seconds +bool stops_where_it_is(double Velocity, double Room, double Gravity); + } // namespace braking_curve diff --git a/vehicle/dynamic/consist.cpp b/vehicle/dynamic/consist.cpp index 1bde532dd..c94fd4aed 100644 --- a/vehicle/dynamic/consist.cpp +++ b/vehicle/dynamic/consist.cpp @@ -277,10 +277,11 @@ TDynamicObject::update_neighbours() { // neighbour.vehicle = coupler.Connected; // neighbour.vehicle_end = coupler.ConnectedNr; neighbour.distance = physics::Vehicle::CouplerDist( physics, coupler.Connected ); + neighbour.off_track = false; if( physics->OffTrack.active() || neighbour.vehicle->physics->OffTrack.active() ) { // physics.derailment new: off the track the bodies turn against each other, the ends meet where they are // (the distance between the middles less the half lengths would press the buffers of a bent pair) - neighbour.distance = off_track_gap( end ); + vehicle_dynamic::measure_off_track( neighbour, *this, end ); } // take into account potential adapters attached to the couplers auto const &othercoupler { neighbour.vehicle->physics->Couplers[ neighbour.vehicle_end ] }; @@ -309,7 +310,7 @@ TDynamicObject::update_neighbours() { neighbour.distance = physics::Vehicle::CouplerDist( physics, neighbour.vehicle->physics ); if( physics->OffTrack.active() || neighbour.vehicle->physics->OffTrack.active() ) { // physics.derailment new: the ends where they are, as for a coupled pair - neighbour.distance = off_track_gap( end ); + vehicle_dynamic::measure_off_track( neighbour, *this, end ); } // take into account potential adapters attached to the couplers auto const &othercoupler { neighbour.vehicle->physics->Couplers[ neighbour.vehicle_end ] }; diff --git a/vehicle/dynamic/internal.h b/vehicle/dynamic/internal.h index 99eb1e40c..40c6f734d 100644 --- a/vehicle/dynamic/internal.h +++ b/vehicle/dynamic/internal.h @@ -78,6 +78,10 @@ using off_track_clock = std::chrono::steady_clock; namespace vehicle_dynamic { double seconds_since( off_track_clock::time_point const Start ); +// physics.derailment new, either vehicle of the pair off the track: the distance between their ends where they are, and +// the shares the moves of both vehicles along their own axes have in it (physics::Vehicle::coupler_gap_change()): along +// the direction they are apart in, which for ends gone past each other is the other way than along the track +void measure_off_track( neighbour_data &Neighbour, TDynamicObject const &Self, VehicleEnd End ); } // namespace vehicle_dynamic diff --git a/vehicle/dynamic/offtrack.cpp b/vehicle/dynamic/offtrack.cpp index d5aeb6668..a1cb6bffc 100644 --- a/vehicle/dynamic/offtrack.cpp +++ b/vehicle/dynamic/offtrack.cpp @@ -181,8 +181,26 @@ std::string_view side_name( int const Side ) { return Side < 0 ? "right" : "none"; } +// the level unit vector the vehicle moves along with a positive V, its front +glm::dvec3 level_front_of( TDynamicObject const &Vehicle ) { + glm::dvec3 const front { Vehicle.VectorFront().x, 0.0, Vehicle.VectorFront().z }; + auto const length { glm::length( front ) }; + return length > 1e-6 ? front / length : glm::dvec3 { 0.0, 0.0, 1.0 }; +} + } // namespace +void vehicle_dynamic::measure_off_track( neighbour_data &Neighbour, TDynamicObject const &Self, VehicleEnd const End ) { + auto const &other { *Neighbour.vehicle }; + auto const meeting { derail_motion::meet( end_point_of( Self, End ), end_point_of( other, Neighbour.FacingEnd() ) ) }; + // pressed into each other the buffers give along the bisector of the bend, apart the coupling between the beams + auto const direction { meeting.gap < 0.0 ? meeting.push : meeting.pull }; + Neighbour.distance = static_cast( meeting.gap ); + Neighbour.off_track = true; + Neighbour.own_share = -glm::dot( direction, level_front_of( Self ) ); + Neighbour.other_share = glm::dot( direction, level_front_of( other ) ); +} + double TDynamicObject::off_track_gap( VehicleEnd const End ) const { auto const *neighbour { physics->Neighbours[ End ].vehicle }; @@ -417,7 +435,7 @@ void TDynamicObject::move_off_track( double const Distance ) { crop_off_track_terrain(); } - pull_neighbours( Distance ); + pull_neighbours( time ); // what it cost: a frame of the vehicle off the track should take well under a millisecond auto const took { vehicle_dynamic::seconds_since( started ) }; @@ -443,7 +461,7 @@ void TDynamicObject::place_off_track() { m_offtrackplaced = true; } -void TDynamicObject::pull_neighbours( double const Distance ) { +void TDynamicObject::pull_neighbours( double const Time ) { for (auto const end : VehicleEnds) { auto const &coupler { physics->Couplers[ end ] }; @@ -469,7 +487,10 @@ void TDynamicObject::pull_neighbours( double const Distance ) { auto const magnitude { glm::length( glm::dvec3 { force.x, 0.0, force.z } ) }; auto const mass { other.TotalMass() - other.Mred() }; auto const ratio { derail_motion::pull_ratio( magnitude, angle, mass, other.NAxles() ) }; - if( false == m_pulledoff[ end ].update( ratio >= derailment::climbing_limit, Distance ) ) { + // the flange climbs the rail over the distance the wheels of the vehicle pulled run along it, whatever this one + // does: lying at rest beside the track, it went on pulling the vehicle running past it, which then never got + // the distance the climb takes + if( false == m_pulledoff[ end ].update( ratio >= derailment::climbing_limit, std::abs( other.V() ) * Time ) ) { continue; } // the way the force across it goes: towards this one pulled, away from it pushed