-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.f90
More file actions
115 lines (93 loc) · 3.76 KB
/
interface.f90
File metadata and controls
115 lines (93 loc) · 3.76 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
module camb_iface
use iso_c_binding
use model, only: CAMBparams
use results, only: CAMBdata
use camb, only: CAMB_ReadParams, CAMB_SetDefParams, CAMB_GetResults
use IniObjects, only: TIniFile
use InitialPower, only: TInitialPowerLaw
use reionization, only: TTanhReionization
! --- THE MISSING PIECE ---
use Recombination, only: TRecfast
implicit none
! We will store a "master copy" of the parameters after the first run
type(CAMBparams), save :: P_master_copy
logical, save :: is_first_run = .true.
contains
subroutine camb_from_params_(task, lmax_in, scale_in, cl_tt, cl_te, cl_ee, cl_bb) bind(C, name="camb_from_params_")
use iso_c_binding, only: c_double, c_int
use model, only: CT_Temp, CT_E, CT_Cross, CT_B
implicit none
real(c_double), intent(in) :: task(*)
integer(c_int), intent(in) :: lmax_in
real(c_double), intent(in) :: scale_in
real(c_double), intent(out) :: cl_tt(*), cl_te(*), cl_ee(*), cl_bb(*)
! --- All declarations must be at the top ---
type(CAMBparams) :: P
type(CAMBdata) :: results
integer :: l, lmax_scalar
real(c_double) :: ommh2, ombh2, h, tau, ns, logA, As_val
integer, parameter :: IDX_TT = CT_Temp, IDX_EE = CT_E, IDX_TE = CT_Cross , IDX_BB = CT_B
character(len=1024) :: ErrMsg
! Variables for the one-time file load
type(TIniFile) :: Ini_loader
character(len=256) :: settings_file
logical :: bad
! --- Executable code begins here ---
if (is_first_run) then
settings_file = 'camb_settings.ini'
print *, 'First run: Loading CAMB settings from: ', trim(settings_file)
call Ini_loader%Open(settings_file, bad, .false.)
if (bad) then
print *, 'FATAL ERROR: Could not open camb_settings.ini.'
stop 1
endif
ErrMsg = ''
if (.not. CAMB_ReadParams(P_master_copy, Ini_loader, ErrMsg)) then
print *, 'Error parsing CAMB settings: ', trim(ErrMsg)
stop 1
end if
call Ini_loader%Close()
is_first_run = .false.
print *, 'CAMB settings loaded and cached successfully.'
endif
! On EVERY run, start with the master copy
P = P_master_copy
! Load the 6 MCMC parameters from the C code
ommh2 = task(1)
ombh2 = task(2)
h = task(3)
tau = task(4)
ns = task(5)
logA = task(6)
As_val = exp(logA + 2.0_c_double * tau) * 1.0e-10_c_double
! Overwrite the MCMC parameters on the local P object
P%H0 = 100.0_c_double * h
P%ombh2 = ombh2
P%omch2 = ommh2 - ombh2
select type(RM => P%Reion)
class is (TTanhReionization)
RM%optical_depth = tau
end select
select type(InitPower => P%InitPower)
class is (TInitialPowerLaw)
InitPower%As = As_val
InitPower%ns = ns
end select
if (lmax_in > P%Max_l) P%Max_l = lmax_in
! Run the calculation with the updated parameters
call CAMB_GetResults(results, P)
lmax_scalar = min(results%CLData%lmax_lensed, lmax_in)
do l = 0, lmax_in
cl_tt(l+1) = 0.0_c_double
cl_te(l+1) = 0.0_c_double
cl_ee(l+1) = 0.0_c_double
cl_bb(l+1) = 0.0_c_double
end do
do l = 0, lmax_scalar
cl_tt(l+1) = results%CLData%CL_lensed(l, IDX_TT) * scale_in
cl_te(l+1) = results%CLData%CL_lensed(l, IDX_TE) * scale_in
cl_ee(l+1) = results%CLData%CL_lensed(l, IDX_EE) * scale_in
cl_bb(l+1) = results%CLData%CL_lensed(l, IDX_BB) * scale_in
end do
end subroutine camb_from_params_
end module camb_iface