/* 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 brakes and the air: the brake controls, the pipes, the valves, the compressor, the spring brake #include "stdafx.h" #include "physics/vehicle.h" #include "vehicle/DynObj.h" #include "physics/oerlikonest.h" #include "utilities/Globals.h" #include "utilities/Logs.h" #include "utilities/Timer.h" #include "utilities/parser.h" #include "simulation/simulation.h" #include "vehicle/fieldshunting.h" #include #include #include #include #include #include #include #include #include "physics/components/internal.h" // Nastawianie hamulców void physics::Vehicle::BrakeLevelSet(double b) { // ustawienie pozycji hamulca na wartość (b) w zakresie od -2 do BrakeCtrlPosNo // jedyny dopuszczalny sposób przestawienia hamulca zasadniczego if (brakes.HandlePositionSmooth() == b) return; // nie przeliczać, jak nie ma zmiany brakes.SetHandlePositionSmooth(b); if (brakes.HandlePositionSmooth() < brakes.driverValve->GetPos(bh_MIN)) brakes.SetHandlePositionSmooth(brakes.driverValve->GetPos(bh_MIN)); // odcięcie else if (brakes.HandlePositionSmooth() > brakes.driverValve->GetPos(bh_MAX)) brakes.SetHandlePositionSmooth(brakes.driverValve->GetPos(bh_MAX)); // TODO: verify whether BrakeCtrlPosR and fBrakeCtrlPos can be rolled into single variable brakes.SetHandlePositionReal(brakes.HandlePositionSmooth()); auto x = static_cast(std::floor(brakes.HandlePositionSmooth())); // jeśli odwołujemy się do BrakeCtrlPos w pośrednich, to musi być // obcięte a nie zaokrągone while (x > brakes.HandlePosition() && brakes.HandlePosition() < brakes.config.handlePositions) // jeśli zwiększyło się o 1 if (!IncBrakeLevelOld()) // physics::Vehicle:: break; // wyjście awaryjne while (x < brakes.HandlePosition() && brakes.HandlePosition() >= -1) // jeśli zmniejszyło się o 1 if (!DecBrakeLevelOld()) // physics::Vehicle:: break; brakes.actualPressure = brakes.config.pressureTable[brakes.HandlePosition()]; // skopiowanie pozycji /* //youBy: obawiam sie, ze tutaj to nie dziala :P //Ra 2014-03: było tak zrobione, że działało - po każdej zmianie pozycji była wywoływana ta funkcja // if (BrakeSystem==Pneumatic?BrakeSubsystem==Oerlikon:false) //tylko Oerlikon akceptuje ułamki if(false) if (fBrakeCtrlPos>0.0) {//wartości pośrednie wyliczamy tylko dla hamowania double u=fBrakeCtrlPos-double(x); //ułamek ponad wartość całkowitą if (u>0.0) {//wyliczamy wartości ważone BrakePressureActual.PipePressureVal+=-u*BrakePressureActual.PipePressureVal+u*BrakePressureTable[BrakeCtrlPos+1+2].PipePressureVal; //BrakePressureActual.BrakePressureVal+=-u*BrakePressureActual.BrakePressureVal+u*BrakePressureTable[BrakeCtrlPos+1].BrakePressureVal; //to chyba nie będzie tak działać, zwłaszcza w EN57 BrakePressureActual.FlowSpeedVal+=-u*BrakePressureActual.FlowSpeedVal+u*BrakePressureTable[BrakeCtrlPos+1+2].FlowSpeedVal; } } */ }; bool physics::Vehicle::BrakeLevelAdd(double b) { // dodanie wartości (b) do pozycji hamulca (w tym ujemnej) // zwraca false, gdy po dodaniu było by poza zakresem BrakeLevelSet(brakes.HandlePositionSmooth() + b); return b > 0.0 ? brakes.HandlePositionSmooth() < brakes.config.handlePositions : brakes.HandlePosition() > -1.0; // true, jeśli można kontynuować }; bool physics::Vehicle::IncBrakeLevel() { // nowa wersja na użytek AI, false gdy osiągnięto pozycję BrakeCtrlPosNo return BrakeLevelAdd(1.0); }; bool physics::Vehicle::DecBrakeLevel() { return BrakeLevelAdd(-1.0); }; // nowa wersja na użytek AI, false gdy osiągnięto pozycję -1 /* Ukrotnienie EN57: 1 //układ szeregowy 2 //układ równoległy 3 //bocznik 1 4 //bocznik 2 5 //bocznik 3 6 //do przodu 7 //do tyłu 8 //1 przyspieszenie 9 //minus obw. 2 przyspieszenia 10 //jazda na oporach 11 //SHP 12A //podnoszenie pantografu przedniego 12B //podnoszenie pantografu tylnego 13A //opuszczanie pantografu przedniego 13B //opuszczanie wszystkich pantografów 14 //załączenie WS 15 //rozrząd (WS, PSR, wał kułakowy) 16 //odblok PN 18 //sygnalizacja przetwornicy głównej 19 //luzowanie EP 20 //hamowanie EP 21 //rezerwa** (1900+: zamykanie drzwi prawych) 22 //zał. przetwornicy głównej 23 //wył. przetwornicy głównej 24 //zał. przetw. oświetlenia 25 //wył. przetwornicy oświetlenia 26 //sygnalizacja WS 28 //sprężarka 29 //ogrzewanie 30 //rezerwa* (1900+: zamykanie drzwi lewych) 31 //otwieranie drzwi prawych 32H //zadziałanie PN siln. trakcyjnych 33 //sygnał odjazdu 34 //rezerwa (sygnalizacja poślizgu) 35 //otwieranie drzwi lewych ZN //masa */ // ***************************************************************************** // Q: 20160714 // Oblicza iloraz aktualnej pozycji do maksymalnej hamulca pomocnicznego // ***************************************************************************** double physics::Vehicle::LocalBrakeRatio(void) const { double LBR; if (brakes.config.driverValveType == TBrakeHandle::MHZ_EN57) if (brakes.operatingMode >= bom_EP) LBR = brakes.driverValve->GetEP(brakes.HandlePositionReal()); else LBR = 0; else { if (LocalBrakePosNo > 0) LBR = brakes.IndependentPosition(); else LBR = 0; } // if (TestFlag(BrakeStatus, b_antislip)) // LBR = std::max(LBR, PipeRatio) + 0.4; return LBR; } // ***************************************************************************** // Q: 20160714 // Oblicza iloraz aktualnej pozycji do maksymalnej hamulca ręcznego // ***************************************************************************** double physics::Vehicle::ManualBrakeRatio(void) const { double MBR; if (ManualBrakePosNo > 0) MBR = (double)brakes.HandBrakePosition() / ManualBrakePosNo; else MBR = 0; return MBR; } // ***************************************************************************** // Q: 20160713 // Zwraca objętość // ***************************************************************************** double physics::Vehicle::BrakeVP(void) const { if (brakes.config.valveVolume > 0) return brakes.reservoirAir / (10.0 * brakes.config.valveVolume); else return 0; } // ***************************************************************************** // Q: 20160713 // Zwraca iloraz różnicy między przewodem kontrolnym i głównym oraz DeltaPipePress // ***************************************************************************** double physics::Vehicle::RealPipeRatio(void) const { double rpp; if (brakes.config.pipePressureRange > 0) rpp = (brakes.controlReservoirPressure - brakePipe.Pressure()) / brakes.config.pipePressureRange; else rpp = 0; return rpp; } // ***************************************************************************** // Q: 20160713 // Zwraca iloraz ciśnienia w przewodzie do DeltaPipePress // ***************************************************************************** double physics::Vehicle::PipeRatio(void) const { double pr; if (brakes.config.pipePressureRange > 0) if (false) // SPKS!! no to jak nie wchodzimy to po co branch? { if (3.0 * brakePipe.Pressure() > brakes.config.pipeHighPressure + brakes.config.pipeLowPressure + brakes.config.pipeLowPressure) pr = (brakes.config.pipeHighPressure - std::min(brakes.config.pipeHighPressure, brakePipe.Pressure())) / (brakes.config.pipePressureRange * 4.0 / 3.0); else pr = (brakes.config.pipeHighPressure - 1.0 / 3.0 * brakes.config.pipePressureRange - std::max(brakes.config.pipeLowPressure, brakePipe.Pressure())) / (brakes.config.pipePressureRange * 2.0 / 3.0); // if (not TestFlag(BrakeStatus, b_Ractive)) // and(BrakeMethod and 1 = 0) and TestFlag(BrakeDelays, bdelay_R) and (Power < 1) and // (BrakeCtrlPos < 1) then pr : = std::min(0.5, pr); // if (Compressor > 0.5) // then pr : = pr * 1.333; // dziwny rapid wywalamy } else pr = (brakes.config.pipeHighPressure - std::max(brakes.config.pipeLowPressure, std::min(brakes.config.pipeHighPressure, brakePipe.Pressure()))) / brakes.config.pipePressureRange; else pr = 0; return pr; } // ************************************************************************************************* // Q: 20160710 // włączenie / wyłączenie hamulca elektro-pneumatycznego // ************************************************************************************************* bool physics::Vehicle::EpFuseSwitch(bool State) { if (epFuse != State) { epFuse = State; return true; } else return false; // if (EpFuse == true) SendCtrlToNext(VehicleCommand::epFuseSwitch, 1, CabActive) // else SendCtrlToNext(VehicleCommand::epFuseSwitch, 0, CabActive); } // ************************************************************************************************* // yB: 20190906 // włączenie / wyłączenie hamulca sprezynowego // ************************************************************************************************* bool physics::Vehicle::SpringBrakeActivate(bool State) { if (lowVoltage.Is24vAvailable() || lowVoltage.Is110vAvailable()) { SendCtrlToNext(VehicleCommand::springBrakeActivate, int(State), CabActive, CouplingSet::FromBits(brakes.spring.controlCouplings)); if (brakes.spring.activate != State) { brakes.spring.activate = State; wake_for_brake(); return true; } } return false; } // ************************************************************************************************* // yB: 20190906 // włączenie / wyłączenie odciecia hamulca sprezynowego // ************************************************************************************************* bool physics::Vehicle::SpringBrakeShutOff(bool State) { if (brakes.spring.shutOff != State) { brakes.spring.shutOff = State; return true; } else return false; } // ************************************************************************************************* // yB: 20190906 // wyluzowanie hamulca sprezynowego // ************************************************************************************************* bool physics::Vehicle::SpringBrakeRelease() { if (brakes.spring.isReady && brakes.spring.cylinder->P() < brakes.spring.minForcePressure) { brakes.spring.isReady = false; wake_for_brake(); return true; } else return false; } // ************************************************************************************************* // Q: 20160710 // załączenie przycisku przeciwpoślizgowego // ************************************************************************************************* bool physics::Vehicle::AntiSlippingButton(void) { // NOTE: disabled the sandbox part, it's already controlled by another part of the AI routine return AntiSlippingBrake() /*|| Sandbox(true)*/; } // ************************************************************************************************* // Q: 20160713 // włączenie / wyłączenie sprężarki // ************************************************************************************************* bool physics::Vehicle::CompressorSwitch(bool State, range_t const Notify) { if (air.compressor.config.start != start_t::manual) { // only pay attention if the compressor can be controlled manually return false; } auto const initialstate{air.compressor.IsAllowed()}; if (air.config.mainReservoirVolume > 0.0 && air.compressor.config.speed > 0.0) { air.compressor.Allow(State); } if (Notify != range_t::local) { SendCtrlToNext(VehicleCommand::compressorSwitch, State ? 1 : 0, CabActive, Notify == range_t::unit ? Coupling::control | Coupling::permanent : Coupling::control); } return air.compressor.IsAllowed() != initialstate; } bool physics::Vehicle::ChangeCompressorPreset(int const State, range_t const Notify) { auto const initialstate{air.compressor.ListPosition()}; air.compressor.SetListPosition(State); if (Notify != range_t::local) { SendCtrlToNext(VehicleCommand::compressorPreset, State, CabActive, Notify == range_t::unit ? Coupling::control | Coupling::permanent : Coupling::control); } return air.compressor.ListPosition() != initialstate; } // ************************************************************************************************* // Q: 20160711 // zwiększenie nastawy hamulca // ************************************************************************************************* bool physics::Vehicle::IncBrakeLevelOld(void) { bool IBLO = false; if (brakes.config.handlePositions > 0) { if (brakes.HandlePosition() < brakes.config.handlePositions) { brakes.SetHandlePosition(brakes.HandlePosition() + 1); // youBy: EP po nowemu IBLO = true; if (brakes.actualPressure.PipePressureVal < 0 && brakes.config.pressureTable[brakes.HandlePosition() - 1].PipePressureVal > 0) brakes.pipeLimiterPressure = brakePipe.Pressure(); } else { IBLO = false; } } return IBLO; } // ***************************************************************************** // Q: 20160711 // zmniejszenie nastawy hamulca // ***************************************************************************** bool physics::Vehicle::DecBrakeLevelOld(void) { bool DBLO = false; if (brakes.config.handlePositions > 0) { if (brakes.HandlePosition() > (brakes.config.driverValveType == TBrakeHandle::FV4a ? -2 : -1)) { brakes.SetHandlePosition(brakes.HandlePosition() - 1); // youBy: EP po nowemu DBLO = true; // if ((BrakePressureTable[BrakeCtrlPos].PipePressureVal<0.0) && // (BrakePressureTable[BrakeCtrlPos+1].PipePressureVal > 0)) // LimPipePress=PipePress; } else DBLO = false; } return DBLO; } // ************************************************************************************************* // Q: 20160711 // zwiększenie nastawy hamulca pomocnicznego // ************************************************************************************************* bool physics::Vehicle::IncLocalBrakeLevel(float const CtrlSpeed) { bool IBL; if (brakes.IndependentPosition() < 1.0 /*and (BrakeCtrlPos<1)*/) { brakes.SetIndependentPosition(std::min(1.0, brakes.IndependentPosition() + CtrlSpeed / LocalBrakePosNo)); wake_for_brake(); IBL = true; } else IBL = false; return IBL; } // ************************************************************************************************* // Q: 20160711 // zmniejszenie nastawy hamulca pomocniczego // ************************************************************************************************* bool physics::Vehicle::DecLocalBrakeLevel(float const CtrlSpeed) { bool DBL; if (brakes.IndependentPosition() > 0) { brakes.SetIndependentPosition(std::max(0.0, brakes.IndependentPosition() - CtrlSpeed / LocalBrakePosNo)); wake_for_brake(); DBL = true; } else DBL = false; return DBL; } // ************************************************************************************************* // Q: 20160711 // zwiększenie nastawy hamulca ręcznego // ************************************************************************************************* bool physics::Vehicle::IncManualBrakeLevel(int CtrlSpeed) { bool IMBL; if (brakes.HandBrakePosition() < ManualBrakePosNo) /*and (BrakeCtrlPos<1)*/ { while (brakes.HandBrakePosition() < ManualBrakePosNo && CtrlSpeed > 0) { brakes.SetHandBrakePosition(brakes.HandBrakePosition() + 1); CtrlSpeed--; } wake_for_brake(); IMBL = true; } else IMBL = false; return IMBL; } // ************************************************************************************************* // Q: 20160711 // zmniejszenie nastawy hamulca ręcznego // ************************************************************************************************* bool physics::Vehicle::DecManualBrakeLevel(int CtrlSpeed) { bool DMBL; if (brakes.HandBrakePosition() > 0) { while (CtrlSpeed > 0 && brakes.HandBrakePosition() > 0) { brakes.SetHandBrakePosition(brakes.HandBrakePosition() - 1); CtrlSpeed--; } wake_for_brake(); DMBL = true; } else DMBL = false; return DMBL; } bool physics::Vehicle::AlarmChainSwitch(bool const State) { if (brakes.alarmChain != State) { // simple routine for the time being brakes.alarmChain = State; return true; } return false; } // ************************************************************************************************* // Q: 20160710 // hamowanie przeciwpoślizgowe // ************************************************************************************************* bool physics::Vehicle::AntiSlippingBrake(void) { bool ASB = false; // Ra: przeniesione z końca if (brakes.config.antiSlipType == 1) { ASB = true; // SPKS!! brakes.distributor->ASB(1); brakes.antiSlipTimer = 0; } return ASB; } // ************************************************************************************************* // Q: 20160711 // włączenie / wyłączenie odluźniacza // ************************************************************************************************* bool physics::Vehicle::BrakeReleaser(int state) { bool OK = true; // false tylko jeśli nie uda się wysłać, GF 20161124 if (state != 0) { // additional limitations imposed by pressure switch if (false == protection.PressureSwitchTripped() || false == ReleaserEnabledOnlyAtNoPowerPos || true == IsMainCtrlNoPowerPos()) { brakes.distributor->Releaser(state); } } else { brakes.distributor->Releaser(state); } if (CabActive != 0) // rekurencyjne wysłanie do następnego OK = SendCtrlToNext(VehicleCommand::brakeReleaser, state, CabActive); return OK; } // ************************************************************************************************* // yB: 20160711 // włączenie / wyłączenie uniwersalnego przycisku hamulcowego // ************************************************************************************************* bool physics::Vehicle::UniversalBrakeButton(int button, int state) { bool OK = true; // false tylko jeśli nie uda się wysłać, GF 20161124 brakes.universalButtonPressed[button] = state > 0; int flag = 0; if (lowVoltage.Is24vAvailable() || lowVoltage.Is110vAvailable()) { for (int i = 0; i < 3; i++) { flag = flag | (brakes.universalButtonPressed[i] ? brakes.config.universalButtonActions[i] : 0); } } brakes.distributor->SetUniversalFlag(flag); brakes.driverValve->SetUniversalFlag(flag); brakes.independentValve->SetUniversalFlag(flag); brakes.pipeLock.unlockPressed = (flag & TUniversalBrake::ub_UnlockPipe) > 0; // if the releaser can be activated by switch if (TestFlag(brakes.config.universalButtonActions[0] & brakes.config.universalButtonActions[1] & brakes.config.universalButtonActions[2], TUniversalBrake::ub_Release)) { BrakeReleaser(TestFlag(flag, TUniversalBrake::ub_Release) ? 1 : 0); } return OK; } // ************************************************************************************************* // Q: 20160711 // włączenie / wyłączenie hamulca elektro-pneumatycznego // ************************************************************************************************* bool physics::Vehicle::SwitchEPBrake(int state) { bool OK; OK = false; if (brakes.config.driverValveType == TBrakeHandle::St113 && cabOccupied != 0) { // the handle works the EP contactors pneumatically, their supply is cut as the Oerlikon handle's is if (state > 0 && EPBrakeControlPowered()) EpForce = brakes.driverValve->GetEP(); // TODO: przetlumaczyc else EpForce = 0; brakes.distributor->SetEPS(EpForce); SendCtrlToNext(VehicleCommand::brake, EpForce, CabActive); } // OK:=SetFlag(BrakeStatus,((2*State-1)*b_epused)); // SendCtrlToNext('Brake',(state*(2*BrakeCtrlPos-1)),CabActive); return OK; } bool physics::Vehicle::EPBrakeControlPowered() const { return lowVoltage.Is24vAvailable() && DirActive != 0 && epFuse; } // ************************************************************************************************* // Q: 20160711 // zwiększenie ciśnienia hamowania // ************************************************************************************************* bool physics::Vehicle::IncBrakePress(double &brake, double PressLimit, double dp) { bool IBP; // if (DynamicBrakeType<>dbrake_switch) and (DynamicBrakeType<>dbrake_none) and // ((BrakePress>2.0) or (PipePress<3.7{(LowPipePress+0.5)})) then if (brakes.electrodynamic.type != dbrake_switch && brakes.electrodynamic.type != dbrake_none && brakes.cylinderPressure > 2.0 && trainType != train_type::emu) // yB radzi nie sprawdzać ciśnienia w przewodzie // hunter-301211: dla EN57 silnikow nie odlaczamy { brakes.electrodynamic.on = true; // uruchamianie hamulca ED albo odlaczanie silnikow if (brakes.electrodynamic.type == dbrake_automatic && abs(Im) > 60) // nie napelniaj wiecej, jak na EP09 dp = 0.0; } if (brake + dp < PressLimit) { brake = brake + dp; IBP = true; } else { IBP = false; brake = PressLimit; } return IBP; } // ************************************************************************************************* // Q: 20160711 // zmniejszenie ciśnienia hamowania // ************************************************************************************************* bool physics::Vehicle::DecBrakePress(double &brake, double PressLimit, double dp) { bool DBP; if (brake - dp > PressLimit) { brake = brake - dp; DBP = true; } else { DBP = false; brake = PressLimit; } // if ((DynamicBrakeType != dbrake_switch) && ((BrakePress < 0.1) && (PipePress > 0.45 // /*(LowPipePress+0.06)*/ ))) if (brakes.electrodynamic.type != dbrake_switch && brakes.cylinderPressure < 0.1) // yB radzi nie sprawdzać ciśnienia w przewodzie brakes.electrodynamic.on = false; // wylaczanie hamulca ED i/albo zalaczanie silnikow return DBP; } // ************************************************************************************************* // Q: 20160711 // przełączenie nastawy hamulca O/P/T // ************************************************************************************************* bool physics::Vehicle::BrakeDelaySwitch(int BDS) { bool rBDS; if (brakes.distributor->SetBDF(BDS)) { brakes.delaySetting = BDS; rBDS = true; brakes.distributor->SetBrakeStatus(brakes.distributor->GetBrakeStatus() & ~64); // kopowanie nastawy hamulca do kolejnego czlonu - do przemyślenia if (CabActive != 0) SendCtrlToNext(VehicleCommand::brakeDelay, brakes.delaySetting, CabActive); } else rBDS = false; return rBDS; } // ************************************************************************************************* // Q: 20160712 // zwiększenie przełożenia hamulca // ************************************************************************************************* bool physics::Vehicle::IncBrakeMult(void) { bool IBM; if (brakes.loadSetting > 0 && brakes.config.maxPressureMass < 2 && brakes.loadSetting < 3) { if (brakes.config.maxCylinderPressure[2] > 0 && brakes.loadSetting == 1) brakes.loadSetting = 2; else brakes.loadSetting = 3; IBM = true; if (brakes.config.cylinders.leverRatio[2] > 0) brakes.config.cylinders.leverRatio[0] = brakes.config.cylinders.leverRatio[2]; } else IBM = false; return IBM; } // ************************************************************************************************* // Q: 20160712 // zmniejszenie przełożenia hamulca // ************************************************************************************************* bool physics::Vehicle::DecBrakeMult(void) { bool DBM; if (brakes.loadSetting > 1 && brakes.config.maxPressureMass < 2) { if (brakes.config.maxCylinderPressure[2] > 0 && brakes.loadSetting == 3) brakes.loadSetting = 2; else brakes.loadSetting = 1; DBM = true; if (brakes.config.cylinders.leverRatio[1] > 0) brakes.config.cylinders.leverRatio[0] = brakes.config.cylinders.leverRatio[1]; } else DBM = false; return DBM; } // ************************************************************************************************* // Q: 20160712 // zaktualizowanie ciśnienia w hamulcach // ************************************************************************************************* void physics::Vehicle::UpdateBrakePressure(double dt) { // const double LBDelay = 5.0; // stala czasowa hamulca // double Rate, Speed, dp, sm; brakes.independentValveFlow = 0; brakes.brakeFlow = 0; brakes.distributor->ForceLeak(dt * air.config.leakRate * 0.25); // fake air leaks from brake system reservoirs auto const brakepress{brakes.cylinderPressure}; brakes.cylinderPressure = brakes.distributor->GetBCP(); if (dt > 0.0) { // averaged over about a second: the valves move the cylinder in pulses, quicker than that within a single step auto const rate{std::abs(brakes.cylinderPressure - brakepress) / dt}; brakes.cylinderPressureRate += (rate - brakes.cylinderPressureRate) * std::min(1.0, dt / 1.0); } // BrakePress:=(Hamulec as TEst4).ImplsRes.pa; brakes.reservoirAir = brakes.distributor->GetBRP(); } // ************************************************************************************************* // Q: 20160712 // Obliczanie pracy sprężarki // ************************************************************************************************* void physics::Vehicle::CompressorCheck(double dt) { auto &compressor{air.compressor}; if (false == compressor.IsFitted()) { compressor.Allow(false); return; } compressor.SelectCab(CabActive); // EmergencyValve brakes.emergencyValve.open = air.MainReservoirPressure() > (brakes.emergencyValve.open ? brakes.emergencyValve.offPressure : brakes.emergencyValve.onPressure); if (brakes.emergencyValve.open) { auto dV = static_cast(PF(0, air.MainReservoirPressure(), brakes.emergencyValve.area) * dt); air.AddAir(-dV); } CompressorInputs inputs; inputs.converterAllowed = converter.IsAllowed(); inputs.mainCircuitPowered = MainCircuitPowered(); inputs.lowVoltage110v = lowVoltage.Is110vAvailable(); // for these multi-unit engines compressors turn off whenever any of them was affected by the governor // NOTE: this is crude implementation, limited only to adjacent vehicles auto const governorlocked = [this](VehicleEnd const End) { auto const &coupler{Couplers[End]}; return coupler.Connected != nullptr && coupler.CouplingFlag.HasAll(Coupling::permanent) && coupler.Connected->air.compressor.IsGovernorLocked(); }; inputs.coupledGovernorLock = governorlocked(VehicleEnd::rear) || governorlocked(VehicleEnd::front); inputs.switchingDelayPassed = LastSwitchingTime > CtrlDelay; inputs.engineRpmRatio = EngineRPMRatio(); auto const step{air.Update(inputs, dt)}; if (step.switched) { if (compressor.IsRunning()) { // jeśli została załączona to trzeba ograniczyć ponowne włączenie LastSwitchingTime = 0; } Timer::subsystem.switching.note(compressor.IsRunning() ? Timer::switching_counter::compressor_on : Timer::switching_counter::compressor_off, this, &TypeName, &Name); } if (step.vented) { SetFlag(soundFlag, sound::relay | sound::loud); } // a compressor not driven by the engine doesn't draw power (some 5 A) from the high- or low voltage circuit yet } // ************************************************************************************************* // Q: 20160712 // aktualizacja ciśnienia w przewodzie głównym // ************************************************************************************************* void physics::Vehicle::UpdatePipePressure(double dt) { if (brakePipe.Pressure() > 1.0) { brakePipe.reservoir->Flow(-brakePipe.Pressure()*air.config.leakRate * dt); brakePipe.reservoir->Act(); } // double dV; // physics::Vehicle *c; // physics::Vehicle double temp; // int b; brakePipe.SetPressure(brakePipe.reservoir->P()); // PPP:=PipePress; brakes.driverValveFlow = 0; if (brakes.config.handlePositions > 1) { UpdateLocalBrakeValve(dt); UpdateMainValve(dt); } UpdateEmergencyValve(dt); // 0.2*Spg brakePipe.reservoir->Flow(-brakes.driverValveFlow); brakePipe.reservoir->Flow(-brakePipe.Pressure() * 0.001 * dt); // if Heating then // Pipe.Flow(PF(PipePress, 0, d2A(7)) * dt); // if ConverterFlag then // Pipe.Flow(PF(PipePress, 0, d2A(12)) * dt); brakes.driverValveFlow = brakes.driverValveFlow / (Dim.L * brakes.config.pipeSection * 20); brakes.controlReservoirPressure = brakes.distributor->GetVRP(); // ciśnienie komory wstępnej rozdzielacza // if (Hamulec is typeid(TWest)) return 0; UpdateDistributorSettings(); if ((brakes.config.driverValveType == TBrakeHandle::FVel6 || brakes.config.driverValveType == TBrakeHandle::FVE408) && cabOccupied != 0) { UpdateEPBrake(); } else if (brakes.config.driverValveType == TBrakeHandle::St113 && cabOccupied != 0 && std::abs(EpForce) > 0.001 && !EPBrakeControlPowered()) { // the EP brake switched on with the button drops out with the control's supply SwitchEPBrake(0); } brakePipe.reservoir->Act(); brakePipe.SetPressure(brakePipe.reservoir->P()); if ((brakes.distributor->GetBrakeStatus() & b_dmg) == b_dmg) // jesli hamulec wyłączony temp = 0.0; // odetnij else temp = 1.0; // połącz brakePipe.reservoir->Flow(temp * brakes.distributor->GetPF(temp * brakePipe.Pressure(), dt, Vel)); // the air the brake hoses carry to and from the neighbours comes once all vehicles have stepped, UpdatePipeCouplers() brakePipe.SetCouplersPending(true); if (brakes.config.antiSlipType == 128) brakes.distributor->ASB(int(slippingWheels && Vel > 1) * (1 + 2 * int(nrot_eps < -0.01))); brakes.pipeFlow = 0; // yB: jednokrokowe liczenie tego wszystkiego brakePipe.reservoir->Act(); brakePipe.SetPressure(brakePipe.reservoir->P()); brakes.driverValveFlow = brakes.driverValveFlow / (100.0 * dt); // normalizacja po czasie do syczenia; if (brakePipe.Pressure() < -1.0) { brakePipe.SetPressure(-1.0); brakePipe.reservoir->CreatePress(-1.0); brakePipe.reservoir->Act(); } air.ClampEmpty(); } // the flow of the local brake valve, by the handle, the TRAXX's timed one, the integrated controller or the parking void physics::Vehicle::UpdateLocalBrakeValve(double dt) { // the local brake of a vehicle prepared for hauling (PulleeMode) is held released, whatever asks for it if (EngineType != TEngineType::ElectricInductionMotor) { double lbpa = brakes.config.independentTraxx ? eim_localbrake : brakes.IndependentPosition(); if (EIMCtrlType > 0 && uniCtrlIntegratedLocalBrakeCtrl) { lbpa = std::max(0.0, -eimicReal); } if (SpeedCtrlUnit.Parking) { lbpa = std::max(lbpa, brakes.holding.deceleration); } brakes.independentValveFlow = brakes.independentValve->GetPF(pulleeMode ? 0.0 : std::max(lbpa, brakes.IndependentPositionEim()), brakes.distributor->GetBCP(), mainPipe.Pressure(), dt, 0); } else { // in EIM stock the pneumatic local brake is normally driven only by the MED algorithm // (LocalBrakePosAEIM). When SplitEDPneumaticBrake is active the dedicated LocalBrake // lever should apply pneumatic pressure on the locomotive directly, bypassing MED. // LocHandleTimeTraxx also bypasses MED, but has 3 positions (brake-hold-unbrake). double lbpa; if (brakes.config.independentTraxx) { lbpa = eim_localbrake; } else if (brakes.electrodynamic.splitFromPneumatic) // When LocHandleTimeTraxx and SplitEDPneumaticBrake are both active, lbpa is taken from LocHandleTimeTraxx, which is the desired behavior. { lbpa = brakes.IndependentPosition(); } else { lbpa = 0.0; } brakes.independentValveFlow = brakes.independentValve->GetPF(pulleeMode ? 0.0 : std::max(lbpa, brakes.IndependentPositionEim()), brakes.distributor->GetBCP(), mainPipe.Pressure(), dt, 0); } } // the lock of the brake pipe and the flow of the driver's handle, from the main reservoir or into the air void physics::Vehicle::UpdateMainValve(double dt) { auto const temp{MainValveSupply()}; brakes.driverValve->SetReductor(brakes.HandleCapPosition()); if (pulleeMode) { // the driver's valve of a vehicle prepared for hauling is cut out, whatever its handle shows brakes.driverValveFlow = 0; } else if ((brakes.config.availableOperatingModes & bom_PS) == 0 || (cabOccupied != 0 && brakes.operatingMode != bom_PS)) { if (brakes.operatingMode < bom_EP || brakes.driverValve->GetPos(bh_EB) - 0.5 < brakes.HandlePositionReal() || (brakes.config.driverValveType != TBrakeHandle::MHZ_EN57 && brakes.config.driverValveType != TBrakeHandle::MHZ_K8P)) { brakes.driverValveFlow = brakes.driverValve->GetPF(MainValveHandlePosition(), brakePipe.Pressure(), temp, dt, brakes.equivalentPipePressure); } else { brakes.driverValveFlow = brakes.driverValve->GetPF(0, brakePipe.Pressure(), temp, dt, brakes.equivalentPipePressure); } } else if (brakes.HandlePosition() == brakes.driverValve->GetPos(bh_EB)) { brakes.driverValveFlow = brakes.driverValve->GetPF(brakes.HandlePositionReal(), brakePipe.Pressure(), temp, dt, brakes.equivalentPipePressure); } if (brakes.driverValveFlow < 0) // && (PipePressureVal > 0.01) //50 { if (air.MainReservoirPressure() > mainPipe.Pressure()) { air.AddAir(brakes.driverValveFlow / 1500.0); mainPipe.reservoir->Flow(brakes.driverValveFlow / 3.0); } else mainPipe.reservoir->Flow(brakes.driverValveFlow); } } // the pressure the driver's handle is fed from: the main pipe's, or the brake pipe's own with the pipe locked double physics::Vehicle::MainValveSupply() { brakes.pipeLock.locked = brakePipe.Pressure() < (brakes.pipeLock.locked ? brakes.pipeLock.offPressure : brakes.pipeLock.onPressure); bool lock_new = (brakes.pipeLock.locked && !brakes.pipeLock.unlockPressed && brakes.HandlePositionReal() > brakes.pipeLock.unlockHandlePosition) || (brakes.config.emergencyCutsOffDriverValve && brakes.emergencyValve.flow > 0); // new simple codition based on .fiz bool lock_old = brakes.config.driverValveType == TBrakeHandle::FV4a // old complex condition based on assumptions && brakePipe.Pressure() < 2.75 && (brakes.distributor->GetStatus() & b_rls) == 0 && brakes.config.subsystem == TBrakeSubSystem::ss_LSt && trainType != train_type::emu && !brakes.pipeLock.unlockPressed; if (lock_old || lock_new) { return brakePipe.Pressure() + 0.00001; } return mainPipe.Pressure(); } // the position the driver's handle works at: its own, or the one the speed control's brake intervention sets double physics::Vehicle::MainValveHandlePosition() const { double pos = brakes.HandlePositionReal(); if (SpeedCtrlUnit.IsActive && SpeedCtrlUnit.BrakeIntervention && !SpeedCtrlUnit.Standby && brakes.HandlePosition() != brakes.driverValve->GetPos(bh_EB)) { pos = brakes.driverValve->GetPos(bh_NP); if (SpeedCtrlUnit.BrakeInterventionBraking) pos = brakes.driverValve->GetPos(bh_FB); if (SpeedCtrlUnit.BrakeInterventionUnbraking) pos = brakes.driverValve->GetPos(bh_RP); } return pos; } void physics::Vehicle::UpdateEmergencyValve(double dt) { // ulepszony hamulec bezp. brakes.emergencyValve.flow = 0.0; // EngDmgFlag 32 (load destroyed) used to trigger this too; dropped, it has nothing to do with // the emergency brake (a broken coupler sets AlarmChainFlag instead). the Elf's emergency brake position // vents the pipe only from the vehicle driven from, whatever the controllers of the others show. // a vehicle prepared for hauling (PulleeMode) vents it for nothing of its own, only for the alarm chain, // which a train parting sets too auto const ownemergency{radioStopFlag || (eimCtrlEmergency && brakes.IndependentPosition() >= 1.0) || (IsMainCtrlEmergencyPos() && IsCabMaster()) || SecuritySystem.is_braking() || (brakes.spring.driveEmergencySpeed >= 0 && Vel > brakes.spring.driveEmergencySpeed && brakes.spring.isActive) || (0 == CabActive && (InactiveCabFlag & activation::emergencybrake) != 0)}; if (brakes.alarmChain || (ownemergency && false == pulleeMode)) { brakes.emergencyValve.flow = PF(0, brakePipe.Pressure(), 0.15) * dt; } brakes.driverValveFlow += brakes.emergencyValve.flow; } // what the distributor valve gets of the local brake and the load, by its kind void physics::Vehicle::UpdateDistributorSettings() { switch (brakes.config.distributorType) { case TBrakeValve::K: case TBrakeValve::W: { if (brakes.config.independentValveType != TBrakeHandle::NoHandle) { brakes.independentCylinderPressure = brakes.independentValve->GetCP(); //(Hamulec as TWest).SetLBP(LocBrakePress); brakes.distributor->SetLBP(brakes.independentCylinderPressure); } if (brakes.config.maxPressureMass < 2) //(Hamulec as TWest).PLC(MaxBrakePress[LoadFlag]) brakes.distributor->PLC(brakes.config.maxCylinderPressure[brakes.loadSetting]); else //(Hamulec as TWest).PLC(TotalMass); brakes.distributor->PLC(totalMass - mred); break; } case TBrakeValve::LSt: case TBrakeValve::EStED: { brakes.independentCylinderPressure = brakes.independentValve->GetCP(); auto const et41or42 = [](train_type const Type) { return Type == train_type::et41 || Type == train_type::et42; }; for (auto const &couplerentry : Couplers) // nie podoba mi się to rozwiązanie, chyba trzeba // dodać jakiś wpis do fizyki na to if (et41or42(trainType) && couplerentry.Connected != nullptr && et41or42(couplerentry.Connected->trainType) && couplerentry.CouplingFlag.HasAll(Coupling::control | Coupling::mainHose)) brakes.independentCylinderPressure = std::max(couplerentry.Connected->brakes.independentValve->GetCP(), brakes.independentCylinderPressure); // if ((DynamicBrakeFlag) && (EngineType == ElectricInductionMotor)) //{ // //if (Vel > 10) // // LocBrakePress = 0; // //else if (Vel > 5) // // LocBrakePress = (10 - Vel) / 5 * LocBrakePress; // } //(Hamulec as TLSt).SetLBP(LocBrakePress); brakes.distributor->SetLBP(brakes.independentCylinderPressure); if (brakes.config.distributorType == TBrakeValve::EStED) { if (brakes.config.maxPressureMass < 2) brakes.distributor->PLC(brakes.config.maxCylinderPressure[brakes.loadSetting]); else brakes.distributor->PLC(totalMass - mred); } break; } case TBrakeValve::CV1_L_TR: { brakes.independentCylinderPressure = brakes.independentValve->GetCP(); //(Hamulec as TCV1L_TR).SetLBP(LocBrakePress); brakes.distributor->SetLBP(brakes.independentCylinderPressure); break; } case TBrakeValve::EP2: case TBrakeValve::EP1: { brakes.distributor->PLC(totalMass - mred); break; } case TBrakeValve::ESt3AL2: case TBrakeValve::NESt3: case TBrakeValve::ESt4: case TBrakeValve::ESt3: { if (brakes.config.maxPressureMass < 2) //(Hamulec as TNESt3).PLC(MaxBrakePress[LoadFlag]) brakes.distributor->PLC(brakes.config.maxCylinderPressure[brakes.loadSetting]); else //(Hamulec as TNESt3).PLC(TotalMass); brakes.distributor->PLC(totalMass - mred); brakes.independentCylinderPressure = brakes.independentValve->GetCP(); //(Hamulec as TNESt3).SetLBP(LocBrakePress); brakes.distributor->SetLBP(brakes.independentCylinderPressure); break; } case TBrakeValve::KE: { brakes.independentCylinderPressure = brakes.independentValve->GetCP(); //(Hamulec as TKE).SetLBP(LocBrakePress); brakes.distributor->SetLBP(brakes.independentCylinderPressure); if (brakes.config.maxPressureMass < 2) //(Hamulec as TKE).PLC(MaxBrakePress[LoadFlag]) brakes.distributor->PLC(brakes.config.maxCylinderPressure[brakes.loadSetting]); else //(Hamulec as TKE).PLC(TotalMass); brakes.distributor->PLC(totalMass - mred); break; } default: { // unsupported brake valve type, we should never land here // ErrorLog( "Unsupported brake valve type (" + std::to_string( BrakeValve ) + ") in " + TypeName ); // ::PostQuitMessage( 0 ); break; } } // switch } // the ep brake of the FVel6 and FVE408 handles, passed along the unit void physics::Vehicle::UpdateEPBrake() { if (EPBrakeControlPowered()) // tu powinien byc jeszcze bezpiecznik EP i baterie - // temp = (Handle as TFVel6).GetCP EpForce = brakes.driverValve->GetEP(); else EpForce = 0.0; brakes.electrodynamic.emuWorking = EpForce > 0.001 ? lowVoltage.Is110vAvailable() : true; double temp1 = EpForce; if (brakes.emuDynamic.epMaxSpeed > 0.001 && Vel > brakes.emuDynamic.epMaxSpeed && brakes.electrodynamic.emuWorking) temp1 = 0; if (brakes.emuDynamic.epMinCurrent > 0.001 && abs(Im) > brakes.emuDynamic.epMinCurrent && brakes.electrodynamic.emuWorking) temp1 = 0; brakes.distributor->SetEPS(temp1); TUHEX_StageActual = static_cast(EpForce); TUHEX_Active = TUHEX_StageActual > 0; // Ra 2014-11: na tym się wysypuje, ale nie wiem, w jakich warunkach SendCtrlToNext(VehicleCommand::brake, EpForce, CabActive); } // ************************************************************************************************* // Q: 20160713 // Aktualizacja ciśnienia w przewodzie zasilającym // ************************************************************************************************* void physics::Vehicle::UpdateScndPipePressure(double dt) { if (mainPipe.Pressure() > 1.0) { mainPipe.reservoir->Flow(-mainPipe.Pressure()*air.config.leakRate * dt); mainPipe.reservoir->Act(); } const double Spz = 0.5067; UpdateSpringBrake(dt); mainPipe.reservoir->Flow(brakes.distributor->GetHPFlow(mainPipe.Pressure(), dt)); // NOTE: condition disabled to allow the air flow from the main hose to the main tank as well if (/* ( ( Compressor > ScndPipePress ) && ( */ air.config.mainReservoirVolume > 0.0 /* ) ) || ( trainType == train_type::emu ) || ( trainType == train_type::dmu ) */) { double const dV = PF(air.MainReservoirPressure(), mainPipe.Pressure(), Spz) * dt; air.AddAir(dV / 1000.0); mainPipe.reservoir->Flow(-dV); } // the air the main reservoir hoses carry to and from the neighbours comes once all vehicles have stepped, UpdatePipeCouplers() mainPipe.SetCouplersPending(true); mainPipe.reservoir->Act(); mainPipe.SetPressure(mainPipe.reservoir->P()); if (mainPipe.Pressure() < -1) { mainPipe.SetPressure(-1); mainPipe.reservoir->CreatePress(-1); mainPipe.reservoir->Act(); } } // ************************************************************************************************* // yB: 20190906 // Aktualizacja ciśnienia w hamulcu sprezynowym // ************************************************************************************************* void physics::Vehicle::UpdateSpringBrake(double /*dt*/) { double BP = brakes.spring.pneumaticConnection ? brakes.cylinderPressure : 0; double MSP = brakes.spring.shutOff ? 0 : brakes.spring.maxSetPressure; if (!brakes.spring.activate) { double desired_press = std::min(std::max(MSP, BP), mainPipe.reservoir->P()); double dv = PF(desired_press, brakes.spring.pressure, brakes.spring.valveOffArea); brakes.spring.cylinder->Flow(-dv); mainPipe.reservoir->Flow(std::max(dv, 0.0)); } else { double dv = PF(BP, brakes.spring.pressure, brakes.spring.valveOnArea); brakes.spring.cylinder->Flow(-dv); } if (brakes.spring.pressure > brakes.spring.resetPressure) brakes.spring.isReady = true; brakes.spring.isActive = brakes.spring.pressure < (brakes.spring.isActive ? brakes.spring.pressureOff : brakes.spring.pressureOn); brakes.spring.release = false; brakes.spring.cylinder->Act(); brakes.spring.pressure = brakes.spring.cylinder->P(); } namespace { // whether the air flowing to a coupled vehicle along a pipe wakes its physics: any flow did, but a vehicle standing next to one // whose physics is on gets a trickle of it all the time, as their pressures settle, and each wake kept it on for 5 s. with // Global.physics_pipewake only a flow which leaves its pressure, with what has flown in while it slept, that much away from the // pressure of the vehicle the air comes from, or which has changed it by that much since it fell asleep: a pressure followed in // small steps would never reach the valves of a vehicle asleep otherwise bool pipe_wakes(double const Flow, double const Pressure, TReservoir const &Reservoir) { if (Flow * Flow <= 0.00000000000001) return false; return (Global.physics_pipewake <= 0.f) || (std::abs(Pressure - Reservoir.P_pending()) > Global.physics_pipewake) || (std::abs(Reservoir.P_pending() - Reservoir.P_applied()) > Global.physics_pipewake); } // the pressure in the pipe of a coupled vehicle the flow to it goes by. the pressure the vehicle keeps is the one of its last step: // with Global.physics_pipewake one asleep can take air for a long time without waking, and the flow, going by that pressure, would // carry on past the pressure of the vehicle it comes from. so of one asleep, the pressure with what has flown in since double neighbour_pipe_pressure(physics::Vehicle const &Neighbour, double const Pressure, TReservoir const &Reservoir) { if ((Global.physics_pipewake <= 0.f) || Neighbour.PhysicActivation()) return Pressure; return Reservoir.P_pending(); } } // namespace // ************************************************************************************************* // Q: 20160715 // oblicza i zwraca przepływ powietrza pomiędzy pojazdami // ************************************************************************************************* double physics::Vehicle::GetDVc(double dt) { // physics::Vehicle *c; physics::Vehicle *c; double dv1; double dv2; // , dV; dv1 = 0; dv2 = 0; // sprzeg 1 if (Couplers[0].Connected != nullptr && Couplers[0].CouplingFlag.HasAll(Coupling::brakeHose)) { //*0.85 c = Couplers[0].Connected; // skrot //0.08 //e/D * L/D = e/D^2 * L dv1 = 0.5 * dt * PF(brakePipe.Pressure(), neighbour_pipe_pressure(*c, c->brakePipe.Pressure(), *c->brakePipe.reservoir), brakes.config.pipeSection / (1.0 + 0.015 / brakes.config.pipeSection * Dim.L)); c->brakePipe.reservoir->Flow(-dv1); if (pipe_wakes(dv1, brakePipe.Pressure(), *c->brakePipe.reservoir)) c->switch_physics(true, wake_cause::mainpipe); } // sprzeg 2 if (Couplers[1].Connected != nullptr && Couplers[1].CouplingFlag.HasAll(Coupling::brakeHose)) { c = Couplers[1].Connected; // skrot dv2 = 0.5 * dt * PF(brakePipe.Pressure(), neighbour_pipe_pressure(*c, c->brakePipe.Pressure(), *c->brakePipe.reservoir), brakes.config.pipeSection / (1.0 + 0.015 / brakes.config.pipeSection * Dim.L)); c->brakePipe.reservoir->Flow(-dv2); if (pipe_wakes(dv2, brakePipe.Pressure(), *c->brakePipe.reservoir)) c->switch_physics(true, wake_cause::mainpipe); } // if ((Couplers[1].Connected != NULL) && (Couplers[0].Connected != NULL)) // if ((Couplers[0].CouplingFlag.HasAll(Coupling::brakeHose)) && // (Couplers[1].CouplingFlag.HasAll(Coupling::brakeHose))) // { // dV = 0.05 * dt * PF(Couplers[0].Connected->PipePress, Couplers[1].Connected->PipePress, // (Spg * 0.85) / (1 + 0.03 * Dim.L)) * // 0; // ktoś mi powie jaki jest sens tego bloku jeśli przepływ mnożony przez zero? // Couplers[0].Connected->Pipe->Flow(+dV); // Couplers[1].Connected->Pipe->Flow(-dV); // } // suma return dv2 + dv1; } // ************************************************************************************************* // the air the main reservoir hoses carry between the vehicle's pipe and the neighbours' in dt: the neighbours' // share is sent to them, the vehicle's own returned // ************************************************************************************************* double physics::Vehicle::GetScndDVc(double dt) { const double Spz = 0.5067; physics::Vehicle *c; double dv1 = 0; double dv2 = 0; // sprzeg 1 if (Couplers[0].Connected != nullptr && Couplers[0].CouplingFlag.HasAll(Coupling::mainHose)) { c = Couplers[0].Connected; // skrot dv1 = 0.5 * dt * PF(mainPipe.Pressure(), neighbour_pipe_pressure(*c, c->mainPipe.Pressure(), *c->mainPipe.reservoir), Spz * 0.75); c->mainPipe.reservoir->Flow(-dv1); if (pipe_wakes(dv1, mainPipe.Pressure(), *c->mainPipe.reservoir)) c->switch_physics(true, wake_cause::scndpipe); } // sprzeg 2 if (Couplers[1].Connected != nullptr && Couplers[1].CouplingFlag.HasAll(Coupling::mainHose)) { c = Couplers[1].Connected; // skrot dv2 = 0.5 * dt * PF(mainPipe.Pressure(), neighbour_pipe_pressure(*c, c->mainPipe.Pressure(), *c->mainPipe.reservoir), Spz * 0.75); c->mainPipe.reservoir->Flow(-dv2); if (pipe_wakes(dv2, mainPipe.Pressure(), *c->mainPipe.reservoir)) c->switch_physics(true, wake_cause::scndpipe); } if (Couplers[1].Connected != nullptr && Couplers[0].Connected != nullptr && Couplers[0].CouplingFlag.HasAll(Coupling::mainHose) && Couplers[1].CouplingFlag.HasAll(Coupling::mainHose)) { double const dV = 0.00025 * dt * PF(Couplers[0].Connected->mainPipe.Pressure(), Couplers[1].Connected->mainPipe.Pressure(), Spz * 0.25); Couplers[0].Connected->mainPipe.reservoir->Flow(+dV); Couplers[1].Connected->mainPipe.reservoir->Flow(-dV); } return dv1 + dv2; } namespace { // a pipe along the train, whose air the hoses between the vehicles carry (physics::Vehicle::UpdatePipeCouplers()) struct train_pipe { physics::TrainPipe physics::Vehicle::*pipe; // the reservoir, the pressure and whether the exchange through the hoses is yet to come CouplingSet hose; // the coupling of the pipe's hose double (physics::Vehicle::*exchange)(double); // the air the hoses carry in a step, the neighbours' share sent to them double (*area)(physics::Vehicle const &); // the cross-section of the hose, as the exchange takes it }; // the brake pipe, GetDVc() train_pipe const brake_pipe{&physics::Vehicle::brakePipe, Coupling::brakeHose, &physics::Vehicle::GetDVc, [](physics::Vehicle const &Vehicle) { return Vehicle.brakes.config.pipeSection / (1.0 + 0.015 / Vehicle.brakes.config.pipeSection * Vehicle.Dim.L); }}; // the main reservoir pipe, GetScndDVc() train_pipe const main_reservoir_pipe{&physics::Vehicle::mainPipe, Coupling::mainHose, &physics::Vehicle::GetScndDVc, [](physics::Vehicle const &) { return 0.5067 * 0.75; }}; // the number of sub-steps of dt the air exchange through the hoses of the vehicle's pipe needs: in one, the air a // hose carries stays below what evens out the pipes on its ends int hose_substeps(physics::Vehicle const &Vehicle, train_pipe const &trainPipe, double const dt) { auto substeps{1}; auto const area{trainPipe.area(Vehicle)}; auto const capacity{(Vehicle.*trainPipe.pipe).reservoir->GetCap()}; for (auto const &hose : Vehicle.Couplers) { if (hose.Connected == nullptr || false == hose.CouplingFlag.HasAll(trainPipe.hose)) { continue; } auto const &other{*hose.Connected}; auto const othercapacity{(other.*trainPipe.pipe).reservoir->GetCap()}; // the flow per bar of the difference, at small differences, where it's the largest auto const pressure{std::max((Vehicle.*trainPipe.pipe).Pressure(), (other.*trainPipe.pipe).Pressure())}; auto const conductance{std::abs(PF(pressure, pressure - 0.01, area)) / 0.01}; auto const equalising{capacity * othercapacity / (capacity + othercapacity)}; substeps = std::max(substeps, static_cast(std::ceil(dt * conductance / equalising))); } return std::min(substeps, 10); } // the vehicles of the list which stepped the pipe, each with the number of the train it is in: the ones joined by the hoses // of the pipe, directly, numbered in the order of their first vehicle in the list. a vehicle which didn't step the pipe // (its physics sleeps) takes the air sent to it without passing it on, so it ends a train std::vector hose_trains(std::vector const &Vehicles, train_pipe const &trainPipe) { auto constexpr none{std::numeric_limits::max()}; std::vector trains(Vehicles.size(), none); // the vehicles by their address, for finding the neighbours in the list std::vector> byaddress; byaddress.reserve(Vehicles.size()); for (std::size_t index = 0; index < Vehicles.size(); ++index) { if ((Vehicles[index]->*trainPipe.pipe).CouplersPending()) { byaddress.emplace_back(Vehicles[index], index); } } std::ranges::sort(byaddress); auto const find = [&byaddress](physics::Vehicle const *Vehicle) { auto const found{std::ranges::lower_bound(byaddress, Vehicle, std::less<>{}, &std::pair::first)}; return (found != byaddress.end() && found->first == Vehicle) ? found->second : none; }; std::size_t count{0}; std::vector stack; for (std::size_t first = 0; first < Vehicles.size(); ++first) { if (false == (Vehicles[first]->*trainPipe.pipe).CouplersPending() || trains[first] != none) { continue; } trains[first] = count; stack.assign(1, first); while (false == stack.empty()) { auto const &vehicle{*Vehicles[stack.back()]}; stack.pop_back(); for (auto const &hose : vehicle.Couplers) { if (hose.Connected == nullptr || false == hose.CouplingFlag.HasAll(trainPipe.hose)) { continue; } if (auto const other{find(hose.Connected)}; other != none && trains[other] == none) { trains[other] = count; stack.push_back(other); } } } ++count; } return trains; } // the air exchange through the hoses of the pipe, for the vehicles which stepped it since the last call, in the order // they stepped: each train in as many sub-steps of dt as its vehicle needing the most of them takes, each going through // its vehicles one after another, as the step of the vehicles does. the air of a train stays in it, so a train comes out // the same whichever other vehicles are exchanged in the same call: all of them, or the ones of a group of // vehicle_table::update_steps_parallel() void exchange_through_hoses(std::vector const &Vehicles, train_pipe const &trainPipe, double const dt) { auto const trains{hose_trains(Vehicles, trainPipe)}; std::vector substeps; for (std::size_t index = 0; index < Vehicles.size(); ++index) { if (false == (Vehicles[index]->*trainPipe.pipe).CouplersPending()) { continue; } if (substeps.size() <= trains[index]) { substeps.resize(trains[index] + 1, 1); } substeps[trains[index]] = std::max(substeps[trains[index]], hose_substeps(*Vehicles[index], trainPipe, dt)); } // the vehicles of each train, in the order of the list: the ones of train t are members[firsts[t]] up to // members[firsts[t + 1]], not included. going through them alone, a sub-step of a train costs its own vehicles, // not a look at every vehicle of the list (all of the scenery's, in update_brake_pipes()), and takes them in the // same order as that did std::vector firsts(substeps.size() + 1, 0); for (auto const train : trains) { if (train < substeps.size()) { ++firsts[train + 1]; } } for (std::size_t train = 0; train < substeps.size(); ++train) { firsts[train + 1] += firsts[train]; } std::vector members(firsts.back()); { auto next{firsts}; for (std::size_t index = 0; index < Vehicles.size(); ++index) { if (trains[index] < substeps.size()) { members[next[trains[index]]++] = index; } } } for (std::size_t train = 0; train < substeps.size(); ++train) { auto const substep{dt / substeps[train]}; for (auto i = 0; i < substeps[train]; ++i) { for (auto member = firsts[train]; member < firsts[train + 1]; ++member) { auto *vehicle{Vehicles[members[member]]}; auto &pipe{*(vehicle->*trainPipe.pipe).reservoir}; pipe.Act(); // what the neighbours have sent in the meantime (vehicle->*trainPipe.pipe).SetPressure(pipe.P()); pipe.Flow((vehicle->*trainPipe.exchange)(substep)); pipe.Act(); (vehicle->*trainPipe.pipe).SetPressure(pipe.P()); } } } for (auto *vehicle : Vehicles) { (vehicle->*trainPipe.pipe).SetCouplersPending(false); } } } // namespace // ************************************************************************************************* // the number of sub-steps of dt the air exchange through the brake hoses of the vehicle needs. taken in a single // step of 0.01 s, the hose between two 14 m freight wagons carries twice the air which evens out their pipes, the // pipes overshoot each other in turns and the distributors see the pressure up to 0.28 bar off what's in the pipe // (eu07/maszyna#368) // ************************************************************************************************* int physics::Vehicle::PipeCouplersSubsteps(double const dt) const { return hose_substeps(*this, brake_pipe, dt); } // ************************************************************************************************* // the number of sub-steps of dt the air exchange through the main reservoir hoses of the vehicle needs. taken in a // single step of 0.01 s, the hose carries about 1.5 times the air which evens out the pipes on its ends; along with // the vehicles' own main reservoirs feeding the pipe in the same step, the pipes of the middle vehicles of a // multiple unit or of locomotives in multiple traction kept jumping 0.8 bar to and fro between the steps // ************************************************************************************************* int physics::Vehicle::ScndPipeCouplersSubsteps(double const dt) const { return hose_substeps(*this, main_reservoir_pipe, dt); } // ************************************************************************************************* // the air exchange through the brake and the main reservoir hoses of the vehicles which stepped their pipes // (UpdatePipePressure(), UpdateScndPipePressure()) since the last call, each pipe in its own sub-steps // ************************************************************************************************* void physics::Vehicle::UpdatePipeCouplers(std::vector const &Vehicles, double const dt) { exchange_through_hoses(Vehicles, brake_pipe, dt); exchange_through_hoses(Vehicles, main_reservoir_pipe, dt); } bool physics::Vehicle::UpdateStopBrakeRelease(double const Demand, double const Dt) { if (Demand <= 0.0) { brakes.holding.releaseTimer = 0.0; return false; } // the step the demand rises above 0 is time 0 of it, so without the delay the brake lets go at once, as it always did auto const released{brakes.holding.releaseTimer >= brakes.holding.releaseDelay}; brakes.holding.releaseTimer += Dt; return released; }