-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUDP_modules.f90
More file actions
780 lines (724 loc) · 23.4 KB
/
Copy pathUDP_modules.f90
File metadata and controls
780 lines (724 loc) · 23.4 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
! ##########################################################
! ### Modules for UDP clustering ###
! ##########################################################
! ### I edited ths to make it modular and python compatible
! ### starting on April 5th 2016
! ### I'm removing subroutines that I don't need (graph, noise, etc)
! ### on April 6th 2016.
! ###
! ###
! ### by Giovanni Pinamonti
! ### PhD student @ SISSA, Trieste
! ### Sector of Molecular and Statistical Biophysics
!
! Program that effectuates Cluster analysis in fortran
! Fully automated clustering by accurate non-parametric density estimation
! (D'Errico et al, publication pendent, 2015)
!
!
! TODO:: Define integer and real types in an accurate way (use KIND in a module)
! a) normal integer gives errors when il number of elements is big
! b) investigate the difference between real and real*8
module dp_clustering
implicit none
contains
!############################################
!### MAIN CLUSTERING ALGORITHM SUBROUTINE ###
!############################################
subroutine dp_advance(dist_mat,Cluster,Rho,Rho_err,Nlist,Nstar,Nele,id_err,sensibility,maxknn)
!$ use omp_lib
implicit none
integer,intent(in) :: maxknn ! maximum number of neighbours to explore
integer,intent(inout) :: id_err
!!Global variables
real*8,intent(in) :: dist_mat(Nele,maxknn) ! Distance matrix
integer,intent(in) :: Nele ! Number of elements
real*8, intent(in) :: sensibility
! These variables are used in densities calculation and then passed to clustering
real*8,intent(in) :: Rho(Nele) ! LOGARITHM of Density
real*8,intent(in) :: Rho_err(Nele) ! error on LOGARITHM of density
integer,intent(in) :: Nlist(Nele,maxknn) ! Neighbour list within dc
integer,intent(in) :: Nstar(Nele) ! N. of NN taken for comp dens
integer,allocatable :: Centers(:) ! Centers of the peaks
integer,intent(inout) :: Cluster(Nele) ! Cluster ID for the element
integer :: Nclus ! Number of Cluster
! These seems to be used for merging
real*8,allocatable :: Bord(:,:) ! Border Densities
real*8,allocatable :: Bord_err(:,:) ! Border Densities Error
real*8,allocatable :: cent(:) ! Center Density
real*8,allocatable :: cent_err(:) ! Center Error
! Underscore m implies data after automatic mergin
!integer,allocatable :: Cluster_m(:) ! Cluster ID for the element
integer :: Cluster_m(Nele) ! Cluster ID for the element
integer :: Nclus_m ! Number of Cluster merged
logical :: verbose=.FALSE.
id_err=0
if(verbose) write(*,*) 'clustering'
call clustering(id_err) ! get Clusters
if(Nclus.gt.1) then
if(sensibility.gt.0.0) then
if(verbose) write(*,*) 'finding borders'
call find_borders(id_err)
if(verbose) write(*,*) 'merging clusters'
call merging(id_err) ! Generate new clusters without peaks within border error
!Cluster=Cluster_m
!deallocate(Cluster_m)
endif
endif
return
contains
subroutine clustering(id_err)
implicit none
integer :: id_err
!! Local variables
integer :: i,j,k
integer :: ig
integer :: l
logical :: idmax
real*8,allocatable :: Rho_prob(:) ! Probability of having maximum Rho
real*8,allocatable :: Rho_copy(:)
integer,allocatable :: iRho(:),ordRho(:)
real*8 :: d,dmin
id_err=0
!! Identify centers: delta>dc eqv. Max(rho) within dc
Cluster(:)=0 !
Nclus=0
! Here I compute the probability of having density rho, g_i
allocate (Rho_prob(Nele))
Rho_prob(:)=0.
!
do i=1,Nele
Rho_prob(i)=Rho(i)-Rho_err(i)
enddo
!write(*,*) 'Rho_prob computed'
!
! copy of rho (not efficient, but clarifies the code) ### !!!
allocate (Rho_copy(Nele))
!write(*,*) Nele
allocate (iRho(Nele))
!write(*,*) iRho
Rho_copy(:)=-Rho_prob(:)
call HPSORT(Nele,Rho_copy,iRho) ! iRho contains the order in density (iRho(1) is the element with highest Rho...)
deallocate (Rho_copy)
allocate (ordRho(Nele))
do i=1,Nele
ordRho(iRho(i))=i ! ordRho is the complementary of iRho. Given an element, ordRho returns its order in density
enddo
! Now I'm getting the clusters
open(123,file="my_centers_premerge.dat")
do i=1,Nele
idmax=.true.
j=1
do while (idmax .and. (j.le.Nstar(i)))
if ((ordRho(i).gt.ordRho(Nlist(i,j)))) idmax=.false.
j=j+1
enddo
if (idmax) then
j=1
do while (idmax .and. (j.lt.ordRho(i)))
k=1
l=iRho(j)
do while (idmax.and.(k.le.Nstar(l)))
if (Nlist(l,k).eq.i) idmax=.false.
k=k+1
enddo
j=j+1
enddo
if (idmax) then
Nclus=Nclus+1
Cluster(i)=Nclus
endif
endif
write(123,*) Cluster(i)
enddo
close(123)
allocate (Centers(Nclus))
do i=1,Nele
if (Cluster(i).ne.0) then
Centers(Cluster(i))=i
endif
enddo
if (Nclus.lt.1) then
! TODO: I'm actually not sure this can happen
Cluster(:)=-1
id_err=9
return
endif
if (Nclus.eq.1) then
return
endif
! Assign points to clusters
do j=1,Nele
i=iRho(j)
if (Cluster(i).eq.0) then
ig=-1
dmin=9.9d9
!do k=1,Nstar(i)
do k=1,maxknn
l=Nlist(i,k)
if(Rho_prob(i).lt.Rho_prob(l)) then
if (dist_mat(i,k).le.dmin) then
ig=l
dmin=dist_mat(i,k)
endif
endif
enddo
if (ig.eq.-1) then
! ### TODO: check this
write(*,*) '*** ig=-1 *** unassigned points!'
id_err=12
RETURN
else
Cluster(i)=Cluster(ig)
endif
endif
enddo
open(1234,file='my_cluster_premerge.dat')
do i=1,Nele
write(1234,*) Cluster(i)
enddo
close(1234)
deallocate (Rho_prob)
deallocate (iRho)
deallocate(ordRho)
return
end subroutine clustering
!
subroutine find_borders(id_err)
integer :: id_err
!! Local variables
integer :: i,j,k
integer :: jj
integer :: ig
integer :: l,icl
integer :: eb(Nclus,Nclus) ! Border elements
real*8 :: d
real*8 :: dmin(Nclus)
integer :: imin(Nclus)
logical :: extend
logical :: viol,newass
integer :: Nnoass
! find border densities
!allocate (Bord(Nclus,Nclus),Bord_err(Nclus,Nclus),eb(Nclus,Nclus))
allocate (Bord(Nclus,Nclus),Bord_err(Nclus,Nclus))
Bord(:,:)=-9.9D9
Bord_err(:,:)=0.
eb(:,:)=0
!allocate (dmin(Nclus),imin(Nclus))
do i=1,Nele
ig=-1
dmin(:)=9.9d9 !### controlla questo
imin(:)=-1
do k=1,Nstar(i)
l=Nlist(i,k)
if (cluster(l).eq.cluster(i)) CYCLE
d=dist_mat(i,k)
if (d.lt.dmin(cluster(l))) then
dmin(cluster(l))=d
imin(cluster(l))=l
ig=1
endif
enddo
!if (dmin.gt.9.8d99) CYCLE
if (ig.eq.-1) CYCLE
extend=.true.
!if (ig.eq.-1) CYCLE
! id_err=12
! RETURN
!endif
do k=1,Nstar(i)
l=Nlist(i,k)
if(cluster(l).ne.cluster(i)) CYCLE
!d=9.9d99
d=9.9d9
do jj=1,maxknn
j=Nlist(l,jj)
ig=imin(cluster(j))
if (j.eq.ig) THEN
d=dist_mat(l,jj)
if(d.lt.dmin(cluster(j))) imin(cluster(j))=-1
ENDIF
enddo
!if (d.lt.dmin) then
! extend=.false.
! EXIT
!endif
! ### TODO: here one might want to add a check to see if the cycle over the NN of i can be stopped
enddo ! k
do icl=1,Nclus
ig=imin(icl)
if (ig.gt.-1) then
if ((Rho(i)-Rho_err(i)).gt. Bord(cluster(i),icl)) then
Bord(cluster(i),icl)=Rho(i)-Rho_err(i)
Bord(icl,cluster(i))=Rho(i)-Rho_err(i)
Bord_err(cluster(i),icl)=Rho_err(i)
Bord_err(icl,cluster(i))=Rho_err(i)
eb(cluster(i),icl)=i
eb(icl,cluster(i))=i
endif
endif
enddo !icl
enddo ! i=1,Nele
open (22,file="my_borders_premerge.dat")
do i=1,Nclus-1
do j=i+1,Nclus
if (eb(i,j).ne.0) then
Bord(i,j)=Rho(eb(i,j))
Bord(j,i)=Rho(eb(i,j))
else
Bord(i,j)=0.
Bord(j,i)=0.
endif
write (22,'(i6,1x,i6,1x,i6,1x,es18.10,1x,es18.10)') i,j,eb(i,j),Bord(i,j),Bord_err(i,j)
enddo
enddo
close(22)
! Info per graph pre automatic merging
allocate (cent(Nclus))
allocate (cent_err(Nclus))
do i=1,Nclus
cent(i)=Rho(Centers(i))
cent_err(i)=Rho_err(Centers(i))
enddo
!deallocate(dmin)
!deallocate(imin)
!deallocate(eb)
return
end subroutine find_borders
!
subroutine merging(id_err)
implicit none
integer :: id_err
!! Local variables
integer :: i,j,k,n,l,alive,dead,niter ! niter is useless
logical :: Survive (Nclus)
logical :: change
integer :: Nbarr
!integer :: Nbarr=(Nclus*Nclus-Nclus)/2
integer :: Bcorr ((Nclus*Nclus-Nclus)/2,2)
real*8 :: Barrier ((Nclus*Nclus-Nclus)/2)
real*8 :: Barrier_err ((Nclus*Nclus-Nclus)/2)
integer :: iBarrier((Nclus*Nclus-Nclus)/2)
real*8 :: c1,c2,b12,b1,b2,f1,f2,f12
integer,allocatable :: M2O(:) !conversion from merged to original cluster number
integer :: O2M(Nclus) ! Conversion from original cluster number to its equivalent in merged
id_err=0
Nbarr=(Nclus*Nclus-Nclus)/2 ! n. of contacts between clusters
!allocate (Barrier(Nbarr))
!allocate (Barrier_err(Nbarr))
!allocate (iBarrier(Nbarr))
!allocate (Bcorr(Nbarr,2))
n=0
do i=1,Nclus-1
do j=i+1,Nclus
n=n+1
Barrier(n)= Bord(i,j)
Barrier_err(n)= Bord_err(i,j)
Bcorr(n,1)=i
Bcorr(n,2)=j
enddo
enddo
if (Nbarr.gt.1) then
call HPSORT(Nbarr,Barrier,iBarrier)
else
iBarrier(1)=1
endif
Survive(:)=.true.
change=.true.
!allocate (Cluster_m(Nele))
Cluster_m(:)=Cluster(:) !### this is probably useless!
niter=0
do while (change)
niter=niter+1
change=.false.
mdo: do n=Nbarr,1,-1
k=iBarrier(n)
i=Bcorr(k,1)
j=Bcorr(k,2)
if ((Bord(i,j).gt.0.).and.(i.ne.j)) then
if (Survive(i).and.Survive(j)) then
c1=(cent(i)-Bord(i,j))/(Bord_err(i,j)+cent_err(i))
c2=(cent(j)-Bord(i,j))/(Bord_err(i,j)+cent_err(j))
!c1=cent(i)-sensibility*cent_err(i)
!c2=cent(j)-sensibility*cent_err(j)
!b12=Bord(i,j)+sensibility*Bord_err(i,j)
b12=min(c1,c2)
f1=cent(i)
f2=cent(j)
f12=Bord(i,j)
b1=abs(f12-f1)
b2=abs(f12-f2)
if ((b12.lt.sensibility).or.(b1.lt.0.0).or.(b2.lt.0.0)) then
!if ((c1.lt.b12).or.(c2.lt.b12)) then
change=.true.
Bord(i,j)=0.
Bord_err(i,j)=0.
if (c1.gt.c2) then
alive=i
dead=j
else
alive=j
dead=i
endif
Bord(alive,alive)=0.
Bord_err(alive,alive)=0.
Survive(dead)=.false.
do k=1,Nclus
if (Survive(k)) then
if (Bord(i,k).gt.Bord(j,k)) then
Bord(alive,k)=Bord(i,k)
Bord(k,alive)=Bord(k,i)
Bord_err(alive,k)=Bord_err(i,k)
Bord_err(k,alive)=Bord_err(k,i)
else
Bord(alive,k)=Bord(j,k)
Bord(k,alive)=Bord(k,j)
Bord_err(alive,k)=Bord_err(j,k)
Bord_err(k,alive)=Bord_err(k,j)
endif
endif
enddo
do l=1,Nele
if (Cluster_m(l).eq.dead) Cluster_m(l)=alive
enddo
if (n.gt.1) then
do l=n-1,1,-1
k=iBarrier(l)
if (Bcorr(k,1).eq.dead) Bcorr(k,1)=alive
if (Bcorr(k,2).eq.dead) Bcorr(k,2)=alive
enddo
endif
exit mdo
endif
endif
endif
enddo mdo
enddo !while change
! get dictionary
Nclus_m=0
do i=1,Nclus
if (Survive(i)) Nclus_m=Nclus_m+1
enddo
allocate (M2O(Nclus_m))
n=0
O2M(:)=-1
do i=1,Nclus
if (Survive(i)) then
n=n+1
M2O(n)=i
O2M(i)=n
endif
enddo
if (Nclus_m.eq.1) then
Cluster_m(:)=1
return
endif
if (Nclus_m.lt.1) then
! TODO: not sure this can happen
id_err=10
Cluster_m(:)=-1
return
endif
! get survival characteristics
do i=1,Nele
Cluster_m(i)=O2M(Cluster_m(i))
enddo
Cluster=Cluster_m
deallocate(Bord)
deallocate(Bord_err)
deallocate(cent)
deallocate(cent_err)
!deallocate(Barrier)
!deallocate(Barrier_err)
!deallocate(iBarrier)
!deallocate(Bcorr)
deallocate(M2O)
return
end subroutine merging
!
end subroutine dp_advance
subroutine get_densities(id_err,dist_mat,Nele,dim,Rho,Rho_err,Nlist,Nstar,maxknn)
!$ use omp_lib
implicit none
integer,intent(in) :: maxknn ! maximum number of neighbours to explore
real*8,intent(in) :: dist_mat(Nele,maxknn) !
integer,intent(in) :: Nele ! Number of elements
integer,intent(in) :: dim ! integer of dimset (avoid real*8 calc)
real*8,intent(inout) :: Rho(Nele) ! Density
real*8,intent(inout) :: Rho_err(Nele) ! Density error
integer,intent(inout) :: Nlist(Nele,maxknn) ! Neighbour list within dc ### dimension 2 maybe can be reduced but maybe not (before was=limit)
integer,intent(inout) :: Nstar(Nele) ! N. of NN taken for comp dens
!internal variables
integer :: i
real*8 :: F
integer :: id_err
real*8 :: Vols(Nele,maxknn)
real*8 :: min_F
id_err=0
Vols=prefactor(dim)*dist_mat**dim
call get_k(id_err,Vols,Nele,dim,Nlist,Nstar,maxknn)
min_F=1.0
do i=1,Nele
F=free_energy(Nstar(i),Vols(i,:))
!Rho(i)=exp(-F)
Rho(i)=F ! I'm computing the log of density...
if (F.lt.min_F) min_F=F
Rho_err(i)=dsqrt(dfloat(4*Nstar(i)+2)/dfloat(Nstar(i)*(Nstar(i)-1))) ! I can do this outside!
enddo
Rho(:)=Rho(:)-min_F+1.0 ! This way the minimum Rho is equal to 1. (for hierarchies it does not matter so much)
return
end subroutine get_densities
subroutine get_k(id_err,Vols,Nele,dim,Nlist,Nstar,maxknn)
!$ use omp_lib
implicit none
integer,intent(in) :: maxknn ! maximum number of neighbours to explore
integer,parameter :: minknn=4 ! minimum number of neighbours to explore
!!Global variables
integer,intent(in) :: Nele ! Number of elements
integer,intent(in) :: dim ! integer of dimset (avoid real*8 calc)
integer,intent(inout) :: Nlist(Nele,maxknn) ! Neighbour list within dc ### dimension 2 maybe can be reduced but maybe not
integer,intent(inout) :: Nstar(Nele) ! N. of NN taken for comp dens
real*8, intent(in) :: Vols(Nele,maxknn) ! the ordered spherical volumes corresponding to nn radii
integer :: id_err
!! Local variables
integer :: limit
integer :: i,j,k
real*8 :: Dk
id_err=0
limit=min(maxknn,int(Nele/2))
do i=1,Nele
! ### get nstar
do k=minknn,maxknn-1
j=Nlist(i,k+1)
Dk= -2*k*( log(Vols(i,k)*Vols(j,k)/(Vols(i,k)+Vols(j,k))**2) + log(4.) )
if (Dk.gt.23.928) then
exit
endif
if (k.gt.limit-1) exit
enddo
Nstar(i)=k ! ### ha senso?
if (Nstar(i).lt.minknn) Nstar(i)=minknn ! ### puo' succedere..?
enddo
return
end subroutine get_k
real*8 function free_energy(Nstar,VV)
! This function computes the optimal "Free energy" aka -log(Rho)
! using the procedure described in Rodriguez et al. JCTC 2018
!
implicit none
integer, intent(in) :: Nstar
integer :: id_err
!! Local variables
integer :: i,j,k,m
integer :: niter
real*8,intent(in) :: VV(:)
real*8 :: dL
real*8 :: vi(Nstar)
logical :: viol
real*8 :: a,F
real*8 :: H(2,2),Hinv(2,2)
real*8 :: func,sigma,t,jf
real*8 :: tt,gb,ga,sa,sb,a_err
real*8 :: stepmax !! This variable controls the maximum variation on the log(rho) accepted during the optimization process
real*8 :: r
! Variables Specific of variable kernel
real*8 :: g
! Variable that accounts for vi=0 --> then use kNN instead for density (error
! still PAk)
logical :: kNN
!real*8 :: Rho_err
! ### Here I'm computing the volumes of the spherical shells between neighbors
Vi(1)=VV(1)
kNN=.false.
do j=2,Nstar
Vi(j)=VV(j)-VV(j-1)
if ((Vi(j).eq.0).and.(.not.kNN)) then !Can this really happen? What does it mean?
write (*,*) "Point density estimated with k-NN" ! Am I really not doing anything if there are 2 neighbors at the same distance??
kNN=.true.
endif
enddo
F=log(float(Nstar)/VV(Nstar)) ! Starting guess for F
a=0. !
if (.not.kNN) then
stepmax=0.1*abs(F)
!H=Hessian(a,b,Nstar)
gb=float(Nstar)
ga=float(Nstar+1)*float(Nstar)/2.
H(1,1)=0.
H(1,2)=0.
H(2,2)=0.
do j=1,Nstar
jf=float(j)
tt=vi(j)*exp(F+a*jf)
gb=gb-tt
ga=ga-jf*tt
H(1,1)=H(1,1)-tt
H(1,2)=H(1,2)-jf*tt
H(2,2)=H(2,2)-jf*jf*tt
enddo
H(2,1)=H(1,2)
Hinv=matinv2(H)
func=100.
niter=0
do while ((func>1D-3).and.(niter.lt.1000))
sb=(Hinv(1,1)*gb+Hinv(1,2)*ga)
sa=(Hinv(2,1)*gb+Hinv(2,2)*ga)
niter=niter+1
sigma=0.1
if (abs(sigma*sb).gt.stepmax) then
sigma=abs(stepmax/sb)
endif
F=F-sigma*sb
a=a-sigma*sa
gb=float(Nstar)
ga=float(Nstar+1)*float(Nstar)/2.
H(1,1)=0.
H(1,2)=0.
H(2,2)=0.
do j=1,Nstar
jf=float(j)
tt=vi(j)*exp(F+a*jf)
gb=gb-tt
ga=ga-jf*tt
H(1,1)=H(1,1)-tt
H(1,2)=H(1,2)-jf*tt
H(2,2)=H(2,2)-jf*jf*tt
enddo
H(2,1)=H(1,2)
Hinv=matinv2(H)
if ((abs(a).le.tiny(a)).or.(abs(F).le.tiny(F))) then
func=max(gb,ga)
else
func=max(abs(gb/F),abs(ga/a))
endif
enddo
H(:,:)=-H(:,:)
Hinv=matinv2(H)
a_err=sqrt(Hinv(2,2))
endif !.not.kNN
! ### I should probably move this outside of the function...
free_energy=F
if (ISNAN(free_energy)) then
write (*,*) "Density NaN at point",i
return
endif
if ((free_energy.gt.huge(free_energy)).or.(free_energy.lt.-huge(free_energy))) then
write (*,*) "Density at point", free_energy
return
endif
!Rho_err=dsqrt(dfloat(4*Nstar+2)/dfloat(Nstar*(Nstar-1))) ! I can do this outside!
return
end function free_energy
pure function Hessian_L(a,F,Nstar,vi) result(H)
! Computes the Hessian of the log-likelihood
! L(F,A|{v_i,l}_l<=Nstar)
! as in Eq. 4 of Rodriguez et al. JCTC 2018
real*8 :: H(2,2) !the Hessian
real*8, intent(in) :: a,F
integer :: j
integer, intent(in) ::Nstar
real*8, intent(in):: vi(Nstar)
real*8 :: jf,tt
H(1,1)=0. !gbb
H(1,2)=0. !gab
H(2,2)=0. !gaa
do j=1,Nstar
jf=float(j)
tt=vi(j)*exp(F+a*jf)
H(1,1)=H(1,1)-tt
H(1,2)=H(1,2)-jf*tt
H(2,2)=H(2,2)-jf*jf*tt
enddo
H(2,1)=H(1,2)
end function Hessian_L
pure function matinv2(A) result(B)
!! Performs a direct calculation of the inverse of a 2×2 matrix.
real*8, intent(in) :: A(2,2) !! Matrix
real*8 :: B(2,2) !! Inverse matrix
real*8 :: detinv
! Calculate the inverse determinant of the matrix
detinv = 1/(A(1,1)*A(2,2) - A(1,2)*A(2,1))
! Calculate the inverse of the matrix
B(1,1) = +detinv * A(2,2)
B(2,1) = -detinv * A(2,1)
B(1,2) = -detinv * A(1,2)
B(2,2) = +detinv * A(1,1)
end function matinv2
real*8 function prefactor(dim)
! get prefactor for Volume calculation
integer, intent(in) :: dim
integer :: k,m,i,ms,ns
real*8, parameter :: pi=3.14159265359
if (mod(dim,2).eq.0) then
k=dim/2
m=1
do i=1,k
m=m*i
enddo
prefactor=pi**k/(dfloat(m))
else
k=(dim-1)/2
ms=0.
do i=1,k
ms=ms+dlog(dfloat(i))
enddo
ns=ms
do i=k+1,dim
ns=ns+dlog(dfloat(i))
enddo
prefactor=2.*dexp(ms-ns+k*dlog(4*pi))
endif
return
end function prefactor
SUBROUTINE HPSORT(N,RA,s_order)
implicit none
integer N,s_order(N)
real*8 RA(N)
integer L,IR,I,J,sord
real*8 RRA
do i=1,n
s_order(i)=i
enddo
L=N/2+1
IR=N
!The index L will be decremented from its initial value during the
!"hiring" (heap creation) phase. Once it reaches 1, the index IR
!will be decremented from its initial value down to 1 during the
!"retirement-and-promotion" (heap selection) phase.
10 continue
if(L > 1)then
L=L-1
RRA=RA(L)
sord=s_order(L)
else
RRA=RA(IR)
sord=s_order(IR)
RA(IR)=RA(1)
s_order(ir)=s_order(1)
IR=IR-1
if(IR.eq.1)then
RA(1)=RRA
s_order(1)=sord
return
end if
end if
I=L
J=L+L
20 if(J.le.IR)then
if(J < IR)then
if(RA(J) < RA(J+1)) J=J+1
end if
if(RRA < RA(J))then
RA(I)=RA(J)
s_order(i)=s_order(j)
I=J; J=J+J
else
J=IR+1
end if
goto 20
end if
RA(I)=RRA
s_order(I)=sord
goto 10
END subroutine HPSORT
end module dp_clustering