-
Notifications
You must be signed in to change notification settings - Fork 22
/
AJEJet.cs
512 lines (425 loc) · 17.8 KB
/
AJEJet.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
using System;
using UnityEngine;
using SolverEngines;
using SolverEngines.EngineFitting;
namespace AJE
{
public class ModuleEnginesAJEJet : ModuleEnginesSolver, IModuleInfo, IEngineStatus, IFittableEngine, AnimationModules.INozzleArea, AnimationModules.IJetAfterburner
{
[EngineFitResult]
[KSPField(isPersistant = false, guiActive = false)]
public float Area = 0.1f;
[EngineParameter]
[KSPField(isPersistant = false, guiActive = false)]
public float BPR = 0;
[EngineParameter]
[KSPField(isPersistant = false, guiActive = false)]
public float CPR = 20;
[EngineParameter]
[KSPField(isPersistant = false, guiActive = false)]
public float FPR = 1;
[EngineParameter]
[KSPField(isPersistant = false, guiActive = false)]
public float Mdes = 0.9f;
[EngineParameter]
[KSPField(isPersistant = false, guiActive = false)]
public float Tdes = 250;
[EngineParameter]
[KSPField(isPersistant = false, guiActive = false)]
public float eta_c = 0.95f;
[EngineParameter]
[KSPField(isPersistant = false, guiActive = false)]
public float eta_t = 0.98f;
[EngineParameter]
[KSPField(isPersistant = false, guiActive = false)]
public float eta_n = 0.9f;
[EngineFitResult]
[KSPField(isPersistant = false, guiActive = false)]
public float FHV = 46.8E6f;
[EngineParameter]
[KSPField(isPersistant = false, guiActive = false)]
public float TIT = 1200;
[EngineFitResult]
[KSPField(isPersistant = false, guiActive = false)]
public float TAB = 0;
[EngineParameter]
[KSPField(isPersistant = false, guiActive = false)]
public bool exhaustMixer = false;
[EngineParameter]
[KSPField(isPersistant = false, guiActive = false)]
public bool adjustableNozzle = true;
[KSPField(isPersistant = false, guiActive = false)]
public bool unifiedThrottle = false;
[EngineParameter]
[KSPField(isPersistant = false, guiActive = false)]
public float defaultTPR = 1f;
[KSPField(isPersistant = false, guiActive = false)]
public float maxT3 = 9999;
[KSPField(isPersistant = false, guiActive = false)]
public bool intakeMatchArea = false;
[KSPField(isPersistant = false, guiActive = false)]
public float areaFudgeFactor = 0.75f;
[EngineFitResult]
public float minThrottle = 0.01f;
[EngineFitResult]
public float turbineAreaRatio = 0.75f;
[EngineFitData]
[KSPField(isPersistant = false, guiActive = false)]
public float drySFC = 0f;
[EngineFitData]
[KSPField(isPersistant = false, guiActive = false)]
public float dryThrust = 0f;
[EngineFitData]
[KSPField(isPersistant = false, guiActive = false)]
public float wetThrust = 0f;
[EngineFitData]
[KSPField(isPersistant = false, guiActive = false)]
public float idleNPR = 1.05f;
[KSPField(isPersistant = false, guiActive = false)]
public string spoolEffectName2 = "spool2";
[KSPField(isPersistant = false, guiActive = false)]
public string powerEffectName2 = "power2";
[KSPField]
public float throttleResponseMultiplier = 1f;
[KSPField(isPersistant = false, guiActive = true, guiName = "Compression Ratio", guiFormat = "F1")]
public float prat3 = 0f;
[KSPField(isPersistant = false, guiName = "Core Throttle", guiFormat = "N2", guiUnits = "%")]
public float actualCoreThrottle;
[KSPField(isPersistant = false, guiName = "Afterburner Throttle", guiFormat = "N2", guiUnits = "%")]
public float actualABThrottle;
[KSPField(isPersistant = false, guiActive = false)]
public bool isCentrifugalFlow = false;
[KSPField(isPersistant = false, guiActive = false)]
public FloatCurve centrifugalFlowMachEtaCurve;
[KSPField(isPersistant = false, guiActive = false)]
public FloatCurve centrifugalFlowMachTPRCurve;
[KSPField(isPersistant = false, guiActive = false)]
public double tt7_max = 4000d;
#if DEBUG
[KSPField(guiActive = true, guiName = "Nozzle Area", guiFormat = "F2", guiUnits = "m^2")]
public float nozzleArea;
[KSPField(guiActive = true, guiName = "Exhaust Temperature", guiFormat = "F1", guiUnits = "K")]
public float exhaustTemp;
[KSPField(guiActive = true, guiFormat = "F2")]
public double temp3;
[KSPField(guiActive = true, guiFormat = "P2")]
public double tpr1;
[KSPField(guiActive = true, guiFormat = "P2")]
public double etaCMach;
[KSPField(guiActive = true, guiFormat = "P2")]
public double tprMach;
#endif
private SolverJet solverJet;
public override void OnAwake()
{
base.OnAwake();
if (centrifugalFlowMachEtaCurve == null)
centrifugalFlowMachEtaCurve = new FloatCurve();
if (centrifugalFlowMachTPRCurve == null)
centrifugalFlowMachTPRCurve = new FloatCurve();
}
public override void OnStart(StartState state)
{
base.OnStart(state);
if (state != StartState.Editor && Afterburning)
{
Fields[nameof(actualThrottle)].guiActive = false;
Fields[nameof(actualCoreThrottle)].guiActive = true;
Fields[nameof(actualABThrottle)].guiActive = true;
}
}
public override void CreateEngine()
{
// Add centrifugal curves if needed
if (centrifugalFlowMachEtaCurve.Curve.length == 0)
{
centrifugalFlowMachEtaCurve.Add(0.85f, 1.0f, 0f, 0f);
centrifugalFlowMachEtaCurve.Add(1.0f, 0.85f, 0f, 0f);
centrifugalFlowMachEtaCurve.Add(1.15f, 0.9f, 0f, 0f);
centrifugalFlowMachEtaCurve.Add(2.0f, 0.8f, 0f, 0f);
}
if (centrifugalFlowMachTPRCurve.Curve.length == 0)
{
centrifugalFlowMachTPRCurve.Add(0.85f, 1.0f, 0f, 0f);
centrifugalFlowMachTPRCurve.Add(1.0f, 0.6f, 0f, 0f);
centrifugalFlowMachTPRCurve.Add(1.15f, 0.8f, 0f, 0f);
centrifugalFlowMachTPRCurve.Add(2.0f, 0.5f, 0f, 0f);
}
// bool DREactive = AssemblyLoader.loadedAssemblies.Any(
// a => a.assembly.GetName().Name.Equals("DeadlyReentry.dll", StringComparison.InvariantCultureIgnoreCase));
// heatProduction = (float)part.maxTemp * 0.1f;
engineSolver = solverJet = new SolverJet();
solverJet.InitializeOverallEngineData(
Area,
BPR,
CPR,
FPR,
Mdes,
Tdes,
eta_c,
eta_t,
eta_n,
FHV,
TIT,
TAB,
minThrottle,
turbineAreaRatio,
exhaustMixer,
adjustableNozzle,
unifiedThrottle,
isCentrifugalFlow,
centrifugalFlowMachEtaCurve,
centrifugalFlowMachTPRCurve,
tt7_max
);
useAtmCurve = atmChangeFlow = useVelCurve = useAtmCurveIsp = useVelCurveIsp = false;
maxEngineTemp = maxT3;
if (autoignitionTemp < 0f || float.IsInfinity(autoignitionTemp))
autoignitionTemp = 500f; // Autoignition of Kerosene is 493.15K
PushAreaToInlet();
// set heat production
heatProduction = Mathf.Min(10f, (1f - eta_c) * (1f - eta_t) * (1f - eta_n) * (10000f + TAB * 10f) / (1f + BPR * 0.5f));
}
public override void CreateEngineIfNecessary()
{
if (engineSolver == null || !(engineSolver is SolverJet))
CreateEngine();
}
public override void Shutdown()
{
base.Shutdown();
currentThrottle = 0.01f;
base.UpdateThrottle();
}
public override void UpdateThrottle()
{
double requiredThrottle = requestedThrottle * thrustPercentage * 0.01d;
double deltaT = TimeWarp.fixedDeltaTime;
double throttleResponseRate = Math.Max(1 / (1.28 * Area * (1 + BPR) + 3.22), 0.1) * throttleResponseMultiplier;
// De-multiplex and then re-multiplex main and afterburner throttles
float currentMainThrottle, currentABThrottle;
double requiredMainThrottle, requiredABThrottle;
if (Afterburning)
{
currentMainThrottle = Mathf.Min(currentThrottle * 1.5f, 1f);
currentABThrottle = Mathf.Max(currentThrottle * 3f - 2f, 0f);
requiredMainThrottle = Math.Min(requiredThrottle * 1.5d, 1d);
requiredABThrottle = Math.Max(requiredThrottle * 3d - 2d, 0d);
}
else
{
currentMainThrottle = currentThrottle;
currentABThrottle = unifiedThrottle ? currentThrottle : 0f;
requiredMainThrottle = requiredThrottle;
requiredABThrottle = 0d;
}
double d = requiredMainThrottle - currentMainThrottle;
double throttleChange = Math.Min(Math.Abs(d), deltaT * throttleResponseRate) * Math.Sign(d);
currentMainThrottle += (float)throttleChange;
if (Afterburning && currentMainThrottle >= 1f && requiredABThrottle > 0d)
{
if (requiredABThrottle > currentABThrottle)
{
double deltaTRemaining = Math.Max(0d, deltaT - (Math.Abs(d) / throttleResponseRate));
double throttleResponseRateAB = throttleResponseRate * 10;
double d2 = requiredABThrottle - currentABThrottle;
double throttleChangeAB = Math.Min(Math.Abs(d2), deltaTRemaining * throttleResponseRateAB) * Math.Sign(d2);
currentABThrottle += (float)throttleChangeAB;
}
else
{
currentABThrottle = (float)requiredABThrottle;
}
}
else if (unifiedThrottle)
{
currentABThrottle = currentMainThrottle;
}
else
{
currentABThrottle = 0f;
}
if (Afterburning)
{
currentThrottle = (currentMainThrottle * 2f / 3f) + (currentABThrottle / 3f);
actualCoreThrottle = currentMainThrottle * 100f;
actualABThrottle = currentABThrottle * 100f;
}
else
{
currentThrottle = currentMainThrottle;
}
base.UpdateThrottle();
}
public override void CalculateEngineParams()
{
base.CalculateEngineParams();
prat3 = (float)solverJet.GetPrat3();
#if DEBUG
nozzleArea = GetNozzleArea();
exhaustTemp = GetEmissiveTemp();
temp3 = solverJet.GetTemp3();
tpr1 = solverJet.GetTPR1();
etaCMach = solverJet.EtaCMach();
tprMach = solverJet.TPRMach();
#endif
}
public override float RequiredIntakeArea()
{
return base.RequiredIntakeArea() * areaFudgeFactor;
}
public override void FXUpdate()
{
base.FXUpdate();
part.Effect(spoolEffectName2, engineSolver.GetFXSpool());
part.Effect(powerEffectName2, engineSolver.GetFXPower());
}
public override void DeactivatePowerFX()
{
base.DeactivatePowerFX();
part.Effect(powerEffectName2, 0f);
}
public override void DeactivateLoopingFX()
{
base.DeactivateLoopingFX();
part.Effect(spoolEffectName2, 0f);
}
public float GetEmissiveTemp()
{
if (isOperational)
return (float)solverJet.GetExhaustTemp();
else
return (float)part.temperature;
}
public float GetNozzleArea()
{
if (isOperational)
return (float)solverJet.GetNozzleArea();
else
return 0f;
}
public float GetCoreThrottle()
{
if (isOperational)
return (float)solverJet.GetCoreThrottle();
else
return 0f;
}
public float GetABThrottle()
{
if (isOperational)
return (float)solverJet.GetABThrottle();
else
return 0f;
}
public float GetStaticDryNozzleArea()
{
SetStaticSimulation();
currentThrottle = FullDryThrottle;
UpdateSolver(ambientTherm, 0d, Vector3d.zero, 0d, true, true, false);
return (float)solverJet.GetNozzleArea();
}
public float GetStaticWetNozzleArea()
{
SetStaticSimulation();
currentThrottle = 1f;
UpdateSolver(ambientTherm, 0d, Vector3d.zero, 0d, true, true, false);
return (float)solverJet.GetNozzleArea();
}
public bool Afterburning => (TAB > 0f) && !unifiedThrottle;
#region Engine Fitting
public bool CanFitEngine => true;
public void PushFitParamsToSolver()
{
solverJet.SetFitParams(Area, FHV, TAB, minThrottle, turbineAreaRatio);
PushAreaToInlet();
}
public void DoEngineFit()
{
SolverJet jetEngine = engineSolver as SolverJet;
jetEngine.FitEngine(dryThrust * 1000d, drySFC, wetThrust * 1000d, idleNPR, defaultTPR : defaultTPR);
Area = (float)jetEngine.GetAref();
FHV = (float)jetEngine.GetFHV();
TAB = (float)jetEngine.GetTAB();
minThrottle = (float)jetEngine.GetMinThrottle();
turbineAreaRatio = (float)jetEngine.GetTurbineAreaRatio();
PushAreaToInlet();
}
protected void PushAreaToInlet()
{
if (intakeMatchArea)
{
AJEInlet intake = part.FindModuleImplementing<AJEInlet>();
if (intake != null)
intake.Area = RequiredIntakeArea();
}
}
#endregion
#region Info
public string GetStaticThrustInfo(bool primaryField)
{
string output = "";
SetStaticSimulation();
currentThrottle = 1f;
UpdateSolver(ambientTherm, 0d, Vector3d.zero, 0d, true, true, false);
double thrust = (engineSolver.GetThrust() * 0.001d);
if (Afterburning)
{
output += "<b>Static Thrust (wet): </b>" + thrust.ToString("N2") + " kN";
if (!primaryField)
output += "\n <b>SFC: </b>" + engineSolver.GetSFC().ToString("N4") + " kg/kgf-h";
currentThrottle = 2f / 3f;
UpdateSolver(ambientTherm, 0d, Vector3d.zero, 0d, true, true, false);
thrust = (engineSolver.GetThrust() * 0.001d);
output += "\n<b>Static Thrust (dry): </b>" + thrust.ToString("N2") + " kN";
if (!primaryField)
output += "\n <b>SFC: </b>" + engineSolver.GetSFC().ToString("N4") + " kg/kgf-h\n";
}
else
{
output += "<b>Static Thrust: </b>" + thrust.ToString("N2") + " kN";
if (!primaryField)
output += "\n <b>SFC: </b>" + engineSolver.GetSFC().ToString("N4") + " kg/kgf-h\n";
}
if (!primaryField)
{
output += "\n<b>Required Area:</b> " + RequiredIntakeArea().ToString("F3") + " m^2";
if (BPR > 0f)
output += "\n<b>Bypass Ratio:</b> " + BPR.ToString("F2");
output += "\n<b>Compression Ratio (static):</b> " + solverJet.GetPrat3().ToString("F1") + "\n";
}
return output;
}
public override string GetModuleTitle() => (BPR > 0) ? "AJE Turbofan" : "AJE Turbojet";
public override string GetPrimaryField() => GetStaticThrustInfo(true);
public override string GetInfo()
{
string output = GetStaticThrustInfo(false);
output += "\n<b><color=#99ff00ff>Propellants:</color></b>\n";
Propellant p;
string pName;
for (int i = 0; i < propellants.Count; ++i)
{
p = propellants[i];
pName = KSPUtil.PrintModuleName(p.name);
output += "- <b>" + pName + "</b>: " + getMaxFuelFlow(p).ToString("0.0##") + "/sec. Max.\n";
output += p.GetFlowModeDescription();
}
output += "<b>Flameout under: </b>" + (ignitionThreshold * 100f).ToString("0.#") + "%\n";
if (!allowShutdown) output += "\n" + "<b><color=orange>Engine cannot be shut down!</color></b>";
if (!allowRestart) output += "\n" + "<b><color=orange>If shutdown, engine cannot restart.</color></b>";
currentThrottle = 0f;
return output;
}
#endregion
private void SetStaticSimulation()
{
ambientTherm = EngineThermodynamics.StandardConditions(true);
inletTherm = ambientTherm;
inletTherm.P *= AJEInlet.OverallStaticTPR(defaultTPR);
areaRatio = 1d;
lastPropellantFraction = 1d;
}
private float FullDryThrottle => Afterburning ? (2f / 3f) : 1f;
}
}