-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.f90
182 lines (161 loc) · 4.89 KB
/
tools.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
! -*- mode: F90 -*-
! Copyright (c) 2013-2015 Alberto Otero de la Roza <[email protected]>,
! Felix Kannemann <[email protected]>, Erin R. Johnson <[email protected]>,
! Ross M. Dickson <[email protected]>, Hartmut Schmider <[email protected]>,
! and Axel D. Becke <[email protected]>
!
! postg is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! This program is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with this program. If not, see <http://www.gnu.org/licenses/>.
!> Specific tools that are independent of the mesh or the molecule.
module tools
public
public :: bhole ! calculate the becke-roussel hole parameters
public :: xlnorm ! inverse BR hole normalization
public :: xfuncs ! rhs of the BR hole equation and derivative
contains
!> Becke-Roussel hole routine
subroutine bhole(rho,quad,hnorm,b,alf,prefac)
use param
real*8, intent(in) :: rho !< Density
real*8, intent(in) :: quad !< Q-value
real*8, intent(in) :: hnorm !< Hole normalization
real*8, intent(out) :: b !< Hole b
real*8, intent(out) :: alf !< Hole a
real*8, intent(out) :: prefac !< Hole A
real*8 :: rhs, x0, shift, x1, x, expo, f, df, quad0
integer :: i
real*8, parameter :: tiny = 1d-20
quad0 = quad
if (abs(quad) < tiny) quad0 = sign(tiny,quad)
rhs=third2*(pi*rho/max(hnorm,tiny))**third2*rho/quad0
x0=2.d0
shift=1.d0
if(rhs.lt.0.d0)go to 10
if(rhs.gt.0.d0)go to 20
10 do i=1,16
x=x0-shift
call xfuncs(x,rhs,f,df)
if(f.lt.0.d0)go to 88
shift=0.1d0*shift
enddo
write(iout,1002)
stop
20 do i=1,16
x=x0+shift
call xfuncs(x,rhs,f,df)
if(f.gt.0.d0)go to 88
shift=0.1d0*shift
enddo
write(iout,1002)
stop
88 continue
do i=1,100
call xfuncs(x,rhs,f,df)
x1=x-f/df
if(dabs(x1-x).lt.1.d-10)go to 111
x=x1
enddo
write(iout,1001)
stop
111 x=x1
expo=dexp(-x)
prefac=rho/expo
alf=(8.d0*pi*prefac/max(hnorm,tiny))**third
b=x/alf
return
1001 format(' ','bhole: newton algorithm fails to converge!')
1002 format(' ','bhole: newton algorithm fails to initialize!')
end subroutine bhole
!> Calculate the inverse BR hole normalization. From numol.
subroutine xlnorm(rho,quad,uxpos,xlnrm)
use param
implicit none
real*8, intent(in) :: rho, quad, uxpos
real*8, intent(out) :: xlnrm
real*8 :: rhs, x0, shift, x, f, df, x1, alf, a
integer :: i
logical :: found
if (rho < 1.d-10) then
xlnrm=1.D0
return
end if
rhs=4.d0*pi/3.d0*rho*rho/quad/uxpos
x0=2.D0
shift=1.D0
found = .false.
if (rhs < 0.d0) then
do i = 1, 16
x = x0 - shift
call xlfuns(x,rhs,f,df)
if (f < 0.D0) then
found = .true.
exit
end if
shift=0.1d0*shift
end do
if (.not.found) &
call error('xlnorm','newton algorithm failed to initialize',2)
else
do i=1, 16
x=x0+shift
call xlfuns(x,rhs,f,df)
if(f > 0.D0) then
found = .true.
exit
endif
shift=0.1d0*shift
end DO
if (.not.found) &
call error('xlnorm','newton algorithm failed to initialize',2)
endif
found = .false.
do i = 1, 100
call xlfuns(x,rhs,f,df)
x1=x-f/df
if(dabs(x1-x) < 1.d-10) then
found = .true.
exit
end if
x=x1
end do
if (.not.found) &
call error('xlnorm','newton algorithm failed to converge',2)
x=x1
alf=dsqrt(6.d0*quad*x/rho/(x-2.d0))
a=rho*exp(x)
xlnrm=min(8.d0*pi*a/alf**3,2.d0)
end subroutine xlnorm
!< RHS of the hole equation, and derivative.
subroutine xfuncs(x,rhs,f,df)
real*8, intent(in) :: x !< x-value
real*8, intent(in) :: rhs !< Right-hand side
real*8, intent(out) :: f !< Value of the function
real*8, intent(out) :: df !< Value of the function derivative
real*8 :: expo23
expo23=dexp(-2.d0/3.d0*x)
f = x*expo23/(x-2.d0) - rhs
df=2.d0/3.d0*(2.d0*x-x**2-3.d0)/(x-2.d0)**2*expo23
end subroutine xfuncs
!< RHS of the BR hole equation and derivative
subroutine xlfuns(x,rhs,f,df)
implicit none
real*8, intent(in) :: x, rhs
real*8, intent(out) :: f, df
real*8 :: expo, bot
expo=exp(x)
bot=(x-2.d0)*(expo-1.d0-0.5d0*x)
f = x*x/bot - rhs
df=4.d0*x-(4.d0*x-3.d0*x*x+x**3)*expo
df=df/bot**2
end subroutine xlfuns
end module tools