18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 04:47:06 +01:00
Files
maszyna/tests/unit/test_fcfuh_default.cpp
T
Hirek f4ef43feec refactor(physics): the definition and the state of the induction motor drive as named fields
The arrays eimc[26] and eimv[21] of InductionMotor, indexed by the eimc_* and
eimv_* constants, become structs with named and described fields. The
definition splits into blocks by what it describes:

  motor      dfic currentPerSlip, dfmax maxSlipFrequency, p polePairs,
             cfu voltsPerHertz, cim torquePerCurrent, icif fieldCurrentRatio
  converter  Uzmax maxLinkVoltage, Uzh brakingLinkVoltage, DU voltageDrop,
             I0 noLoadCurrent, fcfu voltsPerHertz, fcfuH brakingVoltsPerHertz
  traction   F0 startingForce, a1 forceDropPerSpeed, Pmax maxPower,
             Imax maxCurrent
  braking    Fh maxForce, Ph maxPower, Vh0 offSpeed, Vh1 fullSpeed,
             abed cylinderPressureGain, edep maxEpDeceleration

and the state (InductionMotorState) groups the forces, the frequencies, the
values of the motors and the powers: FMAXMAX absoluteMaxForce, Fmax maxForce,
Fful fullForce, Fr force, Fzad forceDemand, ks slipShare, df slipFrequency,
fp supplyFrequency, fkr criticalFrequency, Uzsmax linkVoltage, U voltage,
pole flux, Ic torqueCurrent, If fieldCurrent, M torque, Ipoj lineCurrent,
Imax currentLimit, Pmax powerLimit, Pm mechanicalPower, Pe electricalPower,
eta efficiency.

The old comments of the constants took the "h" entries (Uzh, fcfuH, Fh, Ph,
Vh0, Vh1) for field weakening; the code uses them for the electrodynamic
brake, and the new names say so. The .fiz keys don't change. The constants
eimc_* / eimv_* are gone from physics/vehicletypes.h, and the labels of the
debug panel (TMoverParameters::eimc_labels, eimv_labels) move into
InductionMotor::ConfigListing() and StateListing().

One change on the screen: the eimc column of the debug panel (the engine
section of an induction motor vehicle) showed the labels from "Uzmax:" on
next to the wrong values (shifted by one entry, from "F0:" on by two), and
never showed abed and eped. It now shows each label with its own value. The
eimv column is as it was.

No change in the simulation: 1144/1144 tests, golden traces exact against
staging 62f7a29b.
2026-11-28 16:29:17 +01:00

154 lines
6.0 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "vehicle.h"
#include "utilities/utilities.h"
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
using test::definition;
using test::vehicle;
// the figure of the voltage by the frequency of an induction motor drive in the dynamic brake (fcfuH) takes the
// one of the traction (fcfu) when the definition has none. the loader took it by writing fcfu out with
// std::to_string() and reading the text back as the default of fcfuH (eu07/maszyna#99); it assigns it now
namespace {
// a figure read from a line the way TMoverParameters::LoadFIZ_EngineInductionMotor() reads fcfu
double read_fcfu(std::string const &Text) {
double fcfu{0.0};
extract_value(fcfu, "fcfu", "fcfu=" + Text, "");
return fcfu;
}
// fcfuH missing, the way the loader took it before: the figure written out with six decimals in the C locale
// (the game never sets another) and read back
double fcfuh_before(double const Fcfu) {
double fcfuh{-1.0};
extract_value(fcfuh, "fcfuH", "", std::to_string(Fcfu));
return fcfuh;
}
// the bits of the value, which tell -0.0 from 0.0 too
std::uint64_t bits(double const Value) {
std::uint64_t result;
std::memcpy(&result, &Value, sizeof(result));
return result;
}
// the value with all its digits, for the message
std::string exact(double const Value) {
char text[64];
std::snprintf(text, sizeof(text), "%.17g", Value);
return text;
}
// a generator of its own (splitmix64), for the same numbers on every platform
struct numbers {
std::uint64_t state{0x2545f4914f6cdd1dull};
std::uint64_t next() {
auto value{state += 0x9e3779b97f4a7c15ull};
value = (value ^ (value >> 30)) * 0xbf58476d1ce4e5b9ull;
value = (value ^ (value >> 27)) * 0x94d049bb133111ebull;
return value ^ (value >> 31);
}
};
// the decimal text of Integer / 10^Decimals, as a definition could hold it
std::string decimal(std::uint64_t const Integer, int const Decimals, bool const Negative) {
auto digits{std::to_string(Integer)};
if (Decimals > 0) {
if (digits.size() <= static_cast<std::size_t>(Decimals)) {
digits.insert(0, Decimals + 1 - digits.size(), '0');
}
digits.insert(digits.size() - Decimals, ".");
}
return (Negative ? "-" : "") + digits;
}
} // namespace
TEST_CASE("fcfuH: the figure of the traction is the same, assigned or written out and read back, for every figure of up to six decimals") {
// any decimal figure of up to six decimals: the value read from it lies within half a millionth of it, so
// std::to_string() writes the figure itself and the text reads back to the very same value. checked over the
// figures the game's definitions use (33.5 to 81.5) and the ones around them, and over many others, of up
// to six decimals and up to 18 digits, of either sign
auto compared{0};
auto const check{[&](std::string const &Text) {
auto const fcfu{read_fcfu(Text)};
auto const before{fcfuh_before(fcfu)};
if (bits(before) != bits(fcfu)) {
FAIL_CHECK("fcfu=" << Text << " was " << exact(before) << " as fcfuH, now " << exact(fcfu));
}
++compared;
}};
// every figure of up to two decimals, from -10 to 200
for (auto hundredths{-1000}; hundredths <= 20000; ++hundredths) {
check(decimal(static_cast<std::uint64_t>(std::abs(hundredths)), 2, hundredths < 0));
}
// every figure of up to six decimals around the ones of the game's definitions
for (auto const figure : {33.5, 40.1, 43.7, 45.18, 46.8, 47.6, 48.9, 49.0, 81.5}) {
auto const millionths{static_cast<std::int64_t>(figure * 1e6 + 0.5)};
for (auto offset{-2000}; offset <= 2000; ++offset) {
check(decimal(static_cast<std::uint64_t>(millionths + offset), 6, false));
}
}
// figures of 0 to 6 decimals and 1 to 18 digits
numbers random;
for (auto decimals{0}; decimals <= 6; ++decimals) {
for (auto digits{1}; digits <= 18; ++digits) {
std::uint64_t limit{1};
for (auto digit{0}; digit < digits; ++digit) {
limit *= 10;
}
for (auto sample{0}; sample < 400; ++sample) {
check(decimal(random.next() % limit, decimals, (sample & 1) != 0));
}
}
}
// the way of writing it doesn't matter, only the value
for (auto const text : {"4.76e1", "47.60", "+47.6", "0047.6", "476e-1", "0", "-0", "1e15", "-1e-6", "1e300"}) {
check(text);
}
// nor does a figure the stream can't read, which reads as 0
for (auto const text : {"abc", "-", "x47.6"}) {
check(text);
}
CHECK(compared > 70000);
}
TEST_CASE("fcfuH: where the two ways part, figures no definition has") {
// no definition of the game has such a figure (they have one or two decimals, from 33.5 to 81.5). more than
// six decimals: std::to_string() rounded the value to six, the figure is taken as it's written now
CHECK(fcfuh_before(read_fcfu("0.1234567")) == read_fcfu("0.123457"));
CHECK(fcfuh_before(read_fcfu("1e-7")) == 0.0);
CHECK(read_fcfu("1e-7") == 1e-7);
// a figure past the largest value reads as infinite (with MSVC; the largest value with GCC): written out it
// was "inf", which the stream doesn't read, so the dynamic brake got 0 where it gets the traction's figure now
auto const huge{read_fcfu("1e999")};
CHECK(huge > 1e300);
if (std::isinf(huge)) {
CHECK(fcfuh_before(huge) == 0.0);
}
}
TEST_CASE("fcfuH: the loader gives the dynamic brake the traction's figure when the definition has none") {
for (auto const fcfu : {"43.7", "45.18", "49", "0.000001", "1e2"}) {
CAPTURE(fcfu);
// the EU47's definition has fcfu only
vehicle eu47{definition::fiz("e186_v2/eu47").set("Engine", "fcfu", fcfu)};
CHECK(eu47->inductionMotor.converter.voltsPerHertz == read_fcfu(fcfu));
CHECK(bits(eu47->inductionMotor.converter.brakingVoltsPerHertz) == bits(eu47->inductionMotor.converter.voltsPerHertz));
CHECK(bits(eu47->inductionMotor.converter.brakingVoltsPerHertz) == bits(fcfuh_before(read_fcfu(fcfu))));
// its own figure is kept
vehicle own{definition::fiz("e186_v2/eu47").set("Engine", "fcfu", fcfu).set("Engine", "fcfuH", "40.1")};
CHECK(own->inductionMotor.converter.voltsPerHertz == read_fcfu(fcfu));
CHECK(own->inductionMotor.converter.brakingVoltsPerHertz == read_fcfu("40.1"));
}
}