Repository navigation
Expand file tree
/
Copy pathmodels.cpp
More file actions
4159 lines (3407 loc) · 110 KB
/
Copy pathmodels.cpp
File metadata and controls
4159 lines (3407 loc) · 110 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
/*
-------------------------------------------------------------------
Copyright (C) 2012-2022, Mohammad Al-Mamun, Mahmudul Hasan Anik,
and Andrew W. Steiner
This file is part of Bamr.
Bamr is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Bamr is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Bamr. If not, see <http://www.gnu.org/licenses/>.
-------------------------------------------------------------------
*/
#include "models.h"
#include "bamr_class.h"
#include "mcmc_bamr.h"
#include <o2scl/vector.h>
using namespace std;
using namespace o2scl;
using namespace o2scl_hdf;
using namespace o2scl_const;
using namespace bamr;
model::model(std::shared_ptr<const settings> s,
std::shared_ptr<const ns_data> n) {
set=s;
nsd=n;
cns.nb_start=0.01;
ts.verbose=0;
ts.set_units("1/fm^4","1/fm^4","1/fm^3");
ts.err_nonconv=false;
ts.set_eos(teos);
has_eos=true;
schwarz_km=o2scl_mks::schwarzchild_radius/1.0e3;
teos.verbose=0;
// Transition density parameters
nt_a.set_center(0.1327);
nt_a.set_sigma(0.0024);
nt_b.set_center(-0.0898);
nt_b.set_sigma(0.0055);
nt_c.set_center(0.0228);
nt_c.set_sigma(0.0028);
// If crust_from_L is true and the transition density is outside
// this range, then we reject and go to the next point
nt_low=0.02;
nt_high=0.14;
// -----------------------------------------------------------
// Prepare crust
if (set->use_crust) {
teos.default_low_dens_eos();
// Get the transition density from the crust
double pt, pw;
teos.get_transition(pt,pw);
// We set the transition density a bit lower (because by default
// it's the largest pressure in the crust EOS) and then add a
// small width. This ensures the pressure will be increasing and
// prefers the core EOS over the crust EOS near the transition.
teos.transition_mode=eos_tov_interp::smooth_trans;
teos.set_transition(pt/1.2,1.2);
} else {
teos.no_low_dens_eos();
}
}
void model::compute_star(const ubvector &pars, std::ofstream &scr_out,
int &ret, model_data &dat,
std::string model_type) {
ret=ix_success;
// Call read_table()
table_units<> &eost=dat.eos;
if (has_eos) {
// ---------------------------------------------------------------
// Compute the EOS
compute_eos(pars,ret,scr_out,dat);
if (ret!=ix_success) return;
// Sarah's section
if (set->mmax_deriv==true) {
eost.set_interp_type(o2scl::itp_linear);
eost.set_unit("ed","1/fm^4");
eost.set_unit("pr","1/fm^4");
eost.set_unit("nb","1/fm^3");
teos.read_table(eost,"ed","pr","nb");
// First TOV solve here
ts.mvsr();
// Check the maximum mass
dat.mvsr=*(ts.get_results());
double m_max=dat.mvsr.max("gm");
//cout << "m_max1: " << m_max << endl;
if (m_max<set->min_max_mass) {
scr_out << "Maximum mass too small: " << m_max << " < "
<< set->min_max_mass << "." << std::endl;
ret=ix_small_max;
return;
}
// Find the central energy density of the maximum mass star
size_t row=dat.mvsr.lookup("gm",m_max);
double c_ed=dat.mvsr.get("ed",row);
// Ensure that the last polytrope appears in the center
// of the maximum mass star
if (model_type==((string)"new_poly")) {
double trans2=pars[7];
if (trans2>c_ed) {
scr_out << "Polytrope beyond central density:" << std::endl;
scr_out << "trans2, c_ed = " << trans2 << ", "
<< c_ed << ", trans2>c_ed" << std::endl;
ret=ix_eos_pars_mismatch;
return;
}
}
// Check that the speed of sound is less than 1
eost.deriv("ed","pr","cs2");
for (size_t i=0;i<eost.get_nlines();i++) {
if (eost.get("ed",i)<c_ed) {
if (eost.get("cs2",i)>1.0) {
//cout << eost.get("ed",i) << " " << c_ed << endl;
scr_out << "Acausal EOS: cs2_" << i << "="
<< eost.get("cs2",i) << " > 1" << std::endl;
ret=ix_eos_acausal;
return;
}
}
}
// Now modify the last parameter: exp3
ubvector pars2=pars;
pars2[this->n_eos_params-1]*=1.001;
// Recompute the EOS
compute_eos(pars2,ret,scr_out,dat);
if (ret!=ix_success) return;
eost.set_unit("ed","1/fm^4");
eost.set_unit("pr","1/fm^4");
eost.set_unit("nb","1/fm^3");
// Call read_table()
teos.read_table(eost,"ed","pr","nb");
// Second TOV solve here
ts.mvsr();
// Check the maximum mass
dat.mvsr=*(ts.get_results());
double m_max2=dat.mvsr.max("gm");
//cout << "m_max2: " << m_max2 << endl;
if (m_max2<set->min_max_mass) {
scr_out << "Maximum mass too small: " << m_max2 << " < "
<< set->min_max_mass << "." << std::endl;
ret=ix_small_max;
return;
}
// Now, compute the derivative
double dpdM=(pars2[this->n_eos_params-1]-
pars[this->n_eos_params-1])/(m_max2-m_max);
//cout << "dpdM: " << dpdM << endl;
// Reject the point if the derivative is not finite
if (isfinite(dpdM)!=1) {
scr_out << "Derivative dpdM is infinite." << std::endl;
ret=ix_deriv_infinite;
return;
}
eost.add_constant("dpdM",dpdM);
// Compute the central energy density
row=dat.mvsr.lookup("gm",m_max);
c_ed=dat.mvsr.get("ed",row);
// Ensure that the last polytrope appears in the center
// of the maximum mass star
if (model_type==((string)"new_poly")) {
double trans2=pars[7];
if (trans2>c_ed) {
//cout << "trans2, c_ed (2): " << trans2 << " "
// << c_ed << endl;
scr_out << "Polytrope beyond central density:" << std::endl;
scr_out << "trans2, c_ed = " << trans2 << ", "
<< c_ed << ", trans2>c_ed" << std::endl;
ret=ix_eos_pars_mismatch;
return;
}
}
// Check that the speed of sound is less than 1
eost.deriv("ed","pr","cs2");
for (size_t i=0;i<eost.get_nlines();i++) {
if (eost.get("ed",i)<c_ed) {
/*
cout << i << " " << eost.get("ed",i) << " "
<< c_ed << " "
<< eost.get("cs2",i) << endl;
*/
if (eost.get("cs2",i)>1.0) {
scr_out << "Acausal EOS: cs2_" << i << "="
<< eost.get("cs2",i) << " > 1" << std::endl;
ret=ix_eos_acausal;
return;
}
}
}
// End of Sarah's section
}
// ---------------------------------------------------------------
// Check that pressure is increasing. If we're using a crust EOS,
// choose 0.6 as an arbitrary low-density cutoff which corresponds
// to about n_B=0.12 fm^{-3}, and check the low-density part below
// instead.
for(size_t i=0;(eost.get_nlines()>0 &&
i<eost.get_nlines()-1);i++) {
if ((!set->use_crust || eost.get("ed",i)>0.6) &&
eost.get("pr",i+1)<eost.get("pr",i)) {
scr_out << "Rejected: Pressure decreasing." << std::endl;
scr_out << "ed=" << eost.get("ed",i)
<< " pr=" << eost.get("pr",i) << std::endl;
scr_out << "ed=" << eost.get("ed",i+1)
<< " pr=" << eost.get("pr",i+1) << std::endl;
ret=ix_pressure_decrease;
return;
}
}
}
// ---------------------------------------------------------------
// If requested, compute the baryon density automatically
if (has_eos && set->baryon_density && !eost.is_column("nb")) {
O2SCL_ERR2("Setting baryon_density is true but EOS does not ",
"have a column \"nb\".",o2scl::exc_einval);
}
// ---------------------------------------------------------------
if (has_eos) {
// ---------------------------------------------------------------
// Compute crust transition density and set crust (if necessary)
if (set->compute_cthick && set->baryon_density) {
// If crust_from_L is true, compute the pressure at the number
// density specified by the correlation. Otherwise, just use 0.08
if (set->crust_from_L) {
if (set->verbose>=2) {
std::cout << "Starting crust from L." << std::endl;
}
double asamp, bsamp, csamp;
if (false) {
double f1=pars[0]*1.0e5-floor(pars[0]*1.0e5);
if (f1<=0.0) f1=1.0e-5;
else if (f1>=1.0) f1=1.0-1.0e-5;
asamp=nt_a.invert_cdf(f1);
double f2=pars[1]*1.0e5-floor(pars[1]*1.0e5);
f2=0.5;
if (f2<=0.0) f2=1.0e-5;
else if (f2>=1.0) f2=1.0-1.0e-5;
bsamp=nt_b.invert_cdf(f2);
double f3=pars[2]*1.0e5-floor(pars[2]*1.0e5);
if (f3<=0.0) f3=1.0e-5;
else if (f3>=1.0) f3=1.0-1.0e-5;
csamp=nt_c.invert_cdf(f3);
} else {
asamp=nt_a.mean();
bsamp=nt_b.mean();
csamp=nt_c.mean();
}
double St=eost.get_constant("S")*o2scl_const::hc_mev_fm;
double Lt=eost.get_constant("L")*o2scl_const::hc_mev_fm;
double nt=(asamp+bsamp*(Lt/70.0)+csamp*(Lt*Lt/4900.0))
*(St/30.0);
// If crust_from_L is true and the transition density is
// outside this range, then we reject and go to the next point
if (nt<nt_low || nt>nt_high) {
scr_out << "Transition density, " << nt
<< ", out of range." << endl;
ret=ix_trans_invalid;
return;
}
eost.add_constant("nt",nt);
double prt=eost.interp("nb",nt,"pr");
eost.add_constant("prt",prt);
// Add the transition pressure to the tov_solve object
if (ts.pr_list.size()>0) {
ts.pr_list.clear();
}
ts.pr_list.push_back(prt);
// Set the crust and it's transition pressure
if (eost.get_constant("S")*hc_mev_fm<28.0 ||
eost.get_constant("S")*hc_mev_fm>38.0 ||
eost.get_constant("L")*hc_mev_fm<25.0 ||
eost.get_constant("L")*hc_mev_fm>115.0 ||
eost.get_constant("L")*hc_mev_fm>
eost.get_constant("S")*hc_mev_fm*5.0-65.0) {
scr_out << "S or L out of range" << endl;
ret=ix_SL_invalid;
return;
}
// This function expects its argument in MeV
#ifdef O2SCL_OPENMP
#pragma omp critical (bamr_ngl13_eos)
#endif
{
teos.ngl13_low_dens_eos2
(eost.get_constant("S")*hc_mev_fm,
eost.get_constant("L")*hc_mev_fm,nt,"");
}
// Set the transition pressure and width. Note that
// the ngl13 EOS is in units of Msun/km^3, so we
// convert prt to those units
teos.transition_mode=eos_tov_interp::match_line;
teos.set_transition(o2scl_settings.get_convert_units().convert_const
("1/fm^4","Msun/km^3",prt),1.4);
if (set->verbose>=2) {
std::cout << "Done with crust from L." << std::endl;
}
} else {
// Otherwise, if we're not determining the crust from L, then
// compute the crust thickness based on a density of 0.08
// fm^{-3}
double nt=0.08;
eost.add_constant("nt",nt);
double prt=eost.interp("nb",0.08,"pr");
eost.add_constant("prt",prt);
if (ts.pr_list.size()>0) {
ts.pr_list.clear();
}
ts.pr_list.push_back(prt);
}
} else {
if (ts.pr_list.size()>0) {
ts.pr_list.clear();
}
}
// ---------------------------------------------------------------
// Read the EOS into the tov_eos object
if (set->baryon_density && set->inc_baryon_mass) {
eost.set_unit("ed","1/fm^4");
eost.set_unit("pr","1/fm^4");
eost.set_unit("nb","1/fm^3");
teos.read_table(eost,"ed","pr","nb");
} else {
eost.set_unit("ed","1/fm^4");
eost.set_unit("pr","1/fm^4");
teos.read_table(eost,"ed","pr");
}
// ---------------------------------------------------------------
// We checked that the pressure of the core EOS was increasing
// earlier. Here we also double check that the EOS is increasing
// near the crust-core transition.
if (set->use_crust) {
double ed_last=0.0;
// This range corresponds to between about n_B=0.01 and 0.17
// fm^{-3}
for(double pr=1.0e-4;pr<2.0e-2;pr*=1.1) {
double ed, nb;
teos.ed_nb_from_pr(pr,ed,nb);
if (ed_last>1.0e-20 && ed<ed_last) {
scr_out << "Stability problem near crust-core transition."
<< std::endl;
if (has_esym) {
scr_out << "S=" << eost.get_constant("S")*hc_mev_fm
<< " L=" << eost.get_constant("L")*hc_mev_fm
<< std::endl;
}
scr_out << "Energy decreased with increasing pressure "
<< "at pr=" << pr << std::endl;
scr_out << std::endl;
scr_out << "Full EOS near transition: " << std::endl;
scr_out << "pr ed" << std::endl;
for(pr=1.0e-4;pr<2.0e-2;pr*=1.1) {
teos.ed_nb_from_pr(pr,ed,nb);
scr_out << pr << " " << ed << std::endl;
}
scr_out << std::endl;
ret=ix_crust_unstable;
return;
}
ed_last=ed;
}
// End of 'if (set->use_crust)'
}
// ---------------------------------------------------------------
// If necessary, output debug information (We want to make sure
// this is after tov_eos::read_table() so that we can debug the
// core-crust transition)
if (set->debug_eos) {
o2scl_hdf::hdf_file hfde;
hfde.open_or_create("debug_eos.o2");
// Output the core EOS
hdf_output(hfde,eost,"eos");
// Output core and crust EOS as reported by the
// tov_interp_eos object
o2scl::table_units<> full_eos;
full_eos.line_of_names("ed pr nb");
full_eos.set_unit("ed","1/fm^4");
full_eos.set_unit("pr","1/fm^4");
full_eos.set_unit("nb","1/fm^3");
for(double pr=1.0e-20;pr<10.0;pr*=1.05) {
double ed, nb;
teos.get_eden_user(pr,ed,nb);
double line[3]={ed,pr,nb};
full_eos.line_of_data(3,line);
// Choose a slightly more sparse grid at lower pressures
if (pr<1.0e-4) pr*=1.2;
}
hdf_output(hfde,full_eos,"full_eos");
hfde.close();
// Exit only if debug_star is not also true
if (!set->debug_star) {
std::cout << "Automatically exiting since 'debug_eos' is true."
<< std::endl;
#ifdef BAMR_MPI
// Ensure all the debug information is output before
// we call exit();
MPI_Barrier(MPI_COMM_WORLD);
// Finalize MPI
MPI_Finalize();
#endif
exit(0);
}
}
// ---------------------------------------------------------------
// Solve for M vs. R curve
double m_max=0.0;
ts.princ=set->mvsr_pr_inc;
// Clear old M-R table (new for MCMC para)
if (set->addl_quants) {
ts.ang_vel=true;
ts.calc_gpot=true;
} else {
ts.ang_vel=false;
ts.calc_gpot=false;
}
int info=ts.mvsr();
dat.mvsr=*(ts.get_results());
if (set->verbose>=2) {
scr_out << "Done with TOV." << endl;
}
if (info!=0) {
scr_out << "M vs. R failed: info=" << info << std::endl;
ret=ix_mvsr_failed;
return;
}
dat.mvsr.set_interp_type(o2scl::itp_linear);
// ---------------------------------------------------------------
// Add baryon density to M vs. R table if it's not already there
if (set->baryon_density && !set->inc_baryon_mass) {
dat.mvsr.add_col_from_table(eost,"pr","nb","pr");
dat.mvsr.set_unit("nb","1/fm^3");
}
// ---------------------------------------------------------------
// Check that maximum mass is large enough. Note that the
// mass-radius curve is not differentiable near M_{max}, so the
// best way to increase the accuracy here is to make
// set->mvsr_pr_inc smaller.
m_max=dat.mvsr.max("gm");
dat.mvsr.add_constant("M_max",m_max);
if (m_max<set->min_max_mass) {
scr_out << "Maximum mass too small: " << m_max << " < "
<< set->min_max_mass << "." << std::endl;
ret=ix_small_max;
return;
}
// ---------------------------------------------------------------
// If the EOS is sufficiently stiff, the TOV solver will output
// gibberish, i.e. masses and radii equal to zero, especially at
// the higher pressures. This rules these EOSs out, as they would
// likely be acausal anyway. If this happens frequently, it might
// signal a problem.
size_t ir=dat.mvsr.get_nlines()-1;
if ((dat.mvsr)["gm"][ir]<1.0e-10 ||
(dat.mvsr)["gm"][ir-1]<1.0e-10) {
scr_out << "TOV failure fix." << std::endl;
ret=ix_tov_failure;
return;
}
// ---------------------------------------------------------------
// Check the radius of the maximum mass star
size_t ix_max=dat.mvsr.lookup("gm",m_max);
double r_max=dat.mvsr.get("r",ix_max);
dat.mvsr.add_constant("R_max",r_max);
if (r_max>1.0e4) {
scr_out << "TOV convergence problem: " << std::endl;
ret=ix_tov_conv;
return;
}
// ---------------------------------------------------------------
// Compute the central baryon density in the maximum mass star
if (set->baryon_density) {
double nb_max=dat.mvsr.get("nb",ix_max);
dat.mvsr.add_constant("nb_max",nb_max);
}
// Remove table entries with pressures above the maximum pressure
dat.mvsr.set_nlines(ix_max+1);
// Check for multiple branches in M vs. R
int max_count=0;
if (dat.mvsr.get("gm",0) > dat.mvsr.get("gm",1)) {
max_count++;
}
if (dat.mvsr.get("gm",dat.mvsr.get_nlines()-1) >
dat.mvsr.get("gm",dat.mvsr.get_nlines()-2)) {
max_count++;
}
for(size_t i=1;i<dat.mvsr.get_nlines()-1;i++) {
if (dat.mvsr.get("gm",i)>dat.mvsr.get("gm",i-1) &&
dat.mvsr.get("gm",i)>dat.mvsr.get("gm",i+1)) {
max_count++;
}
}
if (max_count>1) {
scr_out << "Multiple peaks in M vs. R" << endl;
cout << "Multiple peaks in M vs. R" << endl;
for(size_t i=0;i<dat.mvsr.get_nlines();i++) {
cout << dat.mvsr.get("gm",i) << " "
<< dat.mvsr.get("r",i) << endl;
}
}
// Make sure that the M vs. R curve generated enough data. This
// is not typically an issue.
if (dat.mvsr.get_nlines()<10) {
scr_out << "M vs. R failed to generate lines." << std::endl;
ret=ix_mvsr_table;
return;
}
if (nsd->n_sources>0) {
if (dat.sourcet.get_ncolumns()==0) {
dat.sourcet.line_of_names("R M wgt atm ce");
if (set->baryon_density) {
dat.sourcet.new_column("cnb");
}
dat.sourcet.set_nlines(nsd->n_sources);
}
}
// Compute the masses and radii for each source
for(size_t i=0;i<nsd->n_sources;i++) {
if (set->inc_ligo) {
dat.sourcet.set("M",i,m_max*pars[this->n_eos_params+i+3]);
dat.sourcet.set("R",i,
dat.mvsr.interp("gm",dat.sourcet.get("M",i),"r"));
} else {
dat.sourcet.set("M",i,m_max*pars[this->n_eos_params+i+3]);
dat.sourcet.set("R",i,
dat.mvsr.interp("gm",dat.sourcet.get("M",i),"r"));
}
}
// End of loop 'if (has_eos)'
} else {
// Compute mass-radius curve directly
compute_mr(pars,ret,scr_out,dat);
if (ret!=ix_success) {
return;
}
// Compute the masses and radii for each source
for(size_t i=0;i<nsd->n_sources;i++) {
dat.sourcet.set("M",i,pars[this->n_eos_params+i]);
dat.sourcet.set("R",i,
dat.mvsr.interp("gm",dat.sourcet.get("M",i),"r"));
}
}
int mpi_rank=0;
#ifdef BAMR_MPI
MPI_Comm_rank(MPI_COMM_WORLD,&mpi_rank);
#endif
// Output M vs. R curve
if (set->debug_star) {
if (mpi_rank==0) {
o2scl_hdf::hdf_file hfds;
hfds.open_or_create("debug_star.o2");
hdf_output(hfds,dat.mvsr,"mvsr");
hfds.close();
scr_out << "Automatically exiting since 'debug_star' is true."
<< std::endl;
}
#ifdef BAMR_MPI
// Ensure all the debug information is output before
// we call exit();
MPI_Barrier(MPI_COMM_WORLD);
// Finalize MPI
MPI_Finalize();
#endif
exit(0);
}
// -----------------------------------------------------------------
// Check causality. Note that we have to do this after the rows for
// the unstable branch have been removed from the mass-radius table.
if (has_eos) {
// Compute speed of sound squared
double ed_max=dat.mvsr.max("ed");
for(size_t i=0;i<eost.get_nlines();i++) {
if (eost.get("ed",i)>ed_max &&
!std::isfinite(eost.get("pr",i))) {
eost.set("pr",i,0.0);
}
}
eost.deriv("ed","pr","cs2");
for(size_t i=0;i<eost.get_nlines();i++) {
if (eost.get("ed",i)<ed_max &&
(!std::isfinite(eost.get("cs2",i)) ||
eost.get("cs2",i)<0.0)) {
cout << "cs2 not finite." << endl;
cout << "ed_max: " << ed_max << endl;
if (i>0) {
cout << i-1 << " " << eost.get("ed",i-1) << " "
<< eost.get("pr",i-1) << endl;
}
cout << i << " " << eost.get("ed",i) << " "
<< eost.get("pr",i) << endl;
if (i<eost.get_nlines()-1) {
cout << i+1 << " " << eost.get("ed",i+1) << " "
<< eost.get("pr",i+1) << endl;
}
for(size_t j=0;j<eost.get_nlines();j++) {
cout << j << " " << eost.get("ed",j) << " "
<< eost.get("pr",j) << endl;
}
exit(-1);
}
}
for(size_t i=0;i<eost.get_nlines();i++) {
if (eost.get("ed",i)<ed_max && eost["cs2"][i]>1.0) {
scr_out.precision(4);
scr_out << "Rejected: Acausal."<< std::endl;
scr_out << "ed_max=" << ed_max
<< " ed_bad=" << (eost)["ed"][i]
<< " pr_bad=" << (eost)["pr"][i] << std::endl;
scr_out.precision(6);
ret=ix_eos_acausal;
return;
}
}
} else {
for(size_t i=0;i<nsd->n_sources;i++) {
if (dat.sourcet.get("R",i) <
2.94*schwarz_km/2.0*dat.sourcet.get("M",i)) {
scr_out << "Source " << nsd->source_names[i] << " acausal."
<< std::endl;
ret=ix_source_acausal;
return;
}
}
}
// ---------------------------------------------------------------
// Compute M, R for fixed central baryon densities
if (has_eos && set->baryon_density) {
double ed1=eost.interp("nb",0.16,"ed");
dat.mvsr.add_constant("gm_nb1",dat.mvsr.interp("ed",ed1,"gm"));
dat.mvsr.add_constant("r_nb1",dat.mvsr.interp("ed",ed1,"r"));
double ed2=eost.interp("nb",0.32,"ed");
dat.mvsr.add_constant("gm_nb2",dat.mvsr.interp("ed",ed2,"gm"));
dat.mvsr.add_constant("r_nb2",dat.mvsr.interp("ed",ed2,"r"));
double ed3=eost.interp("nb",0.48,"ed");
dat.mvsr.add_constant("gm_nb3",dat.mvsr.interp("ed",ed3,"gm"));
dat.mvsr.add_constant("r_nb3",dat.mvsr.interp("ed",ed3,"r"));
double ed4=eost.interp("nb",0.64,"ed");
dat.mvsr.add_constant("gm_nb4",dat.mvsr.interp("ed",ed4,"gm"));
dat.mvsr.add_constant("r_nb4",dat.mvsr.interp("ed",ed4,"r"));
double ed5=eost.interp("nb",0.80,"ed");
dat.mvsr.add_constant("gm_nb5",dat.mvsr.interp("ed",ed5,"gm"));
dat.mvsr.add_constant("r_nb5",dat.mvsr.interp("ed",ed5,"r"));
}
if (set->verbose>=2) {
cout << "End model::compute_star()." << endl;
}
return;
}
void two_polytropes::setup_params(o2scl::cli &cl) {
p_kin_sym.d=&se.a;
p_kin_sym.help="Kinetic part of symmetry energy.";
cl.par_list.insert(make_pair("kin_sym",&p_kin_sym));
return;
}
void two_polytropes::copy_params(model &m) {
// Dynamic casts to references throw exceptions when they fail
two_polytropes &tp=dynamic_cast<two_polytropes &>(m);
se.a=tp.se.a;
return;
}
void two_polytropes::remove_params(o2scl::cli &cl) {
size_t i=cl.par_list.erase("kin_sym");
if (i!=1) {
O2SCL_ERR("Failed to erase parameter 'kin_sym'.",o2scl::exc_esanity);
}
return;
}
two_polytropes::two_polytropes(std::shared_ptr<const settings> s,
std::shared_ptr<const ns_data> n) :
model(s,n) {
se.kpp=0.0;
se.n0=0.16;
se.eoa=-16.0/hc_mev_fm;
se.a=17.0/hc_mev_fm;
// We include muons by default, but they rarely appear at low-density
cns.include_muons=true;
this->n_eos_params=8;
this->has_esym=true;
}
void two_polytropes::get_param_info(std::vector<std::string> &names,
std::vector<std::string> &units,
std::vector<double> &low,
std::vector<double> &high) {
names={"comp","kprime","esym","gamma","trans1","index1",
"trans2","index2"};
units={"1/fm","1/fm","1/fm","","1/fm^4","","1/fm^4",""};
low.resize(n_eos_params);
low[0]=180.0/hc_mev_fm;
low[1]=-1000.0/hc_mev_fm;
low[2]=28.0/hc_mev_fm;
low[3]=0.0;
// The value 0.75 fm^{-4} is about the energy density of nuclear
// matter
low[4]=0.75;
low[5]=0.2;
low[6]=0.75;
low[7]=0.2;
high.resize(n_eos_params);
high[0]=300.0/hc_mev_fm;
// FSU gold is -280 MeV or so
high[1]=-200.0/hc_mev_fm;
high[2]=38.0/hc_mev_fm;
high[3]=1.2;
// The value of high.trans1 has to be small enough because we
// don't want to evaluate the schematic EOS to too high of a
// density.
high[4]=3.0;
high[5]=1.5;
high[6]=8.0;
high[7]=2.0;
// Go to the parent which takes care of the data-related
// parameters
model::get_param_info(names,units,low,high);
return;
}
void two_polytropes::initial_point(std::vector<double> ¶ms) {
params.resize(8);
params[0]=1.0;
params[1]=-3.0;
params[2]=0.165;
params[3]=0.644;
params[4]=1.51;
params[5]=0.576;
params[6]=4.60;
params[7]=1.21;
model::initial_point(params);
return;
}
void two_polytropes::compute_eos(const ubvector ¶ms, int &ret,
ofstream &scr_out, model_data &dat) {
ret=ix_success;
if (params[4]>params[6]) {
scr_out << "Rejected: Transition densities misordered." << endl;
ret=ix_eos_pars_mismatch;
return;
}
// Set low-density EOS parameters
se.comp=params[0];
se.kprime=params[1];
se.b=params[2]-se.a;
se.gamma=params[3];
se.kpp=0.0;
// Compute low-density eos
cns.nb_end=0.6;
cns.set_eos(se);
cns.calc_eos();
dat.eos=*(cns.get_eos_results());
dat.eos.set_interp_type(itp_linear);
dat.eos.add_constant("S",params[2]);
dat.eos.add_constant("L",se.fesym_slope(0.16));
// Transition densities
double ed1=params[4];
double ed2=params[6];
// Boundary baryon density and pressure by interpolating
// the table
double nb1=dat.eos.interp("ed",ed1,"nb");
double pr1=dat.eos.interp("ed",ed1,"pr");
// Determine 1st polytrope coefficient
double coeff1=pr1/pow(ed1,1.0+1.0/params[5]);
if (coeff1<0.0 || pr1<0.0) {
scr_out << "Rejected: Negative polytrope coefficient or "
<< "matching pressure (1)." << endl;
ret=ix_pressure_negative;
return;
}
// Double check that there is no gap in density between
// the low-density EOS and the first polytrope
double ed_last=dat.eos.max("ed");
if (ed_last<ed1) {
scr_out << "params: ";
vector_out(scr_out,params,true);
scr_out << ed_last << " " << ed1 << endl;
scr_out << "Gap between low-density EOS and polytrope " << endl;
for(size_t j=0;j<dat.eos.get_nlines();j++) {
scr_out << j << " " << dat.eos.get("nb",j) << " ";
scr_out << dat.eos.get("ed",j) << " ";
scr_out << dat.eos.get("pr",j) << std::endl;
}
O2SCL_ERR("Gap between low-density EOS and polytrope.",exc_efailed);
}
// Remove rows beyond 1st transition
for(size_t i=0;i<dat.eos.get_nlines();i++) {
if ((dat.eos)["ed"][i]>ed1) {
dat.eos.delete_row(i);
i=0;
}
}
// Check that low-density EOS has statistics
if (dat.eos.get_nlines()<3) {
scr_out << "Rejected: Polytrope fit failed (1)." << endl;
ret=ix_no_eos_table;
return;
}
// Add first polytrope to table. The shift of 0.001 is
// important to ensure that we don't have two points
// at the same energy density
for(double ed=ed1;ed<ed2-0.001;ed+=0.05) {
double pr=coeff1*pow(ed,1.0+1.0/params[5]);
double nb=nb1*pow(ed/ed1,1.0+params[5])/
pow((ed+pr)/(ed1+pr1),params[5]);
double line[3]={ed,pr,nb};
dat.eos.line_of_data(3,line);
}
// Check that matching didn't fail
if (dat.eos.get_nlines()<3) {
scr_out << "Rejected: Polytrope fit failed (2)." << endl;
ret=ix_no_eos_table;
return;
}
// Boundary baryon density and pressure
double pr2=coeff1*pow(ed2,1.0+1.0/params[5]);
double nb2=nb1*pow(ed2/ed1,1.0+params[5])/
pow((ed2+pr2)/(ed1+pr1),params[5]);
// Determine 2nd polytrope coefficient
double coeff2=pr2/pow(ed2,1.0+1.0/params[7]);
if (coeff2<0.0 || pr2<0.0) {
scr_out << "Rejected: Negative polytrope coefficient or "
<< "matching pressure (2)." << endl;
ret=ix_pressure_negative;