80 CHARACTER(LEN=3) :: cval1
81 CHARACTER(LEN=3*default_string_length) :: message
82 CHARACTER(LEN=60) :: line
83 CHARACTER(LEN=80),
DIMENSION(:),
POINTER :: cval2
84 CHARACTER(LEN=default_string_length) :: description
85 INTEGER :: i, ip, irep, iw, j, k, n_periodic, np, &
87 INTEGER,
DIMENSION(3) :: periodic
88 LOGICAL :: check_failed, debug_dipole, &
89 debug_forces, debug_polar, &
90 debug_stress_tensor, skip, &
92 LOGICAL,
ALLOCATABLE,
DIMENSION(:, :) :: do_dof_atom_coor
93 LOGICAL,
DIMENSION(3) :: do_dof_dipole
94 REAL(kind=
dp) :: amplitude, dd, de, derr, difference, dx, eps_no_error_check, errmax, &
95 maxerr, periodic_stress_sum, std_value, sum_of_differences
96 REAL(kind=
dp),
DIMENSION(2) :: numer_energy
97 REAL(kind=
dp),
DIMENSION(3) :: dipole_moment, dipole_numer, err, &
99 REAL(kind=
dp),
DIMENSION(3, 2) :: dipn
100 REAL(kind=
dp),
DIMENSION(3, 3) :: polar_analytic, polar_numeric
101 REAL(kind=
dp),
DIMENSION(9) :: pvals
102 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: analyt_forces, numer_forces
110 TYPE(
qs_kind_type),
DIMENSION(:),
POINTER :: qs_kind_set
113 NULLIFY (analyt_forces, numer_forces, subsys, particles)
115 root_section => force_env%root_section
117 CALL force_env_get(force_env, para_env=para_env, subsys=subsys, cell=cell)
122 l_val=debug_stress_tensor)
139 r_val=eps_no_error_check)
140 eps_no_error_check = max(eps_no_error_check, epsilon(0.0_dp))
142 l_val=stop_on_mismatch)
146 do_dof_dipole(1) = (index(cval1,
"X") /= 0)
147 do_dof_dipole(2) = (index(cval1,
"Y") /= 0)
148 do_dof_dipole(3) = (index(cval1,
"Z") /= 0)
150 IF (debug_forces)
THEN
151 np = subsys%particles%n_els
152 ALLOCATE (do_dof_atom_coor(3, np))
155 do_dof_atom_coor = .false.
159 READ (cval2(1), fmt=
"(I10)") k
161 do_dof_atom_coor(1, k) = (index(cval2(2),
"X") /= 0)
162 do_dof_atom_coor(2, k) = (index(cval2(2),
"Y") /= 0)
163 do_dof_atom_coor(3, k) = (index(cval2(2),
"Z") /= 0)
166 do_dof_atom_coor = .true.
173 IF (debug_stress_tensor)
THEN
174 IF (sum(cell%perd) == 0)
THEN
175 CALL cp_warn(__location__, &
176 "DEBUG_STRESS_TENSOR requested for PERIODIC NONE. "// &
177 "The isolated-system virial is useful for finite-difference diagnostics, "// &
178 "but it is not a physically meaningful bulk stress.")
185 SELECT CASE (stress_tensor)
190 CALL cp_warn(__location__,
"Numerical stress tensor was requested in "// &
191 "the FORCE_EVAL section. Nothing to debug!")
194 CALL cp_warn(__location__,
"Stress tensor calculation was not enabled in "// &
195 "the FORCE_EVAL section. Nothing to debug!")
202 TYPE(
virial_type) :: virial_analytical, virial_numerical
210 calc_stress_tensor=.true.)
213 virial_analytical = virial
221 virial_numerical = virial
224 IF (virial_analytical%pv_diagonal .OR. virial_numerical%pv_diagonal)
THEN
228 virial_analytical%pv_virial(i, k) = 0.0_dp
229 virial_numerical%pv_virial(i, k) = 0.0_dp
237 WRITE (unit=iw, fmt=
"((T2,A))") &
238 "DEBUG| Numerical pv_virial [a.u.]"
239 WRITE (unit=iw, fmt=
"((T2,A,T21,3(1X,F19.12)))") &
240 (
"DEBUG|", virial_numerical%pv_virial(i, 1:3), i=1, 3)
241 WRITE (unit=iw, fmt=
"(/,(T2,A))") &
242 "DEBUG| Analytical pv_virial [a.u.]"
243 WRITE (unit=iw, fmt=
"((T2,A,T21,3(1X,F19.12)))") &
244 (
"DEBUG|", virial_analytical%pv_virial(i, 1:3), i=1, 3)
245 WRITE (unit=iw, fmt=
"(/,(T2,A))") &
246 "DEBUG| Difference pv_virial [a.u.]"
247 WRITE (unit=iw, fmt=
"((T2,A,T21,3(1X,F19.12)))") &
248 (
"DEBUG|", virial_numerical%pv_virial(i, 1:3) - virial_analytical%pv_virial(i, 1:3), i=1, 3)
249 WRITE (unit=iw, fmt=
"(T2,A,T61,F20.12)") &
250 "DEBUG| Sum of differences", &
251 sum(abs(virial_numerical%pv_virial(:, :) - virial_analytical%pv_virial(:, :)))
252 CALL get_cell(cell=cell, periodic=periodic)
253 n_periodic = count(periodic /= 0)
254 IF (n_periodic > 0 .AND. n_periodic < 3)
THEN
255 periodic_stress_sum = 0.0_dp
258 IF (periodic(i) /= 0 .AND. periodic(k) /= 0)
THEN
259 periodic_stress_sum = periodic_stress_sum + &
260 abs(virial_numerical%pv_virial(i, k) - &
261 virial_analytical%pv_virial(i, k))
265 WRITE (unit=iw, fmt=
"(T2,A,T61,F20.12)") &
266 "DEBUG| Periodic-subspace sum of differences", periodic_stress_sum
271 check_failed = .false.
273 WRITE (unit=iw, fmt=
"(/T2,A)") &
274 "DEBUG| Relative error pv_virial"
275 WRITE (unit=iw, fmt=
"(T2,A,T61,ES20.8)") &
276 "DEBUG| Threshold value for error check [a.u.]", eps_no_error_check
281 IF (abs(virial_analytical%pv_virial(i, k)) >= eps_no_error_check)
THEN
282 err(k) = 100.0_dp*(virial_numerical%pv_virial(i, k) - virial_analytical%pv_virial(i, k))/ &
283 virial_analytical%pv_virial(i, k)
284 WRITE (unit=line(20*(k - 1) + 1:20*k), fmt=
"(1X,F17.2,A2)") err(k),
" %"
286 WRITE (unit=line(20*(k - 1) + 1:20*k), fmt=
"(17X,A3)")
"- %"
290 WRITE (unit=iw, fmt=
"(T2,A,T21,A60)") &
293 IF (any(abs(err(1:3)) > maxerr)) check_failed = .true.
296 WRITE (unit=iw, fmt=
"(T2,A,T61,F18.2,A2)") &
297 "DEBUG| Maximum accepted error", maxerr,
" %"
299 IF (check_failed)
THEN
300 message =
"A mismatch between the analytical and the numerical "// &
301 "stress tensor has been detected. Check the implementation "// &
302 "of the stress tensor"
303 IF (stop_on_mismatch)
THEN
304 cpabort(trim(message))
306 cpwarn(trim(message))
313 IF (debug_forces)
THEN
315 particles => subsys%particles%els
316 SELECT CASE (force_env%in_use)
318 CALL get_qs_env(force_env%qs_env, qs_kind_set=qs_kind_set)
326 calc_stress_tensor=.false.)
328 IF (
ASSOCIATED(analyt_forces))
DEALLOCATE (analyt_forces)
329 np = subsys%particles%n_els
330 ALLOCATE (analyt_forces(np, 3))
332 analyt_forces(ip, 1:3) = particles(ip)%f(1:3)
335 IF (
ASSOCIATED(numer_forces))
DEALLOCATE (numer_forces)
336 ALLOCATE (numer_forces(subsys%particles%n_els, 3))
339 IF (do_dof_atom_coor(k, ip))
THEN
340 numer_energy = 0.0_dp
341 std_value = particles(ip)%r(k)
343 particles(ip)%r(k) = std_value - (-1.0_dp)**j*dx
344 SELECT CASE (force_env%in_use)
346 CALL get_qs_env(force_env%qs_env, qs_kind_set=qs_kind_set)
353 calc_force=.false., &
354 calc_stress_tensor=.false., &
355 consistent_energies=.true.)
356 CALL force_env_get(force_env, potential_energy=numer_energy(j))
358 particles(ip)%r(k) = std_value
359 numer_forces(ip, k) = -0.5_dp*(numer_energy(1) - numer_energy(2))/dx
361 WRITE (unit=iw, fmt=
"(/,T2,A,T17,A,F7.4,A,T34,A,F7.4,A,T52,A,T68,A)") &
362 "DEBUG| Atom",
"E("//achar(119 + k)//
" +", dx,
")", &
363 "E("//achar(119 + k)//
" -", dx,
")", &
364 "f(numerical)",
"f(analytical)"
365 WRITE (unit=iw, fmt=
"(T2,A,I5,4(1X,F16.8))") &
366 "DEBUG|", ip, numer_energy(1:2), numer_forces(ip, k), analyt_forces(ip, k)
369 numer_forces(ip, k) = 0.0_dp
376 IF (do_dof_atom_coor(k, ip))
THEN
378 IF (abs(analyt_forces(ip, k)) >= eps_no_error_check)
THEN
379 err(k) = 100.0_dp*(numer_forces(ip, k) - analyt_forces(ip, k))/analyt_forces(ip, k)
382 IF (abs(analyt_forces(ip, k)) <= 0.0001_dp) my_maxerr(k) = 5.0_dp*my_maxerr(k)
388 IF (any(do_dof_atom_coor(1:3, ip)))
THEN
389 WRITE (unit=iw, fmt=
"(/,T2,A)") &
390 "DEBUG| Atom Coordinate f(numerical) f(analytical) Difference Error [%]"
393 IF (do_dof_atom_coor(k, ip))
THEN
394 difference = analyt_forces(ip, k) - numer_forces(ip, k)
395 IF (abs(analyt_forces(ip, k)) >= eps_no_error_check)
THEN
396 WRITE (unit=iw, fmt=
"(T2,A,I5,T19,A1,T26,F14.8,T42,F14.8,T57,F12.8,T71,F10.2)") &
397 "DEBUG|", ip, achar(119 + k), numer_forces(ip, k), analyt_forces(ip, k), difference, err(k)
399 WRITE (unit=iw, fmt=
"(T2,A,I5,T19,A1,T26,F14.8,T42,F14.8,T57,F12.8,T80,A1)") &
400 "DEBUG|", ip, achar(119 + k), numer_forces(ip, k), analyt_forces(ip, k), difference,
"-"
405 IF (any(abs(err(1:3)) > my_maxerr(1:3)))
THEN
406 message =
"A mismatch between analytical and numerical forces "// &
407 "has been detected. Check the implementation of the "// &
408 "analytical force calculation"
409 IF (stop_on_mismatch)
THEN
418 WRITE (unit=iw, fmt=
"(/,(T2,A))") &
419 "DEBUG|======================== BEGIN OF SUMMARY ===============================", &
420 "DEBUG| Atom Coordinate f(numerical) f(analytical) Difference Error [%]"
421 sum_of_differences = 0.0_dp
426 IF (do_dof_atom_coor(k, ip))
THEN
427 difference = analyt_forces(ip, k) - numer_forces(ip, k)
428 IF (abs(analyt_forces(ip, k)) >= eps_no_error_check)
THEN
429 err(k) = 100_dp*(numer_forces(ip, k) - analyt_forces(ip, k))/analyt_forces(ip, k)
430 errmax = max(errmax, abs(err(k)))
431 WRITE (unit=iw, fmt=
"(T2,A,I5,T19,A1,T26,F14.8,T42,F14.8,T57,F12.8,T71,F10.2)") &
432 "DEBUG|", ip, achar(119 + k), numer_forces(ip, k), analyt_forces(ip, k), difference, err(k)
434 WRITE (unit=iw, fmt=
"(T2,A,I5,T19,A1,T26,F14.8,T42,F14.8,T57,F12.8,T80,A1)") &
435 "DEBUG|", ip, achar(119 + k), numer_forces(ip, k), analyt_forces(ip, k), difference,
"-"
437 sum_of_differences = sum_of_differences + abs(difference)
441 WRITE (unit=iw, fmt=
"(T2,A,T57,F12.8,T71,F10.2)") &
442 "DEBUG| Sum of differences:", sum_of_differences, errmax
443 WRITE (unit=iw, fmt=
"(T2,A)") &
444 "DEBUG|======================== END OF SUMMARY ================================="
447 IF (
ASSOCIATED(analyt_forces))
DEALLOCATE (analyt_forces)
448 IF (
ASSOCIATED(numer_forces))
DEALLOCATE (numer_forces)
449 DEALLOCATE (do_dof_atom_coor)
452 IF (debug_dipole)
THEN
454 CALL get_qs_env(force_env%qs_env, dft_control=dft_control)
455 poldir = [0.0_dp, 0.0_dp, 1.0_dp]
457 CALL set_efield(dft_control, amplitude, poldir)
459 CALL get_qs_env(force_env%qs_env, results=results)
460 description =
"[DIPOLE]"
462 CALL get_results(results, description=description, values=dipole_moment)
464 CALL cp_warn(__location__,
"Debug of dipole moments needs DFT/PRINT/MOMENTS section enabled")
469 IF (do_dof_dipole(k))
THEN
473 poldir = -1.0_dp*poldir
474 CALL set_efield(dft_control, amplitude, poldir)
476 CALL force_env_get(force_env, potential_energy=numer_energy(j))
478 dipole_numer(k) = 0.5_dp*(numer_energy(1) - numer_energy(2))/de
480 dipole_numer(k) = 0.0_dp
484 WRITE (unit=iw, fmt=
"(/,(T2,A))") &
485 "DEBUG|========================= DIPOLE MOMENTS ================================", &
486 "DEBUG| Coordinate D(numerical) D(analytical) Difference Error [%]"
489 IF (do_dof_dipole(k))
THEN
490 dd = dipole_moment(k) - dipole_numer(k)
491 IF (abs(dipole_moment(k)) > eps_no_error_check)
THEN
492 derr = 100._dp*dd/dipole_moment(k)
493 WRITE (unit=iw, fmt=
"(T2,A,T13,A1,T21,F16.8,T38,F16.8,T56,G12.3,T72,F9.3)") &
494 "DEBUG|", achar(119 + k), dipole_numer(k), dipole_moment(k), dd, derr
497 WRITE (unit=iw, fmt=
"(T2,A,T13,A1,T21,F16.8,T38,F16.8,T56,G12.3)") &
498 "DEBUG|", achar(119 + k), dipole_numer(k), dipole_moment(k), dd
502 WRITE (unit=iw, fmt=
"(T2,A,T13,A1,T21,A16,T38,F16.8)") &
503 "DEBUG|", achar(119 + k),
" skipped", dipole_moment(k)
506 WRITE (unit=iw, fmt=
"((T2,A))") &
507 "DEBUG|========================================================================="
508 WRITE (unit=iw, fmt=
"(T2,A,T61,E20.12)")
'DIPOLE : CheckSum =', sum(dipole_moment)
509 IF (any(abs(err(1:3)) > maxerr))
THEN
510 message =
"A mismatch between analytical and numerical dipoles "// &
511 "has been detected. Check the implementation of the "// &
512 "analytical dipole calculation"
513 IF (stop_on_mismatch)
THEN
522 CALL cp_warn(__location__,
"Debug of dipole moments only for Quickstep code available")
526 IF (debug_polar)
THEN
528 CALL get_qs_env(force_env%qs_env, dft_control=dft_control)
529 poldir = [0.0_dp, 0.0_dp, 1.0_dp]
531 CALL set_efield(dft_control, amplitude, poldir)
533 CALL get_qs_env(force_env%qs_env, results=results)
534 description =
"[POLAR]"
536 CALL get_results(results, description=description, values=pvals)
537 polar_analytic(1:3, 1:3) = reshape(pvals(1:9), [3, 3])
539 CALL cp_warn(__location__,
"Debug of polarizabilities needs PROPERTIES/LINRES/POLAR section enabled")
542 description =
"[DIPOLE]"
544 CALL cp_warn(__location__,
"Debug of polarizabilities need DFT/PRINT/MOMENTS section enabled")
552 poldir = -1.0_dp*poldir
553 CALL set_efield(dft_control, amplitude, poldir)
555 CALL get_results(results, description=description, values=dipn(1:3, j))
557 polar_numeric(1:3, k) = 0.5_dp*(dipn(1:3, 2) - dipn(1:3, 1))/de
560 WRITE (unit=iw, fmt=
"(/,(T2,A))") &
561 "DEBUG|========================= POLARIZABILITY ================================", &
562 "DEBUG| Coordinates P(numerical) P(analytical) Difference Error [%]"
565 dd = polar_analytic(k, j) - polar_numeric(k, j)
566 IF (abs(polar_analytic(k, j)) > eps_no_error_check)
THEN
567 derr = 100._dp*dd/polar_analytic(k, j)
568 WRITE (unit=iw, fmt=
"(T2,A,T12,A1,A1,T21,F16.8,T38,F16.8,T56,G12.3,T72,F9.3)") &
569 "DEBUG|", achar(119 + k), achar(119 + j), polar_numeric(k, j), polar_analytic(k, j), dd, derr
571 WRITE (unit=iw, fmt=
"(T2,A,T12,A1,A1,T21,F16.8,T38,F16.8,T56,G12.3)") &
572 "DEBUG|", achar(119 + k), achar(119 + j), polar_numeric(k, j), polar_analytic(k, j), dd
576 WRITE (unit=iw, fmt=
"((T2,A))") &
577 "DEBUG|========================================================================="
578 WRITE (unit=iw, fmt=
"(T2,A,T61,E20.12)")
' POLAR : CheckSum =', sum(polar_analytic)
581 CALL cp_warn(__location__,
"Debug of polarizabilities only for Quickstep code available")
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, mimic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, rhoz_cneo_set, ecoul_1c, rho0_s_rs, rho0_s_gs, rhoz_cneo_s_rs, rhoz_cneo_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, harris_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, wanniercentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Get the QUICKSTEP environment.