17 USE mctc_env,
ONLY: error_type
18 USE mctc_io,
ONLY: structure_type, new
19 USE mctc_io_symbols,
ONLY: symbol_to_number
20 USE tblite_adjlist,
ONLY: adjacency_list, new_adjacency_list
21 USE tblite_basis_type,
ONLY: get_cutoff
22 USE tblite_container,
ONLY: container_cache
23 USE tblite_container_type,
ONLY: container_type
24 USE tblite_cutoff,
ONLY: get_lattice_points
25 USE tblite_data_spin,
ONLY: get_spin_constant
26 USE tblite_integral_multipole,
ONLY: multipole_cgto, multipole_grad_cgto, maxl, msao
27 USE tblite_integral_type,
ONLY: integral_type, new_integral
28 USE tblite_scf,
ONLY: get_mixer_dimension
29 USE tblite_scf_info,
ONLY: scf_info, atom_resolved, shell_resolved, &
30 orbital_resolved, not_used
31 USE tblite_scf_potential,
ONLY: potential_type, new_potential, add_pot_to_h1
32 USE tblite_spin,
ONLY: spin_polarization, new_spin_polarization
33 USE tblite_wavefunction_type,
ONLY: wavefunction_type, new_wavefunction
34 USE tblite_xtb_calculator,
ONLY: xtb_calculator, new_xtb_calculator
35 USE tblite_xtb_gfn1,
ONLY: new_gfn1_calculator
36 USE tblite_xtb_gfn2,
ONLY: new_gfn2_calculator
37 USE tblite_xtb_h0,
ONLY: get_selfenergy, get_hamiltonian, get_occupation, &
38 get_hamiltonian_gradient, tb_hamiltonian
39 USE tblite_xtb_ipea1,
ONLY: new_ipea1_calculator
117#include "./base/base_uses.f90"
122 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'tblite_interface'
124 INTEGER,
PARAMETER :: dip_n = 3
125 INTEGER,
PARAMETER :: quad_n = 6
126 REAL(KIND=
dp),
PARAMETER :: same_atom = 0.00001_dp
127 REAL(KIND=
dp),
PARAMETER :: tblite_scc_pconv = 2.0e-5_dp
147 PURE FUNCTION tb_spin_project(values, ispin)
RESULT(value)
149 REAL(KIND=
dp),
DIMENSION(:),
INTENT(IN) :: values
150 INTEGER,
INTENT(IN) :: ispin
151 REAL(KIND=
dp) ::
value
154 IF (
SIZE(values) > 1)
THEN
157 value = values(1) + values(2)
159 value = values(1) - values(2)
165 END FUNCTION tb_spin_project
172 SUBROUTINE tb_store_density_ref(tb, matrix_p)
174 TYPE(tblite_type),
POINTER :: tb
175 TYPE(dbcsr_p_type),
DIMENSION(:, :),
POINTER :: matrix_p
177 INTEGER :: img, ispin, nimg, nspin
179 nspin =
SIZE(matrix_p, 1)
180 nimg =
SIZE(matrix_p, 2)
181 IF (
ASSOCIATED(tb%rho_ao_kp_ref))
THEN
182 IF (
SIZE(tb%rho_ao_kp_ref, 1) /= nspin .OR.
SIZE(tb%rho_ao_kp_ref, 2) /= nimg) &
185 IF (.NOT.
ASSOCIATED(tb%rho_ao_kp_ref))
THEN
189 ALLOCATE (tb%rho_ao_kp_ref(ispin, img)%matrix)
195 CALL dbcsr_copy(tb%rho_ao_kp_ref(ispin, img)%matrix, matrix_p(ispin, img)%matrix)
199 END SUBROUTINE tb_store_density_ref
214 CHARACTER(LEN=*),
PARAMETER :: routinen =
'tblite_init_geometry'
218 TYPE(
qs_kind_type),
DIMENSION(:),
POINTER :: qs_kind_set
219 INTEGER :: iatom, natom
220 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: xyz
221 INTEGER :: handle, ikind
222 INTEGER,
DIMENSION(3) :: periodic
223 LOGICAL,
DIMENSION(3) :: lperiod
225 CALL timeset(routinen, handle)
228 CALL get_qs_env(qs_env=qs_env, particle_set=particle_set, cell=cell, qs_kind_set=qs_kind_set)
231 natom =
SIZE(particle_set)
232 ALLOCATE (xyz(3, natom))
234 ALLOCATE (tb%el_num(natom))
237 xyz(:, iatom) = particle_set(iatom)%r(:)
238 CALL get_atomic_kind(particle_set(iatom)%atomic_kind, kind_number=ikind)
239 CALL get_qs_kind(qs_kind_set(ikind), zatom=tb%el_num(iatom))
240 IF (tb%el_num(iatom) < 1 .OR. tb%el_num(iatom) > 85)
THEN
241 cpabort(
"only elements 1-85 are supported by tblite")
246 CALL get_cell(cell=cell, periodic=periodic)
247 lperiod(1) = periodic(1) == 1
248 lperiod(2) = periodic(2) == 1
249 lperiod(3) = periodic(3) == 1
252 CALL new(tb%mol, tb%el_num, xyz, lattice=cell%hmat, periodic=lperiod)
256 CALL timestop(handle)
261 cpabort(
"Built without TBLITE")
271 SUBROUTINE tb_update_geometry(qs_env, tb)
278 CHARACTER(LEN=*),
PARAMETER :: routinen =
'tblite_update_geometry'
282 INTEGER :: iatom, natom
283 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: xyz
286 CALL timeset(routinen, handle)
290 CALL get_qs_env(qs_env=qs_env, particle_set=particle_set, cell=cell)
293 natom =
SIZE(particle_set)
294 ALLOCATE (xyz(3, natom))
296 xyz(:, iatom) = particle_set(iatom)%r(:)
298 tb%mol%xyz(:, :) = xyz
299 tb%mol%lattice(:, :) = cell%hmat
303 CALL timestop(handle)
308 cpabort(
"Built without TBLITE")
311 END SUBROUTINE tb_update_geometry
327 TYPE(scf_info) :: info
329 nspin = dft_control%nspins
330 IF (nspin /= 1 .AND. nspin /= 2) cpabort(
"tblite supports only one or two spin channels")
332 tb%mol%charge = dft_control%charge
333 tb%mol%uhf = max(0, dft_control%multiplicity - 1)
334 IF (nspin == 2)
CALL tb_add_spin_polarization(tb)
336 info = tb%calc%variable_info()
337 IF (info%charge > shell_resolved) cpabort(
"tblite: no support for orbital resolved charge")
338 IF (info%dipole > atom_resolved) cpabort(
"tblite: no support for shell resolved dipole moment")
339 IF (info%quadrupole > atom_resolved) &
340 cpabort(
"tblite: no support shell resolved quadrupole moment")
342 CALL new_wavefunction(tb%wfn, tb%mol%nat, tb%calc%bas%nsh, tb%calc%bas%nao, nspin, 0.0_dp)
343 CALL get_occupation(tb%mol, tb%calc%bas, tb%calc%h0, tb%wfn%nocc, tb%wfn%n0at, tb%wfn%n0sh)
344 CALL tb_reset_mixer(tb)
346 CALL new_potential(tb%pot, tb%mol, tb%calc%bas, tb%wfn%nspin)
349 ALLOCATE (tb%e_hal(tb%mol%nat), tb%e_rep(tb%mol%nat), tb%e_disp(tb%mol%nat))
350 ALLOCATE (tb%e_scd(tb%mol%nat), tb%e_es(tb%mol%nat), tb%e_int(tb%mol%nat))
351 ALLOCATE (tb%selfenergy(tb%calc%bas%nsh))
352 IF (
ALLOCATED(tb%calc%ncoord))
ALLOCATE (tb%cn(tb%mol%nat))
356 mark_used(dft_control)
357 cpabort(
"Built without TBLITE")
367 SUBROUTINE tb_add_spin_polarization(tb)
371 CLASS(container_type),
ALLOCATABLE :: cont
372 TYPE(spin_polarization),
ALLOCATABLE :: spin
373 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :, :) :: wll
376 CALL tb_get_spin_constants(tb, wll)
377 CALL new_spin_polarization(spin, tb%mol, wll, tb%calc%bas%nsh_id)
378 CALL move_alloc(spin, cont)
379 CALL tb%calc%push_back(cont)
381 END SUBROUTINE tb_add_spin_polarization
388 SUBROUTINE tb_get_spin_constants(tb, wll)
391 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :, :), &
394 INTEGER :: il, ish, izp, jl, jsh
396 ALLOCATE (wll(tb%calc%bas%nsh, tb%calc%bas%nsh, tb%mol%nid))
398 DO izp = 1, tb%mol%nid
399 DO ish = 1, tb%calc%bas%nsh_id(izp)
400 il = tb%calc%bas%cgto(ish, izp)%ang
401 DO jsh = 1, tb%calc%bas%nsh_id(izp)
402 jl = tb%calc%bas%cgto(jsh, izp)%ang
403 wll(jsh, ish, izp) = get_spin_constant(jl, il, tb%mol%num(izp))
408 END SUBROUTINE tb_get_spin_constants
415 SUBROUTINE tb_reset_mixer(tb)
421 TYPE(scf_info) :: info
423 info = tb%calc%variable_info()
424 IF (
ALLOCATED(tb%mixer))
DEALLOCATE (tb%mixer)
425 CALL new_cp2k_tblite_mixer(tb%mixer, tb%mixer_memory, &
426 tb%wfn%nspin*get_mixer_dimension(tb%mol, tb%calc%bas, info), &
427 tb%mixer_damping, tb%mixer_omega0, tb%mixer_min_weight, &
428 tb%mixer_max_weight, tb%mixer_weight_factor)
432 cpabort(
"Built without TBLITE")
435 END SUBROUTINE tb_reset_mixer
449 SUBROUTINE tb_configure_mixer(tb, iterations, memory, damping, omega0, min_weight, max_weight, &
450 weight_factor, solver)
453 INTEGER,
INTENT(IN) :: iterations, memory, solver
454 REAL(kind=
dp),
INTENT(IN) :: damping, max_weight, min_weight, omega0, &
459 IF (iterations < 1) cpabort(
"tblite SCC mixer ITERATIONS must be positive")
460 IF (memory < 1) cpabort(
"tblite SCC mixer MEMORY must be positive")
461 IF (damping <= 0.0_dp) cpabort(
"tblite SCC mixer damping must be positive")
462 IF (omega0 <= 0.0_dp) cpabort(
"tblite SCC mixer OMEGA0 must be positive")
463 IF (min_weight <= 0.0_dp) cpabort(
"tblite SCC mixer MIN_WEIGHT must be positive")
464 IF (max_weight <= 0.0_dp) cpabort(
"tblite SCC mixer MAX_WEIGHT must be positive")
465 IF (max_weight < min_weight) &
466 cpabort(
"tblite SCC mixer MAX_WEIGHT must not be smaller than MIN_WEIGHT")
467 IF (weight_factor <= 0.0_dp) cpabort(
"tblite SCC mixer WEIGHT_FACTOR must be positive")
471 cpabort(
"Unknown tblite SCC mixer SOLVER")
474 tb%calc%max_iter = iterations
475 tb%mixer_memory = memory
476 tb%mixer_solver = solver
477 tb%mixer_damping = damping
478 tb%calc%mixer_damping = damping
479 tb%mixer_omega0 = omega0
480 tb%mixer_min_weight = min_weight
481 tb%mixer_max_weight = max_weight
482 tb%mixer_weight_factor = weight_factor
486 mark_used(iterations)
490 mark_used(min_weight)
491 mark_used(max_weight)
492 mark_used(weight_factor)
494 cpabort(
"Built without TBLITE")
497 END SUBROUTINE tb_configure_mixer
507 LOGICAL :: use_native_mixer
509 use_native_mixer = .false.
510 IF (.NOT.
ASSOCIATED(dft_control))
RETURN
511 IF (dft_control%qs_control%do_ls_scf)
RETURN
513 SELECT CASE (dft_control%qs_control%xtb_control%tblite_scc_mixer)
515 use_native_mixer = .true.
517 use_native_mixer = .true.
519 use_native_mixer = .false.
521 cpabort(
"Unknown tblite SCC mixer")
537 REAL(kind=
dp),
INTENT(IN) :: eps_scf
538 REAL(kind=
dp) :: mixer_error
541 REAL(kind=
dp) :: raw_error
547 IF (.NOT.
ASSOCIATED(tb))
RETURN
549 IF (
ALLOCATED(tb%mixer))
THEN
550 raw_error = real(tb%mixer%get_error(), kind=
dp)
551 IF (eps_scf > 0.0_dp)
THEN
552 mixer_error = eps_scf*raw_error/ &
553 (tblite_scc_pconv*dft_control%qs_control%xtb_control%tblite_accuracy)
555 mixer_error = raw_error
559 mark_used(dft_control)
577 REAL(kind=
dp),
INTENT(IN) :: accuracy
578 CHARACTER(LEN=*),
INTENT(IN) :: param_file
582 TYPE(error_type),
ALLOCATABLE :: error
584 IF (
ALLOCATED(tb%param))
DEALLOCATE (tb%param)
585 IF (len_trim(param_file) > 0)
THEN
587 CALL tb%param%load(trim(param_file), error)
588 IF (
ALLOCATED(error)) cpabort(
"Could not load tblite PARAM file: "//trim(param_file))
589 CALL new_xtb_calculator(tb%calc, tb%mol, tb%param, error)
593 cpabort(
"Unknown xtb type")
595 CALL new_gfn1_calculator(tb%calc, tb%mol, error)
597 CALL new_gfn2_calculator(tb%calc, tb%mol, error)
599 CALL new_ipea1_calculator(tb%calc, tb%mol, error)
602 IF (
ALLOCATED(error)) cpabort(
"tblite calculator setup failed")
604 tb%accuracy = accuracy
610 mark_used(param_file)
611 cpabort(
"Built without TBLITE")
622 SUBROUTINE tb_init_ham(qs_env, tb, para_env)
630 TYPE(container_cache) :: hcache, rcache
636 IF (
ALLOCATED(tb%grad))
THEN
638 CALL tb_zero_force(qs_env)
642 IF (
ALLOCATED(tb%calc%halogen))
THEN
643 CALL tb%calc%halogen%update(tb%mol, hcache)
644 IF (
ALLOCATED(tb%grad))
THEN
646 CALL tb%calc%halogen%get_engrad(tb%mol, hcache, tb%e_hal, &
648 CALL tb_dump_sigma_component(
"after_halogen", tb%sigma, para_env)
649 CALL tb_grad2force(qs_env, tb, para_env, 0)
651 CALL tb%calc%halogen%get_engrad(tb%mol, hcache, tb%e_hal)
655 IF (
ALLOCATED(tb%calc%repulsion))
THEN
656 CALL tb%calc%repulsion%update(tb%mol, rcache)
657 IF (
ALLOCATED(tb%grad))
THEN
659 CALL tb%calc%repulsion%get_engrad(tb%mol, rcache, tb%e_rep, &
661 CALL tb_dump_sigma_component(
"after_repulsion", tb%sigma, para_env)
662 CALL tb_grad2force(qs_env, tb, para_env, 1)
664 CALL tb%calc%repulsion%get_engrad(tb%mol, rcache, tb%e_rep)
668 IF (
ALLOCATED(tb%calc%dispersion))
THEN
669 CALL tb%calc%dispersion%update(tb%mol, tb%dcache)
670 IF (
ALLOCATED(tb%grad))
THEN
672 CALL tb%calc%dispersion%get_engrad(tb%mol, tb%dcache, tb%e_disp, &
674 CALL tb_dump_sigma_component(
"after_dispersion_static", tb%sigma, para_env)
675 CALL tb_grad2force(qs_env, tb, para_env, 2)
677 CALL tb%calc%dispersion%get_engrad(tb%mol, tb%dcache, tb%e_disp)
681 IF (
ALLOCATED(tb%calc%interactions))
THEN
682 CALL tb%calc%interactions%update(tb%mol, tb%icache)
685 CALL new_potential(tb%pot, tb%mol, tb%calc%bas, tb%wfn%nspin)
686 IF (
ALLOCATED(tb%calc%coulomb))
THEN
687 CALL tb%calc%coulomb%update(tb%mol, tb%cache)
690 IF (
ALLOCATED(tb%grad))
THEN
691 IF (
ALLOCATED(tb%calc%ncoord))
THEN
692 CALL tb%calc%ncoord%get_cn(tb%mol, tb%cn, tb%dcndr, tb%dcndL)
694 CALL get_selfenergy(tb%calc%h0, tb%mol%id, tb%calc%bas%ish_at, &
695 & tb%calc%bas%nsh_id, cn=tb%cn, selfenergy=tb%selfenergy, dsedcn=tb%dsedcn)
697 IF (
ALLOCATED(tb%calc%ncoord))
THEN
698 CALL tb%calc%ncoord%get_cn(tb%mol, tb%cn)
700 CALL get_selfenergy(tb%calc%h0, tb%mol%id, tb%calc%bas%ish_at, &
701 & tb%calc%bas%nsh_id, cn=tb%cn, selfenergy=tb%selfenergy, dsedcn=tb%dsedcn)
708 cpabort(
"Built without TBLITE")
711 END SUBROUTINE tb_init_ham
731 NULLIFY (scf_section, logger)
737 energy%repulsive = sum(tb%e_rep)
738 energy%el_stat = sum(tb%e_es)
739 energy%dispersion = sum(tb%e_disp)
740 energy%dispersion_sc = sum(tb%e_scd)
741 energy%xtb_xb_inter = sum(tb%e_hal)
742 energy%xtb_xb_inter = energy%xtb_xb_inter + sum(tb%e_int)
744 energy%total = energy%core + energy%repulsive + energy%el_stat + energy%dispersion &
745 + energy%dispersion_sc + energy%xtb_xb_inter + energy%kTS &
746 + energy%efield + energy%qmmm_el
751 WRITE (unit=iounit, fmt=
"(/,(T9,A,T60,F20.10))") &
752 "Repulsive pair potential energy: ", energy%repulsive, &
753 "Zeroth order Hamiltonian energy: ", energy%core, &
754 "Electrostatic energy: ", energy%el_stat, &
755 "Self-consistent dispersion energy: ", energy%dispersion_sc, &
756 "Non-self consistent dispersion energy: ", energy%dispersion
757 WRITE (unit=iounit, fmt=
"(T9,A,T60,F20.10)") &
758 "Correction for halogen bonding: ", energy%xtb_xb_inter
759 IF (abs(energy%efield) > 1.e-12_dp)
THEN
760 WRITE (unit=iounit, fmt=
"(T9,A,T60,F20.10)") &
761 "Electric field interaction energy: ", energy%efield
763 IF (qs_env%qmmm)
THEN
764 WRITE (unit=iounit, fmt=
"(T9,A,T60,F20.10)") &
765 "QM/MM Electrostatic energy: ", energy%qmmm_el
769 "PRINT%DETAILED_ENERGY")
775 cpabort(
"Built without TBLITE")
792 CHARACTER(len=2),
INTENT(IN) :: element_symbol
794 INTEGER,
DIMENSION(5),
INTENT(out) :: occ
798 REAL(kind=
dp) :: docc
799 CHARACTER(LEN=default_string_length) :: sng
800 INTEGER :: ang, i_type, id_atom, ind_ao, ipgf, ish, &
801 ishell, ityp, maxl, mprim, natorb, &
808 CALL symbol_to_number(i_type, element_symbol)
809 DO id_atom = 1, tb%mol%nat
810 IF (i_type == tb%el_num(id_atom))
EXIT
813 param%symbol = element_symbol
814 param%defined = .true.
815 ityp = tb%mol%id(id_atom)
818 nset = tb%calc%bas%nsh_id(ityp)
822 mprim = max(mprim, tb%calc%bas%cgto(ishell, ityp)%nprim)
829 gto_basis_set%name = element_symbol//
"_STO-"//trim(sng)//
"G"
830 gto_basis_set%nset = nset
834 CALL reallocate(gto_basis_set%nshell, 1, nset)
835 CALL reallocate(gto_basis_set%n, 1, 1, 1, nset)
836 CALL reallocate(gto_basis_set%l, 1, 1, 1, nset)
837 CALL reallocate(gto_basis_set%zet, 1, mprim, 1, nset)
838 CALL reallocate(gto_basis_set%gcc, 1, mprim, 1, 1, 1, nset)
843 ang = tb%calc%bas%cgto(ishell, ityp)%ang
844 natorb = natorb + (2*ang + 1)
845 param%lval(ishell) = ang
846 maxl = max(ang, maxl)
847 gto_basis_set%lmax(ishell) = ang
848 gto_basis_set%lmin(ishell) = ang
849 gto_basis_set%npgf(ishell) = tb%calc%bas%cgto(ishell, ityp)%nprim
850 gto_basis_set%nshell(ishell) = nshell
851 gto_basis_set%n(1, ishell) = ang + 1
852 gto_basis_set%l(1, ishell) = ang
853 DO ipgf = 1, gto_basis_set%npgf(ishell)
854 gto_basis_set%gcc(ipgf, 1, ishell) = tb%calc%bas%cgto(ishell, ityp)%coeff(ipgf)
855 gto_basis_set%zet(ipgf, ishell) = tb%calc%bas%cgto(ishell, ityp)%alpha(ipgf)
857 DO ipgf = 1, (2*ang + 1)
859 param%lao(ind_ao) = ang
860 param%nao(ind_ao) = ishell
868 param%rcut = get_cutoff(tb%calc%bas, tb%accuracy)
869 param%natorb = natorb
875 IF (tb%calc%bas%nsh_at(id_atom) > 5) cpabort(
"too many shells in tblite")
876 DO ish = 1, tb%calc%bas%nsh_at(id_atom)
877 occ(ish) = nint(tb%calc%h0%refocc(ish, ityp) + docc)
878 docc = docc + tb%calc%h0%refocc(ish, ityp) - real(occ(ish))
879 param%occupation(ish) = occ(ish)
881 IF (abs(docc) > 0.1_dp) cpabort(
"Getting occupation numbers from tblite fails")
882 param%zeff = sum(occ)
885 gto_basis_set%norm_type = 3
890 mark_used(gto_basis_set)
891 mark_used(element_symbol)
893 cpabort(
"Built without TBLITE")
906 LOGICAL,
INTENT(IN) :: calculate_forces
910 CHARACTER(LEN=*),
PARAMETER :: routinen =
'build_tblite_matrices'
912 INTEGER :: handle, maxder, nderivatives, nimg, img, nkind, i, &
913 ic, iw, iatom, jatom, ikind, jkind, iset, jset, n1, n2, icol, &
914 irow, ia, ib, sgfa, sgfb, ldsab, nseta, nsetb, &
915 natorb_a, natorb_b, sgfa0, slot
916 LOGICAL :: found, norml1, norml2, use_arnoldi
917 REAL(kind=
dp) :: dr, rr
918 INTEGER,
DIMENSION(3) :: cell
919 REAL(kind=
dp) :: hij, shpoly
920 REAL(kind=
dp),
DIMENSION(2) :: condnum
921 REAL(kind=
dp),
DIMENSION(3) :: rij
922#if defined(__TBLITE_DEBUG_DIAGNOSTICS)
923 INTEGER :: debug_status
924 CHARACTER(LEN=32) :: debug_value
926 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: atom_of_kind
927 INTEGER,
DIMENSION(:, :, :),
POINTER :: cell_to_index
928 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: owork
929 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :, :) :: oint, sint, hint
930 INTEGER,
DIMENSION(:),
POINTER :: la_max, la_min, lb_max, lb_min
931 INTEGER,
DIMENSION(:),
POINTER :: npgfa, npgfb, nsgfa, nsgfb
932 INTEGER,
DIMENSION(:, :),
POINTER :: first_sgfa, first_sgfb
933 REAL(kind=
dp),
DIMENSION(:),
POINTER :: set_radius_a, set_radius_b
934 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: rpgfa, rpgfb, zeta, zetb, scon_a, scon_b
935 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: sblock, fblock
939 INTEGER,
PARAMETER :: nlock = 501
945 TYPE(
dbcsr_p_type),
DIMENSION(:, :),
POINTER :: matrix_h, matrix_s, matrix_p, matrix_w
956 TYPE(
qs_kind_type),
DIMENSION(:),
POINTER :: qs_kind_set
959 TYPE(tb_hamiltonian),
POINTER :: h0
962 CALL timeset(routinen, handle)
964 NULLIFY (ks_env, energy, atomic_kind_set, qs_kind_set)
965 NULLIFY (matrix_h, matrix_s, atprop, dft_control)
966 NULLIFY (sab_orb, sab_kp, rho, tb, kpoints, cell_to_index)
969 ks_env=ks_env, para_env=para_env, &
971 atomic_kind_set=atomic_kind_set, &
972 qs_kind_set=qs_kind_set, &
973 matrix_h_kp=matrix_h, &
974 matrix_s_kp=matrix_s, &
976 dft_control=dft_control, &
979 rho=rho, tb_tblite=tb)
983 CALL tb_update_geometry(qs_env, tb)
985 nkind =
SIZE(atomic_kind_set)
987 IF (calculate_forces)
THEN
989 IF (
ALLOCATED(tb%grad))
DEALLOCATE (tb%grad)
990 ALLOCATE (tb%grad(3, tb%mol%nat))
991 IF (
ALLOCATED(tb%dsedcn))
DEALLOCATE (tb%dsedcn)
992 ALLOCATE (tb%dsedcn(tb%calc%bas%nsh))
993 IF (
ALLOCATED(tb%calc%ncoord))
THEN
994 IF (
ALLOCATED(tb%dcndr))
DEALLOCATE (tb%dcndr)
995 ALLOCATE (tb%dcndr(3, tb%mol%nat, tb%mol%nat))
996 IF (
ALLOCATED(tb%dcndL))
DEALLOCATE (tb%dcndL)
997 ALLOCATE (tb%dcndL(3, 3, tb%mol%nat))
1000 IF (
ALLOCATED(tb%grad))
DEALLOCATE (tb%grad)
1001 IF (
ALLOCATED(tb%dcndr))
DEALLOCATE (tb%dcndr)
1002 IF (
ALLOCATED(tb%dcndL))
DEALLOCATE (tb%dcndL)
1004 maxder =
ncoset(nderivatives)
1005 nimg = dft_control%nimages
1007 IF (.NOT.
ASSOCIATED(sab_kp)) cpabort(
"Missing k-point neighbor list for tblite")
1009 CALL get_ks_env(ks_env=ks_env, kpoints=kpoints)
1014 CALL tb_init_ham(qs_env, tb, para_env)
1020 IF (calculate_forces)
THEN
1021 NULLIFY (force, matrix_w, virial)
1023 matrix_w_kp=matrix_w, &
1024 virial=virial, force=force)
1026 IF (
SIZE(matrix_p, 1) == 2)
THEN
1028 CALL dbcsr_add(matrix_p(1, img)%matrix, matrix_p(2, img)%matrix, &
1029 alpha_scalar=1.0_dp, beta_scalar=1.0_dp)
1030 CALL dbcsr_add(matrix_w(1, img)%matrix, matrix_w(2, img)%matrix, &
1031 alpha_scalar=1.0_dp, beta_scalar=1.0_dp)
1034 tb%use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
1040 ALLOCATE (basis_set_list(nkind))
1045 CALL create_sab_matrix(ks_env, matrix_s,
"OVERLAP MATRIX", basis_set_list, basis_set_list, &
1047 CALL set_ks_env(ks_env, matrix_s_kp=matrix_s)
1052 ALLOCATE (matrix_h(1, img)%matrix)
1053 CALL dbcsr_create(matrix_h(1, img)%matrix, template=matrix_s(1, img)%matrix, &
1054 name=
"HAMILTONIAN MATRIX")
1057 CALL set_ks_env(ks_env, matrix_h_kp=matrix_h)
1080 ALLOCATE (oint(ldsab, ldsab, maxder), owork(ldsab, ldsab))
1083 DO slot = 1, sab_orb(1)%nl_size
1085 ikind = sab_orb(1)%nlist_task(slot)%ikind
1086 jkind = sab_orb(1)%nlist_task(slot)%jkind
1087 iatom = sab_orb(1)%nlist_task(slot)%iatom
1088 jatom = sab_orb(1)%nlist_task(slot)%jatom
1089 cell(:) = sab_orb(1)%nlist_task(slot)%cell(:)
1090 rij(1:3) = sab_orb(1)%nlist_task(slot)%r(1:3)
1093 icol = max(iatom, jatom)
1094 irow = min(iatom, jatom)
1095 IF (iatom < jatom)
THEN
1107 ic = cell_to_index(cell(1), cell(2), cell(3))
1113 row=irow, col=icol, block=sblock, found=found)
1117 row=irow, col=icol, block=fblock, found=found)
1122 basis_set_a => basis_set_list(ikind)%gto_basis_set
1123 IF (.NOT.
ASSOCIATED(basis_set_a)) cycle
1124 basis_set_b => basis_set_list(jkind)%gto_basis_set
1125 IF (.NOT.
ASSOCIATED(basis_set_b)) cycle
1127 first_sgfa => basis_set_a%first_sgf
1128 la_max => basis_set_a%lmax
1129 la_min => basis_set_a%lmin
1130 npgfa => basis_set_a%npgf
1131 nseta = basis_set_a%nset
1132 nsgfa => basis_set_a%nsgf_set
1133 rpgfa => basis_set_a%pgf_radius
1134 set_radius_a => basis_set_a%set_radius
1135 scon_a => basis_set_a%scon
1136 zeta => basis_set_a%zet
1138 first_sgfb => basis_set_b%first_sgf
1139 lb_max => basis_set_b%lmax
1140 lb_min => basis_set_b%lmin
1141 npgfb => basis_set_b%npgf
1142 nsetb = basis_set_b%nset
1143 nsgfb => basis_set_b%nsgf_set
1144 rpgfb => basis_set_b%pgf_radius
1145 set_radius_b => basis_set_b%set_radius
1146 scon_b => basis_set_b%scon
1147 zetb => basis_set_b%zet
1151 natorb_a = natorb_a + (2*basis_set_a%l(1, iset) + 1)
1155 natorb_b = natorb_b + (2*basis_set_b%l(1, iset) + 1)
1157 ALLOCATE (sint(natorb_a, natorb_b, maxder))
1159 ALLOCATE (hint(natorb_a, natorb_b, maxder))
1164 n1 = npgfa(iset)*(
ncoset(la_max(iset)) -
ncoset(la_min(iset) - 1))
1165 sgfa = first_sgfa(1, iset)
1167 IF (set_radius_a(iset) + set_radius_b(jset) < dr) cycle
1168 n2 = npgfb(jset)*(
ncoset(lb_max(jset)) -
ncoset(lb_min(jset) - 1))
1169 sgfb = first_sgfb(1, jset)
1170 IF (calculate_forces)
THEN
1171 CALL overlap_ab(la_max(iset), la_min(iset), npgfa(iset), rpgfa(:, iset), zeta(:, iset), &
1172 lb_max(jset), lb_min(jset), npgfb(jset), rpgfb(:, jset), zetb(:, jset), &
1173 rij, sab=oint(:, :, 1), dab=oint(:, :, 2:4))
1175 CALL overlap_ab(la_max(iset), la_min(iset), npgfa(iset), rpgfa(:, iset), zeta(:, iset), &
1176 lb_max(jset), lb_min(jset), npgfb(jset), rpgfb(:, jset), zetb(:, jset), &
1177 rij, sab=oint(:, :, 1))
1180 CALL contraction(oint(:, :, 1), owork, ca=scon_a(:, sgfa:), na=n1, ma=nsgfa(iset), &
1181 cb=scon_b(:, sgfb:), nb=n2, mb=nsgfb(jset), fscale=1.0_dp, trans=.false.)
1182 CALL block_add(
"IN", owork, nsgfa(iset), nsgfb(jset), sint(:, :, 1), sgfa, sgfb, trans=.false.)
1183 IF (calculate_forces)
THEN
1185 CALL contraction(oint(:, :, i), owork, ca=scon_a(:, sgfa:), na=n1, ma=nsgfa(iset), &
1186 cb=scon_b(:, sgfb:), nb=n2, mb=nsgfb(jset), fscale=1.0_dp, trans=.false.)
1187 CALL block_add(
"IN", owork, nsgfa(iset), nsgfb(jset), sint(:, :, i), sgfa, sgfb, trans=.false.)
1198 IF (icol <= irow)
THEN
1199 sblock(:, :) = sblock(:, :) + sint(:, :, 1)
1201 sblock(:, :) = sblock(:, :) + transpose(sint(:, :, 1))
1206 IF (icol == irow .AND. dr < same_atom)
THEN
1208 n1 = tb%calc%bas%ish_at(icol)
1210 sgfa = first_sgfa(1, iset)
1211 hij = tb%selfenergy(n1 + iset)
1212 DO ia = sgfa, sgfa + nsgfa(iset) - 1
1213 hint(ia, ia, 1) = hij
1218 rr = sqrt(dr/(h0%rad(jkind) + h0%rad(ikind)))
1219 n1 = tb%calc%bas%ish_at(icol)
1222 sgfa = first_sgfa(1, iset)
1223 sgfa0 = sgfa0 + tb%calc%bas%cgto(iset, tb%mol%id(icol))%nprim
1224 n2 = tb%calc%bas%ish_at(irow)
1226 sgfb = first_sgfb(1, jset)
1227 shpoly = (1.0_dp + h0%shpoly(iset, ikind)*rr) &
1228 *(1.0_dp + h0%shpoly(jset, jkind)*rr)
1229 hij = 0.5_dp*(tb%selfenergy(n1 + iset) + tb%selfenergy(n2 + jset)) &
1230 *h0%hscale(iset, jset, ikind, jkind)*shpoly
1231 DO ia = sgfa, sgfa + nsgfa(iset) - 1
1232 DO ib = sgfb, sgfb + nsgfb(jset) - 1
1233 hint(ia, ib, 1) = hij*sint(ia, ib, 1)
1242 IF (icol <= irow)
THEN
1243 fblock(:, :) = fblock(:, :) + hint(:, :, 1)
1245 fblock(:, :) = fblock(:, :) + transpose(hint(:, :, 1))
1249 DEALLOCATE (sint, hint)
1253 DEALLOCATE (oint, owork)
1265 DO i = 1,
SIZE(matrix_s, 1)
1268 DO i = 1,
SIZE(matrix_h, 1)
1274 IF (dft_control%qs_control%xtb_control%tblite_method ==
gfn2xtb) &
1280 IF (.NOT. calculate_forces)
THEN
1282 "DFT%PRINT%OVERLAP_CONDITION") /= 0)
THEN
1286 CALL section_vals_val_get(qs_env%input,
"DFT%PRINT%OVERLAP_CONDITION%DIAGONALIZATION", l_val=norml2)
1287 CALL section_vals_val_get(qs_env%input,
"DFT%PRINT%OVERLAP_CONDITION%ARNOLDI", l_val=use_arnoldi)
1288 CALL get_qs_env(qs_env=qs_env, blacs_env=blacs_env)
1289 CALL overlap_condnum(matrix_s, condnum, iw, norml1, norml2, use_arnoldi, blacs_env)
1293 DEALLOCATE (basis_set_list)
1295 CALL timestop(handle)
1299 mark_used(calculate_forces)
1300 cpabort(
"Built without TBLITE")
1318 LOGICAL,
INTENT(IN) :: calculate_forces
1319 LOGICAL,
INTENT(IN) :: use_rho
1321#if defined(__TBLITE)
1323 INTEGER :: iatom, ikind, is, ns, atom_a, ii, im
1324 INTEGER :: ispin, nspin
1325 INTEGER :: nimg, nkind, nsgf, natorb, na, n_mix_cols, mix_offset
1326 INTEGER :: n_atom, max_orb, max_shell
1327 INTEGER :: raw_state_status, raw_state_unit
1328 LOGICAL :: discard_mixed_output, do_combined_mixing, do_dipole, do_quadrupole, &
1329 native_sign_mixing, skip_charge_mixing, &
1330 reuse_native_input, skip_scf_dispersion, skip_scf_dispersion_energy, &
1331 seed_native_from_rho, &
1332 skip_scf_dispersion_gradient, skip_scf_dispersion_potential, &
1333 use_native_mixer, use_no_mixer
1334 REAL(kind=
dp) :: native_seed_charge, norm, moment_alpha, &
1336#if defined(__TBLITE_DEBUG_DIAGNOSTICS)
1337 INTEGER :: debug_status
1338 CHARACTER(LEN=32) :: debug_value
1340 CHARACTER(LEN=default_path_length) :: raw_state_file
1341 INTEGER,
DIMENSION(5) :: occ
1342 INTEGER,
DIMENSION(25) :: lao
1343 INTEGER,
DIMENSION(25) :: nao
1344 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: ch_atom, ch_shell, ch_ref, ch_orb, mix_vars
1345 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: aocg, ao_dip, ao_quad
1346 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :, :) :: aocg_spin, ao_dip_spin, ao_quad_spin, &
1347 ch_orb_spin, ch_shell_spin
1350 TYPE(
dbcsr_p_type),
DIMENSION(:, :),
POINTER :: matrix_s, matrix_p
1353 TYPE(error_type),
ALLOCATABLE :: error
1356 TYPE(
qs_kind_type),
DIMENSION(:),
POINTER :: qs_kind_set
1363 NULLIFY (particle_set, qs_kind_set, atomic_kind_set, scf_control)
1364 CALL get_qs_env(qs_env=qs_env, scf_env=scf_env, particle_set=particle_set, qs_kind_set=qs_kind_set, &
1365 atomic_kind_set=atomic_kind_set, matrix_s_kp=matrix_s, rho=rho, para_env=para_env, &
1366 scf_control=scf_control)
1370 do_quadrupole = .false.
1371 skip_scf_dispersion = .false.
1372#if defined(__TBLITE_DEBUG_DIAGNOSTICS)
1373 CALL get_environment_variable(
"CP2K_TBLITE_DEBUG_SKIP_SCF_DISPERSION", debug_value, status=debug_status)
1374 IF (debug_status == 0)
READ (debug_value, *, iostat=debug_status) skip_scf_dispersion
1376 skip_scf_dispersion_energy = skip_scf_dispersion
1377 skip_scf_dispersion_gradient = skip_scf_dispersion
1378 skip_scf_dispersion_potential = skip_scf_dispersion
1379#if defined(__TBLITE_DEBUG_DIAGNOSTICS)
1380 CALL get_environment_variable(
"CP2K_TBLITE_DEBUG_SKIP_SCF_DISPERSION_ENERGY", debug_value, status=debug_status)
1381 IF (debug_status == 0)
READ (debug_value, *, iostat=debug_status) skip_scf_dispersion_energy
1382 CALL get_environment_variable(
"CP2K_TBLITE_DEBUG_SKIP_SCF_DISPERSION_GRADIENT", debug_value, status=debug_status)
1383 IF (debug_status == 0)
READ (debug_value, *, iostat=debug_status) skip_scf_dispersion_gradient
1384 CALL get_environment_variable(
"CP2K_TBLITE_DEBUG_SKIP_SCF_DISPERSION_POTENTIAL", debug_value, status=debug_status)
1385 IF (debug_status == 0)
READ (debug_value, *, iostat=debug_status) skip_scf_dispersion_potential
1387 IF (dft_control%qs_control%do_ls_scf .OR. scf_control%use_ot)
THEN
1388 use_native_mixer = .false.
1389 use_no_mixer = .true.
1391 SELECT CASE (dft_control%qs_control%xtb_control%tblite_scc_mixer)
1394 use_no_mixer = .false.
1396 use_native_mixer = .true.
1397 use_no_mixer = .false.
1399 use_native_mixer = .false.
1400 use_no_mixer = .false.
1402 use_native_mixer = .false.
1403 use_no_mixer = .true.
1405 cpabort(
"Unknown tblite SCC mixer")
1408 IF (use_native_mixer .AND. use_rho .AND. (.NOT. calculate_forces) .AND. &
1409 scf_env%iter_count > dft_control%qs_control%xtb_control%tblite_mixer_iterations)
THEN
1410 cpabort(
"tblite SCC mixer exceeded TBLITE_MIXER/ITERATIONS")
1412 IF (use_native_mixer .AND. use_rho .AND. (.NOT. calculate_forces) .AND. &
1413 scf_env%iter_count == 1)
THEN
1414 CALL tb_configure_mixer(tb, dft_control%qs_control%xtb_control%tblite_mixer_iterations, &
1415 dft_control%qs_control%xtb_control%tblite_mixer_memory, &
1416 dft_control%qs_control%xtb_control%tblite_mixer_damping, &
1417 dft_control%qs_control%xtb_control%tblite_mixer_omega0, &
1418 dft_control%qs_control%xtb_control%tblite_mixer_min_weight, &
1419 dft_control%qs_control%xtb_control%tblite_mixer_max_weight, &
1420 dft_control%qs_control%xtb_control%tblite_mixer_weight_factor, &
1421 dft_control%qs_control%xtb_control%tblite_mixer_solver)
1422 CALL tb_reset_mixer(tb)
1424 nspin = dft_control%nspins
1425 IF (nspin /= tb%wfn%nspin) cpabort(
"CP2K/tblite spin channel mismatch")
1430 ELSEIF (calculate_forces .AND. nspin > 1)
THEN
1431 IF (.NOT.
ASSOCIATED(tb%rho_ao_kp_ref)) &
1432 cpabort(
"Missing converged tblite density for UKS/LSD forces")
1433 matrix_p => tb%rho_ao_kp_ref
1435 matrix_p => scf_env%p_mix_new
1437 IF (nspin > 1 .AND. (.NOT. calculate_forces))
CALL tb_store_density_ref(tb, matrix_p)
1438 IF (
ASSOCIATED(tb%dipbra)) do_dipole = .true.
1439 IF (
ASSOCIATED(tb%quadbra)) do_quadrupole = .true.
1440 reuse_native_input = .false.
1441 IF (use_native_mixer .AND. scf_env%iter_count == 1)
THEN
1442 reuse_native_input = any(abs(tb%wfn%qsh) > 1.0e-14_dp)
1443 IF (do_dipole) reuse_native_input = reuse_native_input .OR. &
1444 any(abs(tb%wfn%dpat) > 1.0e-14_dp)
1445 IF (do_quadrupole) reuse_native_input = reuse_native_input .OR. &
1446 any(abs(tb%wfn%qpat) > 1.0e-14_dp)
1448 n_atom =
SIZE(particle_set)
1449 nkind =
SIZE(atomic_kind_set)
1450 nimg = dft_control%nimages
1452 ALLOCATE (aocg(nsgf, n_atom))
1453 ALLOCATE (aocg_spin(nsgf, n_atom, nspin))
1457 ALLOCATE (ao_dip(n_atom, dip_n))
1458 ALLOCATE (ao_dip_spin(n_atom, dip_n, nspin))
1459 ao_dip_spin = 0.0_dp
1461 IF (do_quadrupole)
THEN
1462 ALLOCATE (ao_quad(n_atom, quad_n))
1463 ALLOCATE (ao_quad_spin(n_atom, quad_n, nspin))
1464 ao_quad_spin = 0.0_dp
1469 CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_kind)
1471 max_orb = max(max_orb, natorb)
1474 max_shell = max(max_shell, tb%calc%bas%nsh_at(is))
1476 ALLOCATE (ch_atom(n_atom, nspin), ch_shell(n_atom, max_shell))
1477 ALLOCATE (ch_orb(max_orb, n_atom), ch_ref(max_orb, n_atom))
1478 ALLOCATE (ch_orb_spin(max_orb, n_atom, nspin), ch_shell_spin(n_atom, max_shell, nspin))
1482 ch_orb_spin = 0.0_dp
1483 ch_shell_spin = 0.0_dp
1487 CALL tb_ao_charges_kp_spin(matrix_p, matrix_s, aocg_spin(:, :, ispin), ispin, para_env)
1490 CALL tb_contract_dens_kp_spin(matrix_p, tb%dipbra, tb%dipket, im, dip_n, &
1491 ao_dip_spin(:, im, ispin), ispin, para_env)
1494 IF (do_quadrupole)
THEN
1496 CALL tb_contract_dens_kp_spin(matrix_p, tb%quadbra, tb%quadket, im, quad_n, &
1497 ao_quad_spin(:, im, ispin), ispin, para_env)
1502 NULLIFY (p_matrix, s_matrix)
1503 p_matrix => matrix_p(:, 1)
1504 s_matrix => matrix_s(1, 1)%matrix
1506 CALL tb_ao_charges_matrix(matrix_p(ispin, 1)%matrix, s_matrix, aocg_spin(:, :, ispin), para_env)
1509 CALL tb_contract_dens_matrix(matrix_p(ispin, 1)%matrix, tb%dipbra(im)%matrix, &
1510 tb%dipket(im)%matrix, ao_dip_spin(:, im, ispin), para_env)
1513 IF (do_quadrupole)
THEN
1515 CALL tb_contract_dens_matrix(matrix_p(ispin, 1)%matrix, tb%quadbra(im)%matrix, &
1516 tb%quadket(im)%matrix, ao_quad_spin(:, im, ispin), para_env)
1521 IF (nspin == 1)
THEN
1522 aocg(:, :) = aocg_spin(:, :, 1)
1523 IF (do_dipole) ao_dip(:, :) = ao_dip_spin(:, :, 1)
1524 IF (do_quadrupole) ao_quad(:, :) = ao_quad_spin(:, :, 1)
1526 aocg(:, :) = aocg_spin(:, :, 1) + aocg_spin(:, :, 2)
1529 DO iatom = 1, n_atom
1530 pao = ao_dip_spin(iatom, im, 1)
1531 ao_dip_spin(iatom, im, 1) = pao + ao_dip_spin(iatom, im, 2)
1532 ao_dip_spin(iatom, im, 2) = pao - ao_dip_spin(iatom, im, 2)
1535 ao_dip(:, :) = ao_dip_spin(:, :, 1)
1537 IF (do_quadrupole)
THEN
1539 DO iatom = 1, n_atom
1540 pao = ao_quad_spin(iatom, im, 1)
1541 ao_quad_spin(iatom, im, 1) = pao + ao_quad_spin(iatom, im, 2)
1542 ao_quad_spin(iatom, im, 2) = pao - ao_quad_spin(iatom, im, 2)
1545 ao_quad(:, :) = ao_quad_spin(:, :, 1)
1551 CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_kind)
1554 atom_a = atomic_kind_set(ikind)%atom_list(iatom)
1557 norm = 2*lao(is) + 1
1558 ch_ref(is, atom_a) = tb%calc%h0%refocc(nao(is), ikind)/norm
1559 ch_orb(is, atom_a) = aocg(is, atom_a) - ch_ref(is, atom_a)
1560 ch_orb_spin(is, atom_a, 1) = ch_orb(is, atom_a)
1561 IF (nspin == 2) ch_orb_spin(is, atom_a, 2) = &
1562 aocg_spin(is, atom_a, 1) - aocg_spin(is, atom_a, 2)
1563 ch_shell(atom_a, ns) = ch_orb(is, atom_a) + ch_shell(atom_a, ns)
1565 ch_shell_spin(atom_a, ns, ispin) = ch_orb_spin(is, atom_a, ispin) + &
1566 ch_shell_spin(atom_a, ns, ispin)
1570 ch_atom(atom_a, ispin) = sum(ch_orb_spin(:, atom_a, ispin))
1574 native_seed_charge = -sum(ch_atom(:, 1))
1575 seed_native_from_rho = sum(abs(aocg_spin)) > 1.0e-10_dp .AND. &
1576 abs(native_seed_charge - real(dft_control%charge,
dp)) < 1.0e-5_dp
1577 DEALLOCATE (aocg, aocg_spin)
1579 raw_state_status = 1
1580#if defined(__TBLITE_DEBUG_DIAGNOSTICS)
1581 CALL get_environment_variable(
"CP2K_TBLITE_RAW_STATE_DUMP", raw_state_file, status=raw_state_status)
1583 IF (raw_state_status == 0)
THEN
1584 OPEN (newunit=raw_state_unit, file=trim(raw_state_file), status=
"REPLACE", action=
"WRITE")
1585 WRITE (raw_state_unit, *)
"qat"
1586 DO iatom = 1, n_atom
1587 WRITE (raw_state_unit,
"(I0,1X,ES24.16)") iatom, -ch_atom(iatom, 1)
1589 WRITE (raw_state_unit, *)
"qsh"
1590 DO iatom = 1, n_atom
1591 DO is = 1, tb%calc%bas%nsh_at(iatom)
1592 WRITE (raw_state_unit,
"(I0,1X,ES24.16)") tb%calc%bas%ish_at(iatom) + is, -ch_shell(iatom, is)
1596 WRITE (raw_state_unit, *)
"dpat"
1597 DO iatom = 1, n_atom
1598 WRITE (raw_state_unit,
"(I0,3(1X,ES24.16))") iatom, -ao_dip(iatom, :)
1601 IF (do_quadrupole)
THEN
1602 WRITE (raw_state_unit, *)
"qpat"
1603 DO iatom = 1, n_atom
1604 WRITE (raw_state_unit,
"(I0,6(1X,ES24.16))") iatom, -ao_quad(iatom, :)
1607 CLOSE (raw_state_unit)
1610 IF (use_native_mixer)
THEN
1611 IF (.NOT.
ALLOCATED(tb%mixer)) cpabort(
"tblite mixer not initialized")
1612 IF (use_rho .AND. (.NOT. calculate_forces) .AND. scf_env%iter_count > 1)
THEN
1613 CALL tb%mixer%next(error)
1614 IF (
ALLOCATED(error)) cpabort(
"tblite native mixer failed")
1615 CALL tb%mixer%get(tb%wfn%qsh)
1616 tb%wfn%qat(:, :) = 0.0_dp
1617 DO iatom = 1, n_atom
1618 ii = tb%calc%bas%ish_at(iatom)
1620 tb%wfn%qat(iatom, ispin) = &
1621 sum(tb%wfn%qsh(ii + 1:ii + tb%calc%bas%nsh_at(iatom), ispin))
1625 CALL tb%mixer%get(tb%wfn%dpat)
1628 IF (do_quadrupole)
THEN
1629 CALL tb%mixer%get(tb%wfn%qpat)
1630 DEALLOCATE (ao_quad)
1633 IF (use_rho .AND. (.NOT. calculate_forces))
THEN
1634 IF (.NOT. reuse_native_input)
THEN
1635 IF (seed_native_from_rho)
THEN
1638 DO iatom = 1, n_atom
1639 ii = tb%calc%bas%ish_at(iatom)
1641 DO is = 1, tb%calc%bas%nsh_at(iatom)
1642 tb%wfn%qsh(ii + is, ispin) = -ch_shell_spin(iatom, is, ispin)
1644 tb%wfn%qat(iatom, ispin) = &
1645 sum(tb%wfn%qsh(ii + 1:ii + tb%calc%bas%nsh_at(iatom), ispin))
1649 DO iatom = 1, n_atom
1651 tb%wfn%dpat(:, iatom, ispin) = -ao_dip_spin(iatom, :, ispin)
1655 IF (do_quadrupole)
THEN
1656 DO iatom = 1, n_atom
1658 tb%wfn%qpat(:, iatom, ispin) = -ao_quad_spin(iatom, :, ispin)
1663 tb%wfn%qsh(:, :) = 0.0_dp
1664 tb%wfn%qat(:, :) = 0.0_dp
1665 IF (do_dipole) tb%wfn%dpat(:, :, :) = 0.0_dp
1666 IF (do_quadrupole) tb%wfn%qpat(:, :, :) = 0.0_dp
1669 IF (do_dipole)
DEALLOCATE (ao_dip)
1670 IF (do_quadrupole)
DEALLOCATE (ao_quad)
1672 IF ((.NOT. use_rho) .AND. (.NOT. calculate_forces))
THEN
1673 CALL tb%mixer%set(tb%wfn%qsh)
1674 IF (do_dipole)
CALL tb%mixer%set(tb%wfn%dpat)
1675 IF (do_quadrupole)
CALL tb%mixer%set(tb%wfn%qpat)
1677 DO iatom = 1, n_atom
1678 ii = tb%calc%bas%ish_at(iatom)
1680 DO is = 1, tb%calc%bas%nsh_at(iatom)
1681 tb%wfn%qsh(ii + is, ispin) = -ch_shell_spin(iatom, is, ispin)
1683 tb%wfn%qat(iatom, ispin) = &
1684 sum(tb%wfn%qsh(ii + 1:ii + tb%calc%bas%nsh_at(iatom), ispin))
1688 DO iatom = 1, n_atom
1690 tb%wfn%dpat(:, iatom, ispin) = -ao_dip_spin(iatom, :, ispin)
1695 IF (do_quadrupole)
THEN
1696 DO iatom = 1, n_atom
1698 tb%wfn%qpat(:, iatom, ispin) = -ao_quad_spin(iatom, :, ispin)
1701 DEALLOCATE (ao_quad)
1703 IF ((.NOT. use_rho) .AND. (.NOT. calculate_forces))
THEN
1704 CALL tb%mixer%diff(tb%wfn%qsh)
1705 IF (do_dipole)
CALL tb%mixer%diff(tb%wfn%dpat)
1706 IF (do_quadrupole)
CALL tb%mixer%diff(tb%wfn%qpat)
1712 native_sign_mixing = .false.
1713 IF (dft_control%qs_control%do_ls_scf .OR. scf_control%use_ot)
THEN
1717 ELSEIF (nspin > 1)
THEN
1718 n_mix_cols = nspin*max_shell
1719 IF (do_dipole) n_mix_cols = n_mix_cols + nspin*dip_n
1720 IF (do_quadrupole) n_mix_cols = n_mix_cols + nspin*quad_n
1721 ALLOCATE (mix_vars(n_atom, n_mix_cols))
1726 mix_vars(:, mix_offset + 1:mix_offset + max_shell) = &
1727 -ch_shell_spin(:, 1:max_shell, ispin)
1728 mix_offset = mix_offset + max_shell
1732 mix_vars(:, mix_offset + 1:mix_offset + dip_n) = &
1733 -ao_dip_spin(:, 1:dip_n, ispin)
1734 mix_offset = mix_offset + dip_n
1737 IF (do_quadrupole)
THEN
1739 mix_vars(:, mix_offset + 1:mix_offset + quad_n) = &
1740 -ao_quad_spin(:, 1:quad_n, ispin)
1741 mix_offset = mix_offset + quad_n
1745 IF (.NOT. use_no_mixer)
THEN
1746 CALL charge_mixing(scf_env%mixing_method, scf_env%mixing_store, &
1747 mix_vars, para_env, scf_env%iter_count)
1752 ch_shell_spin(:, 1:max_shell, ispin) = &
1753 -mix_vars(:, mix_offset + 1:mix_offset + max_shell)
1754 mix_offset = mix_offset + max_shell
1756 ch_shell(:, 1:max_shell) = ch_shell_spin(:, 1:max_shell, 1)
1759 ao_dip_spin(:, 1:dip_n, ispin) = &
1760 -mix_vars(:, mix_offset + 1:mix_offset + dip_n)
1761 mix_offset = mix_offset + dip_n
1763 ao_dip(:, 1:dip_n) = ao_dip_spin(:, 1:dip_n, 1)
1765 IF (do_quadrupole)
THEN
1767 ao_quad_spin(:, 1:quad_n, ispin) = &
1768 -mix_vars(:, mix_offset + 1:mix_offset + quad_n)
1769 mix_offset = mix_offset + quad_n
1771 ao_quad(:, 1:quad_n) = ao_quad_spin(:, 1:quad_n, 1)
1773 DEALLOCATE (mix_vars)
1775 do_combined_mixing = do_dipole .OR. do_quadrupole
1776 native_sign_mixing = do_dipole .OR. do_quadrupole
1777 discard_mixed_output = .false.
1778 skip_charge_mixing = use_no_mixer
1779 IF (skip_charge_mixing)
THEN
1781 ELSEIF (do_combined_mixing)
THEN
1782 n_mix_cols = max_shell
1783 IF (do_dipole) n_mix_cols = n_mix_cols + dip_n
1784 IF (do_quadrupole) n_mix_cols = n_mix_cols + quad_n
1785 ALLOCATE (mix_vars(n_atom, n_mix_cols))
1786 IF (native_sign_mixing)
THEN
1787 mix_vars(:, 1:max_shell) = -ch_shell(:, 1:max_shell)
1789 mix_vars(:, 1:max_shell) = ch_shell(:, 1:max_shell)
1791 mix_offset = max_shell
1793 IF (native_sign_mixing)
THEN
1794 mix_vars(:, mix_offset + 1:mix_offset + dip_n) = -ao_dip(:, 1:dip_n)
1796 mix_vars(:, mix_offset + 1:mix_offset + dip_n) = ao_dip(:, 1:dip_n)
1798 mix_offset = mix_offset + dip_n
1800 IF (do_quadrupole)
THEN
1801 IF (native_sign_mixing)
THEN
1802 mix_vars(:, mix_offset + 1:mix_offset + quad_n) = -ao_quad(:, 1:quad_n)
1804 mix_vars(:, mix_offset + 1:mix_offset + quad_n) = ao_quad(:, 1:quad_n)
1807 CALL charge_mixing(scf_env%mixing_method, scf_env%mixing_store, &
1808 mix_vars, para_env, scf_env%iter_count)
1809 IF (.NOT. discard_mixed_output)
THEN
1810 IF (native_sign_mixing)
THEN
1811 ch_shell(:, 1:max_shell) = -mix_vars(:, 1:max_shell)
1813 ch_shell(:, 1:max_shell) = mix_vars(:, 1:max_shell)
1815 mix_offset = max_shell
1817 IF (native_sign_mixing)
THEN
1818 ao_dip(:, 1:dip_n) = -mix_vars(:, mix_offset + 1:mix_offset + dip_n)
1820 ao_dip(:, 1:dip_n) = mix_vars(:, mix_offset + 1:mix_offset + dip_n)
1822 mix_offset = mix_offset + dip_n
1824 IF (do_quadrupole)
THEN
1825 IF (native_sign_mixing)
THEN
1826 ao_quad(:, 1:quad_n) = -mix_vars(:, mix_offset + 1:mix_offset + quad_n)
1828 ao_quad(:, 1:quad_n) = mix_vars(:, mix_offset + 1:mix_offset + quad_n)
1832 DEALLOCATE (mix_vars)
1834 CALL charge_mixing(scf_env%mixing_method, scf_env%mixing_store, &
1835 ch_shell, para_env, scf_env%iter_count)
1843 moment_alpha = scf_env%p_mix_alpha
1845 DO iatom = 1, n_atom
1846 ii = tb%calc%bas%ish_at(iatom)
1848 DO is = 1, tb%calc%bas%nsh_at(iatom)
1849 tb%wfn%qsh(ii + is, ispin) = -ch_shell_spin(iatom, is, ispin)
1851 tb%wfn%qat(iatom, ispin) = &
1852 sum(tb%wfn%qsh(ii + 1:ii + tb%calc%bas%nsh_at(iatom), ispin))
1856 DO iatom = 1, n_atom
1858 tb%wfn%dpat(:, iatom, ispin) = -ao_dip_spin(iatom, :, ispin)
1863 IF (do_quadrupole)
THEN
1864 DO iatom = 1, n_atom
1866 tb%wfn%qpat(:, iatom, ispin) = -ao_quad_spin(iatom, :, ispin)
1869 DEALLOCATE (ao_quad)
1872 DO iatom = 1, n_atom
1873 ii = tb%calc%bas%ish_at(iatom)
1874 DO is = 1, tb%calc%bas%nsh_at(iatom)
1875 new_charge = -ch_shell(iatom, is)
1876 tb%wfn%qsh(ii + is, 1) = new_charge
1878 IF (native_sign_mixing)
THEN
1879 tb%wfn%qat(iatom, 1) = sum(tb%wfn%qsh(ii + 1:ii + tb%calc%bas%nsh_at(iatom), 1))
1881 tb%wfn%qat(iatom, 1) = -ch_atom(iatom, 1)
1886 DO iatom = 1, n_atom
1888 tb%wfn%dpat(im, iatom, 1) = -ao_dip(iatom, im)
1893 IF (do_quadrupole)
THEN
1894 DO iatom = 1, n_atom
1896 tb%wfn%qpat(im, iatom, 1) = -ao_quad(iatom, im)
1899 DEALLOCATE (ao_quad)
1908 IF (
ALLOCATED(tb%calc%coulomb))
THEN
1909 CALL tb%calc%coulomb%get_potential(tb%mol, tb%cache, tb%wfn, tb%pot)
1910 CALL tb%calc%coulomb%get_energy(tb%mol, tb%cache, tb%wfn, tb%e_es)
1912 IF (
ALLOCATED(tb%calc%dispersion))
THEN
1913 IF (.NOT. skip_scf_dispersion_potential) &
1914 CALL tb%calc%dispersion%get_potential(tb%mol, tb%dcache, tb%wfn, tb%pot)
1915 IF (.NOT. skip_scf_dispersion_energy) &
1916 CALL tb%calc%dispersion%get_energy(tb%mol, tb%dcache, tb%wfn, tb%e_scd)
1918 IF (
ALLOCATED(tb%calc%interactions))
THEN
1919 CALL tb%calc%interactions%get_potential(tb%mol, tb%icache, tb%wfn, tb%pot)
1920 CALL tb%calc%interactions%get_energy(tb%mol, tb%icache, tb%wfn, tb%e_int)
1923 IF (calculate_forces)
THEN
1924 IF (
ALLOCATED(tb%calc%coulomb))
THEN
1926 CALL tb%calc%coulomb%get_gradient(tb%mol, tb%cache, tb%wfn, tb%grad, tb%sigma)
1927 CALL tb_dump_sigma_component(
"after_coulomb", tb%sigma, para_env)
1928 CALL tb_grad2force(qs_env, tb, para_env, 3)
1931 IF (
ALLOCATED(tb%calc%dispersion) .AND. .NOT. skip_scf_dispersion_gradient)
THEN
1933 CALL tb%calc%dispersion%get_gradient(tb%mol, tb%dcache, tb%wfn, tb%grad, tb%sigma)
1934 CALL tb_dump_sigma_component(
"after_dispersion_scf", tb%sigma, para_env)
1935 CALL tb_grad2force(qs_env, tb, para_env, 2)
1938 IF (
ALLOCATED(tb%calc%interactions))
THEN
1940 CALL tb%calc%interactions%get_gradient(tb%mol, tb%icache, tb%wfn, tb%grad, tb%sigma)
1941 CALL tb_dump_sigma_component(
"after_interactions_scf", tb%sigma, para_env)
1942 CALL tb_grad2force(qs_env, tb, para_env, 3)
1946 IF (
ALLOCATED(ao_dip_spin))
DEALLOCATE (ao_dip_spin)
1947 IF (
ALLOCATED(ao_quad_spin))
DEALLOCATE (ao_quad_spin)
1948 DEALLOCATE (ch_atom, ch_shell, ch_orb, ch_ref, ch_orb_spin, ch_shell_spin)
1953 mark_used(dft_control)
1954 mark_used(calculate_forces)
1956 cpabort(
"Built without TBLITE")
1973#if defined(__TBLITE)
1975 INTEGER :: ikind, jkind, iatom, jatom, icol, irow
1976 INTEGER :: ic, id1, id2, id3, iq1, iq2, iq3, iq4, iq5, iq6, &
1977 is, nimg, ni, nj, i, j, nspin
1978 INTEGER :: la, lb, za, zb
1980 INTEGER,
DIMENSION(3) :: cellind
1981 INTEGER,
DIMENSION(25) :: naoa, naob
1982 REAL(kind=
dp),
DIMENSION(3) :: rij
1983 REAL(kind=
dp) :: mpfac
1984#if defined(__TBLITE_DEBUG_DIAGNOSTICS)
1985 INTEGER :: debug_status
1986 CHARACTER(LEN=32) :: debug_value
1988 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: kind_of, sum_shell
1989 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: ashift, bshift
1990 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: ksblock, sblock
1991 INTEGER,
DIMENSION(:, :, :),
POINTER :: cell_to_index
1992 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: dip_ket1, dip_ket2, dip_ket3, &
1993 dip_bra1, dip_bra2, dip_bra3
1994 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: quad_ket1, quad_ket2, quad_ket3, &
1995 quad_ket4, quad_ket5, quad_ket6, &
1996 quad_bra1, quad_bra2, quad_bra3, &
1997 quad_bra4, quad_bra5, quad_bra6
2001 TYPE(
dbcsr_p_type),
DIMENSION(:, :),
POINTER :: matrix_s
2002 TYPE(
dbcsr_p_type),
DIMENSION(:, :),
POINTER :: ks_matrix
2005 DIMENSION(:),
POINTER :: nl_iterator
2010 TYPE(
qs_kind_type),
DIMENSION(:),
POINTER :: qs_kind_set
2013 nimg = dft_control%nimages
2016 NULLIFY (matrix_s, ks_matrix, n_list, kp_list, qs_kind_set)
2017 CALL get_qs_env(qs_env=qs_env, sab_orb=n_list, sab_kp=kp_list, &
2018 matrix_s_kp=matrix_s, matrix_ks_kp=ks_matrix, qs_kind_set=qs_kind_set)
2020 IF (.NOT.
ASSOCIATED(kp_list)) cpabort(
"Missing k-point neighbor list for tblite Hamiltonian")
2023 nspin =
SIZE(ks_matrix, 1)
2026 ALLOCATE (sum_shell(tb%mol%nat))
2028 DO j = 1, tb%mol%nat
2030 i = i + tb%calc%bas%nsh_at(j)
2035 CALL get_qs_env(qs_env=qs_env, atomic_kind_set=atomic_kind_set)
2042 ikind = kind_of(irow)
2043 jkind = kind_of(icol)
2046 CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_atom_a)
2047 CALL get_qs_kind(qs_kind_set(jkind), xtb_parameter=xtb_atom_b)
2051 ni =
SIZE(sblock, 1)
2052 ALLOCATE (ashift(ni, ni))
2054 nj =
SIZE(sblock, 2)
2055 ALLOCATE (bshift(nj, nj))
2060 la = naoa(i) + sum_shell(irow)
2061 ashift(i, i) = tb_spin_project(tb%pot%vsh(la, :), is)
2065 lb = naob(j) + sum_shell(icol)
2066 bshift(j, j) = tb_spin_project(tb%pot%vsh(lb, :), is)
2070 row=irow, col=icol, block=ksblock, found=found)
2072 ksblock = ksblock - 0.5_dp*(matmul(ashift, sblock) &
2073 + matmul(sblock, bshift))
2074 ksblock = ksblock - 0.5_dp*(tb_spin_project(tb%pot%vat(irow, :), is) &
2075 + tb_spin_project(tb%pot%vat(icol, :), is))*sblock
2077 DEALLOCATE (ashift, bshift)
2081 IF (
ASSOCIATED(tb%dipbra))
THEN
2086 NULLIFY (dip_bra1, dip_bra2, dip_bra3)
2088 row=irow, col=icol, block=dip_bra1, found=found)
2091 row=irow, col=icol, block=dip_bra2, found=found)
2094 row=irow, col=icol, block=dip_bra3, found=found)
2096 NULLIFY (dip_ket1, dip_ket2, dip_ket3)
2098 row=irow, col=icol, block=dip_ket1, found=found)
2101 row=irow, col=icol, block=dip_ket2, found=found)
2104 row=irow, col=icol, block=dip_ket3, found=found)
2110 row=irow, col=icol, block=ksblock, found=found)
2112 ksblock = ksblock + mpfac*(dip_ket1*tb_spin_project(tb%pot%vdp(1, irow, :), is) &
2113 + dip_ket2*tb_spin_project(tb%pot%vdp(2, irow, :), is) &
2114 + dip_ket3*tb_spin_project(tb%pot%vdp(3, irow, :), is) &
2115 + dip_bra1*tb_spin_project(tb%pot%vdp(1, icol, :), is) &
2116 + dip_bra2*tb_spin_project(tb%pot%vdp(2, icol, :), is) &
2117 + dip_bra3*tb_spin_project(tb%pot%vdp(3, icol, :), is))
2123 IF (
ASSOCIATED(tb%quadbra))
THEN
2128 NULLIFY (quad_bra1, quad_bra2, quad_bra3, quad_bra4, quad_bra5, quad_bra6)
2130 row=irow, col=icol, block=quad_bra1, found=found)
2133 row=irow, col=icol, block=quad_bra2, found=found)
2136 row=irow, col=icol, block=quad_bra3, found=found)
2139 row=irow, col=icol, block=quad_bra4, found=found)
2142 row=irow, col=icol, block=quad_bra5, found=found)
2145 row=irow, col=icol, block=quad_bra6, found=found)
2148 NULLIFY (quad_ket1, quad_ket2, quad_ket3, quad_ket4, quad_ket5, quad_ket6)
2150 row=irow, col=icol, block=quad_ket1, found=found)
2153 row=irow, col=icol, block=quad_ket2, found=found)
2156 row=irow, col=icol, block=quad_ket3, found=found)
2159 row=irow, col=icol, block=quad_ket4, found=found)
2162 row=irow, col=icol, block=quad_ket5, found=found)
2165 row=irow, col=icol, block=quad_ket6, found=found)
2171 row=irow, col=icol, block=ksblock, found=found)
2174 ksblock = ksblock + mpfac*(quad_ket1*tb_spin_project(tb%pot%vqp(1, irow, :), is) &
2175 + quad_ket2*tb_spin_project(tb%pot%vqp(2, irow, :), is) &
2176 + quad_ket3*tb_spin_project(tb%pot%vqp(3, irow, :), is) &
2177 + quad_ket4*tb_spin_project(tb%pot%vqp(4, irow, :), is) &
2178 + quad_ket5*tb_spin_project(tb%pot%vqp(5, irow, :), is) &
2179 + quad_ket6*tb_spin_project(tb%pot%vqp(6, irow, :), is) &
2180 + quad_bra1*tb_spin_project(tb%pot%vqp(1, icol, :), is) &
2181 + quad_bra2*tb_spin_project(tb%pot%vqp(2, icol, :), is) &
2182 + quad_bra3*tb_spin_project(tb%pot%vqp(3, icol, :), is) &
2183 + quad_bra4*tb_spin_project(tb%pot%vqp(4, icol, :), is) &
2184 + quad_bra5*tb_spin_project(tb%pot%vqp(5, icol, :), is) &
2185 + quad_bra6*tb_spin_project(tb%pot%vqp(6, icol, :), is))
2193 CALL get_qs_env(qs_env=qs_env, kpoints=kpoints)
2194 NULLIFY (cell_to_index)
2197 NULLIFY (nl_iterator)
2201 iatom=iatom, jatom=jatom, r=rij, cell=cellind)
2203 icol = max(iatom, jatom)
2204 irow = min(iatom, jatom)
2206 IF (iatom > jatom)
THEN
2212 ic = cell_to_index(cellind(1), cellind(2), cellind(3))
2217 row=irow, col=icol, block=sblock, found=found)
2221 CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_atom_a)
2222 CALL get_qs_kind(qs_kind_set(jkind), xtb_parameter=xtb_atom_b)
2226 ni =
SIZE(sblock, 1)
2227 ALLOCATE (ashift(ni, ni))
2229 nj =
SIZE(sblock, 2)
2230 ALLOCATE (bshift(nj, nj))
2235 la = naoa(i) + sum_shell(irow)
2236 ashift(i, i) = tb_spin_project(tb%pot%vsh(la, :), is)
2240 lb = naob(j) + sum_shell(icol)
2241 bshift(j, j) = tb_spin_project(tb%pot%vsh(lb, :), is)
2245 row=irow, col=icol, block=ksblock, found=found)
2247 ksblock = ksblock - 0.5_dp*(matmul(ashift, sblock) &
2248 + matmul(sblock, bshift))
2249 ksblock = ksblock - 0.5_dp*(tb_spin_project(tb%pot%vat(irow, :), is) &
2250 + tb_spin_project(tb%pot%vat(icol, :), is))*sblock
2252 DEALLOCATE (ashift, bshift)
2256 IF (
ASSOCIATED(tb%dipbra))
THEN
2257 NULLIFY (nl_iterator)
2261 iatom=iatom, jatom=jatom, cell=cellind)
2262 icol = max(iatom, jatom)
2263 irow = min(iatom, jatom)
2264 ic = cell_to_index(cellind(1), cellind(2), cellind(3))
2266 id1 = 1 + dip_n*(ic - 1)
2267 id2 = 2 + dip_n*(ic - 1)
2268 id3 = 3 + dip_n*(ic - 1)
2270 NULLIFY (dip_bra1, dip_bra2, dip_bra3)
2272 row=irow, col=icol, block=dip_bra1, found=found)
2275 row=irow, col=icol, block=dip_bra2, found=found)
2278 row=irow, col=icol, block=dip_bra3, found=found)
2280 NULLIFY (dip_ket1, dip_ket2, dip_ket3)
2282 row=irow, col=icol, block=dip_ket1, found=found)
2285 row=irow, col=icol, block=dip_ket2, found=found)
2288 row=irow, col=icol, block=dip_ket3, found=found)
2294 row=irow, col=icol, block=ksblock, found=found)
2296 ksblock = ksblock + mpfac*(dip_ket1*tb_spin_project(tb%pot%vdp(1, irow, :), is) &
2297 + dip_ket2*tb_spin_project(tb%pot%vdp(2, irow, :), is) &
2298 + dip_ket3*tb_spin_project(tb%pot%vdp(3, irow, :), is) &
2299 + dip_bra1*tb_spin_project(tb%pot%vdp(1, icol, :), is) &
2300 + dip_bra2*tb_spin_project(tb%pot%vdp(2, icol, :), is) &
2301 + dip_bra3*tb_spin_project(tb%pot%vdp(3, icol, :), is))
2307 IF (
ASSOCIATED(tb%quadbra))
THEN
2308 NULLIFY (nl_iterator)
2312 iatom=iatom, jatom=jatom, cell=cellind)
2313 icol = max(iatom, jatom)
2314 irow = min(iatom, jatom)
2315 ic = cell_to_index(cellind(1), cellind(2), cellind(3))
2317 iq1 = 1 + quad_n*(ic - 1)
2318 iq2 = 2 + quad_n*(ic - 1)
2319 iq3 = 3 + quad_n*(ic - 1)
2320 iq4 = 4 + quad_n*(ic - 1)
2321 iq5 = 5 + quad_n*(ic - 1)
2322 iq6 = 6 + quad_n*(ic - 1)
2324 NULLIFY (quad_bra1, quad_bra2, quad_bra3, quad_bra4, quad_bra5, quad_bra6)
2326 row=irow, col=icol, block=quad_bra1, found=found)
2329 row=irow, col=icol, block=quad_bra2, found=found)
2332 row=irow, col=icol, block=quad_bra3, found=found)
2335 row=irow, col=icol, block=quad_bra4, found=found)
2338 row=irow, col=icol, block=quad_bra5, found=found)
2341 row=irow, col=icol, block=quad_bra6, found=found)
2344 NULLIFY (quad_ket1, quad_ket2, quad_ket3, quad_ket4, quad_ket5, quad_ket6)
2346 row=irow, col=icol, block=quad_ket1, found=found)
2349 row=irow, col=icol, block=quad_ket2, found=found)
2352 row=irow, col=icol, block=quad_ket3, found=found)
2355 row=irow, col=icol, block=quad_ket4, found=found)
2358 row=irow, col=icol, block=quad_ket5, found=found)
2361 row=irow, col=icol, block=quad_ket6, found=found)
2367 row=irow, col=icol, block=ksblock, found=found)
2370 ksblock = ksblock + mpfac*(quad_ket1*tb_spin_project(tb%pot%vqp(1, irow, :), is) &
2371 + quad_ket2*tb_spin_project(tb%pot%vqp(2, irow, :), is) &
2372 + quad_ket3*tb_spin_project(tb%pot%vqp(3, irow, :), is) &
2373 + quad_ket4*tb_spin_project(tb%pot%vqp(4, irow, :), is) &
2374 + quad_ket5*tb_spin_project(tb%pot%vqp(5, irow, :), is) &
2375 + quad_ket6*tb_spin_project(tb%pot%vqp(6, irow, :), is) &
2376 + quad_bra1*tb_spin_project(tb%pot%vqp(1, icol, :), is) &
2377 + quad_bra2*tb_spin_project(tb%pot%vqp(2, icol, :), is) &
2378 + quad_bra3*tb_spin_project(tb%pot%vqp(3, icol, :), is) &
2379 + quad_bra4*tb_spin_project(tb%pot%vqp(4, icol, :), is) &
2380 + quad_bra5*tb_spin_project(tb%pot%vqp(5, icol, :), is) &
2381 + quad_bra6*tb_spin_project(tb%pot%vqp(6, icol, :), is))
2392 mark_used(dft_control)
2393 cpabort(
"Built without TBLITE")
2408#if defined(__TBLITE)
2410 CHARACTER(LEN=*),
PARAMETER :: routinen =
'tb_get_multipole'
2412 INTEGER :: ikind, jkind, iatom, jatom, icol, irow, iset, jset, ityp, jtyp
2413 INTEGER :: ic,
idx, id1, id2, id3, img, iq1, iq2, iq3, iq4, iq5, iq6
2414 INTEGER :: nkind, natom, handle, nimg, i, inda, indb
2415 INTEGER :: atom_a, atom_b, nseta, nsetb, ia, ib, ij
2418 INTEGER,
DIMENSION(3) :: cell
2419 INTEGER,
DIMENSION(:, :, :),
POINTER :: cell_to_index
2420 REAL(kind=
dp),
DIMENSION(3) :: rij
2421 INTEGER,
DIMENSION(:),
POINTER :: la_max, lb_max
2422 INTEGER,
DIMENSION(:),
POINTER :: nsgfa, nsgfb
2423 INTEGER,
DIMENSION(:, :),
POINTER :: first_sgfa, first_sgfb
2424 INTEGER,
ALLOCATABLE :: atom_of_kind(:)
2425 REAL(kind=
dp),
ALLOCATABLE :: stmp(:)
2426 REAL(kind=
dp),
ALLOCATABLE :: dtmp(:, :), qtmp(:, :), dtmpj(:, :), qtmpj(:, :)
2427 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: dip_ket1, dip_ket2, dip_ket3, &
2428 dip_bra1, dip_bra2, dip_bra3
2429 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: quad_ket1, quad_ket2, quad_ket3, &
2430 quad_ket4, quad_ket5, quad_ket6, &
2431 quad_bra1, quad_bra2, quad_bra3, &
2432 quad_bra4, quad_bra5, quad_bra6
2435 TYPE(
dbcsr_p_type),
DIMENSION(:, :),
POINTER :: matrix_s
2443 DIMENSION(:),
POINTER :: nl_iterator
2445 TYPE(
qs_kind_type),
DIMENSION(:),
POINTER :: qs_kind_set
2447 CALL timeset(routinen, handle)
2450 NULLIFY (atomic_kind_set, qs_kind_set, sab_orb, sab_kp, particle_set)
2451 NULLIFY (dft_control, matrix_s, kpoints, cell_to_index)
2452 CALL get_qs_env(qs_env=qs_env, atomic_kind_set=atomic_kind_set, &
2453 qs_kind_set=qs_kind_set, &
2456 particle_set=particle_set, &
2457 dft_control=dft_control, &
2459 matrix_s_kp=matrix_s)
2460 natom =
SIZE(particle_set)
2461 nkind =
SIZE(atomic_kind_set)
2462 nimg = dft_control%nimages
2464 IF (.NOT.
ASSOCIATED(sab_kp)) cpabort(
"Missing k-point neighbor list for tblite multipoles")
2472 ALLOCATE (basis_set_list(nkind))
2475 ALLOCATE (stmp(msao(tb%calc%bas%maxl)**2))
2476 ALLOCATE (dtmp(dip_n, msao(tb%calc%bas%maxl)**2))
2477 ALLOCATE (qtmp(quad_n, msao(tb%calc%bas%maxl)**2))
2478 ALLOCATE (dtmpj(dip_n, msao(tb%calc%bas%maxl)**2))
2479 ALLOCATE (qtmpj(quad_n, msao(tb%calc%bas%maxl)**2))
2488 idx = i + dip_n*(img - 1)
2489 ALLOCATE (tb%dipbra(
idx)%matrix)
2490 ALLOCATE (tb%dipket(
idx)%matrix)
2491 CALL dbcsr_create(tb%dipbra(
idx)%matrix, template=matrix_s(1, img)%matrix, &
2492 name=
"DIPOLE BRAMATRIX")
2493 CALL dbcsr_create(tb%dipket(
idx)%matrix, template=matrix_s(1, img)%matrix, &
2494 name=
"DIPOLE KETMATRIX")
2499 idx = i + quad_n*(img - 1)
2500 ALLOCATE (tb%quadbra(
idx)%matrix)
2501 ALLOCATE (tb%quadket(
idx)%matrix)
2502 CALL dbcsr_create(tb%quadbra(
idx)%matrix, template=matrix_s(1, img)%matrix, &
2503 name=
"QUADRUPOLE BRAMATRIX")
2504 CALL dbcsr_create(tb%quadket(
idx)%matrix, template=matrix_s(1, img)%matrix, &
2505 name=
"QUADRUPOLE KETMATRIX")
2512 NULLIFY (nl_iterator)
2516 iatom=iatom, jatom=jatom, r=rij, cell=cell)
2518 r2 = norm2(rij(:))**2
2520 icol = max(iatom, jatom)
2521 irow = min(iatom, jatom)
2523 IF (iatom < jatom)
THEN
2533 ic = cell_to_index(cell(1), cell(2), cell(3))
2536 id1 = 1 + dip_n*(ic - 1)
2537 id2 = 2 + dip_n*(ic - 1)
2538 id3 = 3 + dip_n*(ic - 1)
2539 iq1 = 1 + quad_n*(ic - 1)
2540 iq2 = 2 + quad_n*(ic - 1)
2541 iq3 = 3 + quad_n*(ic - 1)
2542 iq4 = 4 + quad_n*(ic - 1)
2543 iq5 = 5 + quad_n*(ic - 1)
2544 iq6 = 6 + quad_n*(ic - 1)
2546 ityp = tb%mol%id(icol)
2547 jtyp = tb%mol%id(irow)
2549 NULLIFY (dip_bra1, dip_bra2, dip_bra3)
2551 row=irow, col=icol, block=dip_bra1, found=found)
2554 row=irow, col=icol, block=dip_bra2, found=found)
2557 row=irow, col=icol, block=dip_bra3, found=found)
2560 NULLIFY (dip_ket1, dip_ket2, dip_ket3)
2562 row=irow, col=icol, block=dip_ket1, found=found)
2565 row=irow, col=icol, block=dip_ket2, found=found)
2568 row=irow, col=icol, block=dip_ket3, found=found)
2571 NULLIFY (quad_bra1, quad_bra2, quad_bra3, quad_bra4, quad_bra5, quad_bra6)
2573 row=irow, col=icol, block=quad_bra1, found=found)
2576 row=irow, col=icol, block=quad_bra2, found=found)
2579 row=irow, col=icol, block=quad_bra3, found=found)
2582 row=irow, col=icol, block=quad_bra4, found=found)
2585 row=irow, col=icol, block=quad_bra5, found=found)
2588 row=irow, col=icol, block=quad_bra6, found=found)
2591 NULLIFY (quad_ket1, quad_ket2, quad_ket3, quad_ket4, quad_ket5, quad_ket6)
2593 row=irow, col=icol, block=quad_ket1, found=found)
2596 row=irow, col=icol, block=quad_ket2, found=found)
2599 row=irow, col=icol, block=quad_ket3, found=found)
2602 row=irow, col=icol, block=quad_ket4, found=found)
2605 row=irow, col=icol, block=quad_ket5, found=found)
2608 row=irow, col=icol, block=quad_ket6, found=found)
2612 basis_set_a => basis_set_list(ikind)%gto_basis_set
2613 IF (.NOT.
ASSOCIATED(basis_set_a)) cycle
2614 basis_set_b => basis_set_list(jkind)%gto_basis_set
2615 IF (.NOT.
ASSOCIATED(basis_set_b)) cycle
2616 atom_a = atom_of_kind(icol)
2617 atom_b = atom_of_kind(irow)
2619 first_sgfa => basis_set_a%first_sgf
2620 la_max => basis_set_a%lmax
2621 nseta = basis_set_a%nset
2622 nsgfa => basis_set_a%nsgf_set
2624 first_sgfb => basis_set_b%first_sgf
2625 lb_max => basis_set_b%lmax
2626 nsetb = basis_set_b%nset
2627 nsgfb => basis_set_b%nsgf_set
2631 IF (icol == irow .AND. r2 < same_atom**2)
THEN
2634 CALL multipole_cgto(tb%calc%bas%cgto(jset, ityp), tb%calc%bas%cgto(iset, ityp), &
2635 & r2, -rij, tb%calc%bas%intcut, stmp, dtmp, qtmp)
2637 DO inda = 1, nsgfa(iset)
2638 ia = first_sgfa(1, iset) - first_sgfa(1, 1) + inda
2639 DO indb = 1, nsgfb(jset)
2640 ib = first_sgfb(1, jset) - first_sgfb(1, 1) + indb
2641 ij = indb + nsgfb(jset)*(inda - 1)
2643 dip_ket1(ib, ia) = dip_ket1(ib, ia) + dtmp(1, ij)
2644 dip_ket2(ib, ia) = dip_ket2(ib, ia) + dtmp(2, ij)
2645 dip_ket3(ib, ia) = dip_ket3(ib, ia) + dtmp(3, ij)
2647 quad_ket1(ib, ia) = quad_ket1(ib, ia) + qtmp(1, ij)
2648 quad_ket2(ib, ia) = quad_ket2(ib, ia) + qtmp(2, ij)
2649 quad_ket3(ib, ia) = quad_ket3(ib, ia) + qtmp(3, ij)
2650 quad_ket4(ib, ia) = quad_ket4(ib, ia) + qtmp(4, ij)
2651 quad_ket5(ib, ia) = quad_ket5(ib, ia) + qtmp(5, ij)
2652 quad_ket6(ib, ia) = quad_ket6(ib, ia) + qtmp(6, ij)
2654 dip_bra1(ib, ia) = dip_bra1(ib, ia) + dtmp(1, ij)
2655 dip_bra2(ib, ia) = dip_bra2(ib, ia) + dtmp(2, ij)
2656 dip_bra3(ib, ia) = dip_bra3(ib, ia) + dtmp(3, ij)
2658 quad_bra1(ib, ia) = quad_bra1(ib, ia) + qtmp(1, ij)
2659 quad_bra2(ib, ia) = quad_bra2(ib, ia) + qtmp(2, ij)
2660 quad_bra3(ib, ia) = quad_bra3(ib, ia) + qtmp(3, ij)
2661 quad_bra4(ib, ia) = quad_bra4(ib, ia) + qtmp(4, ij)
2662 quad_bra5(ib, ia) = quad_bra5(ib, ia) + qtmp(5, ij)
2663 quad_bra6(ib, ia) = quad_bra6(ib, ia) + qtmp(6, ij)
2671 CALL multipole_cgto(tb%calc%bas%cgto(jset, jtyp), tb%calc%bas%cgto(iset, ityp), &
2672 & r2, -rij, tb%calc%bas%intcut, stmp, dtmp, qtmp)
2674 DO inda = 1, nsgfa(iset)
2675 ia = first_sgfa(1, iset) - first_sgfa(1, 1) + inda
2676 DO indb = 1, nsgfb(jset)
2677 ib = first_sgfb(1, jset) - first_sgfb(1, 1) + indb
2679 ij = indb + nsgfb(jset)*(inda - 1)
2680 CALL tb_shift_multipole(-rij, stmp(ij), dtmp(:, ij), qtmp(:, ij), &
2681 dtmpj(:, ij), qtmpj(:, ij))
2683 dip_bra1(ib, ia) = dip_bra1(ib, ia) + dtmp(1, ij)
2684 dip_bra2(ib, ia) = dip_bra2(ib, ia) + dtmp(2, ij)
2685 dip_bra3(ib, ia) = dip_bra3(ib, ia) + dtmp(3, ij)
2687 quad_bra1(ib, ia) = quad_bra1(ib, ia) + qtmp(1, ij)
2688 quad_bra2(ib, ia) = quad_bra2(ib, ia) + qtmp(2, ij)
2689 quad_bra3(ib, ia) = quad_bra3(ib, ia) + qtmp(3, ij)
2690 quad_bra4(ib, ia) = quad_bra4(ib, ia) + qtmp(4, ij)
2691 quad_bra5(ib, ia) = quad_bra5(ib, ia) + qtmp(5, ij)
2692 quad_bra6(ib, ia) = quad_bra6(ib, ia) + qtmp(6, ij)
2694 dip_ket1(ib, ia) = dip_ket1(ib, ia) + dtmpj(1, ij)
2695 dip_ket2(ib, ia) = dip_ket2(ib, ia) + dtmpj(2, ij)
2696 dip_ket3(ib, ia) = dip_ket3(ib, ia) + dtmpj(3, ij)
2698 quad_ket1(ib, ia) = quad_ket1(ib, ia) + qtmpj(1, ij)
2699 quad_ket2(ib, ia) = quad_ket2(ib, ia) + qtmpj(2, ij)
2700 quad_ket3(ib, ia) = quad_ket3(ib, ia) + qtmpj(3, ij)
2701 quad_ket4(ib, ia) = quad_ket4(ib, ia) + qtmpj(4, ij)
2702 quad_ket5(ib, ia) = quad_ket5(ib, ia) + qtmpj(5, ij)
2703 quad_ket6(ib, ia) = quad_ket6(ib, ia) + qtmpj(6, ij)
2712 DO i = 1,
SIZE(tb%dipbra)
2716 DO i = 1,
SIZE(tb%quadbra)
2721 DEALLOCATE (basis_set_list)
2723 CALL timestop(handle)
2728 cpabort(
"Built without TBLITE")
2742 PURE SUBROUTINE tb_shift_multipole(vec, s, di, qi, dj, qj)
2744 REAL(kind=
dp),
DIMENSION(:),
INTENT(IN) :: vec
2745 REAL(kind=
dp),
INTENT(IN) :: s
2746 REAL(kind=
dp),
DIMENSION(:),
INTENT(IN) :: di, qi
2747 REAL(kind=
dp),
DIMENSION(:),
INTENT(OUT) :: dj, qj
2751 dj(1) = di(1) + vec(1)*s
2752 dj(2) = di(2) + vec(2)*s
2753 dj(3) = di(3) + vec(3)*s
2755 qj(1) = 2*vec(1)*di(1) + vec(1)**2*s
2756 qj(3) = 2*vec(2)*di(2) + vec(2)**2*s
2757 qj(6) = 2*vec(3)*di(3) + vec(3)**2*s
2758 qj(2) = vec(1)*di(2) + vec(2)*di(1) + vec(1)*vec(2)*s
2759 qj(4) = vec(1)*di(3) + vec(3)*di(1) + vec(1)*vec(3)*s
2760 qj(5) = vec(2)*di(3) + vec(3)*di(2) + vec(2)*vec(3)*s
2761 tr = 0.5_dp*(qj(1) + qj(3) + qj(6))
2763 qj(1) = qi(1) + 1.5_dp*qj(1) - tr
2764 qj(2) = qi(2) + 1.5_dp*qj(2)
2765 qj(3) = qi(3) + 1.5_dp*qj(3) - tr
2766 qj(4) = qi(4) + 1.5_dp*qj(4)
2767 qj(5) = qi(5) + 1.5_dp*qj(5)
2768 qj(6) = qi(6) + 1.5_dp*qj(6) - tr
2770 END SUBROUTINE tb_shift_multipole
2779 SUBROUTINE tb_ao_charges_matrix(p_mat, s_matrix, charges, para_env)
2781 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(INOUT) :: charges
2784 INTEGER :: i, iblock_col, iblock_row, j
2786 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: p_block, s_block
2792 NULLIFY (s_block, p_block)
2794 CALL dbcsr_get_block_p(matrix=p_mat, row=iblock_row, col=iblock_col, block=p_block, found=found)
2795 IF (.NOT. found) cycle
2796 IF (.NOT. (
ASSOCIATED(s_block) .AND.
ASSOCIATED(p_block))) cycle
2798 DO j = 1,
SIZE(p_block, 2)
2799 DO i = 1,
SIZE(p_block, 1)
2800 charges(i, iblock_row) = charges(i, iblock_row) + p_block(i, j)*s_block(i, j)
2803 IF (iblock_col /= iblock_row)
THEN
2804 DO j = 1,
SIZE(p_block, 2)
2805 DO i = 1,
SIZE(p_block, 1)
2806 charges(j, iblock_col) = charges(j, iblock_col) + p_block(i, j)*s_block(i, j)
2812 CALL para_env%sum(charges)
2814 END SUBROUTINE tb_ao_charges_matrix
2824 SUBROUTINE tb_ao_charges_kp_spin(p_matrix_kp, s_matrix_kp, charges, ispin, para_env)
2825 TYPE(
dbcsr_p_type),
DIMENSION(:, :),
POINTER :: p_matrix_kp, s_matrix_kp
2826 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(INOUT) :: charges
2827 INTEGER,
INTENT(IN) :: ispin
2831 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: image_charges
2835 ALLOCATE (image_charges(
SIZE(charges, 1),
SIZE(charges, 2)))
2836 DO ic = 1,
SIZE(s_matrix_kp, 2)
2837 NULLIFY (p_mat, s_mat)
2838 p_mat => p_matrix_kp(ispin, ic)%matrix
2839 s_mat => s_matrix_kp(1, ic)%matrix
2840 IF (
ASSOCIATED(p_mat) .AND.
ASSOCIATED(s_mat))
THEN
2841 image_charges = 0.0_dp
2842 CALL tb_ao_charges_matrix(p_mat, s_mat, image_charges, para_env)
2843 charges(:, :) = charges(:, :) + image_charges(:, :)
2846 DEALLOCATE (image_charges)
2848 END SUBROUTINE tb_ao_charges_kp_spin
2858 SUBROUTINE tb_contract_dens_matrix(p_mat, bra_mat, ket_mat, output, para_env)
2859 TYPE(
dbcsr_type),
POINTER :: p_mat, bra_mat, ket_mat
2860 REAL(kind=
dp),
DIMENSION(:),
INTENT(INOUT) :: output
2863 INTEGER :: i, iblock_col, iblock_row, j
2865 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: bra, ket, p_block
2871 NULLIFY (p_block, bra, ket)
2873 CALL dbcsr_get_block_p(matrix=p_mat, row=iblock_row, col=iblock_col, block=p_block, found=found)
2874 IF (.NOT. found) cycle
2875 CALL dbcsr_get_block_p(matrix=ket_mat, row=iblock_row, col=iblock_col, block=ket, found=found)
2876 IF (.NOT. found) cpabort(
"missing block")
2878 IF (.NOT. (
ASSOCIATED(bra) .AND.
ASSOCIATED(p_block))) cycle
2879 IF (iblock_col == iblock_row)
THEN
2880 DO j = 1,
SIZE(p_block, 1)
2881 DO i = 1,
SIZE(p_block, 2)
2882 output(iblock_row) = output(iblock_row) + p_block(j, i)*bra(j, i)
2886 DO j = 1,
SIZE(p_block, 1)
2887 DO i = 1,
SIZE(p_block, 2)
2888 output(iblock_row) = output(iblock_row) + p_block(j, i)*ket(j, i)
2891 DO j = 1,
SIZE(p_block, 1)
2892 DO i = 1,
SIZE(p_block, 2)
2893 output(iblock_col) = output(iblock_col) + p_block(j, i)*bra(j, i)
2899 CALL para_env%sum(output)
2901 END SUBROUTINE tb_contract_dens_matrix
2914 SUBROUTINE tb_contract_dens_kp_spin(p_matrix, bra_mat, ket_mat, iop, nops, output, ispin, para_env)
2915 TYPE(
dbcsr_p_type),
DIMENSION(:, :),
POINTER :: p_matrix
2916 TYPE(
dbcsr_p_type),
DIMENSION(:),
POINTER :: bra_mat, ket_mat
2917 INTEGER,
INTENT(IN) :: iop, nops
2918 REAL(kind=
dp),
DIMENSION(:),
INTENT(INOUT) :: output
2919 INTEGER,
INTENT(IN) :: ispin
2922 INTEGER :: ic,
idx, nimg
2923 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: image_output
2926 nimg =
SIZE(p_matrix, 2)
2928 ALLOCATE (image_output(
SIZE(output)))
2930 idx = iop + nops*(ic - 1)
2931 cpassert(
idx <=
SIZE(bra_mat))
2932 cpassert(
idx <=
SIZE(ket_mat))
2934 p_mat => p_matrix(ispin, ic)%matrix
2935 image_output = 0.0_dp
2936 CALL tb_contract_dens_matrix(p_mat, bra_mat(
idx)%matrix, ket_mat(
idx)%matrix, image_output, para_env)
2937 output = output + image_output
2939 DEALLOCATE (image_output)
2941 END SUBROUTINE tb_contract_dens_kp_spin
2954 SUBROUTINE contract_dens(p_matrix, bra_mat, ket_mat, output, para_env)
2956 TYPE(
dbcsr_type),
POINTER :: bra_mat, ket_mat
2957 REAL(kind=
dp),
DIMENSION(:),
INTENT(INOUT) :: output
2960 CHARACTER(len=*),
PARAMETER :: routinen =
'contract_dens'
2962 INTEGER :: handle, i, iblock_col, iblock_row, &
2965 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: bra, ket, p_block
2968 CALL timeset(routinen, handle)
2970 nspin =
SIZE(p_matrix)
2975 NULLIFY (p_block, bra, ket)
2979 row=iblock_row, col=iblock_col, block=p_block, found=found)
2980 IF (.NOT. found) cycle
2982 row=iblock_row, col=iblock_col, block=ket, found=found)
2983 IF (.NOT. found) cpabort(
"missing block")
2985 IF (.NOT. (
ASSOCIATED(bra) .AND.
ASSOCIATED(p_block))) cycle
2986 IF (iblock_col == iblock_row)
THEN
2987 DO j = 1,
SIZE(p_block, 1)
2988 DO i = 1,
SIZE(p_block, 2)
2989 output(iblock_row) = output(iblock_row) + p_block(j, i)*bra(j, i)
2993 DO j = 1,
SIZE(p_block, 1)
2994 DO i = 1,
SIZE(p_block, 2)
2995 output(iblock_row) = output(iblock_row) + p_block(j, i)*ket(j, i)
2998 DO j = 1,
SIZE(p_block, 1)
2999 DO i = 1,
SIZE(p_block, 2)
3000 output(iblock_col) = output(iblock_col) + p_block(j, i)*bra(j, i)
3008 CALL para_env%sum(output)
3009 CALL timestop(handle)
3011 END SUBROUTINE contract_dens
3023 SUBROUTINE contract_dens_kp(p_matrix, bra_mat, ket_mat, iop, nops, output, para_env)
3024 TYPE(
dbcsr_p_type),
DIMENSION(:, :),
POINTER :: p_matrix
3025 TYPE(
dbcsr_p_type),
DIMENSION(:),
POINTER :: bra_mat, ket_mat
3026 INTEGER,
INTENT(IN) :: iop, nops
3027 REAL(kind=
dp),
DIMENSION(:),
INTENT(INOUT) :: output
3030 INTEGER :: ic,
idx, nimg
3031 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: image_output
3034 nimg =
SIZE(p_matrix, 2)
3036 ALLOCATE (image_output(
SIZE(output)))
3038 idx = iop + nops*(ic - 1)
3039 cpassert(
idx <=
SIZE(bra_mat))
3040 cpassert(
idx <=
SIZE(ket_mat))
3042 p_image => p_matrix(:, ic)
3043 image_output = 0.0_dp
3044 CALL contract_dens(p_image, bra_mat(
idx)%matrix, ket_mat(
idx)%matrix, image_output, para_env)
3045 output = output + image_output
3047 DEALLOCATE (image_output)
3049 END SUBROUTINE contract_dens_kp
3059 SUBROUTINE tb_grad2force(qs_env, tb, para_env, ityp)
3066 CHARACTER(len=*),
PARAMETER :: routinen =
'tb_grad2force'
3068 CHARACTER(LEN=default_path_length) :: dump_file
3069 INTEGER :: atoma, dump_status, dump_unit, handle, &
3071 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: atom_of_kind, kind_of
3076 CALL timeset(routinen, handle)
3078 NULLIFY (force, atomic_kind_set)
3079 CALL get_qs_env(qs_env=qs_env, force=force, particle_set=particle_set, &
3080 atomic_kind_set=atomic_kind_set)
3082 atom_of_kind=atom_of_kind, kind_of=kind_of)
3084 natom =
SIZE(particle_set)
3087#if defined(__TBLITE_DEBUG_DIAGNOSTICS)
3088 CALL get_environment_variable(
"CP2K_TBLITE_FORCE_DUMP", dump_file, status=dump_status)
3090 IF (dump_status == 0)
THEN
3091 OPEN (newunit=dump_unit, file=trim(dump_file), status=
"UNKNOWN", &
3092 position=
"APPEND", action=
"WRITE")
3093 WRITE (dump_unit,
"(A,1X,I0)")
"component", ityp
3095 WRITE (dump_unit,
"(I0,3(1X,ES24.16))") iatom, tb%grad(:, iatom)/para_env%num_pe
3102 cpabort(
"unknown force type")
3105 ikind = kind_of(iatom)
3106 atoma = atom_of_kind(iatom)
3107 force(ikind)%all_potential(:, atoma) = &
3108 force(ikind)%all_potential(:, atoma) + tb%grad(:, iatom)/para_env%num_pe
3112 ikind = kind_of(iatom)
3113 atoma = atom_of_kind(iatom)
3114 force(ikind)%repulsive(:, atoma) = &
3115 force(ikind)%repulsive(:, atoma) + tb%grad(:, iatom)/para_env%num_pe
3119 ikind = kind_of(iatom)
3120 atoma = atom_of_kind(iatom)
3121 force(ikind)%dispersion(:, atoma) = &
3122 force(ikind)%dispersion(:, atoma) + tb%grad(:, iatom)/para_env%num_pe
3126 ikind = kind_of(iatom)
3127 atoma = atom_of_kind(iatom)
3128 force(ikind)%rho_elec(:, atoma) = &
3129 force(ikind)%rho_elec(:, atoma) + tb%grad(:, iatom)/para_env%num_pe
3133 ikind = kind_of(iatom)
3134 atoma = atom_of_kind(iatom)
3135 force(ikind)%overlap(:, atoma) = &
3136 force(ikind)%overlap(:, atoma) + tb%grad(:, iatom)/para_env%num_pe
3140 ikind = kind_of(iatom)
3141 atoma = atom_of_kind(iatom)
3142 force(ikind)%efield(:, atoma) = &
3143 force(ikind)%efield(:, atoma) + tb%grad(:, iatom)/para_env%num_pe
3147 CALL timestop(handle)
3149 END SUBROUTINE tb_grad2force
3156 SUBROUTINE tb_zero_force(qs_env)
3160 INTEGER :: iatom, ikind, natom
3161 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: kind_of
3166 NULLIFY (force, atomic_kind_set)
3167 CALL get_qs_env(qs_env=qs_env, force=force, particle_set=particle_set, &
3168 atomic_kind_set=atomic_kind_set)
3172 natom =
SIZE(particle_set)
3175 ikind = kind_of(iatom)
3176 force(ikind)%all_potential = 0.0_dp
3177 force(ikind)%repulsive = 0.0_dp
3178 force(ikind)%dispersion = 0.0_dp
3179 force(ikind)%rho_elec = 0.0_dp
3180 force(ikind)%overlap = 0.0_dp
3181 force(ikind)%efield = 0.0_dp
3184 END SUBROUTINE tb_zero_force
3195 LOGICAL,
INTENT(IN) :: use_rho
3196 INTEGER,
INTENT(IN) :: nimg
3198#if defined(__TBLITE)
3199 INTEGER :: i, iatom, ic, ikind, ind, is, ish, ispin, &
3201 INTEGER,
DIMENSION(3) :: cellind
3202 INTEGER,
DIMENSION(:, :, :),
POINTER :: cell_to_index
3204 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: de
3205 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: pblock
3206 TYPE(
dbcsr_p_type),
DIMENSION(:, :),
POINTER :: matrix_p
3210 DIMENSION(:),
POINTER :: nl_iterator
3220 NULLIFY (scf_env, rho, tb, sab_orb, sab_kp, para_env, kpoints)
3221 CALL get_qs_env(qs_env=qs_env, scf_env=scf_env, rho=rho, tb_tblite=tb, &
3222 sab_orb=sab_orb, sab_kp=sab_kp, para_env=para_env, kpoints=kpoints)
3224 NULLIFY (cell_to_index)
3226 IF (.NOT.
ASSOCIATED(sab_kp)) cpabort(
"Missing tblite k-point neighbor list")
3234 ELSEIF (
ASSOCIATED(tb%rho_ao_kp_ref))
THEN
3235 matrix_p => tb%rho_ao_kp_ref
3237 matrix_p => scf_env%p_mix_new
3240 ALLOCATE (de(tb%mol%nat))
3244 NULLIFY (nl_iterator)
3248 iatom=iatom, jatom=jatom, cell=cellind)
3250 IF (iatom /= jatom) cycle
3252 IF (ikind /= jkind) cpabort(
"Type wrong")
3254 is = tb%calc%bas%ish_at(iatom)
3259 ic = cell_to_index(cellind(1), cellind(2), cellind(3))
3263 IF (cellind(1) /= 0) cycle
3264 IF (cellind(2) /= 0) cycle
3265 IF (cellind(3) /= 0) cycle
3267 DO ispin = 1,
SIZE(matrix_p, 1)
3269 row=iatom, col=jatom, block=pblock, found=found)
3271 IF (.NOT. found) cpabort(
"block not found")
3274 DO ish = 1, tb%calc%bas%nsh_id(ikind)
3275 DO i = 1, msao(tb%calc%bas%cgto(ish, ikind)%ang)
3277 de(iatom) = de(iatom) + tb%dsedcn(is + ish)*pblock(ind, ind)
3283 CALL para_env%sum(de)
3286 CALL tb_add_grad(tb%grad, tb%dcndr, de, tb%mol%nat)
3287 IF (tb%use_virial)
CALL tb_add_sig(tb%sigma, tb%dcndL, de, tb%mol%nat)
3288 CALL tb_grad2force(qs_env, tb, para_env, 4)
3295 cpabort(
"Built without TBLITE")
3309 LOGICAL,
INTENT(IN) :: use_rho
3310 INTEGER,
INTENT(IN) :: nimg
3312#if defined(__TBLITE)
3313 INTEGER :: i, ij, iatom, ic, icol, ikind, ni, nj, nkind, nel, &
3314 sgfi, sgfj, ityp, jatom, jkind, jrow, jtyp, iset, jset, &
3315 nseti, nsetj, ia, ib, inda, indb, fold_index, &
3316 has_nyquist_image, n_cn_norm_images, n_periodic, &
3317 n_non_nyquist_images, &
3318 n_sampled_kpoints, sampled_axes, sampled_even_axes, &
3319 sampled_non_nyquist_axes, sampled_odd_axes, nspin
3320 INTEGER,
DIMENSION(3) :: cellind, fold_shape, nkp_cellind, nkp_grid
3321 INTEGER,
DIMENSION(:),
POINTER :: nsgfa, nsgfb
3322 INTEGER,
DIMENSION(:, :),
POINTER :: first_sgfa, first_sgfb
3323 INTEGER,
DIMENSION(:, :, :),
POINTER :: cell_to_index
3324 LOGICAL :: found, has_multipole_response, image_pair, kpoint_image_pair, &
3326 REAL(kind=
dp) :: r2, dr, itemp, jtemp, rr, hij, shpoly, dshpoly, idhdc, jdhdc, &
3327 scal, hp, i_a_shift, j_a_shift, i_a_shift_mag, j_a_shift_mag, &
3328 ishift, jshift, ishift_mag, jshift_mag, pij_charge, &
3329 pij_magnet, wij_charge, &
3330 dip_deriv_fac, quad_deriv_fac, dip_deriv_fac_pair, &
3331 quad_deriv_fac_pair, overlap_deriv_fac, pot_deriv_scale, &
3332 w_deriv_scale, h0_deriv_scale, mp_deriv_scale, &
3333 pot_image_deriv_scale, w_image_deriv_scale, &
3334 h0_image_deriv_scale, mp_image_deriv_scale, &
3335 pot_pair_scale, w_pair_scale, h0_pair_scale, mp_pair_scale, &
3336 pot_self_image_deriv_scale, w_self_image_deriv_scale, &
3337 h0_self_image_deriv_scale, mp_self_image_deriv_scale, &
3338 cn_image_scale, cn_deriv_scale, &
3339 nyquist_self_image_deriv_scale
3340#if defined(__TBLITE_DEBUG_DIAGNOSTICS)
3341 INTEGER :: debug_status
3342 CHARACTER(LEN=32) :: scale_value
3344 REAL(kind=
dp),
DIMENSION(3) :: rij, dgrad
3345 REAL(kind=
dp),
DIMENSION(3, 3) :: hsigma
3346 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: de, t_ov, idip, jdip, idip_mag, jdip_mag, &
3347 iquad, jquad, iquad_mag, jquad_mag
3348 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: non_nyquist_folded_seen
3349 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: t_dip, t_quad, t_d_ov
3350 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :, :) :: t_i_dip, t_i_quad, t_j_dip, t_j_quad
3351 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: pblock, pblock_beta, wblock, wblock_beta
3354 TYPE(
dbcsr_p_type),
DIMENSION(:, :),
POINTER :: matrix_p, matrix_w
3360 DIMENSION(:),
POINTER :: nl_iterator
3365 TYPE(
qs_kind_type),
DIMENSION(:),
POINTER :: qs_kind_set
3368 TYPE(tb_hamiltonian),
POINTER :: h0
3372 NULLIFY (scf_env, rho, tb, sab_orb, sab_kp, para_env, kpoints, matrix_w)
3374 atomic_kind_set=atomic_kind_set, &
3380 para_env=para_env, &
3381 qs_kind_set=qs_kind_set, &
3383 matrix_w_kp=matrix_w)
3385 NULLIFY (cell_to_index)
3387 IF (.NOT.
ASSOCIATED(sab_kp)) cpabort(
"Missing tblite k-point neighbor list")
3389 CALL get_kpoint_info(kpoint=kpoints, cell_to_index=cell_to_index, nkp_grid=nkp_grid)
3393 sampled_odd_axes = 0
3394 IF (nkp_grid(1) > 1 .AND.
modulo(nkp_grid(1), 2) == 1) sampled_odd_axes = sampled_odd_axes + 1
3395 IF (nkp_grid(2) > 1 .AND.
modulo(nkp_grid(2), 2) == 1) sampled_odd_axes = sampled_odd_axes + 1
3396 IF (nkp_grid(3) > 1 .AND.
modulo(nkp_grid(3), 2) == 1) sampled_odd_axes = sampled_odd_axes + 1
3397 n_periodic = count(tb%mol%periodic)
3398 n_sampled_kpoints = 1
3400 IF (tb%mol%periodic(i)) n_sampled_kpoints = n_sampled_kpoints*nkp_grid(i)
3404 has_multipole_response =
ASSOCIATED(tb%dipbra) .OR.
ASSOCIATED(tb%quadbra)
3405 dip_deriv_fac = -1.0_dp
3406 quad_deriv_fac = -1.0_dp
3407 pot_deriv_scale = 1.0_dp
3408 w_deriv_scale = 1.0_dp
3409 h0_deriv_scale = 1.0_dp
3410 mp_deriv_scale = 1.0_dp
3411#if defined(__TBLITE_DEBUG_DIAGNOSTICS)
3412 CALL get_environment_variable(
"CP2K_TBLITE_DH_POT_SCALE", scale_value, status=debug_status)
3413 IF (debug_status == 0)
READ (scale_value, *, iostat=debug_status) pot_deriv_scale
3414 CALL get_environment_variable(
"CP2K_TBLITE_DH_W_SCALE", scale_value, status=debug_status)
3415 IF (debug_status == 0)
READ (scale_value, *, iostat=debug_status) w_deriv_scale
3416 CALL get_environment_variable(
"CP2K_TBLITE_DH_H0_SCALE", scale_value, status=debug_status)
3417 IF (debug_status == 0)
READ (scale_value, *, iostat=debug_status) h0_deriv_scale
3418 CALL get_environment_variable(
"CP2K_TBLITE_DH_MP_SCALE", scale_value, status=debug_status)
3419 IF (debug_status == 0)
READ (scale_value, *, iostat=debug_status) mp_deriv_scale
3421 pot_image_deriv_scale = pot_deriv_scale
3422 w_image_deriv_scale = w_deriv_scale
3423 h0_image_deriv_scale = h0_deriv_scale
3424 mp_image_deriv_scale = mp_deriv_scale
3425#if defined(__TBLITE_DEBUG_DIAGNOSTICS)
3426 CALL get_environment_variable(
"CP2K_TBLITE_DH_POT_IMAGE_SCALE", scale_value, status=debug_status)
3427 IF (debug_status == 0)
READ (scale_value, *, iostat=debug_status) pot_image_deriv_scale
3428 CALL get_environment_variable(
"CP2K_TBLITE_DH_W_IMAGE_SCALE", scale_value, status=debug_status)
3429 IF (debug_status == 0)
READ (scale_value, *, iostat=debug_status) w_image_deriv_scale
3430 CALL get_environment_variable(
"CP2K_TBLITE_DH_H0_IMAGE_SCALE", scale_value, status=debug_status)
3431 IF (debug_status == 0)
READ (scale_value, *, iostat=debug_status) h0_image_deriv_scale
3432 CALL get_environment_variable(
"CP2K_TBLITE_DH_MP_IMAGE_SCALE", scale_value, status=debug_status)
3433 IF (debug_status == 0)
READ (scale_value, *, iostat=debug_status) mp_image_deriv_scale
3435 pot_self_image_deriv_scale = pot_image_deriv_scale
3436 w_self_image_deriv_scale = w_image_deriv_scale
3437 h0_self_image_deriv_scale = h0_image_deriv_scale
3438 mp_self_image_deriv_scale = mp_image_deriv_scale
3444 IF (nimg > 1 .AND. any(nkp_grid > 1) .AND. has_multipole_response) &
3445 mp_self_image_deriv_scale = -mp_image_deriv_scale
3446#if defined(__TBLITE_DEBUG_DIAGNOSTICS)
3447 CALL get_environment_variable(
"CP2K_TBLITE_DH_POT_SELF_IMAGE_SCALE", scale_value, status=debug_status)
3448 IF (debug_status == 0)
READ (scale_value, *, iostat=debug_status) pot_self_image_deriv_scale
3449 CALL get_environment_variable(
"CP2K_TBLITE_DH_W_SELF_IMAGE_SCALE", scale_value, status=debug_status)
3450 IF (debug_status == 0)
READ (scale_value, *, iostat=debug_status) w_self_image_deriv_scale
3451 CALL get_environment_variable(
"CP2K_TBLITE_DH_H0_SELF_IMAGE_SCALE", scale_value, status=debug_status)
3452 IF (debug_status == 0)
READ (scale_value, *, iostat=debug_status) h0_self_image_deriv_scale
3453 CALL get_environment_variable(
"CP2K_TBLITE_DH_MP_SELF_IMAGE_SCALE", scale_value, status=debug_status)
3454 IF (debug_status == 0)
READ (scale_value, *, iostat=debug_status) mp_self_image_deriv_scale
3456 cn_image_scale = 1.0_dp
3457 IF (nimg > 1 .AND. any(tb%mol%periodic)) cn_image_scale = -0.25_dp
3458 cn_deriv_scale = 1.0_dp
3459#if defined(__TBLITE_DEBUG_DIAGNOSTICS)
3460 CALL get_environment_variable(
"CP2K_TBLITE_DH_CN_IMAGE_SCALE", scale_value, status=debug_status)
3461 IF (debug_status == 0)
READ (scale_value, *, iostat=debug_status) cn_image_scale
3467 ELSEIF (
ASSOCIATED(tb%rho_ao_kp_ref))
THEN
3468 matrix_p => tb%rho_ao_kp_ref
3470 matrix_p => scf_env%p_mix_new
3472 nspin =
SIZE(matrix_p, 1)
3475 nkind =
SIZE(atomic_kind_set)
3476 ALLOCATE (basis_set_list(nkind))
3479 ALLOCATE (de(tb%mol%nat))
3481 nel = msao(tb%calc%bas%maxl)**2
3482 ALLOCATE (t_ov(nel))
3483 ALLOCATE (t_d_ov(3, nel))
3484 ALLOCATE (t_dip(dip_n, nel))
3485 ALLOCATE (t_i_dip(3, dip_n, nel), t_j_dip(3, dip_n, nel))
3486 ALLOCATE (t_quad(quad_n, nel))
3487 ALLOCATE (t_i_quad(3, quad_n, nel), t_j_quad(3, quad_n, nel))
3489 ALLOCATE (idip(dip_n), jdip(dip_n), idip_mag(dip_n), jdip_mag(dip_n))
3490 ALLOCATE (iquad(quad_n), jquad(quad_n), iquad_mag(quad_n), jquad_mag(quad_n))
3495 fold_shape = 2*nkp_grid + 1
3496 ALLOCATE (non_nyquist_folded_seen(product(fold_shape)))
3497 non_nyquist_folded_seen = 0
3498 has_nyquist_image = 0
3500 NULLIFY (nl_iterator)
3504 iatom=iatom, jatom=jatom, r=rij, cell=cellind)
3506 icol = max(iatom, jatom)
3507 jrow = min(iatom, jatom)
3509 IF (iatom < jatom)
THEN
3516 ityp = tb%mol%id(icol)
3517 jtyp = tb%mol%id(jrow)
3519 r2 = dot_product(rij, rij)
3521 IF (icol == jrow .AND. dr < same_atom) cycle
3522 rr = sqrt(dr/(h0%rad(ikind) + h0%rad(jkind)))
3525 basis_set_a => basis_set_list(ikind)%gto_basis_set
3526 IF (.NOT.
ASSOCIATED(basis_set_a)) cycle
3527 first_sgfa => basis_set_a%first_sgf
3528 nsgfa => basis_set_a%nsgf_set
3529 nseti = basis_set_a%nset
3530 basis_set_b => basis_set_list(jkind)%gto_basis_set
3531 IF (.NOT.
ASSOCIATED(basis_set_b)) cycle
3532 first_sgfb => basis_set_b%first_sgf
3533 nsgfb => basis_set_b%nsgf_set
3534 nsetj = basis_set_b%nset
3539 ic = cell_to_index(cellind(1), cellind(2), cellind(3))
3542 image_pair = (cellind(1) /= 0 .OR. cellind(2) /= 0 .OR. cellind(3) /= 0)
3545 IF (nkp_grid(i) > 1)
THEN
3546 nkp_cellind(i) =
modulo(cellind(i), nkp_grid(i))
3547 IF (2*nkp_cellind(i) > nkp_grid(i)) nkp_cellind(i) = nkp_cellind(i) - nkp_grid(i)
3551 IF (nkp_cellind(1) /= 0) sampled_axes = sampled_axes + 1
3552 IF (nkp_cellind(2) /= 0) sampled_axes = sampled_axes + 1
3553 IF (nkp_cellind(3) /= 0) sampled_axes = sampled_axes + 1
3554 kpoint_image_pair = image_pair .AND. sampled_axes > 0
3555 sampled_even_axes = 0
3556 IF (nkp_grid(1) > 1 .AND.
modulo(nkp_grid(1), 2) == 0 .AND. &
3557 abs(2*nkp_cellind(1)) == nkp_grid(1)) sampled_even_axes = sampled_even_axes + 1
3558 IF (nkp_grid(2) > 1 .AND.
modulo(nkp_grid(2), 2) == 0 .AND. &
3559 abs(2*nkp_cellind(2)) == nkp_grid(2)) sampled_even_axes = sampled_even_axes + 1
3560 IF (nkp_grid(3) > 1 .AND.
modulo(nkp_grid(3), 2) == 0 .AND. &
3561 abs(2*nkp_cellind(3)) == nkp_grid(3)) sampled_even_axes = sampled_even_axes + 1
3562 sampled_non_nyquist_axes = max(0, sampled_axes - sampled_even_axes)
3563 IF (kpoint_image_pair .AND. sampled_even_axes > 0) has_nyquist_image = 1
3564 IF (kpoint_image_pair .AND. sampled_non_nyquist_axes > 0 .AND. sampled_even_axes == 0)
THEN
3565 fold_index = 1 + (nkp_cellind(1) + nkp_grid(1)) &
3566 + fold_shape(1)*(nkp_cellind(2) + nkp_grid(2)) &
3567 + fold_shape(1)*fold_shape(2)*(nkp_cellind(3) + nkp_grid(3))
3568 non_nyquist_folded_seen(fold_index) = 1
3570 sampled_image_pair = kpoint_image_pair .AND. sampled_even_axes > 0
3571 pot_pair_scale = merge(pot_image_deriv_scale, pot_deriv_scale, image_pair)
3572 w_pair_scale = merge(w_image_deriv_scale, w_deriv_scale, image_pair)
3573 h0_pair_scale = merge(h0_image_deriv_scale, h0_deriv_scale, image_pair)
3574 mp_pair_scale = merge(mp_image_deriv_scale, mp_deriv_scale, image_pair)
3575 IF (icol == jrow .AND. sampled_image_pair)
THEN
3576 pot_pair_scale = pot_self_image_deriv_scale
3577 w_pair_scale = w_self_image_deriv_scale
3581 nyquist_self_image_deriv_scale = 1.0_dp
3582 IF (has_multipole_response)
THEN
3583 IF (n_periodic > 1) &
3584 nyquist_self_image_deriv_scale = 1.0_dp + real(n_periodic,
dp)/ &
3585 REAL(n_sampled_kpoints*(1 + dip_n + quad_n),
dp)
3586 ELSEIF (n_periodic == 3)
THEN
3587 nyquist_self_image_deriv_scale = 1.0_dp + 1.0_dp/real(n_sampled_kpoints*n_periodic,
dp)
3589 w_pair_scale = w_pair_scale*nyquist_self_image_deriv_scale
3593 w_pair_scale = w_pair_scale*(1.0_dp - 0.125_dp*real(sampled_odd_axes,
dp))
3594 h0_pair_scale = h0_self_image_deriv_scale
3595 mp_pair_scale = mp_self_image_deriv_scale
3598 NULLIFY (pblock, pblock_beta)
3600 row=jrow, col=icol, block=pblock, found=found)
3601 IF (.NOT. found) cpabort(
"pblock not found")
3604 row=jrow, col=icol, block=pblock_beta, found=found)
3605 IF (.NOT. found) cpabort(
"pblock beta not found")
3608 NULLIFY (wblock, wblock_beta)
3610 row=jrow, col=icol, block=wblock, found=found)
3614 row=jrow, col=icol, block=wblock_beta, found=found)
3618 i_a_shift = tb%pot%vat(icol, 1)
3619 j_a_shift = tb%pot%vat(jrow, 1)
3620 i_a_shift_mag = 0.0_dp
3621 j_a_shift_mag = 0.0_dp
3622 IF (
SIZE(tb%pot%vat, 2) > 1)
THEN
3623 i_a_shift_mag = tb%pot%vat(icol, 2)
3624 j_a_shift_mag = tb%pot%vat(jrow, 2)
3626 idip(:) = tb%pot%vdp(:, icol, 1)
3627 jdip(:) = tb%pot%vdp(:, jrow, 1)
3628 idip_mag(:) = 0.0_dp
3629 jdip_mag(:) = 0.0_dp
3630 IF (
SIZE(tb%pot%vdp, 3) > 1)
THEN
3631 idip_mag(:) = tb%pot%vdp(:, icol, 2)
3632 jdip_mag(:) = tb%pot%vdp(:, jrow, 2)
3634 iquad(:) = tb%pot%vqp(:, icol, 1)
3635 jquad(:) = tb%pot%vqp(:, jrow, 1)
3636 iquad_mag(:) = 0.0_dp
3637 jquad_mag(:) = 0.0_dp
3638 IF (
SIZE(tb%pot%vqp, 3) > 1)
THEN
3639 iquad_mag(:) = tb%pot%vqp(:, icol, 2)
3640 jquad_mag(:) = tb%pot%vqp(:, jrow, 2)
3642 dip_deriv_fac_pair = dip_deriv_fac
3643 quad_deriv_fac_pair = quad_deriv_fac
3644 overlap_deriv_fac = 1.0_dp
3646 ni = tb%calc%bas%ish_at(icol)
3648 sgfi = first_sgfa(1, iset)
3649 ishift = i_a_shift + tb%pot%vsh(ni + iset, 1)
3651 IF (
SIZE(tb%pot%vsh, 2) > 1) ishift_mag = i_a_shift_mag + tb%pot%vsh(ni + iset, 2)
3652 nj = tb%calc%bas%ish_at(jrow)
3654 sgfj = first_sgfb(1, jset)
3655 jshift = j_a_shift + tb%pot%vsh(nj + jset, 1)
3657 IF (
SIZE(tb%pot%vsh, 2) > 1) jshift_mag = j_a_shift_mag + tb%pot%vsh(nj + jset, 2)
3660 CALL multipole_grad_cgto(tb%calc%bas%cgto(iset, ityp), tb%calc%bas%cgto(jset, jtyp), &
3661 & r2, rij, tb%calc%bas%intcut, t_ov, t_dip, t_quad, t_d_ov, t_i_dip, t_i_quad, &
3662 & t_j_dip, t_j_quad)
3664 shpoly = (1.0_dp + h0%shpoly(iset, ikind)*rr) &
3665 *(1.0_dp + h0%shpoly(jset, jkind)*rr)
3666 dshpoly = ((1.0_dp + h0%shpoly(iset, ikind)*rr)*h0%shpoly(jset, jkind)*rr &
3667 + (1.0_dp + h0%shpoly(jset, jkind)*rr)*h0%shpoly(iset, ikind)*rr) &
3669 scal = h0%hscale(iset, jset, ikind, jkind)
3670 hij = 0.5_dp*(tb%selfenergy(ni + iset) + tb%selfenergy(nj + jset))*scal
3672 idhdc = tb%dsedcn(ni + iset)*shpoly*scal
3673 jdhdc = tb%dsedcn(nj + jset)*shpoly*scal
3678 DO inda = 1, nsgfa(iset)
3679 ia = first_sgfa(1, iset) - first_sgfa(1, 1) + inda
3680 DO indb = 1, nsgfb(jset)
3681 ib = first_sgfb(1, jset) - first_sgfb(1, 1) + indb
3683 ij = inda + nsgfa(iset)*(indb - 1)
3685 pij_charge = pblock(ib, ia)
3687 wij_charge = wblock(ib, ia)
3689 pij_charge = pij_charge + pblock_beta(ib, ia)
3690 pij_magnet = pblock(ib, ia) - pblock_beta(ib, ia)
3695 itemp = itemp + overlap_deriv_fac*idhdc*pij_charge*t_ov(ij)
3696 jtemp = jtemp + overlap_deriv_fac*jdhdc*pij_charge*t_ov(ij)
3698 hp = 2*hij*pij_charge
3699 dgrad(:) = dgrad(:) &
3700 + overlap_deriv_fac*(-pot_pair_scale*((ishift + jshift)*pij_charge &
3701 + (ishift_mag + jshift_mag)*pij_magnet)*t_d_ov(:, ij) &
3702 - 2*w_pair_scale*wij_charge*t_d_ov(:, ij) &
3703 + h0_pair_scale*(hp*shpoly*t_d_ov(:, ij) &
3704 + hp*dshpoly*t_ov(ij)*rij)) &
3705 + mp_pair_scale*(pij_charge*( &
3706 dip_deriv_fac_pair*(matmul(t_i_dip(:, :, ij), idip) &
3707 + matmul(t_j_dip(:, :, ij), jdip)) &
3708 + quad_deriv_fac_pair*(matmul(t_i_quad(:, :, ij), iquad) &
3709 + matmul(t_j_quad(:, :, ij), jquad))) &
3711 dip_deriv_fac_pair*(matmul(t_i_dip(:, :, ij), idip_mag) &
3712 + matmul(t_j_dip(:, :, ij), jdip_mag)) &
3713 + quad_deriv_fac_pair*(matmul(t_i_quad(:, :, ij), iquad_mag) &
3714 + matmul(t_j_quad(:, :, ij), jquad_mag))))
3719 IF (icol == jrow)
THEN
3720 IF (sampled_image_pair)
THEN
3721 de(icol) = de(icol) + cn_image_scale*0.5_dp*(itemp + jtemp)
3723 de(icol) = de(icol) + 0.5_dp*(itemp + jtemp)
3726 de(icol) = de(icol) + itemp
3727 de(jrow) = de(jrow) + jtemp
3729 tb%grad(:, icol) = tb%grad(:, icol) - dgrad
3730 tb%grad(:, jrow) = tb%grad(:, jrow) + dgrad
3731 IF (tb%use_virial)
THEN
3732 IF (icol == jrow)
THEN
3735 IF (sampled_image_pair)
THEN
3736 hsigma(ia, ib) = hsigma(ia, ib) - 0.25_dp*(rij(ia)*dgrad(ib) + rij(ib)*dgrad(ia))
3738 hsigma(ia, ib) = hsigma(ia, ib) + 0.25_dp*(rij(ia)*dgrad(ib) + rij(ib)*dgrad(ia))
3745 hsigma(ia, ib) = hsigma(ia, ib) + 0.50_dp*(rij(ia)*dgrad(ib) + rij(ib)*dgrad(ia))
3755 CALL para_env%sum(hsigma)
3756 CALL para_env%sum(de)
3757 CALL para_env%max(non_nyquist_folded_seen)
3758 CALL para_env%max(has_nyquist_image)
3759 n_non_nyquist_images = count(non_nyquist_folded_seen > 0)
3760 n_cn_norm_images = 1 + n_non_nyquist_images + has_nyquist_image
3764 IF (has_multipole_response .AND. n_periodic == 3 .AND. n_non_nyquist_images > 0) &
3765 cn_deriv_scale = 1.0_dp - 0.5_dp/real(n_cn_norm_images,
dp)
3766 de = cn_deriv_scale*de
3767 CALL para_env%sum(tb%grad)
3769 CALL tb_add_grad(tb%grad, tb%dcndr, de, tb%mol%nat)
3770 CALL tb_grad2force(qs_env, tb, para_env, 4)
3772 CALL tb_dump_sigma_component(
"before_h0_off", tb%sigma, para_env)
3773 tb%sigma = tb%sigma + hsigma
3774 CALL tb_dump_sigma_component(
"after_h0_off_direct", tb%sigma, para_env)
3775 IF (tb%use_virial)
THEN
3776 CALL tb_add_sig(tb%sigma, tb%dcndL, de, tb%mol%nat)
3777 CALL tb_dump_sigma_component(
"after_h0_off_cn", tb%sigma, para_env)
3781 DEALLOCATE (non_nyquist_folded_seen)
3782 DEALLOCATE (basis_set_list)
3783 DEALLOCATE (t_ov, t_d_ov)
3784 DEALLOCATE (t_dip, t_i_dip, t_j_dip)
3785 DEALLOCATE (t_quad, t_i_quad, t_j_quad)
3786 DEALLOCATE (idip, jdip, idip_mag, jdip_mag, iquad, jquad, iquad_mag, jquad_mag)
3788 IF (tb%use_virial)
CALL tb_add_stress(qs_env, tb, para_env)
3794 cpabort(
"Built without TBLITE")
3807 CHARACTER(LEN=*),
PARAMETER :: routinen =
'tb_reference_cli_compare'
3809 CHARACTER(LEN=16) :: solvation_model_name
3810 CHARACTER(LEN=32) :: acc_str, charge_str, efield_x_str, efield_y_str, efield_z_str, &
3811 etemp_guess_val_str, etemp_str, iter_str, spin_str, spinpol_str
3812 CHARACTER(LEN=4*default_path_length+16) :: efield_str, etemp_guess_str, param_str, &
3813 post_processing_output_str, post_processing_str, restart_str, solvation_str, verbosity_str
3814 CHARACTER(LEN=8) :: guess, method, solver
3815 CHARACTER(LEN=8*default_path_length) :: command
3816 CHARACTER(LEN=default_path_length) :: file_base, gen_file, grad_file, &
3817 json_file, log_file, &
3818 post_processing_output_file
3819 INTEGER :: cmdstat, exitstat, handle, iounit, &
3820 n_periodic, natom, nkp, &
3821 reference_iterations, spin
3822 INTEGER,
DIMENSION(3) :: periodic
3823 LOGICAL :: do_kpoints, have_energy, have_gradient, &
3824 have_virial, too_large, &
3826 REAL(kind=
dp) :: cli_energy, cp_energy, ediff, etemp, &
3827 etemp_guess, fmax, fsum, vmax, vsum
3828 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: cli_gradient, cli_virial, cp_gradient
3829 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: xkp
3843 CALL timeset(routinen, handle)
3845 NULLIFY (atomic_kind_set, cell, dft_control, energy, force, kpoints, logger, para_env, particle_set, &
3846 scf_control, virial, xkp)
3847 CALL get_qs_env(qs_env=qs_env, atomic_kind_set=atomic_kind_set, cell=cell, &
3848 dft_control=dft_control, energy=energy, force=force, &
3849 para_env=para_env, particle_set=particle_set, scf_control=scf_control, &
3850 virial=virial, do_kpoints=do_kpoints, kpoints=kpoints)
3852 ref = dft_control%qs_control%xtb_control%reference_cli
3853 IF (.NOT. ref%enabled)
THEN
3854 CALL timestop(handle)
3857 IF (.NOT. para_env%is_source())
THEN
3858 CALL timestop(handle)
3866 verbosity_str =
" --silent"
3869 verbosity_str =
" --verbose"
3871 IF (ref%solvation_active)
THEN
3873 IF (
ASSOCIATED(cell))
CALL get_cell(cell=cell, periodic=periodic)
3874 n_periodic = count(periodic == 1)
3875 IF (n_periodic == 3)
THEN
3876 WRITE (unit=iounit, fmt=
"(/,T2,A)") &
3877 "tblite reference CLI implicit solvation is not supported for PERIODIC XYZ."
3878 WRITE (unit=iounit, fmt=
"(T2,A)") &
3879 "Use PERIODIC NONE for molecular solvation diagnostics, or remove IMPLICIT_SOLVATION."
3880 cpabort(
"REFERENCE_CLI implicit solvation is incompatible with PERIODIC XYZ")
3881 ELSE IF (n_periodic > 0)
THEN
3882 WRITE (unit=iounit, fmt=
"(/,T2,A)") &
3883 "WARNING: tblite reference CLI implicit solvation with finite periodicity is diagnostic only."
3884 WRITE (unit=iounit, fmt=
"(T2,A,I0,A)") &
3885 "The generated native tblite reference geometry has ", n_periodic, &
3886 " periodic direction(s); continuum-solvation conventions are primarily molecular."
3889 unsupported_kpoints = .false.
3890 IF (do_kpoints .AND.
ASSOCIATED(kpoints))
THEN
3893 unsupported_kpoints = nkp > 1
3894 IF (nkp == 1 .AND.
ASSOCIATED(xkp)) unsupported_kpoints = any(abs(xkp(:, 1)) > 1.0e-12_dp)
3896 IF (unsupported_kpoints)
THEN
3897 WRITE (unit=iounit, fmt=
"(/,T2,A)") &
3898 "tblite reference CLI check skipped: CP2K KPOINTS are active."
3899 WRITE (unit=iounit, fmt=
"(T2,A)") &
3900 "The native tblite CLI reference path does not reproduce CP2K multi-k-point sampling."
3901 IF (ref%stop_on_error) cpabort(
"tblite reference CLI cannot check CP2K k-point calculations")
3902 CALL timestop(handle)
3906 WRITE (unit=iounit, fmt=
"(/,T2,A)") &
3907 "WARNING: tblite reference CLI cannot reproduce XTB/SCC_MIXER CP2K."
3908 WRITE (unit=iounit, fmt=
"(T2,A)") &
3909 "The external native tblite run uses tblite's own SCC mixer; only the converged result is compared."
3910 IF (ref%stop_on_error) cpabort(
"tblite reference CLI cannot reproduce SCC_MIXER CP2K")
3912 natom =
SIZE(particle_set)
3913 method = tb_reference_method_name(dft_control%qs_control%xtb_control%tblite_method)
3914 guess = tb_reference_guess_name(ref%guess)
3915 solver = tb_reference_solver_name(dft_control%qs_control%xtb_control%tblite_mixer_solver)
3916 file_base = tb_join_path(ref%work_directory, ref%prefix)
3917 gen_file = trim(file_base)//
".gen"
3918 grad_file = trim(file_base)//
".grad"
3919 json_file = trim(file_base)//
".json"
3920 log_file = trim(file_base)//
".log"
3921 post_processing_output_file =
""
3922 IF (len_trim(ref%grad_file) > 0) grad_file = ref%grad_file
3923 IF (len_trim(ref%json_file) > 0) json_file = ref%json_file
3924 IF (len_trim(ref%post_processing_output_file) > 0) &
3925 post_processing_output_file = ref%post_processing_output_file
3927 WRITE (charge_str,
"(I0)") dft_control%charge
3928 spin = max(0, dft_control%multiplicity - 1)
3929 WRITE (spin_str,
"(I0)") spin
3930 WRITE (acc_str,
"(ES16.8)") dft_control%qs_control%xtb_control%tblite_accuracy
3931 reference_iterations = dft_control%qs_control%xtb_control%tblite_mixer_iterations
3932 WRITE (iter_str,
"(I0)") reference_iterations
3934 IF (ref%efield_active)
THEN
3935 WRITE (efield_x_str,
"(ES16.8)") ref%efield(1)
3936 WRITE (efield_y_str,
"(ES16.8)") ref%efield(2)
3937 WRITE (efield_z_str,
"(ES16.8)") ref%efield(3)
3938 efield_str =
" --efield "//trim(adjustl(efield_x_str))//
","// &
3939 trim(adjustl(efield_y_str))//
","//trim(adjustl(efield_z_str))
3942 solvation_model_name =
""
3943 IF (ref%solvation_active)
THEN
3944 SELECT CASE (ref%solvation_model)
3946 solvation_model_name =
"ALPB"
3947 solvation_str =
" --alpb "
3949 solvation_model_name =
"GBSA"
3950 solvation_str =
" --gbsa "
3952 solvation_model_name =
"GBE"
3953 solvation_str =
" --gbe "
3955 solvation_model_name =
"GB"
3956 solvation_str =
" --gb "
3958 solvation_model_name =
"CPCM"
3959 solvation_str =
" --cpcm "
3961 cpabort(
"Unknown tblite reference CLI implicit-solvation model")
3963 solvation_str = trim(solvation_str)//
" "//trim(tb_shell_quote(ref%solvation_solvent))
3964 SELECT CASE (ref%solvation_born_kernel)
3967 solvation_str = trim(solvation_str)//
" --born-kernel p16"
3969 solvation_str = trim(solvation_str)//
" --born-kernel still"
3971 cpabort(
"Unknown tblite reference CLI Born kernel")
3973 SELECT CASE (ref%solvation_state)
3976 solvation_str = trim(solvation_str)//
" --solv-state bar1mol"
3978 solvation_str = trim(solvation_str)//
" --solv-state reference"
3980 cpabort(
"Unknown tblite reference CLI solution state")
3983 IF (dft_control%qs_control%xtb_control%tblite_mixer_memory /= reference_iterations)
THEN
3984 WRITE (unit=iounit, fmt=
"(/,T2,A)") &
3985 "WARNING: tblite reference CLI cannot reproduce XTB/TBLITE_MIXER/MEMORY."
3986 WRITE (unit=iounit, fmt=
"(T2,A,I0,A,I0,A)") &
3987 "The native reference run uses tblite's internal mixer memory tied to --iterations (", &
3988 reference_iterations,
"), while CP2K uses MEMORY ", &
3989 dft_control%qs_control%xtb_control%tblite_mixer_memory,
"."
3990 IF (ref%stop_on_error) cpabort(
"tblite reference CLI cannot reproduce TBLITE_MIXER/MEMORY")
3992 IF (abs(dft_control%qs_control%xtb_control%tblite_mixer_damping - &
3994 WRITE (unit=iounit, fmt=
"(/,T2,A)") &
3995 "WARNING: tblite reference CLI cannot reproduce XTB/TBLITE_MIXER/DAMPING."
3996 WRITE (unit=iounit, fmt=
"(T2,A,F8.4,A,F8.4,A)") &
3998 ", while CP2K uses DAMPING ", dft_control%qs_control%xtb_control%tblite_mixer_damping,
"."
3999 IF (ref%stop_on_error) cpabort(
"tblite reference CLI cannot reproduce TBLITE_MIXER/DAMPING")
4001 IF (abs(dft_control%qs_control%xtb_control%tblite_mixer_omega0 - &
4003 WRITE (unit=iounit, fmt=
"(/,T2,A)") &
4004 "WARNING: tblite reference CLI cannot reproduce XTB/TBLITE_MIXER/OMEGA0."
4005 WRITE (unit=iounit, fmt=
"(T2,A,ES12.4,A,ES12.4,A)") &
4007 ", while CP2K uses OMEGA0 ", dft_control%qs_control%xtb_control%tblite_mixer_omega0,
"."
4008 IF (ref%stop_on_error) cpabort(
"tblite reference CLI cannot reproduce TBLITE_MIXER/OMEGA0")
4010 IF (abs(dft_control%qs_control%xtb_control%tblite_mixer_min_weight - &
4012 WRITE (unit=iounit, fmt=
"(/,T2,A)") &
4013 "WARNING: tblite reference CLI cannot reproduce XTB/TBLITE_MIXER/MIN_WEIGHT."
4014 WRITE (unit=iounit, fmt=
"(T2,A,ES12.4,A,ES12.4,A)") &
4016 ", while CP2K uses MIN_WEIGHT ", dft_control%qs_control%xtb_control%tblite_mixer_min_weight,
"."
4017 IF (ref%stop_on_error) &
4018 cpabort(
"tblite reference CLI cannot reproduce TBLITE_MIXER/MIN_WEIGHT")
4020 IF (abs(dft_control%qs_control%xtb_control%tblite_mixer_max_weight - &
4022 WRITE (unit=iounit, fmt=
"(/,T2,A)") &
4023 "WARNING: tblite reference CLI cannot reproduce XTB/TBLITE_MIXER/MAX_WEIGHT."
4024 WRITE (unit=iounit, fmt=
"(T2,A,ES12.4,A,ES12.4,A)") &
4026 ", while CP2K uses MAX_WEIGHT ", dft_control%qs_control%xtb_control%tblite_mixer_max_weight,
"."
4027 IF (ref%stop_on_error) &
4028 cpabort(
"tblite reference CLI cannot reproduce TBLITE_MIXER/MAX_WEIGHT")
4030 IF (abs(dft_control%qs_control%xtb_control%tblite_mixer_weight_factor - &
4032 WRITE (unit=iounit, fmt=
"(/,T2,A)") &
4033 "WARNING: tblite reference CLI cannot reproduce XTB/TBLITE_MIXER/WEIGHT_FACTOR."
4034 WRITE (unit=iounit, fmt=
"(T2,A,ES12.4,A,ES12.4,A)") &
4036 ", while CP2K uses WEIGHT_FACTOR ", &
4037 dft_control%qs_control%xtb_control%tblite_mixer_weight_factor,
"."
4038 IF (ref%stop_on_error) &
4039 cpabort(
"tblite reference CLI cannot reproduce TBLITE_MIXER/WEIGHT_FACTOR")
4042 IF (
ASSOCIATED(scf_control) .AND.
ASSOCIATED(scf_control%smear))
THEN
4043 IF (scf_control%smear%do_smear)
THEN
4046 WRITE (unit=iounit, fmt=
"(/,T2,A,A,A)") &
4047 "WARNING: tblite reference CLI cannot reproduce CP2K smearing method ", &
4048 trim(tb_reference_smear_method_name(scf_control%smear%method)),
"."
4049 WRITE (unit=iounit, fmt=
"(T2,A,F12.3,A)") &
4050 "The native reference run uses Fermi-Dirac electronic temperature ", etemp,
" K instead."
4054 WRITE (etemp_str,
"(ES16.8)") etemp
4055 etemp_guess = 0.0_dp
4056 etemp_guess_str =
""
4057 IF (ref%electronic_temperature_guess > 0.0_dp)
THEN
4059 WRITE (etemp_guess_val_str,
"(ES16.8)") etemp_guess
4060 etemp_guess_str =
" --etemp-guess "//trim(adjustl(etemp_guess_val_str))
4063 IF (len_trim(dft_control%qs_control%xtb_control%tblite_param_file) > 0) &
4064 param_str =
" --param "//trim(tb_shell_quote(dft_control%qs_control%xtb_control%tblite_param_file))
4066 IF (dft_control%lsd) spinpol_str =
" --spin-polarized"
4067 post_processing_str =
""
4068 IF (len_trim(ref%post_processing) > 0) &
4069 post_processing_str =
" --post-processing "//trim(tb_shell_quote(ref%post_processing))
4070 post_processing_output_str =
""
4071 IF (len_trim(post_processing_output_file) > 0)
THEN
4072 WRITE (unit=iounit, fmt=
"(/,T2,A)") &
4073 "WARNING: tblite reference CLI POST_PROCESSING_OUTPUT was requested explicitly."
4074 WRITE (unit=iounit, fmt=
"(T2,A)") &
4075 "Some tblite 0.5.0 command-line builds document --post-processing-output but do not parse it."
4076 post_processing_output_str =
" --post-processing-output "// &
4077 trim(tb_shell_quote(post_processing_output_file))
4079 restart_str =
" --no-restart"
4080 IF (len_trim(ref%restart_file) > 0) &
4081 restart_str =
" --restart "//trim(tb_shell_quote(ref%restart_file))
4083 CALL tb_write_reference_gen(qs_env, trim(gen_file))
4085 command = trim(tb_shell_quote(ref%program_name))//
" run --method "//trim(method)// &
4087 trim(spinpol_str)// &
4088 " --charge "//trim(adjustl(charge_str))// &
4089 " --spin "//trim(adjustl(spin_str))// &
4090 " --acc "//trim(adjustl(acc_str))// &
4091 " --guess "//trim(guess)// &
4092 " --solver "//trim(solver)// &
4093 " --iterations "//trim(adjustl(iter_str))// &
4094 " --etemp "//trim(adjustl(etemp_str))// &
4095 trim(etemp_guess_str)// &
4096 trim(efield_str)// &
4097 trim(solvation_str)// &
4098 trim(post_processing_str)// &
4099 trim(post_processing_output_str)// &
4100 trim(restart_str)// &
4101 trim(verbosity_str)//
" --input "//trim(tb_shell_quote(ref%input_format))// &
4102 " --grad "//trim(tb_shell_quote(grad_file))// &
4103 " --json "//trim(tb_shell_quote(json_file))//
" "//trim(tb_shell_quote(gen_file))// &
4104 " > "//trim(tb_shell_quote(log_file))//
" 2>&1"
4108 CALL execute_command_line(trim(command), exitstat=exitstat, cmdstat=cmdstat)
4109 IF (cmdstat /= 0 .OR. exitstat /= 0)
THEN
4110 WRITE (unit=iounit, fmt=
"(/,T2,A)")
"tblite reference CLI check failed to run."
4111 WRITE (unit=iounit, fmt=
"(T2,A,A)")
"Command: ", trim(command)
4112 WRITE (unit=iounit, fmt=
"(T2,A,I0,T32,A,I0)")
"cmdstat:", cmdstat,
"exitstat:", exitstat
4113 IF (ref%stop_on_error) cpabort(
"tblite reference CLI command failed")
4114 CALL tb_reference_cleanup(ref, gen_file, grad_file, json_file, log_file, post_processing_output_file)
4115 CALL timestop(handle)
4119 ALLOCATE (cli_gradient(3, natom), cli_virial(3, 3))
4120 CALL tb_read_reference_grad(trim(grad_file), natom, cli_energy, cli_gradient, cli_virial, &
4121 have_energy, have_gradient, have_virial)
4123 WRITE (unit=iounit, fmt=
"(/,T2,A)")
"tblite reference CLI check"
4124 WRITE (unit=iounit, fmt=
"(T2,A,A)")
"Executable: ", trim(ref%program_name)
4125 WRITE (unit=iounit, fmt=
"(T2,A,A)")
"Method: ", trim(method)
4126 WRITE (unit=iounit, fmt=
"(T2,A,A)")
"Guess: ", trim(guess)
4127 WRITE (unit=iounit, fmt=
"(T2,A,A)")
"Solver: ", trim(solver)
4128 WRITE (unit=iounit, fmt=
"(T2,A,L1)")
"Spin-pol.: ", dft_control%lsd
4129 IF (ref%efield_active) &
4130 WRITE (unit=iounit, fmt=
"(T2,A,3ES16.8,A)")
"Efield: ", ref%efield,
" V/Angstrom"
4131 IF (ref%solvation_active) &
4132 WRITE (unit=iounit, fmt=
"(T2,A,A,1X,A)")
"Solvation: ", trim(solvation_model_name), &
4133 trim(ref%solvation_solvent)
4134 IF (len_trim(ref%post_processing) > 0) &
4135 WRITE (unit=iounit, fmt=
"(T2,A,A)")
"Post proc.: ", trim(ref%post_processing)
4136 IF (len_trim(post_processing_output_file) > 0) &
4137 WRITE (unit=iounit, fmt=
"(T2,A,A)")
"PP output: ", trim(post_processing_output_file)
4138 IF (ref%electronic_temperature_guess > 0.0_dp) &
4139 WRITE (unit=iounit, fmt=
"(T2,A,F12.3,A)")
"Guess etemp:", etemp_guess,
" K"
4140 WRITE (unit=iounit, fmt=
"(T2,A,A)")
"Grad file: ", trim(grad_file)
4141 WRITE (unit=iounit, fmt=
"(T2,A,A)")
"JSON file: ", trim(json_file)
4142 WRITE (unit=iounit, fmt=
"(T2,A,A)")
"Log file: ", trim(log_file)
4145 IF (ref%check_energy)
THEN
4146 IF (have_energy)
THEN
4147 cp_energy = energy%total
4148 ediff = abs(cp_energy - cli_energy)
4149 WRITE (unit=iounit, fmt=
"(T2,A,3ES22.12)") &
4150 "Energy CP2K/CLI/absdiff:", cp_energy, cli_energy, ediff
4151 too_large = too_large .OR. ediff > ref%error_limit
4153 WRITE (unit=iounit, fmt=
"(T2,A)")
"Energy check skipped: no CLI energy found."
4157 IF (ref%check_forces)
THEN
4158 IF (have_gradient .AND.
ASSOCIATED(force))
THEN
4159 ALLOCATE (cp_gradient(3, natom))
4161 fsum = sum(abs(cp_gradient - cli_gradient))
4162 fmax = maxval(abs(cp_gradient - cli_gradient))
4163 WRITE (unit=iounit, fmt=
"(T2,A,2ES22.12)")
"Gradient diff sum/max:", fsum, fmax
4164 too_large = too_large .OR. fmax > ref%error_limit
4165 DEALLOCATE (cp_gradient)
4167 WRITE (unit=iounit, fmt=
"(T2,A)")
"Gradient check skipped: no CLI gradient or CP2K force found."
4171 IF (ref%check_virial)
THEN
4172 IF (have_virial .AND.
ASSOCIATED(virial))
THEN
4175 vsum = sum(abs(-virial%pv_virial - cli_virial))
4176 vmax = maxval(abs(-virial%pv_virial - cli_virial))
4177 WRITE (unit=iounit, fmt=
"(T2,A,2ES22.12)")
"Virial diff sum/max:", vsum, vmax
4178 too_large = too_large .OR. vmax > ref%error_limit
4180 WRITE (unit=iounit, fmt=
"(T2,A)")
"Virial check skipped: no CLI virial or CP2K virial found."
4185 WRITE (unit=iounit, fmt=
"(T2,A,ES12.4)") &
4186 "tblite reference CLI deviation exceeded ERROR_LIMIT = ", ref%error_limit
4187 IF (ref%stop_on_error) cpabort(
"tblite reference CLI deviation exceeded ERROR_LIMIT")
4190 CALL tb_reference_cli_aux_commands(ref, dft_control, gen_file, file_base, verbosity_str, iounit)
4192 CALL tb_reference_cleanup(ref, gen_file, grad_file, json_file, log_file, post_processing_output_file)
4193 DEALLOCATE (cli_gradient, cli_virial)
4195 CALL timestop(handle)
4204 FUNCTION tb_reference_method_name(method_id)
RESULT(method)
4205 INTEGER,
INTENT(IN) :: method_id
4206 CHARACTER(LEN=8) :: method
4208 SELECT CASE (method_id)
4216 cpabort(
"Unknown tblite reference CLI method")
4219 END FUNCTION tb_reference_method_name
4226 FUNCTION tb_reference_guess_name(guess_id)
RESULT(guess)
4227 INTEGER,
INTENT(IN) :: guess_id
4228 CHARACTER(LEN=8) :: guess
4230 SELECT CASE (guess_id)
4238 cpabort(
"Unknown tblite reference CLI guess")
4241 END FUNCTION tb_reference_guess_name
4248 FUNCTION tb_reference_solver_name(solver_id)
RESULT(solver)
4249 INTEGER,
INTENT(IN) :: solver_id
4250 CHARACTER(LEN=8) :: solver
4252 SELECT CASE (solver_id)
4258 cpabort(
"Unknown tblite reference CLI solver")
4261 END FUNCTION tb_reference_solver_name
4268 FUNCTION tb_reference_smear_method_name(method_id)
RESULT(method)
4269 INTEGER,
INTENT(IN) :: method_id
4270 CHARACTER(LEN=24) :: method
4272 SELECT CASE (method_id)
4274 method =
"FERMI_DIRAC"
4276 method =
"ENERGY_WINDOW"
4282 method =
"METHFESSEL_PAXTON"
4284 method =
"MARZARI_VANDERBILT"
4289 END FUNCTION tb_reference_smear_method_name
4300 SUBROUTINE tb_reference_cli_aux_commands(ref, dft_control, gen_file, file_base, verbosity_str, iounit)
4304 CHARACTER(LEN=*),
INTENT(IN) :: gen_file, file_base, verbosity_str
4305 INTEGER,
INTENT(IN) :: iounit
4307 CHARACTER(LEN=32) :: charge_str, efield_x_str, efield_y_str, &
4308 efield_z_str, etemp_guess_val_str, &
4310 CHARACTER(LEN=4*default_path_length+16) :: copy_str, dry_run_str, efield_str, &
4311 etemp_guess_str, grad_str, json_str, &
4312 method_str, output_str
4313 CHARACTER(LEN=8*default_path_length) :: command
4314 CHARACTER(LEN=default_path_length) :: guess_input, log_file
4316 REAL(kind=
dp) :: etemp_guess
4318 WRITE (charge_str,
"(I0)") dft_control%charge
4319 spin = max(0, dft_control%multiplicity - 1)
4320 WRITE (spin_str,
"(I0)") spin
4322 IF (ref%guess_cli%enabled)
THEN
4323 guess_input = ref%guess_cli%input_file
4324 IF (len_trim(guess_input) == 0) guess_input = gen_file
4325 etemp_guess_str =
""
4326 IF (ref%guess_cli%electronic_temperature_guess > 0.0_dp)
THEN
4327 etemp_guess =
cp_unit_from_cp2k(ref%guess_cli%electronic_temperature_guess,
"K")
4328 WRITE (etemp_guess_val_str,
"(ES16.8)") etemp_guess
4329 etemp_guess_str =
" --etemp-guess "//trim(adjustl(etemp_guess_val_str))
4332 IF (ref%guess_cli%efield_active)
THEN
4333 WRITE (efield_x_str,
"(ES16.8)") ref%guess_cli%efield(1)
4334 WRITE (efield_y_str,
"(ES16.8)") ref%guess_cli%efield(2)
4335 WRITE (efield_z_str,
"(ES16.8)") ref%guess_cli%efield(3)
4336 efield_str =
" --efield "//trim(adjustl(efield_x_str))//
","// &
4337 trim(adjustl(efield_y_str))//
","//trim(adjustl(efield_z_str))
4340 IF (ref%guess_cli%grad) grad_str =
" --grad"
4342 IF (len_trim(ref%guess_cli%json_file) > 0) &
4343 json_str =
" --json "//trim(tb_shell_quote(ref%guess_cli%json_file))
4344 log_file = trim(file_base)//
".guess.log"
4345 command = trim(tb_shell_quote(ref%program_name))// &
4346 " guess --charge "//trim(adjustl(charge_str))// &
4347 " --spin "//trim(adjustl(spin_str))// &
4348 " --method "//trim(tb_reference_guess_name(ref%guess_cli%method))// &
4349 " --solver "//trim(tb_reference_solver_name(ref%guess_cli%solver))// &
4350 trim(etemp_guess_str)// &
4351 trim(efield_str)// &
4354 trim(verbosity_str)//
" --input "//trim(tb_shell_quote(ref%guess_cli%input_format))// &
4355 " "//trim(tb_shell_quote(guess_input))// &
4356 " > "//trim(tb_shell_quote(log_file))//
" 2>&1"
4357 CALL tb_reference_cli_execute(ref,
"guess", command, log_file, iounit)
4358 IF (.NOT. ref%keep_files .AND. len_trim(ref%guess_cli%json_file) > 0) &
4359 CALL tb_delete_file(ref%guess_cli%json_file)
4362 IF (ref%param_cli%enabled)
THEN
4364 IF (ref%param_cli%method_explicit .OR. len_trim(ref%param_cli%input_file) == 0) &
4365 method_str =
" --method "// &
4366 trim(tb_reference_method_name(merge(ref%param_cli%method, &
4367 dft_control%qs_control%xtb_control%tblite_method, &
4368 ref%param_cli%method_explicit)))
4370 IF (len_trim(ref%param_cli%output_file) > 0) &
4371 output_str =
" --output "//trim(tb_shell_quote(ref%param_cli%output_file))
4372 log_file = trim(file_base)//
".param.log"
4373 command = trim(tb_shell_quote(ref%program_name))//
" param"// &
4374 trim(method_str)// &
4376 IF (len_trim(ref%param_cli%input_file) > 0) &
4377 command = trim(command)//
" "//trim(tb_shell_quote(ref%param_cli%input_file))
4378 command = trim(command)//
" > "//trim(tb_shell_quote(log_file))//
" 2>&1"
4379 CALL tb_reference_cli_execute(ref,
"param", command, log_file, iounit)
4380 IF (.NOT. ref%keep_files .AND. len_trim(ref%param_cli%output_file) > 0) &
4381 CALL tb_delete_file(ref%param_cli%output_file)
4384 IF (ref%fit_cli%enabled)
THEN
4386 IF (ref%fit_cli%dry_run) dry_run_str =
" --dry-run"
4388 IF (len_trim(ref%fit_cli%copy_file) > 0) &
4389 copy_str =
" --copy "//trim(tb_shell_quote(ref%fit_cli%copy_file))
4390 log_file = trim(file_base)//
".fit.log"
4391 command = trim(tb_shell_quote(ref%program_name))//
" fit"// &
4392 trim(dry_run_str)// &
4394 trim(verbosity_str)//
" "//trim(tb_shell_quote(ref%fit_cli%param_file))// &
4395 " "//trim(tb_shell_quote(ref%fit_cli%input_file))// &
4396 " > "//trim(tb_shell_quote(log_file))//
" 2>&1"
4397 CALL tb_reference_cli_execute(ref,
"fit", command, log_file, iounit)
4398 IF (.NOT. ref%keep_files .AND. len_trim(ref%fit_cli%copy_file) > 0) &
4399 CALL tb_delete_file(ref%fit_cli%copy_file)
4402 IF (ref%tagdiff_cli%enabled)
THEN
4404 IF (ref%tagdiff_cli%fit) method_str =
" --fit"
4405 log_file = trim(file_base)//
".tagdiff.log"
4406 command = trim(tb_shell_quote(ref%program_name))//
" tagdiff"// &
4407 trim(method_str)//
" "//trim(tb_shell_quote(ref%tagdiff_cli%actual_file))// &
4408 " "//trim(tb_shell_quote(ref%tagdiff_cli%reference_file))// &
4409 " > "//trim(tb_shell_quote(log_file))//
" 2>&1"
4410 CALL tb_reference_cli_execute(ref,
"tagdiff", command, log_file, iounit)
4413 END SUBROUTINE tb_reference_cli_aux_commands
4423 SUBROUTINE tb_reference_cli_execute(ref, label, command, log_file, iounit)
4426 CHARACTER(LEN=*),
INTENT(IN) :: label, command, log_file
4427 INTEGER,
INTENT(IN) :: iounit
4429 INTEGER :: cmdstat, exitstat
4433 CALL execute_command_line(trim(command), exitstat=exitstat, cmdstat=cmdstat)
4434 IF (cmdstat /= 0 .OR. exitstat /= 0)
THEN
4435 WRITE (unit=iounit, fmt=
"(/,T2,A,A)")
"tblite reference CLI auxiliary command failed: ", &
4437 WRITE (unit=iounit, fmt=
"(T2,A,A)")
"Command: ", trim(command)
4438 WRITE (unit=iounit, fmt=
"(T2,A,I0,T32,A,I0)")
"cmdstat:", cmdstat,
"exitstat:", exitstat
4439 IF (ref%stop_on_error) cpabort(
"tblite reference CLI auxiliary command failed")
4441 WRITE (unit=iounit, fmt=
"(/,T2,A,A)")
"tblite reference CLI auxiliary command completed: ", &
4443 WRITE (unit=iounit, fmt=
"(T2,A,A)")
"Log file: ", trim(log_file)
4445 IF (.NOT. ref%keep_files)
CALL tb_delete_file(log_file)
4447 END SUBROUTINE tb_reference_cli_execute
4455 FUNCTION tb_join_path(directory, filename)
RESULT(path)
4456 CHARACTER(LEN=*),
INTENT(IN) :: directory, filename
4457 CHARACTER(LEN=default_path_length) :: path
4459 IF (len_trim(directory) == 0 .OR. trim(directory) ==
".")
THEN
4460 path = trim(filename)
4461 ELSE IF (directory(len_trim(directory):len_trim(directory)) ==
"/")
THEN
4462 path = trim(directory)//trim(filename)
4464 path = trim(directory)//
"/"//trim(filename)
4467 END FUNCTION tb_join_path
4474 FUNCTION tb_shell_quote(text)
RESULT(quoted)
4475 CHARACTER(LEN=*),
INTENT(IN) :: text
4476 CHARACTER(LEN=4*default_path_length) :: quoted
4481 DO i = 1, len_trim(text)
4482 IF (text(i:i) ==
"'")
THEN
4483 quoted = trim(quoted)//
"'\\''"
4485 quoted = trim(quoted)//text(i:i)
4488 quoted = trim(quoted)//
"'"
4490 END FUNCTION tb_shell_quote
4497 SUBROUTINE tb_write_reference_gen(qs_env, filename)
4500 CHARACTER(LEN=*),
INTENT(IN) :: filename
4502 CHARACTER(LEN=2),
ALLOCATABLE,
DIMENSION(:) :: symbols, unique_symbols
4503 INTEGER :: iatom, ikind, ios, natom, nuniq, unit_nr
4504 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: species
4505 INTEGER,
DIMENSION(3) :: periodic
4507 REAL(kind=
dp) :: to_angstrom
4511 NULLIFY (cell, particle_set)
4512 CALL get_qs_env(qs_env=qs_env, cell=cell, particle_set=particle_set)
4514 natom =
SIZE(particle_set)
4516 ALLOCATE (symbols(natom), unique_symbols(natom), species(natom))
4519 CALL get_atomic_kind(particle_set(iatom)%atomic_kind, element_symbol=symbols(iatom))
4522 IF (trim(unique_symbols(ikind)) == trim(symbols(iatom)))
THEN
4527 IF (.NOT. found)
THEN
4529 unique_symbols(nuniq) = symbols(iatom)
4532 species(iatom) = ikind
4535 OPEN (newunit=unit_nr, file=trim(filename), status=
"REPLACE", action=
"WRITE", &
4536 form=
"FORMATTED", iostat=ios)
4537 IF (ios /= 0) cpabort(
"Could not open tblite reference CLI geometry file")
4539 CALL get_cell(cell=cell, periodic=periodic)
4540 IF (any(periodic == 1))
THEN
4541 WRITE (unit=unit_nr, fmt=
"(I0,1X,A)") natom,
"S"
4543 WRITE (unit=unit_nr, fmt=
"(I0,1X,A)") natom,
"C"
4545 WRITE (unit=unit_nr, fmt=
"(*(A,1X))") (trim(unique_symbols(ikind)), ikind=1, nuniq)
4547 WRITE (unit=unit_nr, fmt=
"(I0,1X,I0,3(1X,ES24.16))") &
4548 iatom, species(iatom), particle_set(iatom)%r(:)*to_angstrom
4550 IF (any(periodic == 1))
THEN
4551 WRITE (unit=unit_nr, fmt=
"(3(1X,ES24.16))") 0.0_dp, 0.0_dp, 0.0_dp
4553 WRITE (unit=unit_nr, fmt=
"(3(1X,ES24.16))") cell%hmat(:, ikind)*to_angstrom
4558 DEALLOCATE (symbols, unique_symbols, species)
4560 END SUBROUTINE tb_write_reference_gen
4573 SUBROUTINE tb_read_reference_grad(filename, natom, energy, gradient, virial, &
4574 have_energy, have_gradient, have_virial)
4576 CHARACTER(LEN=*),
INTENT(IN) :: filename
4577 INTEGER,
INTENT(IN) :: natom
4578 REAL(kind=
dp),
INTENT(OUT) :: energy
4579 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(OUT) :: gradient, virial
4580 LOGICAL,
INTENT(OUT) :: have_energy, have_gradient, have_virial
4582 CHARACTER(LEN=1024) :: line
4583 INTEGER :: ios, nread, unit_nr
4585 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: values
4587 have_energy = .false.
4588 have_gradient = .false.
4589 have_virial = .false.
4594 INQUIRE (file=trim(filename), exist=exists)
4595 IF (.NOT. exists)
RETURN
4597 OPEN (newunit=unit_nr, file=trim(filename), status=
"OLD", action=
"READ", &
4598 form=
"FORMATTED", iostat=ios)
4599 IF (ios /= 0)
RETURN
4602 READ (unit=unit_nr, fmt=
"(A)", iostat=ios) line
4604 IF (index(line,
"energy :real:0:") > 0)
THEN
4605 READ (unit=unit_nr, fmt=
"(A)", iostat=ios) line
4607 READ (line, *, iostat=ios) energy
4608 have_energy = ios == 0
4610 ELSE IF (index(line,
"gradient :real:2:3,") > 0)
THEN
4611 ALLOCATE (values(3*natom))
4612 CALL tb_read_real_values(unit_nr, values, nread)
4613 IF (nread == 3*natom)
THEN
4614 CALL tb_values_to_matrix(values, gradient)
4615 have_gradient = .true.
4618 ELSE IF (index(line,
"virial :real:2:3,3") > 0)
THEN
4619 ALLOCATE (values(9))
4620 CALL tb_read_real_values(unit_nr, values, nread)
4621 IF (nread == 9)
THEN
4622 CALL tb_values_to_matrix(values, virial)
4623 have_virial = .true.
4630 END SUBROUTINE tb_read_reference_grad
4638 SUBROUTINE tb_read_real_values(unit_nr, values, nread)
4640 INTEGER,
INTENT(IN) :: unit_nr
4641 REAL(kind=
dp),
DIMENSION(:),
INTENT(OUT) :: values
4642 INTEGER,
INTENT(OUT) :: nread
4644 CHARACTER(LEN=1024) :: line
4648 DO WHILE (nread <
SIZE(values))
4649 READ (unit=unit_nr, fmt=
"(A)", iostat=ios) line
4651 CALL tb_parse_real_line(line, values, nread)
4654 END SUBROUTINE tb_read_real_values
4662 SUBROUTINE tb_parse_real_line(line, values, nread)
4664 CHARACTER(LEN=*),
INTENT(IN) :: line
4665 REAL(kind=
dp),
DIMENSION(:),
INTENT(INOUT) :: values
4666 INTEGER,
INTENT(INOUT) :: nread
4668 CHARACTER(LEN=128) :: token
4669 INTEGER :: first, ios, last, pos
4672 DO WHILE (pos <= len_trim(line) .AND. nread <
SIZE(values))
4673 DO WHILE (pos <= len_trim(line) .AND. index(
" ,[]", line(pos:pos)) > 0)
4676 IF (pos > len_trim(line))
EXIT
4678 DO WHILE (pos <= len_trim(line) .AND. index(
" ,[]", line(pos:pos)) == 0)
4682 token = line(first:last)
4683 READ (token, *, iostat=ios) values(nread + 1)
4684 IF (ios == 0) nread = nread + 1
4687 END SUBROUTINE tb_parse_real_line
4694 SUBROUTINE tb_values_to_matrix(values, matrix)
4696 REAL(kind=
dp),
DIMENSION(:),
INTENT(IN) :: values
4697 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(OUT) :: matrix
4702 DO j = 1,
SIZE(matrix, 2)
4703 DO i = 1,
SIZE(matrix, 1)
4705 matrix(i, j) = values(n)
4709 END SUBROUTINE tb_values_to_matrix
4720 SUBROUTINE tb_reference_cleanup(ref, gen_file, grad_file, json_file, log_file, post_processing_output_file)
4723 CHARACTER(LEN=*),
INTENT(IN) :: gen_file, grad_file, json_file, &
4724 log_file, post_processing_output_file
4726 IF (ref%keep_files)
RETURN
4727 CALL tb_delete_file(gen_file)
4728 CALL tb_delete_file(grad_file)
4729 CALL tb_delete_file(json_file)
4730 CALL tb_delete_file(log_file)
4731 IF (len_trim(post_processing_output_file) > 0) &
4732 CALL tb_delete_file(post_processing_output_file)
4734 END SUBROUTINE tb_reference_cleanup
4740 SUBROUTINE tb_delete_file(filename)
4742 CHARACTER(LEN=*),
INTENT(IN) :: filename
4744 INTEGER :: ios, unit_nr
4747 INQUIRE (file=trim(filename), exist=exists)
4748 IF (.NOT. exists)
RETURN
4749 OPEN (newunit=unit_nr, file=trim(filename), status=
"OLD", iostat=ios)
4750 IF (ios == 0)
CLOSE (unit_nr, status=
"DELETE")
4752 END SUBROUTINE tb_delete_file
4760 SUBROUTINE tb_dump_sigma_component(label, sigma, para_env)
4762 CHARACTER(LEN=*),
INTENT(IN) :: label
4763 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(IN) :: sigma
4766 CHARACTER(LEN=default_path_length) :: dump_file
4767 INTEGER :: dump_status, dump_unit, i
4770#if defined(__TBLITE_DEBUG_DIAGNOSTICS)
4771 CALL get_environment_variable(
"CP2K_TBLITE_SIGMA_COMPONENT_DUMP", dump_file, status=dump_status)
4773 IF (dump_status /= 0)
RETURN
4775 OPEN (newunit=dump_unit, file=trim(dump_file), status=
"UNKNOWN", &
4776 position=
"APPEND", action=
"WRITE")
4777 WRITE (dump_unit,
"(A)") trim(label)
4779 WRITE (dump_unit,
"(3(1X,ES24.16))") sigma(i, :)/para_env%num_pe
4783 END SUBROUTINE tb_dump_sigma_component
4791 SUBROUTINE tb_add_stress(qs_env, tb, para_env)
4797 CHARACTER(LEN=default_path_length) :: dump_file
4798 INTEGER :: dump_status, dump_unit, i
4799 INTEGER,
DIMENSION(3) :: periodic
4803 NULLIFY (virial, cell)
4804 CALL get_qs_env(qs_env=qs_env, virial=virial, cell=cell)
4805 CALL get_cell(cell=cell, periodic=periodic)
4807 IF (all(periodic == 0))
THEN
4808 CALL cp_warn(__location__, &
4809 "tblite stress tensor requested for an isolated system. "// &
4810 "The reported virial is useful for finite-difference checks, "// &
4811 "but it is not a physically meaningful bulk stress for an isolated molecule.")
4815#if defined(__TBLITE_DEBUG_DIAGNOSTICS)
4816 CALL get_environment_variable(
"CP2K_TBLITE_VIRIAL_DUMP", dump_file, status=dump_status)
4818 IF (dump_status == 0)
THEN
4819 OPEN (newunit=dump_unit, file=trim(dump_file), status=
"UNKNOWN", &
4820 position=
"APPEND", action=
"WRITE")
4821 WRITE (dump_unit,
"(A)")
"sigma"
4823 WRITE (dump_unit,
"(3(1X,ES24.16))") tb%sigma(i, :)/para_env%num_pe
4828 virial%pv_virial = virial%pv_virial - tb%sigma/para_env%num_pe
4830 END SUBROUTINE tb_add_stress
4839 SUBROUTINE tb_add_grad(grad, deriv, dE, natom)
4841 REAL(kind=
dp),
DIMENSION(:, :) :: grad
4842 REAL(kind=
dp),
DIMENSION(:, :, :) :: deriv
4843 REAL(kind=
dp),
DIMENSION(:) :: de
4850 grad(:, i) = grad(:, i) + deriv(:, i, j)*de(j)
4854 END SUBROUTINE tb_add_grad
4863 SUBROUTINE tb_add_sig(sig, deriv, dE, natom)
4865 REAL(kind=
dp),
DIMENSION(:, :) :: sig
4866 REAL(kind=
dp),
DIMENSION(:, :, :) :: deriv
4867 REAL(kind=
dp),
DIMENSION(:) :: de
4874 sig(:, i) = sig(:, i) + deriv(:, i, j)*de(j)
4878 END SUBROUTINE tb_add_sig
static GRID_HOST_DEVICE int modulo(int a, int m)
Equivalent of Fortran's MODULO, which always return a positive number. https://gcc....
static GRID_HOST_DEVICE int idx(const orbital a)
Return coset index of given orbital angular momentum.
Set of routines to: Contract integrals over primitive Gaussians Decontract (density) matrices Trace m...
Calculation of the overlap integrals over Cartesian Gaussian-type functions.
subroutine, public overlap_ab(la_max, la_min, npgfa, rpgfa, zeta, lb_max, lb_min, npgfb, rpgfb, zetb, rab, sab, dab, ddab)
Calculation of the two-center overlap integrals [a|b] over Cartesian Gaussian-type functions....
Define the atomic kind types and their sub types.
subroutine, public get_atomic_kind_set(atomic_kind_set, atom_of_kind, kind_of, natom_of_kind, maxatom, natom, nshell, fist_potential_present, shell_present, shell_adiabatic, shell_check_distance, damping_present)
Get attributes of an atomic kind set.
subroutine, public get_atomic_kind(atomic_kind, fist_potential, element_symbol, name, mass, kind_number, natom, atom_list, rcov, rvdw, z, qeff, apol, cpol, mm_radius, shell, shell_active, damping)
Get attributes of an atomic kind.
Holds information on atomic properties.
subroutine, public process_gto_basis(gto_basis_set, do_ortho, nset, maxl)
...
subroutine, public allocate_gto_basis_set(gto_basis_set)
...
subroutine, public write_gto_basis_set(gto_basis_set, output_unit, header)
Write a Gaussian-type orbital (GTO) basis set data set to the output unit.
Handles all functions related to the CELL.
subroutine, public get_cell(cell, alpha, beta, gamma, deth, orthorhombic, abc, periodic, h, h_inv, symmetry_id, tag)
Get informations about a simulation cell.
methods related to the blacs parallel environment
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
logical function, public dbcsr_iterator_blocks_left(iterator)
...
subroutine, public dbcsr_iterator_stop(iterator)
...
subroutine, public dbcsr_copy(matrix_b, matrix_a, name, keep_sparsity, keep_imaginary)
...
subroutine, public dbcsr_get_block_p(matrix, row, col, block, found, row_size, col_size)
...
subroutine, public dbcsr_iterator_next_block(iterator, row, column, block, block_number_argument_has_been_removed, row_size, col_size, row_offset, col_offset, transposed)
...
subroutine, public dbcsr_finalize(matrix)
...
subroutine, public dbcsr_iterator_start(iterator, matrix, shared, dynamic, dynamic_byrows)
...
subroutine, public dbcsr_add(matrix_a, matrix_b, alpha_scalar, beta_scalar)
...
subroutine, public dbcsr_print(matrix, variable_name, unit_nr)
Prints given matrix in matlab format (only present blocks).
Routines that link DBCSR and CP2K concepts together.
subroutine, public cp_dbcsr_alloc_block_from_nbl(matrix, sab_orb, desymmetrize)
allocate the blocks of a dbcsr based on the neighbor list
DBCSR operations in CP2K.
subroutine, public cp_dbcsr_write_sparse_matrix(sparse_matrix, before, after, qs_env, para_env, first_row, last_row, first_col, last_col, scale, output_unit, omit_headers, cartesian_basis)
...
various routines to log and control the output. The idea is that decisions about where to log should ...
integer function, public cp_logger_get_default_io_unit(logger)
returns the unit nr for the ionode (-1 on all other processors) skips as well checks if the procs cal...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
routines to handle the output, The idea is to remove the decision of wheter to output and what to out...
integer, parameter, public debug_print_level
integer function, public cp_print_key_unit_nr(logger, basis_section, print_key_path, extension, middle_name, local, log_filename, ignore_should_output, file_form, file_position, file_action, file_status, do_backup, on_file, is_new_file, mpi_io, fout)
...
subroutine, public cp_print_key_finished_output(unit_nr, logger, basis_section, print_key_path, local, ignore_should_output, on_file, mpi_io)
should be called after you finish working with a unit obtained with cp_print_key_unit_nr,...
integer, parameter, public high_print_level
integer function, public cp_print_key_should_output(iteration_info, basis_section, print_key_path, used_print_key, first_time)
returns what should be done with the given property if btest(res,cp_p_store) then the property should...
integer, parameter, public silent_print_level
real(kind=dp) function, public cp_unit_from_cp2k(value, unit_str, defaults, power)
converts from the internal cp2k units to the given unit
Defines the basic variable types.
integer, parameter, public int_8
integer, parameter, public dp
integer, parameter, public default_string_length
integer, parameter, public default_path_length
Types and basic routines needed for a kpoint calculation.
subroutine, public get_kpoint_info(kpoint, kp_scheme, nkp_grid, kp_shift, symmetry, verbose, full_grid, use_real_wfn, eps_geo, parallel_group_size, kp_range, nkp, xkp, wkp, para_env, blacs_env_all, para_env_kp, para_env_inter_kp, blacs_env, kp_env, kp_aux_env, mpools, iogrp, nkp_groups, kp_dist, cell_to_index, index_to_cell, sab_nl, sab_nl_nosym, inversion_symmetry_only, symmetry_backend, symmetry_reduction_method, gamma_centered)
Retrieve information from a kpoint environment.
Utility routines for the memory handling.
Interface to the message passing library MPI.
compute mulliken charges we (currently) define them as c_i = 1/2 [ (PS)_{ii} + (SP)_{ii} ]
Provides Cartesian and spherical orbital pointers and indices.
integer, dimension(:), allocatable, public ncoset
Define the data structure for the particle information.
subroutine, public charge_mixing(mixing_method, mixing_store, charges, para_env, iter_count, scc_mixer, tblite_mixer_iterations, tblite_mixer_damping, tblite_mixer_memory, tblite_mixer_omega0, tblite_mixer_min_weight, tblite_mixer_max_weight, tblite_mixer_weight_factor)
Driver for TB SCC variable mixing, calls the requested method.
Calculation of overlap matrix condition numbers.
subroutine, public overlap_condnum(matrixkp_s, condnum, iunit, norml1, norml2, use_arnoldi, blacs_env)
Calculation of the overlap matrix Condition Number.
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.
subroutine, public total_qs_force(force, qs_force, atomic_kind_set)
Get current total force.
Some utility functions for the calculation of integrals.
subroutine, public basis_set_list_setup(basis_set_list, basis_type, qs_kind_set)
Set up an easy accessible list of the basis sets for all kinds.
Define the quickstep kind type and their sub types.
subroutine, public get_qs_kind(qs_kind, basis_set, basis_type, ncgf, nsgf, all_potential, tnadd_potential, gth_potential, sgp_potential, upf_potential, cneo_potential, se_parameter, dftb_parameter, xtb_parameter, dftb3_param, zatom, zeff, elec_conf, mao, lmax_dftb, alpha_core_charge, ccore_charge, core_charge, core_charge_radius, paw_proj_set, paw_atom, hard_radius, hard0_radius, max_rad_local, covalent_radius, vdw_radius, gpw_type_forced, harmonics, max_iso_not0, max_s_harm, grid_atom, ngrid_ang, ngrid_rad, lmax_rho0, dft_plus_u_atom, l_of_dft_plus_u, n_of_dft_plus_u, u_minus_j, u_of_dft_plus_u, j_of_dft_plus_u, alpha_of_dft_plus_u, beta_of_dft_plus_u, j0_of_dft_plus_u, occupation_of_dft_plus_u, dispersion, bs_occupation, magnetization, no_optimize, addel, laddel, naddel, orbitals, max_scf, eps_scf, smear, u_ramping, u_minus_j_target, eps_u_ramping, init_u_ramping_each_scf, reltmat, ghost, monovalent, floating, name, element_symbol, pao_basis_size, pao_model_file, pao_potentials, pao_descriptors, nelec)
Get attributes of an atomic kind.
subroutine, public get_qs_kind_set(qs_kind_set, all_potential_present, tnadd_potential_present, gth_potential_present, sgp_potential_present, paw_atom_present, dft_plus_u_atom_present, maxcgf, maxsgf, maxco, maxco_proj, maxgtops, maxlgto, maxlprj, maxnset, maxsgf_set, ncgf, npgf, nset, nsgf, nshell, maxpol, maxlppl, maxlppnl, maxppnl, nelectron, maxder, max_ngrid_rad, max_sph_harm, maxg_iso_not0, lmax_rho0, basis_rcut, basis_type, total_zeff_corr, npgf_seg, cneo_potential_present, nkind_q, natom_q)
Get attributes of an atomic kind set.
subroutine, public set_ks_env(ks_env, v_hartree_rspace, s_mstruct_changed, rho_changed, exc_accint, potential_changed, forces_up_to_date, complex_ks, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, kinetic, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_ks_im_kp, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, vee, neighbor_list_id, kpoints, sab_orb, sab_all, sac_ae, sac_ppl, sac_lri, sap_ppnl, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_vdw, sab_scp, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, task_list, task_list_soft, subsys, dft_control, dbcsr_dist, distribution_2d, pw_env, para_env, blacs_env)
...
subroutine, public get_ks_env(ks_env, v_hartree_rspace, s_mstruct_changed, rho_changed, exc_accint, potential_changed, forces_up_to_date, complex_ks, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, kinetic, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_ks_im_kp, rho, rho_xc, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, vee, neighbor_list_id, sab_orb, sab_all, sac_ae, sac_ppl, sac_lri, sap_ppnl, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_vdw, sab_scp, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, task_list, task_list_soft, kpoints, do_kpoints, atomic_kind_set, qs_kind_set, cell, cell_ref, use_ref_cell, particle_set, energy, force, local_particles, local_molecules, molecule_kind_set, molecule_set, subsys, cp_subsys, virial, results, atprop, nkind, natom, dft_control, dbcsr_dist, distribution_2d, pw_env, para_env, blacs_env, nelectron_total, nelectron_spin)
...
Define the neighbor list data types and the corresponding functionality.
subroutine, public neighbor_list_iterator_create(iterator_set, nl, search, nthread)
Neighbor list iterator functions.
subroutine, public neighbor_list_iterator_release(iterator_set)
...
integer function, public neighbor_list_iterate(iterator_set, mepos)
...
subroutine, public get_iterator_info(iterator_set, mepos, ikind, jkind, nkind, ilist, nlist, inode, nnode, iatom, jatom, r, cell)
...
Calculation of overlap matrix, its derivatives and forces.
superstucture that hold various representations of the density and keeps track of which ones are vali...
subroutine, public qs_rho_get(rho_struct, rho_ao, rho_ao_im, rho_ao_kp, rho_ao_im_kp, rho_r, drho_r, rho_g, drho_g, tau_r, tau_g, rho_r_valid, drho_r_valid, rho_g_valid, drho_g_valid, tau_r_valid, tau_g_valid, tot_rho_r, tot_rho_g, rho_r_sccs, soft_valid, complex_rho_ao)
returns info about the density described by this object. If some representation is not available an e...
module that contains the definitions of the scf types
parameters that control an scf iteration
Utilities for string manipulations.
subroutine, public integer_to_string(inumber, string)
Converts an integer number to a string. The WRITE statement will return an error message,...
logical function, public tb_native_scc_mixer_active(dft_control)
Return whether the tblite native SCC mixer is active for this run.
subroutine, public tb_ham_add_coulomb(qs_env, tb, dft_control)
...
subroutine, public tb_init_wf(tb, dft_control)
initialize wavefunction ...
subroutine, public tb_derive_dh_diag(qs_env, use_rho, nimg)
compute the derivative of the energy
subroutine, public tb_update_charges(qs_env, dft_control, tb, calculate_forces, use_rho)
...
subroutine, public tb_init_geometry(qs_env, tb)
intialize geometry objects ...
subroutine, public build_tblite_matrices(qs_env, calculate_forces)
...
subroutine, public tb_get_energy(qs_env, tb, energy)
...
subroutine, public tb_set_calculator(tb, typ, accuracy, param_file)
...
subroutine, public tb_derive_dh_off(qs_env, use_rho, nimg)
compute the derivative of the energy
subroutine, public tb_get_multipole(qs_env, tb)
...
subroutine, public tb_reference_cli_compare(qs_env)
Run native tblite CLI and compare against CP2K/tblite.
real(kind=dp) function, public tb_scf_mixer_error(dft_control, tb, eps_scf)
Return the native tblite SCC mixer residual on the CP2K iter_delta scale.
subroutine, public tb_get_basis(tb, gto_basis_set, element_symbol, param, occ)
...
CP2K-side tblite-compatible SCC Broyden mixer.
subroutine, public allocate_tblite_type(tb_tblite)
...
subroutine, public deallocate_tblite_type(tb_tblite)
...
Definition of the xTB parameter types.
subroutine, public get_xtb_atom_param(xtb_parameter, symbol, aname, typ, defined, z, zeff, natorb, lmax, nao, lao, rcut, rcov, kx, eta, xgamma, alpha, zneff, nshell, nval, lval, kpoly, kappa, hen, zeta, xi, kappa0, alpg, occupation, electronegativity, chmax, en, kqat2, kcn, kq)
...
Provides all information about an atomic kind.
type for the atomic properties
Type defining parameters related to the simulation cell.
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
type of a logger, at the moment it contains just a print level starting at which level it should be l...
Contains information about kpoints.
stores all the informations relevant to an mpi environment
Provides all information about a quickstep kind.
calculation environment to calculate the ks matrix, holds all the needed vars. assumes that the core ...
keeps the density in various representations, keeping track of which ones are valid.