-
Notifications
You must be signed in to change notification settings - Fork 2
/
io.cpp
1367 lines (1055 loc) · 56.1 KB
/
io.cpp
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
/**
* io.cpp
*
* This file contains input/output routines.
*/
#include <iomanip>
#include <sstream>
#include <stdexcept>
#include "aiolos.h"
/**
* Reads the species data such as name, mass, adiabatic index, charge, initial density in ghost cell, opacity files etc.
*
* Does a number of convoluted operations to allow for different file formats. Contains also the code to interpolate opacities on-the-fly.
* This happens assuming that the *.opa, or *.op2 or *.op3 files have an arbitrary wavelength spacing, and opacity points given at any wavelength are exact.
* The mean opacities are then computed as integrals over the bands chosen by the user. Note that those means are not Planck-means, but purely data-driven averages with the
* weighting function per wavelength being 1. If correct Planck, Solar and Rosseland means need to be used, then those have to be already contained in the opa files, or tables need to be specified.
*
* @param[in] filename Species file to read from
* @param[in] species_index This species number
*/
int c_Species::read_species_data(string filename, int species_index) {
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//
// Read and interpret *.spc first
//
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//
// If the opacity model is 'P', physical, then we read in opacity data from files
//
opacity_avg_solar = Eigen::VectorXd::Zero(num_bands_in); //num_cells * num_bands
opacity_avg_planck = Eigen::VectorXd::Zero(num_bands_out); //num_cells * num_bands
opacity_avg_rosseland = Eigen::VectorXd::Zero(num_bands_out); //num_cells * num_bands
ifstream file(filename);
string line;
if(!file) {
cout<<"Couldnt open species file "<<filename<<"!"<<endl;
}
int found = 0;
double temp_static_charge = 0.;
this->num_opacity_datas = -1;
if(debug > 0) cout<<" In read species Pos1"<<endl;
while(std::getline( file, line )) {
std::vector<string> stringlist = stringsplit(line," ");
if(stringlist[0].find("@") != string::npos) {
if(std::stoi(stringlist[1]) == species_index) {
found = 1;
this->speciesname = stringlist[2];
this->mass_amu = std::stod(stringlist[3]);
this->degrees_of_freedom = std::stod(stringlist[4]);
temp_static_charge = std::stod(stringlist[5]);
this->initial_fraction = std::stod(stringlist[6]);
this->density_excess = std::stod(stringlist[7]);
this->is_dust_like = std::stod(stringlist[8]);
this->opacity_data_string = stringlist[9];
if(stringlist.size() == 11) //Assume that additional to an opacity table in opacity_data_string a solar opacity wavelength file has been provided
this->opacity_corrk_string = stringlist[10];
this->inv_mass = 1./(mass_amu*amu);
if(debug > 0)
cout<<"Found species called "<<speciesname<<" with a mass of "<<mass_amu<<" dof "<<degrees_of_freedom<<" gamma "<<gamma_adiabat<<" and initial_fraction = "<<initial_fraction<<endl;
}
}
}
if(std::abs(temp_static_charge) < 0.1) {
this->static_charge = 0;
}
else {
this->static_charge = std::floor(temp_static_charge);
}
if(found == 0) {
if(debug > 0)
cout<<"WARNING: Species number "<<species_index<<" not found in parameterfile!"<<endl;
}
if(found > 1) {
cout<<"WARNING: Species number "<<species_index<<" defined more than once in parameterfile!"<<endl;
}
file.close();
if(debug > 0) cout<<" Species readin finished."<<endl;
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//
// Read and interpret *.opa, *.op2, *.op3, and *.aiopa files
//
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
if(base->opacity_model == 'T' || base->opacity_model == 'K') {
//
// Tabulated data from opacit averages
//
string opacityinputfile = "inputdata/" + opacity_data_string;
std::vector<string> stringending = stringsplit(opacityinputfile,".");
if(stringending[1].compare("aiopa") == 0) {
cout<<"Tabulated opacities chosen, reading file = "<<opacityinputfile<<endl;
}
else
cout<<"Invalid file = "<<opacityinputfile<<" should have format *.aitab ."<<endl;
read_opacity_table(opacityinputfile);
}
if(base->opacity_model == 'P' || base->opacity_model == 'M' || base->opacity_model == 'C' || base->opacity_model == 'K' ) {
if( base->opacity_model == 'K' ) //Run the routine with one argument further on in the species list
opacity_data_string = opacity_corrk_string;
cout<<"P or M or C or K opacity chosen & enough data to read in files. Reading file = "<<"inputdata/"<<opacity_data_string<<endl;
//
// Start reading opacity data
//
string opacityinputfile = "inputdata/" + opacity_data_string;
ifstream file2( opacityinputfile);
string line2;
int num_readin_columns = 1;
std::vector<string> stringending = stringsplit(opacityinputfile,".");
if(debug > 0)
cout<<" DEBUG LV2, namepart1 = "<<stringending[0]<<" namepart2 = "<<stringending[1]<<endl;
if(stringending[1].compare("op2") == 0)
num_readin_columns = 2;
if(stringending[1].compare("op3") == 0)
num_readin_columns = 3;
int data_count=0;
while(std::getline( file2, line2 ))
data_count++;
num_opacity_datas = data_count;
file2.clear();
file2.seekg(0, ios::beg);
if(debug > 0)
cout<<" num_readin_columns = "<<num_readin_columns<<" data_count = "<<data_count<<endl;
Eigen::MatrixXd file_opacity_data = Eigen::MatrixXd::Zero(num_opacity_datas, num_readin_columns + 1); //Saves the data in format wl,opa,opa,opa as it is in the file
data_count = 0;
while(std::getline( file2, line2 )) {
std::vector<string> stringlist = stringsplit(line2," ");
file_opacity_data(data_count, 0) = std::stod(stringlist[0]); // Wavelength
file_opacity_data(data_count, 1) = std::stod(stringlist[1]); // Opacity solar
if(num_readin_columns >= 2) file_opacity_data(data_count, 2) = std::stod(stringlist[2]); // Opacity planck
if(num_readin_columns >= 3) file_opacity_data(data_count, 3) = std::stod(stringlist[3]); // Opacity planck
if(debug > 1) {
cout<< "file_opacity_data = "<<file_opacity_data(data_count, 0)<<" "<<file_opacity_data(data_count, 1);
if(num_readin_columns >= 2) cout<<" "<<file_opacity_data(data_count, 2);
if(num_readin_columns >= 3) cout<<" "<<file_opacity_data(data_count, 3);
cout<<endl;
}
data_count++;
}
if(debug > 0)
cout<<"Before fileclose"<<endl;
file2.close();
if(debug > 0)
cout<<"After fileclose"<<endl;
//
// Interpolate the possibly non-uniformely binned opacity data on a uniform grid for fast interpolation later
//
double minDeltaL = 9999999999.;
double deltaL;
// Determine smallest distance
for(int j = 0; j < num_opacity_datas-1; j++) {
deltaL = file_opacity_data(j+1, 0) - file_opacity_data(j, 0); // Wavelength
minDeltaL = (deltaL < minDeltaL)?deltaL:minDeltaL;
if(debug > 1)
cout<<" found deltaL = "<<deltaL<<" at j = "<<j<<endl;
}
// Create grid with resolution as smallest distance (artificial high-res grid, that is later used for averaging)
int num_tmp_lambdas = (file_opacity_data(num_opacity_datas-1,0) - file_opacity_data(0,0))/minDeltaL + 2;
if(debug>0)
cout<<" found mindeltaL = "<<minDeltaL<<" num_tmp_lambdas "<<num_tmp_lambdas<<endl;
//if(debug>1)
opacity_data = Eigen::MatrixXd::Zero(num_tmp_lambdas, num_readin_columns + 1);
// Find high-res opacity in read-in opacity
for (int col = 1; col < num_readin_columns+1; col++) {
if(debug > 1)
cout<<" Doing column "<<col<<endl;
int j = 0;
for(int i = 0; i< num_tmp_lambdas; i++) {
opacity_data(i,0) = file_opacity_data(0,0) + ((double)i) * minDeltaL;
//for(int j = 0; j < num_opacity_datas-1; j++) {
double wl = opacity_data(i,0);
double lmin = file_opacity_data(j, 0);
double lmax = file_opacity_data(j+1, 0);
if(wl > lmax) {
j++;
lmin = file_opacity_data(j, 0);
lmax = file_opacity_data(j+1, 0);
}
//Boundaries of wl grid
if(i==0)
opacity_data(i,col) = file_opacity_data(0,col);
else if(i==num_tmp_lambdas-1)
opacity_data(i,col) = file_opacity_data(num_opacity_datas-1,col);
else {
//Interpolation on highres grid
double m = std::log10(file_opacity_data(j+1,col) / file_opacity_data(j,col) ) / std::log10(lmax/lmin);
opacity_data(i,col) = pow(10., std::log10(file_opacity_data(j,col)) + m * std::log10(wl/lmin) );
}
}
//cout<<" ~~~~~~~~~~~~~~~ Read in opacities for col_num = "<<num_readin_columns<<endl;
}
//
// Compute *the* average opacity per band (planck/rosseland/flux means have to be done on the fly)
//
if(debug > 1) {
cout<<" minDeltaL = "<<minDeltaL<<" num_tmp_lambdas = "<<num_tmp_lambdas<<endl;
if(debug > 2)
cout<<"tmp_opacity_data = "<<endl<<file_opacity_data<<endl;
//cout<<"opacity_data = "<<endl<<opacity_data<<endl;
}
for(int col = 1; col< num_readin_columns+1; col++) {
int num_bands_readin;
std::vector<double>* lgrid;
Eigen::VectorXd* opacity_avg;
if(col==1) {
num_bands_readin = num_bands_in;
lgrid = &base->l_i_in;
opacity_avg = &opacity_avg_solar;
}
else {
num_bands_readin = num_bands_out;
lgrid = &base->l_i_out;
if(col==2) opacity_avg = &opacity_avg_planck;
if(col==3) opacity_avg = &opacity_avg_rosseland;
}
for(int b = 0; b< num_bands_readin; b++) {
int wlcount = 0;
double lmin = (*lgrid)[b]; //base->l_i[b];
double lmax = (*lgrid)[b+1]; //base->l_i[b+1];
(*opacity_avg)[b] = 1e-10;
//cout<<" DEBUG band b = "<<b<<" lmin/lmax = "<<lmin<<"/"<<lmax<<endl;
double wl;
for(int i = 0; i < num_tmp_lambdas; i++) {
double wl = opacity_data(i,0);
//cout<<" i = "<<i<<" wl = "<<wl<<" opa = "<<opacity_data(i,1)<<endl;
if( wl < lmax && wl > lmin) {
(*opacity_avg)[b]+= opacity_data(i,col);
wlcount++;
}
if(i==0) {
//TODO: When data boundary and band boundary do not coincide
}
else if(i==num_tmp_lambdas-2) {
//TODO: When data boundary and band boundary do not coincide
}
}
if(wlcount > 1) {
(*opacity_avg)[b] /= (double)wlcount;
}
else
cout<<" Band "<<b<<" has wlcount==0! lmin/wl/lmax//opacity_data(0,0/1); = "<<lmin<<"/"<<wl<<"/"<<lmax<<"//"<<opacity_data(0,0)<<"/"<<opacity_data(0,1)<<" wlcount = "<<wlcount<<endl;
if(debug > 1)
cout<<" DEBUG. Band "<<b<<" has wlcount=="<<wlcount<<". lmin/wl/lmax//opacity_data(0,0/1); = "<<lmin<<"/"<<wl<<"/"<<lmax<<"//"<<opacity_data(0,0)<<"/"<<opacity_data(0,1)<<" opa_found = "<<(*opacity_avg)[b]<<endl;
//
// For bands which have no opacity data given / first and last band, we assume the nearest datapoint
//
if(lmax < opacity_data(0,0) ) {
(*opacity_avg)[b] = opacity_data(0,col);
cout<<" Band b, lmax "<<lmax<<"< opa_data(0,0)"<<opacity_data(0,0)<<endl;
}
if(lmin > opacity_data(num_tmp_lambdas-1,0) ) {
(*opacity_avg)[b] = opacity_data(num_tmp_lambdas-1,col);
cout<<" Band b, lmin "<<lmin<<"< opa_data(-1,0)"<<opacity_data(num_tmp_lambdas,0)<<endl;
}
if((*opacity_avg)[b] < 0.) {
cout<<"Band b = "<<b<<"is faulty! opacity = "<<(*opacity_avg)[b]<<" wlcount ="<<wlcount<<endl;
cout<<" Band b, lmin "<<lmin<<" opa_data(-1,0) = "<<opacity_data(num_tmp_lambdas,0)<<endl;
cout<<" Band b, lmax "<<lmax<<" opa_data(0,0) = "<<opacity_data(0,0)<<endl;
}
}
}
if(debug > 1) {
cout<<"Pos before additional columns."<<endl;
}
if(num_readin_columns < 3)
for(int b = 0; b < num_bands_out; b++) {
opacity_avg_rosseland(b) = const_opacity;
if(debug> 1)
cout<<" DEBUG b_out = "<<b<<" avg_rosseland = "<<opacity_avg_rosseland(b)<<" const_opa = "<<const_opacity<<endl;
}
if(num_readin_columns < 2)
for(int b = 0; b < num_bands_out; b++) {
opacity_avg_planck(b) = const_opacity;
if(debug> 1)
cout<<" DEBUG b_out = "<<b<<" avg_planck = "<<opacity_avg_rosseland(b)<<" const_opa = "<<const_opacity<<endl;
}
if(debug > 1) {
cout<<"Pos after additional columns."<<endl;
}
for(int b = 0; b < num_bands_in; b++) if(std::isnan(opacity_avg_solar(b) )) opacity_avg_solar(b) = 1e-10;
for(int b = 0; b < num_bands_out; b++) if(std::isnan(opacity_avg_planck(b) )) opacity_avg_planck(b) = 1e-10;
for(int b = 0; b < num_bands_out; b++) if(std::isnan(opacity_avg_rosseland(b) )) opacity_avg_rosseland(b) = 1e-10;
for(int b = 0; b < num_bands_in; b++) if((opacity_avg_solar(b)<1e-10 )) opacity_avg_solar(b) = 1e-10;
for(int b = 0; b < num_bands_out; b++) if((opacity_avg_planck(b)<1e-10 )) opacity_avg_planck(b) = 1e-10;
for(int b = 0; b < num_bands_out; b++) if((opacity_avg_rosseland(b)<1e-10 )) opacity_avg_rosseland(b) = 1e-10;
if(this->this_species_index ==7) {
if(num_bands_in >= 10)
opacity_avg_solar(10) = 1e-10;
}
//Done! Now plot debug stuff so that we're sure the opacities do what they should.
cout<<"Done reading in data for species = "<<species_index<<". Wavelength grids in/out = ";
for(int b = 0; b <= num_bands_in; b++) cout<<base->l_i_in[b]<<"/";
cout<<" ||| ";
for(int b = 0; b <= num_bands_out; b++) cout<<base->l_i_out[b]<<"/";
cout<<endl<<endl;
cout<<" avg opacities solar = "<<endl;
for(int b = 0; b < num_bands_in; b++) cout<<"<"<<base->l_i_in[b]<<" - "<<base->l_i_in[b+1]<<" mum > "<<opacity_avg_solar(b)<<endl; //"/";
cout<<endl;
cout<<" avg opacities plnck = ";
for(int b = 0; b < num_bands_out; b++) cout<<opacity_avg_planck(b)<<"/";
cout<<endl;
cout<<" avg opacities rossl = ";
for(int b = 0; b < num_bands_out; b++) cout<<opacity_avg_rosseland(b)<<"/";
cout<<endl;
}
else {
//No other opacity mode needed for now. Do nothing. Table reading mode has been moved to lines 120ff.
}
return 0;
}
/**
* Read in pressure and temperature dependent opacity tables from *.aiopa files.
*
* Aiopa files currently have the hardcoded format: Arbitrary number of P-T grids for Irradiation means, One P-T grid for Planck-mean, One P-T grid for outgoing Rosseland mean band value.
*
* @param[in] tablename Filename of the table file.
*/
void c_Species::read_opacity_table(string tablename) {
int num_bands_out = base->num_bands_out;
int num_bands_in = base->num_bands_in;
cout<<"Attempting to open "<<tablename<<"!"<<endl;
ifstream file(tablename);
string line;
if(!file) {
cout<<"Couldnt open opacity table "<<tablename<<"!"<<endl;
}
//simulation_parameter tmp_parameter = {"NaN",0,0.,0,"NaN"};
this->num_opacity_datas = -1;
if(debug >= 1) cout<<" In read Opacities Pos1. Trying to 2*read num_bands_out + num_bands_in ="<<2*num_bands_out+num_bands_in<<" opacity blocks."<<endl;
if(num_bands_in == num_bands_out)
cout<<"WARNING: num_bands_in == num_bands_out, this is the default. Check if you really want this. Might break opacity reading."<<endl;
//Get the size of the table first
//
std::getline( file, line );
//cout<<"Got the line!"<<endl;
std::vector<string> stringlist = stringsplit(line," ");
//cout<<"String is split!"<<stringlist[0]<<endl;
opa_pgrid_size = std::stod(stringlist[0]);
opa_tgrid_size = std::stod(stringlist[1]);
//cout<<" Identified grid size ="<<opa_pgrid_size<<" / "<<opa_tgrid_size<<endl;
if(opa_pgrid_size <= 0 || opa_tgrid_size > 9999) {
cout<<" ERROR with pressure grid size = "<<opa_pgrid_size<<" in read_opacity_table!"<<endl;
}
if(opa_tgrid_size <= 0 || opa_tgrid_size > 9999) {
cout<<" ERROR with temperature grid size = "<<opa_tgrid_size<<" in read_opacity_table!"<<endl;
}
//Now allocate memory and read data
//
opa_pgrid = Eigen::VectorXd::Zero(opa_pgrid_size);
opa_tgrid = Eigen::VectorXd::Zero(opa_tgrid_size);
opa_pgrid_log = Eigen::VectorXd::Zero(opa_pgrid_size);
opa_tgrid_log = Eigen::VectorXd::Zero(opa_tgrid_size);
opa_grid_solar = Eigen::VectorXd::Zero(opa_pgrid_size * opa_tgrid_size * num_bands_in);
opa_grid_rosseland = Eigen::VectorXd::Zero(opa_pgrid_size * opa_tgrid_size * num_bands_out);
opa_grid_planck = Eigen::VectorXd::Zero(opa_pgrid_size * opa_tgrid_size * num_bands_out);
opa_grid_solar_log = Eigen::VectorXd::Zero(opa_pgrid_size * opa_tgrid_size * num_bands_in);
opa_grid_rosseland_log = Eigen::VectorXd::Zero(opa_pgrid_size * opa_tgrid_size * num_bands_out);
opa_grid_planck_log = Eigen::VectorXd::Zero(opa_pgrid_size * opa_tgrid_size * num_bands_out);
//cout<<" After memory allocation. Sizes are "<<opa_pgrid_size * opa_tgrid_size * num_bands_in<<" and "<<opa_pgrid_size * opa_tgrid_size * num_bands_out<<endl;
//One empty line
std::getline( file, line );
//Read pgrid
//
std::getline( file, line );
stringlist = stringsplit(line," ");
for(int i=0; i<opa_pgrid_size; i++) {
opa_pgrid(i) = std::stod(stringlist[i]);
opa_pgrid_log(i) = std::log10(opa_pgrid(i));
//cout<<" opa_pgrid = "<<opa_pgrid(i)<<endl;
}
//Read tgrid
//
std::getline( file, line );
stringlist = stringsplit(line," ");
for(int i=0; i<opa_tgrid_size; i++) {
opa_tgrid(i) = std::stod(stringlist[i]);
opa_tgrid_log(i) = std::log10(opa_tgrid(i));
//cout<<" opa_tgrid = "<<opa_tgrid(i)<<endl;
}
// Planck two-temperature mean-opacity grid
//
std::getline( file, line );
for(int bi = 0; bi<1; bi++) { //for(int bi = 0; bi<num_bands_in; bi++) { TODO: REMOVE THIS LATER!
//std::getline( file, line );
for(int i=0; i<opa_pgrid_size; i++) {
std::getline( file, line );
stringlist = stringsplit(line," ");
for(int j=0; j<opa_tgrid_size; j++) {
//cout<<" opas_ = "<<std::stod(stringlist[j])<<" assigned to "<< j + i * opa_tgrid_size + bi * opa_pgrid_size * opa_tgrid_size<<" list pos "<<j<<endl;
opa_grid_solar( j + i * opa_tgrid_size + bi * opa_pgrid_size * opa_tgrid_size ) = std::stod(stringlist[j]);
opa_grid_solar_log( j + i * opa_tgrid_size + bi * opa_pgrid_size * opa_tgrid_size ) = std::log10(std::stod(stringlist[j]));
}
}
}
cout<<" finshed opas "<<endl;
// Planck single-temperature mean-opacity grid
//
std::getline( file, line );
for(int bo = 0; bo<num_bands_out; bo++) {
//std::getline( file, line );
for(int i=0; i<opa_pgrid_size; i++) {
std::getline( file, line );
stringlist = stringsplit(line," ");
for(int j=0; j<opa_tgrid_size; j++) {
//cout<<" opap_ = "<<std::stod(stringlist[j])<<" assigned to "<< j + i * opa_tgrid_size + bo * opa_pgrid_size * opa_tgrid_size<<" list pos "<<j<<endl;
opa_grid_planck( j + i * opa_tgrid_size + bo * opa_pgrid_size * opa_tgrid_size ) = std::stod(stringlist[j]);
opa_grid_planck_log( j + i * opa_tgrid_size + bo * opa_pgrid_size * opa_tgrid_size ) = std::log10(std::stod(stringlist[j]));
}
}
}
cout<<" finshed opap "<<endl;
// Rosseland mean-opacity grid
//
std::getline( file, line );
for(int bo = 0; bo<num_bands_out; bo++) {
//std::getline( file, line );
for(int i=0; i<opa_pgrid_size; i++) {
std::getline( file, line );
stringlist = stringsplit(line," ");
for(int j=0; j<opa_tgrid_size; j++) {
//cout<<" opar_ = "<<std::stod(stringlist[j])<<" assigned to "<< j + i * opa_tgrid_size + bo * opa_pgrid_size * opa_tgrid_size<<endl;
opa_grid_rosseland( j + i * opa_tgrid_size + bo * opa_pgrid_size * opa_tgrid_size ) = std::stod(stringlist[j]);
opa_grid_rosseland_log( j + i * opa_tgrid_size + bo * opa_pgrid_size * opa_tgrid_size ) = std::log10(std::stod(stringlist[j]));
}
}
}
cout<<" Successfully finished reading opacity table. "<<endl;
//cout<<"Showing read data: pgrid / tgrid = "<<endl<<opa_pgrid<<endl<<"///"<<endl<<opa_tgrid<<endl;
//cout<<"Showing read data: solar opas = "<<endl<<opa_grid_solar<<endl;
//cout<<"Showing read data: planck opas = "<<endl<<opa_grid_planck<<endl;
//cout<<"Showing read data: planck rosseland = "<<endl<<opa_grid_rosseland<<endl;
//char a;
//cin>>a;
}
template<typename T>
T parse_item(string item) {
std::stringstream ss(item);
T val;
ss >> val;
return val;
}
class ParameterError : public std::invalid_argument {
public:
ParameterError(int count_, string msg)
: std::invalid_argument(msg), count(count_)
{ } ;
int count ;
} ;
/**
* Read parameter: Looks for the value of one parameter in a file
*
* @param[in] filename The filename that should contain all parameters and their values
* @param[in] variablename the name of one individual parameter in the file
* @param[in] debug Display debug information.
* @return The value of variablename in filename as given by the template type. User is responsible of type safety here.
*/
template<typename T>
simulation_parameter<T> read_parameter_from_file(string filename, string variablename, int debug) {
ifstream file(filename);
string line;
if(!file) {
std::stringstream error ;
error << "Could not open parameter file:" << filename << endl ;
throw std::runtime_error(error.str()) ;
}
simulation_parameter<T> tmp_parameter = {"NaN",T()};
int found = 0;
//cout<<" In read_parameter Pos1"<<endl;
while(std::getline( file, line )) {
//cout<<" In read_parameter Pos1.5: "<<line<<endl;
if(line.find(variablename) != string::npos) {
tmp_parameter.name = variablename;
tmp_parameter.value = parse_item<T>(stringsplit(line," ")[1]) ;
if(debug > 0)
cout<<"Found variable called "<<variablename<<" and set it to "<<tmp_parameter.value<<endl;
found++;
}
}
//cout<<" In read_parameter Pos2"<<endl;
if(found == 0) {
std:: stringstream error ;
if(debug > 1)
error << "ERROR: Variable "<<variablename<<" not found in parameterfile!"<<endl;
else
error<<"";
throw ParameterError(0, error.str()) ;
}
if(found > 1) {
std:: stringstream error ;
error <<"ERROR: Variable "<<variablename<<" defined more than once in parameterfile!"<<endl;
throw ParameterError(found, error.str()) ;
}
file.close();
return tmp_parameter;
}
/**
* Read parameter: Looks for the value of one parameter in a file
*
* @param[in] filename The filename that should contain all parameters and their values
* @param[in] variablename the name of one individual parameter in the file
* @param[in] debug Display debug information.
* @param[in] default_ Default value for variablename, which is returned if variablename is not found in filename.
* @return The value of variablename in filename as given by the template type. User is responsible of type safety here.
*/
template<typename T>
simulation_parameter<T> read_parameter_from_file(string filename, string variablename, int debug, T default_) {
simulation_parameter<T> parameter ;
try {
parameter = read_parameter_from_file<T>(filename, variablename, debug) ;
} catch (ParameterError& e) {
if (e.count == 0) {
parameter.name = variablename ;
parameter.value = default_ ;
if(debug > 1)
cout<<"WARNING: Parameter "<<variablename<<" not found in "<<filename<<". Assigning default value of "<<default_<<endl;
}
else
throw ; // re-throw because we can't help with duplicated parameters
}
return parameter ;
}
template simulation_parameter<bool> read_parameter_from_file(string, string, int, bool);
template simulation_parameter<bool> read_parameter_from_file(string, string, int);
template simulation_parameter<int> read_parameter_from_file(string, string, int, int);
template simulation_parameter<int> read_parameter_from_file(string, string, int);
template simulation_parameter<double> read_parameter_from_file(string, string, int, double);
template simulation_parameter<double> read_parameter_from_file(string, string, int);
template simulation_parameter<char> read_parameter_from_file(string, string, int, char);
template simulation_parameter<char> read_parameter_from_file(string, string, int);
template simulation_parameter<string> read_parameter_from_file(string, string, int, string);
template simulation_parameter<string> read_parameter_from_file(string, string, int);
template simulation_parameter<Geometry> read_parameter_from_file(string, string, int, Geometry);
template simulation_parameter<Geometry> read_parameter_from_file(string, string, int);
template simulation_parameter<BoundaryType> read_parameter_from_file(string, string, int, BoundaryType);
template simulation_parameter<BoundaryType> read_parameter_from_file(string, string, int);
template simulation_parameter<IntegrationType> read_parameter_from_file(string, string, int, IntegrationType);
template simulation_parameter<IntegrationType> read_parameter_from_file(string, string, int);
/**
* Write the main output file. Each species generates their own output file of a fixed column number (as opposed to diagnostic files, which change their column numbers depending on simulation setup, i.e. num species, bands in, bands out).
*
* File contains mostly hydrodynamic and thermodynamic information, cfl timestep limitations, gravity, heating and cooling functions.
* Users can adjust whether they want ghost cell information in their files or not.
* More details in cheat_sheet.ods in the main aiolos github.
*
* @param[in] timestepnumber Timestamp index on the file (e.g. 0, 1, 2... -1)
*/
void c_Species::print_AOS_component_tofile(int timestepnumber) {
//
// If we are computing winds, compute the analytic solution for outputting it
//
if(base->init_wind==1 && base->problem_number == 2)
compute_analytic_solution();
//
//
// Step 1: Print general information file with fixed column numbers for all setups
//
//
string filename ;
{
stringstream filenamedummy;
string truncated_name = stringsplit(base->simname,".")[0];
filenamedummy<<base->workingdir<<"output_"<<truncated_name<<"_"<<speciesname<<"_t"<<timestepnumber<<".dat";
filename = filenamedummy.str() ;
}
if(debug > 1)
cout<<"Trying to open file "<<filename<<endl;
ofstream outfile(filename, ios::out);
outfile << std::setprecision(15) ;
if (outfile.is_open())
{
//outfile.precision(16);
//double hydrostat2 = 0., hydrostat3 = 0.;
//Print left ghost stuff
outfile<<base->x_i12[0]<<'\t'<<u[0].u1<<'\t'<<u[0].u2<<'\t'<<u[0].u3<<'\t'<<flux[0].u1<<'\t'<<flux[0].u2<<'\t'<<flux[0].u3<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<prim[0].pres<<'\t'<<u[0].u2/u[0].u1<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<base->phi[0]<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<'-'<<endl;
//Print the domain
base->enclosed_mass_tmp[0] = 0.;
for(int i = 2; i <= num_cells; i++) {
base->enclosed_mass_tmp[i] = base->enclosed_mass_tmp[i-1] + 4. * 3.141592 * (pow(base->x_i[i],3.)-pow(base->x_i[i-1],3.) )/3. * u[i].u1;
}
for(int i=1; i<= num_cells; i++) {
double balance1 = ((flux[i-1].u1 * base->surf[i-1] - flux[i].u1 * base->surf[i]) / base->vol[i] + (source[i].u1 +source_pressure[i].u1));
double balance2 = ((flux[i-1].u2 * base->surf[i-1] - flux[i].u2 * base->surf[i]) / base->vol[i] + (source[i].u2 +source_pressure[i].u2));
double balance3 = ((flux[i-1].u3 * base->surf[i-1] - flux[i].u3 * base->surf[i]) / base->vol[i] + (source[i].u3 +source_pressure[i].u3));
//hydrostat = flux[i-1].u2/base->dx[i] ; //hydrostat2 + hydrostat3 ;
//hydrostat2 = flux[i].u2/base->dx[i];//pressure[i+1] - pressure[i];
//hydrostat3 = source[i].u2;//0.5 * (u[i].u1 + u[i+1].u1) * (phi[i+1] - phi[i]);
double Jtot = 0.;
double Stot = 0.;
for(int b=0; b<num_bands_in; b++) {
Stot += base->S_band(i,b);
}
for(int b=0; b<num_bands_out; b++) {
Jtot += base->Jrad_FLD(i,b)/c_light*4.*pi;
}
//outfile<<base->x_i12[i]<<'\t'<<u[i].u1<<'\t'<<u[i].u2<<'\t'<<u[i].u3<<'\t'<<flux[i].u1<<'\t'<<flux[i].u2<<'\t'<<flux[i].u3<<'\t'<<balance1<<'\t'<<balance2<<'\t'<<balance3<<'\t'<<prim[i].pres<<'\t'<<u[i].u2/u[i].u1<<'\t'<<prim[i].temperature <<'\t'<<timesteps_cs[i]<<'\t'<<base->cflfactor/timesteps[i]<<'\t'<<prim[i].sound_speed<<'\t'<<timesteps_de[i]<<'\t'<<u_analytic[i]<<'\t'<<base->alphas_sample(i)<<'\t'<<base->phi[i]<<'\t'<<base->enclosed_mass_tmp[i]<<'\t'<<Jtot<<'\t'<<Stot<<'\t'<<base->friction_sample(i)<<endl;
outfile<<base->x_i12[i]<<'\t'<<u[i].u1<<'\t'<<u[i].u2<<'\t'<<u[i].u3<<'\t'<<flux[i].u1<<'\t'<<flux[i].u2<<'\t'<<flux[i].u3<<'\t'<<balance1<<'\t'<<balance2<<'\t'<<balance3<<'\t'<<prim[i].pres<<'\t'<<u[i].u2/u[i].u1<<'\t'<<prim[i].temperature <<'\t'<<timesteps_cs[i]<<'\t'<<base->cflfactor/timesteps[i]<<'\t'<<prim[i].sound_speed<<'\t'<<timesteps_de[i]<<'\t'<<u_analytic[i]<<'\t'<<base->alphas_sample(i)<<'\t'<<phi_s[i]<<'\t'<<base->enclosed_mass_tmp[i]<<'\t'<<-dG(i)+dGdT(i)*prim[i].temperature <<'\t'<<dS(i)<<'\t'<<base->cell_optical_depth(i,0)<<endl;
}
//Print right ghost stuff
outfile<<base->x_i12[num_cells+1]<<'\t'<<u[num_cells+1].u1<<'\t'<<u[num_cells+1].u2<<'\t'<<u[num_cells+1].u3<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<prim[num_cells+1].pres<<'\t'<<u[num_cells+1].u2/u[num_cells+1].u1<<'\t'<<prim[num_cells+1].temperature<<'\t'<<'-'<<'\t'<<base->phi[num_cells+1]<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<'-'<<'\t'<<'-'<<endl;
cout<<" Sucessfully written file "<<filename<<" for species = "<<speciesname<<" t = "<<base->globalTime<<" dt = "<<base->dt<<", cfl = "<<base->cflfactor<<" steps = "<<base->steps<<endl;
}
else cout << "Unable to open file" << filename << endl;
outfile.close();
}
/**
* Write the diagnostic file. Has a setup-dependent column number i.e. depends on num species, bands in, bands out, as opposed to the main output files which have a fixed number of columns.
*
* File contains information about all radiative bands, incoming radiation, total heating function, emitted radiation, optical depths per cell, integrated optical depths, opacities, radiative temperatures per band, outgoing fluxes per band, convective fluxes. More details in cheat_sheet.ods in the main aiolos github.
*
* @param[in] outputnumber Timestamp index on the file (e.g. 0, 1, 2... -1)
*/
void c_Sim::print_diagnostic_file(int outputnumber) {
//
//
// Print radiation + species diagnostics file. Column numbers in this file will vary depending on setup
//
//
//Only print this file once, as it contains all the info
string filenameDiagnostic ;
{
stringstream filenamedummy;
string truncated_name = stringsplit(simname,".")[0];
filenamedummy<<workingdir<<"diagnostic_"<<truncated_name<<"_t"<<outputnumber<<".dat";
filenameDiagnostic = filenamedummy.str() ;
}
if(debug > 1)
cout<<"Trying to open file "<<filenameDiagnostic<<endl;
ofstream outfileDiagnostic(filenameDiagnostic, ios::out);
outfileDiagnostic << std::setprecision(15) ;
if (outfileDiagnostic.is_open())
{
for(int i=1; i<=num_cells; i++) {
//
//Band-by-band info
//
double Jtot = 0.;
double Stot = 0.;
for(int b=0; b<num_bands_in; b++) {
Stot += S_band(i,b);
}
for(int b=0; b<num_bands_out; b++) {
Jtot += Jrad_FLD(i,b)/c_light*4.*pi; //This actually outputs the energy density E_rad from c E_rad = 4pi J
}
outfileDiagnostic<<x_i12[i]<<'\t'<<Jtot<<'\t'<<Stot; //Rows 1 2 3
//Radiation field
for(int b=0; b<num_bands_out; b++) {
outfileDiagnostic<<'\t'<<Jrad_FLD(i,b);
}
//Solar radiation field
for(int b=0; b<num_bands_in; b++) {
outfileDiagnostic<<'\t'<<S_band(i,b);
}
//Solar heating profile
for(int b=0; b<num_bands_in; b++) {
outfileDiagnostic<<'\t'<<dS_band(i,b) + dS_band_special(i,b);
}
//Total opacity from all species
for(int b=0; b<num_bands_out; b++) {
outfileDiagnostic<<'\t'<<total_opacity(i,b);
}
//Total opacity from all species
for(int b=0; b<num_bands_out; b++) {
outfileDiagnostic<<'\t'<<cell_optical_depth(i,b);
}
//Optical depths for solar radiation
for(int b=0; b<num_bands_in; b++) {
//outfileDiagnostic<<'\t'<<radial_optical_depth(i,b);
outfileDiagnostic<<'\t'<<radial_optical_depth_twotemp(i,b);
}
//
//Species-by-species and band opacities
//
for(int b=0; b<num_bands_in; b++) {
for(int s=0; s<num_species; s++) {
outfileDiagnostic<<'\t'<<species[s].opacity_twotemp(i,b);
}
}
for(int b=0; b<num_bands_out; b++) {
for(int s=0; s<num_species; s++) {
outfileDiagnostic<<'\t'<<species[s].opacity_planck(i,b);
}
for(int s=0; s<num_species; s++) {
outfileDiagnostic<<'\t'<<species[s].opacity(i,b);
}
}
//Radiative and species temperatures
outfileDiagnostic<<'\t'<<pow(Jtot/sigma_rad*c_light/4., 0.25); //col 5: The radiative temperature
for(int s=0; s<num_species; s++)
outfileDiagnostic<<'\t'<<species[s].prim[i].temperature; //col 6-...: all species temperatures
//Pressure
double tempP = 0.;
for(int s=0; s<num_species; s++)
tempP += species[s].prim[i].pres;
outfileDiagnostic<<'\t'<<tempP;
//Fluxes
for(int b=0; b<num_bands_out; b++) {
double dx = (x_i12[i+1]-x_i12[i]) ;
double rhokr = max(2.*(total_opacity(i,b)*total_opacity(i+1,b))/(total_opacity(i,b) + total_opacity(i+1,b)), 4./3./dx );
rhokr = min( 0.5*( total_opacity(i,b) + total_opacity(i+1,b)) , rhokr);
double tau_inv = 1. / (dx * rhokr) ;
//double tau_inv = 0.5 / (dx * (total_opacity(i,b) + total_opacity(i+1,b))) ;
double R = xi_rad * tau_inv * std::abs(Jrad_FLD(i+1,b) - Jrad_FLD(i,b)) / (Jrad_FLD(i, b) + 1e-300) ;
double flux_limiter = 0;
if (R <= 2)
flux_limiter = 2 / (3 + std::sqrt(9 + 10*R*R)) ;
else
flux_limiter = 10 / (10*R + 9 + std::sqrt(81 + 180*R)) ;
double D = surf[i] * flux_limiter * tau_inv;
double flux = - 4. * pi * D * (Jrad_FLD(i+1,b) - Jrad_FLD(i,b));
outfileDiagnostic<<'\t'<<flux;
}
//Convective fluxes
if(use_convective_fluxes) {
for (int s=0; s < num_species; s++) {
outfileDiagnostic<<'\t'<<species[s].lconvect.at(i);
}
}
outfileDiagnostic<<endl;
}
}else cout << "Unable to open file" << filenameDiagnostic << endl;
outfileDiagnostic.close();
}
/**
* Print monitor file. Used to monitor, mass momentum and energy conservation in a cartesian box without any forces acting. Currently disabled output.
*
* @param[in] num_steps Current step number in the main loop.
*/
void c_Sim::print_monitor(int num_steps) {
//
//
// First compute the volumetric sum of all initial conserved quantities
//
//
if(num_steps==0) {
initial_monitored_quantities.u1_tot = 0.;
initial_monitored_quantities.u2_tot = 0.;
initial_monitored_quantities.u3_tot = 0.;
for(int i=2; i<num_cells-1; i++)
for(int s=0; s<num_species; s++) {
initial_monitored_quantities.u1_tot += species[s].u[i].u1 * vol[i];
initial_monitored_quantities.u2_tot += species[s].u[i].u2 * vol[i];
initial_monitored_quantities.u3_tot += species[s].u[i].u3 * vol[i];
}
}
//
//
// Open and write into the monitor file - the file monitoring quantitiess as function of time, not space
//
//
string filename2 ;
{
stringstream filenamedummy;
string truncated_name = stringsplit(simname,".")[0];
filenamedummy<<workingdir<<"monitor_"<<truncated_name<<".dat";
filename2 = filenamedummy.str() ;
}
//
// Delete old file if we start a new one
//
if(steps==0) {
ofstream monitor(filename2);
monitor.close();
}
ofstream monitor(filename2, std::ios_base::app);
monitor << std::setprecision(24);
if(monitor.is_open()){
//
// Compute conserved quantities on entire domain over all species
//
monitored_quantities.u1_tot = 0.;
monitored_quantities.u2_tot = 0.;
monitored_quantities.u3_tot = 0.;
for(int i=2; i<num_cells-1; i++)
for(int s=0; s<num_species; s++) {
monitored_quantities.u1_tot += species[s].u[i].u1 * vol[i];
monitored_quantities.u2_tot += species[s].u[i].u2 * vol[i];
monitored_quantities.u3_tot += species[s].u[i].u3 * vol[i];
}