forked from bobsayshilol/engine-sim
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgas_system.cpp
More file actions
597 lines (480 loc) · 18.9 KB
/
gas_system.cpp
File metadata and controls
597 lines (480 loc) · 18.9 KB
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#include "../include/gas_system.h"
#include "../include/units.h"
#include "../include/utilities.h"
#include <cmath>
#include <cassert>
void GasSystem::setGeometry(double width, double height, double dx, double dy) {
m_width = width;
m_height = height;
m_dx = dx;
m_dy = dy;
}
void GasSystem::initialize(double P, double V, double T, const Mix &mix, int degreesOfFreedom) {
m_degreesOfFreedom = degreesOfFreedom;
m_state.n_mol = P * V / (constants::R * T);
m_state.V = V;
m_state.E_k = T * (0.5 * degreesOfFreedom * m_state.n_mol * constants::R);
m_state.mix = mix;
m_state.momentum[0] = m_state.momentum[1] = 0;
const double hcr = heatCapacityRatio();
m_chokedFlowLimit = chokedFlowLimit(degreesOfFreedom);
m_chokedFlowFactorCached = chokedFlowRate(degreesOfFreedom);
}
void GasSystem::reset(double P, double T, const Mix &mix) {
m_state.n_mol = P * volume() / (constants::R * T);
m_state.E_k = T * (0.5 * m_degreesOfFreedom * m_state.n_mol * constants::R);
m_state.mix = mix;
m_state.momentum[0] = m_state.momentum[1] = 0;
}
void GasSystem::setVolume(double V) {
return changeVolume(V - m_state.V);
}
void GasSystem::setN(double n) {
m_state.E_k = kineticEnergy(n);
m_state.n_mol = n;
}
void GasSystem::changeVolume(double dV) {
const double V = this->volume();
const double L = std::pow(V + dV, 1 / 3.0);
const double surfaceArea = (L * L);
const double dL = -dV / surfaceArea;
const double W = dL * pressure() * surfaceArea;
m_state.V += dV;
m_state.E_k += W;
}
void GasSystem::changePressure(double dP) {
m_state.E_k += dP * volume() * m_degreesOfFreedom * 0.5;
}
void GasSystem::changeTemperature(double dT) {
m_state.E_k += dT * 0.5 * m_degreesOfFreedom * n() * constants::R;
}
void GasSystem::changeEnergy(double dE) {
m_state.E_k += dE;
}
void GasSystem::changeMix(const Mix &mix) {
m_state.mix = mix;
}
void GasSystem::injectFuel(double n) {
const double n_fuel = this->n_fuel() + n;
const double p_fuel = n_fuel / this->n();
m_state.mix.p_fuel = p_fuel;
}
void GasSystem::changeTemperature(double dT, double n) {
m_state.E_k += dT * 0.5 * m_degreesOfFreedom * n * constants::R;
}
double GasSystem::react(double n, const Mix &mix) {
const double l_n_fuel = mix.p_fuel * n;
const double l_n_o2 = mix.p_o2 * n;
const double system_n_fuel = n_fuel();
const double system_n_o2 = n_o2();
const double system_n_inert = n_inert();
const double system_n = this->n();
// Assuming the following reaction:
// 25[O2] + 2[C8H16] -> 16[CO2] + 18[H2O]
constexpr double ideal_o2_ratio = 25.0 / 2;
constexpr double ideal_fuel_ratio = 2.0 / 25;
constexpr double output_input_ratio = (16.0 + 18.0) / (25 + 2);
const double ideal_fuel_n = ideal_fuel_ratio * l_n_o2;
const double ideal_o2_n = ideal_o2_ratio * l_n_fuel;
const double a_n_fuel = std::fmin(
std::fmin(system_n_fuel, l_n_fuel),
ideal_fuel_n);
const double a_n_o2 = std::fmin(
std::fmin(system_n_o2, l_n_o2),
ideal_o2_n);
const double reactants_n = a_n_fuel + a_n_o2;
const double products_n = output_input_ratio * reactants_n;
const double dn = products_n - reactants_n;
m_state.n_mol += dn;
// Adjust mix
const double new_system_n_fuel = system_n_fuel - a_n_fuel;
const double new_system_n_o2 = system_n_o2 - a_n_o2;
const double new_system_n_inert = system_n_inert + products_n;
const double new_system_n = system_n + dn;
if (new_system_n != 0) {
m_state.mix.p_fuel = new_system_n_fuel / new_system_n;
m_state.mix.p_inert = new_system_n_inert / new_system_n;
m_state.mix.p_o2 = new_system_n_o2 / new_system_n;
}
else {
m_state.mix.p_fuel = m_state.mix.p_inert = m_state.mix.p_o2 = 0;
}
return a_n_fuel;
}
double GasSystem::flowConstant(
double targetFlowRate,
double P,
double pressureDrop,
double T,
double hcr)
{
const double T_0 = T;
const double p_0 = P, p_T = P - pressureDrop; // p_0 = upstream pressure
const double chokedFlowLimit =
std::pow((2.0 / (hcr + 1)), hcr / (hcr - 1));
const double p_ratio = p_T / p_0;
double flowRate = 0;
if (p_ratio <= chokedFlowLimit) {
// Choked flow
flowRate = std::sqrt(hcr);
flowRate *= std::pow(2 / (hcr + 1), (hcr + 1) / (2 * (hcr - 1)));
}
else {
flowRate = (2 * hcr) / (hcr - 1);
flowRate *= (1 - std::pow(p_ratio, (hcr - 1) / hcr));
flowRate = std::sqrt(flowRate);
flowRate *= std::pow(p_ratio, 1 / hcr);
}
flowRate *= p_0 / std::sqrt(constants::R * T_0);
return targetFlowRate / flowRate;
}
double GasSystem::k_28inH2O(double flowRateScfm) {
return flowConstant(
units::flow(flowRateScfm, units::scfm),
units::pressure(1.0, units::atm),
units::pressure(28.0, units::inH2O),
units::celcius(25),
heatCapacityRatio(5)
);
}
double GasSystem::k_carb(double flowRateScfm) {
return flowConstant(
units::flow(flowRateScfm, units::scfm),
units::pressure(1.0, units::atm),
units::pressure(1.5, units::inHg),
units::celcius(25),
heatCapacityRatio(5)
);
}
double GasSystem::flowRate(
double k_flow,
double P0,
double P1,
double T0,
double T1,
double hcr,
double chokedFlowLimit,
double chokedFlowRateCached)
{
if (k_flow == 0) return 0;
double direction;
double T_0;
double p_0, p_T; // p_0 = upstream pressure
if (P0 > P1) {
direction = 1.0;
T_0 = T0;
p_0 = P0;
p_T = P1;
}
else {
direction = -1.0;
T_0 = T1;
p_0 = P1;
p_T = P0;
}
const double p_ratio = p_T / p_0;
double flowRate = 0;
if (p_ratio <= chokedFlowLimit) {
// Choked flow
flowRate = chokedFlowRateCached;
flowRate /= std::sqrt(constants::R * T_0);
}
else {
const double s = std::pow(p_ratio, 1 / hcr);
flowRate = (2 * hcr) / (hcr - 1);
flowRate *= s * (s - p_ratio);
flowRate = std::sqrt(std::fmax(flowRate, 0.0) / (constants::R * T_0));
}
flowRate *= direction * p_0;
return flowRate * k_flow;
}
double GasSystem::loseN(double dn, double E_k_per_mol) {
m_state.E_k -= E_k_per_mol * dn;
m_state.n_mol -= dn;
if (m_state.n_mol < 0) {
m_state.n_mol = 0;
}
return dn;
}
double GasSystem::gainN(double dn, double E_k_per_mol, const Mix &mix) {
const double next_n = m_state.n_mol + dn;
const double current_n = m_state.n_mol;
m_state.E_k += dn * E_k_per_mol;
m_state.n_mol = next_n;
if (next_n != 0) {
m_state.mix.p_fuel = (m_state.mix.p_fuel * current_n + dn * mix.p_fuel) / next_n;
m_state.mix.p_inert = (m_state.mix.p_inert * current_n + dn * mix.p_inert) / next_n;
m_state.mix.p_o2 = (m_state.mix.p_o2 * current_n + dn * mix.p_o2) / next_n;
}
else {
m_state.mix.p_fuel = m_state.mix.p_inert = m_state.mix.p_o2 = 0;
}
return -dn;
}
void GasSystem::dissipateExcessVelocity() {
const double v_x = velocity_x();
const double v_y = velocity_y();
const double v_squared = v_x * v_x + v_y * v_y;
const double c = this->c();
const double c_squared = c * c;
if (c_squared >= v_squared || v_squared == 0) {
return;
}
const double k_squared = c_squared / v_squared;
const double k = std::sqrt(k_squared);
m_state.momentum[0] *= k;
m_state.momentum[1] *= k;
m_state.E_k += 0.5 * mass() * (v_squared - c_squared);
if (m_state.E_k < 0) m_state.E_k = 0;
if (std::isnan(m_state.momentum[0]) || std::isnan(m_state.E_k)) {
int a = 0;
}
}
void GasSystem::updateVelocity(double dt, double beta) {
if (n() == 0) return;
const double depth = volume() / (m_width * m_height);
double d_momentum_x = 0;
double d_momentum_y = 0;
const double p0 = dynamicPressure(m_dx, m_dy);
const double p1 = dynamicPressure(-m_dx, -m_dy);
const double p2 = dynamicPressure(m_dy, m_dx);
const double p3 = dynamicPressure(-m_dy, -m_dx);
const double p_sa_0 = p0 * (m_height * depth);
const double p_sa_1 = p1 * (m_height * depth);
const double p_sa_2 = p2 * (m_width * depth);
const double p_sa_3 = p3 * (m_width * depth);
d_momentum_x += p_sa_0 * m_dx;
d_momentum_y += p_sa_0 * m_dy;
d_momentum_x -= p_sa_1 * m_dx;
d_momentum_y -= p_sa_1 * m_dy;
d_momentum_x += p_sa_2 * m_dy;
d_momentum_y += p_sa_2 * m_dx;
d_momentum_x -= p_sa_3 * m_dy;
d_momentum_y -= p_sa_3 * m_dx;
const double m = mass();
const double inv_m = 1 / m;
const double v0_x = m_state.momentum[0] * inv_m;
const double v0_y = m_state.momentum[1] * inv_m;
m_state.momentum[0] -= d_momentum_x * dt * beta;
m_state.momentum[1] -= d_momentum_y * dt * beta;
const double v1_x = m_state.momentum[0] * inv_m;
const double v1_y = m_state.momentum[1] * inv_m;
m_state.E_k -= 0.5 * m * (v1_x * v1_x - v0_x * v0_x);
m_state.E_k -= 0.5 * m * (v1_y * v1_y - v0_y * v0_y);
if (m_state.E_k < 0) m_state.E_k = 0;
}
void GasSystem::dissipateVelocity(double dt, double timeConstant) {
if (n() == 0) return;
const double invMass = 1.0 / mass();
const double velocity_x = m_state.momentum[0] * invMass;
const double velocity_y = m_state.momentum[1] * invMass;
const double velocity_squared =
velocity_x * velocity_x + velocity_y * velocity_y;
const double s = dt / (dt + timeConstant);
m_state.momentum[0] = m_state.momentum[0] * (1 - s);
m_state.momentum[1] = m_state.momentum[1] * (1 - s);
const double newVelocity_x = m_state.momentum[0] * invMass;
const double newVelocity_y = m_state.momentum[1] * invMass;
const double newVelocity_squared =
newVelocity_x * newVelocity_x + newVelocity_y * newVelocity_y;
const double dE_k = 0.5 * mass() * (velocity_squared - newVelocity_squared);
m_state.E_k += dE_k;
}
double GasSystem::flow(const FlowParameters ¶ms) {
GasSystem *source = nullptr, *sink = nullptr;
double sourcePressure = 0, sinkPressure = 0;
double dx, dy;
double sourceCrossSection = 0, sinkCrossSection = 0;
double direction = 0;
const double P_0 =
params.system_0->pressure()
+ params.system_0->dynamicPressure(params.direction_x, params.direction_y);
const double P_1 =
params.system_1->pressure()
+ params.system_1->dynamicPressure(-params.direction_x, -params.direction_y);
if (P_0 > P_1) {
dx = params.direction_x;
dy = params.direction_y;
source = params.system_0;
sink = params.system_1;
sourcePressure = P_0;
sinkPressure = P_1;
sourceCrossSection = params.crossSectionArea_0;
sinkCrossSection = params.crossSectionArea_1;
direction = 1.0;
}
else {
dx = -params.direction_x;
dy = -params.direction_y;
source = params.system_1;
sink = params.system_0;
sourcePressure = P_1;
sinkPressure = P_0;
sourceCrossSection = params.crossSectionArea_1;
sinkCrossSection = params.crossSectionArea_0;
direction = -1.0;
}
double flow = params.dt * flowRate(
params.k_flow,
sourcePressure,
sinkPressure,
source->temperature(),
sink->temperature(),
source->heatCapacityRatio(),
source->m_chokedFlowLimit,
source->m_chokedFlowFactorCached);
const double maxFlow = source->pressureEquilibriumMaxFlow(sink);
flow = clamp(flow, 0.0, 0.9 * source->n());
//flow = clamp(flow, 0.0, maxFlow);
const double fraction = flow / source->n();
const double fractionVolume = fraction * source->volume();
const double fractionMass = fraction * source->mass();
const double remainingMass = (1 - fraction) * source->mass();
if (flow != 0) {
// - Stage 1
// Fraction flows from source to sink.
const double E_k_bulk_src0 = source->bulkKineticEnergy();
const double E_k_bulk_sink0 = sink->bulkKineticEnergy();
const double s0 = source->totalEnergy() + sink->totalEnergy();
const double E_k_per_mol = source->kineticEnergyPerMol();
sink->gainN(flow, E_k_per_mol, source->mix());
source->loseN(flow, E_k_per_mol);
const double s1 = source->totalEnergy() + sink->totalEnergy();
const double dp_x = source->m_state.momentum[0] * fraction;
const double dp_y = source->m_state.momentum[1] * fraction;
source->m_state.momentum[0] -= dp_x;
source->m_state.momentum[1] -= dp_y;
sink->m_state.momentum[0] += dp_x;
sink->m_state.momentum[1] += dp_y;
const double E_k_bulk_src1 = source->bulkKineticEnergy();
const double E_k_bulk_sink1 = sink->bulkKineticEnergy();
sink->m_state.E_k -= ((E_k_bulk_src1 + E_k_bulk_sink1) - (E_k_bulk_src0 + E_k_bulk_sink0));
}
const double sourceMass = source->mass();
const double invSourceMass = 1 / sourceMass;
const double sinkMass = sink->mass();
const double invSinkMass = 1 / sinkMass;
const double c_source = source->c();
const double c_sink = sink->c();
const double sourceInitialMomentum_x = source->m_state.momentum[0];
const double sourceInitialMomentum_y = source->m_state.momentum[1];
const double sinkInitialMomentum_x = sink->m_state.momentum[0];
const double sinkInitialMomentum_y = sink->m_state.momentum[1];
// Momentum in fraction
if (sinkCrossSection != 0) {
const double sinkFractionVelocity =
clamp((fractionVolume / sinkCrossSection) / params.dt, 0.0, c_sink);
const double sinkFractionVelocity_squared = sinkFractionVelocity * sinkFractionVelocity;
const double sinkFractionVelocity_x = sinkFractionVelocity * dx;
const double sinkFractionVelocity_y = sinkFractionVelocity * dy;
const double sinkFractionMomentum_x = sinkFractionVelocity_x * fractionMass;
const double sinkFractionMomentum_y = sinkFractionVelocity_y * fractionMass;
sink->m_state.momentum[0] += sinkFractionMomentum_x;
sink->m_state.momentum[1] += sinkFractionMomentum_y;
}
if (sourceCrossSection != 0 && sourceMass != 0) {
const double sourceFractionVelocity =
clamp((fractionVolume / sourceCrossSection) / params.dt, 0.0, c_source);
const double sourceFractionVelocity_squared = sourceFractionVelocity * sourceFractionVelocity;
const double sourceFractionVelocity_x = sourceFractionVelocity * dx;
const double sourceFractionVelocity_y = sourceFractionVelocity * dy;
const double sourceFractionMomentum_x = sourceFractionVelocity_x * fractionMass;
const double sourceFractionMomentum_y = sourceFractionVelocity_y * fractionMass;
source->m_state.momentum[0] += sourceFractionMomentum_x;
source->m_state.momentum[1] += sourceFractionMomentum_y;
}
if (sourceMass != 0) {
// Energy conservation
const double sourceVelocity0_x = sourceInitialMomentum_x * invSourceMass;
const double sourceVelocity0_y = sourceInitialMomentum_y * invSourceMass;
const double sourceVelocity1_x = source->m_state.momentum[0] * invSourceMass;
const double sourceVelocity1_y = source->m_state.momentum[1] * invSourceMass;
source->m_state.E_k -=
0.5 * sourceMass
* (sourceVelocity1_x * sourceVelocity1_x - sourceVelocity0_x * sourceVelocity0_x);
source->m_state.E_k -=
0.5 * sourceMass
* (sourceVelocity1_y * sourceVelocity1_y - sourceVelocity0_y * sourceVelocity0_y);
}
if (sinkMass > 0) {
const double sinkVelocity0_x = sinkInitialMomentum_x * invSinkMass;
const double sinkVelocity0_y = sinkInitialMomentum_y * invSinkMass;
const double sinkVelocity1_x = sink->m_state.momentum[0] * invSinkMass;
const double sinkVelocity1_y = sink->m_state.momentum[1] * invSinkMass;
sink->m_state.E_k -=
0.5 * sinkMass
* (sinkVelocity1_x * sinkVelocity1_x - sinkVelocity0_x * sinkVelocity0_x);
sink->m_state.E_k -=
0.5 * sinkMass
* (sinkVelocity1_y * sinkVelocity1_y - sinkVelocity0_y * sinkVelocity0_y);
}
if (sink->m_state.E_k < 0) {
sink->m_state.E_k = 0;
}
if (source->m_state.E_k < 0) {
source->m_state.E_k = 0;
}
return flow * direction;
}
double GasSystem::flow(double k_flow, double dt, double P_env, double T_env, const Mix &mix) {
const double maxFlow = pressureEquilibriumMaxFlow(P_env, T_env);
double flow = dt * flowRate(
k_flow,
pressure(),
P_env,
temperature(),
T_env,
heatCapacityRatio(),
m_chokedFlowLimit,
m_chokedFlowFactorCached);
if (std::abs(flow) > std::abs(maxFlow)) {
flow = maxFlow;
}
if (flow < 0) {
const double bulk_E_k_0 = bulkKineticEnergy();
gainN(-flow, kineticEnergyPerMol(T_env, m_degreesOfFreedom), mix);
const double bulk_E_k_1 = bulkKineticEnergy();
m_state.E_k += (bulk_E_k_1 - bulk_E_k_0);
}
else {
const double starting_n = n();
loseN(flow, kineticEnergyPerMol());
m_state.momentum[0] -= (flow / starting_n) * m_state.momentum[0];
m_state.momentum[1] -= (flow / starting_n) * m_state.momentum[1];
}
return flow;
}
double GasSystem::pressureEquilibriumMaxFlow(const GasSystem *b) const {
// pressure_a = (kineticEnergy() + n * b->kineticEnergyPerMol()) / (0.5 * degreesOfFreedom * volume())
// pressure_b = (b->kineticEnergy() - n * / (0.5 * b->degreesOfFreedom * b->volume())
// pressure_a = pressure_b
// E_a = kineticEnergy()
// E_b = b->kineticEnergy()
// D_a = E_a / n()
// D_b = E_b / b->n()
// Q_a = 1 / (0.5 * degreesOfFreedom * volume())
// Q_b = 1 / (0.5 * b->degreesOfFreedom * b->volume())
// pressure_a = Q_a * (E_a + dn * D_b)
// pressure_b = Q_b * (E_b - dn * D_b)
if (pressure() > b->pressure()) {
const double maxFlow =
(b->volume() * kineticEnergy() - volume() * b->kineticEnergy()) /
(b->volume() * kineticEnergyPerMol() + volume() * kineticEnergyPerMol());
return std::fmax(0.0, std::fmin(maxFlow, n()));
}
else {
const double maxFlow =
(b->volume() * kineticEnergy() - volume() * b->kineticEnergy()) /
(b->volume() * b->kineticEnergyPerMol() + volume() * b->kineticEnergyPerMol());
return std::fmin(0.0, std::fmax(maxFlow, -b->n()));
}
}
double GasSystem::pressureEquilibriumMaxFlow(double P_env, double T_env) const {
if (pressure() > P_env) {
return -(P_env * (0.5 * m_degreesOfFreedom * volume()) - kineticEnergy()) / kineticEnergyPerMol();
}
else {
const double E_k_per_mol_env = 0.5 * T_env * constants::R * m_degreesOfFreedom;
return -(P_env * (0.5 * m_degreesOfFreedom * volume()) - kineticEnergy()) / E_k_per_mol_env;
}
}