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 type(grid_t), pointer :: gr_aux => null()
64 real(real64), pointer :: 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)
116 ! Adds the calculated potential
117 if (present(vxc)) then
118 call lalg_axpy(gr%np, st%d%spin_channels, m_one, internal_vxc, vxc)
119 end if
120
121 safe_deallocate_a(fxc)
122 safe_deallocate_a(internal_vxc)
123 case default
124 assert(.false.)
125 end select
126
127 pop_sub(x_fbe_calc)
128 end subroutine x_fbe_calc
129
130 ! -------------------------------------------------------------------------------------
133 subroutine solve_sturm_liouville(namespace, gr, st, space, fxc, vxc)
134 type(namespace_t), intent(in) :: namespace
135 type(grid_t), target, intent(in) :: gr
136 type(states_elec_t), target, intent(in) :: st
137 type(space_t), intent(in) :: space
138 real(real64), contiguous, intent(inout) :: fxc(:,:,:)
139 real(real64), contiguous, intent(inout) :: vxc(:,:)
140
141 real(real64), allocatable :: rhs(:)
142 integer :: iter, ispin
143 real(real64) :: res
144 real(real64), parameter :: threshold = 1e-7_real64
145 character(len=32) :: name
146
147 type(nl_operator_t) :: op(1)
148
149 push_sub(solve_sturm_liouville)
150
151 assert(ubound(fxc, dim=1) >= gr%np_part)
152
153 gr_aux => gr
154 call mesh_init_mesh_aux(gr)
155
156 ! the smoothing is performed uing the same stencil as the Laplacian
157 name = 'FBE preconditioner'
158 call derivatives_get_lapl(gr%der, namespace, op, space, name, 1)
159 safe_allocate(diag_lapl(1:op(1)%np))
160 call dnl_operator_operate_diag(op(1), diag_lapl)
161 call nl_operator_end(op(1))
162
163 safe_allocate(rhs(1:gr%np))
164
165 do ispin = 1, st%d%spin_channels
166 call dderivatives_div(gr%der, fxc(:, :, ispin), rhs)
167 rhs=-rhs
168 rho_aux => st%rho(:, ispin)
170 iter = 500
171 call dqmr_sym_gen_dotu(gr%np, vxc(:, ispin), rhs, &
173 iter, 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_a(rhs)
181
182 safe_deallocate_a(diag_lapl)
183
184 nullify(rho_aux)
185 nullify(gr_aux)
186
187 pop_sub(solve_sturm_liouville)
188 contains
189 !----------------------------------------------------------------
191 subroutine sl_operator(x, hx)
192 real(real64), contiguous, intent(in) :: x(:)
193 real(real64), contiguous, intent(out) :: hx(:)
194
195 integer :: ip, idir
196 real(real64), allocatable :: vxc(:)
197 real(real64), allocatable :: grad_vxc(:,:)
198
199 safe_allocate(vxc(1:gr_aux%np_part))
200 safe_allocate(grad_vxc(1:gr_aux%np_part, 1:gr_aux%box%dim))
201 call lalg_copy(gr_aux%np, x, vxc)
202
203 call dderivatives_grad(gr_aux%der, vxc, grad_vxc)
204
205 !$omp parallel
206 do idir = 1, gr_aux%box%dim
207 !$omp do
208 do ip = 1, gr_aux%np
209 grad_vxc(ip, idir) = grad_vxc(ip, idir)*rho_aux(ip)
210 end do
211 !$omp end do nowait
212 end do
213 !$omp end parallel
214
215 call dderivatives_div(gr_aux%der, grad_vxc, hx)
216
217 safe_deallocate_a(vxc)
218 safe_deallocate_a(grad_vxc)
219 end subroutine sl_operator
220
221 !----------------------------------------------------------------
225 subroutine preconditioner(x, hx)
226 real(real64), contiguous, intent(in) :: x(:)
227 real(real64), contiguous, intent(out) :: hx(:)
228
229 integer :: ip
230
231 !$omp parallel do
232 do ip = 1, gr_aux%np
233 hx(ip) = x(ip) / (max(rho_aux(ip), 1d-12) * diag_lapl(ip))
234 end do
235
236 end subroutine preconditioner
237
238 end subroutine solve_sturm_liouville
239
240 ! -------------------------------------------------------------------------------------
242 real(real64) function get_virial_energy(gr, nspin, fxc) result(exc)
243 type(grid_t), intent(in) :: gr
244 integer, intent(in) :: nspin
245 real(real64), intent(in) :: fxc(:,:,:)
246
247 integer :: isp, idir, ip
248 real(real64), allocatable :: rfxc(:)
249 real(real64) :: xx(gr%box%dim), rr
250
251 push_sub(get_virial_energy)
252
253 exc = m_zero
254 do isp = 1, nspin
255 safe_allocate(rfxc(1:gr%np))
256 do ip = 1, gr%np
257 rfxc(ip) = m_zero
258 call mesh_r(gr, ip, rr, coords=xx)
259 do idir = 1, gr%box%dim
260 rfxc(ip) = rfxc(ip) + fxc(ip, idir, isp) * xx(idir)
261 end do
262 end do
263 exc = exc + dmf_integrate(gr, rfxc)
264 safe_deallocate_a(rfxc)
265 end do
266
267 pop_sub(get_virial_energy)
268 end function get_virial_energy
269
270
271 ! -------------------------------------------------------------------------------------
277 subroutine lda_c_fbe (st, n_blocks, l_dens, l_dedd, l_zk)
278 type(states_elec_t), intent(in) :: st
279 integer, intent(in) :: n_blocks
280 real(real64), intent(in) :: l_dens(:,:)
281 real(real64), intent(inout) :: l_dedd(:,:)
282 real(real64), optional, intent(inout) :: l_zk(:)
283
284 integer :: ip, ispin
285 real(real64) :: rho, beta, beta2, e_c
286 real(real64) :: q
287
288 push_sub(lda_c_fbe)
289
290 ! Set q such that we get the leading order of the r_s->0 limit for the HEG
291 q = ((5.0_real64*sqrt(m_pi)**5)/(m_three*(m_one-log(m_two))))**(m_third)
292 if (present(l_zk)) l_zk = m_zero
293
294 do ip = 1, n_blocks
295 rho = sum(l_dens(1:st%d%spin_channels, ip))
296 if (rho < 1e-20_real64) then
297 l_dedd(1:st%d%spin_channels, ip) = m_zero
298 cycle
299 end if
300 rho = max(rho, 1e-12_real64)
301 beta = q*rho**m_third
302 beta2 = beta**2
303
304 ! Potential
305 ! First part of the potential
306 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
307 ! Second part of the potential
308 l_dedd(1:st%d%spin_channels, ip) = l_dedd(1:st%d%spin_channels, ip) &
309 - (5.0_real64*sqrt(m_pi))/(m_three*q**3)*(log(m_one+sqrt(m_pi)*beta) &
310 -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)
311
312 if (st%d%nspin == 1 .and. present(l_zk)) then
313 ! Energy density
314 ! First part of the energy density
315 e_c = (9.0_real64*q**3)/m_two/beta &
316 - m_two*q**3*sqrt(m_pi) &
317 - 12.0_real64/beta2*(q**3/sqrt(m_pi)) &
318 + m_three/(m_pi*rho)*(m_one/(m_one+sqrt(m_pi)*beta) - m_one &
319 + 5.0_real64*log(m_one+sqrt(m_pi)*beta))
320
321 ! Second part of the energy density
322 e_c = e_c - 5.0_real64/6.0_real64*( &
323 7.0_real64*q**3/beta &
324 + m_three/(m_pi*rho*(m_one+sqrt(m_pi)*beta)) &
325 - 17.0_real64*q**3/sqrt(m_pi)/beta2 &
326 - 11.0_real64*q**3*sqrt(m_pi)/(m_three) &
327 + (20.0_real64/(m_pi*rho) + m_two*sqrt(m_pi)*q**3)*log(m_one+sqrt(m_pi)*beta) &
328 - m_three/(m_pi*rho))
329 e_c = e_c/(q**6)
330 l_zk(ip) = e_c
331 else if(st%d%nspin == 2) then
332 ! Here we have no energy density, so leave the potential unchanged
333 ! This is the approximate potential that we implement here
334 do ispin = 1, st%d%spin_channels
335 l_dedd(ispin, ip) = l_dedd(ispin, ip) * m_two * l_dens(-ispin+3, ip) / rho
336 end do
337 end if
338 end do
339
340 pop_sub(lda_c_fbe)
341 end subroutine lda_c_fbe
342
343 ! -------------------------------------------------------------------------------------
345 subroutine fbe_c_lda_sl (namespace, psolver, gr, st, space, ec, vxc)
346 type(namespace_t), intent(in) :: namespace
347 type(poisson_t), intent(in) :: psolver
348 type(grid_t), intent(in) :: gr
349 type(states_elec_t), intent(inout) :: st
350 type(space_t), intent(in) :: space
351 real(real64), intent(inout) :: ec
352 real(real64), contiguous, optional, intent(inout) :: vxc(:,:)
353
354 integer :: idir, ip, ispin
355 real(real64), allocatable :: fxc(:,:,:), internal_vxc(:,:), grad_rho(:,:,:), tmp1(:,:), tmp2(:,:)
356 real(real64) :: q, beta, rho, l_gdens
357
358 push_sub(fbe_c_lda_sl)
359
360 safe_allocate(internal_vxc(1:gr%np, 1:st%d%spin_channels))
361
362 ! Needed to get the initial guess for the iterative solution of the Sturm-Liouville equation
363 safe_allocate(tmp1(1:st%d%spin_channels, 1:gr%np))
364 safe_allocate(tmp2(1:st%d%spin_channels, 1:gr%np))
365 tmp1 = transpose(st%rho(1:gr%np, 1:st%d%spin_channels))
366 call lda_c_fbe(st, gr%np, tmp1, tmp2)
367 internal_vxc = transpose(tmp2)
368 safe_deallocate_a(tmp1)
369 safe_deallocate_a(tmp2)
371 ! Set q such that we get the leading order of the r_s->0 limit for the HEG
372 q = ((5.0_real64*sqrt(m_pi)**5)/(m_three*(m_one-log(m_two))))**(m_third)
373
374 safe_allocate(fxc(1:gr%np_part, 1:gr%box%dim, 1:st%d%spin_channels))
375 safe_allocate(grad_rho(1:gr%np, 1:gr%box%dim, 1:st%d%spin_channels))
376 do ispin = 1, st%d%spin_channels
377 call dderivatives_grad(gr%der, st%rho(:, ispin), grad_rho(:, :, ispin))
378 end do
379
380 do ispin = 1, st%d%spin_channels
381 do idir = 1, gr%box%dim
382 do ip = 1, gr%np
383 rho = sum(st%rho(ip, 1:st%d%spin_channels))
384 if (st%rho(ip, ispin) < 1e-20_real64) then
385 fxc(ip, idir, ispin) = m_zero
386 cycle
387 end if
388 rho = max(rho, 1e-12_real64)
389 beta = rho**m_third * q
390
391 l_gdens = sum(grad_rho(ip, idir, 1:st%d%spin_channels))
392
393 if (st%d%spin_channels == 1) then
394 fxc(ip, idir, ispin) = l_gdens * &
395 ( m_pi * beta**2/((m_one + sqrt(m_pi)*beta)**2) - m_one &
396 + m_third * m_pi * beta**2 / ((m_one + sqrt(m_pi)*beta)**3) )
397 else
398 fxc(ip, idir, ispin) = m_two * (grad_rho(ip, idir, 3-ispin) * &
399 (m_pi * beta**2/((m_one + sqrt(m_pi)*beta)**2) - m_one ) &
400 + l_gdens * (m_third * m_pi * beta**2 / ((m_one + sqrt(m_pi)*beta)**3) ) &
401 * st%rho(ip, 3-ispin) / rho)
402 end if
403
404 fxc(ip, idir, ispin) = fxc(ip, idir, ispin) * m_pi/(m_three*beta**2) * st%rho(ip, ispin)
405 end do
406 end do
407 end do
408
409 ! We solve the Sturm-Liouville equation
410 if (present(vxc)) then
411 call solve_sturm_liouville(namespace, gr, st, space, fxc, internal_vxc)
412 end if
413
414 ! Get the energy from the virial relation
415 ec = get_virial_energy(gr, st%d%spin_channels, fxc)
416
417 ! Adds the calculated potential
418 if (present(vxc)) then
419 call lalg_axpy(gr%np, st%d%spin_channels, m_one, internal_vxc, vxc)
420 end if
421
422 safe_deallocate_a(fxc)
423
424 pop_sub(fbe_c_lda_sl)
425 end subroutine fbe_c_lda_sl
426
427
428#include "undef.F90"
429#include "real.F90"
430#include "xc_fbe_inc.F90"
431
432#include "undef.F90"
433#include "complex.F90"
434#include "xc_fbe_inc.F90"
435
436end module xc_fbe_oct_m
437
438!! Local Variables:
439!! mode: f90
440!! coding: utf-8
441!! End:
constant times a vector plus a vector
Definition: lalg_basic.F90:171
Copies a vector x, to a vector y.
Definition: lalg_basic.F90:186
double log(double __x) __attribute__((__nothrow__
double sqrt(double __x) __attribute__((__nothrow__
This module implements batches of mesh functions.
Definition: batch.F90:133
This module implements common operations on batches of mesh functions.
Definition: batch_ops.F90:116
This module calculates the derivatives (gradients, Laplacians, etc.) of a function.
subroutine, public dderivatives_grad(der, ff, op_ff, ghost_update, set_bc, to_cartesian)
apply the gradient to a mesh function
subroutine, public derivatives_get_lapl(this, namespace, op, space, name, order)
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:188
real(real64), parameter, public m_one
Definition: global.F90:189
This module implements the underlying real-space grid.
Definition: grid.F90:117
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:115
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:118
pure subroutine, public mesh_r(mesh, ip, rr, origin, coords)
return the distance to the origin for a given grid point
Definition: mesh.F90:336
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:161
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:617
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:115
subroutine, public dqmr_sym_gen_dotu(np, x, b, op, dotu, nrm2, prec, iter, 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:1802
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:371
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 zx_fbe_calc(namespace, psolver, mesh, der, st, ex, vxc, fxc)
Definition: xc_fbe.F90:961
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:170
subroutine dx_fbe_calc(namespace, psolver, mesh, der, st, ex, vxc, fxc)
Definition: xc_fbe.F90:590
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:439
real(real64) function get_virial_energy(gr, nspin, fxc)
Computes the energy from the force virial relation.
Definition: xc_fbe.F90:336
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:168
subroutine sl_operator(x, hx)
Computes Ax = \nabla\cdot(\rho\nabla x)
Definition: xc_fbe.F90:285
subroutine preconditioner(x, hx)
Simple preconditioner Here we need to approximate P^-1 We use the Jacobi approximation and that \nabl...
Definition: xc_fbe.F90:319