-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolver.cpp
More file actions
1654 lines (1473 loc) · 58.4 KB
/
Copy pathSolver.cpp
File metadata and controls
1654 lines (1473 loc) · 58.4 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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of the 1D-HSI solver hosted at github.com/IhmeGroup/sprayHSI
// D. Mohaddes
// September 2021
//
// Created by Danyal Mohaddes on 2020-02-28.
//
#include <math.h>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include "Solver.h"
#include "toml.hpp"
#include "boost/algorithm/string.hpp"
#include "cvode/cvode.h"
#include "cvode/cvode_dense.h"
#include "sundials/sundials_types.h"
#include "RHSFunctor.h"
#include "Meshing.h"
#include "omp.h"
#include "Liquid/FitLiquid.h"
#define NEAR_ONE 0.9999999999
Solver::Solver() {
std::cout << "Solver::Solver()" << std::endl;
}
void Solver::ReadParams(int argc, char* argv[]){
std::cout << "Solver::ReadParams()" << std::endl;
// get input file from command line
input_file = "";
for (int i = 0; i < argc; i++){
if (std::strncmp(argv[i],"-i",2) == 0){
input_file = argv[i+1];
std::cout << "Input file: " << input_file << std::endl;
}
}
if (input_file.empty()){
std::cerr << "No input file provided." << std::endl;
throw(0);
}
// parse input file
const toml::value data = toml::parse(input_file);
// set this-> private members
// Run mode
{
const auto RunMode_ = toml::find(data, "Run_mode");
run_mode = toml::find(RunMode_, "mode").as_string();
if (run_mode == "ignition"){
ign_cond = toml::find(RunMode_,"ignition_condition").as_string();
if (ign_cond == "T_max"){
T_max = toml::find(RunMode_,"T_max").as_floating();
}
}
}
// IO
{
const auto IO_ = toml::find(data, "IO");
verbose = toml::find(IO_, "verbose").as_boolean();
output_interval = toml::find(IO_, "output_interval").as_integer();
output_path = toml::find(IO_, "output_path").as_string();
output_species = toml::get<std::vector<std::string>>(toml::find(IO_, "output_species"));
}
// Physics
{
const auto Physics_ = toml::find(data, "Physics");
p_sys = toml::find(Physics_, "p").as_floating();
// Gas
{
const auto Gas_ = toml::find(Physics_, "Gas");
mech_file = toml::find(Gas_, "mech_file").as_string();
phase_name = toml::find(Gas_, "phase_name").as_string();
mech_qss = toml::find(Gas_, "qss").as_boolean();
reacting = toml::find(Gas_, "reacting").as_boolean();
X_ox = toml::find(Gas_, "oxidizer").as_string();
X_f = toml::find(Gas_, "fuel").as_string();
}
// Spray
{
const auto Spray_ = toml::find(Physics_, "Spray");
spray = toml::find(Spray_, "spray").as_boolean(); // TODO change to simply "spray" true/false. There is no use case for spray with no evap.
if (spray) {
X_liq = toml::find(Spray_, "species").as_string();
liq_type = toml::find(Spray_, "properties").as_string();
spray_gas_slip = toml::find(Spray_,"spray_gas_slip").as_boolean();
} else {
X_liq = "";
liq_type = "fit";
spray_gas_slip = false;
}
}
// Solid
{
const auto Solid_ = toml::find(Physics_, "Solid");
conjugate = toml::find(Solid_, "conjugate").as_boolean();
if (conjugate){
lam_s = toml::find(Solid_, "thermal_conductivity").as_floating();
rho_s = toml::find(Solid_, "density").as_floating();
c_s = toml::find(Solid_, "heat_capacity").as_floating();
}
}
}
// Mesh
{
const auto Mesh_ = toml::find(data, "Mesh");
// Space
{
const auto Space_ = toml::find(Mesh_, "Space");
// Fluid
{
const auto Fluid_ = toml::find(Space_, "Fluid");
N = toml::find(Fluid_, "N").as_integer();
L = toml::find(Fluid_, "L").as_floating();
spacing = toml::find(Fluid_, "spacing").as_string();
if (spacing == "geometric") {
spacing_D0 = toml::find(Fluid_, "wall_spacing").as_floating();
}
}
// Solid
{
const auto Solid_ = toml::find(Space_, "Solid");
if (conjugate) {
N_s = toml::find(Solid_, "N").as_integer();
L_s = toml::find(Solid_, "L").as_floating();
spacing_s = toml::find(Solid_, "spacing").as_string();
if (spacing_s == "geometric") {
spacing_D0_s = toml::find(Solid_, "wall_spacing").as_floating();
}
}
}
}
// Time
{
const auto Time_ = toml::find(Mesh_, "Time");
time_max = toml::find(Time_, "time_max").as_floating();
iteration_max = toml::find(Time_, "iteration_max").as_integer();
dt = toml::find(Time_, "dt").as_floating();
}
}
// Numerics
{
const auto Numerics_ = toml::find(data, "Numerics");
time_scheme = toml::find(Numerics_, "time_scheme").as_string();
if (time_scheme == "CVODE"){
cvode_abstol = toml::find(Numerics_, "cvode_abstol").as_floating();
cvode_reltol = toml::find(Numerics_, "cvode_reltol").as_floating();
cvode_maxsteps = toml::find(Numerics_, "cvode_maxsteps").as_integer();
}
n_omp_threads = toml::find(Numerics_, "openMP_threads").as_integer();
if (spray){
av_Zl = toml::find(Numerics_, "av_Zl").as_floating();
av_md = toml::find(Numerics_, "av_md").as_floating();
av_Td = toml::find(Numerics_, "av_Td").as_floating();
// numerical experiments with dodecane sprays indicate 200 is a reasonable choice
SF_spray = toml::find_or<double>(Numerics_, "SF_spray", 200.0);
nonvap_frac = toml::find_or<double>(Numerics_, "nonvap_frac", 0.1);
} else {
av_Zl = 1.0e-5;
av_md = 1.0e-5;
av_Td = 1.0e-5;
SF_spray = 0.0;
nonvap_frac = 0.1;
}
clip_T = toml::find_or<bool>(Numerics_, "clip_T", true);
if (clip_T){
T_clip_min = toml::find_or<double>(Numerics_, "T_clip_min", 250.0);
T_clip_max = toml::find_or<double>(Numerics_, "T_clip_max", 3000.0);
} else {
T_clip_min = 0.0;
T_clip_max = 10000.0;
}
}
// BCs
{
const auto BCs_ = toml::find(data, "BCs");
// Inlet
{
const auto Inlet_ = toml::find(BCs_, "Inlet");
// Gas
const auto Gas_ = toml::find(Inlet_, "Gas");
mdot = toml::find_or<double>(Gas_, "mdot", -1.0);
u_inf = toml::find_or<double>(Gas_, "u", -1.0);
if (mdot > 0.0 && u_inf > 0.0) {
std::cerr << "Inlet BC: Can only provide one of mdot or u" << std::endl;
throw (0);
}
T_in = toml::find(Gas_, "T").as_floating();
X_in = toml::find(Gas_, "X").as_string();
// Spray
const auto Spray_ = toml::find(Inlet_, "Spray");
if (spray) {
Z_l_in = toml::find(Spray_, "Z_l").as_floating();
T_d_in = toml::find(Spray_, "T_d").as_floating();
m_d_in = toml::find_or<double>(Spray_, "m_d", -1.0);
D_d_in = toml::find_or<double>(Spray_, "D_d", -1.0);
if (D_d_in > 0.0 && m_d_in > 0.0) {
std::cerr << "Inlet BC: Can only provide one of D_d or m_d" << std::endl;
throw (0);
}
} else {
Z_l_in = 0.0;
T_d_in = 300.0;
m_d_in = 1.0e-300;
D_d_in = 1.0e-300;
}
}
// Wall_Interior
{
const auto Wall_Interior_ = toml::find(BCs_, "Wall_Interior");
// Gas
{
const auto Gas_ = toml::find(Wall_Interior_, "Gas");
if (conjugate) {
match_T = toml::find(Gas_, "match_T").as_boolean();
} else {
wall_type = toml::find(Gas_, "type").as_string();
if (wall_type == "adiabatic") {
T_wall = -1.0;
} else if (wall_type == "isothermal") {
T_wall = toml::find(Gas_, "T").as_floating();
} else {
std::cerr << "Unknown Wall BC type " << wall_type << "not supported" << std::endl;
throw (0);
}
}
}
// Spray
{
const auto Spray_ = toml::find(Wall_Interior_, "Spray");
if (spray) {
filming = toml::find(Spray_, "filming").as_boolean();
} else {
filming = false;
}
}
// Solid
{
if (conjugate) {
const auto Solid_ = toml::find(Wall_Interior_, "Solid");
match_q = toml::find(Solid_, "match_q").as_boolean();
}
}
}
// Wall_Exterior
{
if (conjugate){
const auto Wall_Exterior_ = toml::find(BCs_, "Wall_Exterior");
T_s_ext = toml::find(Wall_Exterior_, "T").as_floating();
}
}
}
// ICs
{
const auto ICs_ = toml::find(data, "ICs");
// Check if restart file used
std::string tmp = toml::find_or<std::string>(ICs_,"restart","");
if (tmp.empty()) {
// No restart file
IC_type = toml::find(ICs_, "type").as_string();
if (IC_type == "linear_T") {
Tgas_0 = -1.0;
// Linear_T and adiabatic wall not permitted
if (wall_type == "adiabatic") {
std::cerr << "IC_type = " << IC_type << " and wall_type = " << wall_type << " is not a permitted combination"
<< std::endl;
throw(0);
}
} else if (IC_type == "constant_T") {
Tgas_0 = toml::find(ICs_, "T").as_floating();
// If constant_T, then all initial temperatures must be equal
// For conjugate HT, that means Tgas_0 and T_s_ext
if (conjugate && (Tgas_0 != T_s_ext)){
std::cerr << "When IC_type = " << IC_type << " and conjugate = " << conjugate << ", Tgas_0 = " << Tgas_0
<< " and T_s_ext = " << T_s_ext << " must be equal." << std::endl;
throw(0);
}
// For isothermal wall, that means Tgas_0 and T_wall
if (wall_type == "isothermal" && (T_wall != Tgas_0)){
std::cerr << "When IC_type = " << IC_type << " and wall_type = " << wall_type << ", Tgas_0 = " << Tgas_0
<< " and T_wall = " << T_wall << " must be equal." << std::endl;
throw(0);
}
// No consequences for adiabatic wall.
} else {
std::cerr << "Unknown IC type = " << IC_type << " not supported" << std::endl;
throw (0);
}
// Gas
const auto Gas_ = toml::find(ICs_, "Gas");
X_0 = toml::find(Gas_, "X").as_string();
// Spray
const auto Spray_ = toml::find(ICs_, "Spray");
if (spray){
Z_l_0 = toml::find(Spray_, "Z_l").as_floating();
m_d_0 = toml::find(Spray_, "m_d").as_floating();
T_d_0 = toml::find(Spray_, "T_d").as_floating();
} else {
Z_l_0 = 0.0;
m_d_0 = 1.0e-300;
T_d_0 = 300.0;
}
// With restart file
} else {
restart_file = tmp;
}
}
// Override input file using command line
// Consider the following variables:
// N, L, T_in, X_in, mdot, Z_l_in, m_d_in, T_wall, p_sys, X_0
for (int i = 0; i < argc; i++){
if (std::strcmp(argv[i],"-row_index") == 0){
row_index = atoi(argv[i+1]);
std::cout << " row_index is " << row_index << std::endl;
}
if (std::strcmp(argv[i],"-N") == 0){
N = atoi(argv[i+1]);
std::cout << " N overriden via command line to " << N << std::endl;
}
if (std::strcmp(argv[i],"-L") == 0){
L = atof(argv[i+1]);
std::cout << " L overriden via command line to " << L << std::endl;
}
if (std::strcmp(argv[i],"-T_in") == 0){
T_in = atof(argv[i+1]);
std::cout << " T_in overriden via command line to " << T_in << std::endl;
}
if (std::strcmp(argv[i],"-X_in") == 0){
X_in = argv[i+1];
std::cout << " X_in overriden via command line to " << X_in << std::endl;
}
if (std::strcmp(argv[i],"-mdot") == 0){
mdot = atof(argv[i+1]);
std::cout << " mdot overriden via command line to " << mdot << std::endl;
}
if (std::strcmp(argv[i],"-u_in") == 0){
u_inf = atof(argv[i+1]);
std::cout << " u_in overriden via command line to " << u_inf << std::endl;
if (mdot > 0.0 && u_inf > 0.0) {
std::cerr << "Overriding inlet BC: Can only provide one of mdot or u_in" << std::endl;
throw (0);
}
}
if (std::strcmp(argv[i],"-Z_l_in") == 0){
Z_l_in = atof(argv[i+1]);
std::cout << " Z_l_in overriden via command line to " << Z_l_in << std::endl;
}
if (std::strcmp(argv[i],"-m_d_in") == 0){
m_d_in = atof(argv[i+1]);
std::cout << " m_d_in overriden via command line to " << m_d_in << std::endl;
if (D_d_in > 0.0 && m_d_in > 0.0) {
std::cerr << "Overriding inlet BC: Can only provide one of D_d or m_d" << std::endl;
throw (0);
}
}
if (std::strcmp(argv[i],"-D_d_in") == 0){
D_d_in = atof(argv[i+1]);
std::cout << " D_d_in overriden via command line to " << D_d_in << std::endl;
if (D_d_in > 0.0 && m_d_in > 0.0) {
std::cerr << "Overriding inlet BC: Can only provide one of D_d or m_d" << std::endl;
throw (0);
}
}
if (std::strcmp(argv[i],"-T_wall") == 0){
T_wall = atof(argv[i+1]);
std::cout << " T_wall overriden via command line to " << T_wall << std::endl;
if (conjugate) {
std::cerr << "Cannot provide T_wall for conjugate simulation" << std::endl;
throw(0);
}
if (wall_type == "adiabatic") {
std::cerr << "Cannot provide T_wall for adiabatic wall simulation" << std::endl;
throw(0);
}
}
if (std::strcmp(argv[i],"-T_s_ext") == 0){
T_s_ext = atof(argv[i+1]);
std::cout << " T_s_ext overriden via command line to " << T_s_ext << std::endl;
if (!conjugate) {
std::cerr << "Cannot provide T_s_ext for non-conjugate simulation" << std::endl;
throw(0);
}
}
if (std::strcmp(argv[i],"-p_sys") == 0){
p_sys = atof(argv[i+1]);
std::cout << " p_sys overriden via command line to " << p_sys << std::endl;
}
if (std::strcmp(argv[i],"-X_0") == 0){
X_0 = argv[i+1];
std::cout << " X_0 overriden via command line to " << X_0 << std::endl;
}
}
if (run_mode == "ignition" && row_index < 0){
std::cerr << "Doing an ignition parameter study, but row_index not provided. Please set with -row_index" << std::endl;
throw(0);
}
}
void Solver::SetupSolver() {
std::cout << "Solver::SetupSolver()" << std::endl;
omp_set_num_threads(n_omp_threads);
std::cout << " Eigen::nbThreads() = " << Eigen::nbThreads() << std::endl;
Eigen::initParallel();
#pragma omp parallel
{
std::cout << " Thread #" << omp_get_thread_num() << " reporting" << std::endl;
}
if (time_scheme == "CVODE") {
// Steps from Sec. 4.4 of CVODE User Guide (V2.7.0)
double t0 = time; // initial time
// 2. Set problem dimensions
cvode_N = N * M;
// 3. Set vector of initial values
cvode_y = N_VNew_Serial(cvode_N);
Eigen::Map<Eigen::MatrixXd>(NV_DATA_S(cvode_y), N, M) = phi;
// 4. Create CVODE object
cvode_mem = NULL;
cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON);
// 5. Initialize CVODE solver
CheckCVODE("CVodeInit", CVodeInit(cvode_mem, cvode_RHS, t0, cvode_y));
// 6. Specify integration tolerances
CheckCVODE("CVodeSStolerances" ,CVodeSStolerances(cvode_mem, cvode_reltol, cvode_abstol));
// 7. Set optional inputs
p_rhs_functor = new RHSFunctor(N, M, this);
CheckCVODE("CVodeSetUserData", CVodeSetUserData(cvode_mem, p_rhs_functor));
CheckCVODE("CVodeSetMaxStep", CVodeSetMaxStep(cvode_mem, dt));
CheckCVODE("CVodeSetMaxNumSteps", CVodeSetMaxNumSteps(cvode_mem, cvode_maxsteps));
// 8. Attach linear solver module
CheckCVODE("CVDense", CVDense(cvode_mem, cvode_N));
}
}
void Solver::CheckCVODE(std::string func_name, int flag) {
if (flag != CV_SUCCESS){
std::cerr << func_name << "failed!" << std::endl;
throw(0);
}
}
void Solver::SetupGas() {
std::cout << "Solver::SetupGas()" << std::endl;
for (int i=0; i<omp_get_max_threads(); i++) {
// Gas object
gas_vec.push_back(std::unique_ptr<ThermoPhase>(newPhase(mech_file, phase_name)));
// Kinetics
if (mech_qss) {
gas_qss_vec.push_back(std::unique_ptr<ThermoPhase>(newPhase(mech_file, "QSS")));
std::vector<ThermoPhase *> phases_{gas_vec[i].get(), gas_qss_vec[i].get()};
kin_vec.push_back(std::unique_ptr<Kinetics>(new GasQSSKinetics()));
importKinetics(gas_qss_vec[i]->xml(), phases_, kin_vec[i].get());
} else {
std::vector<ThermoPhase *> phases_{gas_vec[i].get()};
kin_vec.push_back(std::unique_ptr<Kinetics>(newKineticsMgr(gas_vec[i]->xml(), phases_)));
}
// Transport properties
trans_vec.push_back(std::unique_ptr<Transport>(newDefaultTransportMgr(gas_vec[i].get())));
}
int thread = omp_get_thread_num();
ThermoPhase* gas = gas_vec[thread].get();
n_species = gas->nSpecies();
if (verbose) {
gas->setState_TPX(T_in, p_sys, X_in);
std::cout << " SetupGas() at T = " << gas->temperature() << " and p = " << gas->pressure()
<< " gives viscosity = " << trans_vec[thread]->viscosity() << " for X = " << X_in << std::endl;
}
}
void::Solver::SetupLiquid(){
if (spray) {
std::cout << "Solver::SetupLiquid()" << std::endl;
if (liq_type == "fit") {
liq = std::unique_ptr<Liquid>(new FitLiquid(X_liq));
} else {
std::cerr << "Unknown liquid type '" << liq_type << "'" << std::endl;
throw (0);
}
}
}
void Solver::SetBCs() {
// Wall
for (int k = 0; k < M; k++){
switch (k){
//V
case idx_V:
wall_interior_BC(k) = 0.0;
break;
// T
case idx_T:
if ((wall_type == "isothermal") || conjugate){
wall_interior_BC(k) = T_wall;
} else if (wall_type == "adiabatic") {
// 1st order one-sided difference
wall_interior_BC(k) = phi(0, k);
} else {
throw(0);
}
break;
// TODO update Z_l and m_d BCs for filming/rebound
// Z_l
case idx_Z_l:
// 1st order one-sided difference in case of AV
wall_interior_BC(k) = phi(0, k);
break;
// m_d
case idx_m_d:
// 1st order one-sided difference in case of AV
wall_interior_BC(k) = phi(0, k);
break;
// T_d
case idx_T_d:
// 1st order one-sided difference in case of AV
wall_interior_BC(k) = phi(0, k);
break;
// Species
default:
// TODO Species have no flux at wall for now... change when multiphase and filming
wall_interior_BC(k) = phi(0, k);
}
}
// Inlet
for (int k = 0; k < M; k++){
switch (k){
//V
case idx_V:
inlet_BC(k) = 0.0;
break;
// T
case idx_T:
inlet_BC(k) = T_in;
break;
// Z_l
case idx_Z_l:
if (spray)
inlet_BC(k) = Z_l_in;
else
inlet_BC(k) = 0.0;
break;
// m_d
case idx_m_d:
if (spray)
inlet_BC(k) = m_d_in;
else
inlet_BC(k) = 1.0e-300;
break;
// T_d
case idx_T_d:
if (spray)
inlet_BC(k) = T_d_in;
else
inlet_BC(k) = 300.0;
break;
// Species
default:
inlet_BC(k) = Y_in(k-m);
}
}
// Global strain rate
SetState(inlet_BC);
int thread = omp_get_thread_num();
rho_inf = gas_vec[thread]->density();
double u_inf_ = mdot/rho_inf;
a = u_inf_/L;
}
void Solver::DerivedParams() {
std::cout << "Solver::DerivedParams()" << std::endl;
int thread = omp_get_thread_num();
// Input file name without path
// Start with something like "/Users/.../inputFile.in"
std::vector<std::string> split_string_;
boost::split(split_string_, input_file, [](char c){return c == '/';});
// Now have ["Users","...","inputFile.in"]
std::vector<std::string> tmp_;
boost::split(tmp_, split_string_[split_string_.size() - 1], [](char c){return c == '.';});
// Now have ["inputFile","in"]
input_name = tmp_[0];
// Header for output files
// Gas/Spray
output_header = "TITLE = \"" + input_name + "\"";
output_header += "\nVARIABLES = \"X\", \"u\", \"ZBilger\", \"RHO\", \"V\", \"T\", \"Zl\", \"md\", \"Td\",";
for (int i = 0; i < gas_vec[0]->nSpecies(); i++){
output_header += " \"Y_" + gas_vec[0]->speciesName(i) + "\"";
if (i != gas_vec[0]->nSpecies() - 1) output_header += ",";
}
output_header += "\nZONE I=" + std::to_string(N+2) + ", F=POINT";
// Solid
solid_output_header = "TITLE = \"" + input_name + "\"";
solid_output_header += "\nVARIABLES = \"X_S\", \"T_S\"";
solid_output_header += "\nZONE I=" + std::to_string(N_s+2) + ", F=POINT";
ign_header = "row_index,iteration,time,x,dx_avg,u,ZBilger,rho,V,T,Zl,md,Td"; // TODO add the overridden parameters, since these are the parameter study parameters and are useful to have in the param study's (single) ignition file. Can still infer from row_index and order in which program¶ms were called in Python.
for (const auto& s : output_species){
ign_header += ",Y_" + s;
}
// Map of pointer to mass fractions array
Map<const VectorXd> md_(gas_vec[thread]->massFractions(), gas_vec[thread]->nSpecies());
// Initial mass fractions, if not using restart file
if (restart_file.empty()) {
gas_vec[thread]->setState_TPX(T_in, p_sys,
X_0); // choice of temperature shouldn't make a difference for computing mass fractions here
Y_0 = md_;
}
// Inlet mass fractions
gas_vec[thread]->setState_TPX(T_in,p_sys,X_in);
Y_in = md_;
for (int i=0; i<omp_get_max_threads(); i++) {
// Mixture diffusion coefficients (mass-based by default)
mix_diff_coeffs_vec.push_back(VectorXd::Zero(n_species));
// Molar production rates
omega_dot_mol_vec.push_back(VectorXd::Zero(n_species));
// Species molar enthalpies
species_enthalpies_mol_vec.push_back(VectorXd::Zero(n_species));
}
// Gas parameters
gas_vec[thread]->setState_TPX(T_in,p_sys,X_in);
rho_inf = gas_vec[thread]->density();
if (mdot > 0.0) u_inf = mdot/rho_inf;
else mdot = rho_inf * u_inf;
// Spray parameters
// TODO change for multicomponent spray
if (spray) {
T_l = liq->T_sat(p_sys);
L_v = liq->L_v(T_l);
fuel_idx = GetSpeciesIndex(X_liq);
double mu_ = trans_vec[thread]->viscosity(); // using inlet state from above
if (m_d_in > 0.0) D_d_in = GetDd(m_d_in, T_d_in);
else m_d_in = (M_PI/6.0) * liq->rho_liq(T_d_in, p_sys) * pow(D_d_in, 3.0);
// Don't allow error due to D_min to exceed nonvap_frac of initial volume, reduce outer dt if necessary
D_min = pow((SF_spray * dt * 18.0 * mu_ / liq->rho_liq(T_d_in, p_sys)), 0.5);
if (pow(D_min/D_d_in,3.0) > nonvap_frac){
std::cout << "> (D_min/D_inlet)^3 = " << pow(D_min/D_d_in,3.0) << " > nonvap_frac = " << nonvap_frac << ". Reducing dt to resolve evaporation to nonvap_frac." << std::endl;
D_min = D_d_in * pow(nonvap_frac, 1.0/3.0);
dt = liq->rho_liq(T_d_in, p_sys) * pow(D_min, 2.0) / (18.0 * mu_) / SF_spray;
std::cout << "> dt = " << dt << std::endl;
}
std::cout << "> D_min = " << D_min << std::endl;
std::cout << "> (D_min/D_inlet)^3 = " << pow(D_min/D_d_in,3.0) << std::endl;
} else {
T_l = L_v = D_min = 0.0;
fuel_idx = -1;
}
// Set physics
if (!spray_gas_slip){
m = 5;
} else {
std::cerr << "Spray-gas slip not supported." << std::endl;
throw(0);
}
M = m + n_species;
// Resizing arrays
phi = MatrixXd::Zero(N,M);
Phi = MatrixXd::Zero(N+2, M);
u = VectorXd::Zero(N);
rho_inv = VectorXd::Zero(N);
c = MatrixXd::Zero(N,M);
mu = MatrixXd::Zero(N,M);
mu_av = MatrixXd::Zero(N,M);
omegadot = MatrixXd::Zero(N,M);
mdot_liq = VectorXd::Zero(N);
Tdot_liq_1 = VectorXd::Zero(N);
Tdot_liq_2 = VectorXd::Zero(N);
Gammadot = MatrixXd::Zero(N,M);
T_s = VectorXd::Zero(N_s);
}
int Solver::GetSpeciesIndex(std::string cantera_string){
// TODO assumes single component fuel!!!
// Start with "A:0.7"
std::vector<std::string> split_pair;
boost::split(split_pair, cantera_string, [](char c){return c == ':';});
// Now have ["A","0.7"]
int thread = omp_get_thread_num();
return gas_vec[thread]->speciesIndex(split_pair[0]);
}
void Solver::ConstructMesh() {
std::cout << "Solver::ConstructMesh()" << std::endl;
// TODO make this polymorphic
/*
* GAS/SPRAY MESH SETUP: THERE ARE N+2 NODES, OF WHICH 2 ARE BCS
*
* WALL_INTERIOR INLET
* |---> +x
*
* | |----------|----------|----------| ... |----------| |
* 0 dx[0] 1 dx[1] 2 dx[2] N dx[N] N+1
*
*
* SOLID MESH SETUP: THERE ARE N_s+2 NODES, OF WHICH 2 ARE BCS
*
* WALL_EXTERIOR WALL_INTERIOR
* +x <---|
*
* | |----------| ... |----------|----------|----------| |
* Ns+1 dxs[Ns] Ns dxs[2] 2 dxs[1] 1 dxs[0] 0
*
*/
// Gas/Spray mesh
// resize vectors
dx = VectorXd::Zero(N+1);
nodes = VectorXd::Zero(N+2);
if (spacing == "constant"){
double dx_ = L/(N+1);
dx = dx_*VectorXd::Constant(N+1,1.0);
} else if (spacing == "geometric"){
dx(0) = spacing_D0;
auto r_ = GetGeometricRatio<double>(N+1,L,spacing_D0);
std::cout << " Ratio: " << r_ << std::endl;
for (int i=1; i<N+1; i++){
dx(i) = pow(r_, i) * spacing_D0;
}
std::cout << " Max spacing (gas/spray): " << dx(N)*1000.0 << "mm" << std::endl;
}
// loop over node vector and fill according to spacing vector
nodes(0) = 0.0;
nodes(N+1) = L;
for (int i = 1; i < N+1; i++){
nodes(i) = nodes(i-1) + dx(i-1);
}
if (verbose){
std::cout << "dx = \n" << dx << std::endl;
std::cout << "nodes = \n" << nodes << std::endl;
}
// resize BCs
wall_interior_BC = RowVectorXd::Zero(M);
inlet_BC = RowVectorXd::Zero(M);
// Solid mesh
if (conjugate){
// resize vectors
dx_s = VectorXd::Zero(N_s+1);
nodes_s = VectorXd::Zero(N_s+2);
if (spacing_s == "constant"){
double dx_ = L_s/(N_s+1);
dx_s = dx_*VectorXd::Constant(N_s+1,1.0);
} else if (spacing_s == "geometric"){
dx_s(0) = spacing_D0_s;
double r_ = GetGeometricRatio<double>(N_s+1,L_s,spacing_D0_s);
std::cout << " Ratio: " << r_ << std::endl;
for (int i=1; i<N_s+1; i++){
dx_s(i) = pow(r_, i) * spacing_D0_s;
}
std::cout << " Max spacing (solid): " << dx_s(N_s)*1000.0 << "mm" << std::endl;
}
// loop over node vector and fill according to spacing vector
nodes_s(0) = 0.0;
nodes_s(N_s+1) = L_s;
for (int i = 1; i < N_s+1; i++){
nodes_s(i) = nodes_s(i-1) + dx_s(i-1);
}
if (verbose){
std::cout << "dx_s = \n" << dx_s << std::endl;
std::cout << "nodes_s = \n" << nodes_s << std::endl;
}
// resize solution vector
T_s = VectorXd::Zero(N_s);
}
}
void Solver::ConstructOperators() {
std::cout << "Solver::ConstructOperators()" << std::endl;
// TODO make this polymorphic
// Matrices are N x N+2 such that [ddx][wall_interior_BC , phi, inlet_BC]^T = dphi/dx, N x 1.
// ddx
// 1st-order 'upwinded' (but downwinded on the grid because convection is always in -ve x direction)
// generalized to non-uniform grids
// resize matrix
ddx.resize(N,N+2);
ddx.reserve(2*N);
// fill matrix
for (int i = 0; i < N; i++){
for (int j = 0; j < N+2; j++){
if (i+1 == j){
ddx.insert(i,j) = -1.0/dx(i+1);
ddx.insert(i,j+1) = 1.0/dx(i+1);
}
}
}
// d2dx2
// 2nd-order central
// generalized to non-uniform grids (from Taylor Table)
// resize matrix
d2dx2.resize(N,N+2);
d2dx2.reserve(3*N);
// fill matrix
for (int i = 0; i < N; i++){
for (int j = 0; j < N+2; j++){
if (i+1 == j){
double a0 = 2.0/(dx(i)*(dx(i) + dx(i+1)));
double a1 = -2.0/(dx(i) * dx(i+1));
double a2 = 2.0/(dx(i+1)*(dx(i) + dx(i+1)));
d2dx2.insert(i,j-1) = a0;
d2dx2.insert(i,j) = a1;
d2dx2.insert(i,j+1) = a2;
}
}
}
if (verbose){
std::cout << "ddx = \n" << MatrixXd(ddx) << std::endl;
std::cout << "d2dx2 = \n" << MatrixXd(d2dx2) << std::endl;
}
if (conjugate){
// d2dx2
// 2nd-order central
// generalized to non-uniform grids (my derivation)
// resize matrix
d2dx2_s.resize(N_s,N_s+2);
d2dx2_s.reserve(3*N_s);
// fill matrix
for (int i = 0; i < N_s; i++){
for (int j = 0; j < N_s+2; j++){
if (i+1 == j){
d2dx2_s.insert(i,j-1) = (4.0 * dx_s(i+1))/((dx_s(i)*dx_s(i) + dx_s(i+1)*dx_s(i+1))*(dx_s(i)+dx_s(i+1)));
d2dx2_s.insert(i,j) = -4.0/(dx_s(i)*dx_s(i) + dx_s(i+1)*dx_s(i+1));
d2dx2_s.insert(i,j+1) = (4.0 * dx_s(i))/((dx_s(i)*dx_s(i) + dx_s(i+1)*dx_s(i+1))*(dx_s(i)+dx_s(i+1)));
}
}
}
if (verbose){
std::cout << "d2dx2_s = \n" << MatrixXd(d2dx2_s) << std::endl;
}
}
}
void Solver::SetIC() {
std::cout << "Solver::SetIC()" << std::endl;
if (!restart_file.empty()){
std::cout << "> Restart fluid phase from file: " << restart_file << std::endl;
// Set IC using restart file
// ASSUME no change in BCs NOR variables
// Allow a change in number of points->interpolate
// Allow a change in outer dt-> get time from auxiliary data in file
double T_w_gas; // gas temperature at wall, only used for ensuring solid/fluid file compatibility in conjugate mode
{
// Open file
std::ifstream data_stream_(restart_file);
if (!data_stream_.is_open()) {
std::cerr << "Unable to open file: " << restart_file << std::endl;
throw (0);
}
std::string line;
// discard first line
std::getline(data_stream_, line);
// count number of variables m_ from VARIABLES = ...
int m_;
{
std::getline(data_stream_, line);
// Start with something like "A:0.7 B:0.3"
std::vector<std::string> vars;
boost::split(vars, line, [](char c) { return c == ' '; });
// Now have ["A:0.7", "B:0.3"]
m_ = vars.size() - 2; // remove "VARIABLES" and "="
assert(m_ - 4 ==
M); // Derived vars X u ZBilger Rho not considered, restart file must have same number of vars as current sim
}
// get number of grid points n_ from ZONE I= ...; assert r > 2
int n_;
{
std::getline(data_stream_, line);
// Start with something like "ZONE I=14"
std::vector<std::string> tmp;
boost::split(tmp, line, [](char c) { return c == ' '; });
// Now have ["ZONE", "I=14"]
std::vector<std::string> tmp2;
boost::split(tmp2, tmp[1], [](char c) { return c == '='; });
// Now have ["I", "14"]
n_ = std::stoi(tmp2[1]);
}
// create Eigen matrix old_mat of size nxm from data
MatrixXd old_mat = MatrixXd::Zero(n_, m_);
{
for (int i = 0; i < n_; i++) {
std::getline(data_stream_, line);
// Start with something like "0.0 0.5 0.6"
std::vector<std::string> tmp;
boost::split(tmp, line, [](char c) { return c == ' '; }, boost::token_compress_on);
if (tmp[0].empty()) tmp.erase(tmp.begin());
// Now have ["0.0", "0.5", "0.6"]
for (int j = 0; j < m_; j++) {
old_mat(i, j) = std::stod(tmp[j]);
}
}
}
if (verbose) std::cout << "Data read from file: " << std::endl << old_mat << std::endl;
// create position vector old_nodes from old_mat.col(0)
VectorXd old_nodes = old_mat.col(0);
// create old_Phi from old_mat.cols(4:end)
MatrixXd old_Phi = old_mat.rightCols(m_ - 4);
T_w_gas = old_Phi(0,idx_T);
// interpolate phi from old_Phi using nodes and old_nodes
{
VectorXd c0 = VectorXd::Zero(N);
VectorXi i_c0 = VectorXi::Zero(N);
// loop on new interior nodes
for (int i = 0; i < N; i++) {
// find old_node to the left of current node. Always available since phi is interior, and old_Phi is full domain.
double eps = 1.0e-10; // in case restarting from identical mesh
int j = 0;
while (old_nodes(j) < nodes(i + 1) + eps) {
j++;
}