diff --git a/physics/components/collisions.cpp b/physics/components/collisions.cpp index 1eaefb6e6..6e1e511c9 100644 --- a/physics/components/collisions.cpp +++ b/physics/components/collisions.cpp @@ -713,19 +713,27 @@ void physics::Vehicle::check_derailment(TTrackShape const &Shape, TTrackParam co { return; } - if (Global.derailment == global_settings::derailment_model::physical) + auto const physical{Global.derailment == global_settings::derailment_model::physical}; + // physics.derailment.trustlimits: under the trusted speed of the limit of the track the curve alone derails nothing, + // the limit is a defect of the scenery (derailment.h) + auto const trusted{physical && Global.derailment_trust_limits && + derailment::limited_verdict(verdict, derailment::from_track_alone(verdict, vehicle, track, travelSpeed), travelSpeed, Track.Velmax).result == derailment::outcome::none}; + if (physical && false == trusted) { derail(verdict, vehicle, track, Track.Name); return; } - // physics.derailment monitor: the legacy draw decides, the log tells what the criteria would do, once on a track + // physics.derailment monitor: the legacy draw decides, the log tells what the criteria would do; or the verdict held + // back. once on a track if (derailment_reported.has_value() && *derailment_reported == Track.Name) { return; } derailment_reported = Track.Name; ++derailmentreportsValue; - WriteLog("derailment monitor: " + Name + " " + derailment::report(verdict, vehicle, track, travelSpeed, Track.Name != nullptr ? *Track.Name : std::string{})); + auto const trackname{Track.Name != nullptr ? *Track.Name : std::string{}}; + WriteLog(trusted ? "derailment monitor: data: " + Name + " " + derailment::held_back_report(verdict, vehicle, track, travelSpeed, Track.Velmax, trackname) + : "derailment monitor: " + Name + " " + derailment::report(verdict, vehicle, track, travelSpeed, trackname)); } void physics::Vehicle::derail(derailment::verdict const &Verdict, derailment::vehicle_state const &Vehicle, derailment::track_state const &Track, std::string const *TrackName) diff --git a/physics/derailment.cpp b/physics/derailment.cpp index 46945600f..b6962caaf 100644 --- a/physics/derailment.cpp +++ b/physics/derailment.cpp @@ -282,4 +282,43 @@ std::string report(verdict const &Verdict, vehicle_state const &Vehicle, track_s Vehicle.ends[0].compression / 1000.0, Vehicle.ends[1].compression / 1000.0, Track.damage, Track.uneven_cross); } +std::optional 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 diff --git a/physics/derailment.h b/physics/derailment.h index d0b095b0d..d29fbc852 100644 --- a/physics/derailment.h +++ b/physics/derailment.h @@ -226,4 +226,33 @@ std::optional climbing_speed(vehicle_state const &Vehicle, track_state c // track damage ..., cross level ... mm)" std::string report(verdict const &Verdict, vehicle_state const &Vehicle, track_state const &Track, double Velocity, std::string_view TrackName); +// ---- the speed limits of the tracks trusted (physics.derailment.trustlimits in eu07.ini) +// +// some tracks of the sceneries allow more speed than a vehicle survives in their curves (physics.trackcheck finds +// them). a vehicle the curve and the cant alone would derail there at a speed under the trusted one below runs as the +// scenery tells its driver to: the limit is the defect, not the driving, so the vehicle stays on the rails and the log +// tells of the track. over the trusted speed the driver went too fast, and the verdict goes. a verdict the forces of the +// couplings, a buckle, the damage flags or the unevenness of the track bring about goes at any speed + +// the speed limit of a track is trusted up to limit * factor + margin [km/h]: the overspeed a driver keeps to, and a +// margin for the slow limits +inline constexpr double trusted_limit_factor{1.1}; +inline constexpr double trusted_limit_margin{5.0}; // [km/h] +// the speed [km/h] up to which the speed limit [km/h] of a track is trusted; none for a track without a limit (a +// negative one): there the drivers go by the signals and the timetable, and the criteria decide +std::optional trusted_speed(double Limit); + +// whether the curve and the cant alone give the verdict: its criterion holds as well for the vehicle without the forces +// of its couplings, on the track without a buckle, damage flags and unevenness (the track physics.trackcheck checks the +// limits on). the condition at the step of the verdict; the window it held over is the verdict's +bool from_track_alone(verdict const &Verdict, vehicle_state const &Vehicle, track_state const &Track, double Velocity); + +// the verdict that goes with the speed limits trusted, at the speed [m/s] on a track with the limit [km/h]: the given +// one, or one with no outcome (and the values of the given one) when the track alone gives it under the trusted speed +verdict limited_verdict(verdict const &Verdict, bool TrackAlone, double Velocity, double Limit); + +// what the log tells of a verdict held back, after the name of the vehicle: "held on the rails on track t1 under its +// trusted speed of 159 km/h (speed limit 140 km/h, too high for the curve: a defect of the scenery): " and the report +std::string held_back_report(verdict const &Verdict, vehicle_state const &Vehicle, track_state const &Track, double Velocity, double Limit, std::string_view TrackName); + } // namespace derailment diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md index 40c189c66..9712b2f46 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -395,6 +395,15 @@ on level ground, on an embankment 4 m high and in a cutting, made of triangle st centre (-600, 400), the cross section swept along it every 0.03 rad; the banks of the cutting rise 4 m at 1:1.5, then on at 1:5). +The `eu07_td_overspeed_bad_limit*` cases (fragment `td_overspeed_bad_limit.scn`) have that curve with a speed limit of +140 km/h, which it doesn't take (the EU07 overturns in it at about 108 km/h), and a straight track of 900 m on from it: +a defect of the data, as `physics.trackcheck` finds on some tracks of the bundled sceneries. With +`physics.derailment.trustlimits` (the default `yes`) the limit is trusted up to 1.1 times it and 5 km/h, +159 km/h: an EU07 without a driver at 140 km/h stays on the rails and the log names the track once +(`derailment monitor: data: ...`) in `eu07_td_overspeed_bad_limit`; at 175 km/h it overturns +(`eu07_td_overspeed_bad_limit_fast`), and so it does at 140 km/h with `physics.derailment.trustlimits no` +(`eu07_td_overspeed_bad_limit_untrusted`). + `eu07_td_cold_connect` (fragment `td_cold_connect.scn`) has an EU07 with a hand brake (`4E-ZEZ_GREY`; the plain `4e` has none) standing cold on track 1 of Stacja1 (trainset velocity 0, so it starts with the hand brake on) and two coaches standing 45 m ahead of it. Its driver sleeps diff --git a/tests/scenarios/cases/eu07_td_overspeed_bad_limit.json b/tests/scenarios/cases/eu07_td_overspeed_bad_limit.json new file mode 100644 index 000000000..95882c765 --- /dev/null +++ b/tests/scenarios/cases/eu07_td_overspeed_bad_limit.json @@ -0,0 +1,35 @@ +{ + "description": "physics.derailment new with physics.derailment.trustlimits (the default yes): an EU07 without a driver runs at 140 km/h into a curve of R 300 m without cant with a speed limit of 140 km/h, a track laid on its own in the fields of td.scn (autotest_curve) and a straight one on from it. The limit is a defect of the data (EU07 overturns in that curve at about 108 km/h), and 140 km/h is under its trusted speed of 159 km/h (the limit * 1.1 + 5 km/h): the criteria would overturn it, but it stays on the rails and runs on, and the log names the track once as a defect of the scenery.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_overspeed_bad_limit.scn", + "vars": { + "velocity": "140" + }, + "ini": [ + "gfxrenderer null", + "physics.derailment new", + "couplerphysics all" + ], + "expect": "timeout", + "duration": 20, + "timeout": 600, + "checks": [ + { + "type": "log_contains", + "pattern": "derailment monitor: data: autotest_\\S+_off held on the rails on track autotest_curve under its trusted speed of 159 km/h \\(speed limit 140 km/h" + }, + { + "type": "log_lacks", + "pattern": "derailment: autotest_\\S+_off leaves the track" + }, + { + "type": "log_lacks", + "pattern": "Bad driving: autotest_\\S+_off" + }, + { + "type": "on_track", + "vehicles": "{{vehicle}}_off" + } + ] +} diff --git a/tests/scenarios/cases/eu07_td_overspeed_bad_limit_fast.json b/tests/scenarios/cases/eu07_td_overspeed_bad_limit_fast.json new file mode 100644 index 000000000..28ecd8c0a --- /dev/null +++ b/tests/scenarios/cases/eu07_td_overspeed_bad_limit_fast.json @@ -0,0 +1,34 @@ +{ + "description": "physics.derailment new with physics.derailment.trustlimits: the curve of eu07_td_overspeed_bad_limit (R 300 m without cant, a speed limit of 140 km/h trusted up to 159 km/h) taken at 175 km/h, well over the trusted speed: the driver went too fast, the criteria decide, and the EU07 overturns out of the curve and slides on the grass to a stop on its side, as on the curve without a limit (eu07_td_off_track_curve_flat).", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_overspeed_bad_limit.scn", + "vars": { + "velocity": "175" + }, + "ini": [ + "gfxrenderer null", + "physics.derailment new", + "couplerphysics all" + ], + "expect": "timeout", + "duration": 60, + "timeout": 600, + "checks": [ + { + "type": "log_contains", + "pattern": "derailment: autotest_\\S+_off leaves the track" + }, + { + "type": "log_lacks", + "pattern": "derailment monitor: data:" + }, + { + "type": "leaves_track", + "vehicle": "{{vehicle}}_off", + "cause": "overturned", + "min_offset": 10, + "lying": true + } + ] +} diff --git a/tests/scenarios/cases/eu07_td_overspeed_bad_limit_untrusted.json b/tests/scenarios/cases/eu07_td_overspeed_bad_limit_untrusted.json new file mode 100644 index 000000000..b409cf9b5 --- /dev/null +++ b/tests/scenarios/cases/eu07_td_overspeed_bad_limit_untrusted.json @@ -0,0 +1,35 @@ +{ + "description": "eu07_td_overspeed_bad_limit with physics.derailment.trustlimits no: the criteria decide at any speed, as before the key, so the EU07 at the limit of 140 km/h overturns out of the curve of R 300 m it takes at 108 km/h.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_overspeed_bad_limit.scn", + "vars": { + "velocity": "140" + }, + "ini": [ + "gfxrenderer null", + "physics.derailment new", + "couplerphysics all", + "physics.derailment.trustlimits no" + ], + "expect": "timeout", + "duration": 60, + "timeout": 600, + "checks": [ + { + "type": "log_contains", + "pattern": "derailment: autotest_\\S+_off leaves the track" + }, + { + "type": "log_lacks", + "pattern": "derailment monitor: data:" + }, + { + "type": "leaves_track", + "vehicle": "{{vehicle}}_off", + "cause": "overturned", + "min_offset": 10, + "lying": true + } + ] +} diff --git a/tests/scenarios/cases/td_overspeed_bad_limit.scn b/tests/scenarios/cases/td_overspeed_bad_limit.scn new file mode 100644 index 000000000..e74351bdd --- /dev/null +++ b/tests/scenarios/cases/td_overspeed_bad_limit.scn @@ -0,0 +1,33 @@ +// added by tests/scenarios/run.py to td.scn, whose own trainsets were removed (physics.derailment new): the track of +// R 300 m without cant of td_off_track_curve_flat.scn laid on its own in the fields of td.scn, on level ground, with a +// speed limit of 140 km/h the curve doesn't take (EU07 overturns in it at about 108 km/h): a defect of the data, as +// physics.trackcheck finds on some tracks of the bundled sceneries. a straight track of 900 m goes on from its end, +// so a vehicle that keeps to the rails runs on. +// an EU07 with an AI driver and no timetable stands on track 1 of Stacja1, where td.scn keeps its timetable train, so +// it stays put and logs its physics; the EU07 of the case has no driver and runs at {{velocity}} km/h from 20 m past +// point 1 of the curve towards its point 2 +node -1 0 autotest_curve track normal 150.0000 1.435 0.15 25.0 20 0 flat vis + rail_screw_used1 6 1435mm/tpbps-new2 0.2 0.5 1.1 +-300.0000 0.2 400.0000 0.0 //point 1 +-0.0000 0.0 50.2621 //control vector 1 +24.0969 0.0 -44.1091 //control vector 2 +-336.7252 0.2 543.8277 0.0 //point 2 +300.0 +velocity 140 +endtrack +node -1 0 autotest_curve_out track normal 900.0000 1.435 0.15 25.0 20 0 flat vis + rail_screw_used1 6 1435mm/tpbps-new2 0.2 0.5 1.1 +-336.7252 0.2 543.8277 0.0 //point 1 +0.0 0.0 0.0 //control vector 1 +0.0 0.0 0.0 //control vector 2 +-768.2080 0.2 1333.6521 0.0 //point 2 +0 +velocity 140 +endtrack +trainset none tdo_n1734 0.0 0.0 +node -1 0 {{vehicle}} dynamic pkp\4e_v1 4e-004 4e 0 headdriver 0 0 enddynamic +endtrainset + +trainset none autotest_curve 20.0 {{velocity}} +node -1 0 {{vehicle}}_off dynamic pkp\4e_v1 4e-004 4e 0 nobody 0 0 enddynamic +endtrainset diff --git a/tests/unit/test_derailment.cpp b/tests/unit/test_derailment.cpp index 5dec1c570..cb23a5c5e 100644 --- a/tests/unit/test_derailment.cpp +++ b/tests/unit/test_derailment.cpp @@ -343,3 +343,159 @@ TEST_CASE("derailment: the report of the log") { verdict.result = dr::outcome::off_rails; CHECK(dr::report(verdict, vehicle, track, 20.0, "").starts_with("derailed, the flange climbed the rail at 72 km/h on track (none),")); } + +// physics.derailment.trustlimits: the speed limit of a track trusted, a verdict of the curve alone under it held back + +namespace { + +// EU07 alone, as the criteria see it +dr::vehicle_state eu07_alone() { + dr::vehicle_state vehicle; + vehicle.mass = 80000.0; + vehicle.axles = 4; + vehicle.length = 15.915; + vehicle.axle_base = 3.05; + vehicle.centre_of_gravity = eu07; + return vehicle; +} + +// the empty 3WL (10.5 t, 2 axles 6 m apart, 9.87 m long) between two EU07, pushed with the force [N] +dr::vehicle_state pushed_wagon(double const Compression) { + dr::vehicle_state wagon; + wagon.mass = 10500.0; + wagon.axles = 2; + wagon.length = 9.87; + wagon.axle_base = 6.0; + wagon.centre_of_gravity = dr::default_centre_of_gravity(3.0, dr::vehicle_class::unpowered); + for (auto &end : wagon.ends) { + end.compression = Compression; + end.other_length = 15.915; + } + return wagon; +} + +// the verdict of the monitor at the steady speed [km/h] once the vehicle ran 3 m on the track +dr::verdict verdict_at(dr::vehicle_state const &Vehicle, dr::track_state const &Track, double const Speed) { + dr::monitor monitor; + dr::verdict verdict; + for (int step = 0; step < 3; ++step) { + verdict = monitor.update(Vehicle, Track, Speed * kmh, 1.0); + } + return verdict; +} + +// what goes of that verdict on a track with the limit [km/h] +dr::outcome limited(dr::vehicle_state const &Vehicle, dr::track_state const &Track, double const Speed, double const Limit) { + auto const verdict{verdict_at(Vehicle, Track, Speed)}; + return dr::limited_verdict(verdict, dr::from_track_alone(verdict, Vehicle, Track, Speed * kmh), Speed * kmh, Limit).result; +} + +} // namespace + +TEST_CASE("derailment: the trusted speed of a speed limit") { + CHECK(dr::trusted_speed(140.0) == doctest::Approx(140.0 * dr::trusted_limit_factor + dr::trusted_limit_margin)); + CHECK(dr::trusted_speed(140.0) == doctest::Approx(159.0)); + CHECK(dr::trusted_speed(40.0) == doctest::Approx(49.0)); + // a closed track (0) is trusted to a crawl, one without a limit not at all + CHECK(dr::trusted_speed(0.0) == doctest::Approx(dr::trusted_limit_margin)); + CHECK_FALSE(dr::trusted_speed(-1.0).has_value()); +} + +TEST_CASE("derailment: a speed limit too high for the curve is trusted up to its trusted speed") { + // a 300 m curve without cant with a limit of 140 km/h, where EU07 overturns at 108 km/h: the data is wrong + auto const vehicle{eu07_alone()}; + dr::track_state track; + track.radius = 300.0; + REQUIRE(dr::overturning_speed(300.0, 0.0, eu07, 1.435) / kmh < 120.0); + + SUBCASE("at 130 km/h, under the limit, it stays on the rails") { + auto const verdict{verdict_at(vehicle, track, 130.0)}; + REQUIRE(verdict.result == dr::outcome::overturned); + CHECK(dr::from_track_alone(verdict, vehicle, track, 130.0 * kmh)); + auto const held{dr::limited_verdict(verdict, true, 130.0 * kmh, 140.0)}; + CHECK(held.result == dr::outcome::none); + // with the values of the verdict, for the log + CHECK(held.unloading == verdict.unloading); + // and so just under the trusted speed of 159 km/h + CHECK(limited(vehicle, track, 158.5, 140.0) == dr::outcome::none); + } + SUBCASE("at 160 km/h, over the trusted speed, it overturns") { + CHECK(limited(vehicle, track, 160.0, 140.0) == dr::outcome::overturned); + CHECK(limited(vehicle, track, 190.0, 140.0) == dr::outcome::overturned); + } + SUBCASE("a track without a limit isn't trusted") { + CHECK(limited(vehicle, track, 130.0, -1.0) == dr::outcome::overturned); + } + SUBCASE("over a limit the curve takes the verdict goes as it was") { + // 100 km/h: trusted up to 115 km/h + CHECK(limited(vehicle, track, 130.0, 100.0) == dr::outcome::overturned); + CHECK(verdict_at(vehicle, track, 100.0).result == dr::outcome::none); + CHECK(limited(vehicle, track, 100.0, 100.0) == dr::outcome::none); + } + SUBCASE("no verdict stays none") { + dr::verdict none; + CHECK(dr::limited_verdict(none, true, 100.0 * kmh, 140.0).result == dr::outcome::none); + CHECK_FALSE(dr::from_track_alone(none, vehicle, track, 100.0 * kmh)); + } + SUBCASE("the damage flags of line 107 don't take the trust from a curve the speed alone overturns in") { + track.damage = 31; + CHECK(limited(vehicle, track, 130.0, 140.0) == dr::outcome::none); + } + SUBCASE("the log names the track as a defect of the scenery") { + auto const verdict{verdict_at(vehicle, track, 130.0)}; + auto const text{dr::held_back_report(verdict, vehicle, track, 130.0 * kmh, 140.0, "curve_7")}; + CHECK(text.starts_with("held on the rails on track curve_7 under its trusted speed of 159 km/h (speed limit 140 km/h, too high for the curve: a defect of the scenery): " + "overturned at 130 km/h on track curve_7, curve radius 300 m,")); + } +} + +TEST_CASE("derailment: what the couplings, a buckle or the damage of the track bring about goes under any limit") { + SUBCASE("a light wagon pushed hard in a 190 m curve at 60 km/h, with a limit of 60 km/h") { + // pushed with 1 MN the couplings throw it out of the curve, at a speed the curve alone takes + auto const wagon{pushed_wagon(1000e3)}; + dr::track_state track; + track.radius = 190.0; + auto const verdict{verdict_at(wagon, track, 60.0)}; + REQUIRE(verdict.result != dr::outcome::none); + CHECK_FALSE(dr::from_track_alone(verdict, wagon, track, 60.0 * kmh)); + CHECK(limited(wagon, track, 60.0, 60.0) == verdict.result); + // not pushed it runs on + CHECK(verdict_at(pushed_wagon(0.0), track, 60.0).result == dr::outcome::none); + } + SUBCASE("a pushed wagon climbing a bent, twisted rail") { + // as in the case of the pushed light wagon: 300 kN in a 150 m curve at 20 km/h, the rail bent, a twist of 50 mm + auto const wagon{pushed_wagon(300e3)}; + dr::track_state track; + track.radius = 150.0; + track.damage = dr::track_damage::railbend; + track.uneven_cross = 50.0; + auto const verdict{verdict_at(wagon, track, 20.0)}; + REQUIRE(verdict.result == dr::outcome::off_rails); + CHECK_FALSE(dr::from_track_alone(verdict, wagon, track, 20.0 * kmh)); + CHECK(limited(wagon, track, 20.0, 40.0) == dr::outcome::off_rails); + } + SUBCASE("a buckle of a straight track") { + // EU07 at 80 km/h over a buckle bending a straight track with a limit of 100 km/h by 1/150 m + auto const vehicle{eu07_alone()}; + dr::track_state track; + track.buckle_curvature = 1.0 / 150.0; + auto const verdict{verdict_at(vehicle, track, 80.0)}; + REQUIRE(verdict.result == dr::outcome::overturned); + CHECK_FALSE(dr::from_track_alone(verdict, vehicle, track, 80.0 * kmh)); + CHECK(limited(vehicle, track, 80.0, 100.0) == dr::outcome::overturned); + } + SUBCASE("a worn track tipping over a vehicle the curve alone doesn't overturn") { + // 3 km/h under the overturning speed of EU07 on a good track the damage flags of line 107 make the sway + // overturn it: a limit over that speed doesn't hold it on the rails + auto const vehicle{eu07_alone()}; + dr::track_state track; + track.radius = 300.0; + auto const speed{dr::overturning_speed(300.0, 0.0, eu07, 1.435) / kmh - 3.0}; + REQUIRE(verdict_at(vehicle, track, speed).result == dr::outcome::none); + track.damage = 31; + auto const verdict{verdict_at(vehicle, track, speed)}; + REQUIRE(verdict.result == dr::outcome::overturned); + CHECK_FALSE(dr::from_track_alone(verdict, vehicle, track, speed * kmh)); + CHECK(limited(vehicle, track, speed, 140.0) == dr::outcome::overturned); + } +} diff --git a/tests/unit/test_derailment_vehicle.cpp b/tests/unit/test_derailment_vehicle.cpp index bbd8d4af8..9a3e857f1 100644 --- a/tests/unit/test_derailment_vehicle.cpp +++ b/tests/unit/test_derailment_vehicle.cpp @@ -326,3 +326,94 @@ TEST_CASE("derailment: the names of the models in eu07.ini") { } CHECK(name_of(model::physical) == "new"); } + +namespace { + +// physics.derailment.trustlimits for the test, restored after it +class trust_setting { +public: + explicit trust_setting(bool Trust) : m_previous{Global.derailment_trust_limits} { Global.derailment_trust_limits = Trust; } + ~trust_setting() { Global.derailment_trust_limits = m_previous; } + trust_setting(trust_setting const &) = delete; + trust_setting &operator=(trust_setting const &) = delete; + +private: + bool m_previous; +}; + +} // namespace + +TEST_CASE("derailment: the speed limit of the track trusted") { + // a 300 m curve without cant with a limit of 140 km/h, trusted up to 159 km/h; EU07 overturns there at 108 km/h + model_setting const physical{global_settings::derailment_model::physical}; + CHECK(global_settings{}.derailment_trust_limits); + test::track curve; + curve.radius = 300.0; + curve.velocity = 140.0; + curve.name = "curve_bad_limit"; + + SUBCASE("under the trusted speed it stays on the rails, the log names the track once") { + trust_setting const trust{true}; + vehicle eu07vehicle{definition::fiz("4e_v1/4e"), 130.0}; + for (int step = 0; step < 300; ++step) { + eu07vehicle.step(curve); + } + CHECK_FALSE(eu07vehicle->DamageFlag.HasAll(VehicleDamage::derailed)); + CHECK(eu07vehicle->Derailed() == dr::outcome::none); + CHECK(eu07vehicle->derailment_reports() == 1); + REQUIRE(eu07vehicle->derailment_reported.has_value()); + CHECK(*eu07vehicle->derailment_reported == &curve.name); + } + SUBCASE("over it, it overturns") { + trust_setting const trust{true}; + vehicle eu07vehicle{definition::fiz("4e_v1/4e"), 165.0}; + for (int step = 0; step < 20; ++step) { + eu07vehicle.step(curve); + } + CHECK(eu07vehicle->Derailed() == dr::outcome::overturned); + CHECK(eu07vehicle->derailment_reports() == 0); + } + SUBCASE("on a track without a limit it overturns") { + trust_setting const trust{true}; + curve.velocity = -1.0; + vehicle eu07vehicle{definition::fiz("4e_v1/4e"), 130.0}; + for (int step = 0; step < 20; ++step) { + eu07vehicle.step(curve); + } + CHECK(eu07vehicle->Derailed() == dr::outcome::overturned); + } + SUBCASE("trustlimits no: the criteria decide at any speed, as before") { + trust_setting const trust{false}; + vehicle eu07vehicle{definition::fiz("4e_v1/4e"), 130.0}; + for (int step = 0; step < 20; ++step) { + eu07vehicle.step(curve); + } + CHECK(eu07vehicle->Derailed() == dr::outcome::overturned); + CHECK(eu07vehicle->derailment_reports() == 0); + } + SUBCASE("the same with steps of 0.01 and 0.05 s") { + trust_setting const trust{true}; + for (auto const dt : {0.01, 0.05}) { + CAPTURE(dt); + auto const steps{static_cast(std::lround(1.0 / dt))}; + vehicle held{definition::fiz("4e_v1/4e"), 155.0}; + vehicle overturned{definition::fiz("4e_v1/4e"), 163.0}; + for (auto step = 0; step < steps; ++step) { + held.step(curve, dt); + overturned.step(curve, dt); + } + CHECK(held->Derailed() == dr::outcome::none); + CHECK(overturned->Derailed() == dr::outcome::overturned); + } + } + SUBCASE("monitor tells what the criteria find, as before") { + model_setting const monitor{global_settings::derailment_model::monitor}; + trust_setting const trust{true}; + vehicle eu07vehicle{definition::fiz("4e_v1/4e"), 110.0}; + for (int step = 0; step < 100; ++step) { + eu07vehicle.step(curve); + } + CHECK_FALSE(eu07vehicle->DamageFlag.HasAll(VehicleDamage::derailed)); + CHECK(eu07vehicle->derailment_reports() == 1); + } +} diff --git a/tests/unit/vehicle.cpp b/tests/unit/vehicle.cpp index 8e038d664..67f275c9a 100644 --- a/tests/unit/vehicle.cpp +++ b/tests/unit/vehicle.cpp @@ -180,6 +180,7 @@ TTrackParam parameters_of(track const &Track, physics::Vehicle const &Physics) { param.Width = Track.width; param.friction = Track.friction; param.Name = &Track.name; + param.Velmax = Track.velocity; // the vehicle's own kind of way: a road vehicle on rails derails as soon as it moves if (Track.category != 0) { param.CategoryFlag = Track.category; diff --git a/tests/unit/vehicle.h b/tests/unit/vehicle.h index bd05572e8..efb73f137 100644 --- a/tests/unit/vehicle.h +++ b/tests/unit/vehicle.h @@ -72,6 +72,7 @@ struct track { double width{1.435}; double radius{0.0}; // of a curve [m], 0 straight double cant{0.0}; // [rad] + double velocity{-1.0}; // the speed limit [km/h], negative without one std::string name; // for the log }; diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index 72758fd12..acab51a1f 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -1074,6 +1074,12 @@ bool global_settings::ConfigParseSimulation(cParser& Parser, const std::string& return true; } + if (token == "physics.derailment.trustlimits") + { + ParseOne(Parser, derailment_trust_limits, 1); + return true; + } + if (token == "physics.trackcheck") { ParseOne(Parser, track_check, 1); @@ -2572,6 +2578,7 @@ global_settings::export_as_text( std::ostream &Output ) const { } export_as_text( Output, "couplerphysics", couplerphysics ); export_as_text( Output, "physics.derailment", std::string{ name_of( derailment ) } ); + export_as_text( Output, "physics.derailment.trustlimits", derailment_trust_limits ); export_as_text( Output, "physics.trackcheck", track_check ); export_as_text( Output, "physics.collisions", std::string{ name_of( collisions ) } ); export_as_text( Output, "ai.brakesetting.cw1", AIBrakeSettingCw1 ); diff --git a/utilities/Globals.h b/utilities/Globals.h index d8ed3e13e..2e5d97263 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -375,6 +375,11 @@ struct global_settings { // tracks allow more than the reference vehicle survives there): new is opt-in until then enum class derailment_model { off, old, physical, monitor }; derailment_model derailment = derailment_model::monitor; + // physics.derailment.trustlimits, with new: under the speed limit of a track (and 10 % and 5 km/h over it) the curve + // and the cant alone derail nothing, the limit is a defect of the scenery, told once in the log (derailment monitor: + // data:). what the couplings, a buckle or the damage of the track bring about derails at any speed, see + // derailment::limited_verdict(). no: the criteria decide at any speed + bool derailment_trust_limits = true; // the collisions of rail vehicles past their buffers (physics.collisions in eu07.ini): new, the ends crush, a vehicle // may ride up onto the other one or be pushed off the track (physics/collision.h); old, the legacy rule of the safe // closing speed weighed by the masses; monitor, the legacy rule decides and the log tells what the crush would be