(git:27c966b)
Loading...
Searching...
No Matches
glbopt_worker.F
Go to the documentation of this file.
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 Worker routines used by global optimization schemes
10!> \author Ole Schuett
11! **************************************************************************************************
24 USE geo_opt, ONLY: cp_geo_opt
31 USE kinds, ONLY: default_string_length,&
32 dp
33 USE md_run, ONLY: qs_mol_dyn
37 USE physcon, ONLY: angstrom,&
38 kelvin
42#include "../base/base_uses.f90"
43
44 IMPLICIT NONE
45 PRIVATE
46
47 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'glbopt_worker'
48
50 PUBLIC :: glbopt_worker_execute
51 PUBLIC :: glbopt_worker_type
52
54 PRIVATE
55 INTEGER :: id = -1
56 INTEGER :: iw = -1
57 INTEGER :: f_env_id = -1
58 TYPE(f_env_type), POINTER :: f_env => null()
59 TYPE(force_env_type), POINTER :: force_env => null()
60 TYPE(cp_subsys_type), POINTER :: subsys => null()
61 TYPE(section_vals_type), POINTER :: root_section => null()
62 TYPE(global_environment_type), POINTER :: globenv => null()
63 INTEGER :: gopt_max_iter = 0
64 INTEGER :: bump_steps_downwards = 0
65 INTEGER :: bump_steps_upwards = 0
66 INTEGER :: md_bumps_max = 0
67 REAL(kind=dp) :: fragmentation_threshold = 0.0_dp
68 INTEGER :: n_atoms = -1
69 END TYPE glbopt_worker_type
70
71CONTAINS
72
73! **************************************************************************************************
74!> \brief Initializes worker for global optimization
75!> \param worker ...
76!> \param input_declaration ...
77!> \param para_env ...
78!> \param root_section ...
79!> \param input_path ...
80!> \param worker_id ...
81!> \param iw ...
82!> \author Ole Schuett
83! **************************************************************************************************
84 SUBROUTINE glbopt_worker_init(worker, input_declaration, para_env, root_section, &
85 input_path, worker_id, iw)
86 TYPE(glbopt_worker_type), INTENT(INOUT) :: worker
87 TYPE(section_type), POINTER :: input_declaration
88 TYPE(mp_para_env_type), POINTER :: para_env
89 TYPE(section_vals_type), POINTER :: root_section
90 CHARACTER(LEN=*), INTENT(IN) :: input_path
91 INTEGER, INTENT(in) :: worker_id, iw
92
93 INTEGER :: i
94 REAL(kind=dp) :: dist_in_angstrom
95 TYPE(section_vals_type), POINTER :: glbopt_section
96
97 worker%root_section => root_section
98 worker%id = worker_id
99 worker%iw = iw
100
101 ! ======= Create f_env =======
102 CALL create_force_env(worker%f_env_id, &
103 input_declaration=input_declaration, &
104 input_path=input_path, &
105 input=root_section, &
106 output_unit=worker%iw, &
107 mpi_comm=para_env)
108
109 ! ======= More setup stuff =======
110 CALL f_env_add_defaults(worker%f_env_id, worker%f_env)
111 worker%force_env => worker%f_env%force_env
112 CALL force_env_get(worker%force_env, globenv=worker%globenv, subsys=worker%subsys)
113
114 ! We want different random-number-streams for each worker
115 DO i = 1, worker_id
116 CALL worker%globenv%gaussian_rng_stream%reset_to_next_substream()
117 END DO
118
119 CALL cp_subsys_get(worker%subsys, natom=worker%n_atoms)
120
121 ! fetch original value from input
122 CALL section_vals_val_get(root_section, "MOTION%GEO_OPT%MAX_ITER", i_val=worker%gopt_max_iter)
123 glbopt_section => section_vals_get_subs_vals(root_section, "SWARM%GLOBAL_OPT")
124
125 CALL section_vals_val_get(glbopt_section, "BUMP_STEPS_UPWARDS", i_val=worker%bump_steps_upwards)
126 CALL section_vals_val_get(glbopt_section, "BUMP_STEPS_DOWNWARDS", i_val=worker%bump_steps_downwards)
127 CALL section_vals_val_get(glbopt_section, "MD_BUMPS_MAX", i_val=worker%md_bumps_max)
128 CALL section_vals_val_get(glbopt_section, "FRAGMENTATION_THRESHOLD", r_val=dist_in_angstrom)
129 worker%fragmentation_threshold = dist_in_angstrom/angstrom
130 END SUBROUTINE glbopt_worker_init
131
132! **************************************************************************************************
133!> \brief Central execute routine of global optimization worker
134!> \param worker ...
135!> \param cmd ...
136!> \param report ...
137!> \author Ole Schuett
138! **************************************************************************************************
139 SUBROUTINE glbopt_worker_execute(worker, cmd, report)
140 TYPE(glbopt_worker_type), INTENT(INOUT) :: worker
141 TYPE(swarm_message_type), INTENT(IN) :: cmd
142 TYPE(swarm_message_type), INTENT(INOUT) :: report
143
144 CHARACTER(len=default_string_length) :: command
145
146 CALL swarm_message_get(cmd, "command", command)
147 IF (trim(command) == "md_and_gopt") THEN
148 CALL run_mdgopt(worker, cmd, report)
149 ELSE
150 cpabort("Worker: received unknown command")
151 END IF
152
153 END SUBROUTINE glbopt_worker_execute
154
155! **************************************************************************************************
156!> \brief Performs an escape attempt as need by e.g. Minima Hopping
157!> \param worker ...
158!> \param cmd ...
159!> \param report ...
160!> \author Ole Schuett
161! **************************************************************************************************
162 SUBROUTINE run_mdgopt(worker, cmd, report)
163 TYPE(glbopt_worker_type), INTENT(INOUT) :: worker
164 TYPE(swarm_message_type), INTENT(IN) :: cmd
165 TYPE(swarm_message_type), INTENT(INOUT) :: report
166
167 INTEGER :: gopt_steps, iframe, md_steps, &
168 n_fragments, prev_iframe
169 REAL(kind=dp) :: epot, temperature
170 REAL(kind=dp), DIMENSION(:), POINTER :: positions
171 TYPE(glbopt_mdctrl_data_type), TARGET :: mdctrl_data
172 TYPE(mdctrl_type), POINTER :: mdctrl_p
173 TYPE(mdctrl_type), TARGET :: mdctrl
174
175 NULLIFY (positions)
176
177 CALL swarm_message_get(cmd, "temperature", temperature)
178 CALL swarm_message_get(cmd, "iframe", iframe)
179 IF (iframe > 1) THEN
180 CALL swarm_message_get(cmd, "positions", positions)
181 CALL unpack_subsys_particles(worker%subsys, r=positions)
182 END IF
183
184 ! setup mdctrl callback
185 ALLOCATE (mdctrl_data%epot_history(worker%bump_steps_downwards + worker%bump_steps_upwards + 1))
186 mdctrl_data%epot_history = 0.0_dp
187 mdctrl_data%md_bump_counter = 0
188 mdctrl_data%bump_steps_upwards = worker%bump_steps_upwards
189 mdctrl_data%bump_steps_downwards = worker%bump_steps_downwards
190 mdctrl_data%md_bumps_max = worker%md_bumps_max
191 mdctrl_data%output_unit = worker%iw
192 mdctrl%glbopt => mdctrl_data
193 mdctrl_p => mdctrl
194
195 prev_iframe = iframe
196 IF (iframe == 0) iframe = 1 ! qs_mol_dyn behaves differently for STEP_START_VAL=0
197 CALL section_vals_val_set(worker%root_section, "MOTION%MD%STEP_START_VAL", i_val=iframe - 1)
198 CALL section_vals_val_set(worker%root_section, "MOTION%MD%TEMPERATURE", r_val=temperature)
199
200 IF (worker%iw > 0) THEN
201 WRITE (worker%iw, '(A,33X,F20.3)') ' GLBOPT| MD temperature [K]', temperature*kelvin
202 WRITE (worker%iw, '(A,29X,I10)') " GLBOPT| Starting MD at trajectory frame ", iframe
203 END IF
204
205 ! run MD
206 CALL qs_mol_dyn(worker%force_env, worker%globenv, mdctrl=mdctrl_p)
207
208 iframe = mdctrl_data%itimes + 1
209 md_steps = iframe - prev_iframe
210 IF (worker%iw > 0) WRITE (worker%iw, '(A,I4,A)') " GLBOPT| md ended after ", md_steps, " steps."
211
212 ! fix fragmentation
213 IF (.NOT. ASSOCIATED(positions)) ALLOCATE (positions(3*worker%n_atoms))
214 CALL pack_subsys_particles(worker%subsys, r=positions)
215 n_fragments = 0
216 DO
217 n_fragments = n_fragments + 1
218 IF (fix_fragmentation(positions, worker%fragmentation_threshold)) EXIT
219 END DO
220 CALL unpack_subsys_particles(worker%subsys, r=positions)
221
222 IF (n_fragments > 0 .AND. worker%iw > 0) THEN
223 WRITE (worker%iw, '(A,13X,I10)') " GLBOPT| Ran fix_fragmentation times:", n_fragments
224 END IF
225
226 ! setup geometry optimization
227 IF (worker%iw > 0) WRITE (worker%iw, '(A,13X,I10)') " GLBOPT| Starting local optimisation at trajectory frame ", iframe
228 CALL section_vals_val_set(worker%root_section, "MOTION%GEO_OPT%STEP_START_VAL", i_val=iframe - 1)
229 CALL section_vals_val_set(worker%root_section, "MOTION%GEO_OPT%MAX_ITER", &
230 i_val=iframe + worker%gopt_max_iter)
231
232 ! run geometry optimization
233 CALL cp_geo_opt(worker%force_env, worker%globenv, rm_restart_info=.false.)
234
235 prev_iframe = iframe
236 CALL section_vals_val_get(worker%root_section, "MOTION%GEO_OPT%STEP_START_VAL", i_val=iframe)
237 iframe = iframe + 2 ! Compensates for different START_VAL interpretation.
238 gopt_steps = iframe - prev_iframe - 1
239 IF (worker%iw > 0) WRITE (worker%iw, '(A,I4,A)') " GLBOPT| gopt ended after ", gopt_steps, " steps."
240 CALL force_env_get(worker%force_env, potential_energy=epot)
241 IF (worker%iw > 0) WRITE (worker%iw, '(A,25X,E20.10)') ' GLBOPT| Potential Energy [Hartree]', epot
242
243 ! assemble report
244 CALL swarm_message_add(report, "Epot", epot)
245 CALL swarm_message_add(report, "iframe", iframe)
246 CALL swarm_message_add(report, "md_steps", md_steps)
247 CALL swarm_message_add(report, "gopt_steps", gopt_steps)
248 CALL pack_subsys_particles(worker%subsys, r=positions)
249 CALL swarm_message_add(report, "positions", positions)
250
251 DEALLOCATE (positions)
252 END SUBROUTINE run_mdgopt
253
254! **************************************************************************************************
255!> \brief Helper routine for run_mdgopt, fixes a fragmented atomic cluster.
256!> \param positions ...
257!> \param bondlength ...
258!> \return ...
259!> \author Stefan Goedecker
260! **************************************************************************************************
261 FUNCTION fix_fragmentation(positions, bondlength) RESULT(all_connected)
262 REAL(kind=dp), DIMENSION(:) :: positions
263 REAL(kind=dp) :: bondlength
264 LOGICAL :: all_connected
265
266 REAL(kind=dp), PARAMETER :: bond_thre = 1.25_dp
267
268 INTEGER :: cluster_edge, fragment_edge, i, j, &
269 n_particles, stack_size
270 INTEGER, ALLOCATABLE, DIMENSION(:) :: stack
271 LOGICAL, ALLOCATABLE, DIMENSION(:) :: marked
272 REAL(kind=dp) :: d, dr(3), min_dist, s
273
274 n_particles = SIZE(positions)/3
275 ALLOCATE (stack(n_particles), marked(n_particles))
276
277 marked = .false.; stack_size = 0
278
279 ! First particle taken as root of flooding, mark it and push to stack
280 marked(1) = .true.; stack(1) = 1; stack_size = 1
281
282 DO WHILE (stack_size > 0)
283 i = stack(stack_size); stack_size = stack_size - 1 !pop
284 DO j = 1, n_particles
285 IF (norm2(diff(positions, i, j)) < bond_thre*bondlength) THEN ! they are close = they are connected
286 IF (.NOT. marked(j)) THEN
287 marked(j) = .true.
288 stack(stack_size + 1) = j; stack_size = stack_size + 1 !push
289 END IF
290 END IF
291 END DO
292 END DO
293
294 all_connected = all(marked) !did we visit every particle?
295 IF (all_connected) RETURN
296
297 ! make sure we keep the larger chunk
298 IF (count(marked) < n_particles/2) marked(:) = .NOT. (marked(:))
299
300 min_dist = huge(1.0_dp)
301 cluster_edge = -1
302 fragment_edge = -1
303 DO i = 1, n_particles
304 IF (marked(i)) cycle
305 DO j = 1, n_particles
306 IF (.NOT. marked(j)) cycle
307 d = norm2(diff(positions, i, j))
308 IF (d < min_dist) THEN
309 min_dist = d
310 cluster_edge = i
311 fragment_edge = j
312 END IF
313 END DO
314 END DO
315
316 dr = diff(positions, cluster_edge, fragment_edge)
317 s = 1.0_dp - bondlength/norm2(dr)
318 DO i = 1, n_particles
319 IF (marked(i)) cycle
320 positions(3*i - 2:3*i) = positions(3*i - 2:3*i) - s*dr
321 END DO
322
323 END FUNCTION fix_fragmentation
324
325! **************************************************************************************************
326!> \brief Helper routine for fix_fragmentation, calculates atomic distance
327!> \param positions ...
328!> \param i ...
329!> \param j ...
330!> \return ...
331!> \author Ole Schuett
332! **************************************************************************************************
333 PURE FUNCTION diff(positions, i, j) RESULT(dr)
334 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: positions
335 INTEGER, INTENT(IN) :: i, j
336 REAL(kind=dp), DIMENSION(3) :: dr
337
338 dr = positions(3*i - 2:3*i) - positions(3*j - 2:3*j)
339 END FUNCTION diff
340
341! **************************************************************************************************
342!> \brief Finalizes worker for global optimization
343!> \param worker ...
344!> \author Ole Schuett
345! **************************************************************************************************
346 SUBROUTINE glbopt_worker_finalize(worker)
347 TYPE(glbopt_worker_type), INTENT(INOUT) :: worker
348
349 INTEGER :: ierr
350
351 CALL f_env_rm_defaults(worker%f_env)
352 CALL destroy_force_env(worker%f_env_id, ierr)
353 IF (ierr /= 0) cpabort("destroy_force_env failed")
354 END SUBROUTINE glbopt_worker_finalize
355
356END MODULE glbopt_worker
Adds an entry from a swarm-message.
Returns an entry from a swarm-message.
types that represent a subsys, i.e. a part of the system
subroutine, public unpack_subsys_particles(subsys, f, r, s, v, fscale, cell)
Unpack components of a subsystem particle sets into a single vector.
subroutine, public cp_subsys_get(subsys, ref_count, atomic_kinds, atomic_kind_set, particles, particle_set, local_particles, molecules, molecule_set, molecule_kinds, molecule_kind_set, local_molecules, para_env, colvar_p, shell_particles, core_particles, gci, multipoles, natom, nparticle, ncore, nshell, nkind, atprop, virial, results, cell, cell_ref, use_ref_cell)
returns information about various attributes of the given subsys
subroutine, public pack_subsys_particles(subsys, f, r, s, v, fscale, cell)
Pack components of a subsystem particle sets into a single vector.
interface to use cp2k as library
recursive subroutine, public destroy_force_env(env_id, ierr, q_finalize)
deallocates the force_env with the given id
subroutine, public f_env_add_defaults(f_env_id, f_env, handle)
adds the default environments of the f_env to the stack of the defaults, and returns a new error and ...
recursive subroutine, public create_force_env(new_env_id, input_declaration, input_path, output_path, mpi_comm, output_unit, owns_out_unit, input, ierr, work_dir, initial_variables)
creates a new force environment using the given input, and writing the output to the given output uni...
subroutine, public f_env_rm_defaults(f_env, ierr, handle)
removes the default environments of the f_env to the stack of the defaults, and sets ierr accordingly...
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, ipi_env)
returns various attributes about the force environment
performs geometry optimization
Definition geo_opt.F:13
subroutine, public cp_geo_opt(force_env, globenv, eval_opt_geo, rm_restart_info)
Main driver to perform geometry optimization.
Definition geo_opt.F:62
Worker routines used by global optimization schemes.
subroutine, public glbopt_worker_init(worker, input_declaration, para_env, root_section, input_path, worker_id, iw)
Initializes worker for global optimization.
subroutine, public glbopt_worker_finalize(worker)
Finalizes worker for global optimization.
subroutine, public glbopt_worker_execute(worker, cmd, report)
Central execute routine of global optimization worker.
Define type storing the global information of a run. Keep the amount of stored data small....
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
recursive type(section_vals_type) function, pointer, public section_vals_get_subs_vals(section_vals, subsection_name, i_rep_section, can_return_null)
returns the values of the requested subsection
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
integer, parameter, public default_string_length
Definition kinds.F:57
Perform a molecular dynamics (MD) run using QUICKSTEP.
Definition md_run.F:14
subroutine, public qs_mol_dyn(force_env, globenv, averages, rm_restart_info, hmc_e_initial, hmc_e_final, mdctrl)
Main driver module for Molecular Dynamics.
Definition md_run.F:148
A common interface for passing a callback into the md_run loop.
Interface to the message passing library MPI.
Definition of physical constants:
Definition physcon.F:68
real(kind=dp), parameter, public kelvin
Definition physcon.F:165
real(kind=dp), parameter, public angstrom
Definition physcon.F:144
Swarm-message, a convenient data-container for with build-in serialization.
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
represent a section of the input file
stores all the informations relevant to an mpi environment