18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 06:35:21 +01:00
Files
maszyna/physics/consistbraking.h
jerrrrycho 1996301a7b perf(physics): the MED log is tried once per vehicle when it can't be opened
A vehicle blending ED and EP brakes (ConsistBrakeBlending) opens MEDLOGS/<name>_<n>.csv once it brakes
while moving. Without the MEDLOGS folder, which the game doesn't come with, the open failed and was tried
again on every step of the braking: a path built and a CreateFile call each time, with <n> counted up.
The failure is now remembered and the log isn't tried again for that vehicle. With the folder there the
first attempt opens it, and what is written is the same as before.

Tomaszewo (raj40821) 600 s in --test, seed 12345: OpenLog was 0.18% of the main thread there (few ED
consists brake in it), the run time is within noise (sim 268.3 s median of 3 before, 269.8 s after);
the 14 physics logs and the event log are byte-identical, unit tests 1205/1205 with exact golden traces.
2026-11-30 03:35:29 +01:00

70 lines
2.7 KiB
C++

/*
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/.
*/
// the braking of a consist driven by induction motors as a whole: the vehicle with the driver blends the electrodynamic
// (ED) brakes of the consist with its electropneumatic (EP) ones (the MED of the .fiz), and sets the holding brake.
// it works on the physics of the vehicles alone, given in the order of the consist from the occupied cab
#pragma once
#include <array>
#include <fstream>
#include <string>
#include <string_view>
#include <vector>
namespace physics
{
class Vehicle;
class ConsistBrakeBlending
{
public:
// what the blending needs to know beside the vehicles
struct Inputs
{
// the vehicle whose controller an unpowered lead vehicle (a driving trailer) follows, nullptr when none
Vehicle const *owner{nullptr};
bool aiDriving{false}; // the AI drives: it brakes the locomotive brake of a TRAXX-like handle itself
};
// the step of the blending, from the lead vehicle over its consist (the lead among them). returns the most
// deceleration of the service braking of the consist, which the driver goes by
double Update(Vehicle &Lead, std::vector<Vehicle *> const &Consist, Inputs const &Given, double dt);
// the figures of the last step for the debug screen, in the order of Labels
static constexpr std::array<std::string_view, 8> Labels{"masa: ", "amax: ", "Fzad: ", "FmPN: ", "FmED: ", "FrED: ", "FzPN: ", "nPrF: "};
std::array<double, 8> const &Figures() const { return figures; }
private:
struct Step;
static void SetControl(Step &State, Vehicle &Lead, Inputs const &Given, double dt);
static void SumForces(Step &State, std::vector<Vehicle *> const &Consist);
static void ComputeDeceleration(Step &State, Vehicle const &Lead);
static void UpdateShuntMode(Step &State, Vehicle &Lead);
static void ComputeDemand(Step &State, Vehicle &Lead, double dt);
void OpenLog(Step const &State, Vehicle const &Lead);
void ComputeElectrodynamicDemand(Step &State, Vehicle &Lead, double dt) const;
static void DistributeForces(Step &State, Vehicle const &Lead, std::vector<Vehicle *> const &Consist, double dt);
void Record(Step const &State, Vehicle const &Lead, std::vector<Vehicle *> const &Consist, double dt);
double lastEdDemand{0.0}; // the demand of the ED brake of the step before [N]
std::array<double, 8> figures{};
// the log of the braking (MEDLOGS/), opened while the consist brakes moving
std::ofstream log;
double logTime{0.0};
double logInactiveTime{0.0};
int logCount{0};
bool logUnavailable{false}; // the log couldn't be opened (no MEDLOGS folder): it isn't tried again
};
} // namespace physics