18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 06:35:21 +01:00
Files
maszyna/physics/derailment.cpp
T
jerrrrycho da69df97b8 feat(physics): flank collisions at switches, short of the fouling point
Two vehicles on the rails beside each other at a switch, up to its
fouling point, now meet by their bodies (physics.collisions new, the
default; physics.collisions.flank no turns it off). Before, they passed
through each other there, or, on the switch itself, the vehicle on the
other way was taken for the one ahead and gave a phantom end-on hit.

The neighbour search follows the ways of a switch: the way of the
searcher's own axle, on the next switch the way joined to the track it
comes from. A vehicle on the other way is skipped unless the two are in
line end to end, their facing ends less than 2.2 m apart across so that
a pair of buffers meets (flank::EndsMeet()).

The flank contacts: the fouling zones of all rail switches are made
once (the tracks along each way up to the fouling point, 3.50 m between
the axes as found by fouling::find(), and 30 m past it); only a moving
vehicle with an axle in a zone is checked against the vehicles on the
other way of that switch. The footprints collide as for the vehicles
off the track; the side of a body crushes at 250 kN, past 0.5 m and at
an end at 2 MN, never more than stops the closing within the step. The
force slows both along their rails and loads their wheels across: a
vehicle with its end in the contact climbs at Nadal's limit held over
the 2 m window of EN 14363, a body hit along its side only at three
times the limit at once. It leaves the rails with the new derailment
cause flank_collision (14). Pairs in line end to end stay with the
collision of the ends.

Tests: tests/unit/test_flank_contact.cpp (the neighbours on a 1:9
switch, the crush forces, Skierniewice 2023, a graze at 5 km/h, steps
of 0.01 to 0.05 s); scenario cases eu07_td_switch_flank_foul,
_foul_buffers and _clear on td.scn. The coach of eu07_td_crew_refused
stood 0.6 m into the way of track 1 and is moved clear of the fouling
point.
2026-12-02 11:03:00 +01:00

359 lines
15 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "physics/derailment.h"
#include <algorithm>
#include <cmath>
#include <format>
namespace derailment {
namespace {
// a curve with a radius under this is taken for a straight track [m]
constexpr double straight{1e-3};
bool curved(double const Radius) {
return std::abs(Radius) > straight;
}
// the height the lateral force on the body acts at, with the lean of the body on its suspension [m]
double leaning_height(double const CentreOfGravity, suspension const &Suspension) {
return CentreOfGravity + Suspension.flexibility * std::max(0.0, CentreOfGravity - Suspension.roll_centre);
}
bool has(int const Flags, int const Flag) {
return (Flags & Flag) != 0;
}
// the acceleration [m/s2] of what moves with the track sideways, besides the curve: the sway of the body and the
// short waves of an uneven track
double dynamic_acceleration(track_state const &Track, double const Velocity) {
return body_vibration(Velocity) + uneven_share * Velocity * Velocity * std::abs(Track.uneven_curvature);
}
// the sideways force of the couplings of both ends on the vehicle, positive out of the curve [N]: pushed in, the
// ends go out of the curve, pulled out, they go into it
double coupling_force(vehicle_state const &Vehicle, track_state const &Track) {
auto force{0.0};
for (auto const &end : Vehicle.ends) {
force += end.compression * std::sin(coupling_angle(Vehicle.length, end.other_length, Track.radius));
}
return force;
}
} // namespace
double contact_spacing(double const Gauge) {
return Gauge + rail_head_width;
}
double default_centre_of_gravity(double const BodyHeight, vehicle_class const Class) {
auto const body{BodyHeight > 2.0 ? BodyHeight : 4.0};
return body * (Class == vehicle_class::powered ? 0.38 : 0.35);
}
double loaded_centre_of_gravity(double const EmptyHeight, double const EmptyMass, double const LoadMass, double const LoadShare, double const Floor, double const BodyHeight) {
if (LoadMass <= 0.0 || EmptyMass + LoadMass <= 0.0) {
return EmptyHeight;
}
auto const body{BodyHeight > 2.0 ? BodyHeight : 4.0};
// a floor not given (or the older meaning of the keys) is that of a wagon
auto const floor{Floor > 0.3 ? Floor : 1.2};
auto const load{floor + 0.5 * std::max(0.0, body - floor) * std::clamp(LoadShare, 0.0, 1.0)};
return (EmptyHeight * EmptyMass + load * LoadMass) / (EmptyMass + LoadMass);
}
double unbalanced_acceleration(double const Velocity, double const Radius, double const Cant) {
auto const cant{gravity * std::sin(std::abs(Cant))};
if (false == curved(Radius)) {
return -cant;
}
return Velocity * Velocity / std::abs(Radius) - cant;
}
double line_curvature(track_state const &Track) {
auto const buckle{buckle_share * Track.buckle_curvature};
if (false == curved(Track.radius)) {
return std::abs(buckle);
}
return 1.0 / std::abs(Track.radius) + buckle;
}
double line_acceleration(track_state const &Track, double const Velocity) {
if (Track.buckle_curvature == 0.0) {
return unbalanced_acceleration(Velocity, Track.radius, Track.cant);
}
// bent against the curve, out of the bend is into the curve, where the cant leans the vehicle too
return Velocity * Velocity * line_curvature(Track) - gravity * std::sin(std::abs(Track.cant));
}
double unloading_ratio(double const Acceleration, double const CentreOfGravity, double const ContactSpacing, suspension const &Suspension) {
return 2.0 / ContactSpacing * std::abs(Acceleration) / gravity * leaning_height(CentreOfGravity, Suspension);
}
double overturning_acceleration(double const CentreOfGravity, double const ContactSpacing, suspension const &Suspension) {
return gravity * ContactSpacing / (2.0 * leaning_height(CentreOfGravity, Suspension));
}
double body_vibration(double const Velocity) {
return vibration * std::min(1.0, std::abs(Velocity) / (100.0 / 3.6));
}
double overturning_speed(double const Radius, double const Cant, double const CentreOfGravity, double const Gauge) {
if (false == curved(Radius)) {
return 0.0;
}
vehicle_state vehicle;
vehicle.mass = 1.0;
vehicle.centre_of_gravity = CentreOfGravity;
track_state track;
track.radius = Radius;
track.cant = Cant;
track.gauge = Gauge;
auto const spacing{contact_spacing(Gauge)};
// the unloading grows with the speed: halve the range until the speed of D = 1 is found
auto low{0.0};
auto high{200.0};
for (int iteration = 0; iteration < 60; ++iteration) {
auto const middle{0.5 * (low + high)};
auto const acceleration{body_acceleration(vehicle, track, middle)};
// below the balancing speed the cant leans the vehicle the other way
auto const outwards{unbalanced_acceleration(middle, Radius, Cant) > 0.0};
if (outwards && unloading_ratio(acceleration, CentreOfGravity, spacing) >= 1.0) {
high = middle;
}
else {
low = middle;
}
}
return high;
}
double nadal_limit(double const FlangeAngle, double const Friction) {
auto const slope{std::tan(FlangeAngle)};
return (slope - Friction) / (1.0 + Friction * slope);
}
track_condition condition_of(track_state const &Track) {
track_condition condition;
// the flags make the ride rougher: the sway of the vehicles and the short waves of the track grow, the curve and
// the cant stay what they are. sceneries set them for the jolts of a worn track (damage 31 on all of line 107),
// not for broken rails. a loose rail sways the vehicles 1.4 times as much, as the legacy criterion had it
condition.lateral = 1.0 + 0.4 * has(Track.damage, track_damage::freerail) + 0.5 * has(Track.damage, track_damage::railbend) + 0.1 * has(Track.damage, track_damage::railwear) +
0.1 * has(Track.damage, track_damage::thinrail);
// the twist of the track lifts a wheel: 1 % of its load for each mm of cross level, up to the 0.6 the twist test
// of EN 14363 lets the load drop by
condition.unloading = std::min(0.6, 0.01 * std::max(0.0, Track.uneven_cross));
return condition;
}
double coupling_angle(double const Length, double const OtherLength, double const Radius) {
if (false == curved(Radius) || OtherLength <= 0.0) {
return 0.0;
}
return (Length + OtherLength) / (4.0 * std::abs(Radius));
}
wheel_forces flange_forces(vehicle_state const &Vehicle, track_state const &Track, double const Velocity) {
wheel_forces result;
if (Vehicle.mass <= 0.0) {
return result;
}
auto const axles{static_cast<double>(std::max(Vehicle.axles, 1))};
auto const condition{condition_of(Track)};
auto const spacing{contact_spacing(Track.gauge)};
auto const height{leaning_height(Vehicle.centre_of_gravity, suspension{})};
auto const staticload{Vehicle.mass * gravity / (2.0 * axles)};
auto const inertia{Vehicle.mass * line_acceleration(Track, Velocity)};
auto const couplings{coupling_force(Vehicle, Track)};
auto const quasistatic{inertia + couplings};
// the sway and the uneven track push the worst way: towards the rail the vehicle already leans on
auto const direction{quasistatic >= 0.0 ? 1.0 : -1.0};
auto const dynamic{Vehicle.mass * dynamic_acceleration(Track, Velocity) * condition.lateral};
result.lateral = direction * (std::abs(quasistatic) + dynamic);
// the leading axle meets the rail at an angle in a curve, the creep force of its inner wheel at most the friction
auto guiding{0.0};
if (auto const curvature{std::abs(line_curvature(Track))}; curvature > 0.0) {
auto const attack{0.5 * Vehicle.axle_base * curvature};
guiding = tread_friction * staticload * std::min(1.0, attack / saturated_attack);
}
result.y = std::abs(result.lateral) / axles + guiding;
// the lateral forces at their heights move the load onto the wheels they push towards
auto const moment{direction * (inertia * height + couplings * buffer_height) + dynamic * height};
auto const shift{moment / (spacing * axles)};
result.q = std::max(0.0, (staticload + shift) * (1.0 - condition.unloading));
return result;
}
double body_acceleration(vehicle_state const &Vehicle, track_state const &Track, double const Velocity) {
if (Vehicle.mass <= 0.0) {
return 0.0;
}
auto const height{leaning_height(Vehicle.centre_of_gravity, suspension{})};
// the couplings pull at the height of the buffers: as much of a lean as their moment gives
auto const moment{Vehicle.mass * line_acceleration(Track, Velocity) * height + coupling_force(Vehicle, Track) * buffer_height};
return std::abs(moment) / (Vehicle.mass * height) + dynamic_acceleration(Track, Velocity) * condition_of(Track).lateral;
}
bool stretch::update(bool const Holds, double const Distance, double const Window) {
if (false == Holds) {
m_distance = 0.0;
return false;
}
m_distance += std::abs(Distance);
return m_distance >= Window;
}
verdict monitor::update(vehicle_state const &Vehicle, track_state const &Track, double const Velocity, double const Distance) {
verdict result;
result.acceleration = body_acceleration(Vehicle, Track, Velocity);
result.unloading = unloading_ratio(result.acceleration, Vehicle.centre_of_gravity, contact_spacing(Track.gauge));
result.flange_ratio = flange_forces(Vehicle, Track, Velocity).ratio();
auto const overturns{m_overturning.update(result.unloading >= 1.0, Distance)};
auto const climbs{m_climbing.update(result.flange_ratio >= climbing_limit, Distance)};
if (overturns) {
result.result = outcome::overturned;
}
else if (climbs) {
result.result = outcome::off_rails;
}
return result;
}
void monitor::reset() {
m_overturning.reset();
m_climbing.reset();
}
double sliding_friction(outcome const Outcome) {
switch (Outcome) {
case outcome::off_rails:
return off_rails_friction;
case outcome::overturned:
return overturned_friction;
default:
return 0.0;
}
}
std::optional<double> climbing_speed(vehicle_state const &Vehicle, track_state const &Track) {
auto const climbs = [&](double const Velocity) { return flange_forces(Vehicle, Track, Velocity).ratio() >= climbing_limit; };
// from the speed the cant balances, where the vehicle starts to lean on the outer rail
auto const balancing{curved(Track.radius) ? std::sqrt(gravity * std::abs(Track.radius) * std::sin(std::abs(Track.cant))) : 0.0};
if (climbs(balancing)) {
return balancing;
}
// in steps of 1 m/s up to the first speed it climbs at, which needn't hold above it, then halving the step
constexpr double fastest{200.0};
auto const steps{static_cast<int>(std::ceil(fastest - balancing))};
for (auto step = 0; step < steps; ++step) {
auto low{balancing + step};
auto high{std::min(low + 1.0, fastest)};
if (false == climbs(high)) {
continue;
}
for (int iteration = 0; iteration < 30; ++iteration) {
auto const middle{0.5 * (low + high)};
(climbs(middle) ? high : low) = middle;
}
return high;
}
return std::nullopt;
}
std::string report(verdict const &Verdict, vehicle_state const &Vehicle, track_state const &Track, double const Velocity, std::string_view const TrackName) {
auto const cant{1000.0 * contact_spacing(Track.gauge) * std::sin(std::abs(Track.cant))};
return std::format("{} at {:.0f} km/h on track {}, curve radius {:.0f} m, cant {:.0f} mm: lateral acceleration {:.2f} m/s2, wheel unloading {:.2f}, Y/Q {:.2f}"
" (mass {:.1f} t, centre of gravity {:.2f} m, couplings pushing {:.0f} / {:.0f} kN, track damage {}, cross level {:.0f} mm)",
Verdict.result == outcome::overturned ? "overturned" : "derailed, the flange climbed the rail", std::abs(Velocity) * 3.6, TrackName.empty() ? "(none)" : TrackName,
std::abs(Track.radius), cant, Verdict.acceleration, Verdict.unloading, Verdict.flange_ratio, Vehicle.mass / 1000.0, Vehicle.centre_of_gravity,
Vehicle.ends[0].compression / 1000.0, Vehicle.ends[1].compression / 1000.0, Track.damage, Track.uneven_cross);
}
std::string_view reason_name(reason const Reason) {
switch (Reason) {
case reason::none: return "none";
case reason::flange_climb: return "flange_climb";
case reason::overturned: return "overturned";
case reason::end_of_track: return "end_of_track";
case reason::pulled_off: return "pulled_off";
case reason::collision: return "collision";
case reason::buckle: return "buckle";
case reason::overriding: return "overriding";
case reason::pushed_off: return "pushed_off";
case reason::overspeed: return "overspeed";
case reason::track_gauge: return "track_gauge";
case reason::track_type: return "track_type";
case reason::trailed_switch: return "trailed_switch";
case reason::other: return "other";
case reason::flank_collision: return "flank_collision";
}
return "other";
}
std::string cell_text(notice const &Notice) {
return std::format("{} {} {}", Notice.vehicle, reason_name(Notice.why), Notice.track.empty() ? std::string_view{"none"} : std::string_view{Notice.track});
}
std::string derailed_name(std::string_view const Vehicle) {
return std::string{Vehicle} + ":derailed";
}
std::string describe(notice const &Notice) {
return std::format("{} derailed ({}) {} at {:.0f} km/h", Notice.vehicle, reason_name(Notice.why),
Notice.track.empty() ? std::string{"on a track without a name"} : "on track " + Notice.track, std::abs(Notice.velocity));
}
std::optional<double> trusted_speed(double const Limit) {
if (Limit < 0.0) {
return std::nullopt;
}
return Limit * trusted_limit_factor + trusted_limit_margin;
}
bool from_track_alone(verdict const &Verdict, vehicle_state const &Vehicle, track_state const &Track, double const Velocity) {
auto vehicle{Vehicle};
vehicle.ends = {};
track_state track;
track.radius = Track.radius;
track.cant = Track.cant;
track.gauge = Track.gauge;
switch (Verdict.result) {
case outcome::overturned:
return unloading_ratio(body_acceleration(vehicle, track, Velocity), vehicle.centre_of_gravity, contact_spacing(track.gauge)) >= 1.0;
case outcome::off_rails:
return flange_forces(vehicle, track, Velocity).ratio() >= climbing_limit;
default:
return false;
}
}
verdict limited_verdict(verdict const &Verdict, bool const TrackAlone, double const Velocity, double const Limit) {
auto const trusted{trusted_speed(Limit)};
if (Verdict.result == outcome::none || false == TrackAlone || false == trusted.has_value() || std::abs(Velocity) * 3.6 > *trusted) {
return Verdict;
}
auto result{Verdict};
result.result = outcome::none;
return result;
}
std::string held_back_report(verdict const &Verdict, vehicle_state const &Vehicle, track_state const &Track, double const Velocity, double const Limit, std::string_view const TrackName) {
return std::format("held on the rails on track {} under its trusted speed of {:.0f} km/h (speed limit {:.0f} km/h, too high for the curve: a defect of the scenery): {}",
TrackName.empty() ? "(none)" : TrackName, trusted_speed(Limit).value_or(0.0), Limit, report(Verdict, Vehicle, Track, Velocity, TrackName));
}
} // namespace derailment