-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparameters.cpp
More file actions
1595 lines (1389 loc) · 53.3 KB
/
parameters.cpp
File metadata and controls
1595 lines (1389 loc) · 53.3 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
#include <algorithm>
#include <functional>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
#include <sstream>
#include <stdexcept>
#include <utility>
#if (__cplusplus >= 201703L)
#include <filesystem>
namespace fs = std::filesystem;
#endif
#include <libconfig.h++>
extern "C" {
#include <cmath>
#include <gsl/gsl_eigen.h>
#include <gsl/gsl_vector_complex.h>
#include <gsl/gsl_matrix_complex_double.h>
}
#include "../include/io.hpp"
#include "../include/omega_matrix.hpp"
#include "../include/version.hpp"
#include "../include/parameters.hpp"
using std::size_t;
using std::pow;
using std::string;
/* Special definition for double, allowing also int and converting */
template<>
bool Config::set_param_value<double>(
const libconfig::Config& cfg,
const std::string& param,
bool required
)
{
try {
const libconfig::Setting& setting = cfg.lookup(param);
switch (setting.getType()) {
case libconfig::Setting::TypeInt:
case libconfig::Setting::TypeInt64:
{
int value = setting;
set<double>(param, value);
return true;
}
case libconfig::Setting::TypeFloat:
{
set<double>(param, setting);
return true;
}
default:
throw ConfigException("Encountered type exception parsing " +
param + " setting.");
}
}
catch (const ConfigException& ce) {
throw ce;
}
catch (const libconfig::SettingNotFoundException& nfex) {
if (required) {
throw ConfigException("No " + param + " (required) setting found in "
"configuration file.");
}
return false;
}
}
/* Special definition for size_t, converting from int */
template<>
bool Config::set_param_value<size_t>(
const libconfig::Config& cfg,
const std::string& param,
bool required
)
{
try {
int value = cfg.lookup(param);
if (value < 0) {
throw ConfigException("Negative value for " + param + " setting "
"not accepted.");
}
set<size_t>(param, static_cast<size_t>(value));
}
catch (const ConfigException& e) {
throw e;
}
catch (const libconfig::SettingNotFoundException& nfex) {
if (required) {
throw ConfigException("No " + param + " (required) setting found in "
"configuration file.");
}
return false;
}
catch (const libconfig::SettingTypeException& tex) {
throw ConfigException("Encountered type exception parsing " + param +
" setting.");
}
return true;
}
template<typename T>
bool Config::set_param_value(
const libconfig::Config& cfg,
const string& param,
bool required
)
{
try {
set<T>(param, cfg.lookup(param));
}
catch (const libconfig::SettingNotFoundException& nfex) {
if (required) {
throw ConfigException("No " + param + " (required) setting found in "
"configuration file.");
}
return false;
}
catch (const libconfig::SettingTypeException& tex) {
throw ConfigException("Encountered type exception parsing " + param +
" setting.");
}
return true;
}
template<typename T>
bool Config::set_param_value(
const libconfig::Setting& cfg,
const string& prefix,
const string& param,
bool required
)
{
try {
set<T>(prefix + "_" + param, cfg.lookup(param));
}
catch (const libconfig::SettingNotFoundException& nfex) {
if (required) {
throw ConfigException("No " + param + " (required) setting found in "
"configuration file.");
}
return false;
}
catch (const libconfig::SettingTypeException& tex) {
throw ConfigException("Encountered type exception parsing " + param +
" setting.");
}
return true;
}
template<typename T>
T Config::get_param_value(
const libconfig::Config& cfg,
const string& param,
bool required
)
{
try {
return cfg.lookup(param);
}
catch (const libconfig::SettingNotFoundException& nfex) {
if (required) {
throw ConfigException("No " + param + " (required) setting found in "
"configuration file.");
}
}
catch (const libconfig::SettingTypeException& tex) {
throw ConfigException("Encountered type exception parsing " + param +
" setting.");
}
return T();
}
Vec1D<string> Config::keys_not_recognized(const libconfig::Config& cfg) const {
const libconfig::Setting& root = cfg.getRoot();
Vec1D<string> keys;
/* Keys that are converted into other options in Config, these are allowed
* as well */
Vec1D<string> additional_keys = {
"loops",
"k_a_grid",
"k_a_grid_file",
"k_b_grid",
"k_b_grid_file",
"k_c_grid",
"k_c_grid_file",
"correlations",
"input_ps_rescale",
"bias_parameters",
"cuba_settings",
"ode_settings",
"kappa_values",
"zeta_files",
"xi_files",
"omega_eigenspace_settings"
};
for (int i = 0; i < root.getLength(); ++i) {
string key = root[i].getName();
if (params.find(key) == params.end() &&
std::find(additional_keys.begin(), additional_keys.end(), key)
== additional_keys.end())
{
keys.push_back(key);
}
}
return keys;
}
void Config::set_spectrum(const libconfig::Config& cfg)
{
string spectrum_str = get_param_value<string>(cfg, "spectrum", true);
std::transform(spectrum_str.begin(), spectrum_str.end(),
spectrum_str.begin(),
[](unsigned char c) {return tolower(c);}
);
if (spectrum_str == "powerspectrum") {
set("spectrum", POWERSPECTRUM);
}
else if (spectrum_str == "bispectrum") {
set("spectrum", BISPECTRUM);
}
else {
throw ConfigException("Unknown spectrum parsed from configuration file.");
}
try {
const libconfig::Setting& correlation = cfg.lookup("correlations");
int count = correlation.getLength();
if (count == 0 && !get<bool>("rsd")) {
std::cout << "Info: correlations setting empty, computing";
if (get<Spectrum>("spectrum") == POWERSPECTRUM) {
std::cout << " [0,0] ";
pair_correlations_.push_back({0,0});
}
else if (get<Spectrum>("spectrum") == BISPECTRUM) {
std::cout << " [0,0,0] ";
triple_correlations_.push_back({0,0,0});
}
std::cout << std::endl;
}
else if (count > 0 && get<bool>("rsd")) {
std::cout << "Warning: correlations setting ignored for RSD."
<< std::endl;
}
else {
for (int i = 0; i < count; ++i) {
int a = 0;
int b = 0;
int c = 0;
if (get<Spectrum>("spectrum") == POWERSPECTRUM) {
if (correlation[i].getLength() != 2) {
throw ConfigException(
"Correlation must be two indices (powerspectrum)");
}
a = correlation[i][0];
b = correlation[i][1];
pair_correlations_.push_back({a,b});
}
else if (get<Spectrum>("spectrum") == BISPECTRUM) {
if (correlation[i].getLength() != 3) {
throw ConfigException(
"Correlation must be three indices (bispectrum)");
}
a = correlation[i][0];
b = correlation[i][1];
c = correlation[i][1];
triple_correlations_.push_back({a,b,c});
}
if (a < 0 || a >= COMPONENTS ||
b < 0 || b >= COMPONENTS ||
c < 0 || c >= COMPONENTS
) {
throw ConfigException("a, b and c must be element in [0," +
std::to_string(COMPONENTS) + "]");
}
}
}
}
catch (const libconfig::SettingNotFoundException& nfex) {
if (!get<bool>("rsd")) {
std::cout << "Info: correlations setting empty, computing";
if (get<Spectrum>("spectrum") == POWERSPECTRUM) {
std::cout << " [0,0] ";
pair_correlations_.push_back({0,0});
}
else if (get<Spectrum>("spectrum") == BISPECTRUM) {
std::cout << " [0,0,0] ";
triple_correlations_.push_back({0,0,0});
}
std::cout << std::endl;
}
}
catch (const ConfigException& e) {
throw e;
}
catch (const libconfig::SettingTypeException& tex) {
throw ConfigException("Encountered type exception for correlations setting.");
}
}
void Config::set_bispectrum_ext_momenta(const libconfig::Config& cfg)
{
double k_a = get<double>("k_a");
string k_b_grid_file;
Vec2D<double> k_b_grid;
if (cfg.lookupValue("k_b_grid", k_b_grid_file)) {
read_delimited_file(k_b_grid_file, k_b_grid);
}
/* If k_b_idx != -1, i.e. given as command line option, use this value */
if (!k_b_grid.empty() && (k_b_idx != -1 || cfg.lookupValue("k_b_idx", k_b_idx))) {
try {
set("k_b", k_b_grid.at(static_cast<size_t>(k_b_idx)).at(0));
}
catch (const std::out_of_range& e) {
throw ConfigException("k_b_idx out of range (of k_b_grid).");
}
}
else if (cfg.exists("k_b")) {
set_param_value<double>(cfg, "k_b");
}
else {
set("k_b", k_a);
std::cout << "Info: no k_b read, setting k_b = k_a." << std::endl;
}
double k_b = get<double>("k_b");
double k_c = 0;
double cos_ab = 0;
bool k_c_given = false;
bool cos_ab_given = false;
string k_c_grid_file;
Vec2D<double> k_c_grid;
if (cfg.lookupValue("k_c_grid", k_c_grid_file)) {
read_delimited_file(k_c_grid_file, k_c_grid);
}
/* If k_c_idx != -1, i.e. given as command line option, use this value */
if (!k_c_grid.empty() && (k_c_idx != -1 || cfg.lookupValue("k_c_idx", k_c_idx))) {
try {
k_c = k_c_grid.at(static_cast<size_t>(k_c_idx)).at(0);
}
catch (const std::out_of_range& e) {
throw ConfigException("k_c_idx out of range (of k_c_grid).");
}
k_c_given = true;
}
else if (cfg.exists("k_c")) {
set_param_value<double>(cfg, "k_c");
k_c = get<double>("k_c");
k_c_given = true;
}
if (cfg.exists("cos_ab")) {
set_param_value<double>(cfg, "cos_ab");
cos_ab = get<double>("cos_ab");
cos_ab_given = true;
if (cos_ab < -1 || cos_ab > 1) {
throw ConfigException(
"Got cos_ab = " + std::to_string(cos_ab) + ", which is not"
"between -1 and 1.");
}
}
/* If neither/both k_c and cos_ab was given, set k_c = k_a */
if ((!k_c_given) && (!cos_ab_given)) {
set("k_c", k_a);
k_c = k_a;
std::cout << "Info: no k_c read, setting k_c = k_a." << std::endl;
}
if (k_c_given && cos_ab_given) {
throw ConfigException(
"Both k_c and cos_ab given. Please choose one.");
}
/* k_c = - k_a - k_b */
if (k_c_given) {
cos_ab = 0.5 * ((k_c*k_c)/(k_a*k_b) - k_a/k_b - k_b/k_a);
set<double>("cos_ab", cos_ab);
if (cos_ab < -1 || cos_ab > 1) {
throw ConfigException(
"The k_a, k_b, k_c values given does not constitute a valid "
"configuration (cos_ab out of range).");
}
}
else {
/* cos_ab given */
k_c = std::sqrt(SQUARE(k_a) + SQUARE(k_b) + 2*k_a*k_b*cos_ab);
set<double>("k_c", k_c);
}
}
void Config::set_input_ps(const libconfig::Config& cfg)
{
/* Input power spectrum */
set_param_value<string>(cfg, "input_ps_file", true);
try {
/* input_ps_rescale setting. Rescaling factor set to 1 by default. */
/* First, check if given as string "2pi^-3" or "2pi^3" */
string r_str;
double r_double;
int r_int;
if (cfg.lookupValue("input_ps_rescale", r_str)) {
set<string>("input_ps_rescale_str", r_str);
if (r_str.compare("2pi^-3") == 0) {
set("input_ps_rescale_num", pow(TWOPI,-3));
}
else if (r_str.compare("2pi^3") == 0) {
set("input_ps_rescale_num", pow(TWOPI,3));
}
else {
throw ConfigException("Got input_ps_rescale string \"" + r_str +
"\" which is neither \"2pi^-3\" nor \"2pi^3\".");
}
}
/* If not found as string, try double */
else if (cfg.lookupValue("input_ps_rescale", r_double)) {
set("input_ps_rescale_num", r_double);
}
/* Last, try integer */
else if (cfg.lookupValue("input_ps_rescale", r_int)) {
set("input_ps_rescale_num", r_int);
}
}
catch (const libconfig::SettingTypeException& tex) {
throw ConfigException(
"Encountered type exception parsing input_ps_rescale setting.");
}
catch (const ConfigException& e) {
throw e;
}
}
bool Config::set_output_file(const libconfig::Config& cfg)
{
try {
std::string output_path;
// Case 1: output_file is explicitly set
if (set_param_value<std::string>(cfg, "output_file")) {
#if (__cplusplus >= 201703L)
fs::path file_path(get<std::string>("output_file"));
fs::path parent = file_path.parent_path();
if (!parent.empty() && !fs::exists(parent)) {
std::error_code ec;
if (!fs::create_directories(parent, ec)) {
throw ConfigException("Failed to create output directory \"" +
parent.string() + "\": " + ec.message());
}
}
// Test file writability
std::ofstream test(file_path);
if (!test) {
throw ConfigException("Cannot write to output file \"" + file_path.string() + "\".");
}
#endif
return true;
}
// Case 2: output_path is provided
if (!cfg.exists("output_path")) {
return false;
}
else {
cfg.lookupValue("output_path", output_path);
#if (__cplusplus >= 201703L)
fs::path dir_path(output_path);
if (!fs::exists(dir_path)) {
std::error_code ec;
if (!fs::create_directories(dir_path, ec)) {
throw ConfigException("Failed to create output directory \"" +
dir_path.string() + "\": " + ec.message());
}
}
if (!fs::is_directory(dir_path)) {
throw ConfigException("\"" + output_path + "\" is not a directory.");
}
#endif
set<std::string>("output_file",
create_filename_from_wavenumbers(output_path, ".dat"));
return true;
}
}
catch (const libconfig::SettingNotFoundException&) {
return false;
}
catch (const libconfig::SettingTypeException&) {
throw ConfigException("Encountered type exception parsing output_file setting.");
}
catch (const ConfigException& ex) {
throw ex;
}
}
void Config::set_dynamics(const libconfig::Config& cfg)
{
Dynamics dynamics;
string dynamics_str = get_param_value<string>(cfg, "dynamics", true);
/*Convert dynamics_str to lower case for comparison*/
std::transform(dynamics_str.begin(), dynamics_str.end(),
dynamics_str.begin(), [](unsigned char c) {return
tolower(c);});
if (dynamics_str == "eds-spt") {
dynamics = EDS_SPT;
}
else if (dynamics_str == "evolve-asymp-ics") {
if (get<bool>("rsd") && !get<bool>("biased_tracers")) {
throw ConfigException("Generic RSD calculation not implemented for "
"asymptotic IC dynamics.");
}
dynamics = EVOLVE_ASYMPTOTIC_ICS;
}
else if (dynamics_str == "evolve-eds-ics") {
if (get<bool>("rsd") && !get<bool>("biased_tracers")) {
throw ConfigException("Generic RSD calculation not implemented for "
"EdS IC dynamics.");
}
dynamics = EVOLVE_EDS_ICS;
}
else {
throw ConfigException("Unknown dynamics in configuration file.");
}
set("dynamics", dynamics);
/* Settings for evolution dynamics */
if (dynamics == EVOLVE_ASYMPTOTIC_ICS || dynamics == EVOLVE_EDS_ICS) {
/* ODE settings */
try {
if (cfg.exists("ode_settings")) {
const libconfig::Setting& ode_settings = cfg.lookup("ode_settings");
set<double>("ode_atol", ode_settings.lookup("abs_tolerance"));
set<double>("ode_rtol", ode_settings.lookup("rel_tolerance"));
set<double>("ode_hstart", ode_settings.lookup("start_step"));
}
}
catch (const libconfig::SettingTypeException& tex) {
throw ConfigException("Encountered type exception parsing ODE settings.");
}
set_param_value<size_t>(cfg, "time_steps", true);
set_param_value<double>(cfg, "eta_ini", true);
set_param_value<double>(cfg, "eta_fin", true);
/* Read files containing scale/time-dependent functions depending on
* cosmology. kappa is a constant, zeta(eta) depends only on time,
* xi(eta, k) depends also on scale */
try {
const libconfig::Setting& kappa_list = cfg.lookup("kappa_values");
for (int i = 0; i < kappa_list.getLength(); ++i) {
kappa_.push_back(kappa_list[i]);
}
const libconfig::Setting& zeta_files_list = cfg.lookup("zeta_files");
for (int i = 0; i < zeta_files_list.getLength(); ++i) {
zeta_files_.push_back(zeta_files_list[i].c_str());
}
const libconfig::Setting& xi_files_list = cfg.lookup("xi_files");
for (int i = 0; i < xi_files_list.getLength(); ++i) {
xi_files_.push_back(xi_files_list[i].c_str());
}
}
catch (const libconfig::SettingNotFoundException& nfex) {
throw ConfigException(
"Missing file for interpolation in configuration.");
}
catch (const libconfig::SettingTypeException& tex) {
throw ConfigException("Encountered type exception parsing interpolation files.");
}
/* Specific settings for asymptotic ics */
if (dynamics == EVOLVE_ASYMPTOTIC_ICS) {
/* Need additional information about time steps before eta_ini to set ICs */
set_param_value<size_t>(cfg, "pre_time_steps", true);
set_param_value<double>(cfg, "eta_asymp", true);
/* Default omega_k_min/omega_k_max to q_min/q_max */
set<double>("omega_k_min", get<double>("q_min"));
set<double>("omega_k_max", get<double>("q_max"));
/* Settings for computing Omega matrix eigenvalues */
if (cfg.exists("omega_eigenspace_settings")) {
try {
const libconfig::Setting& omega_eigenspace_settings =
cfg.lookup("omega_eigenspace_settings");
set_param_value<int>(omega_eigenspace_settings, "omega", "eigenmode");
set_param_value<double>(omega_eigenspace_settings, "omega", "k_min");
set_param_value<double>(omega_eigenspace_settings, "omega", "k_min");
set_param_value<int>(omega_eigenspace_settings, "omega", "N");
set_param_value<double>(omega_eigenspace_settings, "omega", "imag_threshold");
}
catch (const libconfig::SettingTypeException& tex) {
throw ConfigException("Encountered type exception parsing "
"omega_eigenspace_settings.");
}
}
}
}
}
void Config::set_cuba_config(const libconfig::Setting& cuba_settings)
{
set_param_value<double>(cuba_settings, "cuba", "abs_tolerance");
set_param_value<double>(cuba_settings, "cuba", "rel_tolerance");
set_param_value<int>(cuba_settings, "cuba", "verbosity_level");
set_param_value<bool>(cuba_settings, "cuba", "retain_statefile");
/* Try int */
try {
set<int>("cuba_max_evaluations", cuba_settings.lookup("max_evaluations"));
}
catch (const libconfig::SettingTypeException& tex) {
/* If not recognized, try double */
try {
double value = cuba_settings.lookup("max_evaluations");
if (value > std::numeric_limits<int>::max() || value < std::numeric_limits<int>::min()) {
throw std::overflow_error("Value is out of range for int");
}
set<int>("cuba_max_evaluations", static_cast<int>(std::round(value)));
}
catch (std::overflow_error& oe) {
throw ConfigException("Parsing cuba_settings: "
"max_evaluations: value is out of range for int");
}
catch (const libconfig::SettingTypeException& tex) {
throw ConfigException("Encountered type exception parsing "
"cuba_settings: max_evaluations setting.");
}
}
catch (const libconfig::SettingNotFoundException& nfex) {
std::cout << "No cuba max. evaluations given. Using default value: "
<< get<double>("cuba_maxeval") << std::endl;
}
set_param_value<int>(cuba_settings, "cuba", "n_cores");
}
void Config::set_cuba_statefile(const libconfig::Setting& cuba_settings)
{
try {
string statefile_path;
// Case 1: 'statefile' is explicitly provided
if (set_param_value<string>(cuba_settings, "cuba", "statefile")) {
#if (__cplusplus >= 201703L)
fs::path file_path(get<string>("statefile"));
fs::path parent = file_path.parent_path();
if (!parent.empty() && !fs::exists(parent)) {
std::error_code ec;
if (!fs::create_directories(parent, ec)) {
throw ConfigException("Failed to create CUBA statefile directory \"" +
parent.string() + "\": " + ec.message());
}
}
// Optional: check if the file can be written
std::ofstream test(file_path);
if (!test) {
throw ConfigException("Cannot write to CUBA statefile \"" +
file_path.string() + "\".");
}
#endif
return;
}
// Case 2: fallback to 'statefile_path'
if (cuba_settings.lookupValue("statefile_path", statefile_path)) {
#if (__cplusplus >= 201703L)
fs::path dir_path(statefile_path);
if (!fs::exists(dir_path)) {
std::error_code ec;
if (!fs::create_directories(dir_path, ec)) {
throw ConfigException("Failed to create CUBA statefile directory \"" +
dir_path.string() + "\": " + ec.message());
}
}
if (!fs::is_directory(dir_path)) {
throw ConfigException("CUBA statefile path \"" + statefile_path +
"\" is not a directory.");
}
#endif
set<string>("statefile",
create_filename_from_wavenumbers(statefile_path, ".state"));
return;
}
}
catch (const libconfig::SettingTypeException&) {
throw ConfigException("Encountered type exception for CUBA statefile setting.");
}
}
string Config::create_filename_from_wavenumbers(
const string& base_path,
const string& file_extension
)
{
std::stringstream ss;
ss << base_path;
ss << "/";
/* Add k_a_idx (& k_b_idx) to end of file */
if (k_a_idx != -1) {
ss << std::setfill('0') << std::setw(3) << k_a_idx;
}
else {
ss << std::scientific << std::setprecision(6) << get<double>("k_a");
}
if (get<Spectrum>("spectrum") == BISPECTRUM) {
ss << "_";
if (k_b_idx != -1) {
ss << std::setfill('0') << std::setw(3) << k_b_idx;
}
else {
ss << std::scientific << std::setprecision(6) << get<double>("k_b");
}
ss << "_";
if (k_c_idx != -1) {
ss << std::setfill('0') << std::setw(3) << k_c_idx;
}
else {
ss << std::scientific << std::setprecision(6) << get<double>("k_c");
}
}
ss << file_extension;
return ss.str();
}
Config::Config()
{
/* Set default values */
set("n_loops", 0);
set("dynamics", EDS_SPT);
set("spectrum", POWERSPECTRUM);
set("single_hard_limit", false);
set("sh_Q1", 10.0);
set("k_a", 0.0);
set("k_b", 0.0);
set("k_c", 0.0);
set("cos_ab", 0.0);
/* Integration limits */
set("q_min", 1e-4);
set("q_max", 1.0);
set("input_ps_file", string());
set("input_ps_rescale_num", 1.0);
set("input_ps_rescale_str", string());
set("output_path", string());
set("output_file", string());
set("rsd", false);
set("rsd_growth_f", 0.0);
set("biased_tracers", false);
bias_parameters_ = {1,0,0,0};
set("ir_resum", false);
/* Values from 1605.02149 */
set("k_s", 0.2);
set("k_osc", 1.0/110.0);
/* Which PT order are we working at? Relevant for avoiding overcounting
* of IR contributions */
set("pt_order", 0);
set("compute_eft_displacement_dispersion", false);
set("eft_displacement_dispersion", 0);
/* Upper limit for EFT displacement dispersion integral. Lower limit = cutoff */
set("eft_displacement_dispersion_infty_", 10);
set("cuba_abs_tolerance", 1e-12);
set("cuba_rel_tolerance", 1e-4);
set<int>("cuba_max_evaluations", 1e6);
set("cuba_verbosity_level", 0);
set("cuba_n_cores", 0);
set("cuba_retain_statefile", false);
set("cuba_statefile", string());
set("cuba_statefile_path", string());
set("description", string());
/* Write header with additional information in output */
set("write_header", true);
/* Print results only to stdout, not to file */
set("stdout_mode", false);
set("ode_abs_tolerance", 1e-6);
set("ode_rel_tolerance", 1e-4);
set("ode_start_step", 1e-3);
set("eta_ini", 0.0);
set("eta_fin", 0.0);
set<size_t>("time_steps", 0);
set<size_t>("pre_time_steps", 0);
set("eta_asymp", 0.0);
set("omega_eigenmode", 0);
set("omega_k_min", get<double>("q_min"));
set("omega_k_max", get<double>("q_max"));
set("omega_N", 100);
set("omega_imag_threshold", 1e-3);
}
Config::Config(const string& ini_file,
int k_a_idx,
int k_b_idx,
int k_c_idx
)
: Config()
{
this->k_a_idx = k_a_idx;
this->k_b_idx = k_b_idx;
this->k_c_idx = k_c_idx;
libconfig::Config cfg;
/* Read config file */
try {
cfg.readFile(ini_file.c_str());
}
catch (const libconfig::FileIOException& ioex) {
throw ConfigException("Unable to read \"" + ini_file + "\".");
}
catch (const libconfig::ParseException& pex) {
throw ConfigException("Parse error at " + string(pex.getFile()) +
":" + std::to_string(pex.getLine()) + " - " +
string(pex.getError()));
}
Vec1D<string> keys = keys_not_recognized(cfg);
if (!keys.empty()) {
string error_msg = "Keys in configuration file not recognized: ";
for (auto key : keys) {
error_msg += key + ", ";
}
throw ConfigException(error_msg);
}
/* Number of loops */
int n_loops = get_param_value<int>(cfg, "loops", true);
set("n_loops", n_loops);
/* First wavenumber */
string k_a_grid_file;
Vec2D<double> k_a_grid;
if (cfg.lookupValue("k_a_grid", k_a_grid_file)) {
read_delimited_file(k_a_grid_file, k_a_grid);
}
/* If k_a_idx != -1, i.e. given as command line option, use this value */
if (!k_a_grid.empty() && (k_a_idx != -1 || cfg.lookupValue("k_a_idx", k_a_idx))) {
try {
set("k_a", k_a_grid.at(static_cast<size_t>(k_a_idx)).at(0));
}
catch (const std::out_of_range& e) {
throw ConfigException("k_a_idx out of range (of k_a_grid).");
}
}
else if (cfg.exists("k_a")) {
set_param_value<double>(cfg, "k_a");
}
else {
throw ConfigException("Did not obtain any value for k_a. Either provide "
"k_a, or k_a_idx and k_a_grid.");
}
/* Integration ranges */
set_param_value<double>(cfg, "q_min", true);
set_param_value<double>(cfg, "q_max", true);
/* RSD. Read before spectrum settings, so that warning messages can be
* given if rsd AND correlations are set */
set_param_value<bool>(cfg, "rsd");
if(get<bool>("rsd")) {
if(!set_param_value<double>(cfg, "rsd_growth_f")) {
std::cout <<
"Info: No value for rsd_growth_f read, using default value = "
<< get<double>("rsd_growth_f") << "."<< std::endl;
}
}
/* Biased tracers */
set_param_value<bool>(cfg, "biased_tracers");
if(get<bool>("biased_tracers")) {
if (!get<bool>("rsd")) {
throw ConfigException("Biased tracers only implemented for rsd = true.");
}
if (get<int>("n_loops") > 1) {
throw ConfigException("Biased tracers only implemented for n_loops <= 1.");
}
try {
const libconfig::Setting& bias_params_setting = cfg.lookup("bias_parameters");
int count = bias_params_setting.getLength();
if (count != 4) {
throw ConfigException(
"There should be 4 bias parameters in the configuration");
}
for (int i = 0; i < count; ++i) {
switch (bias_params_setting[i].getType()) {
case libconfig::Setting::TypeInt:
case libconfig::Setting::TypeInt64:
{
int value = bias_params_setting[i];
bias_parameters_.at(static_cast<size_t>(i)) =
static_cast<double>(value);
break;
}
case libconfig::Setting::TypeFloat:
{
bias_parameters_.at(static_cast<size_t>(i)) =
static_cast<double>(bias_params_setting[i]);
break;
}
default:
throw ConfigException("Encountered type exception parsing bias_parameters.");
}
}
}
catch (const ConfigException& ce) {
throw ce;
}
catch (const libconfig::SettingNotFoundException& nfex) {
std::cout <<
"Info: No value for bias parameters read, using default values = ";
for (auto& el : bias_parameters_) {
std::cout << el << ",";
}
std::cout << std::endl;
}
catch (const libconfig::SettingTypeException& tex) {
throw ConfigException("Encountered type exception parsing bias_parameters.");
}
}
/* IR resummation */
set_param_value<bool>(cfg, "ir_resum");
if(get<bool>("ir_resum")) {
set_param_value<double>(cfg, "k_s");
set_param_value<double>(cfg, "k_osc");
set_param_value<int>(cfg, "pt_order", true);
}
/* Compute eft_displacement_dispersion? */
set_param_value<bool>(cfg, "compute_eft_displacement_dispersion");
if(get<bool>("compute_eft_displacement_dispersion")) {
if(!set_param_value<double>(cfg, "eft_displacement_dispersion_infty")) {
std::cout <<
"Info: No value for eft_displacement_dispersion_infty read, "