Skip to content

Commit d7b38a5

Browse files
authored
Merge pull request #870 from cesarBLG/fix-water-restore
Fix water restore
2 parents 5f7e609 + 5cb32fa commit d7b38a5

File tree

1 file changed

+33
-52
lines changed

1 file changed

+33
-52
lines changed

Source/Orts.Simulation/Simulation/RollingStocks/MSTSSteamLocomotive.cs

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,6 @@ public class MSTSSteamLocomotive : MSTSLocomotive
155155
float PrevCombinedTenderWaterVolumeUKG;
156156
float PreviousTenderWaterVolumeUKG;
157157
public float MaxLocoTenderWaterMassKG = 1; // Maximum read from Eng file - this value must be non-zero, if not defined in ENG file, can cause NaN errors
158-
float RestoredMaxTotalCombinedWaterVolumeUKG; // Values to restore after game save
159-
float RestoredCombinedTenderWaterVolumeUKG; // Values to restore after game save
160158

161159
// Tender
162160

@@ -184,7 +182,6 @@ public class MSTSSteamLocomotive : MSTSLocomotive
184182
float baseStartTempK; // Starting water temp
185183
float StartBoilerHeatBTU;
186184
public float BoilerMassLB; // current total mass of water and steam in boiler (changes as boiler usage changes)
187-
bool RestoredGame = false; // Flag to indicate that game is being restored. This will stop some values from being "initialised", as this will overwrite restored values.
188185

189186
float BoilerKW; // power of boiler
190187
float MaxBoilerKW; // power of boiler at full performance
@@ -971,15 +968,14 @@ public override void Copy(MSTSWagon copy)
971968
/// </summary>
972969
public override void Save(BinaryWriter outf)
973970
{
974-
outf.Write(RestoredGame);
975971
outf.Write(BoilerHeatOutBTUpS);
976972
outf.Write(BoilerHeatInBTUpS);
977973
outf.Write(PreviousBoilerHeatOutBTUpS);
978974
outf.Write(PreviousBoilerHeatSmoothedBTU);
979975
outf.Write(BurnRateRawKGpS);
980976
outf.Write(TenderCoalMassKG);
981-
outf.Write(RestoredMaxTotalCombinedWaterVolumeUKG);
982-
outf.Write(RestoredCombinedTenderWaterVolumeUKG);
977+
outf.Write(MaxTotalCombinedWaterVolumeUKG);
978+
outf.Write(CombinedTenderWaterVolumeUKG);
983979
outf.Write(CumulativeWaterConsumptionLbs);
984980
outf.Write(CurrentAuxTenderWaterVolumeUKG);
985981
outf.Write(CurrentLocoTenderWaterVolumeUKG);
@@ -1035,15 +1031,14 @@ public override void Save(BinaryWriter outf)
10351031
/// </summary>
10361032
public override void Restore(BinaryReader inf)
10371033
{
1038-
RestoredGame = inf.ReadBoolean();
10391034
BoilerHeatOutBTUpS = inf.ReadSingle();
10401035
BoilerHeatInBTUpS = inf.ReadSingle();
10411036
PreviousBoilerHeatOutBTUpS = inf.ReadSingle();
10421037
PreviousBoilerHeatSmoothedBTU = inf.ReadSingle();
10431038
BurnRateRawKGpS = inf.ReadSingle();
10441039
TenderCoalMassKG = inf.ReadSingle();
1045-
RestoredMaxTotalCombinedWaterVolumeUKG = inf.ReadSingle();
1046-
RestoredCombinedTenderWaterVolumeUKG = inf.ReadSingle();
1040+
MaxTotalCombinedWaterVolumeUKG = inf.ReadSingle();
1041+
CombinedTenderWaterVolumeUKG = inf.ReadSingle();
10471042
CumulativeWaterConsumptionLbs = inf.ReadSingle();
10481043
CurrentAuxTenderWaterVolumeUKG = inf.ReadSingle();
10491044
CurrentLocoTenderWaterVolumeUKG = inf.ReadSingle();
@@ -1450,12 +1445,6 @@ public override void Initialize()
14501445

14511446
MaxTotalCombinedWaterVolumeUKG = (Kg.ToLb(MaxLocoTenderWaterMassKG) / WaterLBpUKG); // Initialise loco with tender water only - will be updated as appropriate
14521447

1453-
if (RestoredCombinedTenderWaterVolumeUKG > 1.0)// Check to see if this is a restored game -(assumed so if Restored >0), then set water controller values based upon saved values
1454-
{
1455-
MaxTotalCombinedWaterVolumeUKG = RestoredMaxTotalCombinedWaterVolumeUKG;
1456-
CombinedTenderWaterVolumeUKG = RestoredCombinedTenderWaterVolumeUKG;
1457-
}
1458-
14591448
InitializeTenderWithWater();
14601449

14611450
InitializeTenderWithCoal();
@@ -1509,11 +1498,9 @@ public override void Initialize()
15091498
Trace.TraceWarning("Evaporation Area not found in ENG file and has been set to {0} m^2", EvaporationAreaM2); // Advise player that Evaporation Area is missing from ENG file
15101499
}
15111500

1512-
if (!RestoredGame) // If this is not a restored game, then initialise these values
1513-
{
1514-
CylinderSteamUsageLBpS = 1.0f; // Set to 1 to ensure that there are no divide by zero errors
1515-
WaterFraction = 0.9f; // Initialise boiler water level at 90%
1516-
}
1501+
CylinderSteamUsageLBpS = 1.0f; // Set to 1 to ensure that there are no divide by zero errors
1502+
WaterFraction = 0.9f; // Initialise boiler water level at 90%
1503+
15171504
float MaxWaterFraction = 0.9f; // Initialise the max water fraction when the boiler starts
15181505
if (BoilerEvapRateLbspFt2 == 0) // If boiler evaporation rate is not in ENG file then set a default value
15191506
{
@@ -1895,39 +1882,36 @@ public override void Initialize()
18951882

18961883
// Initialise Locomotive in a Hot or Cold Start Condition
18971884

1898-
if (!RestoredGame) // Only initialise the following values if game is not being restored.
1885+
if (HotStart)
18991886
{
1900-
if (HotStart)
1901-
{
1902-
// Hot Start - set so that FlueTemp is at maximum, boilerpressure slightly below max
1903-
BoilerPressurePSI = MaxBoilerPressurePSI - 5.0f;
1904-
baseStartTempK = C.ToK(C.FromF(PressureToTemperaturePSItoF[BoilerPressurePSI]));
1905-
BoilerStartkW = Kg.FromLb((BoilerPressurePSI / MaxBoilerPressurePSI) * TheoreticalMaxSteamOutputLBpS) * W.ToKW(W.FromBTUpS(SteamHeatPSItoBTUpLB[BoilerPressurePSI])); // Given pressure is slightly less then max, this figure should be slightly less, ie reduce TheoreticalMaxSteamOutputLBpS, for the time being assume a ratio of bp to MaxBP
1906-
FlueTempK = (BoilerStartkW / (W.ToKW(BoilerHeatTransferCoeffWpM2K) * EvaporationAreaM2 * HeatMaterialThicknessFactor)) + baseStartTempK;
1907-
BoilerMassLB = WaterFraction * BoilerVolumeFT3 * WaterDensityPSItoLBpFT3[BoilerPressurePSI] + (1 - WaterFraction) * BoilerVolumeFT3 * SteamDensityPSItoLBpFT3[BoilerPressurePSI];
1908-
BoilerHeatBTU = WaterFraction * BoilerVolumeFT3 * WaterDensityPSItoLBpFT3[BoilerPressurePSI] * WaterHeatPSItoBTUpLB[BoilerPressurePSI] + (1 - WaterFraction) * BoilerVolumeFT3 * SteamDensityPSItoLBpFT3[BoilerPressurePSI] * SteamHeatPSItoBTUpLB[BoilerPressurePSI];
1909-
StartBoilerHeatBTU = BoilerHeatBTU;
1910-
}
1911-
else
1912-
{
1913-
// Cold Start - as per current
1914-
BoilerPressurePSI = MaxBoilerPressurePSI * 0.66f; // Allow for cold start - start at 66% of max boiler pressure - check pressure value given heat in boiler????
1915-
baseStartTempK = C.ToK(C.FromF(PressureToTemperaturePSItoF[BoilerPressurePSI]));
1916-
BoilerStartkW = Kg.FromLb((BoilerPressurePSI / MaxBoilerPressurePSI) * TheoreticalMaxSteamOutputLBpS) * W.ToKW(W.FromBTUpS(SteamHeatPSItoBTUpLB[BoilerPressurePSI]));
1917-
FlueTempK = (BoilerStartkW / (W.ToKW(BoilerHeatTransferCoeffWpM2K) * EvaporationAreaM2 * HeatMaterialThicknessFactor)) + baseStartTempK;
1918-
BoilerMassLB = WaterFraction * BoilerVolumeFT3 * WaterDensityPSItoLBpFT3[BoilerPressurePSI] + (1 - WaterFraction) * BoilerVolumeFT3 * SteamDensityPSItoLBpFT3[BoilerPressurePSI];
1919-
BoilerHeatBTU = WaterFraction * BoilerVolumeFT3 * WaterDensityPSItoLBpFT3[BoilerPressurePSI] * WaterHeatPSItoBTUpLB[BoilerPressurePSI] + (1 - WaterFraction) * BoilerVolumeFT3 * SteamDensityPSItoLBpFT3[BoilerPressurePSI] * SteamHeatPSItoBTUpLB[BoilerPressurePSI];
1920-
}
1887+
// Hot Start - set so that FlueTemp is at maximum, boilerpressure slightly below max
1888+
BoilerPressurePSI = MaxBoilerPressurePSI - 5.0f;
1889+
baseStartTempK = C.ToK(C.FromF(PressureToTemperaturePSItoF[BoilerPressurePSI]));
1890+
BoilerStartkW = Kg.FromLb((BoilerPressurePSI / MaxBoilerPressurePSI) * TheoreticalMaxSteamOutputLBpS) * W.ToKW(W.FromBTUpS(SteamHeatPSItoBTUpLB[BoilerPressurePSI])); // Given pressure is slightly less then max, this figure should be slightly less, ie reduce TheoreticalMaxSteamOutputLBpS, for the time being assume a ratio of bp to MaxBP
1891+
FlueTempK = (BoilerStartkW / (W.ToKW(BoilerHeatTransferCoeffWpM2K) * EvaporationAreaM2 * HeatMaterialThicknessFactor)) + baseStartTempK;
1892+
BoilerMassLB = WaterFraction * BoilerVolumeFT3 * WaterDensityPSItoLBpFT3[BoilerPressurePSI] + (1 - WaterFraction) * BoilerVolumeFT3 * SteamDensityPSItoLBpFT3[BoilerPressurePSI];
1893+
BoilerHeatBTU = WaterFraction * BoilerVolumeFT3 * WaterDensityPSItoLBpFT3[BoilerPressurePSI] * WaterHeatPSItoBTUpLB[BoilerPressurePSI] + (1 - WaterFraction) * BoilerVolumeFT3 * SteamDensityPSItoLBpFT3[BoilerPressurePSI] * SteamHeatPSItoBTUpLB[BoilerPressurePSI];
1894+
StartBoilerHeatBTU = BoilerHeatBTU;
1895+
}
1896+
else
1897+
{
1898+
// Cold Start - as per current
1899+
BoilerPressurePSI = MaxBoilerPressurePSI * 0.66f; // Allow for cold start - start at 66% of max boiler pressure - check pressure value given heat in boiler????
1900+
baseStartTempK = C.ToK(C.FromF(PressureToTemperaturePSItoF[BoilerPressurePSI]));
1901+
BoilerStartkW = Kg.FromLb((BoilerPressurePSI / MaxBoilerPressurePSI) * TheoreticalMaxSteamOutputLBpS) * W.ToKW(W.FromBTUpS(SteamHeatPSItoBTUpLB[BoilerPressurePSI]));
1902+
FlueTempK = (BoilerStartkW / (W.ToKW(BoilerHeatTransferCoeffWpM2K) * EvaporationAreaM2 * HeatMaterialThicknessFactor)) + baseStartTempK;
1903+
BoilerMassLB = WaterFraction * BoilerVolumeFT3 * WaterDensityPSItoLBpFT3[BoilerPressurePSI] + (1 - WaterFraction) * BoilerVolumeFT3 * SteamDensityPSItoLBpFT3[BoilerPressurePSI];
1904+
BoilerHeatBTU = WaterFraction * BoilerVolumeFT3 * WaterDensityPSItoLBpFT3[BoilerPressurePSI] * WaterHeatPSItoBTUpLB[BoilerPressurePSI] + (1 - WaterFraction) * BoilerVolumeFT3 * SteamDensityPSItoLBpFT3[BoilerPressurePSI] * SteamHeatPSItoBTUpLB[BoilerPressurePSI];
1905+
}
19211906

1922-
WaterTempNewK = C.ToK(C.FromF(PressureToTemperaturePSItoF[BoilerPressurePSI])); // Initialise new boiler pressure
1923-
FireMassKG = IdealFireMassKG;
1907+
WaterTempNewK = C.ToK(C.FromF(PressureToTemperaturePSItoF[BoilerPressurePSI])); // Initialise new boiler pressure
1908+
FireMassKG = IdealFireMassKG;
19241909

1925-
BoilerSteamHeatBTUpLB = SteamHeatPSItoBTUpLB[BoilerPressurePSI];
1926-
BoilerWaterHeatBTUpLB = WaterHeatPSItoBTUpLB[BoilerPressurePSI];
1927-
BoilerSteamDensityLBpFT3 = SteamDensityPSItoLBpFT3[BoilerPressurePSI];
1928-
BoilerWaterDensityLBpFT3 = WaterDensityPSItoLBpFT3[BoilerPressurePSI];
1910+
BoilerSteamHeatBTUpLB = SteamHeatPSItoBTUpLB[BoilerPressurePSI];
1911+
BoilerWaterHeatBTUpLB = WaterHeatPSItoBTUpLB[BoilerPressurePSI];
1912+
BoilerSteamDensityLBpFT3 = SteamDensityPSItoLBpFT3[BoilerPressurePSI];
1913+
BoilerWaterDensityLBpFT3 = WaterDensityPSItoLBpFT3[BoilerPressurePSI];
19291914

1930-
}
19311915
DamperFactorManual = TheoreticalMaxSteamOutputLBpS / SpeedEquivMpS; // Calculate a factor for damper control that will vary with speed.
19321916
BlowerSteamUsageFactor = 0.04f * MaxBoilerOutputLBpH / 3600 / MaxBoilerPressurePSI;
19331917

@@ -1986,7 +1970,6 @@ public override void Initialize()
19861970
Trace.TraceInformation("========================================================================================================================================================");
19871971

19881972
}
1989-
RestoredGame = true; // Set flag for restored game indication
19901973
#endregion
19911974
}
19921975

@@ -2779,8 +2762,6 @@ private void UpdateTender(float elapsedClockSeconds)
27792762
}
27802763

27812764
TenderWaterPercent = CombinedTenderWaterVolumeUKG / MaxTotalCombinedWaterVolumeUKG; // Calculate the current % of water in tender
2782-
RestoredMaxTotalCombinedWaterVolumeUKG = MaxTotalCombinedWaterVolumeUKG;
2783-
RestoredCombinedTenderWaterVolumeUKG = CombinedTenderWaterVolumeUKG;
27842765
CurrentAuxTenderWaterVolumeUKG = (Kg.ToLb(Train.MaxAuxTenderWaterMassKG) / WaterLBpUKG) * TenderWaterPercent; // Adjust water level in aux tender
27852766
CurrentLocoTenderWaterVolumeUKG = (Kg.ToLb(MaxLocoTenderWaterMassKG) / WaterLBpUKG) * TenderWaterPercent; // Adjust water level in locomotive tender
27862767
PrevCombinedTenderWaterVolumeUKG = CombinedTenderWaterVolumeUKG; // Store value for next iteration

0 commit comments

Comments
 (0)