diff --git a/CMakeLists.txt b/CMakeLists.txt index 98e703cc0..5aa62523d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -539,6 +539,7 @@ set(CORE_SOURCES "McZapkie/Mover.cpp" "McZapkie/couplerphysics.cpp" "McZapkie/derailment.cpp" +"world/TrackCheck.cpp" "McZapkie/Oerlikon_ESt.cpp" "utilities/parser.cpp" "utilities/utilities.cpp" diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index d4a7cb2b2..c254d7da5 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -714,6 +714,8 @@ struct TTrackParam int DamageFlag = 0; /// Maximum allowed velocity on this segment (used by AI driver). double Velmax; /*dla uzytku maszynisty w ai_driver*/ + /// Name of the track, for the log; nullptr where there is none. + std::string const *Name = nullptr; }; /// Traction-supply parameters at the vehicle's current location. @@ -2051,6 +2053,10 @@ class TMoverParameters // the criteria of derailment (physics.derailment new), and the distance travelled since they were checked [m] derailment::monitor derailment_monitor; double derailment_distance = 0.0; + // physics.derailment monitor: the name of the track the criteria were last reported on (one report on a track), and + // how many reports there were + std::optional derailment_reported; + int derailment_reports = 0; odometer::reading Odometer; // total distance run, kept between the sessions (eu07/maszyna#72) std::pair EnergyMeter; // energy from grid [kWh] double V = 0.0; // predkosc w [m/s] względem sprzęgów (dodania gdy jedzie w stronę 0) @@ -2437,10 +2443,11 @@ class TMoverParameters // was a broken coupler to repair bool repair_coupler(int const End); void Derail(DerailReason Reason); - // physics.derailment new: the criteria of derailment.h over the step, which travelled the Distance [m] + // physics.derailment new: the criteria of derailment.h over the step, which travelled the Distance [m]; monitor: what + // they would do, to the log void check_derailment(TTrackShape const &Shape, TTrackParam const &Track, double Distance); // the vehicle leaves the rails, as the criteria of derailment.h found - void derail(derailment::verdict const &Verdict, derailment::vehicle_state const &Vehicle, derailment::track_state const &Track); + void derail(derailment::verdict const &Verdict, derailment::vehicle_state const &Vehicle, derailment::track_state const &Track, std::string const *TrackName = nullptr); // height of the centre of gravity above the rail with the load [m] double centre_of_gravity() const; // how the vehicle left the rails by the criteria of derailment.h: upright, or on its side. none for any other diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index d73479058..c47131a83 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -1549,13 +1549,26 @@ void TMoverParameters::check_derailment(TTrackShape const &Shape, TTrackParam co track.damage = Track.DamageFlag; auto const verdict{derailment_monitor.update(vehicle, track, V, Distance)}; - if (verdict.result != derailment::outcome::none) + if (verdict.result == derailment::outcome::none) { - derail(verdict, vehicle, track); + return; } + if (Global.derailment == global_settings::derailment_model::physical) + { + 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 + if (derailment_reported.has_value() && *derailment_reported == Track.Name) + { + return; + } + derailment_reported = Track.Name; + ++derailment_reports; + WriteLog("derailment monitor: " + Name + " " + derailment::report(verdict, vehicle, track, V, Track.Name != nullptr ? *Track.Name : std::string{})); } -void TMoverParameters::derail(derailment::verdict const &Verdict, derailment::vehicle_state const &Vehicle, derailment::track_state const &Track) +void TMoverParameters::derail(derailment::verdict const &Verdict, derailment::vehicle_state const &Vehicle, derailment::track_state const &Track, std::string const *TrackName) { if (false == SetFlag(DamageFlag, dtrain_out)) { @@ -1569,12 +1582,7 @@ void TMoverParameters::derail(derailment::verdict const &Verdict, derailment::ve { AlarmChainFlag = true; } - auto const cant{1000.0 * derailment::contact_spacing(Track.gauge) * std::sin(std::abs(Track.cant))}; - ErrorLog(std::format("Bad driving: {} {} at {:.0f} km/h, 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)", - Name, Derailed == derailment::outcome::overturned ? "overturned" : "derailed, the flange climbed the rail", std::abs(V) * 3.6, std::abs(Track.radius), cant, - Verdict.acceleration, Verdict.unloading, Verdict.flange_ratio, Vehicle.mass / 1000.0, Vehicle.centre_of_gravity, Vehicle.ends[end::front].compression / 1000.0, - Vehicle.ends[end::rear].compression / 1000.0, Track.damage, Track.uneven_cross)); + ErrorLog("Bad driving: " + Name + " " + derailment::report(Verdict, Vehicle, Track, V, TrackName != nullptr ? *TrackName : std::string{})); if (Track.buckle_curvature != 0.0) { ErrorLog(std::format("Bad driving: {} derailed on buckled track, the buckle bending the line by 1/{:.0f} m {} the curve", Name, 1.0 / std::abs(Track.buckle_curvature), @@ -1709,7 +1717,7 @@ double TMoverParameters::ComputeMovement(double dt, double dt1, const TTrackShap Derail(END_OF_TRACK); // too fast in a curve: the legacy draw in every step (physics.derailment old), or the physical criteria // once the step's distance is known, below - if (Global.derailment == global_settings::derailment_model::old && FuzzyLogic(AccN / g * (1.0 + 0.1 * (Track.DamageFlag & dtrack_freerail)), TrackW / Dim.H, 1)) + if ((Global.derailment == global_settings::derailment_model::old || Global.derailment == global_settings::derailment_model::monitor) && FuzzyLogic(AccN / g * (1.0 + 0.1 * (Track.DamageFlag & dtrack_freerail)), TrackW / Dim.H, 1)) Derail(TOO_HIGH_SPEED); // wykolejanie na poszerzeniu toru if (FuzzyLogic(abs(Track.Width - TrackW), TrackW / 10.0, 1)) @@ -1722,7 +1730,7 @@ double TMoverParameters::ComputeMovement(double dt, double dt1, const TTrackShap // dL:=(V+AccS*dt/2)*dt; // przyrost dlugosci czyli przesuniecie dL = (3.0 * V - Vprev) * dt / 2.0; // metoda Adamsa-Bashfortha} - if (Global.derailment == global_settings::derailment_model::physical) + if (Global.derailment == global_settings::derailment_model::physical || Global.derailment == global_settings::derailment_model::monitor) { // over the distance of this step and of the simpler ones of the frame before it check_derailment(Shape, Track, derailment_distance + std::abs(dL)); diff --git a/McZapkie/derailment.cpp b/McZapkie/derailment.cpp index 9ddf31917..e9cfa452e 100644 --- a/McZapkie/derailment.cpp +++ b/McZapkie/derailment.cpp @@ -12,6 +12,7 @@ http://mozilla.org/MPL/2.0/. #include #include +#include namespace derailment { @@ -247,4 +248,38 @@ double sliding_friction(outcome const Outcome) { } } +std::optional 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(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); +} + } // namespace derailment diff --git a/McZapkie/derailment.h b/McZapkie/derailment.h index 65058bd4a..50a3d84cb 100644 --- a/McZapkie/derailment.h +++ b/McZapkie/derailment.h @@ -10,6 +10,9 @@ http://mozilla.org/MPL/2.0/. #pragma once #include +#include +#include +#include // the criteria of a vehicle leaving the rails, as plain functions of what the vehicle and the track under it are // like, so the collisions and the buckled track can use them as well as the speed in a curve does @@ -213,4 +216,14 @@ inline constexpr double off_rails_friction{0.3}; inline constexpr double overturned_friction{0.5}; double sliding_friction(outcome Outcome); +// the lowest speed [m/s] from the one the cant balances at which the flange of the leading axle climbs the rail (Y / Q +// at the climbing limit); none when it doesn't climb up to 200 m/s: a lone vehicle on a good track mostly overturns +// first, its Y / Q goes to the ratio of the contact spacing to the height of the centre of gravity at speed +std::optional climbing_speed(vehicle_state const &Vehicle, track_state const &Track); + +// what the criteria found, for the log: "overturned at 113 km/h on track t1, curve radius 300 m, cant 0 mm: lateral +// acceleration ..., wheel unloading ..., Y/Q ... (mass ..., centre of gravity ..., couplings pushing ... / ... kN, +// 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); + } // namespace derailment diff --git a/simulation/simulationstateserializer.cpp b/simulation/simulationstateserializer.cpp index 9d1e94869..3a4edb7b1 100644 --- a/simulation/simulationstateserializer.cpp +++ b/simulation/simulationstateserializer.cpp @@ -445,6 +445,9 @@ state_serializer::deserialize_firstinit( cParser &Input, scene::scratch_data &Sc Scratchpad.binary.terrain_binaryfiles.clear(); simulation::Paths.InitTracks(); + if( Global.track_check ) { + simulation::Paths.CheckTracks(); + } // the roads tie up their own lanes where they gain or lose some, the junctions tie the lanes of the roads together, // what's left loose after that gets closed by the roads simulation::Roads.InitLanes(); diff --git a/tests/unit/test_derailment.cpp b/tests/unit/test_derailment.cpp index 4fa314fcd..3afca6a26 100644 --- a/tests/unit/test_derailment.cpp +++ b/tests/unit/test_derailment.cpp @@ -143,7 +143,7 @@ TEST_CASE("derailment: the decision and the place don't depend on the step") { auto const coarse{run(1.05 * critical, 0.5)}; REQUIRE(fine.has_value()); REQUIRE(coarse.has_value()); - CHECK(std::abs(*coarse - *fine) <= 1.05 * critical * 0.5); + CHECK(std::abs(coarse.value() - fine.value()) <= 1.05 * critical * 0.5); } TEST_CASE("derailment: a peak shorter than the window doesn't derail") { @@ -278,3 +278,68 @@ TEST_CASE("derailment: the damage flags of the track make the ride rougher, not CHECK(dr::body_acceleration(eu07vehicle, worn, 30.0) - dr::body_acceleration(eu07vehicle, good, 30.0) == doctest::Approx(1.1 * dr::vibration)); } } + +TEST_CASE("derailment: the speed the flange climbs the rail at") { + dr::vehicle_state vehicle; + vehicle.mass = 80000.0; + vehicle.axles = 4; + vehicle.length = 16.0; + vehicle.axle_base = 3.0; + vehicle.centre_of_gravity = 1.65; + dr::track_state track; + track.radius = 300.0; + SUBCASE("a lone vehicle on a good track overturns first: its Y/Q stays under the limit") { + CHECK_FALSE(dr::climbing_speed(vehicle, track).has_value()); + } + SUBCASE("on a twisted track it climbs, at the speed Y/Q reaches the limit") { + // 50 mm of cross level takes half the load off the wheel + track.uneven_cross = 50.0; + auto const speed{dr::climbing_speed(vehicle, track)}; + REQUIRE(speed.has_value()); + CHECK(*speed > 0.0); + CHECK(dr::flange_forces(vehicle, track, *speed).ratio() >= dr::climbing_limit); + CHECK(dr::flange_forces(vehicle, track, *speed - 0.01).ratio() < dr::climbing_limit); + SUBCASE("the cant moves it up") { + track.cant = std::asin(0.1 / 1.5); + auto const canted{dr::climbing_speed(vehicle, track)}; + REQUIRE(canted.has_value()); + CHECK(*canted > *speed); + } + } + SUBCASE("pushed hard enough it climbs at a standstill: from the speed the cant balances") { + // an empty 3WL between heavy vehicles pushing 1 MN in a curve of 150 m, on 40 mm of cross level + vehicle.mass = 10500.0; + vehicle.axles = 2; + vehicle.length = 9.0; + vehicle.axle_base = 5.0; + vehicle.centre_of_gravity = 1.1; + vehicle.ends[0] = {1000000.0, 16.0}; + vehicle.ends[1] = {1000000.0, 16.0}; + track.radius = 150.0; + track.uneven_cross = 40.0; + auto const speed{dr::climbing_speed(vehicle, track)}; + REQUIRE(speed.has_value()); + CHECK(*speed == 0.0); + } +} + +TEST_CASE("derailment: the report of the log") { + dr::verdict verdict; + verdict.result = dr::outcome::overturned; + verdict.acceleration = 3.31; + verdict.unloading = 1.02; + verdict.flange_ratio = 0.55; + dr::vehicle_state vehicle; + vehicle.mass = 80000.0; + vehicle.centre_of_gravity = 1.65; + vehicle.ends[1].compression = 50000.0; + dr::track_state track; + track.radius = -300.0; + track.damage = 31; + auto const text{dr::report(verdict, vehicle, track, 113.0 / 3.6, "track_7")}; + CHECK(text == + "overturned at 113 km/h on track track_7, curve radius 300 m, cant 0 mm: lateral acceleration 3.31 m/s2, wheel unloading 1.02, Y/Q 0.55 (mass 80.0 t, centre of gravity 1.65 m," + " couplings pushing 0 / 50 kN, track damage 31, cross level 0 mm)"); + 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),")); +} diff --git a/tests/unit/test_derailment_vehicle.cpp b/tests/unit/test_derailment_vehicle.cpp index 23e395fdc..6e8e4661a 100644 --- a/tests/unit/test_derailment_vehicle.cpp +++ b/tests/unit/test_derailment_vehicle.cpp @@ -5,6 +5,8 @@ #include "McZapkie/MOVER.h" #include "utilities/Globals.h" +#include + // a vehicle too fast in a curve with physics.derailment of eu07.ini: the criteria of McZapkie/derailment.h checked by // TMoverParameters::check_derailment(), and what the vehicle comes to off the rails @@ -82,13 +84,14 @@ TEST_CASE("derailment: a vehicle too fast in a curve") { SUBCASE("the same with steps of 0.01 and 0.05 s") { for (auto const dt : {0.01, 0.05}) { CAPTURE(dt); + auto const steps{static_cast(std::lround(1.0 / dt))}; vehicle eu07vehicle{definition::fiz("4e_v1/4e"), critical + 2.0}; - for (auto time = 0.0; time < 1.0; time += dt) { + for (auto step = 0; step < steps; ++step) { eu07vehicle.step(curve, dt); } CHECK(eu07vehicle->Derailed == dr::outcome::overturned); vehicle slower{definition::fiz("4e_v1/4e"), critical - 2.0}; - for (auto time = 0.0; time < 1.0; time += dt) { + for (auto step = 0; step < steps; ++step) { slower.step(curve, dt); } CHECK(slower->Derailed == dr::outcome::none); @@ -161,3 +164,66 @@ TEST_CASE("derailment: a stopped derailed vehicle stays put on a gradient") { } CHECK(still); } + +TEST_CASE("derailment: monitor, the legacy draw decides and the log tells what the criteria would do") { + model_setting const monitor{global_settings::derailment_model::monitor}; + test::track curve; + curve.radius = 300.0; + curve.name = "curve_1"; + auto const critical{dr::overturning_speed(300.0, 0.0, eu07, 1.435) / kmh}; + // over the speed the criteria overturn the EU07 at, under the threshold of the legacy draw (112 km/h) + vehicle eu07vehicle{definition::fiz("4e_v1/4e"), critical + 2.0}; + REQUIRE(critical + 2.0 < 112.0); + for (int step = 0; step < 100; ++step) { + eu07vehicle.step(curve); + } + CHECK_FALSE(TestFlag(eu07vehicle->DamageFlag, dtrain_out)); + CHECK(eu07vehicle->Derailed == dr::outcome::none); + // once on a track, however long it stays over the criteria + CHECK(eu07vehicle->derailment_reports == 1); + REQUIRE(eu07vehicle->derailment_reported.has_value()); + CHECK(*eu07vehicle->derailment_reported == &curve.name); + // and once more on the next one + test::track next{curve}; + next.name = "curve_2"; + for (int step = 0; step < 100; ++step) { + eu07vehicle.step(next); + } + CHECK(eu07vehicle->derailment_reports == 2); + CHECK_FALSE(TestFlag(eu07vehicle->DamageFlag, dtrain_out)); + + SUBCASE("under the criteria nothing is told") { + vehicle slower{definition::fiz("4e_v1/4e"), critical - 5.0}; + for (int step = 0; step < 100; ++step) { + slower.step(curve); + } + CHECK(slower->derailment_reports == 0); + } + SUBCASE("far over the legacy threshold the legacy draw derails it, the legacy way") { + vehicle faster{definition::fiz("4e_v1/4e"), 160.0}; + for (int step = 0; step < 600; ++step) { + faster.step(curve); + } + CHECK(TestFlag(faster->DamageFlag, dtrain_out)); + CHECK(faster->Derailed == dr::outcome::none); + CHECK(faster->V == doctest::Approx(0.0).epsilon(0.01)); + } +} + +TEST_CASE("derailment: the names of the models in eu07.ini") { + using model = global_settings::derailment_model; + CHECK(derailment_model_of("new") == model::physical); + CHECK(derailment_model_of("old") == model::old); + CHECK(derailment_model_of("off") == model::off); + CHECK(derailment_model_of("no") == model::off); + CHECK(derailment_model_of("monitor") == model::monitor); + // a name it doesn't know is the default + CHECK(derailment_model_of("physical") == model::physical); + CHECK(global_settings{}.derailment == model::physical); + CHECK_FALSE(global_settings{}.track_check); + // written back the way it's read + for (auto const value : {model::off, model::old, model::physical, model::monitor}) { + CHECK(derailment_model_of(name_of(value)) == value); + } + CHECK(name_of(model::physical) == "new"); +} diff --git a/tests/unit/test_track_check.cpp b/tests/unit/test_track_check.cpp new file mode 100644 index 000000000..ce0424d81 --- /dev/null +++ b/tests/unit/test_track_check.cpp @@ -0,0 +1,158 @@ +#include "stdafx.h" +#include "doctest.h" +#include "world/TrackCheck.h" + +#include +#include +#include +#include + +// physics.trackcheck: the geometry of the rail tracks of a scenery the criteria of derailment find suspicious, see +// world/TrackCheck.h + +namespace tc = track_check; + +namespace { + +bool has(tc::findings const &Found, tc::kind const Kind) { + return std::find(Found.kinds.begin(), Found.kinds.end(), Kind) != Found.kinds.end(); +} + +tc::track curve(double const Radius, double const Velocity, double const Cant = 0.0) { + tc::track track; + track.name = "t1"; + track.radius = Radius; + track.velocity = Velocity; + track.cant = {Cant, Cant}; + return track; +} + +double overturning(double const Radius, double const Cant = 0.0) { + auto const &eu07{tc::default_references().front().vehicle}; + return derailment::overturning_speed(Radius, std::asin(Cant / 1500.0), eu07.centre_of_gravity, 1.435) * 3.6; +} + +} // namespace + +TEST_CASE("trackcheck: the default vehicles") { + auto const &references{tc::default_references()}; + REQUIRE(references.size() == 2); + CHECK(references[0].name == "EU07"); + CHECK(references[0].vehicle.centre_of_gravity == doctest::Approx(1.65).epsilon(0.01)); + CHECK(references[1].vehicle.centre_of_gravity == doctest::Approx(1.42).epsilon(0.01)); + // the EU07 overturns at 108 km/h in a curve of 300 m without cant + CHECK(overturning(300.0) == doctest::Approx(108.0).epsilon(0.02)); +} + +TEST_CASE("trackcheck: the speed limit of a curve") { + auto const critical{overturning(300.0)}; + SUBCASE("under the speeds of the vehicles nothing") { + CHECK(tc::check(curve(300.0, critical - 10.0)).empty()); + } + SUBCASE("over the overturning speed of the EU07") { + auto const found{tc::check(curve(300.0, critical + 5.0))}; + CHECK(has(found, tc::kind::overturning)); + CHECK_FALSE(has(found, tc::kind::overturning_default)); + CHECK(found.text.find("over the overturning speed of EU07") != std::string::npos); + CHECK(found.text.find("speed limit 113 km/h") != std::string::npos); + } + SUBCASE("the cant raises it") { + CHECK(tc::check(curve(300.0, critical + 5.0, 100.0)).empty()); + } + SUBCASE("without a speed limit of its own the default one goes, counted apart") { + auto const found{tc::check(curve(300.0, -1.0))}; + CHECK(has(found, tc::kind::overturning_default)); + CHECK_FALSE(has(found, tc::kind::overturning)); + CHECK(found.text.find("default speed 125 km/h") != std::string::npos); + CHECK(found.text.find("speed limit none") != std::string::npos); + } + SUBCASE("a closed track (speed 0) is not checked") { + CHECK(tc::check(curve(100.0, 0.0)).empty()); + } + SUBCASE("nor the speed of a switch") { + auto track{curve(150.0, 100.0)}; + track.plain = false; + CHECK(tc::check(track).empty()); + } + SUBCASE("the cant at the end with the less of it") { + auto track{curve(300.0, critical + 5.0, 100.0)}; + track.cant[0] = 0.0; + CHECK(has(tc::check(track), tc::kind::overturning)); + } +} + +TEST_CASE("trackcheck: the cant") { + SUBCASE("the same cant at both ends of a straight track") { + auto const found{tc::check(curve(0.0, 100.0, 60.0))}; + CHECK(has(found, tc::kind::cant_on_straight)); + CHECK(found.text.starts_with("cant 60 / 60 mm on a straight track")); + } + SUBCASE("a transition has it at one end only") { + auto track{curve(0.0, 100.0, 60.0)}; + track.cant[0] = 0.0; + CHECK(tc::check(track).empty()); + } + SUBCASE("a few mm are left alone") { + CHECK(tc::check(curve(0.0, 100.0, 4.0)).empty()); + } + SUBCASE("over 160 mm") { + auto const found{tc::check(curve(1000.0, 60.0, 170.0))}; + CHECK(has(found, tc::kind::cant_excess)); + CHECK(found.kinds.size() == 1); + CHECK(tc::check(curve(1000.0, 60.0, 150.0)).empty()); + } +} + +TEST_CASE("trackcheck: a tight curve") { + CHECK(has(tc::check(curve(30.0, 50.0)), tc::kind::tight_curve)); + CHECK_FALSE(has(tc::check(curve(30.0, 30.0)), tc::kind::tight_curve)); + CHECK_FALSE(has(tc::check(curve(60.0, 50.0)), tc::kind::tight_curve)); +} + +TEST_CASE("trackcheck: the unevenness") { + auto track{curve(0.0, 100.0)}; + track.uneven.value = 14.f; + track.uneven.side = 20.f; + track.uneven.cross = 20.f; + CHECK(tc::check(track).empty()); + track.uneven.side = 40.f; + track.uneven.cross = 26.f; + auto const found{tc::check(track)}; + CHECK(has(found, tc::kind::uneven)); + CHECK(found.text.starts_with("uneven side 40, cross 26 mm out of range")); +} + +TEST_CASE("trackcheck: the scan of a scenery") { + std::vector tracks; + tracks.push_back(curve(300.0, 140.0)); + tracks.back().name = "fast"; + tracks.push_back(curve(0.0, 100.0)); + tracks.back().name = "worn"; + tracks.back().damage = 31; + tracks.push_back(curve(0.0, 100.0, 50.0)); + tracks.back().name = "leaning"; + tracks.back().damage = 31; + tracks.push_back(curve(0.0, 100.0)); + tracks.back().name = "fine"; + tracks.back().damage = 2; + + std::vector log; + auto const summary{tc::scan(tracks, [&](std::string const &Line) { log.push_back(Line); })}; + CHECK(summary.tracks == 4); + CHECK(summary.suspicious == 2); + CHECK(summary.count(tc::kind::overturning) == 1); + CHECK(summary.count(tc::kind::cant_on_straight) == 1); + CHECK(summary.count(tc::kind::tight_curve) == 0); + // the damage flags are counted, not told track by track + CHECK(summary.damage.at(31) == 2); + CHECK(summary.damage.at(2) == 1); + REQUIRE(log.size() == 4); + CHECK(log[0].starts_with("trackcheck: fast: speed limit 140 km/h over the overturning speed of EU07")); + CHECK(log[1].starts_with("trackcheck: leaning: cant 50 / 50 mm on a straight track")); + CHECK(log[1].ends_with("damage 31)")); + CHECK(log[2].starts_with("trackcheck: 4 rail tracks checked, 2 suspicious: overturning 1; flange climbing 0;")); + CHECK(log[3] == "trackcheck: damage flags: 2 on 1; 31 on 2"); + for (auto const &line : log) { + CHECK(line.starts_with("trackcheck: ")); + } +} diff --git a/tests/unit/vehicle.cpp b/tests/unit/vehicle.cpp index 100344369..61609d3ba 100644 --- a/tests/unit/vehicle.cpp +++ b/tests/unit/vehicle.cpp @@ -179,6 +179,7 @@ TTrackParam parameters_of(track const &Track, TMoverParameters const &Mover) { TTrackParam param; param.Width = Track.width; param.friction = Track.friction; + param.Name = &Track.name; // 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 784a4486d..1e7db3599 100644 --- a/tests/unit/vehicle.h +++ b/tests/unit/vehicle.h @@ -68,6 +68,7 @@ struct track { double width{1.435}; double radius{0.0}; // of a curve [m], 0 straight double cant{0.0}; // [rad] + std::string name; // for the log }; // the catenary of the Polish railways [V] diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index 37dd30eed..d47a12325 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -898,15 +898,17 @@ bool global_settings::ConfigParseSimulation(cParser& Parser, const std::string& if (token == "physics.derailment") { - // new: the physical criteria (McZapkie/derailment.h), old: the legacy draw, off: no derailment from the speed + // new: the physical criteria (McZapkie/derailment.h), old: the legacy draw, off: no derailment from the speed, + // monitor: the legacy draw, and the criteria to the log std::string model; ParseOne(Parser, model, 1); - if (model == "old") - derailment = derailment_model::old; - else if (model == "off" || model == "no") - derailment = derailment_model::off; - else - derailment = derailment_model::physical; + derailment = derailment_model_of(model); + return true; + } + + if (token == "physics.trackcheck") + { + ParseOne(Parser, track_check, 1); return true; } @@ -1808,10 +1810,8 @@ global_settings::export_as_text( std::ostream &Output ) const { couplerphysics = "all"; } export_as_text( Output, "couplerphysics", couplerphysics ); - export_as_text( Output, "physics.derailment", std::string{ - derailment == derailment_model::old ? "old" : - derailment == derailment_model::off ? "off" : - "new" } ); + export_as_text( Output, "physics.derailment", std::string{ name_of( derailment ) } ); + export_as_text( Output, "physics.trackcheck", track_check ); export_as_text( Output, "ai.brakesetting.cw1", AIBrakeSettingCw1 ); export_as_text( Output, "scalespeculars", ScaleSpecularValues ); export_as_text( Output, "gfxrenderer", GfxRenderer ); diff --git a/utilities/Globals.h b/utilities/Globals.h index e82beddd4..533a3cde8 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -297,9 +297,13 @@ struct global_settings { // a vehicle too fast in a curve (physics.derailment in eu07.ini): new, it overturns or its flange climbs the rail // by the criteria of McZapkie/derailment.h held over 2 m, and slides on with the friction of the ballast; old, the // legacy draw of the lateral acceleration against the gauge to the height of the vehicle in every step, which stops - // it; off, it stays on the rails at any speed - enum class derailment_model { off, old, physical }; + // it; off, it stays on the rails at any speed; monitor, the legacy draw decides and the log tells where the criteria + // would derail a vehicle (derailment monitor:, once for a vehicle on a track), for the run of a scenery as it was + enum class derailment_model { off, old, physical, monitor }; derailment_model derailment = derailment_model::physical; + // physics.trackcheck: the geometry of the rail tracks the criteria of derailment find suspicious, to the log once the + // scenery loaded (trackcheck:, see world/TrackCheck.h) + bool track_check = false; bool control_coupling_engine_check = true; // no multiple-unit control between electric and diesel traction vehicles (eu07/maszyna#520) bool gui_defaultwindows = true; bool gui_showtranscripts = true; @@ -449,6 +453,23 @@ template <> void global_settings::export_as_text( std::ostream &Output, std::string const Key, bool const &Value ) const; +// the models of physics.derailment by their names in eu07.ini: new (the default, also for a name it doesn't know), old, off +// or no, monitor +inline global_settings::derailment_model derailment_model_of(std::string_view const Name) { + if( Name == "old" ) { return global_settings::derailment_model::old; } + if( Name == "off" || Name == "no" ) { return global_settings::derailment_model::off; } + if( Name == "monitor" ) { return global_settings::derailment_model::monitor; } + return global_settings::derailment_model::physical; +} +inline std::string_view name_of( global_settings::derailment_model const Model ) { + switch( Model ) { + case global_settings::derailment_model::old: { return "old"; } + case global_settings::derailment_model::off: { return "off"; } + case global_settings::derailment_model::monitor: { return "monitor"; } + default: { return "new"; } + } +} + extern global_settings& GetGlobalSettings(); #define Global (GetGlobalSettings()) diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index ca4ca771f..b806ef5ce 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -3239,6 +3239,7 @@ bool TDynamicObject::Update(double dt, double dt1) tp.CategoryFlag = MyTrack->iCategoryFlag & 15; tp.DamageFlag = MyTrack->iDamageFlag; tp.QualityFlag = MyTrack->iQualityFlag; + tp.Name = &MyTrack->name(); // couplers if( MoverParameters->Couplers[0].CouplingFlag != coupling::faux diff --git a/world/Track.cpp b/world/Track.cpp index 27bf7c9dc..e776ea874 100644 --- a/world/Track.cpp +++ b/world/Track.cpp @@ -16,6 +16,7 @@ http://mozilla.org/MPL/2.0/. #include "world/Track.h" #include "world/TrackRails.h" +#include "world/TrackCheck.h" #include "simulation/simulation.h" #include "scene/scene.h" #include "utilities/Globals.h" @@ -4197,6 +4198,68 @@ path_table::InitTracks() { TTrack::fetch_default_profiles(); } +namespace { + +// the tightest radius of the curve of a path in the plan [m], from the circles through its design points about 5 m apart +// (without the unevenness, whose short waves would bend it): the curve the axles of a vehicle follow, whether the track +// gives the radius or only its control points. 0 for a straight one; a radius over 15 km is taken for one, as +// TDynamicObject::Update() takes it +double tightest_radius(TSegment const &Segment) { + + auto const length { Segment.GetLength() }; + if( length < 0.5 ) { + return 0.0; + } + auto const intervals { std::max( 2, static_cast( std::ceil( length / 5.0 ) ) ) }; + auto const flat = [&]( int const Index ) { + auto const point { Segment.FastGetPoint( static_cast( Index ) / intervals ) }; + return glm::dvec2 { point.x, point.z }; }; + auto tightest { 0.0 }; + for( int index = 1; index < intervals; ++index ) { + auto const a { flat( index - 1 ) }; + auto const b { flat( index ) }; + auto const c { flat( index + 1 ) }; + auto const ab { b - a }; + auto const ac { c - a }; + auto const area2 { std::abs( ab.x * ac.y - ab.y * ac.x ) }; + if( area2 < 1e-9 ) { + continue; + } + auto const radius { glm::length( ab ) * glm::length( c - b ) * glm::length( ac ) / ( 2.0 * area2 ) }; + if( radius < 15000.0 && ( tightest == 0.0 || radius < tightest ) ) { + tightest = radius; + } + } + return tightest; +} + +} // namespace + +void +path_table::CheckTracks() const { + + std::vector tracks; + for( auto *path : sequence() ) { + if( path == nullptr || ( path->iCategoryFlag & 1 ) == 0 || path->Segment == nullptr ) { + continue; + } + track_check::track track; + track.name = path->name(); + track.velocity = path->VelocityGet(); + track.plain = ( path->eType == tt_Normal ); + track.radius = track.plain ? tightest_radius( *path->Segment ) : 0.0; + float roll1 { 0.f }, roll2 { 0.f }; + path->Segment->GetRolls( roll1, roll2 ); + track.gauge = path->fTrackWidth; + auto const spacing { 1000.0 * derailment::contact_spacing( track.gauge ) }; + track.cant = { spacing * std::sin( roll1 ), spacing * std::sin( roll2 ) }; + track.damage = path->iDamageFlag; + track.uneven = path->m_uneven; + tracks.emplace_back( std::move( track ) ); + } + track_check::scan( tracks, []( std::string const &Line ) { WriteLog( Line ); } ); +} + // legacy method, sends list of occupied paths over network void path_table::TrackBusyList() const { diff --git a/world/Track.h b/world/Track.h index 81c80785d..906cbb944 100644 --- a/world/Track.h +++ b/world/Track.h @@ -555,6 +555,9 @@ public: // legacy method, initializes tracks after deserialization from scenario file void InitTracks(); + // physics.trackcheck: logs the geometry of the rail tracks the checks of world/TrackCheck.h find suspicious + void + CheckTracks() const; // legacy method, sends list of occupied paths over network void TrackBusyList() const; diff --git a/world/TrackCheck.cpp b/world/TrackCheck.cpp new file mode 100644 index 000000000..22bc61861 --- /dev/null +++ b/world/TrackCheck.cpp @@ -0,0 +1,184 @@ +/* +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 "world/TrackCheck.h" + +#include +#include +#include + +namespace track_check { + +namespace { + +constexpr double kmh{3.6}; + +reference make_reference(std::string const &Name, double const Mass, double const Length, double const AxleBase, double const BodyHeight, derailment::vehicle_class const Class) { + reference result; + result.name = Name; + result.vehicle.mass = Mass; + result.vehicle.axles = 4; + result.vehicle.length = Length; + result.vehicle.axle_base = AxleBase; + result.vehicle.centre_of_gravity = derailment::default_centre_of_gravity(BodyHeight, Class); + return result; +} + +// the cant [rad] of the given height of the outer rail [mm] +double cant_angle(double const Cant, double const Gauge) { + return std::asin(std::clamp(std::abs(Cant) / (1000.0 * derailment::contact_spacing(Gauge)), 0.0, 1.0)); +} + +void append(std::string &Text, std::string const &Part) { + if (false == Text.empty()) { + Text += "; "; + } + Text += Part; +} + +// the values of the unevenness past the range, as "side 40, cross 30" +std::string uneven_excess(track_uneven::attribute const &Uneven, settings const &Settings) { + std::string result; + auto const test = [&](char const *Name, float const Value, double const Most) { + if (Value > Most) { + if (false == result.empty()) { + result += ", "; + } + result += std::format("{} {:g}", Name, Value); + } + }; + test("value", Uneven.value, Settings.most_uneven); + test("up", Uneven.up, Settings.most_uneven); + test("side", Uneven.side, Settings.most_uneven); + test("cross", Uneven.cross, Settings.most_cross); + test("dip", Uneven.dip, Settings.most_uneven); + test("branch", Uneven.branch, Settings.most_uneven); + test("branch up", Uneven.branchup, Settings.most_uneven); + test("branch side", Uneven.branchside, Settings.most_uneven); + return result; +} + +} // namespace + +std::vector const &default_references() { + static std::vector const references{ + make_reference("EU07", 80000.0, 15.915, 3.0, 4.348, derailment::vehicle_class::powered), + make_reference("coach", 40000.0, 24.5, 2.5, 4.05, derailment::vehicle_class::unpowered), + }; + return references; +} + +std::string_view name_of(kind const Kind) { + switch (Kind) { + case kind::overturning: + return "overturning"; + case kind::climbing: + return "flange climbing"; + case kind::overturning_default: + return "overturning at the default speed"; + case kind::climbing_default: + return "flange climbing at the default speed"; + case kind::cant_on_straight: + return "cant on a straight"; + case kind::cant_excess: + return "cant too high"; + case kind::tight_curve: + return "tight curve too fast"; + case kind::uneven: + return "uneven out of range"; + default: + return "?"; + } +} + +findings check(track const &Track, std::vector const &References, settings const &Settings) { + findings result; + auto const found = [&](kind const Kind, std::string const &Text) { + if (std::find(result.kinds.begin(), result.kinds.end(), Kind) == result.kinds.end()) { + result.kinds.push_back(Kind); + } + append(result.text, Text); + }; + auto const curved{Track.radius > 0.0}; + auto const given{Track.velocity >= 0.0}; + auto const velocity{given ? Track.velocity : Settings.default_velocity}; + + if (Track.plain) { + // the speed in the curve, with the cant both ends have at the least + if (curved && velocity > 0.0) { + auto const cant{std::min(std::abs(Track.cant[0]), std::abs(Track.cant[1]))}; + derailment::track_state track; + track.radius = Track.radius; + track.cant = cant_angle(cant, Track.gauge); + track.gauge = Track.gauge; + for (auto const &reference : References) { + auto const overturning{derailment::overturning_speed(track.radius, track.cant, reference.vehicle.centre_of_gravity, track.gauge) * kmh}; + if (overturning > 0.0 && velocity > overturning) { + found(given ? kind::overturning : kind::overturning_default, + std::format("{} {:.0f} km/h over the overturning speed of {} {:.0f} km/h", given ? "speed limit" : "default speed", velocity, reference.name, overturning)); + } + if (auto const climbing{derailment::climbing_speed(reference.vehicle, track)}; climbing.has_value() && velocity > *climbing * kmh) { + found(given ? kind::climbing : kind::climbing_default, + std::format("{} {:.0f} km/h over the flange climbing speed of {} {:.0f} km/h", given ? "speed limit" : "default speed", velocity, reference.name, *climbing * kmh)); + } + } + } + // cant held over the whole of a straight track: a transition has it at one end only + if (false == curved && std::abs(Track.cant[0]) > Settings.straight_cant && std::abs(Track.cant[1]) > Settings.straight_cant && Track.cant[0] * Track.cant[1] > 0.0) { + found(kind::cant_on_straight, std::format("cant {:.0f} / {:.0f} mm on a straight track", Track.cant[0], Track.cant[1])); + } + if (curved && velocity > Settings.tight_velocity && Track.radius < Settings.tight_radius) { + found(kind::tight_curve, std::format("radius {:.0f} m with a {} of {:.0f} km/h", Track.radius, given ? "speed limit" : "default speed", velocity)); + } + } + if (auto const most{std::max(std::abs(Track.cant[0]), std::abs(Track.cant[1]))}; most > Settings.most_cant) { + found(kind::cant_excess, std::format("cant {:.0f} mm over {:.0f} mm", most, Settings.most_cant)); + } + if (auto const excess{uneven_excess(Track.uneven, Settings)}; false == excess.empty()) { + found(kind::uneven, std::format("uneven {} mm out of range", excess)); + } + if (false == result.empty()) { + result.text += std::format(" (radius {:.0f} m, cant {:.0f} / {:.0f} mm, speed limit {}, damage {})", Track.radius, Track.cant[0], Track.cant[1], + given ? std::format("{:.0f} km/h", Track.velocity) : std::string{"none"}, Track.damage); + } + return result; +} + +summary scan(std::vector const &Tracks, sink const &Log, std::vector const &References, settings const &Settings) { + summary result; + for (auto const &track : Tracks) { + ++result.tracks; + if (track.damage != 0) { + ++result.damage[track.damage]; + } + auto const found{check(track, References, Settings)}; + if (found.empty()) { + continue; + } + ++result.suspicious; + for (auto const kind : found.kinds) { + ++result.counts[static_cast(kind)]; + } + Log(std::format("trackcheck: {}: {}", track.name.empty() ? std::string{"(none)"} : track.name, found.text)); + } + std::string counts; + for (std::size_t kind = 0; kind < result.counts.size(); ++kind) { + append(counts, std::format("{} {}", name_of(static_cast(kind)), result.counts[kind])); + } + Log(std::format("trackcheck: {} rail tracks checked, {} suspicious: {}", result.tracks, result.suspicious, counts)); + std::string damage; + for (auto const &[flags, count] : result.damage) { + append(damage, std::format("{} on {}", flags, count)); + } + Log(std::format("trackcheck: damage flags: {}", damage.empty() ? std::string{"none"} : damage)); + return result; +} + +} // namespace track_check diff --git a/world/TrackCheck.h b/world/TrackCheck.h new file mode 100644 index 000000000..d07b3cb49 --- /dev/null +++ b/world/TrackCheck.h @@ -0,0 +1,96 @@ +/* +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 "McZapkie/derailment.h" +#include "world/TrackUneven.h" + +#include +#include +#include +#include +#include +#include + +// the check of the geometry of the rail tracks of a scenery (physics.trackcheck yes in eu07.ini): what the criteria of +// derailment (McZapkie/derailment.h) and the plain rules of the track find suspicious, to the log once the scenery +// loaded, so a run can tell a faulty scenery from a faulty simulation. one line for a track with all it found +// ("trackcheck: : ..."), and a summary with the count of each kind. the damage flags are only counted: sceneries +// set them for the jolts of a worn track (damage 31 on every track of line 107) +namespace track_check { + +// a rail track as the check sees it +struct track { + std::string name; + double velocity{-1.0}; // the speed limit of the track [km/h], negative when it gives none + double radius{0.0}; // the tightest radius of its curve [m], 0 on a straight one + std::array cant{0.0, 0.0}; // at its ends [mm], positive raising the right rail + double gauge{1.435}; // [m] + int damage{0}; // dtrack_* flags + bool plain{true}; // a plain track, its speed and curve checked; a switch or a crossing has the paths and the + // speeds of its position, only its unevenness and its damage are + track_uneven::attribute uneven; +}; + +// a vehicle the speed limit of a curve is checked for +struct reference { + std::string name; + derailment::vehicle_state vehicle; +}; +// the default vehicles of derailment.h: an EU07 (4 axles, 80 t, 15.9 m, bogies of 3 m, the centre of gravity of its +// 4.35 m high body) and a 4-axle coach (40 t, 24.5 m, bogies of 2.5 m, 4.05 m high), alone, without a .fiz +std::vector const &default_references(); + +struct settings { + // the speed limit of a track which gives none [km/h]: the top speed of the EU07, the trains the default vehicles + // make up run no faster + double default_velocity{125.0}; + double most_cant{160.0}; // [mm], 150 the most of the Polish rules (Id-1), and a margin + double straight_cant{5.0}; // the cant both ends of a straight track have, past which it's cant without a curve [mm] + double tight_radius{40.0}; // a curve tighter than this [m] + double tight_velocity{40.0}; // with a speed limit over this [km/h] + double most_uneven{30.0}; // the value, up, side and dip of the unevenness [mm]: Id-14 lets the poorest track 28 mm + double most_cross{25.0}; // the cross level [mm]: 20 mm in class C of Id-14 +}; + +enum class kind { + overturning, // the speed limit is over the speed a reference vehicle overturns at + climbing, // or over the one its flange climbs the rail at + overturning_default, // the same with the default speed limit, of a track which gives none + climbing_default, + cant_on_straight, // both ends of a straight track have the same cant + cant_excess, // over the most cant + tight_curve, // a curve tighter than the tight radius with a speed limit over the tight velocity + uneven, // the unevenness out of the plausible range + count_ +}; +std::string_view name_of(kind Kind); + +// what the check found on a track +struct findings { + std::vector kinds; + std::string text; // what the log line tells after the name of the track, empty when nothing + bool empty() const { return kinds.empty(); } +}; +findings check(track const &Track, std::vector const &References = default_references(), settings const &Settings = {}); + +struct summary { + int tracks{0}; // checked + int suspicious{0}; // with a line of their own + std::array(kind::count_)> counts{}; + std::map damage; // the tracks of each value of the damage flags, none for 0 + int count(kind const Kind) const { return counts[static_cast(Kind)]; } +}; + +using sink = std::function; +// checks the tracks, logs a line for each one found suspicious and the summary +summary scan(std::vector const &Tracks, sink const &Log, std::vector const &References = default_references(), settings const &Settings = {}); + +} // namespace track_check