(git:374b731)
Loading...
Searching...
No Matches
cg_optimizer.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
9!> \brief Routines for Geometry optimization using Conjugate Gradients
10!> \author Teodoro Laino [teo]
11!> 10.2005
12! **************************************************************************************************
14
15 USE cell_types, ONLY: cell_type
16 USE cg_utils, ONLY: cg_linmin, &
21 USE cp_output_handling, ONLY: cp_iterate, &
25 USE force_env_types, ONLY: force_env_get, &
28 USE gopt_f_methods, ONLY: gopt_f_ii, &
29 gopt_f_io, &
34 USE gopt_f_types, ONLY: gopt_f_type
45 USE kinds, ONLY: dp
46 USE machine, ONLY: m_walltime
49 print_spgr, &
53#include "../base/base_uses.f90"
54
55 IMPLICIT NONE
56 PRIVATE
57
58
59! **************************************************************************************************
60!> \brief evaluate the potential energy and its gradients using an array
61!> with same dimension as the particle_set
62!> \param gopt_env the geometry optimization environment
63!> \param x the position where the function should be evaluated
64!> \param f the function value
65!> \param gradient the value of its gradient
66!> \par History
67!> none
68!> \author Teodoro Laino [tlaino] - University of Zurich - 01.2008
69! **************************************************************************************************
70INTERFACE
71
72 SUBROUTINE cp_eval_at(gopt_env, x, f, gradient, master, &
73 final_evaluation, para_env)
74
76 USE gopt_f_types, ONLY: gopt_f_type
77 USE kinds, ONLY: dp
78
79 TYPE(gopt_f_type), POINTER :: gopt_env
80 REAL(KIND=dp), DIMENSION(:), POINTER :: x
81 REAL(KIND=dp), INTENT(out), OPTIONAL :: f
82 REAL(KIND=dp), DIMENSION(:), OPTIONAL, &
83 POINTER :: gradient
84 INTEGER, INTENT(IN) :: master
85 LOGICAL, INTENT(IN), OPTIONAL :: final_evaluation
86 TYPE(mp_para_env_type), POINTER :: para_env
87
88 END SUBROUTINE cp_eval_at
89
90END INTERFACE
91
92 PUBLIC :: geoopt_cg
93 LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .true.
94 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cg_optimizer'
95
96CONTAINS
97
98! **************************************************************************************************
99!> \brief Driver for conjugate gradient optimization technique
100!> \param force_env ...
101!> \param gopt_param ...
102!> \param globenv ...
103!> \param geo_section ...
104!> \param gopt_env ...
105!> \param x0 ...
106!> \param do_update ...
107!> \par History
108!> 10.2005 created [tlaino]
109!> \author Teodoro Laino
110! **************************************************************************************************
111 RECURSIVE SUBROUTINE geoopt_cg(force_env, gopt_param, globenv, geo_section, &
112 gopt_env, x0, do_update)
113
114 TYPE(force_env_type), POINTER :: force_env
115 TYPE(gopt_param_type), POINTER :: gopt_param
116 TYPE(global_environment_type), POINTER :: globenv
117 TYPE(section_vals_type), POINTER :: geo_section
118 TYPE(gopt_f_type), POINTER :: gopt_env
119 REAL(kind=dp), DIMENSION(:), POINTER :: x0
120 LOGICAL, INTENT(OUT), OPTIONAL :: do_update
121
122 CHARACTER(len=*), PARAMETER :: routinen = 'geoopt_cg'
123
124 INTEGER :: handle, output_unit
125 LOGICAL :: my_do_update
126 TYPE(cp_logger_type), POINTER :: logger
127 TYPE(cp_subsys_type), POINTER :: subsys
128 TYPE(spgr_type), POINTER :: spgr
129
130 CALL timeset(routinen, handle)
131
132 NULLIFY (spgr)
133 logger => cp_get_default_logger()
134 spgr => gopt_env%spgr
135
136 output_unit = cp_print_key_unit_nr(logger, geo_section, "PRINT%PROGRAM_RUN_INFO", &
137 extension=".geoLog")
138 CALL print_geo_opt_header(gopt_env, output_unit, "CONJUGATE GRADIENTS")
139
140 ! find space_group
141 CALL force_env_get(force_env, subsys=subsys)
142 CALL section_vals_val_get(geo_section, "KEEP_SPACE_GROUP", l_val=spgr%keep_space_group)
143 IF (spgr%keep_space_group) THEN
144 SELECT CASE (gopt_env%type_id)
146 CALL force_env_get(force_env, subsys=subsys)
147 CALL identify_space_group(subsys, geo_section, gopt_env, output_unit)
148 CALL spgr_apply_rotations_coord(spgr, x0)
149 CALL print_spgr(spgr)
151 SELECT CASE (gopt_env%cell_method_id)
153 CALL force_env_get(force_env, subsys=subsys)
154 CALL identify_space_group(subsys, geo_section, gopt_env, output_unit)
155 CALL spgr_apply_rotations_coord(spgr, x0)
156 CALL print_spgr(spgr)
158 spgr%keep_space_group = .false.
159 CASE (default_cell_md_id)
160 cpabort("KEEP_SPACE_GROUP not implemented for motion method MD.")
161 CASE DEFAULT
162 spgr%keep_space_group = .false.
163 END SELECT
164 CASE DEFAULT
165 spgr%keep_space_group = .false.
166 END SELECT
167 END IF
168
169 CALL cp_cg_main(force_env, x0, gopt_param, output_unit, globenv, &
170 gopt_env, do_update=my_do_update)
171
172 CALL cp_print_key_finished_output(output_unit, logger, geo_section, &
173 "PRINT%PROGRAM_RUN_INFO")
174 IF (PRESENT(do_update)) do_update = my_do_update
175
176 CALL timestop(handle)
177
178 END SUBROUTINE geoopt_cg
179
180! **************************************************************************************************
181!> \brief This really performs the conjugate gradients optimization
182!> \param force_env ...
183!> \param x0 ...
184!> \param gopt_param ...
185!> \param output_unit ...
186!> \param globenv ...
187!> \param gopt_env ...
188!> \param do_update ...
189!> \par History
190!> 10.2005 created [tlaino]
191!> \author Teodoro Laino
192! **************************************************************************************************
193 RECURSIVE SUBROUTINE cp_cg_main(force_env, x0, gopt_param, output_unit, globenv, &
194 gopt_env, do_update)
195 TYPE(force_env_type), POINTER :: force_env
196 REAL(kind=dp), DIMENSION(:), POINTER :: x0
197 TYPE(gopt_param_type), POINTER :: gopt_param
198 INTEGER, INTENT(IN) :: output_unit
199 TYPE(global_environment_type), POINTER :: globenv
200 TYPE(gopt_f_type), POINTER :: gopt_env
201 LOGICAL, INTENT(OUT), OPTIONAL :: do_update
202
203 CHARACTER(len=*), PARAMETER :: routinen = 'cp_cg_main'
204
205 CHARACTER(LEN=5) :: wildcard
206 INTEGER :: handle, iter_nr, its, max_steep_steps, &
207 maxiter
208 LOGICAL :: conv, fletcher_reeves, &
209 save_consistent_energy_force, &
210 should_stop
211 REAL(kind=dp) :: emin, eold, opt_energy, res_lim, t_diff, &
212 t_now, t_old
213 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: xold
214 REAL(kind=dp), DIMENSION(:), POINTER :: g, h, xi
215 TYPE(cell_type), POINTER :: cell
216 TYPE(cp_logger_type), POINTER :: logger
217 TYPE(cp_subsys_type), POINTER :: subsys
218 TYPE(section_vals_type), POINTER :: root_section
219 TYPE(spgr_type), POINTER :: spgr
220
221 CALL timeset(routinen, handle)
222 t_old = m_walltime()
223 NULLIFY (logger, g, h, xi, spgr)
224 root_section => force_env%root_section
225 logger => cp_get_default_logger()
226 conv = .false.
227 maxiter = gopt_param%max_iter
228 max_steep_steps = gopt_param%max_steep_steps
229 fletcher_reeves = gopt_param%Fletcher_Reeves
230 res_lim = gopt_param%restart_limit
231 ALLOCATE (g(SIZE(x0)))
232 ALLOCATE (h(SIZE(x0)))
233 ALLOCATE (xi(SIZE(x0)))
234 ALLOCATE (xold(SIZE(x0)))
235 CALL force_env_get(force_env, cell=cell, subsys=subsys)
236
237 spgr => gopt_env%spgr
238 ! applies rotation matrices to coordinates
239 IF (spgr%keep_space_group) THEN
240 CALL spgr_apply_rotations_coord(spgr, x0)
241 END IF
242
243 ! Evaluate energy and forces at the first step
244 ![NB] consistent energies and forces not required for CG, but some line minimizers might set it
245 save_consistent_energy_force = gopt_env%require_consistent_energy_force
246 gopt_env%require_consistent_energy_force = .false.
247
248 CALL cp_eval_at(gopt_env, x0, opt_energy, xi, gopt_env%force_env%para_env%mepos, &
249 .false., gopt_env%force_env%para_env)
250
251 gopt_env%require_consistent_energy_force = save_consistent_energy_force
252
253 ! Symmetrize coordinates and forces
254 IF (spgr%keep_space_group) THEN
255 CALL spgr_apply_rotations_coord(spgr, x0)
256 CALL spgr_apply_rotations_force(spgr, xi)
257 END IF
258
259 g = -xi
260 h = g
261 xi = h
262 emin = huge(0.0_dp)
263 CALL cp_iterate(logger%iter_info, increment=0, iter_nr_out=iter_nr)
264 ! Main Loop
265 wildcard = " SD"
266 t_now = m_walltime()
267 t_diff = t_now - t_old
268 t_old = t_now
269 CALL gopt_f_io_init(gopt_env, output_unit, opt_energy, wildcard, used_time=t_diff, its=iter_nr)
270 eold = opt_energy
271 DO its = iter_nr + 1, maxiter
272 CALL cp_iterate(logger%iter_info, last=(its == maxiter))
273 CALL section_vals_val_set(gopt_env%geo_section, "STEP_START_VAL", i_val=its)
274 CALL gopt_f_ii(its, output_unit)
275
276 ! Symmetrize coordinates and forces
277 IF (spgr%keep_space_group) THEN
278 CALL spgr_apply_rotations_coord(spgr, x0)
279 CALL spgr_apply_rotations_force(spgr, g)
280 CALL spgr_apply_rotations_force(spgr, xi)
281 END IF
282
283 xold(:) = x0
284
285 ! Line minimization
286 CALL cg_linmin(gopt_env, x0, xi, g, opt_energy, output_unit, gopt_param, globenv)
287
288 ! Applies rotation matrices to coordinates
289 IF (spgr%keep_space_group) THEN
290 CALL spgr_apply_rotations_coord(spgr, x0)
291 END IF
292
293 ! Check for an external exit command
294 CALL external_control(should_stop, "GEO", globenv=globenv)
295 IF (should_stop) EXIT
296
297 ! Some IO and Convergence check
298 t_now = m_walltime()
299 t_diff = t_now - t_old
300 t_old = t_now
301 CALL gopt_f_io(gopt_env, force_env, root_section, its, opt_energy, &
302 output_unit, eold, emin, wildcard, gopt_param, SIZE(x0), x0 - xold, xi, conv, &
303 used_time=t_diff)
304 eold = opt_energy
305 emin = min(emin, opt_energy)
306
307 IF (conv .OR. (its == maxiter)) EXIT
308 ![NB] consistent energies and forces not required for CG, but some line minimizers might set it
309 save_consistent_energy_force = gopt_env%require_consistent_energy_force
310 gopt_env%require_consistent_energy_force = .false.
311
312 CALL cp_eval_at(gopt_env, x0, opt_energy, xi, gopt_env%force_env%para_env%mepos, &
313 .false., gopt_env%force_env%para_env)
314
315 gopt_env%require_consistent_energy_force = save_consistent_energy_force
316
317 ! Symmetrize coordinates and forces
318 IF (spgr%keep_space_group) THEN
319 CALL spgr_apply_rotations_force(spgr, xi)
320 END IF
321
322 ! Get Conjugate Directions: updates the searching direction (h)
323 wildcard = " CG"
324 CALL get_conjugate_direction(gopt_env, fletcher_reeves, g, xi, h)
325
326 ! Symmetrize coordinates and forces
327 IF (spgr%keep_space_group) THEN
328 CALL spgr_apply_rotations_force(spgr, g)
329 CALL spgr_apply_rotations_force(spgr, h)
330 END IF
331
332 ! Reset Condition or Steepest Descent Requested
333 ! ABS(DOT_PRODUCT(g, h))/SQRT((DOT_PRODUCT(g, g)*DOT_PRODUCT(h, h))) > res_lim ...
334 IF ((dot_product(g, h)*dot_product(g, h)) > (res_lim*res_lim*dot_product(g, g)*dot_product(h, h)) &
335 .OR. its + 1 <= max_steep_steps) THEN
336 ! Steepest Descent
337 wildcard = " SD"
338 h = -xi
339 END IF
340 g = -xi
341 xi = h
342 END DO
343
344 IF (its == maxiter .AND. (.NOT. conv)) THEN
345 CALL print_geo_opt_nc(gopt_env, output_unit)
346 END IF
347
348 ! Write final particle information and restart, if converged
349 IF (PRESENT(do_update)) do_update = conv
350 CALL cp_iterate(logger%iter_info, last=.true., increment=0)
351 CALL gopt_f_io_finalize(gopt_env, force_env, x0, conv, its, root_section, &
352 gopt_env%force_env%para_env, gopt_env%force_env%para_env%mepos, output_unit)
353
354 DEALLOCATE (xold)
355 DEALLOCATE (g)
356 DEALLOCATE (h)
357 DEALLOCATE (xi)
358
359 CALL timestop(handle)
360
361 END SUBROUTINE cp_cg_main
362
363END MODULE cg_optimizer
subroutine cp_eval_at(gopt_env, x, f, gradient, master, final_evaluation, para_env)
evaluete the potential energy and its gradients using an array with same dimension as the particle_se...
Handles all functions related to the CELL.
Definition cell_types.F:15
Routines for Geometry optimization using Conjugate Gradients.
recursive subroutine, public geoopt_cg(force_env, gopt_param, globenv, geo_section, gopt_env, x0, do_update)
Driver for conjugate gradient optimization technique.
Utilities for Geometry optimization using Conjugate Gradients.
Definition cg_utils.F:13
subroutine, public get_conjugate_direction(gopt_env, fletcher_reeves, g, xi, h)
Computes the Conjugate direction for the next search.
Definition cg_utils.F:1065
recursive subroutine, public cg_linmin(gopt_env, xvec, xi, g, opt_energy, output_unit, gopt_param, globenv)
Main driver for line minimization routines for CG.
Definition cg_utils.F:93
Routines to handle the external control of CP2K.
subroutine, public external_control(should_stop, flag, globenv, target_time, start_time, force_check)
External manipulations during a run : when the <PROJECT_NAME>.EXIT_$runtype command is sent the progr...
various routines to log and control the output. The idea is that decisions about where to log should ...
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 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,...
subroutine, public cp_iterate(iteration_info, last, iter_nr, increment, iter_nr_out)
adds one to the actual iteration
types that represent a subsys, i.e. a part of the system
Interface for the force calculations.
recursive subroutine, public force_env_get(force_env, in_use, fist_env, qs_env, meta_env, fp_env, subsys, para_env, potential_energy, additional_potential, kinetic_energy, harmonic_shell, kinetic_shell, cell, sub_force_env, qmmm_env, qmmmx_env, eip_env, pwdft_env, globenv, input, force_env_section, method_name_id, root_section, mixed_env, nnp_env, embed_env)
returns various attributes about the force environment
Define type storing the global information of a run. Keep the amount of stored data small....
contains a functional that calculates the energy and its derivatives for the geometry optimizer
subroutine, public print_geo_opt_header(gopt_env, output_unit, label)
...
subroutine, public gopt_f_io_init(gopt_env, output_unit, opt_energy, wildcard, its, used_time)
Handles the Output during an optimization run.
recursive subroutine, public gopt_f_io_finalize(gopt_env, force_env, x0, conv, its, root_section, para_env, master, output_unit)
Handles the Output at the end of an optimization run.
subroutine, public gopt_f_io(gopt_env, force_env, root_section, its, opt_energy, output_unit, eold, emin, wildcard, gopt_param, ndf, dx, xi, conv, pred, rat, step, rad, used_time)
Handles the Output during an optimization run.
subroutine, public print_geo_opt_nc(gopt_env, output_unit)
...
subroutine, public gopt_f_ii(its, output_unit)
Prints iteration step of the optimization procedure on screen.
contains a functional that calculates the energy and its derivatives for the geometry optimizer
contains typo and related routines to handle parameters controlling the GEO_OPT module
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public default_cell_geo_opt_id
integer, parameter, public default_cell_method_id
integer, parameter, public default_minimization_method_id
integer, parameter, public default_ts_method_id
integer, parameter, public default_cell_md_id
integer, parameter, public default_cell_direct_id
objects that represent the structure of input sections and the data contained in an input section
subroutine, public section_vals_val_set(section_vals, keyword_name, i_rep_section, i_rep_val, val, l_val, i_val, r_val, c_val, l_vals_ptr, i_vals_ptr, r_vals_ptr, c_vals_ptr)
sets the requested value
subroutine, public section_vals_val_get(section_vals, keyword_name, i_rep_section, i_rep_val, n_rep_val, val, l_val, i_val, r_val, c_val, l_vals, i_vals, r_vals, c_vals, explicit)
returns the requested value
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Machine interface based on Fortran 2003 and POSIX.
Definition machine.F:17
real(kind=dp) function, public m_walltime()
returns time from a real-time clock, protected against rolling early/easily
Definition machine.F:123
Interface to the message passing library MPI.
Space Group Symmetry Type Module (version 1.0, Ferbruary 12, 2021)
Space Group Symmetry Module (version 1.0, January 16, 2020)
subroutine, public print_spgr(spgr)
routine prints Space Group Information.
subroutine, public spgr_apply_rotations_coord(spgr, coord)
routine applies the rotation matrices to the coordinates.
subroutine, public identify_space_group(subsys, geo_section, gopt_env, iunit)
routine indentifies the space group and finds rotation matrices.
subroutine, public spgr_apply_rotations_force(spgr, force)
routine applies the rotation matrices to the forces.
Type defining parameters related to the simulation cell.
Definition cell_types.F:55
type of a logger, at the moment it contains just a print level starting at which level it should be l...
represents a system: atoms, molecules, their pos,vel,...
wrapper to abstract the force evaluation of the various methods
contains the initially parsed file and the initial parallel environment
calculates the potential energy of a system, and its derivatives
stores all the informations relevant to an mpi environment