Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief Gaussian-product geometry and radial seeds shared by four-center evaluation and screening.
10 : ! **************************************************************************************************
11 : MODULE four_center_integral_preparation
12 : USE basis_set_types, ONLY: gto_basis_set_type
13 : USE four_center_integral_types, ONLY: eri4_contraction_type,&
14 : eri4_pgf_image_type,&
15 : eri4_pgf_pair_type
16 : USE input_constants, ONLY: do_potential_id
17 : USE integral_library_types, ONLY: coulomb_operator_type
18 : USE kinds, ONLY: dp
19 : USE libint_wrapper, ONLY: eri_primitive_data_type,&
20 : prim_data_f_size
21 : USE mathconstants, ONLY: pi
22 : USE orbital_pointers, ONLY: nco,&
23 : nso
24 : USE orbital_transformation_matrices, ONLY: orbtramat
25 : USE r12_fundamentals, ONLY: compute_r12_fundamentals
26 : #include "./base/base_uses.f90"
27 :
28 : IMPLICIT NONE
29 : PRIVATE
30 : PUBLIC :: min_image_gap_sq, prepare_eri4_contraction, prepare_eri4_pair, prepare_eri4_product, prepare_eri4_primitive
31 :
32 : CONTAINS
33 :
34 : ! **************************************************************************************************
35 : !> \brief Split the initialized basis coefficients into angular and radial factors.
36 : !> \param basis basis used by the four-center driver
37 : !> \param contraction factors indexed by angular momentum and shell set
38 : ! **************************************************************************************************
39 4290 : SUBROUTINE prepare_eri4_contraction(basis, contraction)
40 : TYPE(gto_basis_set_type), INTENT(IN) :: basis
41 : TYPE(eri4_contraction_type), ALLOCATABLE, &
42 : INTENT(OUT) :: contraction(:, :)
43 :
44 : REAL(dp), PARAMETER :: norm_rtol = 64.0_dp*EPSILON(0.0_dp)
45 :
46 : INTEGER :: first, ic, iset, ishell, l, m, nc, nl, &
47 : np, ref
48 : REAL(dp) :: norm_error, normalization
49 4290 : REAL(dp), ALLOCATABLE :: angular_norm(:)
50 :
51 70548 : ALLOCATE (contraction(0:MAXVAL(basis%lmax(1:basis%nset)), basis%nset))
52 16702 : DO iset = 1, basis%nset
53 12412 : np = basis%npgf(iset)
54 31858 : DO l = basis%lmin(iset), basis%lmax(iset)
55 15156 : nc = nco(l)
56 37608 : nl = COUNT(basis%l(1:basis%nshell(iset), iset) == l)
57 12412 : ASSOCIATE (shell => contraction(l, iset))
58 196442 : ALLOCATE (shell%angular(nc, nso(l)), source=0.0_dp)
59 60624 : ALLOCATE (shell%weights(nl, np))
60 15156 : IF (nl == 0) CYCLE
61 45468 : ALLOCATE (angular_norm(nc))
62 18354 : ishell = FINDLOC(basis%l(1:basis%nshell(iset), iset), l, DIM=1)
63 15156 : first = basis%first_cgf(ishell, iset)
64 : ! At fixed l the Cartesian norms of the supported bases differ
65 : ! between contractions by a common scalar.
66 48790 : angular_norm(:) = basis%norm_cgf(first:first + nc - 1)
67 63946 : ref = MAXLOC(ABS(angular_norm), DIM=1)
68 15156 : CPASSERT(ABS(angular_norm(ref)) > 0.0_dp)
69 48790 : angular_norm(:) = angular_norm/angular_norm(ref)
70 150974 : shell%angular(:, :) = TRANSPOSE(orbtramat(l)%c2s)
71 47176 : DO m = 1, nso(l)
72 150974 : shell%angular(:, m) = shell%angular(:, m)*angular_norm
73 : END DO
74 15156 : ic = 0
75 37608 : DO ishell = 1, basis%nshell(iset)
76 22452 : IF (basis%l(ishell, iset) /= l) CYCLE
77 16166 : first = basis%first_cgf(ishell, iset)
78 16166 : normalization = basis%norm_cgf(first + ref - 1)
79 51470 : norm_error = MAXVAL(ABS(basis%norm_cgf(first:first + nc - 1) - normalization*angular_norm))
80 16166 : CPASSERT(norm_error <= norm_rtol*ABS(normalization))
81 16166 : ic = ic + 1
82 65682 : shell%weights(ic, :) = basis%gcc(1:np, ishell, iset)*normalization
83 : END DO
84 30312 : DEALLOCATE (angular_norm)
85 : END ASSOCIATE
86 : END DO
87 : END DO
88 4290 : END SUBROUTINE prepare_eri4_contraction
89 :
90 : ! **************************************************************************************************
91 : !> \brief Prepare one Gaussian pair, retaining the caller's image topology and screening metadata.
92 : !> \param ra first center
93 : !> \param rb second center in the selected image
94 : !> \param zeta first Gaussian exponent
95 : !> \param zetb second Gaussian exponent
96 : !> \param rab2 squared center distance, reusable across primitive pairs
97 : !> \param pair exponent data; image-list ownership is unchanged
98 : !> \param image product center and logarithmic Gaussian attenuation for this image
99 : ! **************************************************************************************************
100 247170730 : SUBROUTINE prepare_eri4_pair(ra, rb, zeta, zetb, rab2, pair, image)
101 : REAL(dp), INTENT(IN) :: ra(3), rb(3), zeta, zetb, rab2
102 : TYPE(eri4_pgf_pair_type), INTENT(INOUT) :: pair
103 : TYPE(eri4_pgf_image_type), INTENT(INOUT) :: image
104 :
105 247170730 : pair%zeta = zeta
106 247170730 : pair%zetb = zetb
107 247170730 : pair%zetapzetb = zeta + zetb
108 247170730 : pair%ZetaInv = 1.0_dp/pair%zetapzetb
109 988682920 : image%ra = ra
110 988682920 : image%rb = rb
111 247170730 : image%S1234 = -zeta*zetb*pair%ZetaInv*rab2
112 988682920 : image%P = (zeta*ra + zetb*rb)*pair%ZetaInv
113 247170730 : END SUBROUTINE prepare_eri4_pair
114 :
115 : ! **************************************************************************************************
116 : !> \brief Prepare the image-independent quartet data once per combination of Gaussian pairs.
117 : !> \param pair_a bra Gaussian pair
118 : !> \param pair_b ket Gaussian pair
119 : !> \param m_max maximum radial order, including any derivative increment
120 : !> \param product reusable template; centers and radial seeds are filled for each selected image
121 : ! **************************************************************************************************
122 184367589 : SUBROUTINE prepare_eri4_product(pair_a, pair_b, m_max, product)
123 : TYPE(eri4_pgf_pair_type), INTENT(IN) :: pair_a, pair_b
124 : INTEGER, INTENT(IN) :: m_max
125 : TYPE(eri_primitive_data_type), INTENT(INOUT) :: product
126 :
127 184367589 : CPASSERT(m_max < prim_data_f_size)
128 :
129 184367589 : product%prepared_max_order = m_max
130 921837945 : product%exponents = [pair_a%zeta, pair_a%zetb, pair_b%zeta, pair_b%zetb]
131 184367589 : product%ZetaInv = pair_a%ZetaInv
132 184367589 : product%EtaInv = pair_b%ZetaInv
133 184367589 : product%ZetapEtaInv = pair_a%zetapzetb + pair_b%zetapzetb
134 184367589 : product%ZetapEtaInv = 1.0_dp/product%ZetapEtaInv
135 184367589 : product%Rho = pair_a%zetapzetb*pair_b%zetapzetb*product%ZetapEtaInv
136 184367589 : END SUBROUTINE prepare_eri4_product
137 :
138 : ! **************************************************************************************************
139 : !> \brief Complete a prepared quartet for one selected image, including normalized radial seeds.
140 : !> \param product image-independent quartet template
141 : !> \param image_a bra pair geometry
142 : !> \param image_b ket pair geometry after the caller's image translation
143 : !> \param attenuation exponential of the two pair logarithmic attenuations, reusable across translations
144 : !> \param pq2 squared product-center distance, already available from cutoff screening
145 : !> \param potential_parameter radial operator parameters
146 : !> \param params complete primitive in original center order, ready for backend permutation
147 : ! **************************************************************************************************
148 400008592 : SUBROUTINE prepare_eri4_primitive(product, image_a, image_b, attenuation, pq2, potential_parameter, params)
149 : TYPE(eri_primitive_data_type), INTENT(IN) :: product
150 : TYPE(eri4_pgf_image_type), INTENT(IN) :: image_a, image_b
151 : REAL(dp), INTENT(IN) :: attenuation, pq2
152 : TYPE(coulomb_operator_type), INTENT(IN) :: potential_parameter
153 : TYPE(eri_primitive_data_type), INTENT(INOUT) :: params
154 :
155 : INTEGER :: i, m_max
156 : REAL(dp) :: den, fundamentals_work(prim_data_f_size), G(3), normalization, num, prefactor, &
157 : ssss, T
158 :
159 400008592 : m_max = product%prepared_max_order
160 400008592 : params%prepared_max_order = m_max
161 2000042960 : params%exponents = product%exponents
162 400008592 : params%ZetaInv = product%ZetaInv
163 400008592 : params%EtaInv = product%EtaInv
164 400008592 : params%ZetapEtaInv = product%ZetapEtaInv
165 400008592 : params%Rho = product%Rho
166 1600034368 : params%centers(:, 1) = image_a%ra
167 1600034368 : params%centers(:, 2) = image_a%rb
168 1600034368 : params%centers(:, 3) = image_b%ra
169 1600034368 : params%centers(:, 4) = image_b%rb
170 1600034368 : params%P = image_a%P
171 1600034368 : params%Q = image_b%P
172 :
173 : ASSOCIATE (A => image_a%ra, B => image_a%rb, C => image_b%ra, D => image_b%rb, &
174 : zeta_a => product%exponents(1), zeta_b => product%exponents(2), &
175 : zeta_c => product%exponents(3), zeta_d => product%exponents(4))
176 1600034368 : params%W = ((zeta_a + zeta_b)*params%P + (zeta_c + zeta_d)*params%Q)*params%ZetapEtaInv
177 400008592 : T = params%Rho*pq2
178 800017184 : IF (potential_parameter%potential_type == do_potential_id) THEN
179 294732 : num = zeta_a*zeta_b
180 294732 : den = zeta_a + zeta_b
181 1178928 : ssss = -num/den*SUM((A - B)**2)
182 294732 : num = den*zeta_c
183 294732 : den = den + zeta_c
184 1178928 : ssss = ssss - num/den*SUM((params%P - C)**2)
185 1178928 : G = (zeta_a*A + zeta_b*B + zeta_c*C)/den
186 294732 : num = den*zeta_d
187 294732 : den = den + zeta_d
188 1178928 : ssss = ssss - num/den*SUM((G - D)**2)
189 6484104 : params%Fm(:) = EXP(ssss)
190 : ! The ID seed already contains the four-Gaussian attenuation.
191 294732 : normalization = 1.0_dp
192 294732 : IF (attenuation > EPSILON(0.0_dp)) normalization = 1.0_dp/attenuation
193 : ELSE
194 : CALL compute_r12_fundamentals(params%Fm, normalization, m_max, potential_parameter%potential_type, &
195 : params%Rho, T, fundamentals_work, &
196 : omega=potential_parameter%omega, &
197 : cutoff_radius=potential_parameter%cutoff_radius, &
198 : scale_coulomb=potential_parameter%scale_coulomb, &
199 : scale_longrange=potential_parameter%scale_longrange, &
200 399713860 : scale_gaussian=potential_parameter%scale_gaussian)
201 : END IF
202 : END ASSOCIATE
203 :
204 400008592 : prefactor = normalization*attenuation*SQRT((Pi*params%ZetapEtaInv)**3)
205 1455338835 : DO i = 1, m_max + 1
206 1455338835 : params%Fm(i) = params%Fm(i)*prefactor
207 : END DO
208 400008592 : END SUBROUTINE prepare_eri4_primitive
209 :
210 : ! **************************************************************************************************
211 : !> \brief Squared-distance gap to the next image for an orthorhombic cell.
212 : !> \param r minimum-image displacement
213 : !> \param hmat cell matrix
214 : !> \return smallest squared-distance increment to an adjacent image
215 : ! **************************************************************************************************
216 27160062 : PURE FUNCTION min_image_gap_sq(r, hmat) RESULT(gap)
217 : REAL(dp), DIMENSION(3), INTENT(IN) :: r
218 : REAL(dp), DIMENSION(3, 3), INTENT(IN) :: hmat
219 : REAL(dp) :: gap
220 :
221 : REAL(dp), DIMENSION(3) :: cell_len
222 :
223 108640248 : cell_len = [ABS(hmat(1, 1)), ABS(hmat(2, 2)), ABS(hmat(3, 3))]
224 108640248 : gap = MINVAL(cell_len*(cell_len - 2.0_dp*ABS(r)))
225 :
226 27160062 : END FUNCTION min_image_gap_sq
227 :
228 : END MODULE four_center_integral_preparation
|