18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 18:47:06 +01:00
Files
maszyna/tests/unit/invariants.cpp
T
Hirek ef8a1fdbfd refactor(physics): six more of what the rest only reads are private, under names of their own
The mass of the empty vehicle, the braking force, the outcome of a derailment,
the departure signal, the traction mode of a dual-mode vehicle and the moment
of the motors are private to physics::Vehicle like the 53 before them, read
through Mass(), Fb(), Derailed(), DepartureSignal(), TractionMode() and Mm().
Inside the class they are emptyMass, brakingForce, derailOutcome,
departureSignalOn, tractionModeSetting and motorMoment, as the plain camelCase
names were taken by locals there.

No change in behaviour: the golden traces recorded on dfb43c4f match bit for bit.
2026-11-29 13:23:53 +01:00

164 lines
10 KiB
C++

#include "stdafx.h"
#include "invariants.h"
#include <cmath>
#include <cstdio>
#include <initializer_list>
#include <utility>
namespace test {
namespace {
std::string values(std::initializer_list<std::pair<char const *, double>> Values) {
std::string result;
for (auto const &[name, value] : Values) {
result += std::format("{}{}={:.6g}", result.empty() ? "" : " ", name, value);
}
return result;
}
bool electric(physics::Vehicle const &M) {
return M.EngineType == TEngineType::ElectricSeriesMotor || M.EngineType == TEngineType::ElectricInductionMotor;
}
double max_brake_press(physics::Vehicle const &M) {
auto result{0.0};
for (auto const press : M.brakes.config.maxCylinderPressure) {
result = std::max(result, press);
}
return result;
}
// the margin over a limit which a step of the simulation may overshoot it by
constexpr double pressure_margin{0.05};
// the highest acceleration of a vehicle, 5 g, which a collision goes well over
constexpr double highest_acceleration{5.0 * 9.81};
} // namespace
std::vector<invariant> const &invariants() {
static std::vector<invariant> const rules{
{"every value a number",
[](auto const &M, double, auto const *) {
std::initializer_list<double> const values{M.V(), M.AccS(), M.Ft(), M.Fb(), M.Im, M.Itot, M.EngineVoltage,
M.enrot, M.brakePipe.Pressure(), M.brakes.cylinderPressure, M.brakes.independentCylinderPressure, M.mainPipe.Pressure(), M.air.MainReservoirPressure(), M.lowVoltage.BatteryVoltage()};
return std::ranges::all_of(values, [](double const Value) { return std::isfinite(Value); });
},
[](auto const &M) {
return values({{"V", M.V()}, {"AccS", M.AccS()}, {"Ft", M.Ft()}, {"Fb", M.Fb()}, {"Im", M.Im}, {"Itot", M.Itot}, {"EngineVoltage", M.EngineVoltage},
{"enrot", M.enrot}, {"PipePress", M.brakePipe.Pressure()}, {"BrakePress", M.brakes.cylinderPressure}, {"LocBrakePress", M.brakes.independentCylinderPressure},
{"ScndPipePress", M.mainPipe.Pressure()}, {"Compressor", M.air.MainReservoirPressure()}, {"BatteryVoltage", M.lowVoltage.BatteryVoltage()}});
}},
{"brake cylinders between empty and the vehicle's highest pressure",
[](auto const &M, double, auto const *) {
// a step overshoots the setting a little: the EU07's LSt fills its cylinders to 4.0 bar of its 3.8
auto const limit{1.1 * max_brake_press(M) + pressure_margin};
return M.brakes.cylinderPressure >= -pressure_margin && M.brakes.cylinderPressure <= limit && M.brakes.independentCylinderPressure >= -pressure_margin && M.brakes.independentCylinderPressure <= limit;
},
[](auto const &M) { return values({{"BrakePress", M.brakes.cylinderPressure}, {"LocBrakePress", M.brakes.independentCylinderPressure}, {"max(MaxBrakePress)", max_brake_press(M)}}); }},
{"pipes between empty and the highest governor setting",
[](auto const &M, double, auto const *) {
// all the air comes from the main reservoirs, which the game sets up to 10 bar, some (the Impuls) to 12;
// the driver's valve in its filling position joins the brake pipe to them
auto const highest{1.01 * std::max(10.0, M.air.compressor.config.maxPressure) + pressure_margin};
std::initializer_list<double> const pressures{M.brakePipe.Pressure(), M.mainPipe.Pressure(), M.air.MainReservoirPressure()};
return std::ranges::all_of(pressures, [highest](double const Press) { return Press >= -pressure_margin && Press <= highest; });
},
[](auto const &M) { return values({{"PipePress", M.brakePipe.Pressure()}, {"ScndPipePress", M.mainPipe.Pressure()}, {"Compressor", M.air.MainReservoirPressure()}}); }},
{"main reservoir no higher than the compressor's governor",
[](auto const &M, double, auto const *) { return M.air.compressor.config.maxPressure <= 0.0 || M.air.MainReservoirPressure() <= 1.01 * M.air.compressor.config.maxPressure + pressure_margin; },
[](auto const &M) { return values({{"Compressor", M.air.MainReservoirPressure()}, {"MaxCompressor", M.air.compressor.config.maxPressure}}); }},
{"no motor current with the line breaker open",
[](auto const &M, double, auto const *) { return M.lineBreaker.IsClosed() || false == electric(M) || std::abs(M.Im) < 1.0; },
[](auto const &M) { return values({{"Mains", M.lineBreaker.IsClosed()}, {"Im", M.Im}, {"Itot", M.Itot}}); }},
{"no motor current with the line contactors open, but for the dynamic brake",
[](auto const &M, double, auto const *) {
// the dynamic brake closes its own circuit of the motors and the resistors
return M.lineContactors.IsClosed() || M.brakes.electrodynamic.on || M.EngineType != TEngineType::ElectricSeriesMotor || std::abs(M.Im) < 1.0;
},
[](auto const &M) {
return values({{"StLinFlag", M.lineContactors.IsClosed()}, {"DynamicBrakeFlag", M.brakes.electrodynamic.on}, {"Im", M.Im}, {"MainCtrlActualPos", M.MainCtrlActualPos}});
}},
{"no tractive force without power",
[](auto const &M, double, auto const *) { return M.Power >= 0.01 || std::abs(M.Ft()) < 1.0; },
[](auto const &M) { return values({{"Power", M.Power}, {"Ft", M.Ft()}}); }},
{"engine fill between none and full",
[](auto const &M, double, auto const *) { return M.diesel.fill >= 0.0 && M.diesel.fill <= 1.0; },
[](auto const &M) { return values({{"dizel_fill", M.diesel.fill}}); }},
{"diesel engine turning forward, no faster than its top speed",
[](auto const &M, double, auto const *) {
// the governor lets it overshoot a little (the SN61 by 1%)
return M.EngineType != TEngineType::DieselEngine || (M.enrot >= 0.0 && M.enrot <= 1.05 * M.diesel.maxRevolutions);
},
[](auto const &M) { return values({{"enrot", M.enrot}, {"dizel_nmax", M.diesel.maxRevolutions}}); }},
{"battery no higher than its nominal voltage",
[](auto const &M, double, auto const *) { return M.lowVoltage.config.nominalBatteryVoltage <= 0.0 || M.lowVoltage.BatteryVoltage() <= 1.05 * M.lowVoltage.config.nominalBatteryVoltage; },
[](auto const &M) { return values({{"BatteryVoltage", M.lowVoltage.BatteryVoltage()}, {"NominalBatteryVoltage", M.lowVoltage.config.nominalBatteryVoltage}}); }},
{"distance counted as the speed runs",
[](auto const &M, double Time, auto const *Before) {
if (Before == nullptr) {
return true;
}
// the speed of the step is between the speeds at its ends, or a bit off it, as the movement is integrated;
// through a standstill, down to none
auto const dt{Time - Before->time};
auto const covered{(M.DistCounter() - Before->distCounter) * 1000.0};
auto const lowest{M.V() * Before->v > 0.0 ? std::min(std::abs(M.V()), std::abs(Before->v)) : 0.0};
auto const highest{std::max(std::abs(M.V()), std::abs(Before->v))};
return covered >= lowest * dt * 0.9 - 0.01 && covered <= highest * dt * 1.1 + 0.01;
},
[](auto const &M) { return values({{"V", M.V()}, {"DistCounter", M.DistCounter()}}); }},
{"no acceleration over 5 g",
[](auto const &M, double Time, auto const *Before) {
return Before == nullptr || std::abs(M.V() - Before->v) <= highest_acceleration * (Time - Before->time) + 0.01;
},
[](auto const &M) { return values({{"V", M.V()}, {"AccS", M.AccS()}}); }},
{"hand brake applied, the wheels braked on the move",
[](auto const &M, double, auto const *) { return false == M.brakes.config.hasHandBrake || M.brakes.HandBrakePosition() == 0 || std::abs(M.V()) < 0.5 || std::abs(M.Fb()) > 0.0; },
[](auto const &M) { return values({{"ManualBrakePos", M.brakes.HandBrakePosition()}, {"V", M.V()}, {"Fb", M.Fb()}}); }},
};
return rules;
}
namespace {
struct excuse {
char const *trace; // a part of the trace's name
char const *invariant;
char const *reason;
};
std::vector<excuse> const excuses{
// what the tests do
{"couplers/collision/", "no acceleration over 5 g", "vehicles run into each other"},
{"controls/collision/", "no acceleration over 5 g", "vehicles run into each other"},
{"couplers/collision/", "distance counted as the speed runs", "the buffers throw the vehicle back within a step"},
{"switches_live/", "no acceleration over 5 g", "sampled by hand, a second apart in the trace for 2 s run"},
{"switches_live/", "distance counted as the speed runs", "sampled by hand, a second apart in the trace for 2 s run"},
{"brake handles/", "main reservoir no higher than the compressor's governor", "the test holds the main reservoir at 8 bar"},
{"local brake/", "main reservoir no higher than the compressor's governor", "the test holds the main reservoir at 8 bar"},
{"brake cycle/at speed/", "brake cylinders between empty and the vehicle's highest pressure", "the rapid step of the valves at speed"},
// the vehicles as they are
{"elf/2", "main reservoir no higher than the compressor's governor",
"the units' reservoirs share the supply pipe: the A car's governor stops at 9 bar, the C car's at 8.5"},
{"sm03_v1/", "brake cylinders between empty and the vehicle's highest pressure",
"its K valve fills the cylinders to the auxiliary reservoir's pressure (4.27 bar), over MaxBP 3.5"},
{"eu04_v1/", "brake cylinders between empty and the vehicle's highest pressure",
"its K valve fills the cylinders to the auxiliary reservoir's pressure (3.19 bar), over MaxBP 2.8"},
{"sn61", "brake cylinders between empty and the vehicle's highest pressure",
"its ESt3d_PZZ valve fills the cylinders to the auxiliary reservoir's pressure in an emergency (4.6 bar), over MaxBP 3.8"},
{"sm03_v1/", "diesel engine turning forward, no faster than its top speed", "a lower gear at speed drives the engine over its top speed, by 10%"},
{"wmb10_v1/", "diesel engine turning forward, no faster than its top speed", "a lower gear at speed drives the engine over its top speed, by 25%"},
{"controls/full physics/", "no acceleration over 5 g", "the couplers' slack taken up as the train starts throws the wagons about at up to 5.7 g"},
{"controls/full physics/", "distance counted as the speed runs", "the couplers' slack taken up as the train starts throws the wagons back within a step"},
};
} // namespace
bool excused(std::string_view const Trace, std::string_view const Invariant) {
return std::ranges::any_of(excuses, [&](excuse const &Excuse) { return Excuse.invariant == Invariant && Trace.find(Excuse.trace) != std::string_view::npos; });
}
} // namespace test