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 2 : PROGRAM atom_upf_unittest
9 2 : USE atom_basis, ONLY: atom_basis_opt
10 : USE atom_kind_orbitals, ONLY: sgp_potential_conversion
11 : USE atom_operators, ONLY: atom_ppint_release,&
12 : atom_ppint_setup
13 : USE atom_sgp, ONLY: atom_sgp_potential_type,&
14 : atom_sgp_release,&
15 : fit_core_density_gaussians,&
16 : sgp_construction
17 : USE atom_types, ONLY: &
18 : CGTO_BASIS, atom_basis_gridrep, atom_basis_type, atom_integrals, atom_potential_type, &
19 : init_atom_basis, init_atom_basis_default_pp, init_atom_potential, release_atom_basis, &
20 : release_atom_potential, sgp_pseudo, upf_pseudo
21 : USE atom_upf, ONLY: atom_read_upf,&
22 : atom_release_upf,&
23 : atom_upfpot_type
24 : USE atom_utils, ONLY: atom_core_density,&
25 : atom_density
26 : USE cp_files, ONLY: close_file,&
27 : open_file
28 : USE cp_log_handling, ONLY: cp_get_default_logger,&
29 : cp_logger_get_default_io_unit,&
30 : cp_logger_type
31 : USE cp_output_handling, ONLY: silent_print_level
32 : USE external_potential_types, ONLY: allocate_potential,&
33 : deallocate_potential,&
34 : init_potential,&
35 : sgp_potential_type
36 : USE ieee_arithmetic, ONLY: ieee_is_finite
37 : USE input_constants, ONLY: contracted_gto,&
38 : do_gapw_gct,&
39 : do_gapw_log,&
40 : do_rhf_atom
41 : USE input_cp2k_atom, ONLY: create_atom_section
42 : USE input_section_types, ONLY: section_release,&
43 : section_type,&
44 : section_vals_create,&
45 : section_vals_get_subs_vals,&
46 : section_vals_release,&
47 : section_vals_type,&
48 : section_vals_val_set
49 : USE kinds, ONLY: default_path_length,&
50 : dp
51 : USE libcp2k, ONLY: cp2k_finalize,&
52 : cp2k_init
53 : USE mathconstants, ONLY: fourpi,&
54 : pi
55 : USE orbital_pointers, ONLY: co,&
56 : init_orbital_pointers,&
57 : nco,&
58 : nso
59 : USE orbital_transformation_matrices, ONLY: init_spherical_harmonics
60 : USE qs_grid_atom, ONLY: allocate_grid_atom,&
61 : create_grid_atom,&
62 : deallocate_grid_atom,&
63 : grid_atom_type
64 : USE upf_types, ONLY: upf_has_complete_partial_waves,&
65 : upf_nlcc_kinetic
66 :
67 : IMPLICIT NONE
68 : LOGICAL :: source_root_argument
69 :
70 : CHARACTER(LEN=default_path_length) :: filename
71 : INTEGER :: i, io_unit
72 : REAL(KIND=dp) :: error(6)
73 210 : TYPE(atom_upfpot_type) :: pot
74 : TYPE(atom_sgp_potential_type) :: sgp
75 :
76 2 : CALL cp2k_init()
77 2 : io_unit = cp_logger_get_default_io_unit()
78 2 : CALL check_core_derivatives()
79 2 : CALL check_projector_addition_theorem()
80 2 : CALL check_atomic_potential()
81 2 : CALL check_upf_units()
82 2 : CALL check_nlcc_fit()
83 2 : CALL check_basis_origin()
84 2 : CALL check_basis_derivatives()
85 2 : CALL check_radial_grid_endpoints()
86 2 : CALL check_contracted_basis_reader()
87 2 : CALL check_basis_optimization_export()
88 2 : CALL check_uspp_import()
89 2 : CALL check_uspp_partial_waves()
90 2 : CALL check_paw_import()
91 :
92 : ! The same temporary is reused for successive KIND sections in Quickstep.
93 2 : ALLOCATE (pot%rho_nlcc(3))
94 8 : pot%rho_nlcc = 1.0_dp
95 2 : pot%core_correction = .TRUE.
96 10 : pot%econf = [2, 4, 0, 0]
97 2 : pot%pname = "previous kind"
98 2 : CALL atom_release_upf(pot)
99 2 : IF (ALLOCATED(pot%rho_nlcc)) ERROR STOP "UPF release leaked the core density"
100 2 : IF (pot%core_correction) ERROR STOP "UPF release retained core-correction metadata"
101 10 : IF (ANY(pot%econf /= 0)) ERROR STOP "UPF release retained the previous electron configuration"
102 2 : IF (pot%pname /= "") ERROR STOP "UPF release retained the previous potential name"
103 2 : CALL atom_release_upf(pot)
104 :
105 2 : ALLOCATE (sgp%a_nlcc(2), sgp%c_nlcc(2))
106 2 : sgp%has_nlcc = .TRUE.
107 2 : sgp%n_nlcc = 2
108 2 : sgp%is_nonlocal(2) = .TRUE.
109 2 : CALL atom_sgp_release(sgp)
110 2 : IF (ASSOCIATED(sgp%a_nlcc) .OR. ASSOCIATED(sgp%c_nlcc)) ERROR STOP "SGP release leaked NLCC arrays"
111 2 : IF (sgp%has_nlcc .OR. sgp%n_nlcc /= 0) ERROR STOP "SGP release retained NLCC metadata"
112 14 : IF (ANY(sgp%is_nonlocal)) ERROR STOP "SGP release retained nonlocal channels"
113 2 : CALL atom_sgp_release(sgp)
114 :
115 : ! Optional external UPF files exercise the real reader and Gaussian transformation.
116 : ! This also permits auditing a complete SSSP library without bundling it in CP2K.
117 4 : DO i = 1, COMMAND_ARGUMENT_COUNT()
118 2 : CALL GET_COMMAND_ARGUMENT(i, filename)
119 : ! do_regtest.py passes the CP2K source root before any optional UPF audit files.
120 2 : IF (i == 1) THEN
121 2 : INQUIRE (FILE=TRIM(filename)//"/tests/UNIT_TESTS", EXIST=source_root_argument)
122 2 : IF (source_root_argument) CYCLE
123 : END IF
124 0 : CALL atom_read_upf(pot, TRIM(filename))
125 0 : IF (ABS(SUM(pot%econf) - pot%zion) > 1.e-10_dp) ERROR STOP "UPF valence count mismatch"
126 0 : IF (.NOT. ALL(ieee_is_finite(pot%vlocal))) ERROR STOP "Non-finite UPF local potential"
127 0 : IF (ANY(pot%r(2:) <= pot%r(:SIZE(pot%r) - 1))) ERROR STOP "UPF mesh is not increasing"
128 0 : IF (ANY(pot%rab <= 0.0_dp)) ERROR STOP "Non-positive UPF mesh weights"
129 0 : IF (ALLOCATED(pot%dion)) THEN
130 0 : IF (.NOT. ALL(ieee_is_finite(pot%dion))) ERROR STOP "Non-finite UPF projector coefficients"
131 0 : IF (.NOT. ALL(ieee_is_finite(pot%beta))) ERROR STOP "Non-finite UPF projectors"
132 0 : IF (MAXVAL(ABS(pot%dion - TRANSPOSE(pot%dion))) > 1.e-10_dp) ERROR STOP "Non-Hermitian UPF D matrix"
133 : END IF
134 0 : CALL sgp_construction(sgp, upf_pot=pot, error=error)
135 0 : IF (.NOT. ALL(ieee_is_finite(error))) ERROR STOP "Non-finite UPF transformation errors"
136 0 : IF (pot%core_correction) THEN
137 0 : IF (.NOT. ASSOCIATED(sgp%a_nlcc)) ERROR STOP "Missing transformed UPF core density"
138 0 : IF (.NOT. ALL(ieee_is_finite(sgp%c_nlcc))) ERROR STOP "Non-finite transformed UPF core density"
139 : END IF
140 0 : IF (io_unit > 0) WRITE (io_unit, '(A,1X,A,1X,6ES16.7)') "UPF transformation:", TRIM(filename), error
141 0 : CALL atom_sgp_release(sgp)
142 4 : CALL atom_release_upf(pot)
143 : END DO
144 :
145 2 : CALL cp2k_finalize()
146 :
147 : CONTAINS
148 :
149 : ! **************************************************************************************************
150 : !> \brief ...
151 : ! **************************************************************************************************
152 2 : SUBROUTINE check_uspp_import()
153 : CHARACTER(LEN=*), PARAMETER :: fixture = "uspp-augmentation-test.upf"
154 :
155 : CHARACTER(LEN=256) :: reason
156 : INTEGER :: unit
157 : LOGICAL :: available
158 2 : REAL(dp), ALLOCATABLE :: kinetic(:)
159 210 : TYPE(atom_upfpot_type) :: pot
160 : TYPE(cp_logger_type), POINTER :: logger
161 :
162 2 : logger => cp_get_default_logger()
163 2 : IF (logger%para_env%is_source()) THEN
164 1 : OPEN (NEWUNIT=unit, FILE=fixture, STATUS='REPLACE', ACTION='WRITE')
165 : WRITE (unit, '(A)') &
166 1 : '<UPF version="2.0.1">', &
167 1 : "<PP_HEADER generated='Generated using ""atomic""' element=""Pt"" pseudo_type=""USPP""", &
168 1 : " is_ultrasoft='true' z_valence='16' mesh_size=""3"" number_of_proj=""2"" l_max=""1""", &
169 1 : ' core_correction="true" with_metagga_info="true" />', &
170 1 : '<PP_MESH mesh="3">', &
171 1 : '<PP_R>', &
172 1 : '0.5 1.0 1.5', &
173 1 : '</PP_R>', &
174 1 : '<PP_RAB>', &
175 1 : '0.5 0.5 0.5', &
176 1 : '</PP_RAB>', &
177 1 : '</PP_MESH>', &
178 1 : '<PP_NLCC>', '1 0.5 0.1', '</PP_NLCC>', &
179 1 : '<PP_TAUMOD type="real" size="3" convention="hartree_per_volume">', &
180 1 : '0.2 0.7 0.05', '</PP_TAUMOD>', &
181 1 : '<PP_TAUATOM type="real" size="3">', '3 2 1', '</PP_TAUATOM>', &
182 1 : '<PP_LOCAL type="real" size="3">', &
183 1 : '6 4 2', &
184 1 : '</PP_LOCAL>', &
185 1 : '<PP_NONLOCAL>', &
186 1 : '<PP_BETA.1 type="real" size="3" index="1" angular_momentum="0">', &
187 1 : '1 4 9', &
188 1 : '</PP_BETA.1>', &
189 1 : '<PP_BETA.2 type="real" size="3" index="2" angular_momentum="1">', &
190 1 : '2 -1 3', &
191 1 : '</PP_BETA.2>', &
192 1 : '<PP_DIJ>', &
193 1 : '2 0 0 8', &
194 1 : '</PP_DIJ>', &
195 1 : '<PP_AUGMENTATION q_with_l="T" nqf="0" nqlc="3">', &
196 1 : '<PP_Q>', &
197 1 : '2 0 0 3', &
198 1 : '</PP_Q>', &
199 1 : '<PP_QIJL.2.2.0 first_index="2" second_index="2" composite_index="3" angular_momentum="0">', &
200 1 : '-3 4 5', &
201 1 : '</PP_QIJL.2.2.0>', &
202 1 : '<PP_QIJL.1.1.0 type="real" size="3" first_index="1" second_index="1"', &
203 1 : ' composite_index="1" angular_momentum="0" is_null="false">', &
204 1 : '1 2 3', &
205 1 : '</PP_QIJL.1.1.0>', &
206 1 : '<PP_QIJL.1.2.1 first_index="1" second_index="2" composite_index="2" angular_momentum="1">', &
207 1 : '4 -5 6', &
208 1 : '</PP_QIJL.1.2.1>', &
209 1 : '<PP_QIJL.2.2.2 first_index="2" second_index="2" composite_index="3"', &
210 1 : ' angular_momentum="2" is_null="T" size="0"/>', &
211 1 : '</PP_AUGMENTATION>', &
212 1 : '</PP_NONLOCAL>', &
213 2 : '</UPF>'
214 1 : CLOSE (unit)
215 : END IF
216 2 : CALL logger%para_env%sync()
217 2 : CALL atom_read_upf(pot, fixture, read_augmentation=.TRUE.)
218 2 : IF (pot%generated /= 'Generated using "atomic"') ERROR STOP 'Quoted UPF header value was changed'
219 10 : IF (ANY(pot%econf(0:3) /= [1, 6, 9, 0])) ERROR STOP 'Wrong 62-electron core subtraction'
220 8 : IF (ANY(SHAPE(pot%qijl) /= [3, 3, 3]) .OR. LBOUND(pot%qijl, 3) /= 0) ERROR STOP 'Wrong augmentation shape'
221 14 : IF (MAXVAL(ABS(pot%qij - RESHAPE([2._dp, 0._dp, 0._dp, 3._dp], [2, 2]))) > 1.e-14_dp) THEN
222 0 : ERROR STOP 'PP_Q normalization changed'
223 : END IF
224 8 : IF (MAXVAL(ABS(pot%qijl(:, 1, 0) - [1._dp, 2._dp, 3._dp])) > 1.e-14_dp) ERROR STOP 'Wrong s-s augmentation'
225 8 : IF (MAXVAL(ABS(pot%qijl(:, 2, 1) - [4._dp, -5._dp, 6._dp])) > 1.e-14_dp) ERROR STOP 'Wrong s-p augmentation'
226 8 : IF (MAXVAL(ABS(pot%qijl(:, 3, 0) - [-3._dp, 4._dp, 5._dp])) > 1.e-14_dp) ERROR STOP 'Wrong p-p augmentation'
227 26 : IF (ANY(pot%qijl(:, :, 2) /= 0._dp)) ERROR STOP 'Null augmentation was not zero initialized'
228 8 : IF (MAXVAL(ABS(pot%beta(:, 2) - [4._dp, -1._dp, 2._dp])) > 1.e-14_dp) ERROR STOP 'USPP beta normalization changed'
229 2 : IF (ABS(pot%dion(2, 2) - 4._dp) > 1.e-14_dp) ERROR STOP 'USPP D is not in Hartree'
230 2 : IF (.NOT. pot%with_metagga_info) ERROR STOP 'UPF meta-GGA header flag was not retained'
231 8 : IF (ANY(pot%tau_atom /= [3._dp, 2._dp, 1._dp]) .OR. pot%tau_atom_convention /= '') THEN
232 0 : ERROR STOP 'Raw UPF atomic tau was converted or assigned an unknown convention'
233 : END IF
234 2 : CALL upf_nlcc_kinetic(pot, kinetic, available, reason)
235 8 : IF (.NOT. available .OR. ANY(kinetic /= [0.2_dp, 0.7_dp, 0.05_dp])) THEN
236 0 : ERROR STOP 'Explicit canonical UPF core tau was not preserved'
237 : END IF
238 2 : pot%tau_nlcc_convention = ''
239 2 : CALL upf_nlcc_kinetic(pot, kinetic, available, reason)
240 2 : IF (available) ERROR STOP 'Unlabelled core tau was accepted as canonical'
241 2 : DEALLOCATE (pot%tau_nlcc)
242 2 : CALL upf_nlcc_kinetic(pot, kinetic, available, reason)
243 2 : IF (available) ERROR STOP 'NLCC charge was used to infer missing core tau'
244 8 : pot%rho_nlcc = 0.0_dp
245 2 : CALL upf_nlcc_kinetic(pot, kinetic, available, reason)
246 8 : IF (.NOT. available .OR. ANY(kinetic /= 0.0_dp)) ERROR STOP 'Absent core tau was not exact zero'
247 8 : ALLOCATE (pot%tau_nlcc(3), SOURCE=1.0_dp)
248 2 : CALL upf_nlcc_kinetic(pot, kinetic, available, reason)
249 2 : IF (available) ERROR STOP 'Nonzero core tau without NLCC charge was accepted'
250 2 : CALL atom_release_upf(pot)
251 2 : IF (ALLOCATED(pot%qij) .OR. ALLOCATED(pot%qijl)) ERROR STOP 'Augmentation arrays were not released'
252 2 : IF (ALLOCATED(pot%tau_nlcc) .OR. ALLOCATED(pot%tau_atom)) ERROR STOP 'UPF kinetic arrays were not released'
253 2 : IF (pot%with_metagga_info .OR. pot%tau_nlcc_convention /= '' .OR. pot%tau_atom_convention /= '') THEN
254 0 : ERROR STOP 'UPF kinetic metadata was not reset'
255 : END IF
256 2 : CALL logger%para_env%sync()
257 2 : IF (logger%para_env%is_source()) THEN
258 1 : OPEN (NEWUNIT=unit, FILE=fixture, STATUS='OLD')
259 1 : CLOSE (unit, STATUS='DELETE')
260 : END IF
261 :
262 4 : END SUBROUTINE check_uspp_import
263 :
264 : ! **************************************************************************************************
265 : !> \brief ...
266 : ! **************************************************************************************************
267 2 : SUBROUTINE check_uspp_partial_waves()
268 : CHARACTER(LEN=*), PARAMETER :: fixture = 'uspp-partial-waves-test.upf'
269 :
270 : CHARACTER(LEN=5) :: has_wfc
271 : INTEGER :: unit, variant
272 210 : TYPE(atom_upfpot_type) :: potential
273 : TYPE(cp_logger_type), POINTER :: logger
274 :
275 2 : logger => cp_get_default_logger()
276 6 : DO variant = 1, 2
277 4 : has_wfc = 'false'
278 4 : IF (variant == 1) has_wfc = 'true'
279 4 : IF (logger%para_env%is_source()) THEN
280 2 : OPEN (NEWUNIT=unit, FILE=fixture, STATUS='REPLACE', ACTION='WRITE')
281 : WRITE (unit, '(A)') &
282 2 : '<UPF version="2.0.1">', &
283 2 : '<PP_HEADER element="H" pseudo_type="US" is_ultrasoft="true" is_paw="false"', &
284 2 : ' has_wfc="'//TRIM(has_wfc)//'" z_valence="1" mesh_size="3"', &
285 2 : ' number_of_proj="2" number_of_wfc="1" l_max="1"/>', &
286 2 : '<PP_MESH mesh="3">', &
287 2 : '<PP_R>', '0.5 1 1.5', '</PP_R>', &
288 2 : '<PP_RAB>', '0.5 0.5 0.5', '</PP_RAB>', '</PP_MESH>', &
289 2 : '<PP_LOCAL>', '6 4 2', '</PP_LOCAL>', &
290 2 : '<PP_NONLOCAL>', &
291 2 : '<PP_BETA.1 index="1" angular_momentum="0" size="3">', '1 4 9', '</PP_BETA.1>', &
292 2 : '<PP_BETA.2 index="2" angular_momentum="1" size="3">', '2 -1 3', '</PP_BETA.2>', &
293 2 : '<PP_DIJ>', '2 0 0 8', '</PP_DIJ>', &
294 2 : '<PP_AUGMENTATION q_with_l="T" nqf="0" nqlc="3">', &
295 2 : '<PP_Q>', '2 0 0 3', '</PP_Q>', &
296 2 : '<PP_QIJL.1.1.0 first_index="1" second_index="1" composite_index="1" angular_momentum="0">', &
297 2 : '1 2 3', '</PP_QIJL.1.1.0>', &
298 2 : '<PP_QIJL.1.2.1 first_index="1" second_index="2" composite_index="2" angular_momentum="1">', &
299 2 : '4 -5 6', '</PP_QIJL.1.2.1>', &
300 2 : '<PP_QIJL.2.2.0 first_index="2" second_index="2" composite_index="3" angular_momentum="0">', &
301 2 : '-3 4 5', '</PP_QIJL.2.2.0>', &
302 2 : '<PP_QIJL.2.2.2 first_index="2" second_index="2" composite_index="3" angular_momentum="2">', &
303 2 : '0 0 0', '</PP_QIJL.2.2.2>', '</PP_AUGMENTATION>', '</PP_NONLOCAL>', &
304 4 : '<PP_PSWFC>', '<PP_CHI.1 l="0">', '999 999 999', '</PP_CHI.1>', '</PP_PSWFC>'
305 2 : IF (variant == 1) THEN
306 : ! Reordered, deliberately unnormalized waves distinguish every index and side.
307 : WRITE (unit, '(A)') &
308 1 : '<PP_FULL_WFC number_of_wfc="2">', &
309 1 : '<PP_PSWFC.2 type="real" size="3" l="1">', '2 3 -4', '</PP_PSWFC.2>', &
310 1 : '<PP_AEWFC.1 index="1" l="0">', '-3 5 -7', '</PP_AEWFC.1>', &
311 1 : '<PP_AEWFC.2 index="2" l="1">', '5 -7 9', '</PP_AEWFC.2>', &
312 2 : '<PP_PSWFC.1 l="0">', '1 -2 3', '</PP_PSWFC.1>', '</PP_FULL_WFC>'
313 : END IF
314 2 : WRITE (unit, '(A)') '</UPF>'
315 2 : CLOSE (unit)
316 : END IF
317 4 : CALL logger%para_env%sync()
318 4 : IF (variant == 1) THEN
319 2 : CALL atom_read_upf(potential, fixture, read_header=.TRUE.)
320 2 : IF (ALLOCATED(potential%partial_waves)) ERROR STOP 'USPP full waves imported without augmentation request'
321 2 : CALL atom_release_upf(potential)
322 : END IF
323 4 : CALL atom_read_upf(potential, fixture, read_augmentation=.TRUE.)
324 4 : IF (potential%is_paw) ERROR STOP 'USPP full waves were classified as PAW'
325 4 : IF (variant == 1) THEN
326 2 : IF (.NOT. ALLOCATED(potential%partial_waves)) ERROR STOP 'Missing optional USPP full waves'
327 : ASSOCIATE (waves => potential%partial_waves)
328 6 : IF (ANY(SHAPE(waves%ae_wfc) /= [3, 2])) ERROR STOP 'USPP full-wave count differs from beta count'
329 18 : IF (ANY(waves%ae_wfc /= RESHAPE([-3._dp, 5._dp, -7._dp, 5._dp, -7._dp, 9._dp], [3, 2]))) THEN
330 0 : ERROR STOP 'USPP AE full-wave order or original normalization changed'
331 : END IF
332 18 : IF (ANY(waves%ps_wfc /= RESHAPE([1._dp, -2._dp, 3._dp, 2._dp, 3._dp, -4._dp], [3, 2]))) THEN
333 0 : ERROR STOP 'USPP PS full waves changed or were confused with atomic guesses'
334 : END IF
335 6 : IF (ANY(waves%angular_momenta /= [0, 1])) ERROR STOP 'Wrong USPP full-wave angular momenta'
336 : END ASSOCIATE
337 2 : IF (.NOT. upf_has_complete_partial_waves(potential)) ERROR STOP 'Complete original full waves not recognized'
338 2 : DEALLOCATE (potential%partial_waves%ps_wfc)
339 2 : IF (upf_has_complete_partial_waves(potential)) ERROR STOP 'Incomplete full-wave pair accepted'
340 : ELSE
341 2 : IF (upf_has_complete_partial_waves(potential)) ERROR STOP 'Atomic guess waves accepted as complete full waves'
342 2 : IF (ALLOCATED(potential%partial_waves)) ERROR STOP 'USPP without full waves allocated partial-wave data'
343 : END IF
344 4 : CALL atom_release_upf(potential)
345 4 : IF (ALLOCATED(potential%partial_waves)) ERROR STOP 'USPP full-wave data not released'
346 6 : CALL logger%para_env%sync()
347 : END DO
348 2 : IF (logger%para_env%is_source()) THEN
349 1 : OPEN (NEWUNIT=unit, FILE=fixture, STATUS='OLD')
350 1 : CLOSE (unit, STATUS='DELETE')
351 : END IF
352 2 : END SUBROUTINE check_uspp_partial_waves
353 :
354 : ! **************************************************************************************************
355 : !> \brief Read PAW one-center data and one optional frozen-core orbital.
356 : ! **************************************************************************************************
357 2 : SUBROUTINE check_paw_import()
358 : CHARACTER(LEN=*), PARAMETER :: fixture = 'paw-import-test.upf'
359 :
360 : INTEGER :: unit
361 210 : TYPE(atom_upfpot_type) :: pot
362 : TYPE(cp_logger_type), POINTER :: logger
363 :
364 2 : logger => cp_get_default_logger()
365 2 : IF (logger%para_env%is_source()) THEN
366 1 : OPEN (NEWUNIT=unit, FILE=fixture, STATUS='REPLACE', ACTION='WRITE')
367 : WRITE (unit, '(A)') &
368 1 : '<UPF version="2.0.1">', &
369 1 : '<PP_HEADER element="Li" pseudo_type="PAW" is_ultrasoft="true" is_paw="true"', &
370 1 : ' z_valence="1" mesh_size="3" number_of_proj="1" l_max="0"/>', &
371 1 : '<PP_MESH mesh="3">', '<PP_R>', '0.5 1 1.5', '</PP_R>', &
372 1 : '<PP_RAB>', '0.5 0.5 0.5', '</PP_RAB>', '</PP_MESH>', &
373 1 : '<PP_LOCAL>', '-6 -4 -2', '</PP_LOCAL>', '<PP_NONLOCAL>', &
374 1 : '<PP_BETA.1 index="1" angular_momentum="0" size="3">', '1 2 3', '</PP_BETA.1>', &
375 1 : '<PP_DIJ>', '2', '</PP_DIJ>', &
376 1 : '<PP_AUGMENTATION q_with_l="T" nqf="0" nqlc="1" cutoff_r_index="2" l_max_aug="0">', &
377 1 : '<PP_Q>', '0.25', '</PP_Q>', '<PP_MULTIPOLES>', '0.125', '</PP_MULTIPOLES>', &
378 1 : '<PP_QIJL.1.1.0 first_index="1" second_index="1" composite_index="1" angular_momentum="0">', &
379 1 : '1 2 0', '</PP_QIJL.1.1.0>', '</PP_AUGMENTATION>', '</PP_NONLOCAL>', &
380 1 : '<PP_FULL_WFC number_of_wfc="1">', &
381 1 : '<PP_AEWFC.1 l="0">', '-2 3 4', '</PP_AEWFC.1>', &
382 1 : '<PP_PSWFC.1 l="0">', '1 3 4', '</PP_PSWFC.1>', '</PP_FULL_WFC>', &
383 1 : '<PP_PAW paw_data_format="2" core_energy="-8">', &
384 1 : '<PP_OCCUPATIONS>', '1', '</PP_OCCUPATIONS>', &
385 1 : '<PP_AE_NLCC>', '2 1 0', '</PP_AE_NLCC>', &
386 1 : '<PP_AE_VLOC>', '-10 -6 -2', '</PP_AE_VLOC>', '</PP_PAW>', &
387 1 : '<PP_GIPAW gipaw_data_format="2">', '<PP_GIPAW_CORE_ORBITALS number_of_core_orbitals="1">', &
388 1 : '<PP_GIPAW_CORE_ORBITAL.1 index="1" n="1.0" l="0.0">', &
389 2 : '3 -2 1', '</PP_GIPAW_CORE_ORBITAL.1>', '</PP_GIPAW_CORE_ORBITALS>', '</PP_GIPAW>', '</UPF>'
390 1 : CLOSE (unit)
391 : END IF
392 2 : CALL logger%para_env%sync()
393 2 : CALL atom_read_upf(pot, fixture, read_header=.TRUE.)
394 2 : IF (.NOT. pot%is_paw .OR. ALLOCATED(pot%paw)) ERROR STOP 'Wrong PAW header-only import'
395 2 : CALL atom_release_upf(pot)
396 2 : CALL atom_read_upf(pot, fixture, read_paw=.TRUE.)
397 2 : IF (.NOT. upf_has_complete_partial_waves(pot)) ERROR STOP 'Missing complete PAW waves'
398 : ASSOCIATE (paw => pot%paw)
399 14 : IF (ANY(paw%ae_wfc(:, 1) /= [-2._dp, 3._dp, 4._dp]) .OR. &
400 0 : ANY(paw%ps_wfc(:, 1) /= [1._dp, 3._dp, 4._dp])) ERROR STOP 'Changed PAW wave normalization'
401 8 : IF (paw%cutoff_index /= 2 .OR. paw%multipoles(1, 1, 0) /= 0.125_dp .OR. &
402 0 : ANY(pot%qijl(:, 1, 0) /= [1._dp, 2._dp, 0._dp])) ERROR STOP 'Changed PAW augmentation'
403 8 : IF (.NOT. paw%has_core_energy .OR. paw%core_energy /= -4._dp .OR. &
404 0 : ANY(paw%vlocal /= [-5._dp, -3._dp, -1._dp])) ERROR STOP 'Wrong PAW energy units'
405 10 : IF (ANY(paw%rho_core /= [2._dp, 1._dp, 0._dp]) .OR. &
406 0 : ANY(paw%occupations /= 1._dp)) ERROR STOP 'Changed PAW core charge or occupations'
407 2 : IF (.NOT. ALLOCATED(paw%gipaw_core_orbitals)) ERROR STOP 'Missing GIPAW core orbitals'
408 2 : ASSOCIATE (orbitals => paw%gipaw_core_orbitals)
409 12 : IF (ANY(orbitals%principal_quantum_numbers /= 1) .OR. ANY(orbitals%angular_momenta /= 0) .OR. &
410 2 : ANY(orbitals%wfc(:, 1) /= [3._dp, -2._dp, 1._dp])) ERROR STOP 'Changed GIPAW core orbital'
411 : END ASSOCIATE
412 : END ASSOCIATE
413 2 : CALL atom_release_upf(pot)
414 2 : IF (ALLOCATED(pot%paw) .OR. pot%is_paw) ERROR STOP 'PAW data were not reset on release'
415 2 : CALL logger%para_env%sync()
416 2 : IF (logger%para_env%is_source()) THEN
417 1 : OPEN (NEWUNIT=unit, FILE=fixture, STATUS='OLD')
418 1 : CLOSE (unit, STATUS='DELETE')
419 : END IF
420 2 : END SUBROUTINE check_paw_import
421 :
422 : ! **************************************************************************************************
423 : !> \brief Check the repaired radial maps at the size that exposes endpoint cancellation.
424 : ! **************************************************************************************************
425 2 : SUBROUTINE check_radial_grid_endpoints()
426 : INTEGER, PARAMETER :: n = 6400, types(2) = [do_gapw_gct, do_gapw_log]
427 : REAL(dp), PARAMETER :: inner(2) = [4.83458313724687745e-18_dp, 6.97482911687147684e-18_dp], &
428 : outer(2) = [2.06843066219244770e17_dp, 57.5213142095328827_dp]
429 :
430 : INTEGER :: i
431 : REAL(dp) :: error
432 : TYPE(grid_atom_type), POINTER :: grid
433 :
434 2 : NULLIFY (grid)
435 6 : DO i = 1, SIZE(types)
436 4 : CALL allocate_grid_atom(grid)
437 4 : CALL create_grid_atom(grid, n, 1, 2, 0, types(i))
438 51204 : IF (.NOT. ALL(ieee_is_finite(grid%rad)) .OR. .NOT. ALL(ieee_is_finite(grid%wr))) THEN
439 0 : ERROR STOP 'Nonfinite radial grid'
440 : END IF
441 51208 : IF (ANY(grid%rad <= 0.0_dp) .OR. ANY(grid%wr <= 0.0_dp)) ERROR STOP 'Nonpositive radial grid'
442 25600 : IF (ANY(grid%rad(:n - 1) <= grid%rad(2:))) ERROR STOP 'Unordered radial points'
443 : ! Independent analytic moments include the r**2 radial measure.
444 25604 : error = ABS(SUM(grid%wr*EXP(-grid%rad)) - 2.0_dp)
445 25604 : error = MAX(error, ABS(SUM(grid%wr*EXP(-grid%rad)/grid%rad) - 1.0_dp))
446 25604 : error = MAX(error, ABS(SUM(grid%wr*EXP(-grid%rad2)) - SQRT(pi)/4.0_dp))
447 4 : IF (error > 1.e-10_dp) ERROR STOP 'Radial moment error'
448 : ! Independent high-precision endpoints reject clipping rounded cosines.
449 4 : IF (ABS(grid%rad(n)/inner(i) - 1.0_dp) > 1.e-13_dp .OR. &
450 0 : ABS(grid%rad(1)/outer(i) - 1.0_dp) > 1.e-13_dp) ERROR STOP 'Radial grid endpoint error'
451 6 : CALL deallocate_grid_atom(grid)
452 : END DO
453 2 : END SUBROUTINE check_radial_grid_endpoints
454 :
455 : ! **************************************************************************************************
456 : !> \brief Check primitive and contracted Gaussian derivatives on UPF meshes starting at zero.
457 : ! **************************************************************************************************
458 2 : SUBROUTINE check_basis_origin()
459 : INTEGER :: i, l, n, variant
460 : REAL(KIND=dp) :: expected(3), r(6001), rab(6001), scale
461 : TYPE(atom_basis_type) :: basis, radial
462 :
463 2 : CALL init_atom_basis_default_pp(basis)
464 14 : n = MAXVAL(basis%nbas)
465 : ! A long supplied mesh must not generate an unused singular auxiliary grid.
466 2 : r = [(0.002_dp*REAL(i - 1, dp), i=1, SIZE(r))]
467 12004 : rab = 0.002_dp
468 2 : CALL atom_basis_gridrep(basis, radial, r, rab)
469 24006 : IF (ANY(radial%grid%rad /= r) .OR. ANY(radial%grid%wr /= rab*r**2)) THEN
470 0 : ERROR STOP "Supplied UPF mesh changed during basis evaluation"
471 : END IF
472 12004 : IF (MAXVAL(ABS(radial%bf(:, 1, 0) - EXP(-basis%am(1, 0)*r**2))) > 1.e-14_dp) THEN
473 0 : ERROR STOP "Incorrect basis values on long UPF mesh"
474 : END IF
475 2 : CALL release_atom_basis(radial)
476 6 : DO variant = 1, 2
477 4 : scale = REAL(variant, dp)
478 4 : IF (variant == 2) THEN
479 2 : basis%basis_type = CGTO_BASIS
480 10 : ALLOCATE (basis%cm(n, n, 0:UBOUND(basis%am, 2)))
481 5054 : basis%cm = 0.0_dp
482 42 : DO i = 1, n
483 282 : basis%cm(i, i, :) = scale
484 : END DO
485 : END IF
486 4 : CALL atom_basis_gridrep(basis, radial, [0.0_dp, 1.e-16_dp, 0.1_dp], [0.1_dp, 0.1_dp, 0.1_dp])
487 5836 : IF (.NOT. ALL(ieee_is_finite(radial%bf)) .OR. .NOT. ALL(ieee_is_finite(radial%dbf)) .OR. &
488 0 : .NOT. ALL(ieee_is_finite(radial%ddbf))) ERROR STOP "Nonfinite Gaussian derivatives at the UPF origin"
489 32 : DO l = 0, UBOUND(basis%am, 2)
490 508 : DO i = 1, basis%nbas(l)
491 480 : expected = 0.0_dp
492 720 : IF (l == 0) expected = [1.0_dp, 0.0_dp, -2.0_dp*basis%am(i, l)]
493 480 : IF (l == 1) expected(2) = 1.0_dp
494 400 : IF (l == 2) expected(3) = 2.0_dp
495 1920 : IF (MAXVAL(ABS([radial%bf(1, i, l), radial%dbf(1, i, l), radial%ddbf(1, i, l)] - &
496 24 : scale*expected)) > 1.e-12_dp) ERROR STOP "Incorrect Gaussian origin limit"
497 : END DO
498 : END DO
499 6 : CALL release_atom_basis(radial)
500 : END DO
501 2 : CALL release_atom_basis(basis)
502 74 : END SUBROUTINE check_basis_origin
503 :
504 : ! **************************************************************************************************
505 : !> \brief Differentiate radial basis values and densities independently at finite radii.
506 : ! **************************************************************************************************
507 2 : SUBROUTINE check_basis_derivatives()
508 : REAL(dp), PARAMETER :: radius(4) = [0.08_dp, 0.3_dp, 0.9_dp, 2.1_dp], step = 2.e-4_dp
509 :
510 : INTEGER :: i, k, l, n, variant
511 : REAL(dp) :: derivative(5), errors(3), first, lap(5), &
512 : r(5), reference, rho(5), second
513 : REAL(dp), ALLOCATABLE :: pmat(:, :, :)
514 : TYPE(atom_basis_type) :: basis, radial
515 :
516 2 : CALL init_atom_basis_default_pp(basis)
517 14 : n = MAXVAL(basis%nbas)
518 : ! Nonunit exponents and signed mixing exercise primitives and genuine contractions.
519 14 : basis%nbas = 2
520 14 : basis%nprim = 2
521 14 : basis%am(1, :) = 0.37_dp
522 14 : basis%am(2, :) = 2.3_dp
523 10 : ALLOCATE (pmat(n, n, 0:UBOUND(basis%am, 2)))
524 2 : pmat = 0.0_dp
525 14 : pmat(1, 1, :) = 0.8_dp
526 14 : pmat(2, 2, :) = 0.4_dp
527 14 : pmat(1, 2, :) = -0.17_dp
528 14 : pmat(2, 1, :) = -0.17_dp
529 2 : errors = 0.0_dp
530 6 : DO variant = 1, 2
531 4 : IF (variant == 2) THEN
532 2 : basis%basis_type = CGTO_BASIS
533 10 : ALLOCATE (basis%cm(n, n, 0:UBOUND(basis%am, 2)))
534 5054 : basis%cm = 0.0_dp
535 14 : basis%cm(1, 1, :) = 0.7_dp
536 14 : basis%cm(2, 1, :) = -0.2_dp
537 14 : basis%cm(1, 2, :) = 0.1_dp
538 14 : basis%cm(2, 2, :) = 1.3_dp
539 : END IF
540 22 : DO k = 1, SIZE(radius)
541 96 : r = radius(k) + step*[-2.0_dp, -1.0_dp, 0.0_dp, 1.0_dp, 2.0_dp]
542 16 : CALL atom_basis_gridrep(basis, radial, r, SPREAD(step, 1, 5))
543 128 : DO l = 0, UBOUND(basis%am, 2)
544 304 : DO i = 1, 2
545 1152 : first = DOT_PRODUCT(radial%bf(:, i, l), [1.0_dp, -8.0_dp, 0.0_dp, 8.0_dp, -1.0_dp])/(12*step)
546 1152 : second = DOT_PRODUCT(radial%bf(:, i, l), [-1.0_dp, 16.0_dp, -30.0_dp, 16.0_dp, -1.0_dp])/(12*step**2)
547 192 : errors(1) = MAX(errors(1), ABS(first - radial%dbf(3, i, l))/(1 + ABS(first)))
548 288 : errors(2) = MAX(errors(2), ABS(second - radial%ddbf(3, i, l))/(1 + ABS(second)))
549 : END DO
550 : END DO
551 16 : CALL atom_density(rho, pmat, radial, UBOUND(basis%am, 2), typ="RHO")
552 32 : CALL atom_density(derivative, pmat, radial, UBOUND(basis%am, 2), typ="DER")
553 112 : CALL atom_density(lap, pmat, radial, UBOUND(basis%am, 2), typ="LAP", rr=r*r)
554 96 : first = DOT_PRODUCT(rho, [1.0_dp, -8.0_dp, 0.0_dp, 8.0_dp, -1.0_dp])/(12*step)
555 96 : second = DOT_PRODUCT(rho, [-1.0_dp, 16.0_dp, -30.0_dp, 16.0_dp, -1.0_dp])/(12*step**2)
556 16 : errors(1) = MAX(errors(1), ABS(first - derivative(3))/(1 + ABS(first)))
557 16 : reference = second + 2*first/radius(k)
558 16 : errors(3) = MAX(errors(3), ABS(reference - lap(3))/(1 + ABS(reference)))
559 20 : CALL release_atom_basis(radial)
560 : END DO
561 : END DO
562 2 : IF (io_unit > 0) WRITE (io_unit, '(A,3ES24.15)') "Atomic radial derivative/Laplacian scaled errors: ", errors
563 2 : DEALLOCATE (pmat)
564 2 : CALL release_atom_basis(basis)
565 8 : IF (MAXVAL(errors) > 2.e-7_dp) ERROR STOP "Atomic radial finite-difference derivatives disagree"
566 82 : END SUBROUTINE check_basis_derivatives
567 :
568 : ! **************************************************************************************************
569 : !> \brief Check fitted densities and their derivatives against an analytic reference.
570 : !> Evaluation points differ from the input mesh, and neither input mesh
571 : !> nor reference exponents coincide with the internal fitting sequence.
572 : ! **************************************************************************************************
573 2 : SUBROUTINE check_nlcc_fit()
574 : INTEGER, PARAMETER :: nr = 1601
575 : REAL(dp), PARAMETER :: amplitudes(3) = [0.02_dp, 1.7_dp, 0.4_dp], &
576 : exponents(3) = [0.15_dp, 2.3_dp, 35.0_dp]
577 :
578 : INTEGER :: i, mesh
579 : REAL(dp) :: density_error, derivative, derivative_error, dx, r(nr), rab(nr), radius, &
580 : reference, reference_derivative, rho(nr), value
581 2 : REAL(dp), ALLOCATABLE :: alpha(:), coeff(:)
582 :
583 6 : DO mesh = 1, 2
584 : dx = LOG(20.0_dp/1.e-6_dp)/REAL(nr - 1, dp)
585 6408 : DO i = 1, nr
586 6404 : IF (mesh == 1) THEN
587 3202 : r(i) = 20.0_dp*REAL(i - 1, dp)/REAL(nr - 1, dp)
588 3202 : rab(i) = 20.0_dp/REAL(nr - 1, dp)
589 : ELSE
590 3202 : r(i) = 1.e-6_dp*EXP(dx*REAL(i - 1, dp))
591 3202 : rab(i) = dx*r(i)
592 : END IF
593 25620 : rho(i) = SUM(amplitudes*EXP(-exponents*r(i)**2))
594 : END DO
595 4 : CALL fit_core_density_gaussians(r, rab, rho, alpha, coeff)
596 4 : density_error = 0.0_dp
597 4 : derivative_error = 0.0_dp
598 4808 : DO i = 0, 1200
599 4804 : radius = REAL(i, dp)*0.017_dp
600 19216 : reference = SUM(amplitudes*EXP(-exponents*radius**2))
601 19216 : reference_derivative = -2.0_dp*radius*SUM(exponents*amplitudes*EXP(-exponents*radius**2))
602 235396 : value = SUM(coeff*EXP(-alpha*radius**2))
603 235396 : derivative = -2.0_dp*radius*SUM(alpha*coeff*EXP(-alpha*radius**2))
604 4804 : density_error = MAX(density_error, ABS(value - reference))
605 4808 : derivative_error = MAX(derivative_error, ABS(derivative - reference_derivative))
606 : END DO
607 4 : IF (io_unit > 0) WRITE (io_unit, '(A,I2,2ES16.7)') &
608 2 : "Core Gaussian fit, density and derivative:", mesh, density_error, derivative_error
609 4 : IF (density_error > 2.e-7_dp) ERROR STOP "Gaussian core-density fit is inaccurate"
610 4 : IF (derivative_error > 3.e-6_dp) ERROR STOP "Gaussian core-density derivative is inaccurate"
611 4 : rho = 0.0_dp
612 4 : CALL fit_core_density_gaussians(r, rab, rho, alpha, coeff)
613 198 : IF (ANY(coeff /= 0.0_dp)) ERROR STOP "Zero core density generated nonzero Gaussian coefficients"
614 : END DO
615 2 : END SUBROUTINE check_nlcc_fit
616 :
617 : ! **************************************************************************************************
618 : !> \brief Read an independent miniature UPF fixture and check its numerical convention.
619 : !> The values are deliberately simple and are not a physical pseudopotential.
620 : !> Checking beta separately matters: an inverse rescaling of D leaves the NC
621 : !> operator unchanged but would require rescaling USPP augmentation data too.
622 : ! **************************************************************************************************
623 2 : SUBROUTINE check_upf_units()
624 : CHARACTER(LEN=*), PARAMETER :: fixture = &
625 : 'upf-units-test-with-a-potential-filename-longer-than-'// &
626 : 'the-default-eighty-character-string-buffer.upf'
627 :
628 : INTEGER :: unit
629 10758 : TYPE(atom_potential_type) :: atomic
630 : TYPE(cp_logger_type), POINTER :: logger
631 : TYPE(section_type), POINTER :: specification
632 : TYPE(section_vals_type), POINTER :: input, potential_section
633 :
634 2 : logger => cp_get_default_logger()
635 2 : IF (logger%para_env%is_source()) THEN
636 1 : CALL open_file(fixture, unit_number=unit, file_status="REPLACE", file_action="WRITE")
637 : WRITE (unit, '(A)') &
638 1 : '<UPF version="2.0.1">', &
639 1 : '<PP_HEADER element="He" pseudo_type="NC" z_valence="2"', &
640 1 : ' mesh_size="3" number_of_proj="2" l_max="0" />', &
641 1 : '<PP_MESH mesh="3">', &
642 1 : '<PP_R type="real" size="3">', &
643 1 : '0.5 1.0 1.5', &
644 1 : '</PP_R>', &
645 1 : '<PP_RAB type="real" size="3">', &
646 1 : '0.5 0.5 0.5', &
647 1 : '</PP_RAB>', &
648 1 : '</PP_MESH>', &
649 1 : '<PP_LOCAL type="real" size="3">', &
650 1 : '6.0 4.0 2.0', &
651 1 : '</PP_LOCAL>', &
652 1 : '<PP_NONLOCAL>', &
653 1 : '<PP_BETA.1 type="real" size="3" index="1" angular_momentum="0">', &
654 1 : '1.0 4.0 9.0', &
655 1 : '</PP_BETA.1>', &
656 1 : '<PP_BETA.2 type="real" size="3" index="2" angular_momentum="0">', &
657 1 : '2.0 -1.0 3.0', &
658 1 : '</PP_BETA.2>', &
659 1 : '<PP_DIJ type="real" size="4">', &
660 1 : '2.0 0.4 0.4 8.0', &
661 1 : '</PP_DIJ>', &
662 1 : '</PP_NONLOCAL>', &
663 2 : '</UPF>'
664 1 : CALL close_file(unit)
665 : END IF
666 2 : CALL logger%para_env%sync()
667 2 : NULLIFY (specification, input)
668 2 : CALL create_atom_section(specification)
669 2 : CALL section_vals_create(input, specification)
670 2 : potential_section => section_vals_get_subs_vals(input, 'POTENTIAL')
671 2 : CALL section_vals_val_set(potential_section, 'PSEUDO_TYPE', i_val=upf_pseudo)
672 2 : CALL section_vals_val_set(potential_section, 'POTENTIAL_FILE_NAME', c_val=fixture)
673 2 : CALL init_atom_potential(atomic, potential_section, 2)
674 : ASSOCIATE (potential => atomic%upf_pot)
675 8 : IF (MAXVAL(ABS(potential%vlocal - [3.0_dp, 2.0_dp, 1.0_dp])) > 1.e-14_dp) THEN
676 0 : ERROR STOP "UPF local potential is not in Hartree"
677 : END IF
678 8 : IF (MAXVAL(ABS(potential%beta(:, 1) - [2.0_dp, 4.0_dp, 6.0_dp])) > 1.e-14_dp) THEN
679 0 : ERROR STOP "UPF changed the first tabulated projector amplitude"
680 : END IF
681 8 : IF (MAXVAL(ABS(potential%beta(:, 2) - [4.0_dp, -1.0_dp, 2.0_dp])) > 1.e-14_dp) THEN
682 0 : ERROR STOP "UPF changed the second tabulated projector amplitude"
683 : END IF
684 16 : IF (MAXVAL(ABS(potential%dion - RESHAPE([1.0_dp, 0.2_dp, 0.2_dp, 4.0_dp], [2, 2]))) > 1.e-14_dp) THEN
685 0 : ERROR STOP "UPF D matrix is not in the tabulated-projector convention"
686 : END IF
687 : END ASSOCIATE
688 2 : CALL release_atom_potential(atomic)
689 2 : CALL section_vals_release(input)
690 2 : CALL section_release(specification)
691 2 : CALL logger%para_env%sync()
692 2 : IF (logger%para_env%is_source()) THEN
693 1 : CALL open_file(fixture, unit_number=unit, file_status="OLD")
694 1 : CALL close_file(unit, file_status="DELETE")
695 : END IF
696 2 : END SUBROUTINE check_upf_units
697 :
698 : ! **************************************************************************************************
699 : !> \brief Compare the converted atomic SGP operator with analytic Gaussian integrals.
700 : !> This checks that atomic guesses and the Quickstep Hamiltonian describe
701 : !> the same radial projectors and erf-regularized local Coulomb potential.
702 : ! **************************************************************************************************
703 2 : SUBROUTINE check_atomic_potential()
704 : INTEGER :: l
705 : REAL(KIND=dp) :: a, b, p, reference
706 : TYPE(atom_basis_type) :: basis
707 : TYPE(atom_integrals) :: integrals
708 10758 : TYPE(atom_potential_type) :: atomic
709 : TYPE(sgp_potential_type), POINTER :: potential
710 :
711 2 : NULLIFY (potential)
712 2 : CALL allocate_potential(potential)
713 2 : potential%zeff = 1.0_dp
714 2 : potential%core_charge_radius = 0.4_dp
715 2 : potential%alpha_core_charge = 0.5_dp/potential%core_charge_radius**2
716 2 : potential%n_nonlocal = 1
717 2 : potential%lmax = 2
718 8 : potential%is_nonlocal(0:2) = .TRUE.
719 2 : potential%n_local = 1
720 : ALLOCATE (potential%elec_conf(0:3), potential%a_nonlocal(1), &
721 : potential%c_nonlocal(1, 1, 0:2), potential%h_nonlocal(1, 0:2), &
722 2 : potential%a_local(1), potential%c_local(1))
723 10 : potential%elec_conf = [1, 0, 0, 0]
724 4 : potential%a_nonlocal = 0.7_dp
725 20 : potential%c_nonlocal = 1.0_dp/SQRT(fourpi)
726 14 : potential%h_nonlocal = 1.0_dp
727 4 : potential%a_local = 1.2_dp
728 4 : potential%c_local = 0.3_dp
729 2 : atomic%ppot_type = sgp_pseudo
730 2 : CALL sgp_potential_conversion(potential, atomic%sgp_pot)
731 2 : CALL init_atom_basis_default_pp(basis)
732 2 : CALL atom_ppint_setup(integrals, basis, atomic)
733 8 : DO l = 0, 2
734 6 : a = basis%am(8, l) + potential%a_nonlocal(1)
735 6 : reference = (GAMMA(l + 1.5_dp)/(2.0_dp*a**(l + 1.5_dp)))**2
736 8 : IF (ABS(integrals%hnl(8, 8, l) - reference) > 1.e-9_dp*ABS(reference)) THEN
737 0 : ERROR STOP "Atomic SGP nonlocal operator differs from analytic Gaussian integrals"
738 : END IF
739 : END DO
740 2 : p = 2.0_dp*basis%am(8, 0)
741 2 : b = 1.0_dp/(SQRT(2.0_dp)*potential%core_charge_radius)
742 : reference = -potential%zeff*b/(2.0_dp*p*SQRT(p + b*b)) + &
743 2 : potential%c_local(1)*GAMMA(1.5_dp)/(2.0_dp*(p + potential%a_local(1))**1.5_dp)
744 2 : IF (ABS(integrals%core(8, 8, 0) - reference) > 1.e-9_dp*ABS(reference)) THEN
745 0 : ERROR STOP "Atomic SGP local operator differs from analytic Gaussian integrals"
746 : END IF
747 2 : CALL atom_ppint_release(integrals)
748 2 : CALL release_atom_basis(basis)
749 2 : CALL deallocate_potential(potential)
750 464 : END SUBROUTINE check_atomic_potential
751 :
752 : ! **************************************************************************************************
753 : !> \brief Verify the physical angular kernel of Gaussian UPF projectors through l=5.
754 : !> For beta_l(r) = r**l exp(-a*r**2), stripping off the radial exponentials
755 : !> at unit vectors leaves sum_m Y_lm(u) Y_lm(v) = (2*l+1) P_l(u.v)/(4*pi).
756 : !> The reference uses the Legendre recurrence, independently of the Cartesian
757 : !> to spherical transformation used by the production projector integrals.
758 : ! **************************************************************************************************
759 2 : SUBROUTINE check_projector_addition_theorem()
760 : INTEGER, PARAMETER :: lmax = 5
761 :
762 : INTEGER :: i, j, l, lx, ly, lz, nc, ns
763 : REAL(KIND=dp) :: cross, diagonal, p(0:lmax), u(3), v(3), x
764 2 : REAL(KIND=dp), ALLOCATABLE :: pu(:), pv(:)
765 : TYPE(sgp_potential_type), POINTER :: potential
766 :
767 2 : NULLIFY (potential)
768 2 : CALL init_orbital_pointers(lmax)
769 2 : CALL init_spherical_harmonics(lmax, -1)
770 2 : CALL allocate_potential(potential)
771 2 : potential%n_nonlocal = 1
772 2 : potential%lmax = lmax
773 14 : potential%nppnl = SUM(nco(0:lmax))
774 2 : ALLOCATE (potential%c_nonlocal(1, 1, 0:lmax), potential%h_nonlocal(1, 0:lmax))
775 : ! atom_sgp stores the radial coefficients divided by sqrt(4*pi).
776 38 : potential%c_nonlocal = 1.0_dp/SQRT(fourpi)
777 26 : potential%h_nonlocal = 1.0_dp
778 2 : CALL init_potential(potential)
779 2 : u = [0.6_dp, 0.0_dp, 0.8_dp]
780 2 : v = [0.3_dp, -0.4_dp, 0.5_dp]
781 14 : v = v/NORM2(v)
782 8 : x = DOT_PRODUCT(u, v)
783 2 : p(0) = 1.0_dp
784 2 : p(1) = x
785 10 : DO l = 2, lmax
786 10 : p(l) = ((2*l - 1)*x*p(l - 1) - (l - 1)*p(l - 2))/l
787 : END DO
788 : nc = 0
789 : ns = 0
790 14 : DO l = 0, lmax
791 48 : ALLOCATE (pu(nso(l)), pv(nso(l)))
792 12 : pu = 0.0_dp
793 12 : pv = 0.0_dp
794 54 : DO lx = 0, l
795 166 : DO ly = 0, l - lx
796 112 : lz = l - lx - ly
797 112 : i = nc + co(lx, ly, lz)
798 1106 : DO j = 1, nso(l)
799 952 : pu(j) = pu(j) + potential%cprj_ppnl(i, ns + j)*u(1)**lx*u(2)**ly*u(3)**lz
800 1064 : pv(j) = pv(j) + potential%cprj_ppnl(i, ns + j)*v(1)**lx*v(2)**ly*v(3)**lz
801 : END DO
802 : END DO
803 : END DO
804 12 : diagonal = REAL(2*l + 1, dp)/fourpi
805 12 : cross = diagonal*p(l)
806 : IF (ABS(DOT_PRODUCT(pu, pu) - diagonal) > 1.e-12_dp .OR. &
807 228 : ABS(DOT_PRODUCT(pv, pv) - diagonal) > 1.e-12_dp .OR. &
808 : ABS(DOT_PRODUCT(pu, pv) - cross) > 1.e-12_dp) THEN
809 0 : ERROR STOP "SGP projector kernel violates the spherical-harmonic addition theorem"
810 : END IF
811 12 : nc = nc + nco(l)
812 12 : ns = ns + nso(l)
813 14 : DEALLOCATE (pu, pv)
814 : END DO
815 2 : CALL deallocate_potential(potential)
816 2 : END SUBROUTINE check_projector_addition_theorem
817 :
818 : ! **************************************************************************************************
819 : !> \brief Verify radial derivatives and the spherical Laplacian against finite differences.
820 : ! **************************************************************************************************
821 2 : SUBROUTINE check_core_derivatives()
822 : REAL(KIND=dp), PARAMETER :: h = 1.e-4_dp
823 :
824 : REAL(KIND=dp) :: deriv(4), lap(4), lap_fd(4), minus(4), &
825 : plus(4), r(4), rho(4)
826 10798 : TYPE(atom_potential_type) :: potential
827 :
828 2 : potential%ppot_type = sgp_pseudo
829 2 : potential%sgp_pot%has_nlcc = .TRUE.
830 2 : potential%sgp_pot%n_nlcc = 2
831 6 : potential%sgp_pot%a_nlcc(1:2) = [0.7_dp, 2.3_dp]
832 6 : potential%sgp_pot%c_nlcc(1:2) = [1.8_dp, -0.2_dp]
833 2 : r = [0.0_dp, 0.1_dp, 0.7_dp, 2.0_dp]
834 2 : rho = 0.0_dp
835 2 : plus = 0.0_dp
836 2 : minus = 0.0_dp
837 2 : deriv = 0.0_dp
838 2 : lap = 0.0_dp
839 2 : CALL atom_core_density(rho, potential, "RHO", r)
840 10 : CALL atom_core_density(plus, potential, "RHO", r + h)
841 10 : CALL atom_core_density(minus, potential, "RHO", r - h)
842 2 : CALL atom_core_density(deriv, potential, "DER", r)
843 2 : CALL atom_core_density(lap, potential, "LAP", r)
844 10 : IF (MAXVAL(ABS(deriv - (plus - minus)/(2.0_dp*h))) > 1.e-7_dp) THEN
845 0 : ERROR STOP "SGP core-density derivative differs from finite differences"
846 : END IF
847 10 : lap_fd = (plus - 2.0_dp*rho + minus)/h**2
848 2 : lap_fd(1) = 3.0_dp*lap_fd(1)
849 8 : lap_fd(2:) = lap_fd(2:) + (plus(2:) - minus(2:))/(h*r(2:))
850 10 : IF (MAXVAL(ABS(lap - lap_fd)) > 1.e-6_dp) ERROR STOP "SGP core-density Laplacian differs from finite differences"
851 2 : END SUBROUTINE check_core_derivatives
852 :
853 : ! **************************************************************************************************
854 : !> \brief Check independent shell, primitive and set counts in file and inline atomic bases.
855 : ! **************************************************************************************************
856 2 : SUBROUTINE check_contracted_basis_reader()
857 : INTEGER, PARAMETER :: contractions(3) = [11, 1, 1], lower_l(3) = [0, 1, 2], &
858 : primitives(3) = [11, 41, 2], sets(3) = [1, 1, 21], upper_l(3) = [3, 6, 3]
859 2 : CHARACTER(LEN=1600), ALLOCATABLE :: lines(:)
860 : INTEGER :: b, c, i, j, l, mode, n, nc, np, ns, row, &
861 : s, t, unit
862 : REAL(dp) :: alpha, coefficient, expected, &
863 : normalization
864 : TYPE(atom_basis_type) :: basis
865 : CHARACTER(LEN=*), PARAMETER :: fixture = "atom-contracted-basis-test"
866 :
867 : TYPE(cp_logger_type), POINTER :: logger
868 : TYPE(section_type), POINTER :: specification
869 : TYPE(section_vals_type), POINTER :: input, pp_basis
870 :
871 2 : logger => cp_get_default_logger()
872 8 : DO b = 1, SIZE(sets)
873 6 : np = primitives(b)
874 6 : nc = contractions(b)
875 6 : ns = sets(b)
876 18 : ALLOCATE (lines(1 + ns*(np + 1)))
877 6 : WRITE (lines(1), '(I0)') ns
878 6 : row = 1
879 52 : DO s = 1, ns
880 46 : row = row + 1
881 150 : WRITE (lines(row), '(*(I0,1X))') 1, lower_l(b), upper_l(b), np, (nc, l=lower_l(b), upper_l(b))
882 240 : DO i = 1, np
883 188 : row = row + 1
884 188 : alpha = REAL(10*s + i, dp)/20.0_dp
885 188 : WRITE (lines(row), '(*(F12.6,1X))') alpha, &
886 2610 : ((REAL(100*s + 10*i + c + 5*l, dp)/1000.0_dp, c=1, nc), l=lower_l(b), upper_l(b))
887 : END DO
888 : END DO
889 6 : IF (logger%para_env%is_source()) THEN
890 3 : OPEN (NEWUNIT=unit, FILE=fixture, STATUS="REPLACE", ACTION="WRITE")
891 123 : WRITE (unit, '(A)') 'H READER-TEST', (TRIM(lines(i)), i=1, SIZE(lines))
892 3 : CLOSE (unit)
893 : END IF
894 6 : CALL logger%para_env%sync()
895 18 : DO mode = 1, 2
896 12 : NULLIFY (specification, input)
897 12 : CALL create_atom_section(specification)
898 12 : CALL section_vals_create(input, specification)
899 12 : pp_basis => section_vals_get_subs_vals(input, 'PP_BASIS')
900 12 : CALL section_vals_val_set(pp_basis, 'BASIS_TYPE', i_val=contracted_gto)
901 12 : CALL section_vals_val_set(pp_basis, 'GRID_POINTS', i_val=32)
902 12 : CALL section_vals_val_set(pp_basis, 'BASIS_SET', c_val='READER-TEST')
903 12 : CALL section_vals_val_set(pp_basis, 'BASIS_SET_FILE_NAME', c_val=fixture)
904 12 : IF (mode == 2) THEN
905 246 : DO i = 1, SIZE(lines)
906 246 : CALL section_vals_val_set(pp_basis, 'BASIS%_DEFAULT_KEYWORD_', i_rep_val=i, c_val=TRIM(lines(i)))
907 : END DO
908 : END IF
909 12 : CALL init_atom_basis(basis, pp_basis, 1, 'PP')
910 84 : DO l = 0, UBOUND(basis%nbas, 1)
911 72 : n = 0
912 72 : IF (l >= lower_l(b) .AND. l <= upper_l(b)) n = ns
913 72 : IF (basis%nprim(l) /= n*np .OR. basis%nbas(l) /= n*nc) THEN
914 0 : ERROR STOP "Incorrect atomic contracted basis dimensions"
915 : END IF
916 288 : DO s = 1, n
917 1608 : DO i = 1, np
918 1332 : alpha = REAL(10*s + i, dp)/20.0_dp
919 1332 : j = (s - 1)*np + i
920 1332 : IF (ABS(basis%am(j, l) - alpha) > 1.e-14_dp) ERROR STOP "Incorrect contracted basis exponent"
921 : ! Inverse square root of the analytic radial Gaussian norm.
922 1332 : normalization = SQRT(2.0_dp*(2.0_dp*alpha)**(l + 1.5_dp)/GAMMA(l + 1.5_dp))
923 9588 : DO t = 1, n
924 19196 : DO c = 1, nc
925 9812 : coefficient = 0.0_dp
926 9812 : IF (s == t) coefficient = REAL(100*s + 10*i + c + 5*l, dp)/1000.0_dp
927 9812 : expected = normalization*coefficient
928 17864 : IF (ABS(basis%cm(j, (t - 1)*nc + c, l) - expected) > 1.e-12_dp*MAX(1.0_dp, ABS(expected))) THEN
929 0 : ERROR STOP "Incorrect contracted basis coefficient or normalization"
930 : END IF
931 : END DO
932 : END DO
933 : END DO
934 : END DO
935 : END DO
936 12 : CALL release_atom_basis(basis)
937 12 : CALL section_vals_release(input)
938 18 : CALL section_release(specification)
939 : END DO
940 6 : CALL logger%para_env%sync()
941 6 : IF (logger%para_env%is_source()) THEN
942 3 : OPEN (NEWUNIT=unit, FILE=fixture, STATUS="OLD")
943 3 : CLOSE (unit, STATUS="DELETE")
944 : END IF
945 8 : DEALLOCATE (lines)
946 : END DO
947 40 : END SUBROUTINE check_contracted_basis_reader
948 :
949 : ! **************************************************************************************************
950 : !> \brief Verify exported orbitals use the best basis restored after Powell optimization.
951 : ! **************************************************************************************************
952 2 : SUBROUTINE check_basis_optimization_export()
953 : CHARACTER(LEN=160) :: line
954 : INTEGER :: i, j, status, unit
955 : REAL(dp) :: alpha(2), coefficient(2), norm
956 2 : REAL(dp), POINTER :: exponents(:)
957 : TYPE(cp_logger_type), POINTER :: logger
958 : TYPE(section_type), POINTER :: specification
959 : TYPE(section_vals_type), POINTER :: input
960 :
961 2 : NULLIFY (specification, input)
962 2 : CALL create_atom_section(specification)
963 2 : CALL section_vals_create(input, specification)
964 2 : CALL section_vals_val_set(input, 'ELEMENT', c_val='He')
965 2 : CALL section_vals_val_set(input, 'ELECTRON_CONFIGURATION', c_val='1s2')
966 2 : CALL section_vals_val_set(input, 'MAX_ANGULAR_MOMENTUM', i_val=0)
967 2 : CALL section_vals_val_set(input, 'AE_BASIS%NUM_GTO', i_val=2)
968 2 : ALLOCATE (exponents(2))
969 6 : exponents = [2.0_dp, 0.4_dp]
970 2 : CALL section_vals_val_set(input, 'AE_BASIS%S_EXPONENTS', r_vals_ptr=exponents)
971 2 : CALL section_vals_val_set(input, 'METHOD%METHOD_TYPE', i_val=do_rhf_atom)
972 2 : CALL section_vals_val_set(input, 'OPTIMIZATION%EPS_SCF', r_val=1.e-10_dp)
973 2 : CALL section_vals_val_set(input, 'OPTIMIZATION%N_DIIS', i_val=8)
974 2 : CALL section_vals_val_set(input, 'OPTIMIZATION%MAX_ITER', i_val=100)
975 2 : CALL section_vals_val_set(input, 'POWELL%MAX_FUN', i_val=5)
976 2 : CALL section_vals_val_set(input, 'POWELL%STEP_SIZE', r_val=0.15_dp)
977 2 : CALL section_vals_val_set(input, 'POWELL%ACCURACY', r_val=1.e-5_dp)
978 2 : CALL section_vals_val_set(input, 'PRINT%FIT_BASIS%_SECTION_PARAMETERS_', i_val=silent_print_level)
979 2 : CALL atom_basis_opt(input)
980 2 : CALL section_vals_release(input)
981 2 : CALL section_release(specification)
982 :
983 2 : logger => cp_get_default_logger()
984 2 : CALL logger%para_env%sync()
985 2 : OPEN (NEWUNIT=unit, FILE='OPT_BASIS', STATUS='OLD', ACTION='READ')
986 : DO
987 12 : READ (unit, '(A)', IOSTAT=status) line
988 12 : IF (status /= 0) ERROR STOP 'Missing optimized basis orbital coefficients'
989 12 : IF (INDEX(line, 'L Quantum Number:') > 0) EXIT
990 : END DO
991 6 : DO i = 1, 2
992 6 : READ (unit, *) alpha(i), coefficient(i)
993 : END DO
994 2 : CLOSE (unit)
995 2 : IF (alpha(1) <= 2.0_dp) ERROR STOP 'Optimization did not restore the earlier best exponent'
996 : norm = 0.0_dp
997 6 : DO i = 1, 2
998 14 : DO j = 1, 2
999 : norm = norm + coefficient(i)*coefficient(j)* &
1000 12 : (2.0_dp*SQRT(alpha(i)*alpha(j))/(alpha(i) + alpha(j)))**1.5_dp
1001 : END DO
1002 : END DO
1003 : ! The export rounds its coefficients to eight decimal places.
1004 2 : IF (.NOT. ieee_is_finite(norm) .OR. ABS(norm - 1.0_dp) > 1.e-7_dp) THEN
1005 0 : ERROR STOP 'Exported orbital is inconsistent with the optimized basis'
1006 : END IF
1007 2 : CALL logger%para_env%sync()
1008 2 : IF (logger%para_env%is_source()) THEN
1009 1 : OPEN (NEWUNIT=unit, FILE='OPT_BASIS', STATUS='OLD')
1010 1 : CLOSE (unit, STATUS='DELETE')
1011 : END IF
1012 2 : END SUBROUTINE check_basis_optimization_export
1013 :
1014 : END PROGRAM atom_upf_unittest
|