Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief Work function and Fermi level readback from the converged SCF, using the planar
10 : !> average of the Hartree potential over a user-defined region.
11 : !> \author Nanchen DONGFANG
12 : ! **************************************************************************************************
13 : MODULE qs_electrons_workfunction
14 : USE IEEE_ARITHMETIC, ONLY: IEEE_IS_FINITE
15 : USE cp_control_types, ONLY: dft_control_type
16 : USE cp_dbcsr_api, ONLY: dbcsr_p_type
17 : USE cp_fm_types, ONLY: cp_fm_type
18 : USE force_env_types, ONLY: force_env_get,&
19 : force_env_type
20 : USE kinds, ONLY: dp
21 : USE message_passing, ONLY: mp_para_env_type
22 : USE physcon, ONLY: bohr
23 : USE pw_types, ONLY: pw_r3d_rs_type
24 : USE qs_energy_types, ONLY: qs_energy_type
25 : USE qs_environment_types, ONLY: get_qs_env,&
26 : qs_environment_type
27 : USE qs_mo_methods, ONLY: calculate_subspace_eigenvalues
28 : USE qs_mo_types, ONLY: get_mo_set,&
29 : mo_set_type
30 : #include "./base/base_uses.f90"
31 :
32 : IMPLICIT NONE
33 :
34 : PRIVATE
35 : PUBLIC :: qs_electrons_compute_workfunction
36 :
37 : REAL(KIND=dp), PARAMETER, PRIVATE :: HARTREE_TO_EV = 27.211386245988_dp
38 :
39 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_electrons_workfunction'
40 :
41 : CONTAINS
42 :
43 : ! **************************************************************************************************
44 : !> \brief Average the Hartree potential over a slab perpendicular to a lattice axis.
45 : !> \param v_hartree_rspace Hartree potential on the real-space grid
46 : !> \param para_env parallel environment of the grid
47 : !> \param axis axis (1=x, 2=y, 3=z) along which the potential is planar-averaged
48 : !> \param minimum_A lower bound of the averaging slab, Angstrom
49 : !> \param maximum_A upper bound of the averaging slab, Angstrom
50 : !> \param v_avg_hartree the averaged potential, Hartree
51 : ! **************************************************************************************************
52 10 : SUBROUTINE planar_hartree_average(v_hartree_rspace, para_env, axis, minimum_A, maximum_A, &
53 : v_avg_hartree)
54 : TYPE(pw_r3d_rs_type), POINTER :: v_hartree_rspace
55 : TYPE(mp_para_env_type), POINTER :: para_env
56 : INTEGER, INTENT(IN) :: axis
57 : REAL(KIND=dp), INTENT(IN) :: minimum_A, maximum_A
58 : REAL(KIND=dp), INTENT(OUT) :: v_avg_hartree
59 :
60 : INTEGER :: grid_index, ix, iy, iz
61 : INTEGER, DIMENSION(2, 3) :: bounds_global, bounds_local
62 : REAL(KIND=dp) :: cell_hmat(3, 3), coordinate_bohr, cross_product(3), face_norm, height_bohr, &
63 : local_count, local_sum, maximum_bohr, minimum_bohr, udvol
64 :
65 10 : IF (.NOT. ASSOCIATED(para_env)) THEN
66 0 : CPABORT("Planar-potential average could not access para_env.")
67 : END IF
68 10 : IF (.NOT. ASSOCIATED(v_hartree_rspace)) THEN
69 0 : CPABORT("Planar-potential average could not access the Hartree potential grid.")
70 : END IF
71 10 : IF (.NOT. ASSOCIATED(v_hartree_rspace%array)) THEN
72 0 : CPABORT("Planar-potential average found an unallocated Hartree potential grid.")
73 : END IF
74 10 : IF (.NOT. ASSOCIATED(v_hartree_rspace%pw_grid)) THEN
75 0 : CPABORT("Planar-potential average found a Hartree potential grid without metadata.")
76 : END IF
77 10 : IF (v_hartree_rspace%pw_grid%dvol <= 0.0_dp) THEN
78 0 : CPABORT("Planar-potential average found an invalid Hartree potential grid volume.")
79 : END IF
80 :
81 10 : minimum_bohr = minimum_A*bohr
82 10 : maximum_bohr = maximum_A*bohr
83 10 : IF (axis < 1 .OR. axis > 3) THEN
84 0 : CPABORT("Planar-potential average received an invalid averaging axis.")
85 : END IF
86 10 : IF (maximum_bohr <= minimum_bohr) THEN
87 0 : CPABORT("Planar-potential average requires potential_average_max_A > min_A.")
88 : END IF
89 :
90 : cell_hmat(:, 1) = v_hartree_rspace%pw_grid%dh(:, 1)* &
91 40 : REAL(v_hartree_rspace%pw_grid%npts(1), KIND=dp)
92 : cell_hmat(:, 2) = v_hartree_rspace%pw_grid%dh(:, 2)* &
93 40 : REAL(v_hartree_rspace%pw_grid%npts(2), KIND=dp)
94 : cell_hmat(:, 3) = v_hartree_rspace%pw_grid%dh(:, 3)* &
95 40 : REAL(v_hartree_rspace%pw_grid%npts(3), KIND=dp)
96 0 : SELECT CASE (axis)
97 : CASE (1)
98 0 : cross_product(1) = cell_hmat(2, 2)*cell_hmat(3, 3) - cell_hmat(3, 2)*cell_hmat(2, 3)
99 0 : cross_product(2) = cell_hmat(3, 2)*cell_hmat(1, 3) - cell_hmat(1, 2)*cell_hmat(3, 3)
100 0 : cross_product(3) = cell_hmat(1, 2)*cell_hmat(2, 3) - cell_hmat(2, 2)*cell_hmat(1, 3)
101 : CASE (2)
102 0 : cross_product(1) = cell_hmat(2, 3)*cell_hmat(3, 1) - cell_hmat(3, 3)*cell_hmat(2, 1)
103 0 : cross_product(2) = cell_hmat(3, 3)*cell_hmat(1, 1) - cell_hmat(1, 3)*cell_hmat(3, 1)
104 0 : cross_product(3) = cell_hmat(1, 3)*cell_hmat(2, 1) - cell_hmat(2, 3)*cell_hmat(1, 1)
105 : CASE (3)
106 10 : cross_product(1) = cell_hmat(2, 1)*cell_hmat(3, 2) - cell_hmat(3, 1)*cell_hmat(2, 2)
107 10 : cross_product(2) = cell_hmat(3, 1)*cell_hmat(1, 2) - cell_hmat(1, 1)*cell_hmat(3, 2)
108 10 : cross_product(3) = cell_hmat(1, 1)*cell_hmat(2, 2) - cell_hmat(2, 1)*cell_hmat(1, 2)
109 : END SELECT
110 40 : face_norm = SQRT(SUM(cross_product**2))
111 10 : IF (.NOT. IEEE_IS_FINITE(face_norm) .OR. face_norm <= 0.0_dp) THEN
112 0 : CPABORT("Planar-potential average found a singular cell face.")
113 : END IF
114 10 : height_bohr = v_hartree_rspace%pw_grid%vol/face_norm
115 10 : IF (.NOT. IEEE_IS_FINITE(height_bohr) .OR. height_bohr <= 0.0_dp) THEN
116 0 : CPABORT("Planar-potential average found an invalid cell height.")
117 : END IF
118 10 : IF (minimum_bohr < -1.0e-10_dp .OR. maximum_bohr > height_bohr + 1.0e-10_dp) THEN
119 0 : CPABORT("CP2K potential_average_region lies outside the cell")
120 : END IF
121 :
122 10 : udvol = 1.0_dp/v_hartree_rspace%pw_grid%dvol
123 10 : local_sum = 0.0_dp
124 10 : local_count = 0.0_dp
125 :
126 100 : bounds_global = v_hartree_rspace%pw_grid%bounds
127 100 : bounds_local = v_hartree_rspace%pw_grid%bounds_local
128 :
129 910 : DO iz = bounds_local(1, 3), bounds_local(2, 3)
130 27910 : DO iy = bounds_local(1, 2), bounds_local(2, 2)
131 270900 : DO ix = bounds_local(1, 1), bounds_local(2, 1)
132 0 : SELECT CASE (axis)
133 : CASE (1)
134 0 : grid_index = ix - bounds_global(1, 1)
135 : CASE (2)
136 0 : grid_index = iy - bounds_global(1, 2)
137 : CASE (3)
138 243000 : grid_index = iz - bounds_global(1, 3)
139 : END SELECT
140 : coordinate_bohr = REAL(grid_index, KIND=dp)/ &
141 243000 : REAL(v_hartree_rspace%pw_grid%npts(axis), KIND=dp)*height_bohr
142 270000 : IF (coordinate_bohr >= minimum_bohr .AND. coordinate_bohr <= maximum_bohr) THEN
143 51300 : local_sum = local_sum + v_hartree_rspace%array(ix, iy, iz)*udvol
144 51300 : local_count = local_count + 1.0_dp
145 : END IF
146 : END DO
147 : END DO
148 : END DO
149 :
150 10 : CALL para_env%sum(local_sum)
151 10 : CALL para_env%sum(local_count)
152 10 : IF (local_count <= 0.0_dp) THEN
153 0 : CPABORT("Planar-potential average found no grid points in the requested region.")
154 : END IF
155 :
156 10 : v_avg_hartree = local_sum/local_count
157 10 : END SUBROUTINE planar_hartree_average
158 :
159 : ! **************************************************************************************************
160 : !> \brief Compute the work function (vacuum Hartree level minus Fermi level, in eV)
161 : !> from the planar average of the Hartree potential of this force_env's own
162 : !> converged SCF and its Fermi level.
163 : !> \param force_env force_env whose qs_env holds the converged SCF result
164 : !> \param axis axis (1=x, 2=y, 3=z) along which the potential is planar-averaged
165 : !> \param minimum_A lower bound of the averaging slab, Angstrom
166 : !> \param maximum_A upper bound of the averaging slab, Angstrom
167 : !> \param fermi_level_eV returned Fermi level, eV
168 : !> \param workfunction_eV returned work function, eV
169 : ! **************************************************************************************************
170 20 : SUBROUTINE qs_electrons_compute_workfunction(force_env, axis, minimum_A, maximum_A, &
171 : fermi_level_eV, workfunction_eV)
172 : TYPE(force_env_type), POINTER :: force_env
173 : INTEGER, INTENT(IN) :: axis
174 : REAL(KIND=dp), INTENT(IN) :: minimum_A, maximum_A
175 : REAL(KIND=dp), INTENT(OUT) :: fermi_level_eV, workfunction_eV
176 :
177 : CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_electrons_compute_workfunction'
178 :
179 : INTEGER :: handle, homo, ispin, nmo, nspin
180 : REAL(KIND=dp) :: fermi_hartree, v_avg_hartree
181 10 : REAL(KIND=dp), DIMENSION(:), POINTER :: mo_eigenvalues
182 : TYPE(cp_fm_type), POINTER :: mo_coeff
183 10 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks
184 : TYPE(dft_control_type), POINTER :: dft_control
185 10 : TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
186 : TYPE(mp_para_env_type), POINTER :: para_env
187 : TYPE(pw_r3d_rs_type), POINTER :: v_hartree_rspace
188 : TYPE(qs_energy_type), POINTER :: energy
189 : TYPE(qs_environment_type), POINTER :: qs_env
190 :
191 10 : CALL timeset(routineN, handle)
192 :
193 10 : IF (axis < 1 .OR. axis > 3) THEN
194 0 : CPABORT("workfunction axis must be x, y, or z.")
195 : END IF
196 10 : IF (maximum_A <= minimum_A) THEN
197 0 : CPABORT("workfunction region requires maximum_A > minimum_A.")
198 : END IF
199 :
200 10 : NULLIFY (qs_env)
201 10 : CALL force_env_get(force_env, qs_env=qs_env)
202 10 : IF (.NOT. ASSOCIATED(qs_env)) THEN
203 0 : CPABORT("workfunction update requires a QS force environment.")
204 : END IF
205 :
206 10 : NULLIFY (energy, mos, dft_control, matrix_ks, mo_coeff, mo_eigenvalues, para_env, &
207 10 : v_hartree_rspace)
208 : CALL get_qs_env(qs_env, energy=energy, mos=mos, dft_control=dft_control, matrix_ks=matrix_ks, &
209 10 : para_env=para_env, v_hartree_rspace=v_hartree_rspace)
210 10 : IF (.NOT. ASSOCIATED(energy)) THEN
211 0 : CPABORT("workfunction update could not access QS energy data.")
212 : END IF
213 :
214 : ! Tier 1: standard diagonalization sets energy%efermi directly.
215 10 : fermi_hartree = energy%efermi
216 10 : IF (fermi_hartree == 0.0_dp) THEN
217 : ! Tier 2: fall back to the average MO chemical potential (e.g. smearing).
218 0 : IF (.NOT. ASSOCIATED(mos)) THEN
219 0 : CPABORT("Fermi level unset; no MO data available.")
220 : END IF
221 0 : IF (SIZE(mos) <= 0) CPABORT("MO array is empty.")
222 0 : nspin = SIZE(mos)
223 0 : fermi_hartree = 0.0_dp
224 0 : DO ispin = 1, nspin
225 0 : IF (.NOT. IEEE_IS_FINITE(mos(ispin)%mu)) THEN
226 0 : CPABORT("MO chemical potential not finite.")
227 : END IF
228 0 : fermi_hartree = fermi_hartree + mos(ispin)%mu
229 : END DO
230 0 : fermi_hartree = fermi_hartree/REAL(nspin, KIND=dp)
231 : END IF
232 :
233 10 : IF (ABS(fermi_hartree) <= 10.0_dp*EPSILON(fermi_hartree)) THEN
234 : ! Tier 3: OT does not populate mo_set%mu or energy%efermi. Compute the
235 : ! occupied-subspace Ritz eigenvalues from the converged KS matrix and use
236 : ! the HOMO as the zero-temperature electronic chemical potential.
237 : ! Open shell: the chemical potential is the HIGHER of the two spin
238 : ! HOMOs (the level an electron leaves first), not their mean, which
239 : ! sits between them and biases e_F whenever the channels differ.
240 0 : IF (.NOT. ASSOCIATED(mos) .OR. .NOT. ASSOCIATED(matrix_ks)) THEN
241 0 : CPABORT("OT fallback needs MO and KS-matrix data.")
242 : END IF
243 0 : IF (SIZE(matrix_ks) < SIZE(mos)) THEN
244 0 : CPABORT("OT fallback found too few KS matrices.")
245 : END IF
246 0 : IF (ASSOCIATED(dft_control)) THEN
247 0 : IF (dft_control%do_admm) THEN
248 0 : CPABORT("OT fallback does not support ADMM.")
249 : END IF
250 : END IF
251 0 : nspin = SIZE(mos)
252 0 : fermi_hartree = -HUGE(0.0_dp)
253 0 : DO ispin = 1, nspin
254 0 : NULLIFY (mo_coeff, mo_eigenvalues)
255 : homo = 0
256 : nmo = 0
257 : CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, &
258 0 : eigenvalues=mo_eigenvalues, homo=homo, nmo=nmo)
259 0 : IF (.NOT. ASSOCIATED(mo_coeff) .OR. .NOT. ASSOCIATED(mo_eigenvalues)) THEN
260 0 : CPABORT("OT fallback could not access orbitals.")
261 : END IF
262 0 : IF (homo <= 0 .OR. homo > nmo .OR. homo > SIZE(mo_eigenvalues)) THEN
263 0 : CPABORT("OT fallback found an invalid HOMO index.")
264 : END IF
265 : CALL calculate_subspace_eigenvalues(orbitals=mo_coeff, &
266 : ks_matrix=matrix_ks(ispin)%matrix, &
267 : evals_arg=mo_eigenvalues, &
268 0 : do_rotation=.FALSE.)
269 0 : IF (.NOT. IEEE_IS_FINITE(mo_eigenvalues(homo))) THEN
270 0 : CPABORT("OT HOMO energy not finite.")
271 : END IF
272 0 : fermi_hartree = MAX(fermi_hartree, mo_eigenvalues(homo))
273 : END DO
274 : END IF
275 :
276 10 : IF (.NOT. IEEE_IS_FINITE(fermi_hartree)) THEN
277 0 : CPABORT("Fermi level not finite.")
278 : END IF
279 10 : IF (ABS(fermi_hartree) <= 10.0_dp*EPSILON(fermi_hartree)) THEN
280 0 : CPABORT("Fermi level unset after OT fallback.")
281 : END IF
282 10 : fermi_level_eV = fermi_hartree*HARTREE_TO_EV
283 :
284 : ! The average is taken from this force_env's own Hartree potential, so that the two
285 : ! endpoints of a charge-mixing run each contribute their own vacuum level.
286 : CALL planar_hartree_average(v_hartree_rspace, para_env, axis, minimum_A, maximum_A, &
287 10 : v_avg_hartree)
288 10 : workfunction_eV = v_avg_hartree*HARTREE_TO_EV - fermi_level_eV
289 :
290 10 : CALL timestop(handle)
291 :
292 10 : END SUBROUTINE qs_electrons_compute_workfunction
293 :
294 : END MODULE qs_electrons_workfunction
|