-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbasin.f90
More file actions
4080 lines (3929 loc) · 151 KB
/
basin.f90
File metadata and controls
4080 lines (3929 loc) · 151 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
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
!!------ Forming basin for specific real space function, and integrate real space functions in the basins
!!------ The method is adapted from J. Phys.: Condens. Matter 21 (2009) 084204
!Grid must be ortho
subroutine basinana
use defvar
use basinintmod
use util
use GUI
implicit real*8(a-h,o-z)
integer walltime1,walltime2
integer :: igridmethod=3
integer,allocatable :: attconvold(:),realattconv(:),usercluslist(:),tmparr(:)
character basinfilename*200,selectyn,c80tmp*80,ctmp1*20,ctmp2*20,ctmp3*20,ctmp4*20,c2000tmp*2000,c200tmp*200
real*8 :: threslowvalatt=1D-5
real*8,allocatable :: tmparrf(:)
ishowattlab=1
ishowatmlab=0
ishowatt=1
!Don't show topology analysis results to avoid confusion
ishowCPlab=0
ishowpathlab=1
ishow3n3=0
ishow3n1=0
ishow3p1=0
ishow3p3=0
!When enter this module, we reset the whole state, which is signified by numatt. Because the user may calculate grid data at external functions,
!and hence messed up grid setting (orgx/y/z,dx/y/z...), so all of the functions in this module that rely on grid setting will be totally wrong
numatt=0
call delvirorb(1)
do while(.true.)
write(*,*)
write(*,*) " ============= Basin analysis ============="
write(*,*) "-10 Return to main menu"
if (numatt>0) write(*,*) "-6 Set parameter for attractor clustering or manually perform clustering"
if (numatt==0) write(*,*) "-6 Set parameter for attractor clustering"
if (numatt>0) write(*,*) "-5 Export basins as cube file"
if (numatt>0) write(*,*) "-4 Export attractors as pdb/pqr/txt file"
if (numatt>0) write(*,*) "-3 Show information of attractors"
if (numatt>0) write(*,*) "-2 Measure distances, angles and dihedral angles between attractors or atoms"
if (igridmethod==1) write(*,*) "-1 Select the method for generating basins, current: On-grid"
if (igridmethod==2) write(*,*) "-1 Select the method for generating basins, current: Near-grid"
if (igridmethod==3) write(*,*) "-1 Select the method for generating basins, current: Near-grid with refinement"
if (numatt>0) write(*,*) " 0 Visualize attractors and basins"
if (numatt>0) then
write(*,*) " 1 Regenerate basins and relocate attractors"
else
write(*,*) " 1 Generate basins and locate attractors"
end if
if (numatt>0) then
write(*,*) " 2 Integrate real space functions in the basins"
write(*,*) " 3 Calculate electric multipole moments in the basins"
write(*,*) " 4 Calculate localization index and delocalization index for the basins"
write(*,*) " 5 Output orbital overlap matrix in basins to BOM.txt in current folder"
! write(*,*) " 100 Integrate real space functions in the basins with multi-level refinement"
if (ifuncbasin==1) then
write(*,*) " 6 Output orbital overlap matrix in atoms to AOM.txt in current folder"
write(*,*) " 7 Integrate real space functions in AIM basins with mixed type of grids"
write(*,*) " 8 Calculate electric multipole moments in AIM basins with mixed type of grids"
write(*,*) " 9 Obtain atomic contribution to population of external basins"
else if (ifuncbasin==9) then
write(*,"(a)") " 10 Calculate high ELF localization domain population and volume (HELP, HELV)"
end if
write(*,*) "11 Calculate orbital compositions contributed by various basins"
end if
read(*,*) isel
if (numatt==0.and.(isel==-5.or.isel==-4.or.isel==-3.or.isel==-2.or.isel==0.or.isel==2.or.isel==3.or.isel==4.or.isel==5)) then
write(*,*) "Error: You should use option 1 to generate basins first!"
cycle
end if
if (isel==-10) then
idrawbasinidx=-10 !Don't display interbasin in any other GUI
return
else if (isel==-6) then
do while(.true.)
write(*,*) "0 Return"
write(*,"(a,f14.5,a)") " 1 Set relative value difference criterion, current:",valcritclus*100,"%"
write(*,"(a,i3)") " 2 Set the multiplier for distance criterion, current:",mergeattdist
if (numatt>=2) write(*,*) "3 Cluster specified attractors"
read(*,*) isel2
if (isel2==0) then
exit
else if (isel2==1) then
write(*,"(a)") " Input a value in percentage, e.g. 0.5"
write(*,"(a)") " Note: Inputting X means if the value difference between two attractors is less than X%, &
then they will be regarded as degenerate and may be clustered according to distance criterion"
read(*,*) valcritclus
valcritclus=valcritclus/100D0
else if (isel2==2) then
write(*,"(a)") " Input a value, e.g. 6"
write(*,"(a)") " Note: Inputting P means for any two attractors that have relative value difference less than the one set by option 1, &
if their interval is less than P*sqrt(dx^2+dy^2+dz^2), &
where dx,dy,dz are grid spacing in X,Y,Z,then they will be clustered together. If you want to nullify the clustering step after generating &
basins, simply set this value to 0"
read(*,*) mergeattdist
else if (isel2==3) then
if (numatt<2) then
write(*,*) "Error: At least two attractors must be presented!"
cycle
end if
write(*,*) "Input the attractors you want to cluster, e.g. 4,5,8,9,22,21"
read(*,"(a)") c2000tmp
call str2arr(c2000tmp,ntobeclus) !Find how many terms
if (allocated(attconvold)) deallocate(attconvold,realattconv,usercluslist)
allocate(attconvold(-2:numatt),realattconv(-2:numrealatt),usercluslist(ntobeclus))
realattconv(-2)=-2
realattconv(-1)=-1
realattconv(0)=0
call str2arr(c2000tmp,ntobeclus,usercluslist)
call sort(usercluslist)
attconvold=attconv
idesrealatt=usercluslist(1)
do itmp=2,ntobeclus
irealatt=usercluslist(itmp)
do jtmp=1,nrealatthas(irealatt)
iatt=realatttable(irealatt,jtmp)
attconv(iatt)=idesrealatt
end do
end do
call clusdegenatt(1)
realattconv(1)=1
do itmp=1,numatt !Build conversion relationship between previous real attractors and new real attractors
if (itmp/=1) then
if (attconvold(itmp)/=attconvold(itmp-1)) realattconv(attconvold(itmp))=attconv(itmp)
end if
end do
do iz=2,nz-1
do iy=2,ny-1
do ix=2,nx-1
gridbas(ix,iy,iz)=realattconv(gridbas(ix,iy,iz))
end do
end do
end do
call detectinterbasgrd(6) !Detect interbasin grids
write(*,*) "Done!"
write(*,*)
end if
end do
else if (isel==-5) then !Output cube file for basins for visualizing interbasin surface
write(*,"(a)") " Inputting e.g. 4,6-9,15,17 will output these basins to individual basin[index].cub files to current folder"
write(*,"(a)") " Inputting ""a"" will output all basins into basin.cub in current folder, the grid value corresponds to basin index"
read(*,"(a)") c2000tmp
if (index(c2000tmp,'a')==0) then
call str2arr(c2000tmp,ntmp)
if (allocated(tmparr)) deallocate(tmparr)
allocate(tmparr(ntmp))
call str2arr(c2000tmp,ntmp,tmparr)
if (any(tmparr<=0).or.any(tmparr>numatt)) then
write(*,*) "Error: One or more basin indices exceed valid range!"
write(*,*) "Press ENTER button to continue"
read(*,*)
cycle
end if
write(*,"(a)") " Select how to output cube file. The grid value in the select and unselected region will be 1.0 and 0.0, respectively"
write(*,*) "1 Output all basin grids"
write(*,*) "2 Output boundary basin grids"
write(*,*) "3 Output all basin grids where electron density > 0.001 a.u."
read(*,*) igrid
if (allocated(cubmattmp)) deallocate(cubmattmp)
allocate(cubmattmp(nx,ny,nz))
do idx=1,ntmp
ibas=tmparr(idx)
write(basinfilename,"('basin',i4.4,'.cub')") ibas
write(*,"(' Outputting basin',i8,' as ',a)") ibas,trim(basinfilename)
cubmattmp=gridbas(:,:,:)
where(cubmattmp/=ibas)
cubmattmp=0
elsewhere
cubmattmp=1
end where
if (igrid==1) then
continue
else if (igrid==2) then
where (.not.interbasgrid) cubmattmp=0
else if (igrid==3) then
if (ifuncbasin==1) then !The cubmat already records electron density
where(cubmat<0.001D0) cubmattmp=0
else
call saverhocub
where(rhocub<0.001D0) cubmattmp=0
end if
end if
open(10,file=basinfilename,status="replace")
call outcube(cubmattmp,nx,ny,nz,orgx,orgy,orgz,gridvec1,gridvec2,gridvec3,10)
close(10)
end do
deallocate(cubmattmp)
write(*,"(a)") " Done! Cube files for the selected basins have been outputted to current folder"
if (selectyn=='y'.or.selectyn=='Y') then
write(*,"(a)") " Values 1 and 0 in these files indicate that the corresponding point belongs and not belongs to the basin, &
respectively. You can plot isosurface with isovalue=0.5 to visualize basin region"
else
write(*,"(a)") " You can plot isosurface with isovalue=0.5 to visualize the basin surface"
end if
else
if (allocated(cubmattmp)) deallocate(cubmattmp)
allocate(cubmattmp(nx-2,ny-2,nz-2))
cubmattmp=gridbas(2:nx-1,2:ny-1,2:nz-1)
open(10,file="basin.cub",status="replace")
call outcube(cubmattmp,nx-2,ny-2,nz-2,orgx+dx,orgy+dy,orgz+dz,gridvec1,gridvec2,gridvec3,10)
close(10)
deallocate(cubmattmp)
write(*,"(a)") " Done! basin.cub has been outputted to current folder. The grid values correspond to basin index"
end if
else if (isel==-4) then
write(*,*) "0 Return"
write(*,*) "1 Export coordinates of all attractors as attractors.pdb"
write(*,"(a)") " 2 Export coordinates and function values of all attractors as attractors.pqr"
write(*,"(a)") " 3 Export coordinates and function values of all attractors as attractors.txt"
read(*,*) isel2
if (isel2==0) then
continue
else if (isel2==1) then
open(10,file="attractors.pdb",status="replace")
do iatt=1,numatt
write(10,"(a6,i5,1x,a4,1x,a3, 1x,a1,i4,4x,3f8.3,2f6.2,10x,a2)") "HETATM",iatt,' '//"C "//' ',"ATT",'A',attconv(iatt),attxyz(iatt,:)*b2a,1.0,0.0,"C "
end do
close(10)
write(*,*) "Done, all attractors have been exported to attractors.pdb in current folder"
write(*,"(a)") " Note: The residue indices in the pdb file correspond to the attractor indices after clustering, &
while the atomic indices correspond to the raw attractor indices"
else if (isel2==2) then
open(10,file="attractors.pqr",status="replace")
do iatt=1,numatt
write(10,"(a6,i5,1x,a4,1x,a3, 1x,a1,i4,4x,3f8.3,E18.8E3,f5.2,1x,a2)") "HETATM",iatt,' '//"C "//' ',"ATT",'A',attconv(iatt),attxyz(iatt,:)*b2a,attval(iatt),1.0,"C "
end do
close(10)
write(*,*) "Done, all attractors have been exported to attractors.pqr in current folder"
write(*,"(a)") " Note: The residue indices in the pqr file correspond to the attractor indices after clustering, &
while the atomic indices correspond to the raw attractor indices. The atomic charge column corresponds to function value"
else if (isel2==3) then
open(10,file="attractors.txt",status="replace")
do iatt=1,numatt
write(10,"(3f12.6,1PE18.8E3)") attxyz(iatt,:),attval(iatt)
end do
close(10)
write(*,*) "Done, all attractors have been exported to attractors.txt in current folder"
write(*,"(a)") " In this file, the first three columns correspond to X,Y,Z coordinate in Bohr, the final column is function value"
end if
else if (isel==-3) then
write(*,*) " Attractor X,Y,Z coordinate (Angstrom) Value"
do irealatt=1,numrealatt
write(*,"(i8,3f14.8,1E18.9)") irealatt,realattxyz(irealatt,1:3)*b2a,realattval(irealatt)
end do
do irealatt=1,numrealatt
if (nrealatthas(irealatt)/=1) then
write(*,*)
write(*,"(' The members of degenerate attractor',i6,':')") irealatt
do itmp=1,nrealatthas(irealatt)
iatt=realatttable(irealatt,itmp)
write(*,"(i8,' XYZ:',3f13.7,' Value:',1E18.9)") iatt,attxyz(iatt,:)*b2a,attval(iatt)
end do
end if
end do
if (numrealatt>1) then
write(*,"(/,a)") " Do you also want to print the attractor information after sorting according to values? (y/n)"
read(*,*) selectyn
if (selectyn=='y'.or.selectyn=='Y') then
if (allocated(tmparr)) deallocate(tmparr)
allocate(tmparr(numrealatt))
forall(i=1:numrealatt) tmparr(i)=i
allocate(tmparrf(numrealatt))
tmparrf=realattval
call sortr8(tmparrf,"val",tmparr)
write(*,*) " Attractor X,Y,Z coordinate (Angstrom) Value"
do idx=1,numrealatt
irealatt=tmparr(idx)
write(*,"(i8,3f14.8,1E18.9)") irealatt,realattxyz(irealatt,1:3)*b2a,realattval(irealatt)
end do
deallocate(tmparrf,tmparr)
end if
end if
else if (isel==-2) then
write(*,*) "q = Quit. Selection method:"
write(*,*) "a? = Atom ?"
write(*,*) "c? = Attractor ? (If the attractor is degenerate, average coordinate is used)"
write(*,*) "d? = Attractor ? (Only for degenerate attractors, cycle each of its members)"
write(*,*) "For example:"
write(*,*) """a1 c3"" returns the distance between atom1 and att.3"
write(*,*) """a4 a2"" returns the distance between atom4 and atom2"
write(*,*) """c6 a2 a5"" returns the angle of att.6-atom2-atom5"
write(*,*) """c2 c4 a3 c7"" returns the dihedral angle of att.2-att.4-atom3-att.7"
write(*,*) """d3 a1"" returns the distance between members of att.3 and atom1"
write(*,"(a)") " ""d3 a1 c2"" returns the the angle of (members of att.3)--atom1--att.2, &
meanwhile outputs the vertical distance from (members of att.3) to the line linking atom1 and att.2"
do while(.true.)
read(*,"(a)") c80tmp
c80tmp=adjustl(c80tmp)
imeasure=0
do ichar=1,len_trim(c80tmp) !imeasure=1/2/3: measure distance,angle,dihedral
if (c80tmp(ichar:ichar)==','.or.c80tmp(ichar:ichar)==' ') imeasure=imeasure+1
end do
nelement=0 !Validate "d" selection
do ichar=1,len_trim(c80tmp)
if (c80tmp(ichar:ichar)=='d') nelement=nelement+1
end do
if (nelement/=0) then
if (c80tmp(1:1)/='d'.or.nelement>1) then
write(*,*) "Error: ""d"" type of selection can only occur once and must be the first term"
cycle
else if (imeasure==3) then
write(*,*) "Error: Dihedral angle calculation does not support ""d"" type of selection"
cycle
end if
end if
if (c80tmp(1:1)=='q') then
exit
else if (imeasure==1.or.imeasure==2.or.imeasure==3) then
if (imeasure==1) read(c80tmp,*) ctmp1,ctmp2 !Read two terms
if (imeasure==2) read(c80tmp,*) ctmp1,ctmp2,ctmp3 !Read three terms
if (imeasure==3) read(c80tmp,*) ctmp1,ctmp2,ctmp3,ctmp4 !Read four terms
if (ctmp1(1:1)=='a') then
read(ctmp1(2:),*) iatm
tmpx1=a(iatm)%x
tmpy1=a(iatm)%y
tmpz1=a(iatm)%z
else if (ctmp1(1:1)=='c') then
read(ctmp1(2:),*) irealatt
tmpx1=realattxyz(irealatt,1)
tmpy1=realattxyz(irealatt,2)
tmpz1=realattxyz(irealatt,3)
else if (ctmp1(1:1)=='d') then
read(ctmp1(2:),*) irealattde
end if
if (ctmp2(1:1)=='a') then
read(ctmp2(2:),*) iatm
tmpx2=a(iatm)%x
tmpy2=a(iatm)%y
tmpz2=a(iatm)%z
else if (ctmp2(1:1)=='c') then
read(ctmp2(2:),*) irealatt
tmpx2=realattxyz(irealatt,1)
tmpy2=realattxyz(irealatt,2)
tmpz2=realattxyz(irealatt,3)
end if
if (imeasure==1) then
if (ctmp1(1:1)/='d') then
write(*,"(' The distance is',f12.6,' Bohr (',f12.6 ' Angstrom)')") &
dsqrt((tmpx1-tmpx2)**2+(tmpy1-tmpy2)**2+(tmpz1-tmpz2)**2),dsqrt((tmpx1-tmpx2)**2+(tmpy1-tmpy2)**2+(tmpz1-tmpz2)**2)*b2a
else if (ctmp1(1:1)=='d') then
distmin=9999999
distmax=-1
distavg=0
do itmp=1,nrealatthas(irealattde)
iatt=realatttable(irealattde,itmp)
tmpx1=attxyz(iatt,1)
tmpy1=attxyz(iatt,2)
tmpz1=attxyz(iatt,3)
distnow=dsqrt((tmpx1-tmpx2)**2+(tmpy1-tmpy2)**2+(tmpz1-tmpz2)**2)
distavg=distavg+distnow
if (distnow<distmin) distmin=distnow
if (distnow>distmax) distmax=distnow
end do
distavg=distavg/nrealatthas(irealattde)
write(*,"(' Note: Attractor',i6,' has',i6,' members')") irealattde,nrealatthas(irealattde)
write(*,"(' The minimum distance is',f12.6,' Bohr (',f12.6 ' Angstrom)')") distmin,distmin*b2a
write(*,"(' The maximum distance is',f12.6,' Bohr (',f12.6 ' Angstrom)')") distmax,distmax*b2a
write(*,"(' The average distance is',f12.6,' Bohr (',f12.6 ' Angstrom)')") distavg,distavg*b2a
end if
end if
if (imeasure==2.or.imeasure==3) then !Analyze one more term, then print angle
if (ctmp3(1:1)=='a') then
read(ctmp3(2:),*) iatm
tmpx3=a(iatm)%x
tmpy3=a(iatm)%y
tmpz3=a(iatm)%z
else if (ctmp3(1:1)=='c') then
read(ctmp3(2:),*) irealatt
tmpx3=realattxyz(irealatt,1)
tmpy3=realattxyz(irealatt,2)
tmpz3=realattxyz(irealatt,3)
end if
end if
if (imeasure==2) then
if (ctmp1(1:1)/='d') then
write(*,"(' The angle is',f12.6,' degree')") xyz2angle(tmpx1,tmpy1,tmpz1,tmpx2,tmpy2,tmpz2,tmpx3,tmpy3,tmpz3)
else if (ctmp1(1:1)=='d') then
angmin=180
angmax=-1
angavg=0
distmin=999999
distmax=-1
distavg=0
prjx=0
prjy=0
prjz=0
do itmp=1,nrealatthas(irealattde)
iatt=realatttable(irealattde,itmp)
tmpx1=attxyz(iatt,1)
tmpy1=attxyz(iatt,2)
tmpz1=attxyz(iatt,3)
angnow=xyz2angle(tmpx1,tmpy1,tmpz1,tmpx2,tmpy2,tmpz2,tmpx3,tmpy3,tmpz3)
angavg=angavg+angnow
if (angnow<angmin) angmin=angnow
if (angnow>angmax) angmax=angnow
distnow=potlinedis(tmpx1,tmpy1,tmpz1,tmpx2,tmpy2,tmpz2,tmpx3,tmpy3,tmpz3)
distavg=distavg+distnow
if (distnow<distmin) distmin=distnow
if (distnow>distmax) distmax=distnow
call pointprjline(tmpx1,tmpy1,tmpz1,tmpx2,tmpy2,tmpz2,tmpx3,tmpy3,tmpz3,prjxtmp,prjytmp,prjztmp)
prjx=prjx+prjxtmp
prjy=prjy+prjytmp
prjz=prjz+prjztmp
end do
angavg=angavg/nrealatthas(irealattde)
distavg=distavg/nrealatthas(irealattde)
prjx=prjx/nrealatthas(irealattde)
prjy=prjy/nrealatthas(irealattde)
prjz=prjz/nrealatthas(irealattde)
write(*,"(' Note: Attractor',i6,' has',i6,' members')") irealattde,nrealatthas(irealattde)
write(*,"(' The minimum angle is',f12.6,' degree')") angmin
write(*,"(' The maximum angle is',f12.6,' degree')") angmax
write(*,"(' The average angle is',f12.6,' degree')") angavg
write(*,"(' The minimum distance is',f12.6,' Bohr (',f12.6 ' Angstrom)')") distmin,distmin*b2a
write(*,"(' The maximum distance is',f12.6,' Bohr (',f12.6 ' Angstrom)')") distmax,distmax*b2a
write(*,"(' The average distance is',f12.6,' Bohr (',f12.6 ' Angstrom)')") distavg,distavg*b2a
write(*,"(' The average X,Y,Z coordinate by projecting the members of ',a,' to the line linking ',a,' and ',a)") trim(ctmp1),trim(ctmp2),trim(ctmp3)
write(*,"(3f14.8,' Angstrom')") prjx*b2a,prjy*b2a,prjz*b2a
end if
end if
if (imeasure==3) then !Analyze one more term, then print dihedral angle
if (ctmp4(1:1)=='a') then
read(ctmp4(2:),*) iatm
tmpx4=a(iatm)%x
tmpy4=a(iatm)%y
tmpz4=a(iatm)%z
else if (ctmp4(1:1)=='c') then
read(ctmp4(2:),*) irealatt
tmpx4=realattxyz(irealatt,1)
tmpy4=realattxyz(irealatt,2)
tmpz4=realattxyz(irealatt,3)
end if
write(*,"(' The dihedral angle is',f12.6,' degree')") xyz2dih(tmpx1,tmpy1,tmpz1,tmpx2,tmpy2,tmpz2,tmpx3,tmpy3,tmpz3,tmpx4,tmpy4,tmpz4)
end if
else
write(*,*) "Error: Invalid input"
end if
end do
else if (isel==-1) then
write(*,*) "1: On-grid method, Comput .Mat. Sci., 36, 354 (2006)"
write(*,*) "2: Near-grid method, J. Phys.: Condens. Matter, 21, 08420 (2009)"
write(*,*) "3: Near-grid method with boundary refinement step"
write(*,"(a)") " Note: Near-grid method (adapted by Tian Lu) is more accurate than On-grid method and thus is more recommended; with the boundary refinement step, the result will be better"
read(*,*) igridmethod
else if (isel==0) then
ioldtextheigh=textheigh
textheigh=40 !Default textheigh is too small
call drawbasinintgui
textheigh=ioldtextheigh
else if (isel==1) then
isourcedata=1 !Default status is calculating grid data here
if (allocated(cubmat)) then
write(*,"(a)") " Note: There has been a grid data in the memory, please select generating the basins by which manner"
write(*,*) "0 Return"
write(*,*) "1 Generate the basins by selecting a real space function"
write(*,*) "2 Generate the basins by using the grid data stored in memory"
read(*,*) isourcedata
end if
if (isourcedata==0) then
cycle
else if (isourcedata==1) then
write(*,*) "Select the real space function to be integrated"
call selfunc_interface(1,ifuncbasin)
call setgridforbasin(ifuncbasin)
if (allocated(cubmat)) deallocate(cubmat)
allocate(cubmat(nx,ny,nz))
call savecubmat(ifuncbasin,0,iorbsel)
else if (isourcedata==2) then
ifuncbasin=1 !I assume that the file loaded records electron density
write(*,"(' The range of x is from ',f12.6,' to ',f12.6,' Bohr,' i5,' points')") ,orgx,orgx+(nx-1)*dx,nx
write(*,"(' The range of y is from ',f12.6,' to ',f12.6,' Bohr,',i5,' points')") ,orgy,orgy+(ny-1)*dy,ny
write(*,"(' The range of z is from ',f12.6,' to ',f12.6,' Bohr,',i5,' points')") ,orgz,orgz+(nz-1)*dz,nz
write(*,"(' Total number of grid points is ',i10)") nx*ny*nz
write(*,"(' Grid spacing in X,Y,Z (Bohr):',3f12.6)") dx,dy,dz
end if
allocate(grdposneg(nx,ny,nz)) !Record which grids have negative value
grdposneg=.true.
do iz=1,nz
do iy=1,ny
do ix=1,nx
if (cubmat(ix,iy,iz)<0D0) then
grdposneg(ix,iy,iz)=.false.
cubmat(ix,iy,iz)=-cubmat(ix,iy,iz)
end if
end do
end do
end do
! where (cubmat<0D0) !DO NOT USE THIS, because I found that "where" will consuming vary large amount of memory!
! grdposneg=.false.
! cubmat=-cubmat !Invert negative values to positive, after basins are generated the values will be recovered
! end where
if (allocated(gridbas)) deallocate(gridbas)
allocate(gridbas(nx,ny,nz))
call setupmovevec
write(*,*)
write(*,*) "Generating basins, please wait..."
call walltime(walltime1)
call generatebasin(igridmethod) !Generate basins
call walltime(walltime2)
write(*,"(' Generating basins took up wall clock time',i10,' s')") walltime2-walltime1
numunassign=count(gridbas(2:nx-1,2:ny-1,2:nz-1)==0)
write(*,"(' The number of unassigned grids:',i12)") numunassign
numgotobound=count(gridbas(2:nx-1,2:ny-1,2:nz-1)==-1)
write(*,"(' The number of grids travelled to box boundary:',i12)") numgotobound
where (.not.grdposneg) cubmat=-cubmat !Recover original grid data
deallocate(grdposneg)
do iatt=1,numatt !Eliminate the attractors with very low value
if ( abs(cubmat(attgrid(iatt,1),attgrid(iatt,2),attgrid(iatt,3)))<threslowvalatt ) then
write(*,"(' Note: There are attractors having very low absolute value (<',1PE8.2,') and thus insignificant, how to deal with them?')") threslowvalatt
write(*,*) "1 Do nothing"
write(*,*) "2 Set corresponding grids as unassigned status"
write(*,*) "3 Assign corresponding grids to the nearest significant attractors"
write(*,*) "Hint: For most cases, option 3 is recommended"
read(*,*) isel2
call elimlowvalatt(threslowvalatt,isel2)
exit
end if
end do
!Generate actual coordinate of attractors, which will be used to plot in the local GUI, and in any other external GUIs
if (allocated(attxyz)) deallocate(attxyz,attval)
allocate(attxyz(numatt,3),attval(numatt))
do iatt=1,numatt
ix=attgrid(iatt,1)
iy=attgrid(iatt,2)
iz=attgrid(iatt,3)
attxyz(iatt,1)=orgx+(ix-1)*dx
attxyz(iatt,2)=orgy+(iy-1)*dy
attxyz(iatt,3)=orgz+(iz-1)*dz
attval(iatt)=cubmat(ix,iy,iz)
end do
!Currently attractors have been finally determined, one shouldn't perturb them further more
call clusdegenatt(0) !Cluster degenerate attractors as "real attractors" and calculate average coordinate and value for the real attractors
call detectinterbasgrd(6) !Detect interbasin grids
numinterbas=count(interbasgrid)
write(*,"(' The number of interbasin grids:',i12)") numinterbas
else if (isel==2) then
call integratebasin
else if (isel==3.or.isel==4.or.isel==5.or.isel==6.or.isel==7.or.isel==-7.or.isel==8) then
if (.not.allocated(b)) then
write(*,"(a)") " Note: No GTF (Gaussian type function) information is available in your input file, please input the file &
containing GTF information of your system, such as .wfn/.wfx and .fch file. e.g. C:\abc.wfn"
read(*,"(a)") c200tmp
call readinfile(c200tmp,1)
end if
if (isel==3) then
call multipolebasin
else if (isel==4) then
call LIDIbasin(0)
else if (isel==5) then
call LIDIbasin(1)
else if (isel==6) then
call LIDIbasin(2)
else if (isel==7) then
write(*,*) "0 Return"
write(*,*) "1 Integrate a specific function with atomic-center + uniform grids"
write(*,*) "2 The same as 1, but with exact refinement of basin boundary"
write(*,*) "3 The same as 2, but with approximate refinement of basin boundary"
write(*,*) "Hint:"
write(*,*) "Accuracy: 2>=3>>1 Time spent: 2>3>>1 Memory requirement: 3>2=1"
! write(*,*) "Robost: 1=2>3"
read(*,*) iseltmp
call integratebasinmix(iseltmp)
else if (isel==-7) then
call integratebasinmix_LSB
else if (isel==8) then
call integratebasinmix(10)
end if
! else if (isel==100) then
! call integratebasinrefine
else if (isel==9) then
call atmpopinbasin
else if (isel==10) then
call HELP_HELV
else if (isel==11) then
call basinorbcomp
end if
end do
end subroutine
!!---- setup move vector and move length for the 26 neighbour
subroutine setupmovevec
use defvar
use basinintmod
vec26x=0
vec26y=0
vec26z=0
!The nearest neighbours:
len26(1:2)=dx
vec26x(1)=1
vec26x(2)=-1
len26(3:4)=dy
vec26y(3)=1
vec26y(4)=-1
len26(5:6)=dz
vec26z(5)=1
vec26z(6)=-1
!On the edges:
len26(7:10)=dsqrt(dx*dx+dy*dy)
vec26x(7)=1
vec26y(7)=1
vec26x(8)=-1
vec26y(8)=1
vec26x(9)=-1
vec26y(9)=-1
vec26x(10)=1
vec26y(10)=-1
len26(11:14)=dsqrt(dx*dx+dz*dz)
vec26x(11)=1
vec26z(11)=1
vec26x(12)=-1
vec26z(12)=1
vec26x(13)=-1
vec26z(13)=-1
vec26x(14)=1
vec26z(14)=-1
len26(15:18)=dsqrt(dy*dy+dz*dz)
vec26y(15)=1
vec26z(15)=1
vec26y(16)=1
vec26z(16)=-1
vec26y(17)=-1
vec26z(17)=-1
vec26y(18)=-1
vec26z(18)=1
!At the vertices:
len26(19:26)=dsqrt(dx*dx+dy*dy+dz*dz)
vec26z(19:22)=1
vec26x(19)=1
vec26y(19)=1
vec26x(20)=-1
vec26y(20)=1
vec26x(21)=-1
vec26y(21)=-1
vec26x(22)=1
vec26y(22)=-1
vec26z(23:26)=-1
vec26x(23)=1
vec26y(23)=-1
vec26x(24)=1
vec26y(24)=1
vec26x(25)=-1
vec26y(25)=1
vec26x(26)=-1
vec26y(26)=-1
if (allocated(gridbas)) then
gridbas=0 !Unassigned state
gridbas(1,:,:)=-2 !Set status for box boundary grids
gridbas(nx,:,:)=-2
gridbas(:,1,:)=-2
gridbas(:,ny,:)=-2
gridbas(:,:,1)=-2
gridbas(:,:,nz)=-2
end if
end subroutine
!!------- Generate basins from regular grid
! igridmethod=1: On grid, Comput.Mat.Sci.,36,354 =2: Near-grid method (slower, but more accurate), see J.Phys.:Condens.Matter,21,084204
! =3: Near-grid with refinement
! The near-grid method is improved by Tian Lu, namely at later stage automatically switch to on-grid method to guarantee convergence
subroutine generatebasin(igridmethod)
use defvar
use basinintmod
implicit real*8(a-h,o-z)
integer,parameter :: nmaxtrjgrid=3000
integer igridmethod
integer ntrjgrid !Recording trjgrid contains how many elements now
integer trjgrid(nmaxtrjgrid,3) !The trajectory contains which grids, record their indices sequentially, trjgrid(i,1/2/3) = ix/iy/iz of the ith grid
! real*8 trjval(nmaxtrjgrid),gradmaxval(nmaxtrjgrid) !******For debugging******
if (allocated(attgrid)) deallocate(attgrid)
allocate(attgrid(nint(nx*ny*nz/20D0),3)) !I think the number of attractors in general is impossible to exceeds nx*ny*nz/20
numatt=0
write(*,*) " Attractor X,Y,Z coordinate (Angstrom) Value"
!Cycle all grids, but the box boundary ones are ignored (due to gridbas=-2), since can't evalute its gradients in all directions by finite difference
nsteplimit=min( nmaxtrjgrid,nint(dsqrt(dfloat(nx*nx+ny*ny+nz*nz))*2) )
nstepdiscorr=nint(nsteplimit/2D0)
if (igridmethod==1) then
1 continue
!$OMP PARALLEL DO private(ix,iy,iz,ntrjgrid,inowx,inowy,inowz,trjgrid,valnow,imove,gradtmp,igradmax,gradmax,iatt,itrjgrid,idtmp) &
!$OMP shared(gridbas,numatt,attgrid) schedule(DYNAMIC) NUM_THREADS(nthreads)
do iz=2,nz-1
do iy=2,ny-1
do ix=2,nx-1
if (gridbas(ix,iy,iz)/=0) cycle
! if (interbasgrid(ix,iy,iz)==.false.) cycle
ntrjgrid=0
inowx=ix
inowy=iy
inowz=iz
do while(.true.) !Steepest ascent
ntrjgrid=ntrjgrid+1
if (ntrjgrid>nsteplimit) exit !Unconverged. The gridbas for these unassigned grids will still be 0
trjgrid(ntrjgrid,1)=inowx
trjgrid(ntrjgrid,2)=inowy
trjgrid(ntrjgrid,3)=inowz
!Test all 26 directions to find out the maximal gradient direction
valnow=cubmat(inowx,inowy,inowz)
do imove=1,26
gradtmp=(cubmat(inowx+vec26x(imove),inowy+vec26y(imove),inowz+vec26z(imove))-valnow)/len26(imove)
if (imove==1.or.gradtmp>gradmax) then
igradmax=imove
gradmax=gradtmp
end if
end do
!Test if this is an attractor, if yes, assign all grid in this trajectory
if (gradmax<=0) then !Equal sign is important, because when system has symmetry, adjacent grid may be degenerate about mirrow plane, now the ascent should be terminated
if (valnow==0D0) exit !The region far beyond system, the value may be exactly zero due to cutoff of exponent, these grids shouldn't be regarded as attractors
!$OMP CRITICAL
cyciatt3: do iatt=1,numatt
if (inowx==attgrid(iatt,1).and.inowy==attgrid(iatt,2).and.inowz==attgrid(iatt,3)) then
do itrjgrid=1,ntrjgrid
gridbas(trjgrid(itrjgrid,1),trjgrid(itrjgrid,2),trjgrid(itrjgrid,3))=iatt
end do
exit cyciatt3
end if
do imove=1,26
if (inowx+vec26x(imove)==attgrid(iatt,1).and.inowy+vec26y(imove)==attgrid(iatt,2).and.inowz+vec26z(imove)==attgrid(iatt,3)) then
do itrjgrid=1,ntrjgrid
gridbas(trjgrid(itrjgrid,1),trjgrid(itrjgrid,2),trjgrid(itrjgrid,3))=iatt
end do
exit cyciatt3
end if
end do
end do cyciatt3
if (iatt>numatt) then !A new attractor, iatt=numatt+1 currently
numatt=numatt+1
do itrjgrid=1,ntrjgrid
gridbas(trjgrid(itrjgrid,1),trjgrid(itrjgrid,2),trjgrid(itrjgrid,3))=numatt
end do
attgrid(numatt,1)=inowx
attgrid(numatt,2)=inowy
attgrid(numatt,3)=inowz
if (grdposneg(inowx,inowy,inowz)) then
write(*,"(i8,3f14.8,f20.8)") numatt,(orgx+(inowx-1)*dx)*b2a,(orgy+(inowy-1)*dy)*b2a,(orgz+(inowz-1)*dz)*b2a,cubmat(inowx,inowy,inowz)
else !This grid should has negative value
write(*,"(i8,3f14.8,f20.8)") numatt,(orgx+(inowx-1)*dx)*b2a,(orgy+(inowy-1)*dy)*b2a,(orgz+(inowz-1)*dz)*b2a,-cubmat(inowx,inowy,inowz)
end if
end if
!$OMP end CRITICAL
exit
end if
!Move to next grid
inowx=inowx+vec26x(igradmax)
inowy=inowy+vec26y(igradmax)
inowz=inowz+vec26z(igradmax)
!Test if this grid has already been assigned
idtmp=gridbas(inowx,inowy,inowz)
if (idtmp>0) then
do itrjgrid=1,ntrjgrid
gridbas(trjgrid(itrjgrid,1),trjgrid(itrjgrid,2),trjgrid(itrjgrid,3))=idtmp
end do
exit
end if
!Test if encountered box boundary
if (inowx==1.or.inowx==nx.or.inowy==1.or.inowy==ny.or.inowz==1.or.inowz==nz) then
do itrjgrid=1,ntrjgrid
gridbas(trjgrid(itrjgrid,1),trjgrid(itrjgrid,2),trjgrid(itrjgrid,3))=-1
end do
exit
end if
end do !End ascent
end do !End cycle x
end do !End cycle y
end do !End cycle z
!$OMP END PARALLEL DO
else if (igridmethod==2.or.igridmethod==3) then
!$OMP PARALLEL DO private(ix,iy,iz,corrx,corry,corrz,ntrjgrid,inowx,inowy,inowz,trjgrid,valnow,imove,gradtmp,igradmax,gradmax,iatt,&
!$OMP itrjgrid,idtmp,icorrx,icorry,icorrz,gradx,grady,gradz,sclgrad,ineiidx) shared(gridbas,numatt,attgrid) schedule(DYNAMIC) NUM_THREADS(nthreads)
do iz=2,nz-1
do iy=2,ny-1
do ix=2,nx-1
if (gridbas(ix,iy,iz)/=0) cycle
ntrjgrid=0
inowx=ix
inowy=iy
inowz=iz
corrx=0D0 !Correction vector
corry=0D0
corrz=0D0
do while(.true.) !Steepest ascent
ntrjgrid=ntrjgrid+1
trjgrid(ntrjgrid,1)=inowx
trjgrid(ntrjgrid,2)=inowy
trjgrid(ntrjgrid,3)=inowz
if (ntrjgrid>nsteplimit) then !These gridbas for these unconverged grids will still be 0
! do itmp=1,ntrjgrid-1 !******For debugging******
! write(*,"(i5,3i6,2f16.10)") itmp,trjgrid(itmp,1:3),trjval(itmp),gradmaxval(itmp)
! end do
exit
end if
!Test all 26 directions to find out the maximal gradient direction
valnow=cubmat(inowx,inowy,inowz)
do imove=1,26
gradtmp=(cubmat(inowx+vec26x(imove),inowy+vec26y(imove),inowz+vec26z(imove))-valnow)/len26(imove)
if (imove==1.or.gradtmp>gradmax) then
igradmax=imove
gradmax=gradtmp
end if
end do
! write(*,"(3i5,2f14.8)") inowx,inowy,inowz,gradmax,valnow !Trace the trajectory !******For debugging******
if (gradmax<=0D0) then !Equal sign is important, because when system has symmetry, adjacent grid may be degenerate about mirrow plane, now the ascent should be terminated
if (valnow==0D0) exit !The region far beyond system, the value may be exactly zero due to cutoff of exponent, these grids shouldn't be regarded as attractors
!$OMP CRITICAL
cyciatt: do iatt=1,numatt
!Test if current grid is attractor iatt, is yes, assign all grid in this trajectory
if (inowx==attgrid(iatt,1).and.inowy==attgrid(iatt,2).and.inowz==attgrid(iatt,3)) then
do itrjgrid=1,ntrjgrid
gridbas(trjgrid(itrjgrid,1),trjgrid(itrjgrid,2),trjgrid(itrjgrid,3))=iatt
end do
exit cyciatt
end if
!Test if neighbour grid (+/-x,+/-y,+/-z) is attractor iatt, is yes, assign all grid in this trajectory
!The reason I do this is because when the points are symmetric to Cartesian plane, many adjacent and degenerate attractors will occur,&
!while this treatment can combine them as a single one
do imove=1,26
if (inowx+vec26x(imove)==attgrid(iatt,1).and.inowy+vec26y(imove)==attgrid(iatt,2).and.inowz+vec26z(imove)==attgrid(iatt,3)) then
do itrjgrid=1,ntrjgrid
gridbas(trjgrid(itrjgrid,1),trjgrid(itrjgrid,2),trjgrid(itrjgrid,3))=iatt
end do
exit cyciatt
end if
end do
end do cyciatt
!A new attractor
if (iatt==numatt+1) then
numatt=numatt+1
do itrjgrid=1,ntrjgrid
gridbas(trjgrid(itrjgrid,1),trjgrid(itrjgrid,2),trjgrid(itrjgrid,3))=numatt
end do
attgrid(numatt,1)=inowx
attgrid(numatt,2)=inowy
attgrid(numatt,3)=inowz
if (grdposneg(inowx,inowy,inowz)) then
write(*,"(i8,3f14.8,f20.8)") numatt,(orgx+(inowx-1)*dx)*b2a,(orgy+(inowy-1)*dy)*b2a,(orgz+(inowz-1)*dz)*b2a,cubmat(inowx,inowy,inowz)
else !This grid should has negative value
write(*,"(i8,3f14.8,f20.8)") numatt,(orgx+(inowx-1)*dx)*b2a,(orgy+(inowy-1)*dy)*b2a,(orgz+(inowz-1)*dz)*b2a,-cubmat(inowx,inowy,inowz)
end if
end if
!$OMP end CRITICAL
exit
end if
!Correction step may lead to oscillator when encountering circular ELF/LOL attractor, so if the current number of step is already large
!(larger than half of upper limit of step number), then correction will be disabled (namely switch to on-grid method) to guarantee convergence.
if ( ntrjgrid<nstepdiscorr .and. (abs(corrx)>(dx/2D0).or.abs(corry)>(dy/2D0).or.abs(corrz)>(dz/2D0)) ) then !This time we do correction step
if (abs(corrx)>(dx/2D0)) then
icorrx=nint(corrx/abs(corrx)) !Get sign of corrx
inowx=inowx+icorrx
corrx=corrx-icorrx*dx
end if
if (abs(corry)>(dy/2D0)) then
icorry=nint(corry/abs(corry))
inowy=inowy+icorry
corry=corry-icorry*dy
end if
if (abs(corrz)>(dz/2D0)) then
icorrz=nint(corrz/abs(corrz))
inowz=inowz+icorrz
corrz=corrz-icorrz*dz
end if
else !Move to next grid according to maximal gradient and then update correction vector
!Calculate true gradient
gradx=(cubmat(inowx+1,inowy,inowz)-cubmat(inowx-1,inowy,inowz))/(2*dx)
grady=(cubmat(inowx,inowy+1,inowz)-cubmat(inowx,inowy-1,inowz))/(2*dy)
gradz=(cubmat(inowx,inowy,inowz+1)-cubmat(inowx,inowy,inowz-1))/(2*dz)
sclgrad=min(dx/abs(gradx),dy/abs(grady),dz/abs(gradz))
inowx=inowx+vec26x(igradmax)
inowy=inowy+vec26y(igradmax)
inowz=inowz+vec26z(igradmax)
corrx=corrx+gradx*sclgrad-vec26x(igradmax)*dx
corry=corry+grady*sclgrad-vec26y(igradmax)*dy
corrz=corrz+gradz*sclgrad-vec26z(igradmax)*dz
! if (abs(corrx)>(dx/2D0).or.abs(corry)>(dy/2D0).or.abs(corrz)>(dz/2D0)) cycle
end if
!Test if this grid has already been assigned and all of the neighbours were also assigned to the same attractor
idtmp=gridbas(inowx,inowy,inowz)
if (idtmp>0) then
do imove=1,26
ineiidx=gridbas(inowx+vec26x(imove),inowy+vec26y(imove),inowz+vec26z(imove))
if (ineiidx/=idtmp) exit
end do
if (imove==27) then
do itrjgrid=1,ntrjgrid
gridbas(trjgrid(itrjgrid,1),trjgrid(itrjgrid,2),trjgrid(itrjgrid,3))=idtmp
end do
exit
end if
end if
!Test if encountered box boundary
if (inowx==1.or.inowx==nx.or.inowy==1.or.inowy==ny.or.inowz==1.or.inowz==nz) then
do itrjgrid=1,ntrjgrid
gridbas(trjgrid(itrjgrid,1),trjgrid(itrjgrid,2),trjgrid(itrjgrid,3))=-1
end do
exit
end if
end do !End ascent
end do !End cycle x
end do !End cycle y
end do !End cycle z
!$OMP END PARALLEL DO
!!!! Refining the basin boundary
if (igridmethod==3) then
! do itime=1,3 !Refine one time in general is sufficient
write(*,*) "Detecting boundary grids..."
call detectinterbasgrd(6) !It seems that using 26 directions to determine boundary grids doesn't bring evident benefit
write(*,"(' There are',i12,' grids at basin boundary')") count(interbasgrid)
write(*,*) "Refining basin boundary..."
!Below code is the adapted copy of above near-grid code
!$OMP PARALLEL DO private(ix,iy,iz,corrx,corry,corrz,ntrjgrid,inowx,inowy,inowz,valnow,imove,gradtmp,igradmax,gradmax,iatt,&
!$OMP ineiidx,idtmp,icorrx,icorry,icorrz,gradx,grady,gradz,sclgrad) shared(gridbas,attgrid) schedule(DYNAMIC) NUM_THREADS(nthreads)
do iz=2,nz-1
do iy=2,ny-1
do ix=2,nx-1
if (.not.interbasgrid(ix,iy,iz)) cycle
if (gridbas(ix,iy,iz)<=0) cycle !Ignored the ones unassigned or gone to box boundary
ntrjgrid=0
inowx=ix
inowy=iy
inowz=iz
corrx=0D0 !Correction vector
corry=0D0
corrz=0D0
do while(.true.) !Steepest ascent
ntrjgrid=ntrjgrid+1
if (ntrjgrid>nsteplimit) exit
valnow=cubmat(inowx,inowy,inowz)
do imove=1,26
gradtmp=(cubmat(inowx+vec26x(imove),inowy+vec26y(imove),inowz+vec26z(imove))-valnow)/len26(imove)
if (imove==1.or.gradtmp>gradmax) then
igradmax=imove
gradmax=gradtmp
end if
end do
if (gradmax<=0) then !Equal sign is important, because when system has symmetry, adjacent grid may be degenerate about mirrow plane, now the ascent should be terminated
cyciatt2: do iatt=1,numatt
if (inowx==attgrid(iatt,1).and.inowy==attgrid(iatt,2).and.inowz==attgrid(iatt,3)) then
gridbas(ix,iy,iz)=iatt
exit cyciatt2
end if
do imove=1,26 !Test if neighbour grid (+/-x,+/-y,+/-z) is attractor iatt
if (inowx+vec26x(imove)==attgrid(iatt,1).and.inowy+vec26y(imove)==attgrid(iatt,2).and.inowz+vec26z(imove)==attgrid(iatt,3)) then
gridbas(ix,iy,iz)=iatt
exit cyciatt2
end if
end do
end do cyciatt2
if (iatt>numatt) then
write(*,*) "Warning: Found new attractor at refining process!"
end if
exit
end if
if ( ntrjgrid<nstepdiscorr .and. (abs(corrx)>(dx/2D0).or.abs(corry)>(dy/2D0).or.abs(corrz)>(dz/2D0)) ) then !This time we do correction step
if (abs(corrx)>(dx/2D0)) then
icorrx=nint(corrx/abs(corrx)) !Get sign of corrx
inowx=inowx+icorrx
corrx=corrx-icorrx*dx
end if
if (abs(corry)>(dy/2D0)) then
icorry=nint(corry/abs(corry))
inowy=inowy+icorry
corry=corry-icorry*dy
end if
if (abs(corrz)>(dz/2D0)) then
icorrz=nint(corrz/abs(corrz))
inowz=inowz+icorrz
corrz=corrz-icorrz*dz
end if
else !Move to next grid according to maximal gradient and then update correction vector
gradx=(cubmat(inowx+1,inowy,inowz)-cubmat(inowx-1,inowy,inowz))/(2*dx)
grady=(cubmat(inowx,inowy+1,inowz)-cubmat(inowx,inowy-1,inowz))/(2*dy)
gradz=(cubmat(inowx,inowy,inowz+1)-cubmat(inowx,inowy,inowz-1))/(2*dz)