/
FuelFlowSimulation.cs
278 lines (228 loc) · 9.32 KB
/
FuelFlowSimulation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using System;
using System.Collections.Generic;
using MechJebLib.Simulations.PartModules;
using static MechJebLib.Utils.Statics;
#nullable enable
namespace MechJebLib.Simulations
{
// TODO:
// - residuals
// - spoolup time
// - the NO_FLOW ablator fix
// - the srb residual fuel issue
// - isSepratron check in AllowedToStage()
// - crew mass
// - cosLoss
// - "free" resources like air intake
// - git rid of the annoying extra stage
// - use objectpooling
// - use vessel modification callbacks
public class FuelFlowSimulation
{
private const int MAXSTEPS = 100;
public readonly List<FuelStats> Segments = new List<FuelStats>();
private readonly SimVessel _vessel;
private readonly List<SimPart> _sources = new List<SimPart>();
private FuelStats? _currentSegment;
private double _time;
public FuelFlowSimulation(SimVessel vessel)
{
_vessel = vessel;
}
public void Run()
{
_time = 0;
_currentSegment = null;
Segments.Clear();
_vessel.MainThrottle = 1.0;
Log($"starting stage: {_vessel.CurrentStage}");
while (_vessel.CurrentStage >= 0)
{
Log($"current stage: {_vessel.CurrentStage}");
SimulateStage();
FinishSegment();
_vessel.Stage();
}
}
private void SimulateStage()
{
UpdateEngineStats();
UpdateActiveEngines();
UpdateResourceDrainsAndResiduals();
GetNextSegment();
double currentThrust = _vessel.ThrustCurrent.magnitude;
Log("starting stage");
for (int steps = MAXSTEPS; steps > 0; steps--)
{
if (AllowedToStage())
{
Log("allowed to stage");
return;
}
double dt = MinimumTimeStep();
if (Math.Abs(_vessel.ThrustCurrent.magnitude - currentThrust) / currentThrust > 1e-4 && dt > 0.1)
{
FinishSegment();
GetNextSegment();
currentThrust = _vessel.ThrustCurrent.magnitude;
}
_time += dt;
ApplyResourceDrains(dt);
UpdateEngineStats();
UpdateActiveEngines();
UpdateResourceDrainsAndResiduals();
Log($"took timestep of {dt}");
}
throw new Exception("FuelFlowSimulation hit max steps of " + MAXSTEPS + " steps");
}
private void UpdateEngineStats()
{
_vessel.UpdateEngineStats();
}
private void ApplyResourceDrains(double dt)
{
for (int i = 0; i < _vessel.Parts.Count; i++)
{
SimPart p = _vessel.Parts[i];
p.ApplyResourceDrains(dt);
}
_vessel.UpdateMass();
}
private void UpdateResourceDrainsAndResiduals()
{
for (int i = 0; i < _vessel.Parts.Count; i++)
{
SimPart p = _vessel.Parts[i];
p.ClearResourceDrains();
}
for (int i = 0; i < _vessel.ActiveEngines.Count; i++)
{
SimModuleEngines e = _vessel.ActiveEngines[i];
foreach (int resourceId in e.PropellantFlowModes.Keys)
switch (e.PropellantFlowModes[resourceId])
{
case SimFlowMode.NO_FLOW:
UpdateResourceDrainsInPart(e.Part, e.ResourceConsumptions[resourceId], resourceId);
e.Part.UpdateResourceResidual(e.ModuleResiduals, resourceId);
break;
case SimFlowMode.ALL_VESSEL:
case SimFlowMode.ALL_VESSEL_BALANCE:
UpdateResourceDrainsInParts(_vessel.Parts, e.ResourceConsumptions[resourceId], resourceId, false);
UpdateResourceResidualsInParts(_vessel.Parts, e.ModuleResiduals, resourceId);
break;
case SimFlowMode.STAGE_PRIORITY_FLOW:
case SimFlowMode.STAGE_PRIORITY_FLOW_BALANCE:
UpdateResourceDrainsInParts(_vessel.Parts, e.ResourceConsumptions[resourceId], resourceId, true);
UpdateResourceResidualsInParts(_vessel.Parts, e.ModuleResiduals, resourceId);
break;
case SimFlowMode.STAGE_STACK_FLOW:
case SimFlowMode.STAGE_STACK_FLOW_BALANCE:
case SimFlowMode.STACK_PRIORITY_SEARCH:
UpdateResourceDrainsInParts(e.Part.CrossFeedPartSet, e.ResourceConsumptions[resourceId], resourceId, true);
UpdateResourceResidualsInParts(e.Part.CrossFeedPartSet, e.ModuleResiduals, resourceId);
break;
case SimFlowMode.NULL:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
private void UpdateResourceDrainsInParts(IList<SimPart> parts, double resourceConsumption, int resourceId, bool usePriority)
{
int maxPriority = int.MinValue;
_sources.Clear();
for (int i = 0; i < parts.Count; i++)
{
SimPart p = parts[i];
SimResource? resource = p.GetResource(resourceId);
if (resource == null)
continue;
if (resource.Free)
continue;
if (resource.Amount < p.ResourceRequestRemainingThreshold) continue;
if (usePriority)
{
if (p.ResourcePriority < maxPriority)
continue;
if (p.ResourcePriority > maxPriority)
{
_sources.Clear();
maxPriority = p.ResourcePriority;
}
}
_sources.Add(p);
}
for (int i = 0; i < _sources.Count; i++)
UpdateResourceDrainsInPart(_sources[i], resourceConsumption / _sources.Count, resourceId);
}
private void UpdateResourceDrainsInPart(SimPart p, double resourceConsumption, int resourceId)
{
p.AddResourceDrain(resourceId, resourceConsumption);
}
private void UpdateResourceResidualsInParts(IList<SimPart> parts, double residual, int resourceId)
{
foreach (SimPart part in parts)
part.UpdateResourceResidual(residual, resourceId);
}
private double MinimumTimeStep()
{
double maxTime = _vessel.ResourceMaxTime();
return maxTime < double.MaxValue ? maxTime : 0;
}
private void UpdateActiveEngines()
{
_vessel.UpdateActiveEngines();
}
private void FinishSegment()
{
Log("FinishSegment() called");
if (_currentSegment is null)
return;
_currentSegment.DeltaTime = _time - _currentSegment.StartTime;
_currentSegment.EndMass = _vessel.Mass;
_currentSegment.ComputeStats();
Segments.Add(_currentSegment);
}
private void GetNextSegment()
{
Log("GetNextSegment() called");
if (!(_currentSegment is null))
_currentSegment.StagedMass = _currentSegment.EndMass - _vessel.Mass;
_currentSegment = new FuelStats
{
KSPStage = _vessel.CurrentStage, Thrust = _vessel.ThrustCurrent.magnitude, StartTime = _time, StartMass = _vessel.Mass
};
}
// FIXME: This needs to check isSepratron, which is not implemented
private bool AllowedToStage()
{
// always stage if all the engines are burned out
if (_vessel.ActiveEngines.Count == 0) return true;
for (int i = 0; i < _vessel.ActiveEngines.Count; i++)
{
SimModuleEngines e = _vessel.ActiveEngines[i];
// never stage an active engine
if (e.Part.DecoupledInStage >= _vessel.CurrentStage - 1)
{
return false;
}
if (e.WouldDropAccessibleFuelTank(_vessel.CurrentStage - 1))
{
return false;
}
}
// do not trigger a stage that doesn't decouple anything until the engines burn out
bool dropsParts = false;
for (int i = 0; i < _vessel.Parts.Count; i++)
{
SimPart p = _vessel.Parts[i];
if (p.DecoupledInStage >= _vessel.CurrentStage - 1)
dropsParts = true;
}
if (!dropsParts) return false;
// we always stage at this point, except for the stage zero which we burn down until the engines go out
return _vessel.CurrentStage > 0;
}
}
}