Octopus
xc_fbe.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2006 M. Marques, A. Castro, A. Rubio, G. Bertsch
2!! Copyright (C) 2023-2024 N. Tancogne-Dejean
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
22module xc_fbe_oct_m
23 use batch_oct_m
25 use comm_oct_m
26 use debug_oct_m
30 use global_oct_m
31 use grid_oct_m
34 use math_oct_m
36 use mesh_oct_m
38 use mpi_oct_m
41 use parser_oct_m
46 use space_oct_m
54
55 implicit none
56
57 private
58 public :: &
59 x_fbe_calc, &
60 lda_c_fbe, &
62
63 real(real64), pointer :: rho_aux(:) => null()
64 real(real64), pointer :: lapl_rho_aux(:) => null()
65 real(real64), allocatable :: diag_lapl(:)
66
67
68contains
69
70 ! -------------------------------------------------------------------------------------
76 subroutine x_fbe_calc (id, namespace, psolver, gr, st, space, ex, vxc)
77 integer, intent(in) :: id
78 type(namespace_t), intent(in) :: namespace
79 type(poisson_t), intent(in) :: psolver
80 type(grid_t), intent(in) :: gr
81 type(states_elec_t), intent(inout) :: st
82 type(space_t), intent(in) :: space
83 real(real64), intent(inout) :: ex
84 real(real64), contiguous, optional, intent(inout) :: vxc(:,:)
85
86 real(real64), allocatable :: fxc(:,:,:), internal_vxc(:,:)
87
88 push_sub(x_fbe_calc)
89
90 select case(id)
91 case(xc_oep_x_fbe)
92 if (states_are_real(st)) then
93 call dx_fbe_calc(namespace, psolver, gr, gr%der, st, ex, vxc=vxc)
94 else
95 call zx_fbe_calc(namespace, psolver, gr, gr%der, st, ex, vxc=vxc)
96 end if
97 case(xc_oep_x_fbe_sl)
98 safe_allocate(fxc(1:gr%np_part, 1:gr%box%dim, 1:st%d%spin_channels))
99 safe_allocate(internal_vxc(1:gr%np, 1:st%d%spin_channels))
100 internal_vxc = m_zero
101 ! We first compute the force density
102 if (states_are_real(st)) then
103 call dx_fbe_calc(namespace, psolver, gr, gr%der, st, ex, vxc=internal_vxc, fxc=fxc)
104 else
105 call zx_fbe_calc(namespace, psolver, gr, gr%der, st, ex, vxc=internal_vxc, fxc=fxc)
106 end if
107
108 ! We solve the Sturm-Liouville equation
109 if (present(vxc)) then
110 call solve_sturm_liouville(namespace, gr, st, space, fxc, internal_vxc)
111 end if
112
113 ! Get the energy from the virial relation
114 ex = get_virial_energy(gr, st%d%spin_channels, fxc)
115
116 ! Adds the calculated potential
117 call lalg_axpy(gr%np, st%d%spin_channels, m_one, internal_vxc, vxc)
118
119 safe_deallocate_a(fxc)
120 safe_deallocate_a(internal_vxc)
121 case default
122 assert(.false.)
123 end select
124
125 pop_sub(x_fbe_calc)
126 end subroutine x_fbe_calc
127
128 ! -------------------------------------------------------------------------------------
131 subroutine solve_sturm_liouville(namespace, gr, st, space, fxc, vxc)
132 type(namespace_t), intent(in) :: namespace
133 type(grid_t), target, intent(in) :: gr
134 type(states_elec_t), target, intent(in) :: st
135 type(space_t), intent(in) :: space
136 real(real64), contiguous, intent(inout) :: fxc(:,:,:)
137 real(real64), contiguous, intent(inout) :: vxc(:,:)
138
139 real(real64), allocatable :: rhs(:)
140 integer :: iter, ispin
141 real(real64) :: res
142 real(real64), parameter :: threshold = 1e-7_real64
143 character(len=80) :: name
144
145 type(nl_operator_t) :: op(1)
146
147 push_sub(solve_sturm_liouville)
148
149 assert(ubound(fxc, dim=1) >= gr%np_part)
150
151 call mesh_init_mesh_aux(gr)
152
153 ! the smoothing is performed uing the same stencil as the Laplacian
154 name = 'FBE preconditioner'
155 call derivatives_get_lapl(gr%der, namespace, op, space, name, 1)
156 safe_allocate(diag_lapl(1:op(1)%np))
157 call dnl_operator_operate_diag(op(1), diag_lapl)
158 call nl_operator_end(op(1))
160 safe_allocate(rhs(1:gr%np))
161 safe_allocate(lapl_rho_aux(1:gr%np))
162
163 do ispin = 1, st%d%spin_channels
164 call dderivatives_div(gr%der, fxc(:, :, ispin), rhs)
165 rho_aux => st%rho(:, ispin)
166 call dderivatives_lapl(gr%der, rho_aux, lapl_rho_aux)
167
168 iter = 500
169 call dqmr_sym_gen_dotu(gr%np, vxc(:, ispin), rhs, &
171 iter, userdata=[c_loc(gr)], residue = res, threshold = threshold, showprogress = .false.)
172
173 write(message(1), '(a, i6, a)') "Info: Sturm-Liouville solver converged in ", iter, " iterations."
174 write(message(2), '(a, es14.6)') "Info: The residue is ", res
175 call messages_info(2, namespace=namespace)
176 end do
177
178 safe_deallocate_p(lapl_rho_aux)
179 safe_deallocate_a(rhs)
180 safe_deallocate_a(diag_lapl)
181
182 pop_sub(solve_sturm_liouville)
183 end subroutine solve_sturm_liouville
184
185 !----------------------------------------------------------------
191 subroutine sl_operator(x, hx, userdata)
192 real(real64), contiguous, intent(in) :: x(:)
193 real(real64), contiguous, intent(out) :: hx(:)
194 type(c_ptr), intent(in) :: userdata(:)
195
196 integer :: ip
197 real(real64), allocatable :: vxc(:)
198 real(real64), allocatable :: prod(:)
199 real(real64), allocatable :: lapl_vxc(:)
200 real(real64), allocatable :: lapl_product(:)
201 type(grid_t), pointer :: gr
202
203 assert(size(userdata) == 1)
204 assert(c_associated(userdata(1)))
205 call c_f_pointer(userdata(1), gr)
206
207 safe_allocate(vxc(1:gr%np_part))
208 safe_allocate(lapl_vxc(1:gr%np))
209 call lalg_copy(gr%np, x, vxc)
210 call dderivatives_lapl(gr%der, vxc, lapl_vxc)
211
212 safe_allocate(prod(1:gr%np_part))
213 safe_allocate(lapl_product(1:gr%np))
214 do ip = 1, gr%np_part
215 prod(ip) = rho_aux(ip) * vxc(ip)
216 end do
217 call dderivatives_lapl(gr%der, prod, lapl_product, set_bc=.false.)
218
219 do ip = 1, gr%np
220 hx(ip) = m_half * (rho_aux(ip) * lapl_vxc(ip) - vxc(ip) * lapl_rho_aux(ip) + lapl_product(ip))
221 end do
222
223 safe_deallocate_a(vxc)
224 safe_deallocate_a(prod)
225 safe_deallocate_a(lapl_vxc)
226 safe_deallocate_a(lapl_product)
227
228 end subroutine sl_operator
229
230 !----------------------------------------------------------------
234 subroutine preconditioner(x, hx, userdata)
235 real(real64), contiguous, intent(in) :: x(:)
236 real(real64), contiguous, intent(out) :: hx(:)
237 type(c_ptr), intent(in) :: userdata(:)
238
239 integer :: ip
240 type(grid_t), pointer :: gr
241
242 assert(size(userdata) == 1)
243 assert(c_associated(userdata(1)))
244 call c_f_pointer(userdata(1), gr)
245
246 !$omp parallel do
247 do ip = 1, gr%np
248 hx(ip) = x(ip) / (max(rho_aux(ip), 1d-12) * diag_lapl(ip))
249 end do
250
251 end subroutine preconditioner
252
253 ! -------------------------------------------------------------------------------------
255 real(real64) function get_virial_energy(gr, nspin, fxc) result(exc)
256 type(grid_t), intent(in) :: gr
257 integer, intent(in) :: nspin
258 real(real64), intent(in) :: fxc(:,:,:)
259
260 integer :: isp, idir, ip
261 real(real64), allocatable :: rfxc(:)
262 real(real64) :: xx(gr%box%dim), rr
263
264 push_sub(get_virial_energy)
265
266 exc = m_zero
267 do isp = 1, nspin
268 safe_allocate(rfxc(1:gr%np))
269 do ip = 1, gr%np
270 rfxc(ip) = m_zero
271 call mesh_r(gr, ip, rr, coords=xx)
272 do idir = 1, gr%box%dim
273 rfxc(ip) = rfxc(ip) + fxc(ip, idir, isp) * xx(idir)
274 end do
275 end do
276 exc = exc + dmf_integrate(gr, rfxc)
277 safe_deallocate_a(rfxc)
278 end do
279
280 pop_sub(get_virial_energy)
281 end function get_virial_energy
282
283
284 ! -------------------------------------------------------------------------------------
291 subroutine lda_c_fbe (st, n_blocks, l_dens, l_dedd, l_zk)
292 type(states_elec_t), intent(in) :: st
293 integer, intent(in) :: n_blocks
294 real(real64), intent(in) :: l_dens(:,:)
295 real(real64), intent(inout) :: l_dedd(:,:)
296 real(real64), optional, intent(inout) :: l_zk(:)
297
298 integer :: ip, ispin
299 real(real64) :: rho, beta, beta2, e_c
300 real(real64) :: q
301
302 push_sub(lda_c_fbe)
303
304 ! Set q such that we get the leading order of the r_s->0 limit for the HEG
305 q = ((5.0_real64*sqrt(m_pi)**5)/(m_three*(m_one-log(m_two))))**(m_third)
306 if (present(l_zk)) l_zk = m_zero
307
308 do ip = 1, n_blocks
309 rho = sum(l_dens(1:st%d%spin_channels, ip))
310 if (rho < 1e-20_real64) then
311 l_dedd(1:st%d%spin_channels, ip) = m_zero
312 cycle
313 end if
314 rho = max(rho, 1e-12_real64)
315 beta = q*rho**m_third
316 beta2 = beta**2
317
318 ! Potential
319 ! First part of the potential
320 l_dedd(1:st%d%spin_channels, ip) = (m_pi/(q**3))*((sqrt(m_pi)*beta/(m_one+sqrt(m_pi)*beta))**2 -m_one) * beta
321 ! Second part of the potential
322 l_dedd(1:st%d%spin_channels, ip) = l_dedd(1:st%d%spin_channels, ip) &
323 - (5.0_real64*sqrt(m_pi))/(m_three*q**3)*(log(m_one+sqrt(m_pi)*beta) &
324 -m_half/(m_one+sqrt(m_pi)*beta)**2 + m_two/(m_one+sqrt(m_pi)*beta)) + (5.0_real64*sqrt(m_pi))/(m_two*q**3)
325
326 if (st%d%nspin == 1 .and. present(l_zk)) then
327 ! Energy density
328 ! First part of the energy density
329 e_c = (9.0_real64*q**3)/m_two/beta &
330 - m_two*q**3*sqrt(m_pi) &
331 - 12.0_real64/beta2*(q**3/sqrt(m_pi)) &
332 + m_three/(m_pi*rho)*(m_one/(m_one+sqrt(m_pi)*beta) - m_one &
333 + 5.0_real64*log(m_one+sqrt(m_pi)*beta))
334
335 ! Second part of the energy density
336 e_c = e_c - 5.0_real64/6.0_real64*( &
337 7.0_real64*q**3/beta &
338 + m_three/(m_pi*rho*(m_one+sqrt(m_pi)*beta)) &
339 - 17.0_real64*q**3/sqrt(m_pi)/beta2 &
340 - 11.0_real64*q**3*sqrt(m_pi)/(m_three) &
341 + (20.0_real64/(m_pi*rho) + m_two*sqrt(m_pi)*q**3)*log(m_one+sqrt(m_pi)*beta) &
342 - m_three/(m_pi*rho))
343 e_c = e_c/(q**6)
344 l_zk(ip) = e_c
345 else if(st%d%nspin == 2) then
346 ! Here we have no energy density, so leave the potential unchanged
347 ! This is the approximate potential that we implement here
348 do ispin = 1, st%d%spin_channels
349 l_dedd(ispin, ip) = l_dedd(ispin, ip) * m_two * l_dens(-ispin+3, ip) / rho
350 end do
351 end if
352 end do
353
354 pop_sub(lda_c_fbe)
355 end subroutine lda_c_fbe
356
357 ! -------------------------------------------------------------------------------------
359 subroutine fbe_c_lda_sl (namespace, psolver, gr, st, space, ec, vxc)
360 type(namespace_t), intent(in) :: namespace
361 type(poisson_t), intent(in) :: psolver
362 type(grid_t), intent(in) :: gr
363 type(states_elec_t), intent(inout) :: st
364 type(space_t), intent(in) :: space
365 real(real64), intent(inout) :: ec
366 real(real64), contiguous, optional, intent(inout) :: vxc(:,:)
367
368 integer :: idir, ip, ispin
369 real(real64), allocatable :: fxc(:,:,:), internal_vxc(:,:), grad_rho(:,:,:), tmp1(:,:), tmp2(:,:)
370 real(real64) :: q, beta, rho, l_gdens
371
372 push_sub(fbe_c_lda_sl)
373
374 safe_allocate(internal_vxc(1:gr%np, 1:st%d%spin_channels))
375
376 ! Needed to get the initial guess for the iterative solution of the Sturm-Liouville equation
377 safe_allocate(tmp1(1:st%d%spin_channels, 1:gr%np))
378 safe_allocate(tmp2(1:st%d%spin_channels, 1:gr%np))
379 tmp1 = transpose(st%rho(1:gr%np, 1:st%d%spin_channels))
380 call lda_c_fbe(st, gr%np, tmp1, tmp2)
381 internal_vxc = transpose(tmp2)
382 safe_deallocate_a(tmp1)
383 safe_deallocate_a(tmp2)
384
385 ! Set q such that we get the leading order of the r_s->0 limit for the HEG
386 q = ((5.0_real64*sqrt(m_pi)**5)/(m_three*(m_one-log(m_two))))**(m_third)
387
388 safe_allocate(fxc(1:gr%np_part, 1:gr%box%dim, 1:st%d%spin_channels))
389 safe_allocate(grad_rho(1:gr%np, 1:gr%box%dim, 1:st%d%spin_channels))
390 do ispin = 1, st%d%spin_channels
391 call dderivatives_grad(gr%der, st%rho(:, ispin), grad_rho(:, :, ispin))
392 end do
393
394 do ispin = 1, st%d%spin_channels
395 do idir = 1, gr%box%dim
396 do ip = 1, gr%np
397 rho = sum(st%rho(ip, 1:st%d%spin_channels))
398 if (st%rho(ip, ispin) < 1e-20_real64) then
399 fxc(ip, idir, ispin) = m_zero
400 cycle
401 end if
402 rho = max(rho, 1e-12_real64)
403 beta = rho**m_third * q
404
405 l_gdens = sum(grad_rho(ip, idir, 1:st%d%spin_channels))
406
407 if (st%d%spin_channels == 1) then
408 fxc(ip, idir, ispin) = l_gdens * &
409 ( m_pi * beta**2/((m_one + sqrt(m_pi)*beta)**2) - m_one &
410 + m_third * m_pi * beta**2 / ((m_one + sqrt(m_pi)*beta)**3) )
411 else
412 fxc(ip, idir, ispin) = m_two * (grad_rho(ip, idir, 3-ispin) * &
413 (m_pi * beta**2/((m_one + sqrt(m_pi)*beta)**2) - m_one ) &
414 + l_gdens * (m_third * m_pi * beta**2 / ((m_one + sqrt(m_pi)*beta)**3) ) &
415 * st%rho(ip, 3-ispin) / rho)
416 end if
417
418 fxc(ip, idir, ispin) = fxc(ip, idir, ispin) * m_pi/(m_three*beta**2) * st%rho(ip, ispin)
419 end do
420 end do
421 end do
422
423 ! We solve the Sturm-Liouville equation
424 if (present(vxc)) then
425 call solve_sturm_liouville(namespace, gr, st, space, fxc, internal_vxc)
426 end if
427
428 ! Get the energy from the virial relation
429 ec = get_virial_energy(gr, st%d%spin_channels, fxc)
430
431 ! Adds the calculated potential
432 call lalg_axpy(gr%np, st%d%spin_channels, m_one, internal_vxc, vxc)
433
434 safe_deallocate_a(fxc)
435
436 pop_sub(fbe_c_lda_sl)
437 end subroutine fbe_c_lda_sl
438
439
440#include "undef.F90"
441#include "real.F90"
442#include "xc_fbe_inc.F90"
443
444#include "undef.F90"
445#include "complex.F90"
446#include "xc_fbe_inc.F90"
447
448end module xc_fbe_oct_m
449
450!! Local Variables:
451!! mode: f90
452!! coding: utf-8
453!! End:
constant times a vector plus a vector
Definition: lalg_basic.F90:173
Copies a vector x, to a vector y.
Definition: lalg_basic.F90:188
double log(double __x) __attribute__((__nothrow__
double sqrt(double __x) __attribute__((__nothrow__
This module implements batches of mesh functions.
Definition: batch.F90:135
This module implements common operations on batches of mesh functions.
Definition: batch_ops.F90:118
This module calculates the derivatives (gradients, Laplacians, etc.) of a function.
subroutine, public derivatives_get_lapl(this, namespace, op, space, name, order)
subroutine, public dderivatives_lapl(der, ff, op_ff, ghost_update, set_bc, factor)
apply the Laplacian to a mesh function
subroutine, public dderivatives_div(der, ff, op_ff, ghost_update, set_bc, to_cartesian)
apply the divergence operator to a vector of mesh functions
real(real64), parameter, public m_zero
Definition: global.F90:190
real(real64), parameter, public m_half
Definition: global.F90:196
real(real64), parameter, public m_one
Definition: global.F90:191
This module implements the underlying real-space grid.
Definition: grid.F90:119
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:117
This module defines various routines, operating on mesh functions.
real(real64) function, public dmf_dotu_aux(f1, f2)
dot product between two vectors (mesh functions) without conjugation
subroutine, public mesh_init_mesh_aux(mesh)
Initialise a pointer to the grid/mesh, that is globally exposed, such that low level mesh operations ...
real(real64) function, public dmf_nrm2_aux(ff)
calculate norm2 of a vector (mesh function)
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:120
pure subroutine, public mesh_r(mesh, ip, rr, origin, coords)
return the distance to the origin for a given grid point
Definition: mesh.F90:339
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:162
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:600
This module defines non-local operators.
subroutine, public dnl_operator_operate_diag(op, fo)
subroutine, public nl_operator_end(op)
This module is an helper to perform ring-pattern communications among all states.
This module is intended to contain "only mathematical" functions and procedures.
Definition: solvers.F90:117
subroutine, public dqmr_sym_gen_dotu(np, x, b, op, dotu, nrm2, prec, iter, userdata, residue, threshold, showprogress, converged, use_initial_guess)
for complex symmetric matrices W Chen and B Poirier, J Comput Phys 219, 198-209 (2006)
Definition: solvers.F90:1774
pure logical function, public states_are_real(st)
This module provides routines for communicating all batches in a ring-pattern scheme.
This module handles spin dimensions of the states and the k-point distribution.
subroutine, public lda_c_fbe(st, n_blocks, l_dens, l_dedd, l_zk)
Computes the local density correlation potential and energy obtained from the Colle-Salvetti approxim...
Definition: xc_fbe.F90:387
subroutine solve_sturm_liouville(namespace, gr, st, space, fxc, vxc)
Solve the Sturm-Liouville equation On entry, vxc is the adiabatic one, on exit, it is the solution of...
Definition: xc_fbe.F90:227
subroutine sl_operator(x, hx, userdata)
Computes Ax = \nabla\cdot(\rho\nabla x) = 1/2(\nabla^2 (\rho x) - x \nabla^2 \rho + \rho \nabla^2 x) ...
Definition: xc_fbe.F90:287
subroutine zx_fbe_calc(namespace, psolver, mesh, der, st, ex, vxc, fxc)
Definition: xc_fbe.F90:979
subroutine, public x_fbe_calc(id, namespace, psolver, gr, st, space, ex, vxc)
Interface to X(x_fbe_calc) Two possible run modes possible: adiabatic and Sturm-Liouville....
Definition: xc_fbe.F90:172
subroutine preconditioner(x, hx, userdata)
Simple preconditioner Here we need to approximate P^-1 We use the Jacobi approximation and that \nabl...
Definition: xc_fbe.F90:330
subroutine dx_fbe_calc(namespace, psolver, mesh, der, st, ex, vxc, fxc)
Definition: xc_fbe.F90:604
subroutine, public fbe_c_lda_sl(namespace, psolver, gr, st, space, ec, vxc)
Sturm-Liouville version of the FBE local-density correlation functional.
Definition: xc_fbe.F90:455
real(real64) function get_virial_energy(gr, nspin, fxc)
Computes the energy from the force virial relation.
Definition: xc_fbe.F90:351
integer, parameter, public xc_oep_x_fbe_sl
Exchange approximation based on the force balance equation - Sturn-Liouville version.
integer, parameter, public xc_oep_x_fbe
Exchange approximation based on the force balance equation.
Description of the grid, containing information on derivatives, stencil, and symmetries.
Definition: grid.F90:171