18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 02:42:46 +01:00
Files
maszyna/vehicle/ai/physicslog.cpp
T
jerrrrycho ecf524063e refactor: std::array for the buffers of the physics log line and the seed of the lazy engine, ownP in the if
Sonar's remarks on the new code of this branch: the C arrays of append_fixed(), append_int() and
lazy_random_engine are std::arrays (S5945), the pressure of the ESt3 relays read once is declared in the
if that uses it (S6004), and the thread_local pointer to the lazy engine has the NOSONAR of the one above
it. The snprintf of the infinities and NaNs of the physics log stays, with a NOSONAR: std::format writes
them otherwise than printf's inf and nan(ind). No change of what the code does.
2026-12-01 16:59:35 +01:00

168 lines
6.3 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/.
*/
/*
MaSzyna EU07 locomotive simulator
Copyright (C) 2001-2004 Marcin Wozniak, Maciej Czapkiewicz and others
*/
// the log of the physics of the train the driver runs
#include "stdafx.h"
#include "vehicle/Driver.h"
#include "physics/vehicle.h"
#include "utilities/Globals.h"
#include "utilities/translation.h"
#include "utilities/Logs.h"
#include "utilities/physicslog.h"
#include "vehicle/Train.h"
#include "vehicle/brakereleaser.h"
#include <array>
#include <charconv>
#include <cmath>
#include <cstdio>
#include "vehicle/brakesetting.h"
#include "vehicle/brakingcurve.h"
#include "vehicle/fieldshunting.h"
#include "vehicle/handover.h"
#include "vehicle/signalpassing.h"
#include "vehicle/timetableservice.h"
#include "world/mtable.h"
#include "world/stationnames.h"
#include "vehicle/DynObj.h"
#include "world/Event.h"
#include "world/MemCell.h"
#include "world/RoadOrder.h"
#include "simulation/simulation.h"
#include "simulation/blackboxrecorder.h"
#include "simulation/simulationtime.h"
#include "simulation/simulationtest.h"
#include "world/Track.h"
#include "world/station.h"
#include "input/keyboardinput.h"
#include <algorithm>
#include <format>
#include <ranges>
#include "vehicle/ai/internal.h"
std::string_view TController::physicslog_header() {
// the columns of the readers of these logs, the Hasler tape of the starter among them, stay where they are:
// a new one goes at the end
return "Time[s] Velocity[m/s] Acceleration[m/ss] "
"Coupler.Dist[m] Coupler.Force[N] TractionForce[kN] FrictionForce[kN] BrakeForce[kN] "
"BrakePress[MPa] PipePress[MPa] MotorCurrent[A] "
"MCP SCP BCP LBP Direction Command CVal1 CVal2 "
"Security Wheelslip "
"EngineTemp[Deg] OilTemp[Deg] WaterTemp[Deg] WaterAuxTemp[Deg] "
"VehicleVelocity[km/h]"
"\r\n";
}
namespace {
// a number of the physics log as `<< std::fixed << std::setprecision( 4 )` writes it into a stream of the C locale, which
// is printf's "%.4f": std::to_chars gives the same digits, rounded to the nearest as printf rounds them, without the
// stream and the locale. infinities and NaNs (printf's inf, nan(ind)) and numbers too long for the buffer go through
// snprintf with the same format
void append_fixed( std::string &Line, double const Value ) {
std::array<char, 128> buffer;
if( std::isfinite( Value ) ) {
auto const result { std::to_chars( buffer.data(), buffer.data() + buffer.size(), Value, std::chars_format::fixed, 4 ) };
if( result.ec == std::errc{} ) {
Line.append( buffer.data(), result.ptr );
return;
}
}
auto const length { std::snprintf( nullptr, 0, "%.4f", Value ) }; // NOSONAR: printf's own text of inf and NaN, which std::format writes otherwise
std::string text( static_cast<std::size_t>( length ) + 1, '\0' );
std::snprintf( text.data(), text.size(), "%.4f", Value ); // NOSONAR: printf's own text of inf and NaN, which std::format writes otherwise
text.resize( static_cast<std::size_t>( length ) );
Line += text;
}
void append_int( std::string &Line, int const Value ) {
std::array<char, 16> buffer;
auto const result { std::to_chars( buffer.data(), buffer.data() + buffer.size(), Value ) };
Line.append( buffer.data(), result.ptr );
}
} // namespace
std::string TController::physicslog_line() const {
// the columns of physicslog_header(), separated by spaces; the numbers as a stream with std::fixed and std::setprecision( 4 ) wrote them
std::string line;
line.reserve( 320 );
auto const number = [ &line ]( double const Value ) {
append_fixed( line, Value );
line += ' '; };
auto const integer = [ &line ]( int const Value ) {
append_int( line, Value );
line += ' '; };
number( ElapsedTime );
number( fabs( 11.31 * occupied->WheelDiameter() * occupied->nrot() ) );
number( mvControlling->AccS() );
number( occupied->Couplers[ 1 ].Dist );
number( occupied->Couplers[ 1 ].CForce );
number( occupied->Ft() );
number( occupied->Ff );
number( occupied->Fb() );
number( occupied->brakes.cylinderPressure );
number( occupied->brakePipe.Pressure() );
number( mvControlling->Im );
integer( int( mvControlling->MainCtrlPos() ) );
integer( int( mvControlling->ScndCtrlPos() ) );
number( occupied->brakes.HandlePositionSmooth() );
number( occupied->brakes.IndependentPosition() );
integer( int( mvControlling->DirActive ) );
line += ( occupied->CommandIn.Text.empty() ? "none" : occupied->CommandIn.Text.c_str() );
line += ' ';
number( occupied->CommandIn.Value1 );
number( occupied->CommandIn.Value2 );
integer( int( mvControlling->SecuritySystem.is_blinking() ) );
integer( int( mvControlling->SlippingWheels() ) );
number( mvControlling->diesel.heat.Ts );
number( mvControlling->diesel.heat.To );
number( mvControlling->diesel.heat.temperatura1 );
number( mvControlling->diesel.heat.temperatura2 );
// vehicle speed [km/h], the second column is the wheel speed, which stays at 0 while the wheels are locked in a skid
append_fixed( line, occupied->V() * 3.6 );
line += "\r\n";
return line;
}
void TController::begin_physicslog()
{
// physicslog/<vehicle>.dat, written every two seconds along with the logs of the other drivers
#if LOGPRESS == 0
PhysicsLogs.begin( VehicleName, physicslog_header() );
#endif
#if LOGPRESS == 1
PhysicsLogs.begin( VehicleName, "t\tVel\tAcc\tPP\tVVP\tBP\tBVP\tCVP\n" );
#endif
PhysicsLogBegun = true;
}
void TController::PhysicsLog()
{ // zapis logu - na razie tylko wypisanie parametrów
#if LOGPRESS == 0
PhysicsLogs.append( VehicleName, physicslog_line() );
#endif
#if LOGPRESS == 1
std::ostringstream line;
line << ElapsedTime << "\t" << fabs(11.31 * occupied->WheelDiameter() * occupied->nrot())
<< "\t";
line << mvControlling->AccS() << "\t";
line << occupied->brakePipe.Pressure() << "\t" << occupied->brakes.controlReservoirPressure << "\t"
<< occupied->brakes.cylinderPressure << "\t";
line << occupied->brakes.reservoirAir << "\t" << occupied->brakes.distributor->GetCRP() << "\n";
PhysicsLogs.append( VehicleName, line.str() );
#endif
};