Octopus
curv_gygi.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2006 M. Marques, A. Castro, A. Rubio, G. Bertsch
2!! Copyright (C) 2025 S. Ohlmann
3!!
4!! This program is free software; you can redistribute it and/or modify
5!! it under the terms of the GNU General Public License as published by
6!! the Free Software Foundation; either version 2, or (at your option)
7!! any later version.
8!!
9!! This program is distributed in the hope that it will be useful,
10!! but WITHOUT ANY WARRANTY; without even the implied warranty of
11!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12!! GNU General Public License for more details.
13!!
14!! You should have received a copy of the GNU General Public License
15!! along with this program; if not, write to the Free Software
16!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17!! 02110-1301, USA.
18!!
19
20#include "global.h"
21
24
25module curv_gygi_oct_m
27 use debug_oct_m
28 use global_oct_m
29 use, intrinsic :: iso_fortran_env
31 use math_oct_m
34 use parser_oct_m
37 use unit_oct_m
39
40 implicit none
41
42 private
43 public :: &
47
48 type, extends(coordinate_system_t) :: curv_gygi_t
49 private
50 real(real64), public :: A
51 real(real64), public :: alpha
52 real(real64), public :: beta
53 real(real64), allocatable :: pos(:, :)
54 integer :: npos
55 type(root_solver_t) :: rs
56 contains
57 procedure :: to_cartesian => curv_gygi_to_cartesian
58 procedure :: from_cartesian => curv_gygi_from_cartesian
59 procedure :: write_info => curv_gygi_write_info
60 procedure :: surface_element => curv_gygi_surface_element
61 procedure :: jacobian => curv_gygi_jacobian
62 procedure :: jacobian_inverse => curv_gygi_jacobian_inverse
63 procedure :: trace_hessian => curv_gygi_trace_hessian
64 final :: curv_gygi_finalize
65 end type curv_gygi_t
66
67 interface curv_gygi_t
68 procedure curv_gygi_constructor
69 end interface curv_gygi_t
70
71 ! Auxiliary variables for the root solver.
72 class(curv_gygi_t), pointer :: gygi_p
73 type(curv_gygi_t), target :: gygi_global
74 real(real64), allocatable :: chi_p(:)
75
76contains
77
78 ! ---------------------------------------------------------
79 function curv_gygi_constructor(namespace, dim, npos, pos) result(gygi)
80 type(namespace_t), intent(in) :: namespace
81 integer, intent(in) :: dim
82 integer, intent(in) :: npos
83 real(real64), intent(in) :: pos(1:dim,1:npos)
84 class(curv_gygi_t), pointer :: gygi
85
86 push_sub(curv_gygi_constructor)
87
88 safe_allocate(gygi)
89
90 gygi%dim = dim
91 gygi%local_basis = .true.
92 gygi%orthogonal = .true. ! This needs to be checked.
93
94 gygi%npos = npos
95 safe_allocate(gygi%pos(1:dim, 1:gygi%npos))
96 gygi%pos(:, :) = pos(:, :)
97
98 !%Variable CurvGygiA
99 !%Type float
100 !%Default 0.5
101 !%Section Mesh::Curvilinear::Gygi
102 !%Description
103 !% The grid spacing is reduced locally around each atom, and the reduction is
104 !% given by 1/(1+<i>A</i>), where <i>A</i> is specified by this variable. So, if
105 !% <i>A</i>=1/2 (the default), the grid spacing is reduced to two thirds = 1/(1+1/2).
106 !% [This is the <math>A_{\alpha}</math> variable in Eq. 2 of F. Gygi and G. Galli, <i>Phys.
107 !% Rev. B</i> <b>52</b>, R2229 (1995)]. It must be larger than zero.
108 !% It must not be larger than two, as the coordinate transformation is not bijective
109 !% anymore for larger values of <i>A</i>. This caps the local refinement that can be
110 !% obtained at 1/(1+2), i.e. a factor of three, which is the governing limitation when
111 !% attempting all-electron calculations on heavy atoms.
112 !%End
113 call parse_variable(namespace, 'CurvGygiA', m_half, gygi%A)
114
115 !%Variable CurvGygiAlpha
116 !%Type float
117 !%Default 2.0 a.u.
118 !%Section Mesh::Curvilinear::Gygi
119 !%Description
120 !% This number determines the region over which the grid is enhanced (range of
121 !% enhancement of the resolution). That is, the grid is enhanced on a sphere
122 !% around each atom, whose radius is given by this variable. [This is the <math>a_{\alpha}</math>
123 !% variable in Eq. 2 of F. Gygi and G. Galli, <i>Phys. Rev. B</i> <b>52</b>, R2229 (1995)].
124 !% It must be larger than zero.
125 !%End
126
127 call parse_variable(namespace, 'CurvGygiAlpha', m_two, gygi%alpha, units_inp%length)
128 !%Variable CurvGygiBeta
129 !%Type float
130 !%Default 4.0 a.u.
131 !%Section Mesh::Curvilinear::Gygi
132 !%Description
133 !% This number determines the distance over which Euclidean coordinates are
134 !% recovered. [This is the <math>b_{\alpha}</math> variable in Eq. 2 of F. Gygi and G. Galli,
135 !% <i>Phys. Rev. B</i> <b>52</b>, R2229 (1995)]. It must be larger than zero.
136 !%End
137 call parse_variable(namespace, 'CurvGygiBeta', m_four, gygi%beta, units_inp%length)
138
139 if (gygi%a <= m_zero) call messages_input_error(namespace, 'CurvGygiA')
140 if (gygi%a > m_two) call messages_input_error(namespace, 'CurvGygiA')
141 if (gygi%alpha <= m_zero) call messages_input_error(namespace, 'CurvGygiAlpha')
142 if (gygi%beta <= m_zero) call messages_input_error(namespace, 'CurvGygiBeta')
144 gygi%min_mesh_scaling_product = (m_one / (m_one + gygi%A))**gygi%dim
146 ! initialize root solver
147 call root_solver_init(gygi%rs, namespace, dim, solver_type = root_newton, maxiter = 500, abs_tolerance = 1.0e-10_real64)
151
152 ! ---------------------------------------------------------
153 subroutine curv_gygi_copy(this_out, this_in)
154 type(curv_gygi_t), intent(inout) :: this_out
155 type(curv_gygi_t), intent(in) :: this_in
159 this_out%A = this_in%A
160 this_out%alpha = this_in%alpha
161 this_out%beta = this_in%beta
162 safe_allocate_source_a(this_out%pos, this_in%pos)
163 this_out%pos = this_in%pos
164 this_out%npos = this_in%npos
165 this_out%dim = this_in%dim
166 this_out%local_basis = this_in%local_basis
167 this_out%orthogonal = this_in%orthogonal
168 call root_solver_init(this_out%rs, global_namespace, this_in%dim, solver_type = root_newton, &
169 maxiter = 500, abs_tolerance = 1.0e-10_real64)
170
171 pop_sub(curv_gygi_copy)
172 end subroutine curv_gygi_copy
173
174 ! ---------------------------------------------------------
175 subroutine curv_gygi_finalize(this)
176 type(curv_gygi_t), intent(inout) :: this
177
178 push_sub(curv_gygi_finalize)
179
180 safe_deallocate_a(this%pos)
181
182 pop_sub(curv_gygi_finalize)
183 end subroutine curv_gygi_finalize
184
185 ! ---------------------------------------------------------
186 real(real64) pure function gygi_f(this, r)
187 class(curv_gygi_t), intent(in) :: this
188 real(real64), intent(in) :: r
189
190 ! no PUSH_SUB, called too often
191
192 if (r < 1.0e-4_real64) then
193 ! below 1e-4, the relative deviation of this Taylor expansion from the function is less than 1e-15
194 gygi_f = this%A * (m_one + (-m_one/(m_three*this%alpha**2) - m_one/this%beta**2)*r**2)
195 else if (-(r/this%beta)**2 <= m_min_exp_arg) then
196 gygi_f = m_zero
197 else
198 gygi_f = this%A * this%alpha/r * tanh(r/this%alpha) * exp(-(r/this%beta)**2)
199 end if
200 end function gygi_f
201
202 ! ---------------------------------------------------------
203 ! absorb division by r into the function to get the correct expansion
204 real(real64) pure function gygi_dfdr_over_r(this, r)
205 class(curv_gygi_t), intent(in) :: this
206 real(real64), intent(in) :: r
207
208 ! no PUSH_SUB, called too often
209
210 if (r < 3.0e-3_real64) then
211 ! below 3e-3, the expansion is closer than 1e-13 to the function and does not suffer from
212 ! rounding errors
213 gygi_dfdr_over_r = this%A * ((-m_two/(m_three*this%alpha**2) - m_two/this%beta**2) + &
214 (8._real64/(15._real64*this%alpha**4) + m_four/(m_three*this%alpha**2*this%beta**2) &
215 + m_two/this%beta**4) * r**2)
216 else if (-(r/this%beta)**2 <= m_min_exp_arg) then
217 gygi_dfdr_over_r = m_zero
218 else
219 gygi_dfdr_over_r = this%A * (this%beta**2*r/cosh(r/this%alpha)**2 - &
220 this%alpha*(this%beta**2+m_two*r**2) * tanh(r/this%alpha)) &
221 / (this%beta**2*r**3) * exp(-(r/this%beta)**2)
222 end if
223 end function gygi_dfdr_over_r
224
225 ! ---------------------------------------------------------
226 ! only a combination of the second derivative is needed, namely
227 ! (d2f/d2r - 1/r df/dr)/r**2
228 real(real64) pure function gygi_d2fdr2_combination(this, r)
229 class(curv_gygi_t), intent(in) :: this
230 real(real64), intent(in) :: r
231
232 ! no PUSH_SUB, called too often
233
234 if (r < 3.0e-3_real64) then
235 ! below 3e-3, the expansion is closer than 1e-5 to the function and does not suffer from
236 ! rounding errors
237 gygi_d2fdr2_combination = this%A * ( &
238 m_two*(8._real64/(15._real64*this%alpha**4) + m_four/(m_three*this%alpha**2*this%beta**2) &
239 + m_two/this%beta**4) &
240 - m_four*(34._real64/(105._real64*this%alpha**6) + m_four/(m_five*this%alpha**4*this%beta**2) &
241 + m_one/(this%alpha**2*this%beta**4) + m_one/this%beta**6) * r**2 &
242 )
243 else if (-(r/this%beta)**2 <= m_min_exp_arg) then
245 else
246 gygi_d2fdr2_combination = this%A * exp(-(r/this%beta)**2)*( &
247 -m_two*tanh(r/this%alpha)/(cosh(r/this%alpha)**2*this%alpha*r) &
248 + tanh(r/this%alpha)*(m_three*this%alpha/r**3+m_four*this%alpha/(this%beta**2*r)+m_four*this%alpha*r/this%beta**4) &
249 + m_one/cosh(r/this%alpha)**2*(-m_three/r**2-m_four/this%beta**2))/r**2
250 end if
251 end function gygi_d2fdr2_combination
252
253
254 ! ---------------------------------------------------------
255 function curv_gygi_to_cartesian(this, chi) result(xx)
256 class(curv_gygi_t), target, intent(in) :: this
257 real(real64), intent(in) :: chi(:)
258 real(real64) :: xx(1:this%dim), xx_start(1:this%dim)
259
260 logical :: conv
261 integer :: i_conv, n_conv
262
263 ! no PUSH_SUB, called too often
264
265 gygi_p => this
266 safe_allocate(chi_p(1:this%dim))
267 chi_p(:) = chi(:)
268
269 call droot_solver_run(this%rs, getf, xx, conv, startval = chi)
270 nullify(gygi_p)
271
272 if (.not. conv) then
273 call curv_gygi_copy(gygi_global, this)
275 ! try to converge with decreasing A
276 n_conv = 8
277 do i_conv = 1, n_conv
278 gygi_p%A = gygi_p%A / m_two
279 call droot_solver_run(gygi_p%rs, getf, xx, conv, startval = chi)
280 if (conv) then
281 exit
282 end if
283 end do
284 if (conv) then
285 ! increase A again and take previous xx as starting point
286 n_conv = i_conv
287 do i_conv = n_conv, 1, -1
288 gygi_p%A = gygi_p%A * m_two
289 xx_start = xx
290 call droot_solver_run(gygi_p%rs, getf, xx, conv, startval = xx_start)
291 end do
292 end if
293 nullify(gygi_p)
295 end if
296
297 safe_deallocate_a(chi_p)
298
299 if (.not. conv) then
300 message(1) = "During the construction of the adaptive grid, the Newton-Raphson"
301 message(2) = "method did not converge for point:"
302 write(message(3),'(9f14.6)') xx(1:this%dim)
303 message(4) = "Try varying the Gygi parameters -- usually reducing CurvGygiA or"
304 message(5) = "CurvGygiAlpha (or both) solves the problem."
305 call messages_fatal(5)
306 end if
307
308 end function curv_gygi_to_cartesian
309
310 ! ---------------------------------------------------------
311 pure function curv_gygi_from_cartesian(this, xx) result(chi)
312 class(curv_gygi_t), target, intent(in) :: this
313 real(real64), intent(in) :: xx(:)
314 real(real64) :: chi(1:this%dim)
315
316 integer :: i, ia
317 real(real64) :: diff(this%dim), f
318
319 ! no PUSH_SUB, called too often
320
321 chi(1:this%dim) = xx(1:this%dim)
322 do ia = 1, this%npos
323 diff = xx(1:this%dim) - this%pos(1:this%dim, ia)
324 f = gygi_f(this, norm2(diff))
325 do i = 1, this%dim
326 chi(i) = chi(i) + diff(i) * f
327 end do
328 end do
329
330 end function curv_gygi_from_cartesian
331
332 ! ---------------------------------------------------------
333 subroutine curv_gygi_write_info(this, iunit, namespace)
334 class(curv_gygi_t), intent(in) :: this
335 integer, optional, intent(in) :: iunit
336 type(namespace_t), optional, intent(in) :: namespace
337
338 push_sub(curv_gygi_write_info)
339
340 write(message(1), '(a)') ' Curvilinear Method = gygi'
341 write(message(2), '(a)') ' Gygi Parameters:'
342 write(message(3), '(4x,a,f6.3)') 'A = ', this%a
343 write(message(4), '(4x,3a,f6.3)') 'alpha [', trim(units_abbrev(units_out%length)), '] = ', &
344 units_from_atomic(units_out%length, this%alpha)
345 write(message(5), '(4x,3a,f6.3)') 'beta [', trim(units_abbrev(units_out%length)), '] = ', &
346 units_from_atomic(units_out%length, this%beta)
347 call messages_info(5, iunit=iunit, namespace=namespace)
348
349 pop_sub(curv_gygi_write_info)
350 end subroutine curv_gygi_write_info
351
352 ! ---------------------------------------------------------
353 real(real64) function curv_gygi_surface_element(this, idir) result(ds)
354 class(curv_gygi_t), intent(in) :: this
355 integer, intent(in) :: idir
356
357 ds = m_zero
358 message(1) = 'Surface element with gygi curvilinear coordinates not implemented'
359 call messages_fatal(1)
360
361 end function curv_gygi_surface_element
362
363 ! ---------------------------------------------------------
364 function curv_gygi_jacobian(this, chi) result(jacobian)
365 class(curv_gygi_t), intent(in) :: this
366 real(real64), intent(in) :: chi(:)
367 real(real64) :: jacobian(1:this%dim, 1:this%dim)
368
369 jacobian = this%jacobian_inverse(chi)
370 call lalg_inverse(this%dim, jacobian, "dir")
371 end function curv_gygi_jacobian
372
373 ! ---------------------------------------------------------
374 function curv_gygi_jacobian_inverse(this, chi) result(jacobian_inverse)
375 class(curv_gygi_t), intent(in) :: this
376 real(real64), intent(in) :: chi(:)
377 real(real64) :: jacobian_inverse(1:this%dim, 1:this%dim)
378
379 real(real64) :: xx(1:this%dim)
380
381 ! no PUSH_SUB, called too often
382
383 xx(:) = this%to_cartesian(chi)
384 jacobian_inverse = curv_gygi_jacobian_inverse_cartesian(this, xx)
385
386 end function curv_gygi_jacobian_inverse
387
388 ! ---------------------------------------------------------
389 function curv_gygi_jacobian_inverse_cartesian(this, xx) result(jacobian_inverse)
390 class(curv_gygi_t), intent(in) :: this
391 real(real64), intent(in) :: xx(:)
392 real(real64) :: jacobian_inverse(1:this%dim, 1:this%dim)
393
394 integer :: i, ix, iy
395 real(real64) :: r, diff(1:this%dim), dfdr_over_r, f
396
397 ! no PUSH_SUB, called too often
398
399 jacobian_inverse(1:this%dim, 1:this%dim) = diagonal_matrix(this%dim, m_one)
400
401 do i = 1, this%npos
402 diff = xx(1:this%dim) - this%pos(1:this%dim, i)
403 r = norm2(diff)
404
405 f = gygi_f(this, r)
406 dfdr_over_r = gygi_dfdr_over_r(this, r)
407
408 do ix = 1, this%dim
409 jacobian_inverse(ix, ix) = jacobian_inverse(ix, ix) + f
410 do iy = 1, this%dim
411 jacobian_inverse(ix, iy) = jacobian_inverse(ix, iy) + diff(ix)*diff(iy)*dfdr_over_r
412 end do
413 end do
414 end do
415
417
418
419 ! ---------------------------------------------------------
420 pure subroutine curv_gygi_hessian(this, xx, hessian, natoms)
421 class(curv_gygi_t), intent(in) :: this
422 real(real64), intent(in) :: xx(:)
423 real(real64), intent(out) :: hessian(:, :, :)
424 integer, optional, intent(in) :: natoms
425
426 integer :: ia, i, j, k, natoms_
427 real(real64) :: r, diff(this%dim), dfdr_over_r, d2fdr2_combination
429 ! no PUSH_SUB, called too often
430
431 hessian(1:this%dim, 1:this%dim, 1:this%dim) = m_zero
432
433 natoms_ = this%npos
434 if (present(natoms)) natoms_ = natoms
435
436 do ia = 1, natoms_
437 diff = xx(1:this%dim) - this%pos(1:this%dim, ia)
438 r = norm2(diff)
439
440 dfdr_over_r = gygi_dfdr_over_r(this, r)
441 d2fdr2_combination = gygi_d2fdr2_combination(this, r)
442
443 do i = 1, this%dim
444 do j = 1, this%dim
445 do k = 1, this%dim
446 if (i == j) then
447 hessian(i, j, k) = hessian(i, j, k) + diff(k)*dfdr_over_r
448 end if
449 if (i == k) then
450 hessian(i, j, k) = hessian(i, j, k) + diff(j)*dfdr_over_r
451 end if
452 if (j == k) then
453 hessian(i, j, k) = hessian(i, j, k) + diff(i)*dfdr_over_r
454 end if
455 hessian(i, j, k) = hessian(i, j, k) + diff(i)*diff(j)*diff(k)*d2fdr2_combination
456 end do
457 end do
458 end do
459 end do
460
461 end subroutine curv_gygi_hessian
462
463 ! ---------------------------------------------------------
464 function curv_gygi_trace_hessian(this, chi) result(trace_hessian)
465 class(curv_gygi_t), intent(in) :: this
466 real(real64), intent(in) :: chi(:)
467 real(real64) :: trace_hessian(1:this%dim)
468
469 integer :: ia, i, k
470 real(real64) :: r, xx(this%dim), diff(this%dim), dfdr_over_r, d2fdr2_combination
471
472 ! no PUSH_SUB, called too often
473
474 xx(:) = this%to_cartesian(chi)
475 trace_hessian(:) = m_zero
476
477 do ia = 1, this%npos
478 diff = xx(1:this%dim) - this%pos(1:this%dim, ia)
479 r = norm2(diff)
480
481 dfdr_over_r = gygi_dfdr_over_r(this, r)
482 d2fdr2_combination = gygi_d2fdr2_combination(this, r)
483
484 do i = 1, this%dim
485 do k = 1, this%dim
486 if (i == k) then
487 trace_hessian(i) = trace_hessian(i) + m_two*diff(k)*dfdr_over_r
488 end if
489 trace_hessian(i) = trace_hessian(i) + diff(i)*dfdr_over_r
490 trace_hessian(i) = trace_hessian(i) + diff(i)*diff(k)*diff(k)*d2fdr2_combination
491 end do
492 end do
493 end do
494
495 end function curv_gygi_trace_hessian
496
497 ! ---------------------------------------------------------
498 subroutine getf(y, f, jf)
499 real(real64), intent(in) :: y(:)
500 real(real64), intent(out) :: f(:), jf(:, :)
501
502 ! no PUSH_SUB, called too often
503
505 f = gygi_p%from_cartesian(y)
506 f(1:gygi_p%dim) = f(1:gygi_p%dim) - chi_p(1:gygi_p%dim)
507
508 end subroutine getf
509
510end module curv_gygi_oct_m
511
512!! Local Variables:
513!! mode: f90
514!! coding: utf-8
515!! End:
double exp(double __x) __attribute__((__nothrow__
double tanh(double __x) __attribute__((__nothrow__
double cosh(double __x) __attribute__((__nothrow__
This module implements the curvilinear coordinates given in F. Gygi and G. Galli, PRB 52 R2229 (1996)...
Definition: curv_gygi.F90:120
type(curv_gygi_t), target gygi_global
Definition: curv_gygi.F90:168
subroutine curv_gygi_write_info(this, iunit, namespace)
Definition: curv_gygi.F90:429
real(real64), dimension(:), allocatable chi_p
Definition: curv_gygi.F90:169
real(real64) pure function gygi_f(this, r)
Definition: curv_gygi.F90:282
real(real64) pure function gygi_d2fdr2_combination(this, r)
Definition: curv_gygi.F90:324
real(real64) pure function gygi_dfdr_over_r(this, r)
Definition: curv_gygi.F90:300
pure subroutine, public curv_gygi_hessian(this, xx, hessian, natoms)
Definition: curv_gygi.F90:516
real(real64) function, dimension(1:this%dim, 1:this%dim) curv_gygi_jacobian_inverse_cartesian(this, xx)
Definition: curv_gygi.F90:485
real(real64) function, dimension(1:this%dim) curv_gygi_trace_hessian(this, chi)
Definition: curv_gygi.F90:560
pure real(real64) function, dimension(1:this%dim) curv_gygi_from_cartesian(this, xx)
Definition: curv_gygi.F90:407
subroutine, public curv_gygi_copy(this_out, this_in)
Definition: curv_gygi.F90:249
real(real64) function, dimension(1:this%dim) curv_gygi_to_cartesian(this, chi)
Definition: curv_gygi.F90:351
subroutine getf(y, f, jf)
Definition: curv_gygi.F90:594
real(real64) function curv_gygi_surface_element(this, idir)
Definition: curv_gygi.F90:449
class(curv_gygi_t) function, pointer curv_gygi_constructor(namespace, dim, npos, pos)
Definition: curv_gygi.F90:175
class(curv_gygi_t), pointer gygi_p
Definition: curv_gygi.F90:167
real(real64) function, dimension(1:this%dim, 1:this%dim) curv_gygi_jacobian(this, chi)
Definition: curv_gygi.F90:460
real(real64) function, dimension(1:this%dim, 1:this%dim) curv_gygi_jacobian_inverse(this, chi)
Definition: curv_gygi.F90:470
subroutine curv_gygi_finalize(this)
Definition: curv_gygi.F90:271
real(real64), parameter, public m_two
Definition: global.F90:202
real(real64), parameter, public m_zero
Definition: global.F90:200
real(real64), parameter, public m_four
Definition: global.F90:204
real(real64), parameter, public m_min_exp_arg
Definition: global.F90:219
real(real64), parameter, public m_half
Definition: global.F90:206
real(real64), parameter, public m_one
Definition: global.F90:201
real(real64), parameter, public m_three
Definition: global.F90:203
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:117
subroutine, public messages_input_error(namespace, var, details, row, column)
Definition: messages.F90:691
type(namespace_t), public global_namespace
Definition: namespace.F90:135
integer, parameter, public root_newton
subroutine, public root_solver_init(rs, namespace, dimensionality, solver_type, maxiter, rel_tolerance, abs_tolerance)
brief This module defines the class unit_t which is used by the unit_systems_oct_m module.
Definition: unit.F90:134
This module defines the unit system, used for input and output.
type(unit_system_t), public units_inp
the units systems for reading and writing
static double f(double w, void *p)
abstract class to describe coordinate systems
int true(void)