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
35 use math_oct_m
37 use mesh_oct_m
39 use mpi_oct_m
42 use parser_oct_m
47 use space_oct_m
55
56 implicit none
57
58 private
59 public :: &
60 x_fbe_calc, &
61 lda_c_fbe, &
63
64 real(real64), pointer :: rho_aux(:) => null()
65 real(real64), pointer :: lapl_rho_aux(:) => null()
66 real(real64), allocatable :: diag_lapl(:)
67
68
69contains
70
71 ! -------------------------------------------------------------------------------------
77 subroutine x_fbe_calc (id, namespace, psolver, gr, st, space, ex, vxc)
78 integer, intent(in) :: id
79 type(namespace_t), intent(in) :: namespace
80 type(poisson_t), intent(in) :: psolver
81 type(grid_t), target, intent(in) :: gr
82 type(states_elec_t), intent(inout) :: st
83 type(space_t), intent(in) :: space
84 real(real64), intent(inout) :: ex
85 real(real64), contiguous, optional, intent(inout) :: vxc(:,:)
86
87 real(real64), allocatable :: fxc(:,:,:), internal_vxc(:,:)
88
89 push_sub(x_fbe_calc)
90
91 select case(id)
92 case(xc_oep_x_fbe)
93 if (states_are_real(st)) then
94 call dx_fbe_calc(namespace, psolver, gr, st, ex, vxc=vxc)
95 else
96 call zx_fbe_calc(namespace, psolver, gr, st, ex, vxc=vxc)
97 end if
98 case(xc_oep_x_fbe_sl)
99 safe_allocate(fxc(1:gr%np_part, 1:gr%box%dim, 1:st%d%spin_channels))
100 safe_allocate(internal_vxc(1:gr%np, 1:st%d%spin_channels))
101 internal_vxc = m_zero
102 ! We first compute the force density
103 if (states_are_real(st)) then
104 call dx_fbe_calc(namespace, psolver, gr, st, ex, vxc=internal_vxc, fxc=fxc)
105 else
106 call zx_fbe_calc(namespace, psolver, gr, st, ex, vxc=internal_vxc, fxc=fxc)
107 end if
108
109 ! We solve the Sturm-Liouville equation
110 if (present(vxc)) then
111 call solve_sturm_liouville(namespace, gr, st, space, fxc, internal_vxc)
112 end if
113
114 ! Get the energy from the virial relation
115 ex = get_virial_energy(gr, st%d%spin_channels, fxc)
116
117 ! Adds the calculated potential
118 call lalg_axpy(gr%np, st%d%spin_channels, m_one, internal_vxc, vxc)
119
120 safe_deallocate_a(fxc)
121 safe_deallocate_a(internal_vxc)
122 case default
123 assert(.false.)
124 end select
125
126 pop_sub(x_fbe_calc)
127 end subroutine x_fbe_calc
128
129 ! -------------------------------------------------------------------------------------
132 subroutine solve_sturm_liouville(namespace, gr, st, space, fxc, vxc)
133 type(namespace_t), intent(in) :: namespace
134 type(grid_t), target, intent(in) :: gr
135 type(states_elec_t), target, intent(in) :: st
136 type(space_t), intent(in) :: space
137 real(real64), contiguous, intent(inout) :: fxc(:,:,:)
138 real(real64), contiguous, intent(inout) :: vxc(:,:)
139
140 real(real64), allocatable :: rhs(:)
141 integer :: iter, ispin
142 real(real64) :: res
143 real(real64), parameter :: threshold = 1e-7_real64
144 character(len=80) :: name
145
146 type(nl_operator_t) :: op(1)
147
148 push_sub(solve_sturm_liouville)
149
150 assert(ubound(fxc, dim=1) >= gr%np_part)
151
152 call mesh_init_mesh_aux(gr)
153
154 ! the smoothing is performed uing the same stencil as the Laplacian
155 name = 'FBE preconditioner'
156 call derivatives_get_lapl(gr%der, namespace, op, space, name, 1)
157 safe_allocate(diag_lapl(1:op(1)%np))
158 call dnl_operator_operate_diag(op(1), diag_lapl)
159 call nl_operator_end(op(1))
161 safe_allocate(rhs(1:gr%np))
162 safe_allocate(lapl_rho_aux(1:gr%np))
163
164 do ispin = 1, st%d%spin_channels
165 call dderivatives_div(gr%der, fxc(:, :, ispin), rhs)
166 rhs=-rhs
167 rho_aux => st%rho(:, ispin)
168 call dderivatives_lapl(gr%der, rho_aux, lapl_rho_aux)
169
170 iter = 500
171 call dqmr_sym_gen_dotu(gr%np, vxc(:, ispin), rhs, &
173 iter, userdata=[c_loc(gr)], residue = res, threshold = threshold, showprogress = .false.)
174
175 write(message(1), '(a, i6, a)') "Info: Sturm-Liouville solver converged in ", iter, " iterations."
176 write(message(2), '(a, es14.6)') "Info: The residue is ", res
177 call messages_info(2, namespace=namespace)
178 end do
179
180 safe_deallocate_p(lapl_rho_aux)
181 safe_deallocate_a(rhs)
182 safe_deallocate_a(diag_lapl)
183
184 nullify(rho_aux)
185
186 pop_sub(solve_sturm_liouville)
187 end subroutine solve_sturm_liouville
188
189 !----------------------------------------------------------------
195 subroutine sl_operator(x, hx, userdata)
196 real(real64), contiguous, intent(in) :: x(:)
197 real(real64), contiguous, intent(out) :: hx(:)
198 type(c_ptr), intent(in) :: userdata(:)
199
200 integer :: ip
201 real(real64), allocatable :: vxc(:)
202 real(real64), allocatable :: prod(:)
203 real(real64), allocatable :: lapl_vxc(:)
204 real(real64), allocatable :: lapl_product(:)
205 type(grid_t), pointer :: gr
206
207 assert(size(userdata) == 1)
208 assert(c_associated(userdata(1)))
209 call c_f_pointer(userdata(1), gr)
210
211 safe_allocate(vxc(1:gr%np_part))
212 safe_allocate(lapl_vxc(1:gr%np))
213 call lalg_copy(gr%np, x, vxc)
214 call dderivatives_lapl(gr%der, vxc, lapl_vxc)
215
216 safe_allocate(prod(1:gr%np_part))
217 safe_allocate(lapl_product(1:gr%np))
218 do ip = 1, gr%np_part
219 prod(ip) = rho_aux(ip) * vxc(ip)
220 end do
221 call dderivatives_lapl(gr%der, prod, lapl_product, set_bc=.false.)
222
223 do ip = 1, gr%np
224 hx(ip) = m_half * (rho_aux(ip) * lapl_vxc(ip) - vxc(ip) * lapl_rho_aux(ip) + lapl_product(ip))
225 end do
226
227 safe_deallocate_a(vxc)
228 safe_deallocate_a(prod)
229 safe_deallocate_a(lapl_vxc)
230 safe_deallocate_a(lapl_product)
231
232 end subroutine sl_operator
233
234 !----------------------------------------------------------------
238 subroutine preconditioner(x, hx, userdata)
239 real(real64), contiguous, intent(in) :: x(:)
240 real(real64), contiguous, intent(out) :: hx(:)
241 type(c_ptr), intent(in) :: userdata(:)
242
243 integer :: ip
244 type(grid_t), pointer :: gr
245
246 assert(size(userdata) == 1)
247 assert(c_associated(userdata(1)))
248 call c_f_pointer(userdata(1), gr)
249
250 !$omp parallel do
251 do ip = 1, gr%np
252 hx(ip) = x(ip) / (max(rho_aux(ip), 1.0e-12_real64) * diag_lapl(ip))
253 end do
254
255 end subroutine preconditioner
256
257 ! -------------------------------------------------------------------------------------
259 real(real64) function get_virial_energy(gr, nspin, fxc) result(exc)
260 type(grid_t), intent(in) :: gr
261 integer, intent(in) :: nspin
262 real(real64), intent(in) :: fxc(:,:,:)
263
264 integer :: isp, idir, ip
265 real(real64), allocatable :: rfxc(:)
266 real(real64) :: xx(gr%box%dim), rr
267
268 push_sub(get_virial_energy)
269
270 exc = m_zero
271 do isp = 1, nspin
272 safe_allocate(rfxc(1:gr%np))
273 do ip = 1, gr%np
274 rfxc(ip) = m_zero
275 call mesh_r(gr, ip, rr, coords=xx)
276 do idir = 1, gr%box%dim
277 rfxc(ip) = rfxc(ip) + fxc(ip, idir, isp) * xx(idir)
278 end do
279 end do
280 exc = exc + dmf_integrate(gr, rfxc)
281 safe_deallocate_a(rfxc)
282 end do
283
284 pop_sub(get_virial_energy)
285 end function get_virial_energy
286
287
288 ! -------------------------------------------------------------------------------------
295 subroutine lda_c_fbe (st, n_blocks, l_dens, l_dedd, l_zk)
296 type(states_elec_t), intent(in) :: st
297 integer, intent(in) :: n_blocks
298 real(real64), intent(in) :: l_dens(:,:)
299 real(real64), intent(inout) :: l_dedd(:,:)
300 real(real64), optional, intent(inout) :: l_zk(:)
301
302 integer :: ip, ispin
303 real(real64) :: rho, beta, beta2, e_c
304 real(real64) :: q
305
306 push_sub(lda_c_fbe)
307
308 ! Set q such that we get the leading order of the r_s->0 limit for the HEG
309 q = ((5.0_real64*sqrt(m_pi)**5)/(m_three*(m_one-log(m_two))))**(m_third)
310 if (present(l_zk)) l_zk = m_zero
311
312 do ip = 1, n_blocks
313 rho = sum(l_dens(1:st%d%spin_channels, ip))
314 if (rho < 1e-20_real64) then
315 l_dedd(1:st%d%spin_channels, ip) = m_zero
316 cycle
317 end if
318 rho = max(rho, 1e-12_real64)
319 beta = q*rho**m_third
320 beta2 = beta**2
321
322 ! Potential
323 ! First part of the potential
324 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
325 ! Second part of the potential
326 l_dedd(1:st%d%spin_channels, ip) = l_dedd(1:st%d%spin_channels, ip) &
327 - (5.0_real64*sqrt(m_pi))/(m_three*q**3)*(log(m_one+sqrt(m_pi)*beta) &
328 -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)
329
330 if (st%d%nspin == 1 .and. present(l_zk)) then
331 ! Energy density
332 ! First part of the energy density
333 e_c = (9.0_real64*q**3)/m_two/beta &
334 - m_two*q**3*sqrt(m_pi) &
335 - 12.0_real64/beta2*(q**3/sqrt(m_pi)) &
336 + m_three/(m_pi*rho)*(m_one/(m_one+sqrt(m_pi)*beta) - m_one &
337 + 5.0_real64*log(m_one+sqrt(m_pi)*beta))
338
339 ! Second part of the energy density
340 e_c = e_c - 5.0_real64/6.0_real64*( &
341 7.0_real64*q**3/beta &
342 + m_three/(m_pi*rho*(m_one+sqrt(m_pi)*beta)) &
343 - 17.0_real64*q**3/sqrt(m_pi)/beta2 &
344 - 11.0_real64*q**3*sqrt(m_pi)/(m_three) &
345 + (20.0_real64/(m_pi*rho) + m_two*sqrt(m_pi)*q**3)*log(m_one+sqrt(m_pi)*beta) &
346 - m_three/(m_pi*rho))
347 e_c = e_c/(q**6)
348 l_zk(ip) = e_c
349 else if(st%d%nspin == 2) then
350 ! Here we have no energy density, so leave the potential unchanged
351 ! This is the approximate potential that we implement here
352 do ispin = 1, st%d%spin_channels
353 l_dedd(ispin, ip) = l_dedd(ispin, ip) * m_two * l_dens(-ispin+3, ip) / rho
354 end do
355 end if
356 end do
357
358 pop_sub(lda_c_fbe)
359 end subroutine lda_c_fbe
360
361 ! -------------------------------------------------------------------------------------
363 subroutine fbe_c_lda_sl (namespace, gr, st, space, ec, vxc)
364 type(namespace_t), intent(in) :: namespace
365 type(grid_t), target, intent(in) :: gr
366 type(states_elec_t), intent(inout) :: st
367 type(space_t), intent(in) :: space
368 real(real64), intent(inout) :: ec
369 real(real64), contiguous, optional, intent(inout) :: vxc(:,:)
370
371 integer :: idir, ip, ispin
372 real(real64), allocatable :: fxc(:,:,:), internal_vxc(:,:), grad_rho(:,:,:), tmp1(:,:), tmp2(:,:)
373 real(real64) :: q, beta, rho, l_gdens
374
375 push_sub(fbe_c_lda_sl)
376
377 safe_allocate(internal_vxc(1:gr%np, 1:st%d%spin_channels))
378
379 ! Needed to get the initial guess for the iterative solution of the Sturm-Liouville equation
380 safe_allocate(tmp1(1:st%d%spin_channels, 1:gr%np))
381 safe_allocate(tmp2(1:st%d%spin_channels, 1:gr%np))
382 tmp1 = transpose(st%rho(1:gr%np, 1:st%d%spin_channels))
383 call lda_c_fbe(st, gr%np, tmp1, tmp2)
384 internal_vxc = transpose(tmp2)
385 safe_deallocate_a(tmp1)
386 safe_deallocate_a(tmp2)
387
388 ! Set q such that we get the leading order of the r_s->0 limit for the HEG
389 q = ((5.0_real64*sqrt(m_pi)**5)/(m_three*(m_one-log(m_two))))**(m_third)
391 safe_allocate(fxc(1:gr%np_part, 1:gr%box%dim, 1:st%d%spin_channels))
392 safe_allocate(grad_rho(1:gr%np, 1:gr%box%dim, 1:st%d%spin_channels))
393 do ispin = 1, st%d%spin_channels
394 call dderivatives_grad(gr%der, st%rho(:, ispin), grad_rho(:, :, ispin))
395 end do
396
397 do ispin = 1, st%d%spin_channels
398 do idir = 1, gr%box%dim
399 do ip = 1, gr%np
400 rho = sum(st%rho(ip, 1:st%d%spin_channels))
401 if (st%rho(ip, ispin) < 1e-20_real64) then
402 fxc(ip, idir, ispin) = m_zero
403 cycle
404 end if
405 rho = max(rho, 1e-12_real64)
406 beta = rho**m_third * q
407
408 l_gdens = sum(grad_rho(ip, idir, 1:st%d%spin_channels))
409
410 if (st%d%spin_channels == 1) then
411 fxc(ip, idir, ispin) = l_gdens * &
412 ( m_pi * beta**2/((m_one + sqrt(m_pi)*beta)**2) - m_one &
413 + m_third * m_pi * beta**2 / ((m_one + sqrt(m_pi)*beta)**3) )
414 else
415 fxc(ip, idir, ispin) = m_two * (grad_rho(ip, idir, 3-ispin) * &
416 (m_pi * beta**2/((m_one + sqrt(m_pi)*beta)**2) - m_one ) &
417 + l_gdens * (m_third * m_pi * beta**2 / ((m_one + sqrt(m_pi)*beta)**3) ) &
418 * st%rho(ip, 3-ispin) / rho)
419 end if
420
421 fxc(ip, idir, ispin) = fxc(ip, idir, ispin) * m_pi/(m_three*beta**2) * st%rho(ip, ispin)
422 end do
423 end do
424 end do
425
426 ! We solve the Sturm-Liouville equation
427 if (present(vxc)) then
428 call solve_sturm_liouville(namespace, gr, st, space, fxc, internal_vxc)
429 end if
430
431 ! Get the energy from the virial relation
432 ec = get_virial_energy(gr, st%d%spin_channels, fxc)
433
434 ! Adds the calculated potential
435 call lalg_axpy(gr%np, st%d%spin_channels, m_one, internal_vxc, vxc)
436
437 safe_deallocate_a(fxc)
438
439 pop_sub(fbe_c_lda_sl)
440 end subroutine fbe_c_lda_sl
441
442#include "undef.F90"
443#include "real.F90"
444#include "xc_fbe_inc.F90"
445
446#include "undef.F90"
447#include "complex.F90"
448#include "xc_fbe_inc.F90"
449
450end module xc_fbe_oct_m
451
452!! Local Variables:
453!! mode: f90
454!! coding: utf-8
455!! 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:191
real(real64), parameter, public m_half
Definition: global.F90:197
real(real64), parameter, public m_one
Definition: global.F90:192
This module implements the underlying real-space grid.
Definition: grid.F90:119
Computes and , suitable as an operator callback for iterative solvers (CG, QMR, etc....
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:342
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:594
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:391
subroutine dx_fbe_calc(namespace, psolver, gr, st, ex, vxc, fxc)
Definition: xc_fbe.F90:606
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:228
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:291
subroutine zx_fbe_calc(namespace, psolver, gr, st, ex, vxc, fxc)
Definition: xc_fbe.F90:1003
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:173
subroutine, public fbe_c_lda_sl(namespace, gr, st, space, ec, vxc)
Sturm-Liouville version of the FBE local-density correlation functional.
Definition: xc_fbe.F90:459
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:334
real(real64) function get_virial_energy(gr, nspin, fxc)
Computes the energy from the force virial relation.
Definition: xc_fbe.F90:355
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