33 #ifdef COSMICFISH_EFTCAMB
53 Type(cambparams) ,
intent(in) :: P
55 real(dl),
dimension(:),
intent(out) :: sn_magnitude
56 real(dl),
dimension(:),
intent(in) :: redshift
57 real(dl),
dimension(:),
intent(in) :: color
58 real(dl),
dimension(:),
intent(in) :: stretch
59 integer ,
intent(in) :: num
62 integer,
intent(out) :: err
71 real(dl) :: alpha, beta, zero_mag
77 call cambparams_set(p)
79 #ifdef COSMICFISH_EFTCAMB
81 if (p%EFTflag /= 0)
then
82 call eftcamb_initialization(eftsuccess)
83 if (.not. eftsuccess)
then
84 write(*,*)
'EFTCAMB: unstable model'
91 alpha = fp%fisher_SN%alpha_SN
92 beta = fp%fisher_SN%alpha_SN
93 zero_mag = fp%fisher_SN%M0_SN
97 sn_magnitude(i) = 5._dl*log10( luminositydistance(redshift(i)) ) &
98 & -alpha*stretch(i) +beta*color(i) + zero_mag -25._dl
112 Type(cambparams) ,
intent(in) :: P
114 real(dl),
dimension(:,:),
intent(out) :: sn_mock
127 do j=1, fp%fisher_SN%number_SN_windows
128 do k=1, fp%fisher_SN%SN_number(j)
129 sn_mock(1,i) =
random_uniform( fp%fisher_SN%SN_redshift_start(j), fp%fisher_SN%SN_redshift_end(j))
131 sn_mock(3,i) =
random_gaussian( 0._dl, fp%fisher_SN%stretch_dispersion )
143 use constants
, only : c
147 Type(cambparams) ,
intent(in) :: P
149 real(dl),
dimension(:,:),
intent(in) :: sn_mock
152 real(dl),
dimension(:) ,
intent(out) :: sn_mock_covariance
159 do i=1, fp%fisher_SN%total_SN_number
160 sn_mock_covariance(i) = (fp%fisher_SN%magnitude_sigma)**2 &
161 & +(fp%fisher_SN%sigma_lens_0*sn_mock(1,i))**2 &
162 & +(5._dl/sn_mock(1,i)/log(10._dl)*fp%fisher_SN%c_sigmaz/c*1000._dl)**2
166 do i=1, fp%fisher_SN%total_SN_number
167 sn_mock_covariance(i) = sn_mock_covariance(i) &
168 & + fp%fisher_SN%beta_SN**2*(fp%fisher_SN%dcolor_offset+fp%fisher_SN%dcolor_zcorr*sn_mock(1,i)**2)**2 &
169 & + fp%fisher_SN%alpha_SN**2*(fp%fisher_SN%dshape_offset+fp%fisher_SN%dshape_zcorr*sn_mock(1,i)**2)**2 &
170 & + 2._dl*fp%fisher_SN%alpha_SN*(fp%fisher_SN%cov_ms_offset+fp%fisher_SN%cov_ms_zcorr*sn_mock(1,i)**2) &
171 & - 2._dl*fp%fisher_SN%beta_SN*(fp%fisher_SN%cov_mc_offset+fp%fisher_SN%cov_mc_zcorr*sn_mock(1,i)**2) &
172 & - 2._dl*fp%fisher_SN%alpha_SN*fp%fisher_SN%beta_SN*(fp%fisher_SN%cov_sc_offset+fp%fisher_SN%cov_sc_zcorr*sn_mock(1,i)**2)
179 subroutine save_sn_mock_to_file( mock_size, sn_mock_data, sn_mock_covariance, filename, magnitude )
183 integer ,
intent(in) :: mock_size
184 real(dl),
dimension(:,:),
intent(in) :: sn_mock_data
185 real(dl),
dimension(:) ,
intent(in) :: sn_mock_covariance
186 character(len=*) ,
intent(in) :: filename
188 real(dl),
dimension(:) ,
intent(in),
optional :: magnitude
195 open(unit=666, file=filename, action=
"write", status=
"replace")
197 if (
present(magnitude) )
then
199 write(666,
'(a)')
"# number of the SN, redshift, magnitude, color, stretch, covariance "
201 write(666,
'(I10,5E15.5)') i, sn_mock_data(1,i), magnitude(i), sn_mock_data(2,i), sn_mock_data(3,i), sn_mock_covariance(i)
206 write(666,
'(a)')
"# number of the SN, redshift, color, stretch, covariance "
208 write(666,
'(I10,4E15.5)') i, sn_mock_data(1,i), sn_mock_data(2,i), sn_mock_data(3,i), sn_mock_covariance(i)
222 & mag_initial, mag_derivative, mag_der_error, &
230 Type(cambparams) ,
intent(in) :: P
233 integer ,
intent(in) :: param_num
234 integer ,
intent(in) :: num_param
236 real(dl),
dimension(:),
intent(in) :: mag_initial
237 real(dl),
dimension(:),
intent(out) :: mag_derivative
238 real(dl),
dimension(:),
intent(out) :: mag_der_error
240 real(dl),
dimension(:,:),
intent(in) :: sn_mock
242 real(dl),
intent(in) :: initial_step
243 integer ,
intent(out) :: error_code
250 real(dl),
dimension(:),
allocatable :: sn_temp_1, sn_temp_2, errt
251 integer ,
dimension(:),
allocatable :: accept_mask
252 real(dl),
dimension(:,:,:),
allocatable :: a
253 real(dl),
dimension(num_param) :: param_vector, fiducial_param_vector, temp_param_vector
255 real(dl),
parameter :: con = 1.4_dl
256 real(dl),
parameter :: big = 1.d+30
257 real(dl),
parameter :: safe = 2._dl
258 integer ,
parameter :: ntab = 30
259 real(dl) :: con2 = con*con
260 integer :: i, j, k, l, m, side, err, AllocateStatus
262 real(dl) :: temp1, temp2, temp3
263 Type(cambparams) :: P_temp
266 real(dl) :: acceptance
267 integer :: mock_sn_size
268 real(dl),
dimension(:),
allocatable :: redshift, color, stretch
272 mag_derivative = 0._dl
273 mag_der_error = 0._dl
276 if ( initial_step == 0._dl )
then
277 stop
'Observed_SN_Magnitude_derivative initial stepsize is zero'
280 mock_sn_size = fp%fisher_SN%total_SN_number
283 if ( fp%adaptivity )
then
284 allocate( sn_temp_1(mock_sn_size), &
285 & sn_temp_2(mock_sn_size), &
286 & errt(mock_sn_size), &
287 & accept_mask(mock_sn_size),&
288 & stat = allocatestatus )
289 if (allocatestatus /= 0) stop
"Observed_SN_Magnitude_derivative: Allocation failed. Not enough memory"
290 allocate( a(ntab, ntab, mock_sn_size) , stat = allocatestatus )
291 if (allocatestatus /= 0) stop
"Observed_SN_Magnitude_derivative: Allocation failed. Not enough memory for the derivative array"
293 allocate( sn_temp_1(mock_sn_size), &
294 & sn_temp_2(mock_sn_size), &
295 & stat = allocatestatus )
296 if (allocatestatus /= 0) stop
"Observed_SN_Magnitude_derivative: Allocation failed. Not enough memory"
297 allocate( a(1,1, mock_sn_size) , stat = allocatestatus )
298 if (allocatestatus /= 0) stop
"Observed_SN_Magnitude_derivative: Allocation failed. Not enough memory for the derivative array"
301 allocate( redshift(mock_sn_size), &
302 & color(mock_sn_size), &
303 & stretch(mock_sn_size), &
304 & stat = allocatestatus )
307 redshift(:) = sn_mock(1,:)
308 color(:) = sn_mock(2,:)
309 stretch(:) = sn_mock(3,:)
317 fiducial_param_vector = param_vector
318 temp_param_vector = param_vector
324 if ( fp%adaptivity ) accept_mask = 0
327 param_vector(param_num) = fiducial_param_vector(param_num) + hh
329 call observed_sn_magnitude( p_temp, fp_temp, sn_temp_1, redshift, color, stretch, mock_sn_size, err )
331 if ( err /= 0 ) side = 1
333 temp_param_vector(param_num) = fiducial_param_vector(param_num) - hh
335 call observed_sn_magnitude( p_temp, fp_temp, sn_temp_2, redshift, color, stretch, mock_sn_size, err )
339 if ( side == 1 )
then
341 mag_der_error = 0.0_dl
342 mag_derivative = 0.0_dl
344 else if ( side == 0 )
then
351 a(1,1,:) = (sn_temp_1(:) - sn_temp_2(:))/(2.0_dl*hh)
352 else if (side == 1)
then
353 a(1,1,:) = (sn_temp_2(:) - mag_initial(:))/(-hh)
354 else if (side == 2)
then
355 a(1,1,:) = (sn_temp_1(:) - mag_initial(:))/(hh)
359 if ( .not. fp%adaptivity )
then
360 mag_derivative(:) = a(1,1,:)
361 mag_der_error = 0.0_dl
362 deallocate( sn_temp_1, sn_temp_2, a )
370 if ( fp%cosmicfish_feedback >= 2 )
then
371 accepted = sum(accept_mask)
372 acceptance =
REAL(accepted)/
REAL(mock_sn_size)
373 print*,
'iteration:', i
374 print*,
' stepsize:', hh
375 print*,
' accepted elements', accepted,
'accepted ratio', acceptance
383 param_vector(param_num) = fiducial_param_vector(param_num) + hh
384 temp_param_vector(param_num) = fiducial_param_vector(param_num) - hh
387 call observed_sn_magnitude( p_temp, fp_temp, sn_temp_1, redshift, color, stretch, mock_sn_size, err )
390 mag_der_error = 0.0_dl
391 mag_derivative = 0.0_dl
396 call observed_sn_magnitude( p_temp, fp_temp, sn_temp_2, redshift, color, stretch, mock_sn_size, err )
399 mag_der_error = 0.0_dl
400 mag_derivative = 0.0_dl
404 a(1,i,:) = (sn_temp_1(:) - sn_temp_2(:))/(2.0_dl*hh)
405 else if (side==1)
then
406 param_vector(param_num) = fiducial_param_vector(param_num) - hh
408 call observed_sn_magnitude( p_temp, fp_temp, sn_temp_1, redshift, color, stretch, mock_sn_size, err )
411 mag_der_error = 0.0_dl
412 mag_derivative = 0.0_dl
416 a(1,i,:) = (sn_temp_1(:) - mag_initial(:))/(-hh)
417 else if (side==2)
then
418 param_vector(param_num) = fiducial_param_vector(param_num) + hh
420 call observed_sn_magnitude( p_temp, fp_temp, sn_temp_1, redshift, color, stretch, mock_sn_size, err )
423 mag_der_error = 0.0_dl
424 mag_derivative = 0.0_dl
428 a(1,i,:) = (sn_temp_1(:) - mag_initial(:))/(hh)
435 forall ( k = 1:mock_sn_size, accept_mask(k) == 0 )
437 a(j,i,k) = (a(j-1,i,k)*fac-a(j-1,i-1,k))/(fac-1._dl)
439 errt(k) = max( abs(a(j,i,k)-a(j-1,i,k)), &
440 & abs(a(j,i,k)-a(j-1,i-1,k)))
447 forall ( k = 1:mock_sn_size, &
448 & errt(k) .le. mag_der_error(k) .and. accept_mask(k) == 0 )
450 mag_der_error(k) = errt(k)
451 mag_derivative(k) = a(j,i,k)
458 forall ( k = 1:mock_sn_size, &
459 & accept_mask(k) == 0 .and. &
460 & (a(i,i,k) - a(i-1,i-1,k)) .ge. safe*mag_der_error(k) )
466 if ( sum(accept_mask) == mock_sn_size )
exit
471 if ( sum(accept_mask) /= mock_sn_size )
then
476 if ( fp%adaptivity )
then
477 deallocate( sn_temp_1, sn_temp_2, errt, accept_mask, a )
486 subroutine fisher_sn( P, FP, num_param, Fisher_Matrix, outroot )
490 Type(cambparams) ,
intent(in) :: P
493 integer ,
intent(in) :: num_param
494 real(dl),
dimension(num_param,num_param),
intent(out) :: Fisher_Matrix
495 character(len=*),
intent(in),
optional :: outroot
497 real(dl) :: time_1, time_2, time_1_MC, time_2_MC
498 real(dl) :: initial_step, temp, temp1, temp2
499 integer :: AllocateStatus, err, ind, ind2
500 integer :: i, j, k, l, number
501 real(dl),
allocatable :: param_array(:)
502 real(dl),
allocatable,
dimension(:,:) :: sn_mock
503 real(dl),
allocatable,
dimension(:) :: sn_mock_covariance
504 real(dl),
allocatable,
dimension(:) :: sn_fiducial, sn_derivative, sn_temp
505 real(dl),
allocatable,
dimension(:,:) :: sn_derivative_array
508 real(dl),
dimension(num_param,num_param) :: MC_Fisher_Matrix
511 allocate( param_array(num_param) )
515 number = fp%fisher_SN%total_SN_number
518 allocate( sn_mock(3,number), stat = allocatestatus )
519 if (allocatestatus /= 0) stop
"Allocation failed. Not enough memory to allocate SN mock."
520 allocate( sn_mock_covariance(number), stat = allocatestatus )
521 if (allocatestatus /= 0) stop
"Allocation failed. Not enough memory to allocate SN mock covariance."
522 allocate( sn_fiducial(number), sn_derivative(number), sn_temp(number), stat = allocatestatus )
523 if (allocatestatus /= 0) stop
"Allocation failed. Not enough memory to allocate SN fiducial."
524 allocate( sn_derivative_array( num_param, number ), stat = allocatestatus)
525 if (allocatestatus /= 0) stop
"Allocation failed. Not enough memory to allocate sn_derivative_array"
529 mc_fisher_matrix = 0._dl
531 time_1_mc = omp_get_wtime()
532 do mc_i=1, fp%fisher_SN%SN_Fisher_MC_samples
541 time_1 = omp_get_wtime()
544 time_2 = omp_get_wtime() - time_1
547 & filename=trim(outroot)//
'sn_fiducial_'//trim(adjustl(
integer_to_string(mc_i)))//
'.dat', magnitude = sn_fiducial )
550 else if ( err == 4 )
then
551 write(*,*)
'Fiducial model unstable. Calculation cannot proceed.'
554 write(*,*)
'Error in computing the fiducial. Calculation cannot proceed.'
559 if ( mc_i==1 .and. fp%cosmicfish_feedback >= 1 )
then
560 write(*,
'(a)')
'**************************************************************'
561 write(*,
'(a,i10)')
'Number of supernovae : ', number
562 write(*,
'(a,i10)')
'Number of parameters : ', num_param
563 write(*,
'(a,i10)')
'Number of MC repetitions : ', fp%fisher_SN%SN_Fisher_MC_samples
564 write(*,
'(a,f10.3,a)')
'Time taken for SN calculation: ', time_2,
' (s)'
565 write(*,
'(a,i10)')
'Number of omp processes : ', omp_get_max_threads()
566 if ( fp%adaptivity )
then
567 write(*,
'(a,f10.3,a)')
'Estimated completion time : ', fp%fisher_SN%SN_Fisher_MC_samples*time_2*20._dl*num_param,
' (s)'
569 write(*,
'(a,f10.3,a)')
'Estimated completion time : ', fp%fisher_SN%SN_Fisher_MC_samples*time_2*2._dl*num_param,
' (s)'
571 write(*,
'(a)')
'**************************************************************'
574 if ( mc_i>1 .and. fp%cosmicfish_feedback >= 1 )
then
575 write(*,
'(a)')
'**************************************************************'
576 write(*,
'(a,i10)')
'Doing MC computation of SN Fisher sample: ', mc_i
577 write(*,
'(a)')
'**************************************************************'
581 if ( fp%cosmicfish_feedback >= 1 )
then
582 write(*,
'(a)')
'Computation of the SN magnitude derivatives'
585 time_1 = omp_get_wtime()
586 do ind = 1, num_param
588 if ( fp%cosmicfish_feedback >= 1 )
then
589 write(*,
'(a,i5,a,E15.5)')
'Doing parameter number: ', ind,
' value: ', param_array(ind)
592 if ( param_array(ind) /= 0._dl )
then
593 initial_step = param_array(ind)*3.0_dl/100._dl
595 initial_step = 3.0_dl/100._dl
599 & sn_fiducial, sn_derivative, sn_temp, &
605 if (err /= 0) print*,
'WARNING: something went wrong with the SN derivative of parameter:', ind,
'Error code:', err
607 if ( mc_i==1 .and.
present(outroot) .and. fp%cosmicfish_feedback >= 2 )
then
609 & filename=trim(outroot)//
'sn_derivative_'//trim(adjustl(
integer_to_string(ind)))//
'.dat', magnitude = sn_derivative )
612 sn_derivative_array(ind,:) = sn_derivative(:)
615 time_2 = omp_get_wtime() - time_1
617 if ( fp%cosmicfish_feedback >= 1 )
then
619 write(*,
'(a,f8.3,a)')
'Time to compute all the derivatives:', time_2,
' (s)'
623 do ind = 1, num_param
624 do ind2 = 1, num_param
629 temp2 = temp2 + sn_derivative_array(ind,k)*sn_derivative_array(ind2,k)/sn_mock_covariance(k)
632 mc_fisher_matrix(ind, ind2) = mc_fisher_matrix(ind, ind2) + temp2
636 time_2 = omp_get_wtime() - time_1
638 if ( fp%cosmicfish_feedback >= 1 )
then
639 write(*,
'(a,f8.3,a)')
'Time to compute the Fisher matrix:', time_2,
' (s)'
643 time_2_mc = omp_get_wtime() - time_1
645 fisher_matrix = mc_fisher_matrix/fp%fisher_SN%SN_Fisher_MC_samples
647 if ( fp%cosmicfish_feedback >= 1 )
then
648 write(*,
'(a)')
'**************************************************************'
649 write(*,
'(a,f8.3,a)')
'Time to compute the MC Fisher matrix:', time_2_mc,
' (s)'
650 write(*,
'(a)')
'**************************************************************'
subroutine, public params_array_to_camb_param(P, FP, num_param, params_array)
This subroutine updates the values of the parameters P and FP based on the values contained in the pa...
This module contains the subroutine and functions to generate random numbers from different distribut...
subroutine, public observed_sn_magnitude_derivative(P, FP, param_num, mag_initial, mag_derivative, mag_der_error, sn_mock, initial_step, num_param, error_code)
This subroutine computes the derivative of the observed magnitude of a set of supernovae. The calculation is carried out with a fixed stepsize finite difference formula or by the Richardson’s deferred approach to the limit.
subroutine, public generate_sn_mock_data(P, FP, sn_mock)
This subroutine generates a mock SN data set compilation based on the input specifications contained ...
subroutine, public camb_params_to_params_array(P, FP, num_param, params_array)
This subroutine updates the values of the parameters array based on the values of P and FP...
This module contains the definitions of derived data types used to store the informations about the f...
This module contains the interface layer between CosmicFish and CAMB.
This module contains several general purpose utilities.
real(dl) function random_uniform(a, b)
This function returns a random number generated from the uniform distribution in [a,b(. To improve performances no check is done in order to ensure that the random number generator is initialised.
character(10) function, public integer_to_string(number)
This function converts an integer to a string. Usefull for numbered files output. ...
real(dl) function random_gaussian(mu, sigma)
This function returns a random number generated from the gaussian distribution with mean mu and varia...
subroutine, public observed_sn_magnitude(P, FP, sn_magnitude, redshift, color, stretch, num, err)
This subroutine returns the observed magnitude of a set of supernovae based on the input cosmological...
subroutine, public fisher_sn(P, FP, num_param, Fisher_Matrix, outroot)
This subroutine computes the SN Fisher matrix.
subroutine, public save_sn_mock_to_file(mock_size, sn_mock_data, sn_mock_covariance, filename, magnitude)
This subroutine outputs to file a SN covariance.
subroutine, public generate_sn_mock_covariance(P, FP, sn_mock, sn_mock_covariance)
This subroutine generates a mock SN covariance based on the input specifications contained in the Cos...
This module contains the code that computes the Fisher matrix for Supernovae measurements.
This derived data type contains all the informations that the cosmicfish code needs to run...