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 Contains types used for a Dimer Method calculations
10 : !> \par History
11 : !> -Luca Bellucci 11.2017 - CNR-NANO, Pisa
12 : !> add K-DIMER from doi:10.1063/1.4898664
13 : !> \author Luca Bellucci and Teodoro Laino - created [tlaino] - 01.2008
14 : ! **************************************************************************************************
15 : MODULE dimer_methods
16 : USE bibliography, ONLY: Henkelman2014,&
17 : Xiao2014,&
18 : cite_reference
19 : USE cell_methods, ONLY: init_cell
20 : USE cell_types, ONLY: cell_type
21 : USE cp_log_handling, ONLY: cp_get_default_logger,&
22 : cp_logger_type
23 : USE cp_output_handling, ONLY: cp_print_key_finished_output,&
24 : cp_print_key_unit_nr
25 : USE cp_subsys_types, ONLY: cp_subsys_get,&
26 : cp_subsys_set,&
27 : cp_subsys_type,&
28 : pack_subsys_particles,&
29 : unpack_subsys_particles
30 : USE dimer_types, ONLY: dimer_env_type,&
31 : dimer_fixed_atom_control,&
32 : dimer_remove_translation
33 : USE dimer_utils, ONLY: get_theta
34 : USE force_env_methods, ONLY: force_env_calc_energy_force
35 : USE force_env_types, ONLY: force_env_get
36 : USE gopt_f_types, ONLY: gopt_f_type
37 : USE input_constants, ONLY: do_first_rotation_step,&
38 : do_second_rotation_step,&
39 : do_third_rotation_step
40 : USE input_section_types, ONLY: section_vals_get_subs_vals,&
41 : section_vals_type
42 : USE kinds, ONLY: dp
43 : USE motion_utils, ONLY: rot_ana,&
44 : thrs_motion
45 : USE particle_list_types, ONLY: particle_list_type
46 : USE solid_state_coordinates, ONLY: solid_state_force,&
47 : solid_state_unpack,&
48 : ss_cell_dof
49 : USE virial_types, ONLY: virial_type
50 : #include "../base/base_uses.f90"
51 :
52 : IMPLICIT NONE
53 : PRIVATE
54 :
55 : LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .FALSE.
56 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'dimer_methods'
57 :
58 : PUBLIC :: cp_eval_at_ts, dimer_apply_solid_state_geometry
59 :
60 : CONTAINS
61 :
62 : ! **************************************************************************************************
63 : !> \brief Computes the dimer energy/gradients (including the rotation of the dimer)
64 : !> \param gopt_env ...
65 : !> \param x ...
66 : !> \param f ...
67 : !> \param gradient ...
68 : !> \param calc_force ...
69 : !> \date 01.2008
70 : !> \par History
71 : !> none
72 : !> \author Luca Bellucci and Teodoro Laino - created [tlaino]
73 : ! **************************************************************************************************
74 1840 : RECURSIVE SUBROUTINE cp_eval_at_ts(gopt_env, x, f, gradient, calc_force)
75 : TYPE(gopt_f_type), POINTER :: gopt_env
76 : REAL(KIND=dp), DIMENSION(:), POINTER :: x
77 : REAL(KIND=dp), INTENT(OUT) :: f
78 : REAL(KIND=dp), DIMENSION(:), POINTER :: gradient
79 : LOGICAL, INTENT(IN) :: calc_force
80 :
81 : CHARACTER(len=*), PARAMETER :: routineN = 'cp_eval_at_ts'
82 :
83 : INTEGER :: handle, iw
84 : LOGICAL :: eval_analytical
85 : REAL(KIND=dp) :: angle1, angle2, f1, gm1, gm2, norm, swf
86 : TYPE(cp_logger_type), POINTER :: logger
87 : TYPE(dimer_env_type), POINTER :: dimer_env
88 : TYPE(section_vals_type), POINTER :: print_section
89 :
90 1840 : NULLIFY (dimer_env, logger, print_section)
91 1840 : CALL timeset(routineN, handle)
92 1840 : logger => cp_get_default_logger()
93 :
94 1840 : CPASSERT(ASSOCIATED(gopt_env))
95 1840 : dimer_env => gopt_env%dimer_env
96 1840 : CPASSERT(ASSOCIATED(dimer_env))
97 1840 : iw = cp_print_key_unit_nr(logger, gopt_env%geo_section, "PRINT%PROGRAM_RUN_INFO", extension=".log")
98 : ! Possibly rotate Dimer or just compute Gradients of point 0 for Translation
99 1840 : IF (gopt_env%dimer_rotation) THEN
100 : IF (debug_this_module .AND. (iw > 0)) THEN
101 : WRITE (iw, '(A)') "NVEC:"
102 : WRITE (iw, '(3F15.9)') dimer_env%nvec
103 : END IF
104 2176 : SELECT CASE (dimer_env%rot%rotation_step)
105 : CASE (do_first_rotation_step, do_third_rotation_step)
106 738 : eval_analytical = .TRUE.
107 738 : IF ((dimer_env%rot%rotation_step == do_third_rotation_step) .AND. (dimer_env%rot%interpolate_gradient)) THEN
108 : eval_analytical = .FALSE.
109 : END IF
110 : IF (eval_analytical) THEN
111 : ! Compute energy, gradients and rotation vector for R1
112 142 : CALL cp_eval_at_ts_low(gopt_env, x, 1, dimer_env, calc_force, f1, dimer_env%rot%g1)
113 : ELSE
114 596 : angle1 = dimer_env%rot%angle1
115 596 : angle2 = dimer_env%rot%angle2
116 : dimer_env%rot%g1 = SIN(angle1 - angle2)/SIN(angle1)*dimer_env%rot%g1 + &
117 : SIN(angle2)/SIN(angle1)*dimer_env%rot%g1p + &
118 10958 : (1.0_dp - COS(angle2) - SIN(angle2)*TAN(angle1/2.0_dp))*dimer_env%rot%g0
119 : END IF
120 :
121 : ! Determine the theta vector (i.e. the search direction for line minimization)
122 25374 : gradient = -2.0_dp*(dimer_env%rot%g1 - dimer_env%rot%g0)
123 : IF (debug_this_module .AND. (iw > 0)) THEN
124 : WRITE (iw, '(A)') "G1 vector:"
125 : WRITE (iw, '(3F15.9)') dimer_env%rot%g1
126 : WRITE (iw, '(A)') "-2*(G1-G0) vector:"
127 : WRITE (iw, '(3F15.9)') gradient
128 : END IF
129 738 : CALL get_theta(gradient, dimer_env, norm)
130 738 : f = norm
131 738 : dimer_env%cg_rot%norm_theta_old = dimer_env%cg_rot%norm_theta
132 738 : dimer_env%cg_rot%norm_theta = norm
133 :
134 : IF (debug_this_module .AND. (iw > 0)) THEN
135 : WRITE (iw, '(A,F20.10)') "Rotational Force step (1,3): module:", norm
136 : WRITE (iw, '(3F15.9)') gradient
137 : END IF
138 :
139 : ! Compute curvature and derivative of the curvature w.r.t. the rotational angle
140 13056 : dimer_env%rot%curvature = DOT_PRODUCT(dimer_env%rot%g1 - dimer_env%rot%g0, dimer_env%nvec)/dimer_env%dr
141 13056 : dimer_env%rot%dCdp = 2.0_dp*DOT_PRODUCT(dimer_env%rot%g1 - dimer_env%rot%g0, gradient)/dimer_env%dr
142 :
143 738 : dimer_env%rot%rotation_step = do_second_rotation_step
144 13056 : gradient = -gradient
145 : CASE (do_second_rotation_step)
146 : ! Compute energy, gradients and rotation vector for R1
147 700 : CALL cp_eval_at_ts_low(gopt_env, x, 1, dimer_env, calc_force, f1, dimer_env%rot%g1p)
148 12730 : dimer_env%rot%curvature = DOT_PRODUCT(dimer_env%rot%g1p - dimer_env%rot%g0, dimer_env%nvec)/dimer_env%dr
149 700 : dimer_env%rot%rotation_step = do_third_rotation_step
150 :
151 : ! Determine the theta vector (i.e. the search direction for line minimization)
152 : ! This is never used for getting a new theta but is consistent in order to
153 : ! give back the right value of f
154 24760 : gradient = -2.0_dp*(dimer_env%rot%g1p - dimer_env%rot%g0)
155 700 : CALL get_theta(gradient, dimer_env, norm)
156 700 : f = norm
157 :
158 2876 : IF (debug_this_module .AND. (iw > 0)) THEN
159 : WRITE (iw, '(A)') "Rotational Force step (1,3):"
160 : WRITE (iw, '(3F15.9)') gradient
161 : END IF
162 : END SELECT
163 : ELSE
164 : ! Compute energy, gradients and rotation vector for R0
165 402 : CALL cp_eval_at_ts_low(gopt_env, x, 0, dimer_env, calc_force, f, dimer_env%rot%g0)
166 402 : IF (dimer_env%solid_state) dimer_env%enthalpy = f
167 :
168 : ! The dimer is rotated only when we are out of the translation line search
169 402 : IF (.NOT. gopt_env%do_line_search) THEN
170 : IF (debug_this_module .AND. (iw > 0)) THEN
171 : WRITE (iw, '(A)') "Entering the rotation module"
172 : WRITE (iw, '(A)') "G0 vector:"
173 : WRITE (iw, '(3F15.9)') dimer_env%rot%g0
174 : END IF
175 142 : CPASSERT(ASSOCIATED(gopt_env%dimer_rotation_optimizer))
176 : CALL gopt_env%dimer_rotation_optimizer(gopt_env%gopt_dimer_env, x, gopt_env%gopt_dimer_param, &
177 142 : gopt_env%gopt_dimer_env%geo_section)
178 142 : dimer_env%rot%rotation_step = do_first_rotation_step
179 : END IF
180 :
181 402 : print_section => section_vals_get_subs_vals(gopt_env%gopt_dimer_env%geo_section, "PRINT")
182 :
183 : ! Correcting gradients for Translation K-DIMER or STANDARD
184 402 : IF (dimer_env%kdimer) THEN
185 6 : CALL cite_reference(Henkelman2014)
186 : ! K-DIMER
187 6 : IF (iw > 0) THEN
188 3 : WRITE (iw, '(T2,A)') "DIMER| Correcting gradients for Translation with K-DIMER method"
189 : END IF
190 6 : swf = 1.0_dp + EXP(dimer_env%beta*dimer_env%rot%curvature)
191 6 : gm2 = 1.0_dp - (1.0_dp/swf)
192 6 : gm1 = (2.0_dp/swf) - 1.0_dp
193 : gradient = gm2*(dimer_env%rot%g0 - 2.0_dp*DOT_PRODUCT(dimer_env%rot%g0, dimer_env%nvec)*dimer_env%nvec) &
194 168 : - gm1*(DOT_PRODUCT(dimer_env%rot%g0, dimer_env%nvec)*dimer_env%nvec)
195 6 : CALL remove_rot_transl_component(gopt_env, gradient, print_section)
196 : IF (debug_this_module .AND. (iw > 0)) WRITE (iw, *) "K-DIMER", dimer_env%beta, dimer_env%rot%curvature, &
197 : dimer_env%rot%dCdp, gm1, gm2, swf
198 : ELSE
199 396 : IF (iw > 0) THEN
200 198 : WRITE (iw, '(T2,A)') "DIMER| Correcting gradients for Translation with standard method"
201 : END IF
202 396 : IF (dimer_env%rot%curvature > 0) THEN
203 4518 : gradient = -DOT_PRODUCT(dimer_env%rot%g0, dimer_env%nvec)*dimer_env%nvec
204 90 : CALL remove_rot_transl_component(gopt_env, gradient, print_section)
205 : ELSE
206 12600 : gradient = dimer_env%rot%g0 - 2.0_dp*DOT_PRODUCT(dimer_env%rot%g0, dimer_env%nvec)*dimer_env%nvec
207 306 : CALL remove_rot_transl_component(gopt_env, gradient, print_section)
208 : END IF
209 : END IF
210 : IF (debug_this_module .AND. (iw > 0)) THEN
211 : WRITE (iw, *) "final gradient:", gradient
212 : WRITE (iw, '(A,F20.10)') "norm gradient:", NORM2(gradient)
213 : END IF
214 402 : IF (.NOT. gopt_env%do_line_search) THEN
215 2098 : f = NORM2(gradient)
216 142 : IF (dimer_env%solid_state) THEN
217 14 : CALL cite_reference(Xiao2014)
218 14 : IF (iw > 0) THEN
219 7 : WRITE (iw, '(T2,A,1X,F22.12)') "SS-DIMER| Enthalpy [au]", dimer_env%enthalpy
220 7 : WRITE (iw, '(T2,A,1X,ES20.10)') "SS-DIMER| Curvature [au]", dimer_env%rot%curvature
221 91 : WRITE (iw, '(T2,A,1X,ES20.10)') "SS-DIMER| Joint-force norm [au]", NORM2(dimer_env%rot%g0)
222 : END IF
223 : END IF
224 : ELSE
225 3932 : f = -DOT_PRODUCT(gradient, dimer_env%tsl%tls_vec)
226 : END IF
227 : END IF
228 1840 : CALL cp_print_key_finished_output(iw, logger, gopt_env%geo_section, "PRINT%PROGRAM_RUN_INFO")
229 1840 : CALL timestop(handle)
230 1840 : END SUBROUTINE cp_eval_at_ts
231 :
232 : ! **************************************************************************************************
233 : !> \brief This function removes translational forces after project of the gradient
234 : !> \param gopt_env ...
235 : !> \param gradient ...
236 : !> \param print_section ...
237 : !> \par History
238 : !> 2016/03/02 [LTong] added fixed atom constraint for gradient
239 : !> \author Luca Bellucci and Teodoro Laino - created [tlaino] - 01.2008
240 : ! **************************************************************************************************
241 402 : SUBROUTINE remove_rot_transl_component(gopt_env, gradient, print_section)
242 : TYPE(gopt_f_type), POINTER :: gopt_env
243 : REAL(KIND=dp), DIMENSION(:) :: gradient
244 : TYPE(section_vals_type), POINTER :: print_section
245 :
246 : CHARACTER(len=*), PARAMETER :: routineN = 'remove_rot_transl_component'
247 :
248 : INTEGER :: dof, handle, i, j, natoms
249 : REAL(KIND=dp) :: norm, norm_gradient_old
250 402 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: D, mat
251 : TYPE(cp_subsys_type), POINTER :: subsys
252 : TYPE(particle_list_type), POINTER :: particles
253 :
254 402 : CALL timeset(routineN, handle)
255 402 : NULLIFY (mat)
256 402 : CALL force_env_get(gopt_env%force_env, subsys=subsys)
257 402 : CALL cp_subsys_get(subsys, particles=particles)
258 :
259 402 : natoms = particles%n_els
260 402 : IF (gopt_env%dimer_env%solid_state) THEN
261 30 : CALL dimer_remove_translation(gradient, natoms)
262 30 : CALL timestop(handle)
263 30 : RETURN
264 : END IF
265 5640 : norm_gradient_old = NORM2(gradient)
266 372 : IF (norm_gradient_old > 0.0_dp) THEN
267 372 : IF (natoms > 1) THEN
268 : CALL rot_ana(particles%els, mat, dof, print_section, keep_rotations=.FALSE., &
269 334 : mass_weighted=.FALSE., natoms=natoms)
270 :
271 : ! Orthogonalize gradient with respect to the full set of Roto-Trasl vectors
272 1336 : ALLOCATE (D(3*natoms, dof))
273 : ! Check First orthogonality in the first element of the basis set
274 2294 : DO i = 1, dof
275 63280 : D(:, i) = mat(:, i)
276 7084 : DO j = i + 1, dof
277 80780 : norm = DOT_PRODUCT(mat(:, i), mat(:, j))
278 6750 : CPASSERT(ABS(norm) < thrs_motion)
279 : END DO
280 : END DO
281 2294 : DO i = 1, dof
282 32620 : norm = DOT_PRODUCT(gradient, D(:, i))
283 32954 : gradient = gradient - norm*D(:, i)
284 : END DO
285 334 : DEALLOCATE (D)
286 334 : DEALLOCATE (mat)
287 : END IF
288 : END IF
289 : ! apply constraint
290 372 : CALL dimer_fixed_atom_control(gradient, subsys)
291 372 : CALL timestop(handle)
292 402 : END SUBROUTINE remove_rot_transl_component
293 :
294 : ! **************************************************************************************************
295 : !> \brief ...
296 : !> \param gopt_env ...
297 : !> \param x ...
298 : !> \param dimer_index ...
299 : !> \param dimer_env ...
300 : !> \param calc_force ...
301 : !> \param f ...
302 : !> \param gradient ...
303 : !> \par History
304 : !> none
305 : !> \author Luca Bellucci and Teodoro Laino - created [tlaino] - 01.2008
306 : ! **************************************************************************************************
307 1244 : SUBROUTINE cp_eval_at_ts_low(gopt_env, x, dimer_index, dimer_env, calc_force, &
308 1244 : f, gradient)
309 : TYPE(gopt_f_type), POINTER :: gopt_env
310 : REAL(KIND=dp), DIMENSION(:), POINTER :: x
311 : INTEGER, INTENT(IN) :: dimer_index
312 : TYPE(dimer_env_type), POINTER :: dimer_env
313 : LOGICAL, INTENT(IN) :: calc_force
314 : REAL(KIND=dp), INTENT(OUT), OPTIONAL :: f
315 : REAL(KIND=dp), DIMENSION(:), OPTIONAL :: gradient
316 :
317 : CHARACTER(len=*), PARAMETER :: routineN = 'cp_eval_at_ts_low'
318 :
319 : INTEGER :: handle, idg, idir, ip
320 2488 : REAL(KIND=dp) :: pv, work(SIZE(x)), &
321 1244 : forces(SIZE(x) - ss_cell_dof)
322 : TYPE(cell_type), POINTER :: cell
323 : TYPE(cp_subsys_type), POINTER :: subsys
324 : TYPE(particle_list_type), POINTER :: particles
325 : TYPE(virial_type), POINTER :: virial
326 :
327 1244 : CALL timeset(routineN, handle)
328 1244 : IF (dimer_env%solid_state) THEN
329 650 : work(:) = x + (REAL(dimer_index, KIND=dp)*dimer_env%dr)*dimer_env%nvec
330 50 : CALL dimer_apply_solid_state_geometry(gopt_env, work)
331 50 : CALL force_env_calc_energy_force(gopt_env%force_env, calc_force=calc_force)
332 50 : CALL force_env_get(gopt_env%force_env, cell=cell, subsys=subsys)
333 50 : pv = dimer_env%external_pressure*cell%deth
334 50 : IF (PRESENT(f)) THEN
335 50 : CALL force_env_get(gopt_env%force_env, potential_energy=f)
336 50 : f = f + pv
337 : END IF
338 50 : IF (PRESENT(gradient)) THEN
339 50 : CALL cp_subsys_get(subsys, virial=virial)
340 50 : CALL pack_subsys_particles(subsys, f=forces)
341 50 : IF (dimer_index == 0) THEN
342 210 : dimer_env%centre_force(:) = forces
343 30 : dimer_env%centre_virial = virial
344 : END IF
345 50 : CALL solid_state_force(dimer_env%frame, work, forces, virial%pv_virial, pv, gradient)
346 50 : CALL dimer_remove_translation(gradient, SIZE(forces)/3)
347 650 : gradient(:) = -gradient
348 : END IF
349 50 : CALL timestop(handle)
350 : RETURN
351 : END IF
352 1194 : idg = 0
353 1194 : CALL force_env_get(gopt_env%force_env, subsys=subsys)
354 1194 : CALL cp_subsys_get(subsys, particles=particles)
355 7532 : DO ip = 1, particles%n_els
356 26546 : DO idir = 1, 3
357 19014 : idg = idg + 1
358 25352 : particles%els(ip)%r(idir) = x(idg) + REAL(dimer_index, KIND=dp)*dimer_env%nvec(idg)*dimer_env%dr
359 : END DO
360 : END DO
361 :
362 : ! Compute energy and forces
363 1194 : CALL force_env_calc_energy_force(gopt_env%force_env, calc_force=calc_force)
364 :
365 : ! Possibly take the potential energy
366 1194 : IF (PRESENT(f)) THEN
367 1194 : CALL force_env_get(gopt_env%force_env, potential_energy=f)
368 : END IF
369 :
370 : ! Possibly take the gradients
371 1194 : IF (PRESENT(gradient)) THEN
372 1194 : idg = 0
373 1194 : CALL cp_subsys_get(subsys, particles=particles)
374 7532 : DO ip = 1, particles%n_els
375 26546 : DO idir = 1, 3
376 19014 : idg = idg + 1
377 19014 : CPASSERT(SIZE(gradient) >= idg)
378 25352 : gradient(idg) = -particles%els(ip)%f(idir)
379 : END DO
380 : END DO
381 : END IF
382 1194 : CALL timestop(handle)
383 : END SUBROUTINE cp_eval_at_ts_low
384 :
385 : ! **************************************************************************************************
386 : !> \brief Install a solid-state dimer centre or displaced image in the force environment.
387 : !> \param gopt_env optimizer environment
388 : !> \param x joint atomic and cell coordinates
389 : ! **************************************************************************************************
390 154 : SUBROUTINE dimer_apply_solid_state_geometry(gopt_env, x)
391 : TYPE(gopt_f_type), INTENT(IN) :: gopt_env
392 : REAL(KIND=dp), INTENT(IN) :: x(:)
393 :
394 308 : REAL(KIND=dp) :: h(3, 3), r(SIZE(x) - ss_cell_dof)
395 : TYPE(cell_type), POINTER :: cell
396 : TYPE(cp_subsys_type), POINTER :: subsys
397 :
398 154 : CALL solid_state_unpack(gopt_env%dimer_env%frame, x, h, r)
399 154 : CALL force_env_get(gopt_env%force_env, cell=cell, subsys=subsys)
400 154 : CALL init_cell(cell, hmat=h)
401 154 : CALL cp_subsys_set(subsys, cell=cell)
402 154 : CALL unpack_subsys_particles(subsys, r=r)
403 154 : END SUBROUTINE dimer_apply_solid_state_geometry
404 :
405 : END MODULE dimer_methods
|