LCOV - code coverage report
Current view: top level - src/motion - cp_lbfgs_geo.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:123285f) Lines: 85.4 % 41 35
Test Date: 2026-11-04 00:58:17 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              :                                               gopt_f_space_group_init,&
      31              :                                               gopt_f_space_group_show,&
      32              :                                               print_geo_opt_header,&
      33              :                                               print_geo_opt_nc
      34              :    USE gopt_f_types,                    ONLY: gopt_f_type
      35              :    USE gopt_param_types,                ONLY: gopt_param_type
      36              :    USE input_constants,                 ONLY: default_ts_method_id
      37              :    USE input_section_types,             ONLY: section_vals_type,&
      38              :                                               section_vals_val_set
      39              :    USE kinds,                           ONLY: dp
      40              :    USE message_passing,                 ONLY: mp_para_env_type
      41              : #include "../base/base_uses.f90"
      42              : 
      43              :    IMPLICIT NONE
      44              :    PRIVATE
      45              : 
      46              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cp_lbfgs_geo'
      47              : 
      48              :    PUBLIC :: geoopt_lbfgs
      49              : 
      50              : CONTAINS
      51              : 
      52              : ! **************************************************************************************************
      53              : !> \brief ...
      54              : !> \param force_env ...
      55              : !> \param gopt_param ...
      56              : !> \param globenv ...
      57              : !> \param geo_section ...
      58              : !> \param gopt_env ...
      59              : !> \param x0 ...
      60              : !> \par History
      61              : !>      08.2003 created [fawzi]
      62              : !>      01.2020 modified [pcazade]
      63              : !> \author Fawzi Mohamed
      64              : ! **************************************************************************************************
      65          208 :    SUBROUTINE geoopt_lbfgs(force_env, gopt_param, globenv, geo_section, gopt_env, &
      66              :                            x0)
      67              :       TYPE(force_env_type), POINTER                      :: force_env
      68              :       TYPE(gopt_param_type), POINTER                     :: gopt_param
      69              :       TYPE(global_environment_type), POINTER             :: globenv
      70              :       TYPE(section_vals_type), POINTER                   :: geo_section
      71              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
      72              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: x0
      73              : 
      74              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'geoopt_lbfgs'
      75              : 
      76              :       INTEGER                                            :: handle, iter_nr, its, output_unit
      77              :       LOGICAL                                            :: converged, should_stop
      78              :       TYPE(cp_lbfgs_opt_gopt_type), POINTER              :: optimizer
      79              :       TYPE(cp_logger_type), POINTER                      :: logger
      80              :       TYPE(mp_para_env_type), POINTER                    :: para_env
      81              :       TYPE(section_vals_type), POINTER                   :: root_section
      82              : 
      83          104 :       CALL timeset(routineN, handle)
      84              : 
      85          104 :       NULLIFY (optimizer, para_env)
      86          104 :       logger => cp_get_default_logger()
      87          104 :       root_section => force_env%root_section
      88          104 :       CPASSERT(ASSOCIATED(force_env))
      89          104 :       CPASSERT(ASSOCIATED(gopt_param))
      90              : 
      91          104 :       CALL force_env_get(force_env, para_env=para_env)
      92              : 
      93              :       ! Geometry optimization starts now
      94              :       output_unit = cp_print_key_unit_nr(logger, geo_section, "PRINT%PROGRAM_RUN_INFO", &
      95          104 :                                          extension=".geoLog")
      96          104 :       CALL print_geo_opt_header(gopt_env, output_unit, "L-BFGS")
      97              : 
      98              :       ! finds space group
      99          104 :       CALL gopt_f_space_group_init(gopt_env, x0, geo_section, output_unit)
     100              : 
     101              :       ! Stop if not implemented
     102          104 :       IF (gopt_env%type_id == default_ts_method_id) THEN
     103            0 :          CPABORT("L-BFGS method not yet working with DIMER")
     104              :       END IF
     105              : 
     106          520 :       ALLOCATE (optimizer)
     107              :       CALL cp_opt_gopt_create(optimizer, para_env=para_env, obj_funct=gopt_env, &
     108              :                               x0=x0, m=gopt_param%max_h_rank, &
     109              :                               print_every=gopt_param%print_control, &
     110              :                               wanted_relative_f_delta=gopt_param%wanted_rel_f_error, &
     111              :                               wanted_projected_gradient=gopt_param%wanted_proj_gradient, &
     112              :                               max_f_per_iter=gopt_param%max_f_per_iter, &
     113          104 :                               trust_radius=gopt_param%trust_radius)
     114          104 :       CALL cp_iterate(logger%iter_info, increment=0, iter_nr_out=iter_nr)
     115          104 :       converged = .FALSE.
     116              : 
     117         3046 :       DO its = iter_nr + 1, gopt_param%max_iter
     118         3044 :          CALL cp_iterate(logger%iter_info, last=(its == gopt_param%max_iter))
     119         3044 :          CALL section_vals_val_set(geo_section, "STEP_START_VAL", i_val=its)
     120         3044 :          CALL gopt_f_ii(its, output_unit)
     121              : 
     122              :          ! Real optimization step..
     123         3044 :          IF (.NOT. cp_opt_gopt_next(optimizer, geo_section=geo_section, &
     124              :                                     force_env=force_env, gopt_param=gopt_param, &
     125              :                                     converged=converged)) EXIT
     126              : 
     127              :          ! Check for an external exit command
     128         2974 :          CALL external_control(should_stop, "GEO", globenv=globenv)
     129         2974 :          IF (should_stop) THEN
     130            0 :             CALL cp_opt_gopt_stop(optimizer)
     131            0 :             EXIT
     132              :          END IF
     133         6020 :          IF (its == gopt_param%max_iter) EXIT
     134              :       END DO
     135              : 
     136          104 :       IF (optimizer%status == 5) THEN
     137            0 :          IF (output_unit > 0) THEN
     138            0 :             WRITE (output_unit, FMT="(/,T2,A)") "*** L-BFGS ABNORMAL EXIT: OPTIMIZATION NOT CONVERGED ***"
     139            0 :             WRITE (output_unit, FMT="(T2,A)") "L-BFGS task: "//TRIM(optimizer%task)
     140              :          END IF
     141          104 :       ELSE IF ((its == gopt_param%max_iter) .AND. (.NOT. converged)) THEN
     142           32 :          CALL print_geo_opt_nc(gopt_env, output_unit)
     143              :       END IF
     144              : 
     145              :       ! show space_group
     146          104 :       CALL gopt_f_space_group_show(gopt_env, geo_section, output_unit)
     147              : 
     148              :       ! Write final output information, if converged
     149          104 :       CALL cp_iterate(logger%iter_info, last=.TRUE., increment=0)
     150              :       CALL gopt_f_io_finalize(gopt_env, force_env, optimizer%x, converged, its, root_section, &
     151          104 :                               optimizer%para_env, optimizer%master, output_unit)
     152              : 
     153          104 :       CALL cp_opt_gopt_release(optimizer)
     154          104 :       DEALLOCATE (optimizer)
     155              :       CALL cp_print_key_finished_output(output_unit, logger, geo_section, &
     156          104 :                                         "PRINT%PROGRAM_RUN_INFO")
     157              : 
     158          104 :       CALL timestop(handle)
     159              : 
     160          104 :    END SUBROUTINE geoopt_lbfgs
     161              : 
     162              : END MODULE cp_lbfgs_geo
        

Generated by: LCOV version 2.0-1