18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 06:35:21 +01:00
Files
maszyna/tests/unit/test_bufferstop.cpp
T
jerrrrycho 885c28c1ef fix: SonarQube findings of the buffer stops
- The notice of a finished hit is taken out of the contact with std::exchange before it goes to the scenery, so
  what the scenery does with it can't reach the one held in the contact.
- The loops count whole steps (an int, the number of steps taken with ceil) instead of adding a floating step.
- The local final of bufferstop::contact() is ending (final is a contextual keyword).

Unit tests 1294/1294.
2026-12-02 17:04:29 +01:00

411 lines
17 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "physics/bufferstop.h"
#include "physics/collision.h"
#include <algorithm>
#include <cmath>
#include <optional>
// the buffer stops at the free ends of the tracks (physics/bufferstop.h): the attribute, the stages of a stop against a
// vehicle alone, and the models of the automatic rule. the bands are those of the sources of bufferstop.h; they guard the
// tuning, as the accident bands of test_collisions.cpp do
namespace bs = bufferstop;
namespace cl = collision;
namespace {
constexpr double kmh{1.0 / 3.6};
// a vehicle end against a stop: its mass and crush curve, the buffers of EN 15551 category A unless told otherwise
bs::contact_input vehicle_end(double const Mass, cl::vehicle_kind const Kind, double const Dt = 0.01) {
bs::contact_input input;
input.mass = Mass;
input.vehicle_end = {cl::default_crush(Kind), 0.0};
input.buffers = cl::buffer_curve(cl::buffer_category::A, 0.0);
input.vehicle_capacity = 2.0 * cl::rating(cl::buffer_category::A).energy;
input.vehicle_height = 1.04;
input.anticlimber = Kind == cl::vehicle_kind::crash_zone;
input.zone = cl::default_crush(Kind).densify;
input.dt = Dt;
return input;
}
// a rigid impactor, without buffers: as the FEM model of the stop has it
bs::contact_input impactor(double const Mass, cl::vehicle_kind const Kind, double const Dt = 0.01) {
auto input{vehicle_end(Mass, Kind, Dt)};
input.buffers = {1.0, 0.0, 0.0};
input.vehicle_capacity = 0.0;
return input;
}
struct run_result {
bs::state stop;
bs::outcome worst{bs::outcome::none};
double velocity{0.0}; // the vehicle leaves at, towards the stop [m/s]
double peak{0.0}; // [N]
double vehicle_crush{0.0};
double work{0.0};
double absorber{0.0}; // the most of the stroke used [m]
bool climbed{false};
double exit{0.0}; // the velocity the vehicle went on past the stop at, after riding up or through it [m/s]
};
// the vehicle runs into the stop at the speed from 1 cm before it, step by step as the game steps it, until it leaves the
// stop again, rides up it or goes on past it
run_result run(bs::definition const &Stop, bs::contact_input Input, double const Speed, bs::state Start = {}) {
run_result result;
result.stop = Start;
Input.closing = Speed;
Input.penetration = -0.01;
auto touched{false};
auto const steps{static_cast<int>(std::ceil(120.0 / Input.dt))};
for (auto index{0}; index < steps; ++index) {
auto const step{bs::contact(Stop, result.stop, Input)};
result.worst = bs::worse(result.worst, step.result);
result.peak = std::max(result.peak, step.force);
result.vehicle_crush += step.vehicle_crush;
result.work += step.work;
result.absorber = std::max(result.absorber, result.stop.absorber);
Input.closing = step.velocity;
Input.penetration += step.travel;
Input.vehicle_end.crush += step.vehicle_crush;
if (step.climbed || result.stop.spent()) {
result.climbed = step.climbed;
result.exit = step.residual;
break;
}
if (touched && false == step.touched && result.stop.absorber == 0.0) {
// gone off the stop, the absorber of a hydraulic one following it out
break;
}
touched = touched || step.touched;
}
result.velocity = Input.closing;
return result;
}
// the lowest speed [km/h], in steps of a quarter, the vehicle rides up the stop at
std::optional<double> ride_up_speed(bs::definition const &Stop, bs::contact_input const &Input) {
for (auto quarter{16}; quarter <= 80; ++quarter) {
auto const speed{0.25 * quarter};
if (run(Stop, Input, speed * kmh).climbed) {
return speed;
}
}
return std::nullopt;
}
} // namespace
TEST_CASE("bufferstop: the attribute of the track") {
SUBCASE("a kind alone is its preset") {
auto const stop{bs::read_attribute("rigid")};
REQUIRE(stop.has_value());
CHECK(stop->type == bs::kind::rigid);
CHECK(stop->setback == 0.0);
CHECK(stop->frame.peak == bs::preset(bs::kind::rigid).frame.peak);
CHECK(stop->height == doctest::Approx(0.96));
}
SUBCASE("a friction stop: the elements and the run-out, its face that far before the end") {
auto const stop{bs::read_attribute("friction;6;8")};
REQUIRE(stop.has_value());
CHECK(bs::friction_force(*stop, 0.0) == doctest::Approx(240.0e3));
CHECK(stop->runout == doctest::Approx(8.0));
CHECK(stop->setback == doctest::Approx(8.0));
// a face nearer the end leaves less of the run-out
auto const shorter{bs::read_attribute("friction;6;8;5")};
REQUIRE(shorter.has_value());
CHECK(shorter->runout == doctest::Approx(5.0));
}
SUBCASE("a hydraulic stop: the energy over the stroke") {
auto const stop{bs::read_attribute("hydraulic;336;0.6")};
REQUIRE(stop.has_value());
CHECK(stop->absorber_force == doctest::Approx(560.0e3));
CHECK(stop->absorber_stroke == doctest::Approx(0.6));
}
SUBCASE("the setback alone, past the end") {
auto const stop{bs::read_attribute("rigid;;;-0.5")};
REQUIRE(stop.has_value());
CHECK(stop->setback == doctest::Approx(-0.5));
}
SUBCASE("none opts the end out") {
auto const stop{bs::read_attribute("none")};
REQUIRE(stop.has_value());
CHECK(stop->type == bs::kind::none);
}
SUBCASE("what isn't a stop") {
CHECK_FALSE(bs::read_attribute("foo").has_value());
CHECK_FALSE(bs::read_attribute("friction;6;-8").has_value());
CHECK_FALSE(bs::read_attribute("friction;six;8").has_value());
CHECK_FALSE(bs::read_attribute("hydraulic;336;0").has_value());
CHECK_FALSE(bs::read_attribute("rigid;;;x").has_value());
CHECK_FALSE(bs::read_attribute("").has_value());
}
}
TEST_CASE("bufferstop: the brake elements of a sliding stop hold less the longer it slides") {
auto const stop{bs::preset(bs::kind::friction)};
CHECK(bs::friction_force(stop, 0.0) == doctest::Approx(240.0e3));
CHECK(bs::friction_force(stop, 4.9) == doctest::Approx(240.0e3));
CHECK(bs::friction_force(stop, 6.0) == doctest::Approx(216.0e3));
CHECK(bs::friction_force(stop, 10.0) == doctest::Approx(192.0e3));
CHECK(bs::friction_force(stop, 14.0) == doctest::Approx(168.0e3));
// over the 8 m of its run-out the 1848 kJ of the calculation of Rawie 10 EB
auto work{0.0};
constexpr double step{0.001};
for (auto index{0}; index < 8000; ++index) {
auto const slid{(index + 0.5) * step};
work += bs::friction_force(stop, slid) * step;
}
CHECK(work == doctest::Approx(1848.0e3).epsilon(0.001));
}
TEST_CASE("bufferstop: a locomotive shunting into a rigid stop") {
auto const stop{bs::preset(bs::kind::rigid)};
for (auto const speed : {3.0, 5.0}) {
CAPTURE(speed);
auto const hit{run(stop, vehicle_end(80.0e3, cl::vehicle_kind::locomotive), speed * kmh)};
CHECK(hit.stop.crush < cl::crush_mark);
CHECK(hit.vehicle_crush == 0.0);
CHECK(hit.worst == bs::outcome::held);
CHECK_FALSE(hit.climbed);
// it bounces off its buffers, which take in at least half of the energy (EN 15551): at no more than 0.71 of its speed
CHECK(hit.velocity < 0.0);
CHECK(-hit.velocity <= std::sqrt(0.5) * speed * kmh);
if (speed == 3.0) {
// its buffers take it all, under the force the stop gives at
CHECK(hit.peak < stop.frame.peak);
CHECK(hit.stop.crush == 0.0);
}
}
}
TEST_CASE("bufferstop: the FEM case of Kowalczyk, 50 t at 10.8 km/h into a rigid stop") {
auto const stop{bs::preset(bs::kind::rigid)};
auto const speed{10.8 * kmh};
auto const hit{run(stop, impactor(50.0e3, cl::vehicle_kind::locomotive), speed)};
// the FEM: it deforms by 0.18 m, 85 % of the energy going into it
CHECK(hit.stop.crush >= 0.15);
CHECK(hit.stop.crush <= 0.25);
auto const kinetic{0.5 * 50.0e3 * speed * speed};
CHECK(hit.work / kinetic >= 0.80);
CHECK(hit.work / kinetic <= 0.90);
CHECK_FALSE(hit.climbed);
CHECK(hit.worst == bs::outcome::damaged);
CHECK(hit.vehicle_crush == 0.0);
}
TEST_CASE("bufferstop: Enfield, a unit of 140 t at 12 km/h rides up a rigid stop and comes to rest on it") {
auto const stop{bs::preset(bs::kind::rigid)};
auto const hit{run(stop, vehicle_end(140.0e3, cl::vehicle_kind::crash_zone), 12.0 * kmh)};
CHECK(hit.climbed);
CHECK(hit.worst == bs::outcome::rode_up);
CHECK_FALSE(hit.stop.demolished);
CHECK(hit.exit == 0.0);
CHECK(hit.velocity == 0.0);
}
TEST_CASE("bufferstop: Kirkby, 100 t at 47 km/h demolish a rigid stop and go on") {
auto const stop{bs::preset(bs::kind::rigid)};
auto const input{vehicle_end(100.0e3, cl::vehicle_kind::coach)};
auto const hit{run(stop, input, 47.0 * kmh)};
CHECK(hit.worst == bs::outcome::demolished);
CHECK(hit.stop.demolished);
CHECK(hit.exit > 40.0 * kmh);
// the wreck holds nothing any more
auto state{hit.stop};
auto again{input};
again.closing = 10.0 * kmh;
again.penetration = 0.5;
auto const next{bs::contact(stop, state, again)};
CHECK_FALSE(next.touched);
CHECK(next.force == 0.0);
CHECK(next.velocity == again.closing);
CHECK(state.demolished);
}
TEST_CASE("bufferstop: the speed a vehicle starts to ride up a rigid stop at") {
auto const stop{bs::preset(bs::kind::rigid)};
// Enfield rode up at 12 km/h; the bands guard the tuning
auto const unit{ride_up_speed(stop, vehicle_end(140.0e3, cl::vehicle_kind::crash_zone))};
REQUIRE(unit.has_value());
CHECK(*unit >= 7.0);
CHECK(*unit <= 10.0);
auto const locomotive{ride_up_speed(stop, vehicle_end(80.0e3, cl::vehicle_kind::locomotive))};
REQUIRE(locomotive.has_value());
CHECK(*locomotive >= 9.0);
CHECK(*locomotive <= 13.0);
CHECK(*locomotive > *unit);
}
TEST_CASE("bufferstop: a sliding stop holds a train of 314 t at 10 km/h within its run-out") {
auto const stop{bs::preset(bs::kind::friction)};
auto const hit{run(stop, vehicle_end(314.0e3, cl::vehicle_kind::wagon), 10.0 * kmh)};
// 1.2 MJ at 240 kN: about 5 m of the 8
CHECK(hit.stop.slid >= 4.8);
CHECK(hit.stop.slid <= 5.4);
CHECK(hit.worst == bs::outcome::held);
CHECK_FALSE(hit.stop.overrun);
CHECK(hit.vehicle_crush == 0.0);
CHECK(hit.stop.crush == 0.0);
// never more than the brake elements hold it with
CHECK(hit.peak <= bs::friction_force(stop, 0.0) * (1.0 + 1e-9));
}
TEST_CASE("bufferstop: a train of 314 t at 15 km/h overruns a sliding stop") {
auto const stop{bs::preset(bs::kind::friction)};
auto const mass{314.0e3};
auto const speed{15.0 * kmh};
auto const hit{run(stop, vehicle_end(mass, cl::vehicle_kind::wagon), speed)};
CHECK(hit.worst == bs::outcome::overrun);
CHECK(hit.stop.overrun);
CHECK(hit.stop.slid == doctest::Approx(stop.runout));
// the body of the stop takes its share of the momentum as it starts off, then the elements the work of the run-out
auto const start{speed * mass / (mass + stop.body_mass)};
auto const work{1848.0e3};
auto const expected{std::sqrt(start * start - 2.0 * work / (mass + stop.body_mass))};
CHECK(hit.velocity == doctest::Approx(expected).epsilon(0.02));
}
TEST_CASE("bufferstop: a hydraulic stop takes 220 t at 6 km/h within its stroke, and recovers") {
auto const stop{bs::preset(bs::kind::hydraulic)};
auto const hit{run(stop, vehicle_end(220.0e3, cl::vehicle_kind::wagon), 6.0 * kmh)};
CHECK(hit.absorber > 0.3);
CHECK(hit.absorber < stop.absorber_stroke);
CHECK(hit.stop.crush == 0.0);
CHECK(hit.vehicle_crush == 0.0);
CHECK(hit.worst == bs::outcome::held);
// the vehicle gone, the absorber is out again
CHECK(hit.stop.absorber == 0.0);
}
TEST_CASE("bufferstop: past its stroke a hydraulic stop passes the hit to the end of the coach") {
auto const stop{bs::preset(bs::kind::hydraulic)};
auto const hit{run(stop, vehicle_end(300.0e3, cl::vehicle_kind::coach), 8.0 * kmh)};
CHECK(hit.absorber == doctest::Approx(stop.absorber_stroke));
// the coach end, 3 MN at its peak and 2 MN past it, gives before the base of the stop
CHECK(hit.vehicle_crush > cl::crush_mark);
CHECK(hit.stop.crush < cl::crush_mark);
CHECK(hit.vehicle_crush > hit.stop.crush);
}
TEST_CASE("bufferstop: the hits don't depend on the step") {
auto const rigid{bs::preset(bs::kind::rigid)};
auto const friction{bs::preset(bs::kind::friction)};
auto const fem = [&](double const Dt) { return run(rigid, impactor(50.0e3, cl::vehicle_kind::locomotive, Dt), 10.8 * kmh); };
auto const enfield = [&](double const Dt) { return run(rigid, vehicle_end(140.0e3, cl::vehicle_kind::crash_zone, Dt), 12.0 * kmh); };
auto const sliding = [&](double const Dt) { return run(friction, vehicle_end(314.0e3, cl::vehicle_kind::wagon, Dt), 10.0 * kmh); };
auto const fem1{fem(0.01)};
auto const enfield1{enfield(0.01)};
auto const sliding1{sliding(0.01)};
for (auto const dt : {0.005, 0.0025}) {
CAPTURE(dt);
CHECK(fem(dt).stop.crush == doctest::Approx(fem1.stop.crush).epsilon(0.02));
CHECK(fem(dt).work == doctest::Approx(fem1.work).epsilon(0.02));
auto const climbing{enfield(dt)};
CHECK(climbing.climbed == enfield1.climbed);
CHECK(climbing.stop.crush == doctest::Approx(enfield1.stop.crush).epsilon(0.02));
CHECK(sliding(dt).stop.slid == doctest::Approx(sliding1.stop.slid).epsilon(0.02));
}
}
TEST_CASE("bufferstop: a unit with a central coupler crushes the stop sooner than a vehicle with buffers") {
auto const stop{bs::preset(bs::kind::rigid)};
auto const buffered{vehicle_end(80.0e3, cl::vehicle_kind::locomotive)};
auto central{buffered};
// the draft gear of the coupler: 20 mm, 500 kN; no buffers of EN 15551 at the side
central.buffers = {500.0e3 / 0.02, 0.02, 0.0};
central.vehicle_capacity = 0.0;
for (auto const speed : {6.0, 9.0}) {
CAPTURE(speed);
auto const withbuffers{run(stop, buffered, speed * kmh)};
auto const withcoupler{run(stop, central, speed * kmh)};
CHECK(withcoupler.stop.crush > withbuffers.stop.crush);
}
}
TEST_CASE("bufferstop: the hit on the frame is collision::hit() of the end against the stop, immovable") {
auto const stop{bs::preset(bs::kind::rigid)};
bs::state state;
state.crush = 0.02;
auto input{vehicle_end(80.0e3, cl::vehicle_kind::locomotive)};
input.vehicle_end.crush = 0.01;
input.stiffness = 2.0e8;
auto const adapted{bs::frame_hit(stop, state, input, 3.0, 1.1e6, 0.01, 200.0e3)};
cl::hit_input hand;
hand.velocity = 3.0;
hand.mass1 = 80.0e3;
hand.mass2 = 1e12;
hand.restitution = 0.4;
// the steel of the end in series with the structure of the stop
hand.stiffness = 2.0e8 * 17.5e6 / (2.0e8 + 17.5e6);
hand.springs = 1.1e6;
hand.dt = 0.01;
hand.start_velocity = 3.0;
hand.budget = 200.0e3;
auto const direct{cl::hit({cl::default_crush(cl::vehicle_kind::locomotive), 0.01}, {stop.frame, 0.02}, hand)};
REQUIRE(direct.crushing);
CHECK(adapted.crushing == direct.crushing);
CHECK(adapted.impulse == doctest::Approx(direct.impulse));
CHECK(adapted.velocity == doctest::Approx(direct.velocity));
CHECK(adapted.force == doctest::Approx(direct.force));
CHECK(adapted.crush1 == doctest::Approx(direct.crush1));
CHECK(adapted.crush2 == doctest::Approx(direct.crush2));
CHECK(adapted.work == doctest::Approx(direct.work));
CHECK(adapted.spent == direct.spent);
}
TEST_CASE("bufferstop: the models of the stops at the free ends") {
auto const rules{bs::default_rules()};
SUBCASE("by the name of the file") {
auto const concrete{bs::match("kolejowe/koziolbeton.t3d", rules)};
REQUIRE(concrete.has_value());
CHECK(concrete->type == bs::kind::rigid);
auto const lublin{bs::match("models/kolejowe/koziol_lublin.e3d", rules)};
REQUIRE(lublin.has_value());
CHECK(lublin->type == bs::kind::buffered);
CHECK(bs::match("KOLEJOWE\\KozioLOporowy", rules).has_value());
CHECK_FALSE(bs::match("wiejskie/wiatrak_kozlak.t3d", rules).has_value());
CHECK_FALSE(bs::match("eng/mostymw/odbojnice_p10m_1.e3d", rules).has_value());
CHECK_FALSE(bs::match("kolejowe/semafor_2k.t3d", rules).has_value());
}
SUBCASE("rules of a text") {
auto const own{bs::read_rules("// mine\n!*pad*\nstop_* hydraulic 0.5\nbad_* nokind 0\n")};
REQUIRE(own.size() == 2);
CHECK(own[0].excluded);
auto const stop{bs::match("x/stop_pad.t3d", own)};
CHECK_FALSE(stop.has_value());
auto const other{bs::match("x/stop_a.t3d", own)};
REQUIRE(other.has_value());
CHECK(other->type == bs::kind::hydraulic);
CHECK(other->face == doctest::Approx(0.5));
CHECK(bs::wildcard("*oporow*", "kozioloporowy"));
CHECK_FALSE(bs::wildcard("koziol*", "wiatrak_kozlak"));
}
SUBCASE("where the model stands at the end") {
// the end at the origin, the track going out of it along +z
auto const behind{bs::place(0.0, 0.0, 0.0, 1.0, 0.3, 1.2)};
CHECK(behind.along == doctest::Approx(1.2));
CHECK(bs::at_end(behind));
CHECK(bs::model_setback(behind, {"koziol*", bs::kind::rigid, 0.0, false}) == doctest::Approx(-1.0));
auto const at{bs::place(0.0, 0.0, 0.0, 1.0, 0.0, 0.02)};
CHECK(bs::model_setback(at, {"koziol*", bs::kind::rigid, 0.0, false}) == doctest::Approx(-0.02));
// a parallel track 4.5 m off, or one 5 m on
CHECK_FALSE(bs::at_end(bs::place(0.0, 0.0, 0.0, 1.0, 4.5, 0.0)));
CHECK_FALSE(bs::at_end(bs::place(0.0, 0.0, 0.0, 1.0, 0.0, 5.0)));
CHECK_FALSE(bs::at_end(bs::place(0.0, 0.0, 0.0, 1.0, 0.0, -3.5)));
}
}
TEST_CASE("bufferstop: what the scenery is told") {
bs::notice const notice{"loco", "stub", bs::outcome::rode_up, 12.0};
CHECK(bs::cell_text(notice) == "loco rode_up stub");
CHECK(bs::cell_name("stub") == "stub:bufferstop");
CHECK(static_cast<int>(bs::outcome::demolished) == 4);
CHECK(bs::worse(bs::outcome::held, bs::outcome::rode_up) == bs::outcome::rode_up);
CHECK(bs::worse(bs::outcome::demolished, bs::outcome::damaged) == bs::outcome::demolished);
}