(git:27c966b)
Loading...
Searching...
No Matches
cp_lbfgs_optimizer_gopt.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 routines that optimize a functional using the limited memory bfgs
10!> quasi-newton method.
11!> The process set up so that a master runs the real optimizer and the
12!> others help then to calculate the objective function.
13!> The arguments for the objective function are physically present in
14!> every processor (nedeed in the actual implementation of pao).
15!> In the future tha arguments themselves could be distributed.
16!> \par History
17!> 09.2003 globenv->para_env, retain/release, better parallel behaviour
18!> 01.2020 Space Group Symmetry introduced by Pierre-André Cazade [pcazade]
19!> \author Fawzi Mohamed
20!> @version 2.2002
21! **************************************************************************************************
23 USE cp_lbfgs, ONLY: setulb
32 USE gopt_f_methods, ONLY: cp_eval_at,&
33 gopt_f_io,&
36 USE gopt_f_types, ONLY: gopt_f_release,&
41 USE kinds, ONLY: dp
42 USE machine, ONLY: m_walltime
46#include "../base/base_uses.f90"
47
48 IMPLICIT NONE
49 PRIVATE
50
51 LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .true.
52 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cp_lbfgs_optimizer_gopt'
53
54 ! types
56
57 ! core methods
58
59 ! special methos
60
61 ! underlying functions
65
66! **************************************************************************************************
67!> \brief info for the optimizer (see the description of this module)
68!> \param task the actual task of the optimizer (in the master it is up to
69!> date, in case of error also the minions one get updated.
70!> \param csave internal character string used by the lbfgs optimizer,
71!> meaningful only in the master
72!> \param lsave logical array used by the lbfgs optimizer, updated only
73!> in the master
74!> On exit with task = 'NEW_X', the following information is
75!> available:
76!> lsave(1) = .true. the initial x did not satisfy the bounds;
77!> lsave(2) = .true. the problem contains bounds;
78!> lsave(3) = .true. each variable has upper and lower bounds.
79!> \param ref_count reference count (see doc/ReferenceCounting.html)
80!> \param m the dimension of the subspace used to approximate the second
81!> derivative
82!> \param print_every every how many iterations output should be written.
83!> if 0 only at end, if print_every<0 never
84!> \param master the pid of the master processor
85!> \param max_f_per_iter the maximum number of function evaluations per
86!> iteration
87!> \param status 0: just initialized, 1: f g calculation,
88!> 2: begin new iteration, 3: ended iteration,
89!> 4: normal (converged) exit, 5: abnormal (error) exit,
90!> 6: daellocated
91!> \param n_iter the actual iteration number
92!> \param kind_of_bound an array with 0 (no bound), 1 (lower bound),
93!> 2 (both bounds), 3 (upper bound), to describe the bounds
94!> of every variable
95!> \param i_work_array an integer workarray of dimension 3*n, present only
96!> in the master
97!> \param isave is an INTEGER working array of dimension 44.
98!> On exit with task = 'NEW_X', it contains information that
99!> the user may want to access:
100!> \param isave (30) = the current iteration number;
101!> \param isave (34) = the total number of function and gradient
102!> evaluations;
103!> \param isave (36) = the number of function value or gradient
104!> evaluations in the current iteration;
105!> \param isave (38) = the number of free variables in the current
106!> iteration;
107!> \param isave (39) = the number of active constraints at the current
108!> iteration;
109!> \param f the actual best value of the object function
110!> \param wanted_relative_f_delta the wanted relative error on f
111!> (to be multiplied by epsilon), 0.0 -> no check
112!> \param wanted_projected_gradient the wanted error on the projected
113!> gradient (hessian times the gradient), 0.0 -> no check
114!> \param last_f the value of f in the last iteration
115!> \param projected_gradient the value of the sup norm of the projected
116!> gradient
117!> \param x the actual evaluation point (best one if converged or stopped)
118!> \param lower_bound the lower bounds
119!> \param upper_bound the upper bounds
120!> \param gradient the actual gradient
121!> \param dsave info date for lbfgs (master only)
122!> \param work_array a work array for lbfgs (master only)
123!> \param para_env the parallel environment for this optimizer
124!> \param obj_funct the objective function to be optimized
125!> \par History
126!> none
127!> \author Fawzi Mohamed
128!> @version 2.2002
129! **************************************************************************************************
131 CHARACTER(len=60) :: task = ""
132 CHARACTER(len=60) :: csave = ""
133 LOGICAL :: lsave(4) = .false.
134 INTEGER :: m = 0, print_every = 0, master = 0, max_f_per_iter = 0, status = 0, n_iter = 0
135 INTEGER, DIMENSION(:), POINTER :: kind_of_bound => null(), i_work_array => null(), isave => null()
136 REAL(kind=dp) :: f = 0.0_dp, wanted_relative_f_delta = 0.0_dp, wanted_projected_gradient = 0.0_dp, &
137 last_f = 0.0_dp, projected_gradient = 0.0_dp, eold = 0.0_dp, emin = 0.0_dp, trust_radius = 0.0_dp
138 REAL(kind=dp), DIMENSION(:), POINTER :: x => null(), lower_bound => null(), upper_bound => null(), &
139 gradient => null(), dsave => null(), work_array => null()
140 TYPE(mp_para_env_type), POINTER :: para_env => null()
141 TYPE(gopt_f_type), POINTER :: obj_funct => null()
143
144CONTAINS
145
146! **************************************************************************************************
147!> \brief calls the L-BFGS optimizer
148!> \param optimizer the optimizer state
149!> \param spgr optional space group information
150!> \param iwunit optional output unit
151! **************************************************************************************************
152 SUBROUTINE cp_opt_gopt_setulb(optimizer, spgr, iwunit)
153 TYPE(cp_lbfgs_opt_gopt_type), INTENT(INOUT) :: optimizer
154 TYPE(spgr_type), OPTIONAL, POINTER :: spgr
155 INTEGER, OPTIONAL :: iwunit
156
157 CALL setulb(SIZE(optimizer%x), optimizer%m, optimizer%x, &
158 optimizer%lower_bound, optimizer%upper_bound, &
159 optimizer%kind_of_bound, optimizer%f, optimizer%gradient, &
160 optimizer%wanted_relative_f_delta, &
161 optimizer%wanted_projected_gradient, optimizer%work_array, &
162 optimizer%i_work_array, optimizer%task, optimizer%print_every, &
163 optimizer%csave, optimizer%lsave, optimizer%isave, &
164 optimizer%dsave, optimizer%trust_radius, spgr=spgr, iwunit=iwunit)
165
166 END SUBROUTINE cp_opt_gopt_setulb
167
168! **************************************************************************************************
169!> \brief initializes the optimizer
170!> \param optimizer ...
171!> \param para_env ...
172!> \param obj_funct ...
173!> \param x0 ...
174!> \param m ...
175!> \param print_every ...
176!> \param wanted_relative_f_delta ...
177!> \param wanted_projected_gradient ...
178!> \param lower_bound ...
179!> \param upper_bound ...
180!> \param kind_of_bound ...
181!> \param master ...
182!> \param max_f_per_iter ...
183!> \param trust_radius ...
184!> \par History
185!> 02.2002 created [fawzi]
186!> 09.2003 refactored (retain/release,para_env) [fawzi]
187!> \author Fawzi Mohamed
188!> \note
189!> redirects the lbfgs output the the default unit
190! **************************************************************************************************
191 SUBROUTINE cp_opt_gopt_create(optimizer, para_env, obj_funct, x0, m, print_every, &
192 wanted_relative_f_delta, wanted_projected_gradient, lower_bound, upper_bound, &
193 kind_of_bound, master, max_f_per_iter, trust_radius)
194 TYPE(cp_lbfgs_opt_gopt_type), INTENT(OUT) :: optimizer
195 TYPE(mp_para_env_type), POINTER :: para_env
196 TYPE(gopt_f_type), POINTER :: obj_funct
197 REAL(kind=dp), DIMENSION(:), INTENT(in) :: x0
198 INTEGER, INTENT(in), OPTIONAL :: m, print_every
199 REAL(kind=dp), INTENT(in), OPTIONAL :: wanted_relative_f_delta, &
200 wanted_projected_gradient
201 REAL(kind=dp), DIMENSION(SIZE(x0)), INTENT(in), &
202 OPTIONAL :: lower_bound, upper_bound
203 INTEGER, DIMENSION(SIZE(x0)), INTENT(in), OPTIONAL :: kind_of_bound
204 INTEGER, INTENT(in), OPTIONAL :: master, max_f_per_iter
205 REAL(kind=dp), INTENT(in), OPTIONAL :: trust_radius
206
207 CHARACTER(len=*), PARAMETER :: routinen = 'cp_opt_gopt_create'
208
209 INTEGER :: handle, lenwa, n
210
211 CALL timeset(routinen, handle)
212
213 NULLIFY (optimizer%kind_of_bound, &
214 optimizer%i_work_array, &
215 optimizer%isave, &
216 optimizer%x, &
217 optimizer%lower_bound, &
218 optimizer%upper_bound, &
219 optimizer%gradient, &
220 optimizer%dsave, &
221 optimizer%work_array, &
222 optimizer%para_env, &
223 optimizer%obj_funct)
224 n = SIZE(x0)
225 optimizer%m = 4
226 IF (PRESENT(m)) optimizer%m = m
227 optimizer%master = para_env%source
228 optimizer%para_env => para_env
229 CALL para_env%retain()
230 optimizer%obj_funct => obj_funct
231 CALL gopt_f_retain(obj_funct)
232 optimizer%max_f_per_iter = 20
233 IF (PRESENT(max_f_per_iter)) optimizer%max_f_per_iter = max_f_per_iter
234 optimizer%print_every = -1
235 optimizer%n_iter = 0
236 optimizer%f = -1.0_dp
237 optimizer%last_f = -1.0_dp
238 optimizer%projected_gradient = -1.0_dp
239 IF (PRESENT(print_every)) optimizer%print_every = print_every
240 IF (PRESENT(master)) optimizer%master = master
241 IF (optimizer%master == optimizer%para_env%mepos) THEN
242 !MK This has to be adapted for a new L-BFGS version possibly
243 lenwa = 2*optimizer%m*n + 5*n + 11*optimizer%m*optimizer%m + 8*optimizer%m
244 ALLOCATE (optimizer%kind_of_bound(n), optimizer%i_work_array(3*n), &
245 optimizer%isave(44))
246 ALLOCATE (optimizer%x(n), optimizer%lower_bound(n), &
247 optimizer%upper_bound(n), optimizer%gradient(n), &
248 optimizer%dsave(29), optimizer%work_array(lenwa))
249 optimizer%x = x0
250 optimizer%task = 'START'
251 optimizer%i_work_array = 0
252 optimizer%isave = 0
253 optimizer%lower_bound = 0.0_dp
254 optimizer%upper_bound = 0.0_dp
255 optimizer%gradient = 0.0_dp
256 optimizer%dsave = 0.0_dp
257 optimizer%work_array = 0.0_dp
258 IF (PRESENT(wanted_relative_f_delta)) THEN
259 optimizer%wanted_relative_f_delta = wanted_relative_f_delta
260 END IF
261 IF (PRESENT(wanted_projected_gradient)) THEN
262 optimizer%wanted_projected_gradient = wanted_projected_gradient
263 END IF
264 optimizer%kind_of_bound = 0
265 IF (PRESENT(kind_of_bound)) optimizer%kind_of_bound = kind_of_bound
266 IF (PRESENT(lower_bound)) optimizer%lower_bound = lower_bound
267 IF (PRESENT(upper_bound)) optimizer%upper_bound = upper_bound
268 IF (PRESENT(trust_radius)) optimizer%trust_radius = trust_radius
269
270 CALL cp_opt_gopt_setulb(optimizer)
271 ELSE
272 NULLIFY ( &
273 optimizer%kind_of_bound, optimizer%i_work_array, optimizer%isave, &
274 optimizer%lower_bound, optimizer%upper_bound, optimizer%gradient, &
275 optimizer%dsave, optimizer%work_array)
276 ALLOCATE (optimizer%x(n))
277 optimizer%x(:) = 0.0_dp
278 ALLOCATE (optimizer%gradient(n))
279 optimizer%gradient(:) = 0.0_dp
280 END IF
281 CALL optimizer%para_env%bcast(optimizer%x, optimizer%master)
282 optimizer%status = 0
283
284 CALL timestop(handle)
285
286 END SUBROUTINE cp_opt_gopt_create
287
288! **************************************************************************************************
289!> \brief releases the optimizer (see doc/ReferenceCounting.html)
290!> \param optimizer the object that should be released
291!> \par History
292!> 02.2002 created [fawzi]
293!> 09.2003 dealloc_ref->release [fawzi]
294!> \author Fawzi Mohamed
295! **************************************************************************************************
296 SUBROUTINE cp_opt_gopt_release(optimizer)
297 TYPE(cp_lbfgs_opt_gopt_type), INTENT(INOUT) :: optimizer
298
299 CHARACTER(len=*), PARAMETER :: routinen = 'cp_opt_gopt_release'
300
301 INTEGER :: handle
302
303 CALL timeset(routinen, handle)
304
305 IF (ASSOCIATED(optimizer%kind_of_bound)) THEN
306 DEALLOCATE (optimizer%kind_of_bound)
307 END IF
308 IF (ASSOCIATED(optimizer%i_work_array)) THEN
309 DEALLOCATE (optimizer%i_work_array)
310 END IF
311 IF (ASSOCIATED(optimizer%isave)) THEN
312 DEALLOCATE (optimizer%isave)
313 END IF
314 IF (ASSOCIATED(optimizer%x)) THEN
315 DEALLOCATE (optimizer%x)
316 END IF
317 IF (ASSOCIATED(optimizer%lower_bound)) THEN
318 DEALLOCATE (optimizer%lower_bound)
319 END IF
320 IF (ASSOCIATED(optimizer%upper_bound)) THEN
321 DEALLOCATE (optimizer%upper_bound)
322 END IF
323 IF (ASSOCIATED(optimizer%gradient)) THEN
324 DEALLOCATE (optimizer%gradient)
325 END IF
326 IF (ASSOCIATED(optimizer%dsave)) THEN
327 DEALLOCATE (optimizer%dsave)
328 END IF
329 IF (ASSOCIATED(optimizer%work_array)) THEN
330 DEALLOCATE (optimizer%work_array)
331 END IF
332 CALL mp_para_env_release(optimizer%para_env)
333 CALL gopt_f_release(optimizer%obj_funct)
334
335 CALL timestop(handle)
336 END SUBROUTINE cp_opt_gopt_release
337
338! **************************************************************************************************
339!> \brief does one optimization step
340!> \param optimizer ...
341!> \param n_iter ...
342!> \param f ...
343!> \param last_f ...
344!> \param projected_gradient ...
345!> \param converged ...
346!> \param geo_section ...
347!> \param force_env ...
348!> \param gopt_param ...
349!> \par History
350!> 01.2020 modified [pcazade]
351!> \author Fawzi Mohamed
352!> @version 2.2002
353!> \note
354!> use directly mainlb in place of setulb ??
355! **************************************************************************************************
356 SUBROUTINE cp_opt_gopt_step(optimizer, n_iter, f, last_f, &
357 projected_gradient, converged, geo_section, force_env, &
358 gopt_param)
359 TYPE(cp_lbfgs_opt_gopt_type), INTENT(INOUT) :: optimizer
360 INTEGER, INTENT(out), OPTIONAL :: n_iter
361 REAL(kind=dp), INTENT(out), OPTIONAL :: f, last_f, projected_gradient
362 LOGICAL, INTENT(out), OPTIONAL :: converged
363 TYPE(section_vals_type), POINTER :: geo_section
364 TYPE(force_env_type), POINTER :: force_env
365 TYPE(gopt_param_type), POINTER :: gopt_param
366
367 CHARACTER(len=*), PARAMETER :: routinen = 'cp_opt_gopt_step'
368
369 CHARACTER(LEN=5) :: wildcard
370 INTEGER :: dataunit, handle, its
371 LOGICAL :: conv, is_master, just_entered
372 REAL(kind=dp) :: t_diff, t_now, t_old
373 REAL(kind=dp), DIMENSION(:), POINTER :: xold
374 TYPE(cp_logger_type), POINTER :: logger
375 TYPE(cp_subsys_type), POINTER :: subsys
376
377 NULLIFY (logger, xold)
378 logger => cp_get_default_logger()
379 CALL timeset(routinen, handle)
380 just_entered = .true.
381 is_master = optimizer%master == optimizer%para_env%mepos
382 IF (PRESENT(converged)) converged = optimizer%status == 4
383 ALLOCATE (xold(SIZE(optimizer%x)))
384
385 ! collecting subsys
386 CALL force_env_get(force_env, subsys=subsys)
387
388 ! applies rotation matrices to coordinates
389 CALL gopt_f_project_coord(optimizer%obj_funct, optimizer%x)
390
391 xold = optimizer%x
392 t_old = m_walltime()
393
394 IF (optimizer%status >= 4) THEN
395 cpwarn("status>=4, trying to restart")
396 optimizer%status = 0
397 dataunit = cp_print_key_unit_nr(logger, geo_section, &
398 "PRINT%PROGRAM_RUN_INFO", extension=".geoLog")
399 IF (is_master) THEN
400 optimizer%task = 'START'
401 CALL cp_opt_gopt_setulb(optimizer, spgr=optimizer%obj_funct%spgr, iwunit=dataunit)
402 END IF
403 CALL cp_print_key_finished_output(dataunit, logger, geo_section, &
404 "PRINT%PROGRAM_RUN_INFO")
405 END IF
406
407 DO
408 dataunit = cp_print_key_unit_nr(logger, geo_section, &
409 "PRINT%PROGRAM_RUN_INFO", extension=".geoLog")
410 ifmaster: IF (is_master) THEN
411 IF (optimizer%task(1:7) == 'RESTART') THEN
412 ! restart the optimizer
413 optimizer%status = 0
414 optimizer%task = 'START'
415 ! applies rotation matrices to coordinates and forces
416 CALL gopt_f_project_coord(optimizer%obj_funct, optimizer%x)
417 CALL gopt_f_project_vector(optimizer%obj_funct, optimizer%gradient)
418 CALL cp_opt_gopt_setulb(optimizer, spgr=optimizer%obj_funct%spgr, iwunit=dataunit)
419 CALL gopt_f_project_coord(optimizer%obj_funct, optimizer%x)
420 CALL gopt_f_project_vector(optimizer%obj_funct, optimizer%gradient)
421 END IF
422 IF (optimizer%task(1:2) == 'FG') THEN
423 IF (optimizer%isave(36) > optimizer%max_f_per_iter) THEN
424 optimizer%task = 'STOP: CPU, hit max f eval in iter'
425 optimizer%status = 5 ! anormal exit
426 CALL cp_opt_gopt_setulb(optimizer, spgr=optimizer%obj_funct%spgr, iwunit=dataunit)
427 ELSE
428 optimizer%status = 1
429 END IF
430 ELSE IF (optimizer%task(1:5) == 'NEW_X') THEN
431 IF (just_entered) THEN
432 optimizer%status = 2
433 ! applies rotation matrices to coordinates and forces
434 CALL gopt_f_project_coord(optimizer%obj_funct, optimizer%x)
435 CALL gopt_f_project_vector(optimizer%obj_funct, optimizer%gradient)
436 CALL cp_opt_gopt_setulb(optimizer, spgr=optimizer%obj_funct%spgr, iwunit=dataunit)
437 CALL gopt_f_project_coord(optimizer%obj_funct, optimizer%x)
438 CALL gopt_f_project_vector(optimizer%obj_funct, optimizer%gradient)
439 ELSE
440 ! applies rotation matrices to coordinates and forces
441 CALL gopt_f_project_coord(optimizer%obj_funct, optimizer%x)
442 CALL gopt_f_project_vector(optimizer%obj_funct, optimizer%gradient)
443 optimizer%status = 3
444 END IF
445 ELSE IF (optimizer%task(1:4) == 'CONV') THEN
446 optimizer%status = 4
447 ELSE IF (optimizer%task(1:4) == 'STOP') THEN
448 optimizer%status = 5
449 cpwarn("task became stop in an unknown way")
450 ELSE IF (optimizer%task(1:5) == 'ERROR') THEN
451 optimizer%status = 5
452 ELSE
453 cpwarn("unknown task '"//optimizer%task//"'")
454 END IF
455 END IF ifmaster
456 CALL cp_print_key_finished_output(dataunit, logger, geo_section, &
457 "PRINT%PROGRAM_RUN_INFO")
458 CALL optimizer%para_env%bcast(optimizer%status, optimizer%master)
459 ! Dump info
460 IF (optimizer%status == 3) THEN
461 its = 0
462 IF (is_master) THEN
463 ! Iteration level is taken into account in the optimizer external loop
464 its = optimizer%isave(30)
465 END IF
466 END IF
467 !
468 SELECT CASE (optimizer%status)
469 CASE (1)
470 !op=1 evaluate f and g
471 CALL cp_eval_at(optimizer%obj_funct, x=optimizer%x, &
472 f=optimizer%f, &
473 gradient=optimizer%gradient, &
474 master=optimizer%master, para_env=optimizer%para_env)
475 ! do not use keywords?
476 dataunit = cp_print_key_unit_nr(logger, geo_section, &
477 "PRINT%PROGRAM_RUN_INFO", extension=".geoLog")
478 IF (is_master) THEN
479 ! applies rotation matrices to coordinates and forces
480 CALL gopt_f_project_coord(optimizer%obj_funct, optimizer%x)
481 CALL gopt_f_project_vector(optimizer%obj_funct, optimizer%gradient)
482 CALL cp_opt_gopt_setulb(optimizer, spgr=optimizer%obj_funct%spgr, iwunit=dataunit)
483 CALL gopt_f_project_coord(optimizer%obj_funct, optimizer%x)
484 CALL gopt_f_project_vector(optimizer%obj_funct, optimizer%gradient)
485 END IF
486 CALL cp_print_key_finished_output(dataunit, logger, geo_section, &
487 "PRINT%PROGRAM_RUN_INFO")
488 CALL optimizer%para_env%bcast(optimizer%x, optimizer%master)
489 CASE (2)
490 !op=2 begin new iter
491 CALL optimizer%para_env%bcast(optimizer%x, optimizer%master)
492 t_old = m_walltime()
493 CASE (3)
494 !op=3 ended iter
495 wildcard = "LBFGS"
496 dataunit = cp_print_key_unit_nr(logger, geo_section, &
497 "PRINT%PROGRAM_RUN_INFO", extension=".geoLog")
498 IF (is_master) its = optimizer%isave(30)
499 CALL optimizer%para_env%bcast(its, optimizer%master)
500
501 ! Some IO and Convergence check
502 t_now = m_walltime()
503 t_diff = t_now - t_old
504 t_old = t_now
505 CALL gopt_f_io(optimizer%obj_funct, force_env, force_env%root_section, &
506 its, optimizer%f, dataunit, optimizer%eold, optimizer%emin, wildcard, gopt_param, &
507 SIZE(optimizer%x), optimizer%x - xold, optimizer%gradient, conv, used_time=t_diff)
508 CALL optimizer%para_env%bcast(conv, optimizer%master)
509 CALL cp_print_key_finished_output(dataunit, logger, geo_section, &
510 "PRINT%PROGRAM_RUN_INFO")
511 optimizer%eold = optimizer%f
512 optimizer%emin = min(optimizer%emin, optimizer%eold)
513 xold = optimizer%x
514 IF (PRESENT(converged)) converged = conv
515 EXIT
516 CASE (4)
517 !op=4 (convergence - normal exit)
518 ! Specific L-BFGS convergence criteria.. overrides the convergence criteria on
519 ! stepsize and gradients
520 dataunit = cp_print_key_unit_nr(logger, geo_section, &
521 "PRINT%PROGRAM_RUN_INFO", extension=".geoLog")
522 IF (dataunit > 0) THEN
523 WRITE (dataunit, '(T2,A)') ""
524 WRITE (dataunit, '(T2,A)') "************************************************"
525 WRITE (dataunit, '(T2,A)') "* Specific L-BFGS convergence criteria *"
526 WRITE (dataunit, '(T2,A)') "* WANTED_PROJ_GRADIENT and WANTED_REL_F_ERROR *"
527 WRITE (dataunit, '(T2,A)') "* satisfied .... run CONVERGED! *"
528 WRITE (dataunit, '(T2,A)') "* * * * *"
529 WRITE (dataunit, '(T2,A)') "* General convergence criteria on stepsize and *"
530 WRITE (dataunit, '(T2,A)') "* gradients may or may not have been satisfied *"
531 WRITE (dataunit, '(T2,A)') "* yet; if unsatisfactory, try tightening the *"
532 WRITE (dataunit, '(T2,A)') "* L-BFGS convergence criteria and restart run. *"
533 WRITE (dataunit, '(T2,A)') "************************************************"
534 WRITE (dataunit, '(T2,A)') ""
535 END IF
536 CALL cp_print_key_finished_output(dataunit, logger, geo_section, &
537 "PRINT%PROGRAM_RUN_INFO")
538 IF (PRESENT(converged)) converged = .true.
539 EXIT
540 CASE (5)
541 ! Restore the last accepted point; a failed FG request may leave x at a trial point.
542 CALL optimizer%para_env%bcast(optimizer%task, optimizer%master)
543 optimizer%x = xold
544 CALL cp_eval_at(optimizer%obj_funct, x=optimizer%x, &
545 f=optimizer%f, gradient=optimizer%gradient, &
546 master=optimizer%master, para_env=optimizer%para_env)
547 IF (PRESENT(converged)) converged = .false.
548 EXIT
549 CASE (6)
550 ! deallocated
551 cpabort("step on a deallocated opt structure ")
552 CASE default
553 CALL cp_abort(__location__, &
554 "unknown status "//cp_to_string(optimizer%status))
555 optimizer%status = 5
556 EXIT
557 END SELECT
558 IF (optimizer%status == 1 .AND. just_entered) THEN
559 optimizer%eold = optimizer%f
560 optimizer%emin = optimizer%eold
561 END IF
562 just_entered = .false.
563 END DO
564
565 CALL optimizer%para_env%bcast(optimizer%x, optimizer%master)
566 CALL cp_opt_gopt_bcast_res(optimizer, &
567 n_iter=optimizer%n_iter, &
568 f=optimizer%f, last_f=optimizer%last_f, &
569 projected_gradient=optimizer%projected_gradient)
570
571 DEALLOCATE (xold)
572 IF (PRESENT(f)) f = optimizer%f
573 IF (PRESENT(last_f)) last_f = optimizer%last_f
574 IF (PRESENT(projected_gradient)) projected_gradient = optimizer%projected_gradient
575 IF (PRESENT(n_iter)) n_iter = optimizer%n_iter
576 CALL timestop(handle)
577
578 END SUBROUTINE cp_opt_gopt_step
579
580! **************************************************************************************************
581!> \brief returns the results (and broadcasts them)
582!> \param optimizer the optimizer object the info is taken from
583!> \param n_iter the number of iterations
584!> \param f the actual value of the objective function (f)
585!> \param last_f the last value of f
586!> \param projected_gradient the infinity norm of the projected gradient
587!> \par History
588!> none
589!> \author Fawzi Mohamed
590!> @version 2.2002
591!> \note
592!> private routine
593! **************************************************************************************************
594 SUBROUTINE cp_opt_gopt_bcast_res(optimizer, n_iter, f, last_f, &
595 projected_gradient)
596 TYPE(cp_lbfgs_opt_gopt_type), INTENT(IN) :: optimizer
597 INTEGER, INTENT(out), OPTIONAL :: n_iter
598 REAL(kind=dp), INTENT(inout), OPTIONAL :: f, last_f, projected_gradient
599
600 REAL(kind=dp), DIMENSION(4) :: results
601
602 IF (optimizer%master == optimizer%para_env%mepos) THEN
603 results = [real(optimizer%isave(30), kind=dp), &
604 optimizer%f, optimizer%dsave(2), optimizer%dsave(13)]
605 END IF
606 CALL optimizer%para_env%bcast(results, optimizer%master)
607 IF (PRESENT(n_iter)) n_iter = nint(results(1))
608 IF (PRESENT(f)) f = results(2)
609 IF (PRESENT(last_f)) last_f = results(3)
610 IF (PRESENT(projected_gradient)) projected_gradient = results(4)
611
612 END SUBROUTINE cp_opt_gopt_bcast_res
613
614! **************************************************************************************************
615!> \brief goes to the next optimal point (after an optimizer iteration)
616!> returns true if converged
617!> \param optimizer the optimizer that goes to the next point
618!> \param n_iter ...
619!> \param f ...
620!> \param last_f ...
621!> \param projected_gradient ...
622!> \param converged ...
623!> \param geo_section ...
624!> \param force_env ...
625!> \param gopt_param ...
626!> \return ...
627!> \par History
628!> 01.2020 modified [pcazade]
629!> \author Fawzi Mohamed
630!> @version 2.2002
631!> \note
632!> if you deactivate convergence control it returns never false
633! **************************************************************************************************
634 FUNCTION cp_opt_gopt_next(optimizer, n_iter, f, last_f, &
635 projected_gradient, converged, geo_section, force_env, &
636 gopt_param) RESULT(res)
637 TYPE(cp_lbfgs_opt_gopt_type), INTENT(INOUT) :: optimizer
638 INTEGER, INTENT(out), OPTIONAL :: n_iter
639 REAL(kind=dp), INTENT(out), OPTIONAL :: f, last_f, projected_gradient
640 LOGICAL, INTENT(out) :: converged
641 TYPE(section_vals_type), POINTER :: geo_section
642 TYPE(force_env_type), POINTER :: force_env
643 TYPE(gopt_param_type), POINTER :: gopt_param
644 LOGICAL :: res
645
646 CALL cp_opt_gopt_step(optimizer, n_iter=n_iter, f=f, &
647 last_f=last_f, projected_gradient=projected_gradient, &
648 converged=converged, geo_section=geo_section, &
649 force_env=force_env, gopt_param=gopt_param)
650 res = (optimizer%status < 4) .AND. .NOT. converged
651
652 END FUNCTION cp_opt_gopt_next
653
654! **************************************************************************************************
655!> \brief stops the optimization
656!> \param optimizer ...
657!> \par History
658!> none
659!> \author Fawzi Mohamed
660!> @version 2.2002
661! **************************************************************************************************
662 SUBROUTINE cp_opt_gopt_stop(optimizer)
663 TYPE(cp_lbfgs_opt_gopt_type), INTENT(INOUT) :: optimizer
664
665 optimizer%task = 'STOPPED on user request'
666 optimizer%status = 4 ! normal exit
667 IF (optimizer%master == optimizer%para_env%mepos) THEN
668 CALL cp_opt_gopt_setulb(optimizer)
669 END IF
670
671 END SUBROUTINE cp_opt_gopt_stop
672
routines that optimize a functional using the limited memory bfgs quasi-newton method....
subroutine, public cp_opt_gopt_create(optimizer, para_env, obj_funct, x0, m, print_every, wanted_relative_f_delta, wanted_projected_gradient, lower_bound, upper_bound, kind_of_bound, master, max_f_per_iter, trust_radius)
initializes the optimizer
subroutine cp_opt_gopt_setulb(optimizer, spgr, iwunit)
calls the L-BFGS optimizer
subroutine, public cp_opt_gopt_stop(optimizer)
stops the optimization
logical function, public cp_opt_gopt_next(optimizer, n_iter, f, last_f, projected_gradient, converged, geo_section, force_env, gopt_param)
goes to the next optimal point (after an optimizer iteration) returns true if converged
subroutine, public cp_opt_gopt_release(optimizer)
releases the optimizer (see doc/ReferenceCounting.html)
LBFGS-B routine (version 3.0, April 25, 2011).
Definition cp_lbfgs.F:19
subroutine, public setulb(n, m, x, lower_bound, upper_bound, nbd, f, g, factr, pgtol, wa, iwa, task, iprint, csave, lsave, isave, dsave, trust_radius, spgr, iwunit)
This subroutine partitions the working arrays wa and iwa, and then uses the limited memory BFGS metho...
Definition cp_lbfgs.F:188
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...
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 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, file_access)
...
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, ipi_env)
returns various attributes about the force environment
contains a functional that calculates the energy and its derivatives for the geometry optimizer
subroutine, public gopt_f_project_coord(gopt_env, x)
Project optimizer coordinates onto the space-group-invariant subspace.
subroutine, public gopt_f_project_vector(gopt_env, v)
Project a gradient, step, or search direction onto the space-group subspace.
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 cp_eval_at(gopt_env, x, f, gradient, master, para_env)
evaluete the potential energy and its gradients using an array with same dimension as the particle_se...
contains a functional that calculates the energy and its derivatives for the geometry optimizer
subroutine, public gopt_f_retain(gopt_env)
...
recursive subroutine, public gopt_f_release(gopt_env)
...
contains typo and related routines to handle parameters controlling the GEO_OPT module
objects that represent the structure of input sections and the data contained in an input section
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:144
Interface to the message passing library MPI.
subroutine, public mp_para_env_release(para_env)
releases the para object (to be called when you don't want anymore the shared copy of this object)
Space Group Symmetry Type Module (version 1.0, Ferbruary 12, 2021).
info for the optimizer (see the description of this module)
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
calculates the potential energy of a system, and its derivatives
stores all the informations relevant to an mpi environment