Octopus
stress.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2016 M. Marques, A. Castro, A. Rubio, G. Bertsch
2!! Copyright (C) 2023 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
22! ---------------------------------------------------------
25module stress_oct_m
28 use comm_oct_m
29 use debug_oct_m
34 use energy_oct_m
36 use epot_oct_m
37 use global_oct_m
38 use grid_oct_m
40 use io_oct_m
42 use ions_oct_m
44 use, intrinsic :: iso_fortran_env
48 use lda_u_oct_m
51 use math_oct_m
52 use mesh_oct_m
56 use mpi_oct_m
62 use ps_oct_m
64 use space_oct_m
74 use types_oct_m
75 use unit_oct_m
77 use v_ks_oct_m
79 use xc_oct_m
80 use xc_f03_lib_m
82 implicit none
83
84 private
85 public :: &
89
90contains
91
92 ! ---------------------------------------------------------
94 subroutine stress_calculate(namespace, gr, hm, st, ions, ks, ext_partners)
95 type(namespace_t), intent(in) :: namespace
96 type(grid_t), intent(inout) :: gr
97 type(hamiltonian_elec_t), intent(inout) :: hm
98 type(states_elec_t), target, intent(inout) :: st
99 type(ions_t), intent(inout) :: ions
100 type(v_ks_t), intent(in) :: ks
101 type(partner_list_t), intent(in) :: ext_partners
102
103 real(real64), allocatable :: rho_total(:)
104 real(real64) :: stress(3,3) ! stress tensor in Cartesian coordinate
105 real(real64) :: stress_kin(3,3), stress_Hartree(3,3), stress_xc(3,3), stress_xc_nlcc(3,3)
106 real(real64) :: stress_ps(3,3), stress_ps_nl(3,3), stress_ps_local(3,3), stress_ii(3,3)
107 real(real64) :: stress_ps_aug(3,3)
108 real(real64) :: stress_hubbard(3,3)
109 integer :: ip
110 real(real64), allocatable :: vh(:)
111 real(real64), allocatable :: grad_vh(:,:)
112 real(real64) :: ehartree
113 real(real64), contiguous, pointer :: rho(:)
114
115 call profiling_in("STRESS_CALCULATE")
116 push_sub(stress_calculate)
117
118 if (st%requires_generalized_overlap .and. st%d%ispin == spinors) then
119 call messages_not_implemented("Stress with ultrasoft/PAW generalized overlap and spinors", namespace=namespace)
120 end if
121 ! The ultrasoft stress terms have not been validated with the PAW one-center contributions.
122 if (hm%ep%has_paw) then
123 call messages_not_implemented("Stress with PAW", namespace=namespace)
124 end if
125
126 if (st%wfs_type /= type_cmplx) then
127 write(message(1),'(a)') 'The stress tensors for real wavefunctions has not been implemented!'
128
129 if (hm%kpoints%full%npoints == 1) then
130 write(message(2),'(a)') 'For testing this feature, you can add ForceComplex=yes to the input file'
131 call messages_fatal(2, namespace=namespace)
132 end if
133
134 call messages_fatal(1, namespace=namespace)
135 end if
136
137 if (ions%space%periodic_dim == 1) then
138 call messages_not_implemented("Stress tensor for 1D periodic systems", namespace=namespace)
139 end if
140
141 if (.not. ions%space%is_periodic()) then
142 write(message(1),'(a)') 'The stress tensor cannot be computed for isolated systems'
143 call messages_fatal(1, namespace=namespace)
144 end if
145
146 if (ks%vdw%vdw_correction /= option__vdwcorrection__none .and. .not. any(ks%vdw%vdw_correction == d3_lib_options)) then
147 write(message(1),'(a)') 'The stress tensor is currently only implemented with DFT-D3 vdW correction'
148 call messages_fatal(1, namespace=namespace)
149 end if
150
151 if (hm%pcm%run_pcm) then
152 call messages_not_implemented('Stress tensor with PCM', namespace=namespace)
153 end if
154
155 if (allocated(hm%v_static)) then
156 call messages_not_implemented('Stress tensor with static electric fields', namespace=namespace)
157 end if
158
159 if (ks%v_ks_photons%active()) then
160 call messages_not_implemented('Stress tensor with photon modes', namespace=namespace)
161 end if
162
163 ! Projector matrices are absent both for relativistic KB projectors and when there is no
164 ! nonlocal potential at all (local-only pseudopotentials, user-defined or all-electron species)
165 if (.not. hm%vnl%apply_projector_matrices) then
166 if (hm%vnl%has_non_local_potential) then
167 call messages_not_implemented('Stress tensor with relativistic Kleinman-Bylander pseudopotential', namespace=namespace)
168 else
169 call messages_not_implemented('Stress tensor without nonlocal projectors (local-only pseudopotentials, '// &
170 'user-defined or all-electron species)', namespace=namespace)
171 end if
172 end if
173
174 if (hm%ep%reltype == scalar_relativistic_zora .or. hm%ep%reltype == fully_relativistic_zora) then
175 call messages_not_implemented('Stress tensor with ZORA', namespace=namespace)
176 end if
177
178 ! Checks for the xc part of KS-DFT and GKS-DFT
179 if (ks%theory_level == kohn_sham_dft .or. ks%theory_level == generalized_kohn_sham_dft) then
180 if (.not. xc_is_energy_functional(hm%xc)) then
181 call messages_not_implemented("Stress tensor with xc functionals that are not energy functionals")
182 end if
183
184 if ( .not. in_family(hm%xc%family, [xc_family_lda, xc_family_gga, xc_family_mgga])) then
185 write(message(1),'(a)') 'The stress tensor computation is currently only possible at the Kohn-Sham DFT level'
186 write(message(2),'(a)') 'with LDA, GGA and mGGA functionals or for independent particles.'
187 call messages_fatal(2, namespace=namespace)
188 end if
189
190 if (in_family(hm%xc%family, [xc_family_gga, xc_family_mgga]) .and. st%d%ispin == spinors) then
191 call messages_not_implemented("Stress tensor for GGAs and mGGAs with spinors", namespace=namespace)
192 end if
193
194 if (family_is_mgga_with_exc(hm%xc)) then
195 ! The diagonal tau term is taken from hm%energy%intnvxc, which only contains the
196 ! mGGA operator expectation value at the generalized Kohn-Sham level.
197 if (ks%theory_level /= generalized_kohn_sham_dft) then
198 call messages_not_implemented("Stress tensor for mGGAs outside of the generalized Kohn-Sham scheme", &
199 namespace=namespace)
200 end if
201
202 ! The strain derivative of the gauge-invariant kinetic energy density has an extra
203 ! current-dependent term, which is not implemented.
204 if (hm%xc%use_gi_ked) then
205 call messages_not_implemented("Stress tensor for mGGAs with XCUseGaugeIndependentKED = yes", &
206 namespace=namespace)
207 end if
208 end if
209 end if
210
211 if (hm%magnetic_constrain%level /= constrain_none) then
212 call messages_not_implemented("Stress tensor with MagneticConstrain /= constrain_none")
213 end if
214
215 stress(:,:) = m_zero
216
217 safe_allocate(rho_total(1:gr%np_part))
218 do ip = 1, gr%np
219 rho_total(ip) = sum(st%rho(ip, 1:st%d%spin_channels))
220 end do
221
222 ! As we rely on some of the full energy components, we need to recompute it first
223 ! TODO: We should restrict the components of the energy needed to be computed
224 call energy_calc_total(namespace, ions%space, hm, gr, st, ext_partners, iunit = -1, full = .true.)
225
226 ! In order to get the electrostatic part (Hartree and local pseudopotential part),
227 ! we need to get the Hartree potential and its gradient
228 safe_allocate(vh(1:gr%np_part))
229 safe_allocate(grad_vh(1:gr%np, 1:gr%der%dim))
230 if (ks%theory_level /= independent_particles) then
231 call lalg_copy(gr%np, hm%ks_pot%vhartree, vh)
232 else
233 if (hm%d%spin_channels > 1) then
234 safe_allocate(rho(1:gr%np_part))
235 call lalg_copy(gr%np, st%rho(:,1), rho)
236 call lalg_axpy(gr%np, m_one, st%rho(:,2), rho)
237 else
238 rho => st%rho(:,1)
239 end if
240 ! In the case of independent particles, we use the electron density without NLCC
241 call dpoisson_solve(hm%psolver, ions%namespace, vh, rho, all_nodes = .true.)
242 if (hm%d%spin_channels > 1) then
243 safe_deallocate_p(rho)
244 else
245 nullify(rho)
246 end if
247 end if
248 ehartree = hm%energy%hartree
249 ! We also compute the gradient here
250 call dderivatives_grad(gr%der, vh, grad_vh)
251
252 ! We now compute the various contributions to the stress tensor
253
254 ! Stress from kinetic energy of electrons
255 call stress_from_kinetic(gr, ions%space, hm, st, gr%symm, ions%latt%rcell_volume, stress_kin)
256 stress = stress + stress_kin
257
258 if (ks%theory_level == independent_particles) then
259 stress_hartree = m_zero
260 stress_xc = m_zero
261 stress_xc_nlcc = m_zero
262 else
263 call stress_from_hartree(gr, ions%space, ions%latt%rcell_volume, grad_vh, ehartree, stress_hartree)
264 stress = stress + stress_hartree
265
266 call stress_from_xc(gr, hm, st, ions%space, ions%latt%rcell_volume, stress_xc)
267
268 ! Nonlinear core correction contribution
269 if (allocated(st%rho_core)) then
270 call stress_from_xc_nlcc(ions%latt%rcell_volume, gr, st, ions, hm%ks_pot%vxc, stress_xc_nlcc)
271 else
272 stress_xc_nlcc = m_zero
273 end if
274 ! Adds the beyond LDA contribution to the stress tensor
275 stress_xc = stress_xc + ks%stress_xc / ions%latt%rcell_volume
276 stress = stress + stress_xc + stress_xc_nlcc
277 end if
278
279 call stress_from_pseudo_local(gr, hm, ions, rho_total, grad_vh, stress_ps_local)
280 stress_ps = stress_ps_local
281 stress = stress + stress_ps_local
282
283 safe_deallocate_a(vh)
284 safe_deallocate_a(grad_vh)
285
286 call stress_from_pseudo_nonloc(gr, st, hm, ions, stress_ps_nl, stress_ps_aug)
287 stress_ps = stress_ps + stress_ps_nl + stress_ps_aug
288 stress = stress + stress_ps_nl + stress_ps_aug
289
290 call stress_from_hubbard(namespace, gr, st, hm, ions%space, ions%latt%rcell_volume, stress_hubbard)
291 stress = stress + stress_hubbard
292
293 call ion_interaction_stress(ions%ion_interaction, ions%space, ions%latt, ions%atom, ions%natoms, ions%pos, stress_ii)
294 stress = stress + stress_ii
295 ! Stress from kinetic energy of ion
296 ! Stress from ion-field interaction
297
298 ! Sign changed to fit conventional definition
299 stress = -stress
300 st%stress_tensors%kinetic = -stress_kin
301 st%stress_tensors%Hartree = -stress_hartree
302 st%stress_tensors%xc = -stress_xc
303 st%stress_tensors%xc_nlcc = -stress_xc_nlcc
304 st%stress_tensors%ps_local = -stress_ps_local
305 st%stress_tensors%ps_nl = -stress_ps_nl
306 st%stress_tensors%ps_aug = -stress_ps_aug
307 st%stress_tensors%hubbard = -stress_hubbard
308 st%stress_tensors%ion_ion = -stress_ii
309
310 ! Stress contribution from vdW D3
311 if (ks%vdw%vdw_correction /= option__vdwcorrection__none) then
312 st%stress_tensors%vdw = hm%ep%vdw_stress
313 else
314 st%stress_tensors%vdw = m_zero
315 end if
316 stress = stress + st%stress_tensors%vdw
317
318 ! Symmetrize the stress tensor if we use k-point symmetries
319 if (hm%kpoints%use_symmetries) then
320 call dsymmetrize_tensor_cart(gr%symm, stress, use_non_symmorphic=.true.)
321 end if
322 ! We guarantee that the matrix is truly symmetric. There could be small numerical asymmetries after symmetrization
323 call symmetrize_matrix(ions%space%periodic_dim, stress)
324
325 st%stress_tensors%total = stress
326
327 ! Some sumrule for validation
328 ! Sumrule is -3P_{kin}\Omega = 2 E_{kin}
329 st%stress_tensors%kinetic_sumrule = m_zero
330 ! Sumrule is -3P_{Hartree}\Omega = E_{Hartree}
331 st%stress_tensors%Hartree_sumrule = m_zero
332 if(ions%space%periodic_dim == 3) then
333 st%stress_tensors%kinetic_sumrule = (stress_kin(1,1) + stress_kin(2,2) + stress_kin(3,3))*ions%latt%rcell_volume
334 st%stress_tensors%kinetic_sumrule = st%stress_tensors%kinetic_sumrule - m_two * hm%energy%kinetic
335
336 st%stress_tensors%hartree_sumrule = (stress_hartree(1,1) + stress_hartree(2,2) + stress_hartree(3,3))*ions%latt%rcell_volume
337 st%stress_tensors%hartree_sumrule = st%stress_tensors%hartree_sumrule - hm%energy%hartree
338 end if
339
340 safe_deallocate_a(rho_total)
341
342 pop_sub(stress_calculate)
343 call profiling_out("STRESS_CALCULATE")
344 end subroutine stress_calculate
345
346 ! -------------------------------------------------------
361 subroutine stress_from_kinetic(gr, space, hm, st, symm, rcell_volume, stress_kin)
362 type(grid_t), intent(in) :: gr
363 class(space_t), intent(in) :: space
364 type(hamiltonian_elec_t), intent(in) :: hm
365 type(states_elec_t), intent(in) :: st
366 type(symmetries_t), intent(in) :: symm
367 real(real64), intent(in) :: rcell_volume
368 real(real64), intent(out) :: stress_kin(3, 3)
369
370 integer :: ik, ist, idir, jdir, ib, minst, maxst
371 complex(real64), allocatable :: stress_l_block(:)
372 type(wfs_elec_t) :: psib, gpsib(space%dim)
373
374 call profiling_in("STRESS_FROM_KINETIC")
375 push_sub(stress_from_kinetic)
376
377 stress_kin(:,:) = m_zero
378
379 safe_allocate(stress_l_block(1:st%block_size))
380
381 do ik = st%d%kpt%start, st%d%kpt%end
382 if (st%kweights(ik) <= m_epsilon) cycle
383
384 do ib = st%group%block_start, st%group%block_end
385 minst = states_elec_block_min(st, ib)
386 maxst = states_elec_block_max(st, ib)
387
388 call hm%phase%copy_and_set_phase(gr, st%d%kpt, st%group%psib(ib, ik), psib)
389
390 ! calculate the gradient
391 call zderivatives_batch_grad(gr%der, psib, gpsib, set_bc=.false.)
392
393 ! Accumulate the result
394 do idir = 1, space%periodic_dim
395 do jdir = idir, space%periodic_dim
396 call zmesh_batch_dotp_vector(gr, gpsib(idir), gpsib(jdir), stress_l_block)
397
398 do ist = minst, maxst
399 stress_kin(idir,jdir) = stress_kin(idir,jdir) &
400 + st%kweights(ik) * st%occ(ist, ik) &
401 * real(stress_l_block(ist - minst + 1), real64)
402 end do
403 end do
404 end do
405
406 do idir = 1, space%dim
407 call gpsib(idir)%end()
408 end do
409 call psib%end()
410
411 end do
412 end do
413
414 if (st%parallel_in_states .or. st%d%kpt%parallel) then
415 call comm_allreduce(st%st_kpt_mpi_grp, stress_kin)
416 end if
417
418
419 ! Symmetrize the kinetic stress tensor
420 call upper_triangular_to_hermitian(space%periodic_dim, stress_kin)
421
422 ! Symmetrize the stress tensor if we use k-point symmetries
423 if (hm%kpoints%use_symmetries) then
424 call dsymmetrize_tensor_cart(symm, stress_kin, use_non_symmorphic=.true.)
425 end if
426
427 stress_kin = stress_kin / rcell_volume
428
429 call profiling_out("STRESS_FROM_KINETIC")
430 pop_sub(stress_from_kinetic)
431 end subroutine stress_from_kinetic
432
433 ! -------------------------------------------------------
450 subroutine stress_from_hartree(gr, space, volume, grad_vh, ehartree, stress_Hartree)
451 type(grid_t), intent(in) :: gr
452 class(space_t), intent(in) :: space
453 real(real64), intent(in) :: volume
454 real(real64), contiguous, intent(in) :: grad_vh(:,:)
455 real(real64), intent(in) :: ehartree
456 real(real64), intent(out) :: stress_Hartree(3, 3)
457
458 integer :: idir, jdir
459
460 call profiling_in("STRESS_FROM_HARTREE")
461 push_sub(stress_from_hartree)
462
463 stress_hartree(:,:) = m_zero
464
465 do idir = 1, space%periodic_dim
466 do jdir = idir, space%periodic_dim
467 stress_hartree(idir, jdir) = -dmf_dotp(gr, grad_vh(:,idir), grad_vh(:, jdir))/m_four/m_pi
468 end do
469 stress_hartree(idir, idir) = stress_hartree(idir, idir) + ehartree
470 end do
471
472 call upper_triangular_to_hermitian(space%periodic_dim, stress_hartree)
473
474 stress_hartree = stress_hartree/volume
475
476 call profiling_out("STRESS_FROM_HARTREE")
477 pop_sub(stress_from_hartree)
478 end subroutine stress_from_hartree
479
480
481 ! -------------------------------------------------------
506 !
507 ! Note: We assume hm%energy%echange, correlation, and intnvxc
508 ! have already been calculated somewhere else.
509 subroutine stress_from_xc(gr, hm, st, space, rcell_volume, stress_xc)
510 type(grid_t), intent(in) :: gr
511 type(hamiltonian_elec_t), intent(in) :: hm
512 type(states_elec_t), intent(in) :: st
513 class(space_t), intent(in) :: space
514 real(real64), intent(in) :: rcell_volume
515 real(real64), intent(out) :: stress_xc(3, 3)
516
517 integer :: ik, ist, idir, jdir, ib, minst, maxst, ispin
518 real(real64) :: stress_tau(3,3)
519 complex(real64), allocatable :: stress_l_block(:)
520 type(wfs_elec_t) :: psib, gpsib(space%dim), vtau_gpsib(space%dim)
521
522 call profiling_in("STRESS_FROM_XC")
523 push_sub(stress_from_xc)
524
525 ! LDA part
526 stress_xc = m_zero
527 do idir = 1, space%periodic_dim
528 stress_xc(idir, idir) = - hm%energy%exchange - hm%energy%correlation + hm%energy%intnvxc
529 end do
530
531 ! tau-mGGA part.
532 ! Only the term involving the gradients of the orbitals is computed here. The diagonal term
533 ! \f$ \delta_{ij}\int d^3r v_\tau \tau \f$ is not added: it is already contained in
534 ! energy%intnvxc above, where v_ks_calc adds the expectation value of the mGGA operator
535 ! (TERM_MGGA) at the generalized Kohn-Sham level.
536 if (family_is_mgga_with_exc(hm%xc)) then
537 stress_tau = m_zero
538
539 safe_allocate(stress_l_block(1:st%block_size))
540
541 ! TODO: This mostly duplicates the kinetic term. Stress_from_kinetic could return the tau part,
542 ! if performances are critical
543 do ik = st%d%kpt%start, st%d%kpt%end
544 if (st%kweights(ik) <= m_epsilon) cycle
545
546 ispin = st%d%get_spin_index(ik)
547
548 do ib = st%group%block_start, st%group%block_end
549 minst = states_elec_block_min(st, ib)
550 maxst = states_elec_block_max(st, ib)
551
552 call hm%phase%copy_and_set_phase(gr, st%d%kpt, st%group%psib(ib, ik), psib)
553
554 ! calculate the gradient
555 call zderivatives_batch_grad(gr%der, psib, gpsib, set_bc=.false.)
556
557 ! vtau \nabla \psi
558 do idir = 1, space%periodic_dim
559 call psib%copy_to(vtau_gpsib(idir))
560 call batch_set_zero(vtau_gpsib(idir))
561 call hm%ks_pot%zapply_vtau_psi(gr, hm%d, ispin, gpsib(idir), vtau_gpsib(idir))
562 end do
563
564 ! Accumulate the result
565 do idir = 1, space%periodic_dim
566 do jdir = idir, space%periodic_dim
567 call zmesh_batch_dotp_vector(gr, vtau_gpsib(idir), gpsib(jdir), stress_l_block)
568
569 do ist = minst, maxst
570 stress_tau(idir, jdir) = stress_tau(idir, jdir) &
571 + m_two * st%kweights(ik) * st%occ(ist, ik) * real(stress_l_block(ist - minst + 1), real64)
572 end do
573 end do
574 end do
575
576 do idir = 1, space%dim
577 call gpsib(idir)%end()
578 end do
579 do idir = 1, space%periodic_dim
580 call vtau_gpsib(idir)%end()
581 end do
582 call psib%end()
583
584 end do
585 end do
586
587 safe_deallocate_a(stress_l_block)
588
589 if (st%parallel_in_states .or. st%d%kpt%parallel) then
590 call comm_allreduce(st%st_kpt_mpi_grp, stress_tau)
591 end if
592
593 ! Symmetrize the tau stress tensor
594 call upper_triangular_to_hermitian(space%periodic_dim, stress_tau)
595
596 ! Symmetrize the stress tensor if we use k-point symmetries
597 if (hm%kpoints%use_symmetries) then
598 call dsymmetrize_tensor_cart(gr%symm, stress_tau, use_non_symmorphic=.true.)
599 end if
600
601 stress_xc(:,:) = stress_xc(:,:) + stress_tau(:,:)
602
603 end if
604
605 stress_xc(:,:) = stress_xc(:,:) / rcell_volume
606
607 call profiling_out("STRESS_FROM_XC")
608 pop_sub(stress_from_xc)
609 end subroutine stress_from_xc
610
611
612 ! -------------------------------------------------------
629 subroutine stress_from_xc_nlcc(rcell_volume, gr, st, ions, vxc, stress_xc_nlcc)
630 real(real64), intent(in) :: rcell_volume
631 type(grid_t), intent(in) :: gr
632 type(states_elec_t), intent(in) :: st
633 type(ions_t), intent(in) :: ions
634 real(real64), contiguous, intent(in) :: vxc(:,:)
635 real(real64), intent(out) :: stress_xc_nlcc(3, 3)
636
637 integer :: idir, jdir, iat
638 real(real64), allocatable :: vxc_tot(:), nlcc_grad_x(:,:,:)
639
640 call profiling_in("STRESS_FROM_XC_NLCC")
641 push_sub(stress_from_xc_nlcc)
642
643 assert(allocated(st%rho_core))
644
645 stress_xc_nlcc = m_zero
646
647 safe_allocate(vxc_tot(1:gr%np))
648 safe_allocate(nlcc_grad_x(1:gr%np, 1:gr%der%dim, 1:gr%der%dim))
649
650 ! Spin average of the xc potential, as the core density is split equally among spin channels
651 call lalg_copy(gr%np, vxc(:, 1), vxc_tot)
652 if (st%d%spin_channels > 1) then
653 call lalg_axpy(gr%np, m_one, vxc(:, 2), vxc_tot)
654 call lalg_scal(gr%np, m_half, vxc_tot)
655 end if
656
657 do iat = ions%atoms_dist%start, ions%atoms_dist%end
658 call species_get_nlcc_grad(ions%atom(iat)%species, ions%space, ions%latt, &
659 ions%pos(:,iat), gr, nlcc_grad_x)
660
661 do idir = 1, ions%space%periodic_dim
662 do jdir = idir, ions%space%periodic_dim
663 stress_xc_nlcc(idir, jdir) = stress_xc_nlcc(idir, jdir) &
664 - dmf_dotp(gr, nlcc_grad_x(:, idir, jdir), vxc_tot)
665 end do
666 end do
667 end do
668 safe_deallocate_a(nlcc_grad_x)
669 safe_deallocate_a(vxc_tot)
670
671 if (ions%atoms_dist%parallel) then
672 call comm_allreduce(ions%atoms_dist%mpi_grp, stress_xc_nlcc)
673 end if
674
675 call upper_triangular_to_hermitian(ions%space%periodic_dim, stress_xc_nlcc)
676
677 stress_xc_nlcc(:,:) = stress_xc_nlcc(:,:) / rcell_volume
678
679 call profiling_out("STRESS_FROM_XC_NLCC")
680 pop_sub(stress_from_xc_nlcc)
681 end subroutine stress_from_xc_nlcc
682
683 ! -------------------------------------------------------
713 subroutine stress_from_pseudo_nonloc(gr, st, hm, ions, stress_ps_nl, stress_ps_aug)
714 type(grid_t), target, intent(in) :: gr
715 type(states_elec_t), intent(inout) :: st
716 type(hamiltonian_elec_t), target, intent(in) :: hm
717 type(ions_t), intent(in) :: ions
718 real(real64), intent(out) :: stress_ps_nl(3, 3)
719 real(real64), intent(out) :: stress_ps_aug(3, 3)
720
721 integer :: ik, ist, idir, jdir
722 integer :: ib, minst, maxst
723 type(wfs_elec_t) :: psib, rvnl_psib(3), gpsib(3)
724 complex(real64), allocatable :: stress_tmp(:)
725 logical :: uspp
726 integer :: ncomp, icomp, imat, nprojs, ispin_pot
727 real(real64) :: evps
728 real(real64), allocatable :: onsite_density_matrix(:, :, :, :), onsite_density_matrix_eps(:, :, :, :)
729 real(real64), allocatable :: us_weight(:), us_weight_eps(:)
730 real(real64), allocatable :: veff(:), grad_veff(:, :, :)
731 type(projector_matrix_t), pointer :: pmat
732
733 call profiling_in("STRESS_FROM_PSEUDO_NL")
735
736 assert(st%wfs_type == type_cmplx)
737
738 safe_allocate(stress_tmp(1:st%block_size))
739
740 stress_ps_nl = m_zero
741 stress_ps_aug = m_zero
742
743 ! For ultrasoft, accumulate the projector density matrices rho_ij = sum_nk w_k f_nk
744 ! <psi|beta_i><beta_j|psi> (onsite_density_matrix, drives the augmentation stress) and the eigenvalue-
745 ! weighted analog (onsite_density_matrix_eps, gives the -eps_n q_ij part of the diagonal energy term).
746 uspp = hm%ep%requires_generalized_overlap
747 ncomp = st%d%nspin
748 if (uspp) then
749 ! Spinors are fenced in stress_calculate
750 assert(st%d%ispin /= spinors)
751 safe_allocate(onsite_density_matrix(1:hm%vnl%max_nprojs, 1:hm%vnl%max_nprojs, 1:hm%vnl%nprojector_matrices, 1:ncomp))
752 safe_allocate(onsite_density_matrix_eps(1:hm%vnl%max_nprojs, 1:hm%vnl%max_nprojs, 1:hm%vnl%nprojector_matrices, 1:ncomp))
753 onsite_density_matrix = m_zero
754 onsite_density_matrix_eps = m_zero
755 end if
756
757 do ik = st%d%kpt%start, st%d%kpt%end
758
759 if (st%kweights(ik) <= m_epsilon) cycle
760
761 do ib = st%group%block_start, st%group%block_end
762 minst = states_elec_block_min(st, ib)
763 maxst = states_elec_block_max(st, ib)
764
765 call hm%phase%copy_and_set_phase(gr, st%d%kpt, st%group%psib(ib, ik), psib)
766
767 ! calculate the gradient
768 call zderivatives_batch_grad(gr%der, psib, gpsib, set_bc=.false.)
769
770
771 ! Get rV_NL |\psi> for all atoms. For ultrasoft projectors the eigenvalues are needed
772 ! to build the per-state coupling D^scr_ij - eps_n q_ij.
773 do idir = 1, gr%der%dim
774 call psib%copy_to(rvnl_psib(idir))
775 call batch_set_zero(rvnl_psib(idir))
776 end do
777 call hm%vnl%zr_vn_local(gr, st%d, gr%der%boundaries%spiral, psib, rvnl_psib, st%eigenval(:, ik))
778
779 do idir = 1, ions%space%periodic_dim
780 do jdir = idir, ions%space%periodic_dim
781 call zmesh_batch_dotp_vector(gr, gpsib(idir), rvnl_psib(jdir), stress_tmp)
782
783 do ist = minst, maxst
784 stress_ps_nl(idir, jdir) = stress_ps_nl(idir, jdir) &
785 + m_two * st%kweights(ik) * st%occ(ist, ik) * real(stress_tmp(ist-minst+1), real64)
786 end do
787
788 end do
789 end do
790
791 ! Ultrasoft: accumulate rho_ij from this (already phase-set) batch, with the occupation
792 ! and the occupation*eigenvalue weights.
793 if (uspp) then
794 safe_allocate(us_weight(1:psib%nst))
795 safe_allocate(us_weight_eps(1:psib%nst))
796 do ist = 1, psib%nst
797 us_weight(ist) = st%kweights(ik)*st%occ(psib%ist(ist), ik)
798 us_weight_eps(ist) = us_weight(ist)*st%eigenval(psib%ist(ist), ik)
799 end do
800 call znonlocal_pseudopotential_density_matrix(hm%vnl, gr, st%d, .false., psib, us_weight, onsite_density_matrix, &
801 weight2 = us_weight_eps, beta_density2 = onsite_density_matrix_eps)
802 safe_deallocate_a(us_weight)
803 safe_deallocate_a(us_weight_eps)
804 end if
805
806 do idir = 1, gr%der%dim
807 call rvnl_psib(idir)%end()
808 call gpsib(idir)%end()
809 end do
810 call psib%end()
811 end do
812 end do
813
814 safe_deallocate_a(stress_tmp)
815
816 if (st%parallel_in_states .or. st%d%kpt%parallel) then
817 call comm_allreduce(st%st_kpt_mpi_grp, stress_ps_nl)
818 if (uspp) then
819 call comm_allreduce(st%st_kpt_mpi_grp, onsite_density_matrix)
820 call comm_allreduce(st%st_kpt_mpi_grp, onsite_density_matrix_eps)
821 end if
822 end if
823
824 ! Symmetrize the kinetic stress tensor
825 call upper_triangular_to_hermitian(ions%space%periodic_dim, stress_ps_nl)
826
827 ! Symmetrize the stress tensor if we use k-point symmetries
828 if (hm%kpoints%use_symmetries) then
829 call dsymmetrize_tensor_cart(gr%symm, stress_ps_nl, use_non_symmorphic=.true.)
830 end if
831
832 ! Add the nonlocal energy. For norm-conserving pseudopotentials this is
833 ! E_NL = sum_nk w f <psi|V_NL|psi>; for ultrasoft the coupling in the energy term is
834 ! also D^scr_ij - eps_n q_ij: extern_non_local already carries
835 ! the screened D^scr part, so only the -eps q part is added.
836 evps = hm%energy%extern_non_local
837 if (uspp) then
838 do imat = 1, hm%vnl%nprojector_matrices
839 pmat => hm%vnl%projector_matrices(imat)
840 if (.not. allocated(pmat%qmix)) cycle
841 nprojs = pmat%nprojs
842 do icomp = 1, ncomp
843 evps = evps - sum(pmat%qmix(1:nprojs, 1:nprojs)*onsite_density_matrix_eps(1:nprojs, 1:nprojs, imat, icomp))
844 end do
845 end do
846 end if
847 do idir = 1, ions%space%periodic_dim
848 stress_ps_nl(idir, idir) = stress_ps_nl(idir, idir) + evps
849 end do
850
851 stress_ps_nl = stress_ps_nl/ions%latt%rcell_volume
852
853 ! Ultrasoft augmentation stress: the augmentation density is rigid under strain (it only
854 ! translates with the atoms), unlike the smooth density which scales homogeneously; the
855 ! correction couples grad V_eff, position-weighted, to Q_ij.
856 if (uspp) then
857 safe_allocate(veff(1:gr%np_part))
858 safe_allocate(grad_veff(1:gr%np, 1:gr%der%dim, 1:ncomp))
859 do ispin_pot = 1, ncomp
860 veff(1:gr%np) = hm%hm_base%potential(1:gr%np, ispin_pot)
861 call dderivatives_grad(gr%der, veff, grad_veff(:, :, ispin_pot))
862 end do
863 call nonlocal_pseudopotential_stress_uspp(hm%vnl, gr, hm%ep, ions%space%periodic_dim, grad_veff, &
864 onsite_density_matrix, stress_ps_aug)
865 safe_deallocate_a(veff)
866 safe_deallocate_a(grad_veff)
867
868 ! (d_a V_eff)(r-R)_b is symmetric in (a,b) only up to grid discretization error
869 stress_ps_aug = m_half*(stress_ps_aug + transpose(stress_ps_aug))
870
871 ! onsite_density_matrix is accumulated on the irreducible k-point wedge
872 if (hm%kpoints%use_symmetries) then
873 call dsymmetrize_tensor_cart(gr%symm, stress_ps_aug, use_non_symmorphic=.true.)
874 end if
875
876 stress_ps_aug = stress_ps_aug/ions%latt%rcell_volume
877
878 safe_deallocate_a(onsite_density_matrix)
879 safe_deallocate_a(onsite_density_matrix_eps)
880 end if
881
882 call profiling_out("STRESS_FROM_PSEUDO_NL")
884
885 end subroutine stress_from_pseudo_nonloc
886
887
888 ! -------------------------------------------------------
908 subroutine stress_from_pseudo_local(gr, hm, ions, rho_total, grad_vh, stress_ps_local)
909 type(grid_t), target, intent(in) :: gr
910 type(hamiltonian_elec_t), intent(in) :: hm
911 type(ions_t), intent(in) :: ions
912 real(real64), contiguous, intent(inout) :: rho_total(:)
913 real(real64), contiguous, intent(in) :: grad_vh(:,:)
914 real(real64), intent(out) :: stress_ps_local(3, 3)
915
916
917 real(real64) :: stress_SR(3, 3), stress_LR(3, 3)
918 real(real64) :: energy_ps_SR, charge, zi
919 real(real64), allocatable :: vloc(:), rvloc(:,:), rho_local_lr(:), rho_lr(:)
920 real(real64), allocatable :: grad_rho(:,:), rho_lr_x(:,:), vlr(:), grad_vlr(:,:)
921 integer :: idir, jdir, iatom
922 type(ps_t), pointer :: spec_ps
923
924 call profiling_in("STRESS_FROM_PSEUDO_LOC")
926
927 ! calculate stress from short-range local pseudopotentials
928 stress_sr = m_zero
929
930 ! The per-atom placements are distributed with the shared epot-owned plane
931 ! distribution; its start/end cover all atoms when it is not parallel, and no
932 ! reduction must follow in that case.
933 safe_allocate(vloc(1:gr%np))
934 vloc = m_zero
935 safe_allocate(rvloc(1:gr%np, 1:gr%der%dim))
936 rvloc = m_zero
937 do iatom = hm%ep%atoms_dist_plane%start, hm%ep%atoms_dist_plane%end
938 call epot_local_pseudopotential_sr(gr, ions, iatom, vloc, rvloc, hm%ep%dgrid)
939 end do
940 if (hm%ep%atoms_dist_plane%parallel) then
941 call comm_allreduce(hm%ep%atoms_dist_plane%mpi_grp, rvloc)
942 end if
943 safe_deallocate_a(vloc)
944
945 safe_allocate(grad_rho(1:gr%np,1:gr%der%dim))
946 call dderivatives_grad(gr%der, rho_total, grad_rho)
947
948 if (hm%ep%requires_generalized_overlap) then
949 ! For ultrasoft, extern_local = sum_nk w f <psi|vpsl|psi> is the smooth-density
950 ! expectation and misses int n_aug vpsl; use the full density instead.
951 energy_ps_sr = dmf_dotp(gr, rho_total, hm%ep%vpsl)
952 else
953 energy_ps_sr = hm%energy%extern_local
954 end if
955 do idir = 1, ions%space%periodic_dim
956 do jdir = idir, ions%space%periodic_dim
957 stress_sr(idir, jdir) = stress_sr(idir, jdir) &
958 +dmf_dotp(gr, rvloc(:, jdir), grad_rho(:, idir))
959 end do
960 stress_sr(idir,idir) = stress_sr(idir,idir) + energy_ps_sr
961 end do
962
963 call upper_triangular_to_hermitian(ions%space%periodic_dim, stress_sr)
964
965 stress_sr = stress_sr/ions%latt%rcell_volume
966
967 safe_deallocate_a(rvloc)
968 safe_deallocate_a(grad_rho)
969
970
971 ! calculate stress from long-range local pseudopotentials
972 stress_lr = m_zero
973
974 ! We treat the long-range part of the local potential as the Hartree term
975 ! We first sum the long range densities from atoms
976 safe_allocate(rho_lr(1:gr%np_part))
977 safe_allocate(rho_lr_x(1:gr%np, 1:gr%der%dim))
978 rho_lr = m_zero
979 rho_lr_x = m_zero
980 safe_allocate(rho_local_lr(1:gr%np))
981 do iatom = ions%atoms_dist%start, ions%atoms_dist%end
982 assert(ions%atom(iatom)%species%is_ps())
983 call species_get_long_range_density(ions%atom(iatom)%species, ions%namespace, ions%space, ions%latt, &
984 ions%pos(:, iatom), gr, rho_local_lr, nlr_x=rho_lr_x)
985
986 call lalg_axpy(gr%np, m_one, rho_local_lr, rho_lr)
987 end do
988 safe_deallocate_a(rho_local_lr)
989
990 if (ions%atoms_dist%parallel) then
991 call comm_allreduce(ions%atoms_dist%mpi_grp, rho_lr)
992 call comm_allreduce(ions%atoms_dist%mpi_grp, rho_lr_x)
993 end if
994
995 do idir = 1, ions%space%periodic_dim
996 do jdir = idir, ions%space%periodic_dim
997 stress_lr(idir, jdir) = stress_lr(idir, jdir) + dmf_dotp(gr, rho_lr_x(:,jdir), grad_vh(:, idir))
998 end do
999 end do
1000 safe_deallocate_a(rho_lr_x)
1001
1002 safe_allocate(vlr(1:gr%np_part))
1003 call dpoisson_solve(hm%psolver, ions%namespace, vlr, rho_lr, all_nodes = .true.)
1004 safe_deallocate_a(rho_lr)
1005
1006 safe_allocate(grad_vlr(1:gr%np, 1:gr%der%dim))
1007 call dderivatives_grad(gr%der, vlr, grad_vlr)
1008 safe_deallocate_a(vlr)
1009
1010 do idir = 1, ions%space%periodic_dim
1011 do jdir = idir, ions%space%periodic_dim
1012 stress_lr(idir, jdir) = stress_lr(idir, jdir) - dmf_dotp(gr, grad_vh(:,idir), grad_vlr(:, jdir))/m_two/m_pi
1013 end do
1014 end do
1015
1016 call upper_triangular_to_hermitian(ions%space%periodic_dim, stress_lr)
1017
1018 safe_deallocate_a(grad_vlr)
1019
1020 ! Contribution from G=0 component of the long-range part
1021 !
1022 if (ions%space%periodic_dim == 3) then
1023 charge = m_zero
1024 do iatom = 1, ions%natoms
1025 charge = charge + ions%atom(iatom)%species%get_zval()
1026 end do
1027
1028 do iatom = 1, ions%natoms
1029 select type(spec => ions%atom(iatom)%species)
1030 type is(pseudopotential_t)
1031 zi = spec%get_zval()
1032 spec_ps => spec%ps
1033
1034 do idir = 1, ions%space%periodic_dim
1035 stress_lr(idir, idir) = stress_lr(idir, idir) &
1036 + m_two*m_pi*spec_ps%sigma_erf**2*charge*zi /ions%latt%rcell_volume
1037 end do
1038 end select
1039 end do
1040 end if
1041
1042 stress_lr = stress_lr/ions%latt%rcell_volume
1043
1044 stress_ps_local = stress_sr + stress_lr
1045
1046 call profiling_out("STRESS_FROM_PSEUDO_LOC")
1048
1049 end subroutine stress_from_pseudo_local
1050
1051 ! -------------------------------------------------------
1052 subroutine epot_local_pseudopotential_sr(mesh, ions, iatom, vpsl, rvpsl, dgrid)
1053 class(mesh_t), intent(in) :: mesh
1054 type(ions_t), intent(in) :: ions
1055 integer, intent(in) :: iatom
1056 real(real64), contiguous, intent(inout) :: vpsl(:)
1057 real(real64), contiguous, intent(inout) :: rvpsl(:,:)
1058 type(double_grid_t), intent(in) :: dgrid
1059
1060 integer :: ip, idir, pd
1061 real(real64) :: radius
1062 real(real64), allocatable :: vl_sm(:, :)
1063 type(submesh_t) :: sphere
1064 type(ps_t), pointer :: ps
1065
1067
1068 if (.not. ions%atom(iatom)%species%is_ps()) then
1070 return
1071 endif
1072
1073 call profiling_in("EPOT_LOCAL_PS_SR")
1074
1075 select type(spec=>ions%atom(iatom)%species)
1076 type is(pseudopotential_t)
1077
1078 ps => spec%ps
1079
1080 radius = ps%vl%x_threshold*1.05_real64
1081
1082 call submesh_init(sphere, ions%space, mesh, ions%latt, ions%pos(:, iatom), &
1083 radius + double_grid_margin(dgrid, mesh))
1084
1085 ! Column 1 is V(r), column 1+idir is (x-R)_idir V(r), both transformed on the
1086 ! fine grid so that rvpsl is consistent with the double-grid energy.
1087 pd = ions%space%periodic_dim
1088 safe_allocate(vl_sm(1:sphere%np, 1:pd + 1))
1089 call double_grid_transform(dgrid, sphere, pd + 1, [(ps%vl, idir = 0, pd)], [(-1, idir = 0, pd)], &
1090 [(0, idir = 0, pd)], [(radius, idir = 0, pd)], vl_sm, cart_dir = [(idir, idir = 0, pd)])
1091
1092 ! Cannot be written (correctly) as a vector expression since for periodic systems,
1093 ! there can be values ip, jp such that sphere%map(ip) == sphere%map(jp).
1094 do ip = 1, sphere%np
1095 vpsl(sphere%map(ip)) = vpsl(sphere%map(ip)) + vl_sm(ip, 1)
1096 rvpsl(sphere%map(ip), 1:pd) = rvpsl(sphere%map(ip), 1:pd) + vl_sm(ip, 2:pd + 1)
1097 end do
1098
1099 safe_deallocate_a(vl_sm)
1100 call submesh_end(sphere)
1101
1102 nullify(ps)
1103
1104 end select
1105
1106 call profiling_out("EPOT_LOCAL_PS_SR")
1108 end subroutine epot_local_pseudopotential_sr
1109
1110
1111 ! -------------------------------------------------------
1126 subroutine stress_from_hubbard(namespace, gr, st, hm, space, rcell_volume, stress_hubbard)
1127 type(namespace_t), intent(in) :: namespace
1128 type(grid_t), target, intent(in) :: gr
1129 type(states_elec_t), intent(in) :: st
1130 type(hamiltonian_elec_t), intent(in) :: hm
1131 type(space_t), intent(in) :: space
1132 real(real64), intent(in) :: rcell_volume
1133 real(real64), intent(out) :: stress_hubbard(3, 3)
1134
1135 integer :: ik, ist, idir, jdir
1136 integer :: ib, minst, maxst
1137 logical :: uspp
1138 real(real64) :: hub_diag
1139 type(wfs_elec_t) :: psib, rvu_psib(3), gpsib(3)
1140 type(wfs_elec_t) :: spsib, chib, gspsib(3), gchib(3), rvnlq_psib(3), rvnlq_chib(3)
1141 complex(real64), allocatable :: stress_tmp(:), stress_tmp2(:)
1142
1143 if (hm%lda_u%level == dft_u_none) then
1144 stress_hubbard = m_zero
1145 return
1146 end if
1147
1148 push_sub_with_profile(stress_from_hubbard)
1149
1150 assert(st%wfs_type == type_cmplx)
1151
1152 ! For ultrasoft/PAW the Hubbard occupations project through the generalized overlap S,
1153 ! n_mm' = sum_nk w f <phi_m|S|psi~><psi~|S|phi_m'>, and the strain derivative of
1154 ! <phi_m|S|psi~> (rigid phi and beta, homogeneously strained psi~) yields, exactly as for
1155 ! the forces (Timrov et al., PRB 102, 235159 (2020), Eqs. 15-18), two contributions:
1156 ! - term A: the norm-conserving structure with S psi~ in both slots,
1157 ! 2 Re < d_a(S psi~) | (r-R_J)_b phi_m > V_mm' <phi_m'|S|psi~> ;
1158 ! - term B: the beta-rigidity of S, coupling through q_ij with chi = P S psi~ (the bare
1159 ! +U ket): 2 Re sum_ij q_ij [ <d_a chi|(r-R_I)_b beta_i><beta_j|psi~>
1160 ! + <d_a psi~|(r-R_I)_b beta_i><beta_j|chi> ]
1161 ! plus the diagonal delta_ab 2 Re <chi|(S-1)|psi~>.
1162 ! The delta_ab Tr[V n] piece is hm%energy%int_dft_u.
1163 uspp = hm%ep%requires_generalized_overlap
1164
1165 safe_allocate(stress_tmp(1:st%block_size))
1166 safe_allocate(stress_tmp2(1:st%block_size))
1167
1168 stress_hubbard = m_zero
1169 hub_diag = m_zero
1170
1171 do ik = st%d%kpt%start, st%d%kpt%end
1172
1173 if (st%kweights(ik) <= m_epsilon) cycle
1174
1175 do ib = st%group%block_start, st%group%block_end
1176 minst = states_elec_block_min(st, ib)
1177 maxst = states_elec_block_max(st, ib)
1178
1179 call hm%phase%copy_and_set_phase(gr, st%d%kpt, st%group%psib(ib, ik), psib)
1180
1181 ! calculate the gradient
1182 call zderivatives_batch_grad(gr%der, psib, gpsib, set_bc=.false.)
1183
1184 ! Get rV_U |\psi> for all atoms
1185 do idir = 1, gr%der%dim
1186 call psib%copy_to(rvu_psib(idir))
1187 call batch_set_zero(rvu_psib(idir))
1188 end do
1189
1190 if (uspp) then
1191 ! S|psi~> and its gradient
1192 call psib%copy_to(spsib)
1193 call zlda_u_apply_overlap(hm%aug_overlap, gr, st%d, psib, spsib)
1194 call hm%phase%set_boundaries(gr, gr%der%boundaries, spsib)
1195 call zderivatives_batch_grad(gr%der, spsib, gspsib, set_bc=.false.)
1196
1197 ! Term A: S-augmented projections <phi_m|S|psi~> (the r-weighted orbital ket stays bare)
1198 call zlda_u_rvu(hm%lda_u, gr, space, hm%d, namespace, spsib, rvu_psib)
1199
1200 ! chi = P S psi~ (the bare +U ket) and its gradient
1201 call psib%copy_to(chib)
1202 call batch_set_zero(chib)
1203 call zlda_u_add_plus_u_ket(hm%lda_u, st%d, gr, psib, chib, hm%aug_overlap)
1204 call hm%phase%set_boundaries(gr, gr%der%boundaries, chib)
1205 call zderivatives_batch_grad(gr%der, chib, gchib, set_bc=.false.)
1206
1207 ! x (S-1)|psi~> and x (S-1)|chi> for term B
1208 do idir = 1, gr%der%dim
1209 call psib%copy_to(rvnlq_psib(idir))
1210 call batch_set_zero(rvnlq_psib(idir))
1211 call psib%copy_to(rvnlq_chib(idir))
1212 call batch_set_zero(rvnlq_chib(idir))
1213 end do
1214 call hm%vnl%zr_vn_local(gr, st%d, gr%der%boundaries%spiral, psib, rvnlq_psib, only_qmix=.true.)
1215 call hm%vnl%zr_vn_local(gr, st%d, gr%der%boundaries%spiral, chib, rvnlq_chib, only_qmix=.true.)
1216 else
1217 call zlda_u_rvu(hm%lda_u, gr, space, hm%d, namespace, psib, rvu_psib)
1218 end if
1219
1220 do idir = 1, gr%der%dim
1221 do jdir = idir, gr%der%dim
1222 if (uspp) then
1223 call zmesh_batch_dotp_vector(gr, gspsib(idir), rvu_psib(jdir), stress_tmp)
1224 else
1225 call zmesh_batch_dotp_vector(gr, gpsib(idir), rvu_psib(jdir), stress_tmp)
1226 end if
1227
1228 do ist = minst, maxst
1229 stress_hubbard(idir, jdir) = stress_hubbard(idir, jdir) &
1230 + m_two * st%kweights(ik) * st%occ(ist, ik) * real(stress_tmp(ist-minst+1), real64)
1231 end do
1232
1233 if (uspp) then
1234 ! Term B (r-weighted): <d_a chi|x_b (S-1)|psi~> + <d_a psi~|x_b (S-1)|chi>
1235 call zmesh_batch_dotp_vector(gr, gchib(idir), rvnlq_psib(jdir), stress_tmp)
1236 call zmesh_batch_dotp_vector(gr, gpsib(idir), rvnlq_chib(jdir), stress_tmp2)
1237 do ist = minst, maxst
1238 stress_hubbard(idir, jdir) = stress_hubbard(idir, jdir) &
1239 + m_two * st%kweights(ik) * st%occ(ist, ik) &
1240 * real(stress_tmp(ist-minst+1) + stress_tmp2(ist-minst+1), real64)
1241 end do
1242 end if
1243
1244 end do
1245 end do
1246
1247 ! Term B, diagonal piece: delta_ab 2 Re <chi|(S-1)|psi~>
1248 if (uspp) then
1249 call zmesh_batch_dotp_vector(gr, chib, spsib, stress_tmp)
1250 call zmesh_batch_dotp_vector(gr, chib, psib, stress_tmp2)
1251 do ist = minst, maxst
1252 hub_diag = hub_diag + m_two * st%kweights(ik) * st%occ(ist, ik) &
1253 * real(stress_tmp(ist-minst+1) - stress_tmp2(ist-minst+1), real64)
1254 end do
1255 end if
1256
1257 do idir = 1, gr%der%dim
1258 call rvu_psib(idir)%end()
1259 call gpsib(idir)%end()
1260 end do
1261 if (uspp) then
1262 do idir = 1, gr%der%dim
1263 call gspsib(idir)%end()
1264 call gchib(idir)%end()
1265 call rvnlq_psib(idir)%end()
1266 call rvnlq_chib(idir)%end()
1267 end do
1268 call spsib%end()
1269 call chib%end()
1270 end if
1271 call psib%end()
1272 end do
1273 end do
1274
1275 safe_deallocate_a(stress_tmp)
1276 safe_deallocate_a(stress_tmp2)
1277
1278 if (st%parallel_in_states .or. st%d%kpt%parallel) then
1279 call comm_allreduce(st%st_kpt_mpi_grp, stress_hubbard)
1280 if (uspp) call comm_allreduce(st%st_kpt_mpi_grp, hub_diag)
1281 end if
1282
1283 ! Symmetrize the kinetic stress tensor
1284 call upper_triangular_to_hermitian(gr%der%dim, stress_hubbard)
1285
1286 ! Symmetrize the stress tensor if we use k-point symmetries
1287 if (hm%kpoints%use_symmetries) then
1288 call dsymmetrize_tensor_cart(gr%symm, stress_hubbard)
1289 end if
1290
1291 ! Add the Hubbard energy and, for ultrasoft, the overlap diagonal correction 2 Re <chi|(S-1)|psi~>
1292 do idir = 1,3
1293 stress_hubbard(idir, idir) = stress_hubbard(idir, idir) + hm%energy%int_dft_u + hub_diag
1294 end do
1295
1296 stress_hubbard = stress_hubbard/rcell_volume
1297
1298 pop_sub_with_profile(stress_from_hubbard)
1299 end subroutine stress_from_hubbard
1300
1301
1302
1303 ! -------------------------------------------------------
1304 subroutine output_stress(iunit, space_dim, stress_tensors, mpi_grp, all_terms)
1305 integer, intent(in) :: iunit
1306 integer, intent(in) :: space_dim
1307 type(stress_t), intent(in) :: stress_tensors
1308 type(mpi_grp_t), intent(in) :: mpi_grp
1309 logical, optional, intent(in) :: all_terms
1310
1311 logical :: write_all_terms
1312 character(len=16) :: stress_unit
1313
1314 write_all_terms = optional_default(all_terms, .true.)
1315
1316 write(stress_unit, '(4a,i1)') trim(units_abbrev(units_out%energy)), '/', &
1317 trim(units_abbrev(units_out%length)), '^', space_dim
1318
1319 if (mpi_grp%is_root()) then
1320
1321 if (write_all_terms) then
1322 write(iunit, '(3a)') 'Kinetic stress tensor [', trim(stress_unit), '] ='
1323 call print_stress_tensor(iunit, space_dim, stress_tensors%kinetic)
1324 if (space_dim == 3) then
1325 write(iunit, '(a, es15.6, 3a)') 'Kinetic pressure sumrule violation: ', &
1326 units_from_atomic(units_out%energy, stress_tensors%kinetic_sumrule), &
1327 ' [', trim(units_abbrev(units_out%energy)), ']'
1328 write(iunit,*)
1329 end if
1330
1331
1332 write(iunit, '(3a)') 'Hartree stress tensor [', trim(stress_unit), '] ='
1333 call print_stress_tensor(iunit, space_dim, stress_tensors%Hartree)
1334 if (space_dim == 3) then
1335 write(iunit, '(a, es15.6, 3a)') 'Hartree pressure sumrule violation: ', &
1336 units_from_atomic(units_out%energy, stress_tensors%hartree_sumrule), &
1337 ' [', trim(units_abbrev(units_out%energy)), ']'
1338 write(iunit,*)
1339 end if
1340
1341 write(iunit, '(3a)') 'XC stress tensor [', trim(stress_unit), '] ='
1342 call print_stress_tensor(iunit, space_dim, stress_tensors%xc)
1343
1344 write(iunit, '(3a)') 'XC NLCC stress tensor [', trim(stress_unit), '] ='
1345 call print_stress_tensor(iunit, space_dim, stress_tensors%xc_nlcc)
1346
1347 write(iunit, '(3a)') 'Local pseudo. stress tensor [', trim(stress_unit), '] ='
1348 call print_stress_tensor(iunit, space_dim, stress_tensors%ps_local)
1349
1350 write(iunit, '(3a)') 'Nonlocal pseudo. stress tensor [', trim(stress_unit), '] ='
1351 call print_stress_tensor(iunit, space_dim, stress_tensors%ps_nl)
1352
1353 ! Only present for ultrasoft/PAW pseudopotentials; skipped otherwise to keep the
1354 ! norm-conserving output unchanged
1355 if (any(abs(stress_tensors%ps_aug) > m_epsilon)) then
1356 write(iunit, '(3a)') 'Augmentation stress tensor [', trim(stress_unit), '] ='
1357 call print_stress_tensor(iunit, space_dim, stress_tensors%ps_aug)
1358 end if
1359
1360 write(iunit, '(3a)') 'Ion-ion stress tensor [', trim(stress_unit), '] ='
1361 call print_stress_tensor(iunit, space_dim, stress_tensors%ion_ion)
1362
1363 write(iunit, '(3a)') 'vdW stress tensor [', trim(stress_unit), '] ='
1364 call print_stress_tensor(iunit, space_dim, stress_tensors%vdw)
1365
1366 write(iunit, '(3a)') 'Hubbard stress tensor [', trim(stress_unit), '] ='
1367 call print_stress_tensor(iunit, space_dim, stress_tensors%hubbard)
1368 end if
1369
1370 write(iunit, '(3a)') 'Total stress tensor [', trim(stress_unit), '] ='
1371 call print_stress_tensor(iunit, space_dim, stress_tensors%total)
1372
1373 end if
1374 end subroutine output_stress
1375
1377 subroutine output_pressure(iunit, space_dim, total_stress_tensor)
1378 integer, intent(in) :: iunit
1379 integer, intent(in) :: space_dim
1380 real(real64), intent(in) :: total_stress_tensor(3,3)
1381
1382 integer :: idim
1383 real(real64) :: pressure
1384 character(len=16) :: stress_unit
1385
1386 write(stress_unit, '(4a,i1)') trim(units_abbrev(units_out%energy)), '/', &
1387 trim(units_abbrev(units_out%length)), '^', space_dim
1388
1389 pressure = m_zero
1390 do idim = 1, space_dim
1391 pressure = pressure - total_stress_tensor(idim, idim) / real(space_dim, real64)
1392 end do
1393
1394 write(iunit,'(3a,es16.8)', advance="no") 'Pressure [', trim(stress_unit), '] = ', &
1395 units_from_atomic(units_out%energy/units_out%length**space_dim, pressure)
1396 if (space_dim == 3) then
1397 write(iunit,'(2x,a,f16.8)') 'Pressure [GPa] = ', units_from_atomic(unit_gpa, pressure)
1398 else
1399 write(iunit,*)
1400 end if
1401
1402 end subroutine output_pressure
1403
1404 subroutine print_stress_tensor(ounit, space_dim, tensor)
1405 integer, intent(in) :: ounit
1406 integer, intent(in) :: space_dim
1407 real(real64), intent(in) :: tensor(3,3)
1408
1409 real(real64) :: tensor_with_unit(3,3)
1410 integer :: idim, jdim
1411
1412 tensor_with_unit = units_from_atomic(units_out%energy/units_out%length**space_dim, tensor)
1413
1414 write(ounit,'(a9,2x)', advance="no")"T_{ij}"
1415 do jdim = 1, space_dim
1416 write(ounit,'(i18)', advance="no") jdim
1417 end do
1418 write(ounit,*)
1419 do idim = 1, space_dim
1420 write(ounit,'(i9,2x)', advance="no") idim
1421 do jdim = 1, space_dim
1422 write(ounit,'(es18.9)', advance="no") tensor_with_unit(idim, jdim)
1423 end do
1424 write(ounit,*)
1425 end do
1426 write(ounit,*)
1427
1428 end subroutine print_stress_tensor
1429
1430
1431end module stress_oct_m
1432
1433!! Local Variables:
1434!! mode: f90
1435!! coding: utf-8
1436!! 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:197
scales a vector by a constant
Definition: lalg_basic.F90:147
This module implements common operations on batches of mesh functions.
Definition: batch_ops.F90:95
subroutine, public batch_set_zero(this, np, async)
fill all mesh functions of the batch with zero
Definition: batch_ops.F90:265
Module implementing boundary conditions in Octopus.
Definition: boundaries.F90:101
This module implements a calculator for the density and defines related functions.
Definition: density.F90:99
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 zderivatives_batch_grad(der, ffb, opffb, ghost_update, set_bc, to_cartesian, factor)
apply the gradient to a batch of mesh functions
The Ono-Hirose double-grid technique [T. Ono and K. Hirose, Phys. Rev. Lett. 82, 5016 (1999); Phys....
real(real64) function, public double_grid_margin(this, mesh)
Submesh radius enlargement needed to contain the effective support of a transformed function: the ten...
subroutine, public double_grid_transform(this, sm, nf, spl, ll, mm, rcut, feff, cart_dir, cache)
Evaluate a batch of localized functions f_jf(x) = spl_jf(r) * Y_{ll,mm}(x) (or a purely radial spl_jf...
integer, parameter, public spinors
subroutine, public energy_calc_total(namespace, space, hm, gr, st, ext_partners, iunit, full)
This subroutine calculates the total energy of the system. Basically, it adds up the KS eigenvalues,...
integer, parameter, public scalar_relativistic_zora
Definition: epot.F90:152
integer, parameter, public fully_relativistic_zora
Definition: epot.F90:152
real(real64), parameter, public m_two
Definition: global.F90:175
real(real64), parameter, public m_zero
Definition: global.F90:173
real(real64), parameter, public m_four
Definition: global.F90:177
real(real64), parameter, public m_pi
some mathematical constants
Definition: global.F90:171
integer, parameter, public independent_particles
Theory level.
Definition: global.F90:223
integer, parameter, public generalized_kohn_sham_dft
Definition: global.F90:223
integer, parameter, public kohn_sham_dft
Definition: global.F90:223
real(real64), parameter, public m_epsilon
Definition: global.F90:189
real(real64), parameter, public m_half
Definition: global.F90:179
real(real64), parameter, public m_one
Definition: global.F90:174
This module implements the underlying real-space grid.
Definition: grid.F90:96
This module defines classes and functions for interaction partners.
Definition: io.F90:93
subroutine, public ion_interaction_stress(this, space, latt, atom, natoms, pos, stress_ii)
Computes the contribution to the stress tensor the ion-ion energy.
A module to handle KS potential, without the external potential.
subroutine, public zlda_u_apply_overlap(aug_overlap, mesh, std, psib, spsib)
Apply the ultrasoft/PAW generalized overlap S = 1 + sum_ij |beta_i> q_ij <beta_j| to an (already phas...
Definition: lda_u.F90:4113
integer, parameter, public dft_u_none
Definition: lda_u.F90:188
subroutine, public zlda_u_add_plus_u_ket(this, d, mesh, psib, kpsib, aug_overlap)
Add the bare +U ket chi = sum_m |phi_m> sum_mp V_mmp <phi_mp|psi> to kpsib.
Definition: lda_u.F90:4252
subroutine, public zlda_u_rvu(this, mesh, space, d, namespace, psib, gpsib)
This routine computes .
Definition: lda_u.F90:5955
This modules implements the routines for doing constrain DFT for noncollinear magnetism.
integer, parameter, public constrain_none
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:94
This module defines functions over batches of mesh functions.
Definition: mesh_batch.F90:95
subroutine, public zmesh_batch_dotp_vector(mesh, aa, bb, dot, reduce, cproduct)
A simple switch between specialized kernels and generic kernels.
This module defines various routines, operating on mesh functions.
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:97
subroutine, public messages_not_implemented(feature, namespace)
Definition: messages.F90:1048
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:138
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:386
subroutine, public nonlocal_pseudopotential_stress_uspp(this, mesh, epot, periodic_dim, grad_pot, aug_weight, stress_aug)
Ultrasoft augmentation contribution to the stress tensor.
subroutine, public znonlocal_pseudopotential_density_matrix(this, mesh, std, spiral_bnd, psib, weight, beta_density, weight2, beta_density2, beta_current)
Accumulate occupation-weighted beta-projector density matrices for USPP density.
subroutine, public dpoisson_solve(this, namespace, pot, rho, all_nodes, kernel, reset)
Calculates the Poisson equation. Given the density returns the corresponding potential.
Definition: poisson.F90:909
/* To use the profiling module, call profiling_in and profiling_out with the label of the region as a...
Definition: profiling.F90:122
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
Definition: profiling.F90:672
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
Definition: profiling.F90:604
Definition: ps.F90:93
subroutine, public species_get_long_range_density(species, namespace, space, latt, pos, mesh, rho, sphere_inout, nlr_x)
subroutine, public species_get_nlcc_grad(species, space, latt, pos, mesh, rho_core_grad_x)
Returns the (r-R_I)-weighted analytic gradient of the NLCC core density.
This module handles spin dimensions of the states and the k-point distribution.
integer pure function, public states_elec_block_max(st, ib)
return index of last state in block ib
integer pure function, public states_elec_block_min(st, ib)
return index of first state in block ib
This module implements the calculation of the stress tensor.
Definition: stress.F90:97
subroutine stress_from_hartree(gr, space, volume, grad_vh, ehartree, stress_Hartree)
Computes the contribution to the stress tensor from the Hartree energy.
Definition: stress.F90:523
subroutine, public output_stress(iunit, space_dim, stress_tensors, mpi_grp, all_terms)
Definition: stress.F90:1377
subroutine stress_from_kinetic(gr, space, hm, st, symm, rcell_volume, stress_kin)
Computes the contribution to the stress tensor from the kinetic energy.
Definition: stress.F90:434
subroutine stress_from_pseudo_local(gr, hm, ions, rho_total, grad_vh, stress_ps_local)
Computes the contribution from the local part of the pseudopotential.
Definition: stress.F90:981
subroutine print_stress_tensor(ounit, space_dim, tensor)
Definition: stress.F90:1477
subroutine, public output_pressure(iunit, space_dim, total_stress_tensor)
Definition: stress.F90:1450
subroutine, public stress_calculate(namespace, gr, hm, st, ions, ks, ext_partners)
This computes the total stress on the lattice.
Definition: stress.F90:167
subroutine stress_from_hubbard(namespace, gr, st, hm, space, rcell_volume, stress_hubbard)
Computes the contribution to the stress tensor from the Hubbard energy.
Definition: stress.F90:1199
subroutine stress_from_xc(gr, hm, st, space, rcell_volume, stress_xc)
Computes the contribution to the stress tensor from the xc energy.
Definition: stress.F90:582
subroutine epot_local_pseudopotential_sr(mesh, ions, iatom, vpsl, rvpsl, dgrid)
Definition: stress.F90:1125
subroutine stress_from_pseudo_nonloc(gr, st, hm, ions, stress_ps_nl, stress_ps_aug)
Computes the contribution to the stress tensor from the nonlocal part of the pseudopotentials.
Definition: stress.F90:786
subroutine stress_from_xc_nlcc(rcell_volume, gr, st, ions, vxc, stress_xc_nlcc)
Computes the NLCC contribution to the stress tensor from the xc energy.
Definition: stress.F90:702
subroutine, public submesh_end(this)
Definition: submesh.F90:693
subroutine, public submesh_init(this, space, mesh, latt, center, rc)
Definition: submesh.F90:209
subroutine, public dsymmetrize_tensor_cart(symm, tensor, use_non_symmorphic)
Symmetric a rank-2 tensor defined in Cartesian space.
type(type_t), parameter, public type_cmplx
Definition: types.F90:114
brief This module defines the class unit_t which is used by the unit_systems_oct_m module.
Definition: unit.F90:111
character(len=20) pure function, public units_abbrev(this)
Definition: unit.F90:202
This module defines the unit system, used for input and output.
type(unit_system_t), public units_out
type(unit_t), public unit_gpa
For output pressure in GPa.
Definition: xc.F90:97
logical pure function, public family_is_mgga_with_exc(xcs)
Is the xc function part of the mGGA family with an energy functional.
Definition: xc.F90:716
logical pure function, public xc_is_energy_functional(xcs)
Is one of the x or c functional is not an energy functional.
Definition: xc.F90:826
pure logical function, public in_family(family, xc_families)
Definition: xc.F90:744
A module that takes care of xc contribution from vdW interactions.
Definition: xc_vdw.F90:95
integer(int64), dimension(5), parameter, public d3_lib_options
VDWCORRECTION options that correspond to the DFT-D3 library.
Definition: xc_vdw.F90:148
Description of the grid, containing information on derivatives, stencil, and symmetries.
Definition: grid.F90:148
Describes mesh distribution to nodes.
Definition: mesh.F90:165
This is defined even when running serial.
Definition: mpi.F90:121
A set of projectors defined on a submesh.
A type storing the information and data about a pseudopotential.
Definition: ps.F90:169
The states_elec_t class contains all electronic wave functions.
A submesh is a type of mesh, used for the projectors in the pseudopotentials It contains points on a ...
Definition: submesh.F90:154
batches of electronic states
Definition: wfs_elec.F90:120
int true(void)