LCOV - code coverage report
Current view: top level - src/motion - cp_lbfgs_geo.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:f7b9015) Lines: 84.6 % 39 33
Test Date: 2026-11-09 01:19:29 Functions: 100.0 % 1 1

            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 Main driver for L-BFGS optimizer
      10              : !> \par History
      11              : !>      01.2020 Space Group Symmetry introduced by Pierre-André Cazade [pcazade]
      12              : ! **************************************************************************************************
      13              : MODULE cp_lbfgs_geo
      14              :    USE cp_external_control,             ONLY: external_control
      15              :    USE cp_lbfgs_optimizer_gopt,         ONLY: cp_lbfgs_opt_gopt_type,&
      16              :                                               cp_opt_gopt_create,&
      17              :                                               cp_opt_gopt_next,&
      18              :                                               cp_opt_gopt_release,&
      19              :                                               cp_opt_gopt_stop
      20              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      21              :                                               cp_logger_type
      22              :    USE cp_output_handling,              ONLY: cp_iterate,&
      23              :                                               cp_print_key_finished_output,&
      24              :                                               cp_print_key_unit_nr
      25              :    USE force_env_types,                 ONLY: force_env_get,&
      26              :                                               force_env_type
      27              :    USE global_types,                    ONLY: global_environment_type
      28              :    USE gopt_f_methods,                  ONLY: gopt_f_ii,&
      29              :                                               gopt_f_io_finalize,&
      30              :                                               print_geo_opt_header,&
      31              :                                               print_geo_opt_nc
      32              :    USE gopt_f_types,                    ONLY: gopt_f_type
      33              :    USE gopt_param_types,                ONLY: gopt_param_type
      34              :    USE input_constants,                 ONLY: default_ts_method_id
      35              :    USE input_section_types,             ONLY: section_vals_type,&
      36              :                                               section_vals_val_set
      37              :    USE kinds,                           ONLY: dp
      38              :    USE message_passing,                 ONLY: mp_para_env_type
      39              : #include "../base/base_uses.f90"
      40              : 
      41              :    IMPLICIT NONE
      42              :    PRIVATE
      43              : 
      44              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cp_lbfgs_geo'
      45              : 
      46              :    PUBLIC :: geoopt_lbfgs
      47              : 
      48              : CONTAINS
      49              : 
      50              : ! **************************************************************************************************
      51              : !> \brief ...
      52              : !> \param force_env ...
      53              : !> \param gopt_param ...
      54              : !> \param globenv ...
      55              : !> \param geo_section ...
      56              : !> \param gopt_env ...
      57              : !> \param x0 ...
      58              : !> \par History
      59              : !>      08.2003 created [fawzi]
      60              : !>      01.2020 modified [pcazade]
      61              : !> \author Fawzi Mohamed
      62              : ! **************************************************************************************************
      63          212 :    SUBROUTINE geoopt_lbfgs(force_env, gopt_param, globenv, geo_section, gopt_env, &
      64              :                            x0)
      65              :       TYPE(force_env_type), POINTER                      :: force_env
      66              :       TYPE(gopt_param_type), POINTER                     :: gopt_param
      67              :       TYPE(global_environment_type), POINTER             :: globenv
      68              :       TYPE(section_vals_type), POINTER                   :: geo_section
      69              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
      70              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: x0
      71              : 
      72              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'geoopt_lbfgs'
      73              : 
      74              :       INTEGER                                            :: handle, iter_nr, its, output_unit
      75              :       LOGICAL                                            :: converged, should_stop
      76              :       TYPE(cp_lbfgs_opt_gopt_type), POINTER              :: optimizer
      77              :       TYPE(cp_logger_type), POINTER                      :: logger
      78              :       TYPE(mp_para_env_type), POINTER                    :: para_env
      79              :       TYPE(section_vals_type), POINTER                   :: root_section
      80              : 
      81          106 :       CALL timeset(routineN, handle)
      82              : 
      83          106 :       NULLIFY (optimizer, para_env)
      84          106 :       logger => cp_get_default_logger()
      85          106 :       root_section => force_env%root_section
      86          106 :       CPASSERT(ASSOCIATED(force_env))
      87          106 :       CPASSERT(ASSOCIATED(gopt_param))
      88              : 
      89          106 :       CALL force_env_get(force_env, para_env=para_env)
      90              : 
      91              :       ! Geometry optimization starts now
      92              :       output_unit = cp_print_key_unit_nr(logger, geo_section, "PRINT%PROGRAM_RUN_INFO", &
      93          106 :                                          extension=".geoLog")
      94          106 :       CALL print_geo_opt_header(gopt_env, output_unit, "L-BFGS")
      95              : 
      96              :       ! Stop if not implemented
      97          106 :       IF (gopt_env%type_id == default_ts_method_id) THEN
      98            0 :          CPABORT("L-BFGS method not yet working with DIMER")
      99              :       END IF
     100              : 
     101          530 :       ALLOCATE (optimizer)
     102              :       CALL cp_opt_gopt_create(optimizer, para_env=para_env, obj_funct=gopt_env, &
     103              :                               x0=x0, m=gopt_param%max_h_rank, &
     104              :                               print_every=gopt_param%print_control, &
     105              :                               wanted_relative_f_delta=gopt_param%wanted_rel_f_error, &
     106              :                               wanted_projected_gradient=gopt_param%wanted_proj_gradient, &
     107              :                               max_f_per_iter=gopt_param%max_f_per_iter, &
     108          106 :                               trust_radius=gopt_param%trust_radius)
     109          106 :       CALL cp_iterate(logger%iter_info, increment=0, iter_nr_out=iter_nr)
     110          106 :       converged = .FALSE.
     111              : 
     112         3320 :       DO its = iter_nr + 1, gopt_param%max_iter
     113         3318 :          CALL cp_iterate(logger%iter_info, last=(its == gopt_param%max_iter))
     114         3318 :          CALL section_vals_val_set(geo_section, "STEP_START_VAL", i_val=its)
     115         3318 :          CALL gopt_f_ii(its, output_unit)
     116              : 
     117              :          ! Real optimization step..
     118         3318 :          IF (.NOT. cp_opt_gopt_next(optimizer, geo_section=geo_section, &
     119              :                                     force_env=force_env, gopt_param=gopt_param, &
     120              :                                     converged=converged)) EXIT
     121              : 
     122              :          ! Check for an external exit command
     123         3246 :          CALL external_control(should_stop, "GEO", globenv=globenv)
     124         3246 :          IF (should_stop) THEN
     125            0 :             CALL cp_opt_gopt_stop(optimizer)
     126            0 :             EXIT
     127              :          END IF
     128         6566 :          IF (its == gopt_param%max_iter) EXIT
     129              :       END DO
     130              : 
     131          106 :       IF (optimizer%status == 5) THEN
     132            0 :          IF (output_unit > 0) THEN
     133            0 :             WRITE (output_unit, FMT="(/,T2,A)") "*** L-BFGS ABNORMAL EXIT: OPTIMIZATION NOT CONVERGED ***"
     134            0 :             WRITE (output_unit, FMT="(T2,A)") "L-BFGS task: "//TRIM(optimizer%task)
     135              :          END IF
     136          106 :       ELSE IF ((its == gopt_param%max_iter) .AND. (.NOT. converged)) THEN
     137           32 :          CALL print_geo_opt_nc(gopt_env, output_unit)
     138              :       END IF
     139              : 
     140              :       ! Write final output information, if converged
     141          106 :       CALL cp_iterate(logger%iter_info, last=.TRUE., increment=0)
     142              :       CALL gopt_f_io_finalize(gopt_env, force_env, optimizer%x, converged, its, root_section, &
     143          106 :                               optimizer%para_env, optimizer%master, output_unit)
     144              : 
     145          106 :       CALL cp_opt_gopt_release(optimizer)
     146          106 :       DEALLOCATE (optimizer)
     147              :       CALL cp_print_key_finished_output(output_unit, logger, geo_section, &
     148          106 :                                         "PRINT%PROGRAM_RUN_INFO")
     149              : 
     150          106 :       CALL timestop(handle)
     151              : 
     152          106 :    END SUBROUTINE geoopt_lbfgs
     153              : 
     154              : END MODULE cp_lbfgs_geo
        

Generated by: LCOV version 2.0-1