-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathnested.F90
3532 lines (2998 loc) · 105 KB
/
nested.F90
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
!Do nested sampling algorithm to calculate Bayesian evidence
! Jul 2015
! Farhan Feroz
module Nested
use utils1
use kmeans_clstr
use xmeans_clstr
use posterior
use priors
implicit none
#ifdef MPI
include 'mpif.h'
integer mpi_status(MPI_STATUS_SIZE), errcode
#endif
integer my_rank
integer maxCls,maxeCls
integer mpi_nthreads !total no. of mpi processors
integer min_pt,totPar
integer nCdims !total no. of parameters on which clustering should be done
integer nlive ! Number of live points
integer ndims ! Number of dimensions
integer nsc_def !no. of iterations per every sub-clustering step
integer updInt !update interval
double precision Ztol !lowest local evidence for which samples to produce
double precision tol ! tolerance at end
double precision ef
logical multimodal ! multimodal or unimodal sampling
logical ceff ! constant efficiency?
integer numlike,globff
double precision logZero
integer maxIter
logical fback,resumeFlag,dlive,genLive,dino
!output files name
character(LEN=1000)physname,broot,rname,resumename,livename,physbirthname,evname,evbirthname,IS_Files(3)
!output file units
integer u_ev,u_ev_birth,u_resume,u_phys,u_live,u_phys_birth,u_IS(3)
double precision gZ,ginfo !total log(evidence) & info
integer count,sCount
logical, dimension(:), allocatable :: pWrap
logical mWrap,aWrap !whether to do wraparound for mode separation
logical debug, prior_warning, resume, outfile
!importance sampling
logical :: IS = .true.
logical bogus
contains
subroutine nestRun(nest_IS,nest_mmodal,nest_ceff,nest_nlive,nest_tol,nest_ef,nest_ndims,nest_totPar,nest_nCdims,maxClst, &
nest_updInt,nest_Ztol,nest_root,seed,nest_pWrap,nest_fb,nest_resume,nest_outfile,initMPI,nest_logZero,nest_maxIter, &
loglike,dumper,context)
implicit none
integer nest_ndims,nest_nlive,nest_updInt,context,seed,i
integer maxClst,nest_nsc,nest_totPar,nest_nCdims,nest_pWrap(*),nest_maxIter
logical nest_IS,nest_mmodal,nest_fb,nest_resume,nest_ceff,nest_outfile,initMPI
character(LEN=1000) nest_root
double precision nest_tol,nest_ef,nest_Ztol,nest_logZero
INTERFACE
!the likelihood function
subroutine loglike(Cube,n_dim,nPar,lnew,context_pass)
integer n_dim,nPar,context_pass
double precision lnew,Cube(nPar)
end subroutine loglike
end INTERFACE
INTERFACE
!the user dumper function
subroutine dumper(nSamples, nlive, nPar, physLive, posterior, paramConstr, maxLogLike, logZ, INSlogZ, logZerr, context_pass)
integer nSamples, nlive, nPar, context_pass
double precision, pointer :: physLive(:,:), posterior(:,:), paramConstr(:)
double precision maxLogLike, logZ, INSlogZ, logZerr
end subroutine dumper
end INTERFACE
#ifdef MPI
if( initMPI ) then
!MPI initializations
call MPI_INIT(errcode)
if (errcode/=MPI_SUCCESS) then
write(*,*)'Error starting MPI. Terminating.'
call MPI_ABORT(MPI_COMM_WORLD,errcode)
end if
endif
call MPI_COMM_RANK(MPI_COMM_WORLD, my_rank, errcode)
call MPI_COMM_SIZE(MPI_COMM_WORLD, mpi_nthreads, errcode)
#else
mpi_nthreads=1
my_rank=0
#endif
bogus = .false.
nest_nsc=50
nlive=nest_nlive
Ztol=nest_Ztol
updInt=nest_updInt
logZero=nest_logZero
maxIter=nest_maxIter
if(maxIter<=0) maxIter=huge(1)
ndims=nest_ndims
totPar=nest_totPar
nCdims=nest_nCdims
debug=.false.
prior_warning=.true.
resume=nest_resume
outfile=nest_outfile
if(.not.outfile) resume=.false.
if(nCdims>ndims) then
if(my_rank==0) then
write(*,*)"ERROR: nCdims can not be greater than ndims."
write(*,*)"Aborting"
endif
#ifdef MPI
call MPI_ABORT(MPI_COMM_WORLD,errcode)
#endif
stop
endif
if(my_rank==0) then
count=0
sCount=0
broot=nest_root
rname = trim(broot)
fback = nest_fb
!output file info
!setup the output files
resumename = trim(rname)//'resume.dat'
physname = trim(rname)//'phys_live.points'
livename = trim(rname)//'live.points'
physbirthname = trim(rname)//'phys_live-birth.txt'
evname = trim(rname)//'ev.dat'
evbirthname = trim(rname)//'dead-birth.txt'
u_ev=55
u_ev_birth=550
u_phys=57
u_live=59
u_phys_birth=590
u_resume=61
if( IS ) then
IS_Files(1) = trim(rname)//'IS.points'
IS_Files(2) = trim(rname)//'IS.ptprob'
IS_Files(3) = trim(rname)//'IS.iterinfo'
u_IS(1) = 62
u_IS(2) = 63
u_IS(3) = 64
endif
endif
allocate(pWrap(ndims))
mWrap=.false.
aWrap=.true.
do i=1,ndims
if(nest_pWrap(i)==0) then
pWrap(i)=.false.
if(i<=nCdims) aWrap=.false.
else
pWrap(i)=.true.
if(i<=nCdims) mWrap=.true.
endif
enddo
IS = nest_IS
multimodal=nest_mmodal
if( IS ) multimodal = .false.
ceff=nest_ceff
tol=nest_tol
ef=nest_ef
if(ef>=1d6) then
dino=.false.
ef=1d0
min_pt=ndims+1
elseif(ceff) then
if(ef>1d0) then
if(my_rank==0) then
write(*,*)"ERROR: Can not undersample in constant efficiency mode."
write(*,*)"Aborting"
endif
#ifdef MPI
call MPI_ABORT(MPI_COMM_WORLD,errcode)
#endif
stop
endif
dino=.true.
min_pt=2
else
dino=.true.
min_pt=2
endif
nsc_def=nest_nsc
if(.not.multimodal) then
maxCls=1
else
maxCls=maxClst*2
endif
maxeCls=nlive/min_pt
if(seed<0) then
!take the seed from system clock
call InitRandomNS(mpi_nthreads)
else
call InitRandomNS(mpi_nthreads,seed)
endif
if(my_rank==0) then
!set the resume flag to true if the resume file exists else to false
if(resume) then
inquire(file=resumename,exist=resumeFlag)
if(.not.resumeFlag) write(*,*)"MultiNest Warning: no resume file found, starting from scratch"
else
resumeFlag=.false.
endif
write(*,*)"*****************************************************"
write(*,*)"MultiNest v3.12"
write(*,*)"Copyright Farhan Feroz & Mike Hobson"
write(*,*)"Release Nov 2019"
write(*,*)
write(*,'(a,i4)')" no. of live points = ",nest_nlive
write(*,'(a,i4)')" dimensionality = ",nest_ndims
if(ceff) write(*,'(a)')" running in constant efficiency mode"
if(resumeFlag) write(*,'(a)')" resuming from previous job"
write(*,*)"*****************************************************"
if (fback) write (*,*) 'Starting MultiNest'
!create the output files
if(.not.resumeFlag .and. outfile) then
open(unit=u_ev,file=evname,status='replace')
close(u_ev)
open(unit=u_ev_birth,file=evbirthname,status='replace')
close(u_ev_birth)
endif
endif
call Nestsample(loglike, dumper, context)
deallocate(pWrap)
call killRandomNS()
#ifdef MPI
if( initMPI ) call MPI_FINALIZE(errcode)
#endif
end subroutine nestRun
!----------------------------------------------------------------------
subroutine Nestsample(loglike, dumper, context)
implicit none
integer context
double precision, allocatable :: p(:,:), phyP(:,:) !live points
double precision, allocatable :: l(:), l0(:) !log-likelihood
double precision vnow1!current vol
double precision ltmp(totPar+2)
character(len=1000) fmt
integer np,i,j,k,ios
logical flag
INTERFACE
!the likelihood function
subroutine loglike(Cube,n_dim,nPar,lnew,context_pass)
integer n_dim,nPar,context_pass
double precision lnew,Cube(nPar)
end subroutine loglike
end INTERFACE
INTERFACE
!the user dumper function
subroutine dumper(nSamples, nlive, nPar, physLive, posterior, paramConstr, maxLogLike, logZ, INSlogZ, logZerr, context_pass)
integer nSamples, nlive, nPar, context_pass
double precision, pointer :: physLive(:,:), posterior(:,:), paramConstr(:)
double precision maxLogLike, logZ, INSlogZ, logZerr
end subroutine dumper
end INTERFACE
allocate( p(ndims,nlive+1), phyP(totPar,nlive+1), l(nlive+1), l0(nlive+1) )
if(my_rank==0) then
np=ndims
globff=0
numlike=0
vnow1=1.d0
write(fmt,'(a,i5,a)') '(',np+1,'E28.18E3)'
genLive=.true.
if(resumeflag) then
!check if the last job was aborted during the live points generation
open(unit=u_resume,file=resumename,status='old')
read(u_resume,*)genLive
if( .not.genLive ) then
read(u_resume,*)i,j,j,j
if( j /= nlive ) then
write(*,*)"ERROR: no. of live points in the resume file is not equal to the the no. passed to nestRun."
write(*,*)"Aborting"
#ifdef MPI
call MPI_ABORT(MPI_COMM_WORLD,errcode)
#endif
stop
endif
endif
close(u_resume)
if( .not.genLive ) then
j = 0
open(unit=u_ev,file=evname,status='old')
write(fmt,'(a,i2.2,a)') '(',totPar+2,'E28.18E3,i3)'
do
read(55,*,IOSTAT=ios) ltmp(1:totPar+2),k
!end of file?
if(ios<0) exit
j = j + 1
enddo
close(u_ev)
if( j + nlive /= i ) then
write(*,*)"ERROR: no. of points in ev.dat file is not equal to the no. specified in resume file."
write(*,*)"Aborting"
#ifdef MPI
call MPI_ABORT(MPI_COMM_WORLD,errcode)
#endif
stop
endif
endif
endif
gZ=logZero
ginfo=0.d0
endif
#ifdef MPI
call MPI_BARRIER(MPI_COMM_WORLD,errcode)
call MPI_BCAST(genLive,1,MPI_LOGICAL,0,MPI_COMM_WORLD,errcode)
#endif
if(genLive) then
if(my_rank==0 .and. fback) write(*,*) 'generating live points'
call gen_initial_live(p,phyP,l,loglike,dumper,context)
l0=logZero
if(my_rank==0 .and. .not.bogus) then
globff=nlive
numlike=nlive
if(fback) write(*,*) 'live points generated, starting sampling'
endif
endif
#ifdef MPI
call MPI_BARRIER(MPI_COMM_WORLD,errcode)
#endif
if( .not.bogus ) call clusteredNest(p,phyP,l,l0,loglike,dumper,context)
if( my_rank==0 ) then
if( .not.bogus ) then
write(*,*)"ln(ev)=",gZ,"+/-",sqrt(ginfo/dble(nlive))
write(*,'(a,i12)')' Total Likelihood Evaluations: ', numlike
write(*,*)"Sampling finished. Exiting MultiNest"
else
write(*,*)"Exit signal received"
endif
setBlk=.false.
endif
deallocate( p, phyP, l )
end subroutine Nestsample
!----------------------------------------------------------------------
subroutine gen_initial_live(p,phyP,l,loglike,dumper,context)
implicit none
integer i,j,iostatus,idum,k,m,nptPerProc,nGen,nstart,nend,context
double precision, allocatable :: pnewP(:,:), phyPnewP(:,:), lnewP(:)
double precision p(ndims,nlive+1), phyP(totPar,nlive+1), l(nlive+1), ldum
integer id
character(len=1000) fmt,fmt1,fmt2
#ifdef MPI
double precision, allocatable :: tmpl(:), tmpp(:,:), tmpphyP(:,:)
integer q
#endif
INTERFACE
!the likelihood function
subroutine loglike(Cube,n_dim,nPar,lnew,context_pass)
integer n_dim,nPar,context_pass
double precision lnew,Cube(nPar)
end subroutine loglike
end INTERFACE
INTERFACE
!the user dumper function
subroutine dumper(nSamples, nlive, nPar, physLive, posterior, paramConstr, maxLogLike, logZ, INSlogZ, logZerr, context_pass)
integer nSamples, nlive, nPar, context_pass
double precision, pointer :: physLive(:,:), posterior(:,:), paramConstr(:)
double precision maxLogLike, logZ, INSlogZ, logZerr
end subroutine dumper
end INTERFACE
allocate( pnewP(ndims,10), phyPnewP(totPar,10), lnewP(10) )
#ifdef MPI
allocate( tmpl(10), tmpp(ndims,10), tmpphyP(totPar,10) )
#endif
if(my_rank==0) then
if(outfile) then
open(unit=u_resume,file=resumename,form='formatted',status='replace')
write(u_resume,'(l2)')genLive
close(u_resume)
write(fmt,'(a,i5,a)') '(',ndims+1,'E28.18E3)'
write(fmt1,'(a,i5,a)') '(',totPar+2,'E28.18E3,i4)'
write(fmt2,'(a,i5,a)') '(',totPar+1,'E28.18E3,i4)'
endif
id=0
i=0
!resume from previous live points generation?
if(outfile) then
if(resumeflag) then
!read hypercube-live file
open(unit=u_live,file=livename,status='old')
do
i=i+1
read(u_live,*,IOSTAT=iostatus) p(:,i),l(i),ldum
if(iostatus<0) then
i=i-1
if(i>nlive) then
write(*,*)"ERROR: more than ",nlive," points in the live points file."
write(*,*)"Aborting"
#ifdef MPI
call MPI_ABORT(MPI_COMM_WORLD,errcode)
#endif
stop
endif
exit
endif
enddo
close(u_live)
if(i>0) then
!read physical live file
open(unit=u_phys,file=physname,status='old')
do j=1,i
read(u_phys,*) phyP(:,j),l(j),idum
enddo
close(u_phys)
endif
open(unit=u_live,file=livename,form='formatted',status='old',position='append')
open(unit=u_phys_birth,file=physbirthname,form='formatted',status='old',position='append')
open(unit=u_phys,file=physname,form='formatted',status='old',position='append')
else
open(unit=u_live,file=livename,form='formatted',status='replace')
open(unit=u_phys_birth,file=physbirthname,form='formatted',status='replace')
open(unit=u_phys,file=physname,form='formatted',status='replace')
if( IS ) then
open(unit=u_IS(2),file=IS_Files(2),form='unformatted',access='sequential',status='replace')
write(u_IS(2))0,0,0
close(u_IS(2))
open(unit=u_IS(1),file=IS_Files(1),form='unformatted',access='sequential',status='replace')
close(u_IS(1))
open(unit=u_IS(3),file=IS_Files(3),form='unformatted',access='sequential',status='replace')
close(u_IS(3))
endif
endif
endif
j=i
nend=i
nGen = nlive - j
nptPerProc = ceiling( dble(nGen) / dble(mpi_nthreads) )
endif
#ifdef MPI
call MPI_BARRIER(MPI_COMM_WORLD,errcode)
call MPI_BCAST(nGen,1,MPI_INTEGER,0,MPI_COMM_WORLD,errcode)
call MPI_BCAST(nptPerProc,1,MPI_INTEGER,0,MPI_COMM_WORLD,errcode)
#endif
if(nGen==0) then
if(my_rank==0) then
genLive=.false.
resumeFlag=.false.
endif
deallocate( pnewP, phyPnewP, lnewP )
#ifdef MPI
deallocate( tmpl, tmpp, tmpphyP )
#endif
if( outfile ) then
close(u_live)
close(u_phys_birth)
close(u_phys)
endif
return
elseif(nGen<0) then
if(my_rank==0) then
write(*,*)"ERROR: live points files have more live points than required."
write(*,*)"Aborting"
endif
#ifdef MPI
call MPI_ABORT(MPI_COMM_WORLD,errcode)
#endif
stop
endif
k=0
j=0
id=my_rank
do
k=k+1
j=j+1
do
call getrandom(ndims,pnewP(:,j),id) ! start points
phyPnewP(1:ndims,j)=pnewP(1:ndims,j)
lnewP(j)=logZero
call loglike(phyPnewP(:,j),ndims,totPar,lnewP(j),context)
if( lnewP(j) == HUGE(1d0) ) bogus = .true.
if(lnewP(j)>logZero) exit
enddo
if(k==nptPerProc .or. j==10) then
if(k==nptPerProc) then
i=mod(nptPerProc,10)
if(i==0) i=10
else
i=10
j=0
endif
#ifdef MPI
call MPI_BARRIER(MPI_COMM_WORLD,errcode)
#endif
if(id/=0) then
#ifdef MPI
!send the generated points to the root node
call MPI_SEND(lnewP(1:i),i,MPI_DOUBLE_PRECISION,0,id,MPI_COMM_WORLD,errcode)
call MPI_SEND(pnewP(1:ndims,1:i),ndims*i,MPI_DOUBLE_PRECISION,0,id,MPI_COMM_WORLD,errcode)
call MPI_SEND(phyPnewP(1:totPar,1:i),totPar*i,MPI_DOUBLE_PRECISION,0,id,MPI_COMM_WORLD,errcode)
#endif
else
!first write the points generated by the root node
nstart=nend+1
p(1:ndims,nstart:nstart+i-1)=pnewP(1:ndims,1:i)
phyP(1:totPar,nstart:nstart+i-1)=phyPnewP(1:totPar,1:i)
l(nstart:nstart+i-1)=lnewP(1:i)
nend=nstart+i-1
#ifdef MPI
!receive the points from other nodes
do m=1,mpi_nthreads-1
call MPI_RECV(tmpl(1:i),i,MPI_DOUBLE_PRECISION,m,m,MPI_COMM_WORLD,mpi_status,errcode)
call MPI_RECV(tmpp(1:ndims,1:i),i*ndims,MPI_DOUBLE_PRECISION,m,m,MPI_COMM_WORLD,mpi_status,errcode)
call MPI_RECV(tmpphyP(1:totPar,1:i),i*totPar,MPI_DOUBLE_PRECISION,m,m,MPI_COMM_WORLD,mpi_status,errcode)
do q = 1 , i
if( nend + 1 <= nlive ) then
l(nend + 1) = tmpl(q)
if( tmpl(q) == HUGE(1d0) ) bogus = .true.
p(1 : ndims, nend + 1) = tmpp(1 : ndims, q)
phyP(1 : totPar, nend + 1) = tmpphyP(1 : totPar, q)
nend = nend + 1
endif
enddo
enddo
#endif
if( outfile .and. .not.bogus ) then
!now write this batch to the files
do m=nstart,nend
write(u_live,fmt) p(1:ndims,m),l(m)
write(u_phys_birth,fmt1) phyP(:,m),l(m),logzero,1
write(u_phys,fmt2) phyP(:,m),l(m),1
enddo
endif
endif
endif
#ifdef MPI
call MPI_BCAST(bogus,1,MPI_LOGICAL,0,MPI_COMM_WORLD,errcode)
#endif
if(k==nptPerProc .or. bogus) exit
enddo
deallocate( pnewP, phyPnewP, lnewP )
#ifdef MPI
deallocate( tmpl, tmpp, tmpphyP )
#endif
if( outfile ) then
close(u_live)
close(u_phys_birth)
close(u_phys)
endif
genLive=.false.
resumeFlag=.false.
#ifdef MPI
call MPI_BARRIER(MPI_COMM_WORLD,errcode)
#endif
end subroutine gen_initial_live
!----------------------------------------------------------------------
subroutine getrandom(n,x,id)
implicit none
integer, intent(in) :: n
double precision, intent(out) :: x(:)
integer i,id
! --- uniform prior ----
do i = 1,n
x(i)=ranmarNS(id)
enddo
return
end subroutine getrandom
!----------------------------------------------------------------------
subroutine clusteredNest(p,phyP,l,l0,loglike,dumper,context)
implicit none
!input variables
integer context
double precision p(ndims,nlive+1) !live points
double precision phyP(totPar,nlive+1) !physical live points
double precision l(nlive+1),l0(nlive+1) !log-likelihood
!work variables
!misc
integer i, j, k, m, n, j1, i1, i2, i3, i4, ff, sff, n1, n2, q, nd, nd_i, nd_j, iostatus
integer num_old
integer, allocatable :: eswitchff(:), escount(:), dmin(:)
double precision d1, d2, d3, d4, d5, urv
double precision h, logX, vprev, vnext, shrink !prior volume
double precision mar_r !marginal acceptance rate
double precision gZOld !global evidence & info
logical eswitch,peswitch,cSwitch !whether to do ellipsoidal sampling or not
logical remFlag, acpt, flag, flag2
integer funit1, funit2, funit3 !file units
character(len=1000) fName1, fName2, fName3 !file names
character(len=1000) fmt,fmt1,fmt2
!diagnostics for determining when to do eigen analysis
integer neVol
parameter(neVol=4)
double precision, dimension(:), allocatable :: totVol, x1, x2, y1, y2, slope, intcpt, cVolFrac, pVolFrac
double precision, dimension(:,:,:), allocatable :: eVolFrac
!info for output file update
double precision, allocatable :: evData(:,:), evDataAll(:), evDataTemp(:)
!isolated cluster info
integer ic_n !no. of nodes
integer, allocatable :: ic_sc(:), ic_npt(:)
logical, allocatable :: ic_done(:)
integer, dimension(:), allocatable :: ic_fNode, ic_nsc, ic_nBrnch
double precision, dimension(:,:,:), allocatable :: ic_brnch, ic_llimits, ic_plimits
double precision, allocatable :: ic_climits(:,:,:), ic_volFac(:)
double precision, dimension(:), allocatable :: ic_Z, ic_Zold, ic_info, ic_vnow, ic_hilike, ic_inc
double precision, dimension(:,:), allocatable :: ic_eff
logical, dimension(:), allocatable :: ic_reme, ic_rFlag, ic_chk
logical modeFound
!means & standard deviations of the live points (for prior edge detection)
double precision, dimension(:,:), allocatable :: ic_mean, ic_sigma
double precision lPts(ndims)
!sub-cluster properties
integer sc_n !no. of sub-clusters
integer, dimension(:), allocatable :: sc_npt, nptk, nptx ,sc_node, nodek, sck
double precision, dimension(:,:), allocatable :: meank, sc_eval, evalk
double precision, dimension(:,:,:), allocatable :: sc_invcov, invcovk, sc_evec, eveck, tMatk
double precision, dimension(:), allocatable :: kfack, volk, effk
double precision, allocatable :: sc_mean(:,:), sc_tmat(:,:,:), sc_kfac(:), sc_eff(:), sc_vol(:)
!auxiliary points (to be re-arranged with main points during clustering)
integer naux !dimensionality of aux points
double precision, dimension(:,:), allocatable :: aux, pt
!rejected point info
double precision lowlike !lowest log-like
double precision lowl0
double precision, allocatable :: lowp(:), lowphyP(:) !point with the lowlike
integer indx(1) !point no. of lowlike
!new point
double precision lnew
double precision, allocatable :: pnew(:), phyPnew(:) ! new point
double precision, dimension(:,:,:), allocatable :: pnewa, phyPnewa
double precision, dimension(:,:), allocatable :: lnewa
integer, dimension(:), allocatable :: rIdx
integer, dimension(:,:), allocatable :: sEll
logical, dimension(:), allocatable :: remain
!mode separation
integer nCdim
!importance sampling
double precision, allocatable :: IS_allpts(:,:), IS_iterinfo(:,:), IS_V(:)
integer IS_counter(7)
integer :: IS_nstore = 10000, IS_nMC = 1000
double precision :: IS_Z(2)
logical :: IS_CheckAll = .false., IS_betterMC = .true., IS_GetVolInsidePrior = .true.
INTERFACE
!the likelihood function
subroutine loglike(Cube,n_dim,nPar,lnew,context_pass)
integer n_dim,nPar,context_pass
double precision lnew,Cube(nPar)
end subroutine loglike
end INTERFACE
INTERFACE
!the user dumper function
subroutine dumper(nSamples, nlive, nPar, physLive, posterior, paramConstr, maxLogLike, logZ, INSlogZ, logZerr, context_pass)
integer nSamples, nlive, nPar, context_pass
double precision, pointer :: physLive(:,:), posterior(:,:), paramConstr(:)
double precision maxLogLike, logZ, INSlogZ, logZerr
end subroutine dumper
end INTERFACE
allocate( eswitchff(maxCls), escount(maxCls), dmin(maxCls) )
allocate( evData(updInt,totPar+4) )
allocate( ic_sc(maxCls), ic_npt(maxCls) )
allocate( ic_done(0:maxCls) )
allocate( ic_climits(maxCls,ndims,2), ic_volFac(maxCls) )
allocate( sc_mean(maxeCls,ndims), sc_tmat(maxeCls,ndims,ndims), sc_kfac(maxeCls), sc_eff(maxeCls), sc_vol(maxeCls) )
allocate( lowp(ndims), lowphyP(totPar) )
allocate( pnew(ndims), phyPnew(totPar) )
!initializations
ic_done=.false.
ic_npt=nlive
ic_climits(:,:,2)=1d0
ic_climits(:,:,1)=0d0
ic_volFac(:)=1d0
if(my_rank==0) then
if( IS ) then
if( IS_GetVolInsidePrior ) then
IS_GetVolInsidePrior = .false.
do i = 1, ndims
if( .not.pWrap(i) ) then
IS_GetVolInsidePrior = .true.
exit
endif
enddo
endif
!memory allocation
allocate(IS_allpts(nlive+IS_nstore,ndims+6), IS_iterinfo(nlive+IS_nstore/10,5), IS_V(maxeCls))
IS_allpts = 0d0
IS_iterinfo = 0d0
do i = 1, nlive
IS_allpts(i,1:ndims) = p(1:ndims,i) !point
enddo
IS_allpts(1:nlive,ndims+1) = l(1:nlive) !likelihood
IS_allpts(1:nlive,ndims+2) = dble(nlive) !p(\Theta) n = \Sum_{i}^{niter} n_{i} E_{i}(\Theta) / V_{i}
IS_allpts(1:nlive,ndims+3) = 1d0 !check this point for ellipsoid membership in later iterations
IS_allpts(1:nlive,ndims+4) = 1d0 !which ellipsoid this point lies in
IS_allpts(1:nlive,ndims+5) = 1d0 !Mahalanobis distance
IS_allpts(1:nlive,ndims+6) = 1d0 !node
IS_iterinfo(1:nlive,1) = 1d0 !volume
IS_iterinfo(1:nlive,2) = 1d0 !total no. of points collected at each iteration, excluding points outside prior
IS_iterinfo(1:nlive,4) = 1d0 !total no. of points collected at each iteration, including points outside prior
IS_iterinfo(1:nlive,3) = 1d0 !effective no. of points collected at each iteration
IS_iterinfo(1:nlive,5) = 1d0 !node
IS_counter(1) = nlive !total no. of points collected so far
IS_counter(2) = 1 !first point to be checked for ellipsoid membership in later iterations
IS_counter(3) = nlive+IS_nstore !total no. of points that can be stored in IS_allpts array
IS_counter(4) = nlive+IS_nstore/10 !total no. of points that can be stored in IS_iterinfo array
IS_counter(5) = nlive !total no. of iterations done so far
IS_counter(6) = 0 !total no. of IS_iterinfo members written to output file
IS_counter(7) = 0 !total no. of IS_allpts members written to output file
endif
allocate(evDataAll(1))
allocate(sc_npt(maxeCls), nptk(maxeCls), nptx(nlive), meank(maxeCls,ndims), &
sc_eval(maxeCls,ndims), evalk(maxeCls,ndims), sc_invcov(maxeCls,ndims,ndims), &
invcovk(maxeCls,ndims,ndims), tMatk(maxeCls,ndims,ndims), &
sc_evec(maxeCls,ndims,ndims), eveck(maxeCls,ndims,ndims), kfack(maxeCls), &
effk(maxeCls), volk(maxeCls), &
sc_node(maxeCls),nodek(maxeCls),sck(maxCls))
allocate(pt(ndims,nlive), aux(ndims+totPar+5-nCdims,nlive))
allocate(ic_fNode(maxCls),ic_nsc(maxCls),ic_nBrnch(maxCls), &
ic_brnch(maxCls,maxCls,2),ic_reme(maxCls),ic_rFlag(maxCls),ic_z(maxCls),ic_zold(maxCls),ic_info(maxCls), &
ic_vnow(maxCls),ic_hilike(maxCls),ic_inc(maxCls),ic_chk(maxCls),ic_llimits(maxCls,ndims,2))
if(multimodal) allocate(ic_plimits(maxCls,nCdims,2))
if(ceff) then
allocate(ic_eff(maxCls,4))
ic_eff(:,1:2)=0d0
ic_eff(:,3)=1d0
ic_eff(:,3)=ef
ic_eff(:,4)=1d0
endif
allocate(pnewa(maxCls,mpi_nthreads,ndims),phyPnewa(maxCls,mpi_nthreads,totPar),lnewa(maxCls,mpi_nthreads), &
sEll(maxCls,mpi_nthreads),remain(maxCls),rIdx(maxCls))
allocate(totVol(maxCls),eVolFrac(maxCls,neVol*2,neVol),x1(maxCls),x2(maxCls),y1(maxCls), &
y2(maxCls),slope(maxCls),intcpt(maxCls),cVolFrac(maxCls),pVolFrac(maxCls))
if( prior_warning ) allocate( ic_mean(maxCls, ndims), ic_sigma(maxCls, ndims) )
!global logZ = log(0)
gZ=logZero
gZOld=logZero
ic_Z=logZero
ic_zold=logZero
ginfo=0.d0
ic_info=0.d0
ic_llimits(:,:,2)=1d0
ic_llimits(:,:,1)=0d0
if(multimodal) then
ic_plimits(:,:,2)=huge(1d0)
ic_plimits(:,:,1)=-huge(1d0)
endif
!just one node to sttart with
ic_n=1
ic_fNode(1)=0
ic_sc(1)=0
ic_nsc=nsc_def
ic_reme=.false.
ic_nBrnch=0
!set the prior volume
ic_vnow=0.d0
ic_vnow(1)=1.d0
!no ellipsoidal sampling to start with
eswitch=.false.
peswitch=.false.
escount=0
!no leftover points
remain=.false.
rIdx=1
!no sub-clusters at the beginning
sc_n=0
totVol=1.d0
sc_node=1
!eigen analysis diagnostics
eVolFrac=0.d0
dmin=ndims+1
ic_rFlag=.false.
sff=0
if(resumeFlag) then
!read the resume file
funit1=u_resume
open(unit=funit1,file=resumename,status='old')
read(funit1,*)genLive
read(funit1,*)globff,numlike,ic_n,nlive
read(funit1,*)gZ,ginfo
ic_rFlag(1:ic_n)=.true.
read(funit1,*)eswitch
peswitch=eswitch
if(eswitch) totVol=1.d0
!read branching info
do i=1,ic_n
read(funit1,*)ic_nBrnch(i)
if(ic_nBrnch(i)>0) read(funit1,*)ic_brnch(i,1:ic_nBrnch(i),1),ic_brnch(i,1:ic_nBrnch(i),2)
enddo
!read the node info
do i=1,ic_n
read(funit1,*)ic_done(i),ic_reme(i),ic_fNode(i),ic_npt(i)
read(funit1,*)ic_vnow(i),ic_Z(i),ic_info(i)
if(ceff) then
read(funit1,*)ic_eff(i,4)
ic_eff(i,3)=ic_eff(i,4)
endif
enddo
if(.not.eswitch .or. ic_n==1) ic_npt(1)=nlive
close(funit1)
eswitchff=0
!read hypercube-live file
open(unit=u_live,file=livename,status='old')
i=0
do
i=i+1
read(u_live,*,IOSTAT=iostatus) p(1:ndims,i),l(i)
if(iostatus<0) then
i=i-1
if(i<nlive) then
write(*,*)"ERROR: live points file has less than ",nlive," points."
write(*,*)"Aborting"
#ifdef MPI
call MPI_ABORT(MPI_COMM_WORLD,errcode)
#endif
stop
endif
exit
endif
if(i>nlive) then
write(*,*)"ERROR: live points file has greater than ",nlive," points."
write(*,*)"Aborting"
#ifdef MPI
call MPI_ABORT(MPI_COMM_WORLD,errcode)
#endif
stop
endif
enddo
close(u_live)
!read physical-live file
open(unit=u_phys,file=physname,status='old')
open(unit=u_phys_birth,file=physbirthname,status='old')
i=0
do
i=i+1
read(u_phys_birth,*,IOSTAT=iostatus) phyP(1:totPar,i),d1,l0(i),j
read(u_phys,*,IOSTAT=iostatus) phyP(1:totPar,i),d1,j
if(iostatus<0) then
i=i-1
if(i<nlive) then
write(*,*)"ERROR: phys live points file has less than ",nlive," points."
write(*,*)"Aborting"
#ifdef MPI
call MPI_ABORT(MPI_COMM_WORLD,errcode)
#endif
stop
endif
exit
endif
if(i>nlive) then
write(*,*)"ERROR: phys live points file has greater than ",nlive," points."
write(*,*)"Aborting"
#ifdef MPI
call MPI_ABORT(MPI_COMM_WORLD,errcode)
#endif
stop
endif
enddo
close(u_phys)
close(u_phys_birth)
!read the IS files
if( IS ) then
!read all the points collected so far
open(unit=u_IS(2),file=IS_Files(2),form='unformatted',access='sequential',status='old')
read(u_IS(2))j,IS_counter(2),k
call ExtendArrayIfRequired(IS_counter(1),j-IS_counter(1),IS_counter(3),IS_nstore,ndims+6,IS_allpts)
IS_counter(1) = j
IS_counter(7) = j
call ExtendArrayIfRequired(IS_counter(5),k-IS_counter(5),IS_counter(4),IS_nstore/10,5,IS_iterinfo)
IS_counter(5) = k
IS_counter(6) = k
do i = 1, IS_counter(1)
read(u_IS(2),IOSTAT=iostatus)IS_allpts(i,ndims+2),IS_allpts(i,ndims+3)