mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-02 07:22:42 +01:00
75efb467db
Review of #574: - The sub-steps of one train and its exchange in them are helpers (train_substeps(), exchange_train()) taken by both the serial loop and the cluster job (exchange_cluster()), which gets one context struct (hose_exchange) by reference, so std::function keeps it in its small buffer. Picking the trains (hose_layout()) is its own function too. - The runner of the clusters gets the number of vehicles exchanged and wakes the pool only for at least 64 of them a thread; below that the clusters go one after another. The neighbours and facts take a thread for each 64 vehicles and stay in the main thread below 128. With performance.multithreading no both still go one after another. - The neighbours and facts moved out of update() into update_neighbours_and_facts() and take_facts(); the inner loop variable is side, no longer shadowing end. The vehicles outside the trains of lay_out_clusters() are gathered by outside_trains(). vehicle_facts::none is nowhere, the none of lay_out_clusters() no_cluster. - The test lambdas capture explicitly and the runner of the test takes its job as auto. Results unchanged: the clusters still work out the same sub-steps and take the trains in the same order.
267 lines
11 KiB
C++
267 lines
11 KiB
C++
#include "stdafx.h"
|
|
#include "doctest.h"
|
|
|
|
#include "physics/vehicle.h"
|
|
#include "physics/brakes.h"
|
|
#include "utilities/Globals.h"
|
|
#include "utilities/utilities.h"
|
|
#include "consist.h"
|
|
#include "vehicle.h"
|
|
|
|
#include <cmath>
|
|
#include <functional>
|
|
#include <random>
|
|
#include <vector>
|
|
|
|
// the air exchange through the brake hoses between the vehicles of a train (physics::Vehicle::UpdatePipeCouplers()).
|
|
// taken in one explicit step of 0.01 s, the hose between two short freight wagons carried twice the air which evens
|
|
// out their pipes: the pipes overshot each other in turns, and an ESt3 distributor saw the pipe pressure up to
|
|
// 0.28 bar off what was in it, lapped at the first stage of braking and kept the AI's train from leaving
|
|
// (eu07/maszyna#368)
|
|
|
|
namespace {
|
|
|
|
using test::definition;
|
|
|
|
// a freight train standing charged: a locomotive and Wagons empty two-axle freight wagons with ESt3, set to G
|
|
void freight_train(test::consist &Train, int const Wagons) {
|
|
Train.add(definition::fiz("st44_v2/m62-1151"));
|
|
for (int i = 0; i < Wagons; ++i) {
|
|
auto &wagon{Train.add(definition::fiz("3wl_v2/3wl"))};
|
|
wagon->BrakeDelaySwitch(bdelay_G);
|
|
REQUIRE(wagon->brakes.config.distributorType == TBrakeValve::ESt3);
|
|
REQUIRE(wagon->brakes.config.pipeSection == doctest::Approx(0.792)); // the pipe of a freight wagon
|
|
}
|
|
Train[0].hold_brake_pipe(5.0); // the locomotive's driver's valve in the running position
|
|
}
|
|
|
|
// a passenger train standing charged: a locomotive and Cars passenger cars with ESt4, set to R
|
|
void passenger_train(test::consist &Train, int const Cars) {
|
|
Train.add(definition::fiz("st44_v2/m62-1151"));
|
|
for (int i = 0; i < Cars; ++i) {
|
|
auto &car{Train.add(definition::fiz("11xa_v2/93a"))};
|
|
REQUIRE(car->brakes.config.pipeSection == doctest::Approx(0.507)); // the pipe of a passenger car
|
|
}
|
|
Train[0].hold_brake_pipe(5.0);
|
|
}
|
|
|
|
// the time [s] until the brake pipe of the train's last vehicle is 0.5 bar down, once the locomotive holds its pipe
|
|
// at Pressure [bar], the physics stepping by Dt
|
|
double application_time(test::consist &Train, double const Pressure, double const Dt) {
|
|
auto &last{Train[Train.size() - 1]};
|
|
auto const start{last->brakePipe.Pressure()};
|
|
Train[0].hold_brake_pipe(Pressure);
|
|
auto time{0.0};
|
|
while (last->brakePipe.Pressure() > start - 0.5 && time < 60.0) {
|
|
Train.step({}, Dt);
|
|
time += Dt;
|
|
}
|
|
return time;
|
|
}
|
|
|
|
// the same train standing a minute, then braked from the locomotive, all of it in steps of Dt
|
|
template <typename Builder>
|
|
double application_time(Builder Train, int const Vehicles, double const Pressure, double const Dt) {
|
|
test::consist train;
|
|
Train(train, Vehicles);
|
|
train.run(60.0, {}, Dt);
|
|
return application_time(train, Pressure, Dt);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("brake pipe: the distributors of a standing freight train see the pressure in the pipe, and none of them brakes") {
|
|
test::consist train;
|
|
freight_train(train, 37);
|
|
// standing a while, the locomotive keeping the pipe charged
|
|
train.run(300.0);
|
|
for (std::size_t i = 1; i < train.size(); ++i) {
|
|
CAPTURE(i);
|
|
auto &wagon{train[i].physics()};
|
|
// the distributor's valve chamber follows the pressure the distributor works with: the pipe's own
|
|
CHECK(std::abs(wagon.brakes.distributor->GetVRP() - wagon.brakePipe.Pressure()) < 0.005);
|
|
CHECK(wagon.brakePipe.Pressure() > 4.9);
|
|
// released, as the AI wants every vehicle before it starts (TController::CheckVehicles())
|
|
CHECK((wagon.brakes.distributor->GetBrakeStatus() & (b_on | b_hld)) == 0);
|
|
CHECK(wagon.brakes.cylinderPressure < 0.1);
|
|
}
|
|
// the exchange between the 14 m wagons takes 3 sub-steps of a step of 0.01 s
|
|
CHECK(train[1]->PipeCouplersSubsteps(0.01) == 3);
|
|
}
|
|
|
|
TEST_CASE("brake pipe: an application from the locomotive reaches the end of a freight train as with a short step") {
|
|
// a light application, the locomotive's pipe 1 bar down: the last wagon's pipe 0.5 bar down as soon as in the
|
|
// step of 0.001 s, at which the exchange through the hoses converges
|
|
auto const converged{application_time(freight_train, 37, 4.0, 0.001)};
|
|
auto const usual{application_time(freight_train, 37, 4.0, 0.01)};
|
|
CAPTURE(converged);
|
|
CAPTURE(usual);
|
|
CHECK(converged < 60.0);
|
|
CHECK(usual == doctest::Approx(converged).epsilon(0.1));
|
|
// a full service application
|
|
auto const convergedfull{application_time(freight_train, 37, 3.5, 0.001)};
|
|
auto const usualfull{application_time(freight_train, 37, 3.5, 0.01)};
|
|
CAPTURE(convergedfull);
|
|
CAPTURE(usualfull);
|
|
CHECK(usualfull == doctest::Approx(convergedfull).epsilon(0.1));
|
|
}
|
|
|
|
TEST_CASE("brake pipe: a passenger train, whose exchange was stable in one step, brakes as with a short step") {
|
|
test::consist train;
|
|
passenger_train(train, 10);
|
|
train.run(300.0);
|
|
for (std::size_t i = 1; i < train.size(); ++i) {
|
|
CAPTURE(i);
|
|
auto &car{train[i].physics()};
|
|
CHECK(std::abs(car.brakes.distributor->GetVRP() - car.brakePipe.Pressure()) < 0.005);
|
|
CHECK((car.brakes.distributor->GetBrakeStatus() & (b_on | b_hld)) == 0);
|
|
CHECK(car.brakes.cylinderPressure < 0.1);
|
|
}
|
|
auto const converged{application_time(passenger_train, 10, 3.5, 0.001)};
|
|
auto const usual{application_time(passenger_train, 10, 3.5, 0.01)};
|
|
CAPTURE(converged);
|
|
CAPTURE(usual);
|
|
CHECK(usual == doctest::Approx(converged).epsilon(0.05));
|
|
}
|
|
|
|
namespace {
|
|
|
|
// Global.physics_sleepheld, off by default, on for the test it's made in
|
|
struct sleepheld_on {
|
|
bool const was{Global.physics_sleepheld};
|
|
sleepheld_on() { Global.physics_sleepheld = true; }
|
|
~sleepheld_on() { Global.physics_sleepheld = was; }
|
|
sleepheld_on(sleepheld_on const &) = delete;
|
|
sleepheld_on &operator=(sleepheld_on const &) = delete;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("brake pipe: the trains of the hose exchange kept from one step to the next come out as worked out in every step") {
|
|
// a locomotive with five freight wagons on a gradient, charged and braked; the last two wagons uncoupled, then the train
|
|
// released. with Global.physics_sleepheld the wagons held by their brakes fall asleep and wake on the way (a vehicle
|
|
// asleep doesn't step its pipe, so it ends the trains of the hoses), and the uncoupling splits a train in two
|
|
sleepheld_on const sleepheld;
|
|
test::track slope;
|
|
slope.gradient = 0.01;
|
|
auto const build = [](test::consist &Train) {
|
|
Train.add(definition::fiz("st44_v2/m62-1151"));
|
|
for (int i = 0; i < 5; ++i) {
|
|
Train.add(definition::fiz("3wl_v2/3wl"));
|
|
}
|
|
Train[0].hold_brake_pipe(5.0);
|
|
};
|
|
// each train with an engine of its own for the draws of its physics, seeded alike
|
|
std::mt19937 keptdraws{12345}; // NOSONAR: randomness of the simulation, not of anything security-relevant
|
|
std::mt19937 freshdraws{12345}; // NOSONAR: randomness of the simulation, not of anything security-relevant
|
|
test::consist kept;
|
|
test::consist fresh;
|
|
fresh.keep_hose_trains(false);
|
|
auto const both = [&keptdraws, &freshdraws, &kept, &fresh](auto const &Action) {
|
|
set_thread_random_engine(&keptdraws);
|
|
Action(kept);
|
|
set_thread_random_engine(&freshdraws);
|
|
Action(fresh);
|
|
set_thread_random_engine(nullptr);
|
|
};
|
|
both(build);
|
|
// the vehicles which came out of a step differing between the two trains, and the times a vehicle fell asleep or woke
|
|
int differing{0};
|
|
int sleeps{0};
|
|
int wakes{0};
|
|
std::vector<bool> awake(kept.size(), true);
|
|
auto const run = [&both, &slope, &kept, &fresh, &differing, &sleeps, &wakes, &awake](double const Seconds) {
|
|
for (auto step = std::lround(Seconds / 0.01); step > 0; --step) {
|
|
both([&slope](test::consist &Train) { Train.step(slope); });
|
|
for (std::size_t i = 0; i < kept.size(); ++i) {
|
|
auto const &a{kept[i].physics()};
|
|
auto const &b{fresh[i].physics()};
|
|
if (a.brakePipe.Pressure() != b.brakePipe.Pressure() || a.brakePipe.reservoir->P() != b.brakePipe.reservoir->P() ||
|
|
a.mainPipe.Pressure() != b.mainPipe.Pressure() || a.mainPipe.reservoir->P() != b.mainPipe.reservoir->P() ||
|
|
a.brakes.cylinderPressure != b.brakes.cylinderPressure) {
|
|
++differing;
|
|
}
|
|
if (awake[i] != a.PhysicActivation()) {
|
|
++(a.PhysicActivation() ? wakes : sleeps);
|
|
awake[i] = a.PhysicActivation();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
run(60.0);
|
|
both([](test::consist &Train) { Train[0].hold_brake_pipe(3.5); });
|
|
run(30.0);
|
|
both([](test::consist &Train) { REQUIRE(Train[3]->Dettach(VehicleEnd::rear)); });
|
|
run(60.0);
|
|
both([](test::consist &Train) { Train[0].hold_brake_pipe(5.0); });
|
|
run(60.0);
|
|
CHECK(differing == 0);
|
|
CHECK(sleeps > 0);
|
|
CHECK(wakes > 0);
|
|
}
|
|
|
|
TEST_CASE("brake pipe: the trains of the hose exchange taken a cluster at a time, in any order, come out as one after another") {
|
|
// a wagon which didn't step its pipe (its physics sleeps) between two locomotives ends the trains of both, which go
|
|
// together in one cluster; a locomotive with two wagons further on, on the same track, is another one
|
|
auto const build = [](test::consist &Train) {
|
|
Train.add(definition::fiz("st44_v2/m62-1151"));
|
|
Train.add(definition::fiz("3wl_v2/3wl"));
|
|
Train.add(definition::fiz("st44_v2/m62-1151"));
|
|
Train.add(definition::fiz("st44_v2/m62-1151"), 0, Coupling::none, 0.0, 50.0);
|
|
Train.add(definition::fiz("3wl_v2/3wl"));
|
|
Train.add(definition::fiz("3wl_v2/3wl"));
|
|
Train[0].hold_brake_pipe(5.0);
|
|
Train[2].hold_brake_pipe(3.5);
|
|
Train[3].hold_brake_pipe(4.0);
|
|
};
|
|
// each train with an engine of its own for the draws of its physics, seeded alike
|
|
std::mt19937 serialdraws{12345}; // NOSONAR: randomness of the simulation, not of anything security-relevant
|
|
std::mt19937 clusterdraws{12345}; // NOSONAR: randomness of the simulation, not of anything security-relevant
|
|
auto const with = [](std::mt19937 &Engine, auto const &Action) {
|
|
set_thread_random_engine(&Engine);
|
|
Action();
|
|
set_thread_random_engine(nullptr);
|
|
};
|
|
test::consist serial;
|
|
test::consist clusters;
|
|
with(serialdraws, [&build, &serial] { build(serial); serial.run(10.0); });
|
|
with(clusterdraws, [&build, &clusters] { build(clusters); clusters.run(10.0); });
|
|
// the exchange alone, as vehicle_table::update_brake_pipes() makes it: the clusters backwards, the last one first
|
|
physics::hose_trains_cache serialcache;
|
|
physics::hose_trains_cache clustercache;
|
|
std::size_t most{0};
|
|
// all the clusters whatever the amount of work, which vehicle_table's runner would leave to one thread here
|
|
clustercache.run = [&most](std::size_t const Count, std::size_t, auto const &Work) {
|
|
most = std::max(most, Count);
|
|
for (auto index = Count; index > 0; --index) {
|
|
Work(index - 1);
|
|
}
|
|
};
|
|
auto const exchange = [](test::consist &Train, physics::hose_trains_cache &Cache) {
|
|
std::vector<physics::Vehicle *> vehicles;
|
|
for (std::size_t i = 0; i < Train.size(); ++i) {
|
|
auto &physics{Train[i].physics()};
|
|
physics.brakePipe.SetCouplersPending(i != 1);
|
|
physics.mainPipe.SetCouplersPending(i != 1);
|
|
vehicles.push_back(&physics);
|
|
}
|
|
physics::UpdatePipeCouplers(vehicles, 0.01, Cache);
|
|
};
|
|
for (int step = 0; step < 500; ++step) {
|
|
exchange(serial, serialcache);
|
|
exchange(clusters, clustercache);
|
|
}
|
|
for (std::size_t i = 0; i < serial.size(); ++i) {
|
|
CAPTURE(i);
|
|
CHECK(serial[i]->brakePipe.Pressure() == clusters[i]->brakePipe.Pressure());
|
|
CHECK(serial[i]->brakePipe.reservoir->P() == clusters[i]->brakePipe.reservoir->P());
|
|
CHECK(serial[i]->mainPipe.Pressure() == clusters[i]->mainPipe.Pressure());
|
|
CHECK(serial[i]->mainPipe.reservoir->P() == clusters[i]->mainPipe.reservoir->P());
|
|
}
|
|
// the air went through the sleeping wagon's pipe both ways
|
|
CHECK(serial[1]->brakePipe.reservoir->P() < 4.9);
|
|
CHECK(serial[1]->brakePipe.reservoir->P() > 3.6);
|
|
// the two locomotives with the sleeping wagon in one cluster, the train further on in the other
|
|
CHECK(most == 2);
|
|
}
|