18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 11:56:50 +01:00
Files
maszyna/tests/unit/invariants.cpp
T
Hirek 16568d5695 refactor(physics): the brake valves, cylinders, pressures and positions in physics::Braking
The rest of the braking of physics::Vehicle moves into brakes, with English
names (physics/components/braking.h):

  the valves      Hamulec distributor, Handle driverValve,
                  LocHandle independentValve
  the definition  BrakeSystem kind, BrakeSubsystem subsystem, BrakeValve /
                  BrakeValveSize / BrakeValveParams distributorType / Size /
                  Parameters, BrakeHandle / BrakeLocHandle driverValveType /
                  independentValveType, the Handle_* driverValveSettings,
                  LocalBrake localKind, MaxBrakePress maxCylinderPressure,
                  LoadFlag loadSetting, P2FTrans pressureToForce, NBpA
                  blocksPerAxle, High/Low/DeltaPipePress pipeHigh/LowPressure
                  / pipePressureRange, CntrlPipePress controlReservoirPressure,
                  Spg pipeSection, RapidMult / RapidVel rapidRatio / rapidSpeed,
                  BrakeDelay delays, BrakeCtrlPosNo handlePositions, ...
  cylinders       BrakeCylNo / Radius / Dist / Mult / Spring, BrakeSlckAdj,
                  BrakeRigEff: count, radius, stroke, leverRatio, springForce,
                  slackAdjusterForce, riggingEfficiency
  emergencyValve  EmergencyValveOn / Off / Open / Area / Flow
  pipeLock        LockPipeOn / Off, HandleUnlock, LockPipe, UnlockPipe
  the state       BrakePress cylinderPressure, LocBrakePress
                  independentCylinderPressure, EqvtPipePress
                  equivalentPipePressure, Volume reservoirAir, dpBrake / dpPipe /
                  dpMainValve / dpLocalValve brakeFlow / pipeFlow /
                  driverValveFlow / independentValveFlow, BrakeCtrlPos(R, 2)
                  handlePosition(Real), handleCapPosition, fBrakeCtrlPos
                  handlePositionSmooth, LocalBrakePosA(EIM) independentPosition
                  (Eim), BrakeStatus status, AlarmChainFlag alarmChain,
                  BrakeDelayFlag / BrakeDelays delaySetting / availableDelays,
                  BrakeOpModeFlag / BrakeOpModes operatingMode /
                  availableOperatingModes, UnitBrakeForce unitForce, ...

Renamed in the files of the vehicle itself, and elsewhere only where reached
through -> or .: the driver keeps its own BrakeSystem, the editor its Handle,
the radio its Volume, and the brake valve classes (brakes.*, oerlikonest.*)
their fields of the same names. The C arrays (MaxBrakePress, BrakeCylMult,
the universal buttons) are std::arrays of the same values; the bits of status,
delaySetting and operatingMode stay ints for now.

No change in behaviour: 1149/1149 tests, golden traces exact against staging
62f7a29b.
2026-11-29 01:48:33 +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.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.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.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.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.maxPressure <= 0.0 || M.air.mainReservoirPressure <= 1.01 * M.air.compressor.maxPressure + pressure_margin; },
[](auto const &M) { return values({{"Compressor", M.air.mainReservoirPressure}, {"MaxCompressor", M.air.compressor.maxPressure}}); }},
{"no motor current with the line breaker open",
[](auto const &M, double, auto const *) { return M.Mains || false == electric(M) || std::abs(M.Im) < 1.0; },
[](auto const &M) { return values({{"Mains", M.Mains}, {"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.StLinFlag || M.DynamicBrakeFlag || M.EngineType != TEngineType::ElectricSeriesMotor || std::abs(M.Im) < 1.0;
},
[](auto const &M) {
return values({{"StLinFlag", M.StLinFlag}, {"DynamicBrakeFlag", M.DynamicBrakeFlag}, {"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.NominalBatteryVoltage <= 0.0 || M.BatteryVoltage <= 1.05 * M.NominalBatteryVoltage; },
[](auto const &M) { return values({{"BatteryVoltage", M.BatteryVoltage}, {"NominalBatteryVoltage", M.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.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