diff --git a/CMakeLists.txt b/CMakeLists.txt index 76f359eab..d89b9a45d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -538,6 +538,7 @@ set(CORE_SOURCES "McZapkie/Mover.cpp" "McZapkie/couplerphysics.cpp" "McZapkie/derailment.cpp" +"McZapkie/collision.cpp" "McZapkie/Oerlikon_ESt.cpp" "utilities/parser.cpp" "utilities/utilities.cpp" diff --git a/McZapkie/collision.cpp b/McZapkie/collision.cpp new file mode 100644 index 000000000..60bcf4a41 --- /dev/null +++ b/McZapkie/collision.cpp @@ -0,0 +1,310 @@ +/* +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 "collision.h" + +#include +#include +#include + +#include "derailment.h" + +namespace collision { + +namespace { + +// the hit is integrated in slices of this length at most, and in no more than so many of them [s] +constexpr double slice{0.0005}; +constexpr double slices_max{200.0}; + +// the force of a curve over a range of crush within one of its parts, the force linear in it there [J] +double linear_work(double const ForceFrom, double const ForceTo, double const Length) { + return 0.5 * (ForceFrom + ForceTo) * std::max(Length, 0.0); +} + +} // namespace + +double crush_curve::force(double const Crush) const { + if (Crush >= limit) { + return std::numeric_limits::infinity(); + } + if (Crush < onset) { + return peak; + } + if (Crush < densify) { + return plateau; + } + return plateau * (1.0 + hardening * (Crush - densify)); +} + +double crush_curve::energy(double const From, double const To) const { + auto const from{std::max(From, 0.0)}; + auto const to{std::min(To, limit)}; + if (to <= from) { + return 0.0; + } + auto work{0.0}; + // the peak + work += peak * std::max(0.0, std::min(to, onset) - from); + // the plateau + auto const plateaufrom{std::max(from, onset)}; + auto const plateauto{std::min(to, std::max(densify, onset))}; + work += plateau * std::max(0.0, plateauto - plateaufrom); + // packing down + auto const densefrom{std::max(from, std::max(densify, onset))}; + if (to > densefrom) { + work += linear_work(force(densefrom), plateau * (1.0 + hardening * (to - densify)), to - densefrom); + } + return work; +} + +crush_curve default_crush(vehicle_kind const Kind) { + switch (Kind) { + case vehicle_kind::locomotive: + return {4.0e6, 3.0e6, 0.05, 0.6, 2.0, 1.2}; + case vehicle_kind::crash_zone: + return {2.0e6, 2.0e6, 0.0, 0.8, 2.5, 1.2}; + case vehicle_kind::wagon: + case vehicle_kind::coach: + default: + return {3.0e6, 2.0e6, 0.05, 0.8, 2.0, 1.5}; + } +} + +vehicle_kind kind_of(bool const Powered, bool const Passenger, bool const CrashZone) { + if (CrashZone) { + return vehicle_kind::crash_zone; + } + if (Passenger) { + return vehicle_kind::coach; + } + return Powered ? vehicle_kind::locomotive : vehicle_kind::wagon; +} + +crush_curve crush_of(end_definition const &Definition, vehicle_kind const Kind) { + auto curve{default_crush(Definition.crash_zone.value_or(false) ? vehicle_kind::crash_zone : Kind)}; + curve.peak = std::max(Definition.peak.value_or(curve.peak), 1.0); + curve.plateau = std::max(Definition.plateau.value_or(curve.plateau), 1.0); + curve.onset = std::max(Definition.onset.value_or(curve.onset), 0.0); + curve.densify = std::max(Definition.densify.value_or(curve.densify), curve.onset); + curve.hardening = std::max(Definition.hardening.value_or(curve.hardening), 0.0); + curve.limit = std::max(Definition.limit.value_or(curve.limit), curve.onset); + return curve; +} + +bool anticlimber_of(end_definition const &Definition, vehicle_kind const Kind) { + return Definition.anticlimber.value_or(Definition.crash_zone.value_or(Kind == vehicle_kind::crash_zone)); +} + +double buffer_height(end_definition const &Definition, vehicle_kind const Kind, double const LoadShare) { + auto const wagon{Kind == vehicle_kind::wagon}; + auto const empty{Definition.buffer_height.value_or(wagon ? 1.065 : 1.040)}; + // the springs of a wagon give 125 mm under its full load + return wagon ? empty - 0.125 * std::clamp(LoadShare, 0.0, 1.0) : empty; +} + +buffer_rating rating(buffer_category const Category) { + switch (Category) { + case buffer_category::B: + return {0.105, 50.0e3}; + case buffer_category::C: + return {0.105, 70.0e3}; + case buffer_category::L: + return {0.150, 70.0e3}; + case buffer_category::passenger: + return {0.110, 30.0e3}; + case buffer_category::A: + default: + return {0.105, 30.0e3}; + } +} + +coupler_physics::spring_curve buffer_curve(buffer_category const Category, double const Progression) { + auto const buffer{rating(Category)}; + auto const progression{std::clamp(Progression, 0.0, 0.9)}; + // the energy of the curve to the end of the stroke is F_end * stroke * ((1 - p) / 2 + p / 4), of two buffers + auto const endforce{2.0 * buffer.energy / (buffer.stroke * ((1.0 - progression) / 2.0 + progression / 4.0))}; + return {endforce / buffer.stroke, buffer.stroke, progression}; +} + +std::optional buffer_category_of(char const Name) { + switch (Name) { + case 'A': + case 'a': + return buffer_category::A; + case 'B': + case 'b': + return buffer_category::B; + case 'C': + case 'c': + return buffer_category::C; + case 'L': + case 'l': + return buffer_category::L; + case 'P': + case 'p': + return buffer_category::passenger; + default: + return std::nullopt; + } +} + +hit_result hit(end_state const &First, end_state const &Second, hit_input const &Input) { + hit_result result; + if (Input.velocity <= 0.0 || Input.mass1 <= 0.0 || Input.mass2 <= 0.0 || Input.dt <= 0.0) { + return result; + } + auto const mass{Input.mass1 * Input.mass2 / (Input.mass1 + Input.mass2)}; + auto const restitution{std::clamp(Input.restitution, 0.0, 0.95)}; + // the elastic hit on the steel at the closing velocity adds root * velocity to the force of the springs + auto const root{std::sqrt(std::max(Input.stiffness, 1.0) * mass)}; + auto const springs{std::max(Input.springs, 0.0)}; + // the closing velocity the step started with: the springs slow the pair over the step as well, the hit adds what + // they don't carry of the force the end gives at + auto velocity{Input.start_velocity > 0.0 ? std::max(Input.start_velocity, Input.velocity) : Input.velocity + springs * Input.dt / mass}; + // where the forces of the step alone would take the pair + auto const unhit{velocity - springs * Input.dt / mass}; + + auto crush1{First.crush}; + auto crush2{Second.crush}; + auto const elastic_limit = [&](double const Force) { return std::max(0.0, Force - springs) / root; }; + { + auto const force{std::min(First.curve.force(crush1), Second.curve.force(crush2))}; + if (false == std::isfinite(force) || velocity <= elastic_limit(force)) { + // the steel takes it all: the legacy elastic exchange + return result; + } + } + result.crushing = true; + + auto const slices{std::clamp(std::ceil(Input.dt / slice), 1.0, slices_max)}; + auto const length{Input.dt / slices}; + auto time{0.0}; + auto rebound{false}; + auto crushtime{0.0}; + while (Input.dt - time > 1e-12) { + auto const force1{First.curve.force(crush1)}; + auto const force2{Second.curve.force(crush2)}; + auto const force{std::min(force1, force2)}; + if (false == std::isfinite(force) || velocity <= elastic_limit(force)) { + // both ends crushed all the way, or the pair slowed down to what the steel takes back + rebound = true; + break; + } + auto const limit{elastic_limit(force)}; + auto step{std::min(length, Input.dt - time)}; + auto change{force * step / mass}; + if (velocity - change < limit) { + step = (velocity - limit) * mass / force; + change = velocity - limit; + } + auto const crush{(velocity - 0.5 * change) * step}; + // the weaker end gives, both alike when they're alike + if (std::abs(force1 - force2) <= 1e-9 * force) { + crush1 += 0.5 * crush; + crush2 += 0.5 * crush; + } + else if (force1 < force2) { + crush1 += crush; + } + else { + crush2 += crush; + } + velocity -= change; + time += step; + crushtime += step; + result.force = std::max(result.force, force); + } + + auto after{velocity}; + auto duration{crushtime}; + if (rebound) { + // the steel springs back with the restitution, the springs push the pair apart over the rest of the step + // under the force the weaker end gives at, as the slices stopped there + result.force = std::max(result.force, springs + velocity * root); + if (velocity > 0.0) { + duration += (1.0 + restitution) * velocity * mass / std::max(velocity * root, 1.0); + } + after = -restitution * velocity - springs * (Input.dt - time) / mass; + } + result.velocity = after; + result.impulse = mass * (unhit - after); + result.duration = std::min(duration, Input.dt); + result.crush1 = crush1 - First.crush; + result.crush2 = crush2 - Second.crush; + return result; +} + +override_result override_of(override_input const &Input) { + override_result result; + if (Input.anticlimbers) { + // interlocked, they keep the ends level until the crush zone of one of them is used up + auto const spent1{Input.zone1 <= 0.0 || Input.crush1 >= Input.zone1}; + auto const spent2{Input.zone2 <= 0.0 || Input.crush2 >= Input.zone2}; + if (false == spent1 && false == spent2) { + return result; + } + } + auto const crush{Input.crush1 + Input.crush2}; + if (crush > override_both) { + result.first = true; + result.second = true; + return result; + } + if (crush <= override_crush) { + return result; + } + if (Input.mass1 * override_mass_ratio < Input.mass2) { + result.first = true; + } + else if (Input.mass2 * override_mass_ratio < Input.mass1) { + result.second = true; + } + else if (false == Input.anticlimbers && std::abs(Input.height1 - Input.height2) >= override_height - 1e-9) { + // the higher one rides up + result.first = Input.height1 > Input.height2; + result.second = false == result.first; + } + return result; +} + +double pushed_ratio(pushed_vehicle const &Vehicle, pushed_end const &End, double const Radius) { + if (Vehicle.mass <= 0.0 || End.force <= 0.0) { + return 0.0; + } + auto const axles{static_cast(std::max(Vehicle.axles, 1))}; + // the axles of the half of the vehicle at the end: its bogie, or its axle + auto const half{std::max(axles / 2.0, 1.0)}; + auto const wheelload{Vehicle.mass * gravity / (2.0 * axles)}; + auto const halflength{std::max(0.5 * Vehicle.length, 1.0)}; + auto const angle{straight_angle + derailment::coupling_angle(Vehicle.length, End.other_length, Radius) + crush_angle * std::max(End.crush, 0.0) / halflength}; + // in a curve the leading axle meets the rail at an angle, the creep force of its inner wheel at most the friction + auto guiding{0.0}; + if (std::abs(Radius) > 1e-3) { + auto const attack{Vehicle.axle_base / (2.0 * std::abs(Radius))}; + guiding = derailment::tread_friction * wheelload * std::min(1.0, attack / derailment::saturated_attack); + } + auto const lateral{End.force * angle / half + guiding}; + // the force off the height it's carried at pitches the vehicle: the bogie at the end pressed in takes the load off + auto const pivots{Vehicle.pivots > 0.0 ? Vehicle.pivots : halflength}; + auto const vertical{wheelload - End.force * eccentricity / (2.0 * half * pivots)}; + return vertical > 0.0 ? lateral / vertical : 1e9; +} + +bool pushed_monitor::update(bool const Holds, double const Duration) { + if (false == Holds) { + m_time = 0.0; + return false; + } + m_time += std::max(Duration, 0.0); + return m_time >= pushed_time - 1e-9; +} + +} // namespace collision diff --git a/McZapkie/collision.h b/McZapkie/collision.h new file mode 100644 index 000000000..dd3dad55e --- /dev/null +++ b/McZapkie/collision.h @@ -0,0 +1,214 @@ +/* +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/. +*/ + +#pragma once + +#include + +#include "couplerphysics.h" + +// what a collision of two vehicles does to them past their buffers (physics.derailment new in eu07.ini, see +// TMoverParameters::CollisionDetect()), as plain functions of the vehicles and the hit: +// +// crush: once the buffers of the pair are pressed all the way, the underframes carry the rest of the hit, up to the +// force the weaker end gives at. past it the end crushes for good: a short peak of force, then a lower plateau as +// the structure folds, then it packs down and stiffens (the force-crush curves of the full scale tests of the FRA: +// Tyrell et al., IMECE2002-33247). the hit is an impulse limited to that force over the step, so the force never +// goes past it and the depth of the crush is the same with any step; the depth stays with the end +// override: an end crushed deep enough no longer holds the other one down: the lighter of the two vehicles, or the +// one with its buffers higher (40 mm, the vertical offset of the design collision of EN 15227), climbs onto the +// other one and leaves the rails; past a deeper crush both do. interlocked anticlimbers hold them until the crush +// zone of an end is used up +// pushed off the track: the force pressing a vehicle's end acts at an angle to it, the more in a curve and the +// deeper the end is crushed, which gives a lateral force on its wheels; acting off the height of the centre of the +// vehicle it lifts its leading wheels. Y / Q over Nadal's limit for long enough and the flange climbs the rail +// (the light wagon between heavy ones goes first, in a curve sooner than on a straight track: EN 15566 takes +// 250 kN of the pair of buffers in a curve of 150 m as the safe push) +namespace collision { + +inline constexpr double gravity{9.81}; // [m/s2] + +// ---- the crush of an end + +// force-crush characteristic of the underframe at one end of a vehicle +struct crush_curve { + double peak{3.0e6}; // the force the crushing starts at [N] + double plateau{2.0e6}; // of the crushing past the onset [N] + double onset{0.05}; // the crush past which the peak drops to the plateau [m] + double densify{0.8}; // the crush past which the crushed structure packs down and stiffens [m] + double hardening{2.0}; // the growth of the force past it, to the plateau for each metre [1/m] + double limit{1.5}; // the end can't be crushed past it, it's rigid from there [m] + + // the force the end gives at with the crush [N]; past the limit there's no force it gives at + double force(double Crush) const; + // the work it takes to crush the end from the one depth to the other one [J] + double energy(double From, double To) const; +}; + +// what the vehicle is, for its defaults +enum class vehicle_kind { + wagon, // a freight wagon + coach, // a passenger coach, or a car of a unit + locomotive, + crash_zone // a vehicle built to EN 15227, with the energy absorbers and the anticlimbers at its ends +}; +// the defaults of the kind: a wagon or a coach gives at 1.5 times the force of the proof test of EN 12663 at the +// height of the buffers (2 MN), a locomotive at 4 MN; the plateau is the 2 MN of the crush of the cab car of the FRA +// test. the absorbers of a crash zone give a steady 2 MN over 0.8 m, as the deformation tubes and the crash buffers +// do (2 MN of the crash buffers in service) +crush_curve default_crush(vehicle_kind Kind); + +// the optional keys of the .fiz for an end (BuffCoupl): what's not given comes from the kind of the vehicle +struct end_definition { + std::optional peak; // FcrushPeak [N] + std::optional plateau; // FcrushPlateau [N] + std::optional onset; // CrushOnset [m] + std::optional densify; // CrushDensify [m] + std::optional hardening; // CrushHardening [1/m] + std::optional limit; // CrushLimit [m] + std::optional crash_zone; // CrashZone: built to EN 15227 + std::optional anticlimber; // AntiClimber + std::optional buffer_height; // BufferHeight, of the empty vehicle above the rail [m] +}; +vehicle_kind kind_of(bool Powered, bool Passenger, bool CrashZone); +crush_curve crush_of(end_definition const &Definition, vehicle_kind Kind); +bool anticlimber_of(end_definition const &Definition, vehicle_kind Kind); +// height of the buffers above the rail [m]: UIC 940 to 1065 mm. without a given one, a wagon has them at the top of +// the range empty and lower by 125 mm fully loaded on its springs, a locomotive or a coach at 1040 mm +double buffer_height(end_definition const &Definition, vehicle_kind Kind, double LoadShare); + +// ---- the buffers, EN 15551 + +enum class buffer_category { + A, // 105 mm, 30 kJ + B, // 105 mm, 50 kJ + C, // 105 mm, 70 kJ + L, // the long stroke, 150 mm + passenger // 110 mm, of the coaches +}; +struct buffer_rating { + double stroke{0.105}; // [m] + double energy{30.0e3}; // dynamic energy of one buffer [J] +}; +buffer_rating rating(buffer_category Category); +// the spring curve of the two buffers of an end side by side, of the progression given, which takes in their energy +// over the stroke +coupler_physics::spring_curve buffer_curve(buffer_category Category, double Progression); +std::optional buffer_category_of(char Name); + +// ---- the hit past the buffers + +// an end of the pair, as the hit sees it +struct end_state { + crush_curve curve; + double crush{0.0}; // the depth it's crushed to already [m] +}; + +struct hit_input { + double velocity{0.0}; // the pair closes in on the stop at [m/s] + double mass1{0.0}; // [kg] + double mass2{0.0}; + double restitution{0.2}; // coefficient of restitution of the elastic rest of the hit + double stiffness{1.0e8}; // of the steel parts of both ends in series, past the springs [N/m] + double springs{0.0}; // the force the springs of the pair carry at the stop over the step [N] + double dt{0.01}; // the length of the step [s] + // the closing velocity the pair started the step with, before the forces of the step [m/s]; without it (0), the + // velocity with what the springs took off it over the step + double start_velocity{0.0}; +}; + +struct hit_result { + bool crushing{false}; // false: the hit stays elastic, it's the legacy exchange of momentum + double impulse{0.0}; // the hit adds to the forces of the step, pushing the vehicles apart [N s] + double velocity{0.0}; // the closing velocity after the step [m/s], negative as they part + double force{0.0}; // the peak force between the ends, with the springs [N] + double duration{0.0}; // how long that force acted over the step [s] + double crush1{0.0}; // added to the end of the first vehicle [m] + double crush2{0.0}; // of the second one +}; + +// the hit of the step: the steel takes the hit elastically up to the force the weaker end gives at, past that the end +// crushes at that force until the pair closes in no faster than the steel takes back. the motion over the step is +// integrated in short slices, with the springs slowing the pair as well, so the crush doesn't depend on the step +hit_result hit(end_state const &First, end_state const &Second, hit_input const &Input); + +// a crush of an end deeper than this is a damage worth the log [m] +inline constexpr double crush_mark{0.05}; +// the draw gear sits behind the headstock: crushed this deep, the coupling of the end is torn off [m] +inline constexpr double draw_gear_crush{0.15}; + +// ---- override + +// the crush of the contact of both ends past which the lighter (or the higher) vehicle climbs onto the other one, +// and past which both leave the rails [m] +inline constexpr double override_crush{0.35}; +inline constexpr double override_both{0.6}; +// the masses differ enough for the lighter one to climb +inline constexpr double override_mass_ratio{1.3}; +// the buffers differ in height enough for the higher one to climb: the vertical offset of the design collision of +// EN 15227 [m] +inline constexpr double override_height{0.04}; + +struct override_input { + double crush1{0.0}; // of the end of the first vehicle [m] + double crush2{0.0}; + double mass1{0.0}; // [kg] + double mass2{0.0}; + double height1{1.0}; // of its buffers [m] + double height2{1.0}; + bool anticlimbers{false}; // both ends have them, interlocked + double zone1{0.0}; // the crush zone of the end with the anticlimber, its densify [m] + double zone2{0.0}; +}; +struct override_result { + bool first{false}; // the first vehicle left the rails + bool second{false}; +}; +override_result override_of(override_input const &Input); + +// ---- pushed off the track + +// the angle of the force at the end of a vehicle to it on a straight track: the play of the wheelsets and of the +// bogies, the buffer heads off the middle [rad] +inline constexpr double straight_angle{0.006}; +// the crushed end no longer keeps the ends in line: the angle grows by this share of the crush to the half length +inline constexpr double crush_angle{0.1}; +// the height the force acts at off the height it's carried at, the buffers of the pair not at the same height [m] +inline constexpr double eccentricity{0.10}; +// the time Y / Q has to stay over the limit for the flange to climb the rail [s] +inline constexpr double pushed_time{0.05}; + +struct pushed_vehicle { + double mass{0.0}; // with the load, without the rotating masses [kg] + int axles{4}; + double length{0.0}; // over the buffers [m] + double pivots{0.0}; // the distance of the bogie pivots, of the axles without bogies [m] + double axle_base{0.0}; // the wheelbase of a bogie, of the vehicle without bogies [m] +}; +struct pushed_end { + double force{0.0}; // pressing the end in [N] + double other_length{0.0}; // of the vehicle at that end [m] + double crush{0.0}; // of the end [m] +}; +// Y / Q of the leading wheel at the end pressed in by the force, in a curve of the radius [m] (0 straight) +double pushed_ratio(pushed_vehicle const &Vehicle, pushed_end const &End, double Radius); + +// the time the condition held, without a break +class pushed_monitor { +public: + // true once the condition held for the time it takes the flange to climb, given how long it held in the step [s] + bool update(bool Holds, double Duration); + void reset() { m_time = 0.0; } + double time() const { return m_time; } + +private: + double m_time{0.0}; +}; + +} // namespace collision diff --git a/McZapkie/couplerphysics.cpp b/McZapkie/couplerphysics.cpp index f6a226b55..31afd26fe 100644 --- a/McZapkie/couplerphysics.cpp +++ b/McZapkie/couplerphysics.cpp @@ -235,7 +235,7 @@ pair_result update_pair(end_parameters const &First, end_parameters const &Secon else { State.draw_gear.reset(); } - auto const pushed{press(buffers_of(First, Second, Input.adapter), State.buffers, -Input.gap, -Input.gap_rate)}; + auto const pushed{press(buffers_of(First, Second, Input.adapter), State.buffers, -Input.gap - std::max(Input.crush, 0.0), -Input.gap_rate)}; result.tension = pulled.force; result.compression = pushed.force; diff --git a/McZapkie/couplerphysics.h b/McZapkie/couplerphysics.h index 47789aba2..8d7f9b06e 100644 --- a/McZapkie/couplerphysics.h +++ b/McZapkie/couplerphysics.h @@ -112,6 +112,7 @@ struct pair_input { bool coupled{false}; bool adapter{false}; // an adapter on either end: the buffers don't meet, the coupler takes the push alone bool damage{false}; // the link can stretch and break (crash damage on) + double crush{0.0}; // the ends of both crushed for good (collision.h), the buffers meet that much closer [m] }; struct pair_result {