-
Notifications
You must be signed in to change notification settings - Fork 3
/
MAM.f
8095 lines (6824 loc) · 236 KB
/
MAM.f
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
!******************** MG-MAMPOSSt routines and functions ***********************
! made available in the libMAM.a library
! when compiling, link all the libraries, stored in the build folder
! e.g.:
! f95 -o testMAM.e testMAM.f -L build/ -lMAM -L build/GamI/ -lGamI -L build/Newuoa/ -lNewuoa -L build/JJin/ -lJJin -L build/Utili/ -lUtili -L build/Powell/ -lPowell
! **********************************************************************
! **********************************************************************
!> \addtogroup MAM MG-MAMPOSSt main ingredients
!> @details List of the main functions and subroutines needed for the execution
!! of MG-MAMPOSSt.
!> This block includes all the fundamental routines and functions on which the
!! MG-MAMPOSSt method is based. The central core is represented by the
!! mamposst() subroutine, which computes the tabulated \f$-\ln \mathcal{L}\f$.
!>
!> All the routines in this block need external inputs as described below.
!! In particular, the COMMON block of parameters "paramsoptS.i" should always be included.
!>
!> \ingroup MAM
subroutine mamposst(di,ve,eve,rso,vso,npg1)
!> @authors A. Biviano, G. Mamon, G. Boue', L. Pizzuti
!> @details MAMPOSSt subroutine computes the Log-likelihood in the projected
!! phase space.
!> This is the core of the MG-MAMPOSSt code. Along with
!! the input data, mamposst() requires as external
!! input the values of all the input parameters/Options of
!! MG-MAMPOSSt
!! along with the file names. In particular:
!> - name and id number of the output likelihood file: `iu60`
!> - name and id number of the output file of projected position
!! and velocity with the associated probability p(R_i,v_i): `iu20`
!> - name and id number of the output file containing the best fit of
!! projected number density profile: `iu30`
!> - input parameters in `test_pars.txt` described in
!! the documentation and named as in the COMMON block `paramsoptS.i`
!> - The `Options.txt` file, read within the subroutine
!>
!> In the case of `Nclust>1`
!! in `Options.txt`, the
!! mamposst() subroutine requires the additional data-files:
!> `data/datphys_<i>.dat`,
!! where "i" is an integer number larger than 1.
!>
!> @param[in] di(npg1) dimension REAL*8 projected distance in Mpc
!> @param[in] ve(npg1) dimension REAL*8 line-of-sight velocity [km/s]
!> @param[in] eve(npg1) dimension REAL*8 velocity error [km/s]
!> @param[in] npg1 INTEGER*4 number of galaxies in the
!! phase space
!>
!> @param[out] rso(npg1) dimension REAL*8 projected distance in Mpc of the
!! galaxies considered in the fit
!> @param[out] vso(npg1) dimension REAL*8 line-of-sight velocity [km/s] of
!! the galaxies considered in the fit
!> Example of usage, assuming to have read data and all the input parameters
!! in pars_test.txt and Options.txt:
!>
!> \code{.f}
!>
!> open(20,file="rnvn.dat",status='unknown')
!> open(30,file="Npar.dat",status='unknown')
!> open(60,file="MaxLik.dat",status='unknown')
!> iu20=20
!> iu30=30
!> iu60=60
!> call mamposst(di,ve,eve,rso,vso,npg1)
!> close(30)
!> close(60)
!> \endcode
!> Note that the file "iu20" is closed within the mamposst subroutine.
implicit real*8 (a-h,o-z)
implicit integer*4 (i-n)
logical*4 eval
parameter (pig=3.1415926535d0,iga=25000,
& grav=4.302e-9,q0=-0.64,clight=2.99792458d5, npar=7)
dimension di(npg1),ve(npg1),eve(npg1),rso(npg1),vso(npg1),
& xfv(2), sigma(npar)
dimension dpp(25000),vpp(25000),epp(25000),wpp(25000),
& did(25000),viv(25000),eie(25000),wiw(25000)
dimension freepar(7),freelow(7),freeup(7),wpar(5000),xi(7,7)
CHARACTER(len= 1) ARR1
character(len= 2) arr2
character(len = 13) :: filename
character(len=4) :: dat
character(len=300) :: buffer, label, label2
integer :: pos, pos1, pos2, posl1, posl2, posl, poscom
integer, parameter :: fh = 15
integer :: ios = 0
integer :: line = 0
include 'paramsoptS.i'
include 'datarv.i'
include 'units.i'
include 'free.i'
include 'probs.i'
external fcn1,fcn2,fcn3,fa
external Plens
external sr2int,sr2out,sigmar1,sigmar2,sigmar3,gwenu
print *,' Entering MAMPOSSt subroutine'
c Opening log file for keeping track of all the options
open(11,file="Run_info.txt",status='unknown')
write(11,*) 'Information about the current run'
write(11,*) ''
c Select only galaxy members within the requested radial range
rup=-1.e10
rlow=1.e10
jsel=0
c The radial selection range is in units of Mpc
rlowinmpc=rlowin
rupinmpc=rupin
do j=1,npg1
if (di(j).ge.rlowinmpc.and.
& di(j).le.rupinmpc) then
jsel=jsel+1
r(jsel)=di(j)
v(jsel)=(ve(j)-va)/(1.+va/clight)
e(jsel)=eve(j)
w(jsel)=w(j)
if (r(jsel).le.rlow) rlow=r(jsel)
if (r(jsel).ge.rup) rup=r(jsel)
endif
enddo
nga=jsel
write(*,932) nga,rlow,rup
932 format(/' Using ',i5,' galaxies in the fits',/,
+ ' in the radial range: ',f6.3,'--',f6.3,' Mpc')
c Best-fit to N(R) external to MAMPOSSt, if required
if (nrc.eq.-2) then
c NFW, Hernquist or beta-model (fixed alpha)
rf=1.e20
do j=1,2000
xtry=j*0.002
if (knfit.eq.1) then
res=fcn1(xtry)
elseif (knfit.eq.2) then
res=fcn2(xtry)
else
res=fcn3(xtry)
endif
if (res.lt.rf) then
xf=xtry
rf=res
endif
enddo
rc=xf
rcg=xf
write(*,195) rc
write(11,195) rc
195 format(' Best-fit to N(R) outside MAMPOSSt: r_tr=',f6.3)
endif
c switches to control the grid search *****************
open(fh, file='Options.txt')
! ios is negative if an end of record condition is encountered or if
! an endfile condition was detected. It is positive if an error was
! detected. ios is zero otherwise.
write(*,*) ''
write(*,*) '********************************'
write(*,*) ' Now reading Options'
write(*,*) '********************************'
write(*,*) ''
do while (ios == 0)
read(fh, '(A)', iostat=ios) buffer
if (ios == 0) then
line = line + 1
! Find the first instance of whitespace. Split label and data.
!devo togliere gli spazi all'inizio
ic=1
do while(scan(buffer(ic:ic),' ').ne.0)
!aumenta la posizione se trovi uno spazio
ic=ic+1
enddo
buffer=buffer(ic:)
pos1 = scan(buffer, ' ')
pos2 = scan(buffer, '=')
if (pos2.ge.pos1) then
pos=pos2
else
pos=pos1
endif
! split equality sign and space.
posl1=scan(buffer, ' ')
posl2=scan(buffer, '=')
if (posl1.ne.0.and.posl2.ne.0) then
posl=min(posl1,posl2)
label = buffer(1:posl-1)
else if (posl1.ne.0.and.posl2.eq.0) then
label = buffer(1:posl1)
else if (posl1.eq.0.and.posl2.ne.0) then
label = buffer(1:posl2)
endif
buffer = buffer(pos+1:)
poscom= scan(buffer, '!')
if (poscom.ne.0) buffer= buffer(:poscom-1)
!devo togliere gli spazi all'inizio
c ic=1
c do while(scan(label(ic:ic),' ').ne.0)
!aumenta la posizione se trovi uno spazio
c ic=ic+1
c enddo
c label2=label(ic:)
select case (label)
case ('nmcmc')
read(buffer, *, iostat=ios) nmcmc
if (ios.eq.-1) then
ios=0
nmcmc=1
endif
!write(*,"(' Read nmcmc: ',i10)") nmcmc
if (nmcmc.gt.4.or.nmcmc.lt.0) nmcmc=0
if (kdata.eq.0) then
if (nmcmc.gt.1) nmcmc=0
endif
case ('Nsample')
read(buffer, *, iostat=ios) Nsample
if (ios.eq.-1) then
ios=0
Nsample=400000
endif
!write(*,"(' Read Nsample: ',i10)") Nsample
! lensing
case ('nlens')
read(buffer, *, iostat=ios) nlens
if (ios.eq.-1) then
ios=0
nlens=0
endif
!write(*,"(' Read nlens: ',i10)") nlens
if (nlens.gt.1.or.nlens.lt.0) nlens=0
if(kmp.ne.7.and.kmp.ne.9.and.kmp.ne.8) nlens=0
case ('r200t')
read(buffer, *, iostat=ios) r200t
if (ios.eq.-1) then
ios=0
if (nlens.eq.1) then
write(*,*) 'error: lensing mode requires an'
write(*,*) 'input value for r200. '
write(*,*) 'Switching to nlens=0'
nlens=0
r200t=0.0d0
endif
endif
!write(*,"(' Read r200t: ',f10.2)") r200t
case ('rst')
read(buffer, *, iostat=ios) rst
if (ios.eq.-1) then
ios=0
if (nlens.eq.1) then
write(*,*) 'error: lensing mode requires an'
write(*,*) 'input value for rs. '
write(*,*) 'Switching to nlens=0'
nlens=0
rst=0.0d0
endif
endif
!write(*,"(' Read rst: ',f10.2)") rst
case ('delta1')
read(buffer, *, iostat=ios) wr2
if (ios.eq.-1) then
ios=0
wr2=0.1
if (kmp.eq.8) wr2=0.3
endif
!write(*,"(' Read delta1: ',f10.2)") wr2
case ('delta2')
read(buffer, *, iostat=ios) wr
if (ios.eq.-1) then
ios=0
wr=0.3
if (kmp.eq.8) wr=0.005
endif
!write(*,"(' Read delta2: ',f10.2)") wr
case ('delta3')
read(buffer, *, iostat=ios) wx
if (ios.eq.-1) then
ios=0
wx=0.5
if (kmp.eq.8) wx=30
endif
!write(*,"(' Read delta3: ',f10.2)") wx
! parameter space limits
case ('kpro')
read(buffer, *, iostat=ios) kpro
if (ios.eq.-1) then
ios=0
kpro=1
endif
!write(*,"(' Read kpro: ',i10)") kpro
if (kpro.gt.1.or.kpro.lt.0) kpro=1
! joint analysis
case ('Nclust')
read(buffer, *, iostat=ios) Nclust
if (ios.eq.-1) then
ios=0
Nclust=1
endif
!write(*,"(' Read Nclust: ',i10)") Nclust
case ('nsingle')
read(buffer, *, iostat=ios) nsingle
if (ios.eq.-1) then
ios=0
nsingle=0
endif
!write(*,"(' Read nsingle: ',i10)") nsingle
if (nsingle.gt.1.or.nsingle.lt.0) nsingle=0
! additional options
case ('istop')
read(buffer, *, iostat=ios) istop
if (ios.eq.-1) then
ios=0
istop=0
endif
!write(*,"(' Read istop: ',i10)") istop
if (istop.gt.1.or.istop.lt.0) istop=0
case ('teps')
read(buffer, *, iostat=ios) teps
if (ios.eq.-1) then
ios=0
teps(1)=0.1
teps(2)=0.1
teps(3)=0.1
teps(4)=0.1
teps(5)=0.1
teps(6)=0.1
teps(7)=0.1
endif
!write(*,"(' Read teps: ',7(f10.2))") teps
case ('delik')
read(buffer, *, iostat=ios) delik
if (ios.eq.-1) then
ios=0
delik=0.2
endif
!write(*,"(' Read delik: ',f10.3)") delik
case ('nskip')
read(buffer, *, iostat=ios) nskip
if (ios.eq.-1) then
ios=0
nskip=0
endif
!write(*,"(' Read nskip: ',i10)") nskip
if (nskip.gt.1.or.nskip.lt.0) nskip=0
case ('nres')
read(buffer, *, iostat=ios) nres
if (ios.eq.-1) then
ios=0
nres=0
endif
!write(*,"(' Read nres: ',i10)") nres
if (nres.gt.1.or.nres.lt.0) nres=0
case ('nequ')
read(buffer, *, iostat=ios) nequ
if (ios.eq.-1) then
ios=0
nequ=0
endif
!write(*,"(' Read nequ: ',i10)") nequ
if (nequ.gt.1.or.nequ.lt.0) nequ=0
case ('nsame')
read(buffer, *, iostat=ios) nsame
if (ios.eq.-1) then
ios=0
nsame=0
endif
!write(*,"(' Read nsame: ',i10)") nsame
if (nsame.gt.1.or.nsame.lt.0) nsame=0
if (nmcmc.eq.0) then
nsame=0
write(*,*) 'nsame option not valid in grid mode'
endif
case default
! print *, 'Skipping invalid label at line', line
end select
end if
end do
nplens=10 !number of points in the lensing likelihood
c ******************************************************
write(*,601) nmcmc, nres,nequ,istop,nskip,nlens,Nclust,Nsample
write(11,601) nmcmc,nres,nequ,istop,nskip,nlens,Nclust,Nsample
601 format(/,' Switches selected for parameter exploration: ',/,
& ' grid or MCMC (0/1)=',i1,/,
& ' finer grid= ',i1,/,
& ' equal grid values = ',i1,/,
& ' stop if desired = ',i1,/,
& ' skip the optimization results = ',i1,/,
& ' add lensing probability distribution = ',i1,/,
& ' Number of halos in the analysis = ',i2,/,
& ' Number of sampling points in MCMC = ',i7,/)
c Loop over r200, if requested, searching in the range
c of 0.5 to 1.8 the real r200
if (nr200.ge.2) then
dd=0.21*(500./nga)**(0.2)
if(nres.eq.1) dd=0.1*(500./nga)**(0.2) !finer grid
if (nequ.eq.1) then !equal spaced grid
dd=0.23
if(nres.eq.1) dd=0.08
endif
deltarv=dd/nr200
kr200=-nr200/2
else
kr200=0
endif
fmlmingrid=1.e20
r200g=r200
write(*,*) ' '
write(*,*) ' ... now running MAMPOSSt procedure ... '
c Now run MAMPOSSt
kboby=0 ! used to keep track of minimization algorithms already tried
knewu=0
kpowe=0
174 continue
ktried=kboby+knewu+kpowe
c Using NEWUOA or BOBYQA or POWELL minimization
c The free parameters are 6 in this version but can be less than
c 6 according to the choice of the user
ipar(1)=1 ! identify parameters that are to be minimized
ipar(2)=1
ipar(3)=1
ipar(4)=1
ipar(5)=1
ipar(6)=1
ipar(7)=1
facguess=1.0 ! factor to move away from best guess
c define free parameters
ip=0
if (nr200.gt.0) then ! virial radius
ip=ip+1
freepar(ip)=dlog10(facguess*r200g)
else
ipar(1)=0
endif
if (nrc.gt.0) then ! scale radius of tracers
ip=ip+1
freepar(ip)=dlog10(facguess*rcg)
else
ipar(2)=0
endif
if (nrs.gt.0) then ! scale radius of total matter
ip=ip+1
freepar(ip)=dlog10(facguess*rsg)
else
ipar(3)=0
endif
if (nbs.gt.0) then ! tracer velocity anisotropy parameter
ip=ip+1
freepar(ip)=(facguess*cbeg)
else
ipar(4)=0
endif
if (ntmass.gt.0) then ! modified grav parameter
ip=ip+1
if (kmp.ne.8) freepar(ip)=dlog10(facguess*tmassg)
if (kmp.eq.9) then !explore from 0 to 1 the range of parameter
! in the case of CS
freepar(ip)=(1-dexp(-facguess*tmassg*0.1))
endif
if (kmp.eq.8) then
freepar(ip)=(facguess*(tmassg))
if (tmassg.eq.0.0) freepar(ip)=(facguess*(tmassg+1.e-4))
endif !For BH gravity Y1 can be negative
else
ipar(5)=0
endif
if (nb2.gt.0) then ! tracer velocity anisotropy parameter
ip=ip+1
freepar(ip)=(facguess*cbe0g)
else
ipar(7)=0
endif
if (nhone.gt.0) then ! screening parameter
ip=ip+1
freepar(ip)=dlog10(facguess*screeg)
if(kmp.eq.9) then
freepar(ip)=facguess*screeg/(facguess*screeg+1)
endif
! if (kmp.eq.8) then
! freepar(ip)=(facguess*(screeg))
! if (screeg.eq.0.0) freepar(ip)=(facguess*(screeg+1.e-2)) !TEST2
! endif !For BH gravity Y2 can be negative
else
ipar(6)=0
endif
nfreepar=ip
write(11,*) 'Running MG-MAMPOSSt with the selected profile:'
select case(kmp)
case(1)
write(11,*) 'NFW mass model'
case(2)
write(11,*) 'Hernquist mass model'
case(3)
write(11,*) 'PIEMD mass model'
case(4)
write(11,*) 'Burkert mass model'
case(5)
write(11,*) 'Soft Isothermal mass model'
case(6)
write(11,*) 'Einasto mass model with n=5'
case(7)
write(11,*) 'mNFW (linear Hordenski) mass model'
case(8)
write(11,*) 'mNFW DHOST mass model'
case(9)
write(11,*) 'mNFW chameleon screening mass model'
case(10)
write(11,*) 'gNFW mass model'
end select
write(*,622) nfreepar,ipar,r200g,rcg,rsg,cbeg,tmassg,
& screeg,cbe0g,kmp,kani
write(11,622) nfreepar,ipar,r200g,rcg,rsg,cbeg,tmassg,
& screeg,cbe0g,kmp,kani
622 format(/,' Number of free parameters = ',i1,/
& ' r200, rc, rs, anis1, A1, A2, anis2 = ',7(i1,1x),/,
& ' Initial guess values: ',7(f6.3,1x),/,
& ' mass model= ',i2,/,
& ' anisotropy model= ',i2,/)
if(kmp.eq.8.and.nhone.gt.1) then
ipar(6)=0
nfreepar=nfreepar-1
write(*,*) "MG second parameter not optimized in kinematic BH"
write(*,*) " "
endif
npoints=(nfreepar+1)*(nfreepar+2)/2
c Setting switches
if (nrc.eq.-1) klfm=1 ! Light follows mass
if (nrs.eq.-1) kmfl=1 ! Mass follows Light
if (nrs.eq.-2) klcdm=1 ! LCDM c=c(M)
if (nbs.eq.-1) kaml=1 ! a_ML = r_s
if (nbs.eq.-1) khm=1 ! HM beta(r)
c start optmization unless not required
if (kopt.lt.0.) then
r200new=r200g
rcnew=rcg
rsnew=rsg
cbnew=cbeg
cb0new=cbe0g
scrnew=screeg
tmassnew=tmassg
goto 732
endif
if (kopt.eq.2.and.kani.gt.10) then
c POWELL doesn't work for gT, gOM with MG parametrizations
if (nfreepar.gt.1) then
write(*,*) 'WARNING: POWELL does not work efficiently with'
write(*,*) 'gOM, gT: switching to NEWUOA algorithm OPT=1'
write(*,*) ''
kopt=1
endif
endif
c newuoa and bobyqa only work with at least 2 free params
temp=r200g
if (nfreepar.gt.1.and.ktried.lt.2.9) then
if (kopt.eq.0.and.kboby.eq.0) then
write(*,*) ' '
write(*,*) ' Running optimization algorithm BOBYQA'
write(*,*) ' '
rhoend=1.d-4
iprint=0
maxfun=5000
rhobeg=0.05 ! should be 10% greatest expected change to a variable
addlowup=0.7 ! constant to subtract or add to get lower and upper interval
do ilu=1,nfreepar
freelow(ilu)=freepar(ilu)-addlowup
freeup(ilu)=freepar(ilu)+addlowup
enddo
CALL BOBYQA (nfreepar,npoints,freepar,freelow,freeup,
& rhobeg,rhoend,iprint,maxfun,wpar)
if (freepar(1).ne.freepar(1).and.inew.lt.10) then
write(*,*) ' '
write(*,*) ' BOBYQA failure!'
write(*,*) ' Try another optimization'
kboby=1
kopt=1
goto 174
endif
elseif (kopt.eq.1.and.knewu.eq.0) then
inew=-1
rhoend=1.d-4
iprint=0
maxfun=5000
rhobeg=0.05 ! should be 10% greatest expected change to a variable
write(*,*) ' '
write(*,*) ' Running optimization algorithm NEWUOA'
write(*,*) ' '
cc 294 inew=inew+1
CALL NEWUOA (nfreepar,npoints,freepar,rhobeg,rhoend,
& iprint,maxfun,wpar)
if (freepar(1).ne.freepar(1).and.inew.lt.10) then
write(*,*) ' '
write(*,*) ' NEWUOA failure!'
write(*,*) ' Try another optimization'
knewu=1
kopt=0
goto 174
endif
else
ftol=1.d-4 ! desired fractional tolerance in function to be minimized
npars=7 ! max number of free params
nfreefunc=nfreepar ! passed through a common to function func
do ixi=1,npars
do jxi=1,npars
xi(ixi,jxi)=0.d0
if (ixi.eq.jxi) xi(ixi,jxi)=1.d0
enddo
enddo
write(*,*) ' '
write(*,*) ' Running optimization algorithm POWELL'
write(*,*) ' '
call POWELL(freepar,xi,nfreepar,npars,ftol,iter,fret)
if (freepar(1).ne.freepar(1).and.inew.lt.10) then
write(*,*) ' '
write(*,*) ' POWELL failure!'
write(*,*) ' Try another optimization'
kpowe=1
kopt=1
goto 174
endif
endif
else ! when there is only 1 free param call POWELL
ftol=1.d-4 ! desired fractional tolerance in function to be minimized
npars=7 ! max number of free params
nfreefunc=nfreepar ! passed through a common to function func
do ixi=1,npars
do jxi=1,npars
xi(ixi,jxi)=0.d0
if (ixi.eq.jxi) xi(ixi,jxi)=1.d0
enddo
enddo
write(*,*) ' '
write(*,*) ' Running optimization algorithm POWELL'
write(*,*) ' '
call POWELL(freepar,xi,nfreepar,npars,ftol,iter,fret)
if (freepar(1).ne.freepar(1).and.inew.lt.10) then
write(*,*) ' '
write(*,*) ' POWELL failure!'
endif
endif
call calfun(nfreepar,freepar,f) !compute the likelihood at
!the minimum found
call freepareva(nfreepar,freepar,r200new,rcnew,rsnew,cbnew,
& tmassnew,scrnew,cb0new)
if(kmp.eq.8.and.nhone.gt.1) ipar(6)=1 !set again the free parameter
call sigmaeva(npar,sigma)
write(*,*)
write(*,*) 'optimiz. results: r200,rc,rs,beta,A1,A2,cbe0,-logL'
rs=rsnew
cbe=cbnew
cbe0=cb0new
xfv(1)=rs
xfv(2)=cbe
r200=r200new
tmass=tmassnew
screen=scrnew
call vmaxlik(nfv,xfv,fml2)
write(*,429) r200new, rcnew, rsnew, cbnew, tmassnew, scrnew,
& cb0new, fml2
429 format(7(f6.3,2x),f10.3)
rs=rsg
rc=rcg
cbe=cbeg
cbe0=cbe0g
xfv(1)=rs
xfv(2)=cbe
r200=temp
tmass=tmassg
screen=screeg
call vmaxlik(nfv,xfv,fml3)
write(*,*) 'likelihood of the initial value and Delta chi square:'
write(*,*) 'r200,rc,rs,beta,A1,A2, -logL, Delta chi square:'
write(*,439) r200,rc, rs, cbe,tmass,screen,cbe0,
& fml3, 2*(fml3-fml2)
439 format(7(f6.3,2x),(f10.3,2x),f6.3)
c************ tepsilon values for istop (see option.txt) ***************
c teps(1)=0.07
c teps(2)=0.07
c teps(3)=0.07
c teps(4)=0.07
c teps(5)=0.07
c teps(6)=0.07
c delik=2*0.15
c***********************************************************************
rcdf=dabs((rc-rcnew)/rcnew)
rsdf=dabs((rs-rsnew)/rsnew)
r2df=dabs((r200-r200new)/r200new)
cbdf=dabs((cbe-cbnew)/cbnew)
cb2f=dabs((cbe0-cb0new)/cb0new)
tmdf=dabs((tmass-tmassnew)/tmassnew)
scdf=dabs((screen-scrnew)/scrnew)
dfm=2*(fml3-fml2)
if (istop.eq.1) then !stops if required
if(dfm.gt.(2*delik)) then
write(*,*) ''
write(*,*) 'Initial guess not fulfilling the requirements'
stop
endif
if (r2df.gt.teps(1).or.rcdf.gt.teps(2).or.scdf.gt.teps(6)) then
write(*,*) ''
write(*,*) 'Initial guess not fulfilling the requirements'
stop
endif
if (rsdf.gt.teps(3).or.cbdf.gt.teps(4).or.tmdf.gt.teps(5)) then
write(*,*) ''
write(*,*) 'Initial guess not fulfilling the requirements'
stop
endif
if (cb2f.gt.teps(7)) then
write(*,*) ''
write(*,*) 'Initial guess not fulfilling the requirements'
stop
endif
else
kreq=0
if(dfm.gt.(2*delik)) kreq=1
if(r2df.gt.teps(1).or.rcdf.gt.teps(2).or.scdf.gt.teps(6)) kreq=1
if (rsdf.gt.teps(3).or.cbdf.gt.teps(4).or.tmdf.gt.teps(5)) kreq=1
if (cb2f.gt.teps(7)) kreq=1
if (kreq.eq.1) then
write(*,*) ''
write(*,*) 'Initial guess not fulfilling the requirements'
write(*,*) 'As requested, the parameter exploration starts anyway'
endif
endif
c if the best fit are out of the range, stop the code
if(r200new.gt.r2up.or.r200new.lt.r2low) then
write(*,*) 'Warning: best fit outside the prior range'
write(*,*) 'redefine the best fit as the average value'
r200new=(r2up+r2low)/2
endif
if(rsnew.gt.rsup.or.rsnew.lt.rslow) then
write(*,*) 'Warning: best fit "rs" outside the prior range'
write(*,*) 'redefine the best "rs" fit as the average value'
rsnew=(rsup+rslow)/2
endif
if(rcg.gt.rcup.or.rcg.lt.rclow) then
write(*,*) 'Warning: best fit "rc" outside the prior range'
write(*,*) 'redefine the best fit "rc" as the average value'
rcnew=(rcup+rclow)/2
endif
if(cbnew.gt.bup.or.cbnew.lt.blow) then
write(*,*) 'Warning: best fit "beta" outside the prior range'
write(*,*) 'redefine the best fit "beta" as the average value'
cbnew=(bup+blow)/2
endif
if(tmassnew.gt.tmup.or.tmassnew.lt.tmlow) then
write(*,*) 'Warning: best fit "tmass" outside the prior range'
write(*,*) 'redefine the best fit "tmass" as the average value'
tmassnew=(tmup+tmlow)/2
endif
if(scrnew.gt.scrup.or.scrnew.lt.scrlow) then
write(*,*) 'Warning: best fit "screen" outside the prior range'
write(*,*) 'redefine the best fit "screen" as the average value'
scrnew=(scrup+scrlow)/2
write(*,*) scrnew
endif
if(cb0new.gt.cb0up.or.cb0new.lt.cb0low) then
write(*,*) 'Warning: best fit "beta2" outside the prior range'
write(*,*) 'redefine the best fit "beta2" as the average value'
cb0new=(cb0up+cb0low)/2
endif
732 continue
write(*,*) ''
write(*,*) '**********************************'
if(kbsp.eq.1) write(*,*) 'Running in Fast mode'
if(kbsp.eq.0) write(*,*) 'Running in Normal mode'
write(*,*) '**********************************'
write(*,*) ''
itotal=0
call sigmaeva(npar,sigma)
!format(8(f13.5,2x),i2)
if(nlens.eq.0) then
WRITE(iu60,'(a,a12,2x,8(a13,2x))') '#','r200','rc','rs','cbe',
& 'A1','A2','cbe0','-log(P)', 'anisotropy'
WRITE(iu60,'(a,a12,2x,8(a13,2x))') '#','[Mpc]','[Mpc]','[Mpc]',
& '-','-','-','-','-', '-'
else
WRITE(iu60,'(a1,a12,2x,8(a13,2x))') '#','r200','rc','rs','cbe',
& 'A1','A2','cbe0','-log(P*Plens)', 'anisotropy'
WRITE(iu60,'(a,a12,2x,8(a13,2x))') '#','[Mpc]','[Mpc]','[Mpc]',
& '-','-','-','-','-', '-'
endif
write(iu60, *) ''
if (nmcmc.eq.1) then !Start the MCMC run
if (nsame.eq.0) then
icount=0
call CPU_TIME(start) !uses cpu time for the random generator seed
nstar=int(100*start, kind=8)
nseed = 123434789+(-1)**nstar*nstar
fmlb=f
if (kopt.lt.0) then
if (kmp.eq.9.and.nhone.eq.-1) scrnew=1./sqrt(6.0d0)
rs=rsnew
cbe=cbnew
cbe0=cb0new
rc=rcnew
xfv(1)=rs
xfv(2)=cbe
r200=r200new
tmass=tmassnew
screen=scrnew
call vmaxlik(nfv,xfv,f)
endif
fmlb=f
nlonly=0 !do only lensing analysis
! **********************************************************************
if (nlens.eq.1) then
if(kmp.eq.7.or.kmp.eq.9) then
fmlb=f-Plens(r200,rs)
if (nlonly.eq.1) then
fmlb=-Nclust*Plens(r200,rs)
endif
endif
if(kmp.eq.8) then
call Likelens_bh(nplens,plen)
fmlb=f+plen
if (nlonly.eq.1) then
fmlb=plen*Nclust
endif
endif
endif
c cbnew=cbeg
c****** defines temporary variables ************************************
rsst=rsnew
cbt=cbnew
cb0t=cb0new
r2t=r200new
tmt=tmassnew
rct=rcnew
scrt=scrnew
c**********************************************************************
write(*,*) 'MCMC over ', Nsample, 'trials'
cdc=0 !Dhost model with Y1=Y2=y
do while (icount<Nsample)
if (mod(i,100).eq.0) then
!Change the seed for random generator
nseed=abs(nseed+(-1)**(nseed)*floor(nseed/4.))
endif
r200n=r2t
rcn=rct
rsn=rsst
cbn=cbt
cb0n=cb0t
tmassn=tmt
scrn=scrt
if (cdc.eq.1) scrn=tmt
do i=1,6
if (float(itotal/500).lt.3) then
sigma(i)=(1+float(itotal/500))*sigma(i)
endif
enddo
if (itotal.ge.2000) then
r200n=r200g
rcn=rcg
rsn=rsg
cbn=cbeg
cb0n=cbe0g
tmassn=tmassg
scrn=screeg
endif
call tranmod(r200n,rcn,rsn,cbn,
& tmassn,scrn,cb0n,sigma,7,nseed)
rs=rsn
cbe=cbn
cbe0=cb0n
r200=r200n