LCOV - code coverage report
Current view: top level - src/motion - cp_lbfgs_geo.F (source / functions) Hit Total Coverage
Test: CP2K Regtests (git:5911e77) Lines: 39 42 92.9 %
Date: 2024-12-12 07:20:04 Functions: 1 1 100.0 %

          Line data    Source code
       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 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 cell_types,                      ONLY: cell_type
      15             :    USE cp_external_control,             ONLY: external_control
      16             :    USE cp_lbfgs_optimizer_gopt,         ONLY: cp_lbfgs_opt_gopt_type,&
      17             :                                               cp_opt_gopt_create,&
      18             :                                               cp_opt_gopt_next,&
      19             :                                               cp_opt_gopt_release,&
      20             :                                               cp_opt_gopt_stop
      21             :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      22             :                                               cp_logger_type
      23             :    USE cp_output_handling,              ONLY: cp_iterate,&
      24             :                                               cp_print_key_finished_output,&
      25             :                                               cp_print_key_unit_nr
      26             :    USE cp_subsys_types,                 ONLY: cp_subsys_type
      27             :    USE force_env_types,                 ONLY: force_env_get,&
      28             :                                               force_env_type
      29             :    USE global_types,                    ONLY: global_environment_type
      30             :    USE gopt_f_methods,                  ONLY: gopt_f_ii,&
      31             :                                               gopt_f_io_finalize,&
      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_get,&
      39             :                                               section_vals_val_set
      40             :    USE kinds,                           ONLY: dp
      41             :    USE message_passing,                 ONLY: mp_para_env_type
      42             :    USE space_groups,                    ONLY: identify_space_group,&
      43             :                                               print_spgr,&
      44             :                                               spgr_apply_rotations_coord
      45             :    USE space_groups_types,              ONLY: spgr_type
      46             : #include "../base/base_uses.f90"
      47             : 
      48             :    IMPLICIT NONE
      49             :    PRIVATE
      50             : 
      51             :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cp_lbfgs_geo'
      52             : 
      53             :    PUBLIC :: geoopt_lbfgs
      54             : 
      55             : CONTAINS
      56             : 
      57             : ! **************************************************************************************************
      58             : !> \brief ...
      59             : !> \param force_env ...
      60             : !> \param gopt_param ...
      61             : !> \param globenv ...
      62             : !> \param geo_section ...
      63             : !> \param gopt_env ...
      64             : !> \param x0 ...
      65             : !> \par History
      66             : !>      08.2003 created [fawzi]
      67             : !>      01.2020 modified [pcazade]
      68             : !> \author Fawzi Mohamed
      69             : ! **************************************************************************************************
      70        1176 :    SUBROUTINE geoopt_lbfgs(force_env, gopt_param, globenv, geo_section, gopt_env, &
      71             :                            x0)
      72             :       TYPE(force_env_type), POINTER                      :: force_env
      73             :       TYPE(gopt_param_type), POINTER                     :: gopt_param
      74             :       TYPE(global_environment_type), POINTER             :: globenv
      75             :       TYPE(section_vals_type), POINTER                   :: geo_section
      76             :       TYPE(gopt_f_type), POINTER                         :: gopt_env
      77             :       REAL(KIND=dp), DIMENSION(:), POINTER               :: x0
      78             : 
      79             :       CHARACTER(len=*), PARAMETER                        :: routineN = 'geoopt_lbfgs'
      80             : 
      81             :       INTEGER                                            :: handle, iter_nr, its, output_unit
      82             :       LOGICAL                                            :: converged, should_stop
      83             :       REAL(KIND=dp)                                      :: trust_radius
      84             :       TYPE(cell_type), POINTER                           :: cell
      85             :       TYPE(cp_lbfgs_opt_gopt_type), POINTER              :: optimizer
      86             :       TYPE(cp_logger_type), POINTER                      :: logger
      87             :       TYPE(cp_subsys_type), POINTER                      :: subsys
      88             :       TYPE(mp_para_env_type), POINTER                    :: para_env
      89             :       TYPE(section_vals_type), POINTER                   :: root_section
      90             :       TYPE(spgr_type), POINTER                           :: spgr
      91             : 
      92         392 :       CALL timeset(routineN, handle)
      93             : 
      94         392 :       NULLIFY (optimizer, para_env, spgr)
      95         392 :       logger => cp_get_default_logger()
      96         392 :       spgr => gopt_env%spgr
      97         392 :       root_section => force_env%root_section
      98         392 :       CPASSERT(ASSOCIATED(force_env))
      99         392 :       CPASSERT(ASSOCIATED(gopt_param))
     100             : 
     101             :       ! collecting subsys
     102         392 :       CALL force_env_get(force_env, para_env=para_env, cell=cell, subsys=subsys)
     103             : 
     104             :       ! Geometry optimization starts now
     105             :       output_unit = cp_print_key_unit_nr(logger, geo_section, "PRINT%PROGRAM_RUN_INFO", &
     106         392 :                                          extension=".geoLog")
     107         392 :       CALL print_geo_opt_header(gopt_env, output_unit, "L-BFGS")
     108             : 
     109             :       ! finds space group
     110         392 :       CALL section_vals_val_get(geo_section, "KEEP_SPACE_GROUP", l_val=spgr%keep_space_group)
     111         392 :       IF (spgr%keep_space_group) THEN
     112           2 :          CALL identify_space_group(subsys, geo_section, gopt_env, output_unit)
     113           2 :          CALL spgr_apply_rotations_coord(spgr, x0)
     114           2 :          CALL print_spgr(spgr)
     115             :       END IF
     116             : 
     117             :       ! Stop if not implemented
     118         392 :       IF (gopt_env%type_id == default_ts_method_id) &
     119           0 :          CPABORT("BFGS method not yet working with DIMER")
     120             : 
     121         392 :       CALL section_vals_val_get(geo_section, "LBFGS%TRUST_RADIUS", r_val=trust_radius)
     122        2352 :       ALLOCATE (optimizer)
     123             :       CALL cp_opt_gopt_create(optimizer, para_env=para_env, obj_funct=gopt_env, &
     124             :                               x0=x0, wanted_relative_f_delta=gopt_param%wanted_rel_f_error, &
     125             :                               wanted_projected_gradient=gopt_param%wanted_proj_gradient, m=gopt_param%max_h_rank, &
     126         392 :                               max_f_per_iter=gopt_param%max_f_per_iter, trust_radius=trust_radius)
     127         392 :       CALL cp_iterate(logger%iter_info, increment=0, iter_nr_out=iter_nr)
     128         392 :       converged = .FALSE.
     129             : 
     130        4112 :       DO its = iter_nr + 1, gopt_param%max_iter
     131        4110 :          CALL cp_iterate(logger%iter_info, last=(its == gopt_param%max_iter))
     132        4110 :          CALL section_vals_val_set(geo_section, "STEP_START_VAL", i_val=its)
     133        4110 :          CALL gopt_f_ii(its, output_unit)
     134             : 
     135             :          ! Real optimization step..
     136        4110 :          IF (.NOT. cp_opt_gopt_next(optimizer, geo_section=geo_section, &
     137             :                                     force_env=force_env, gopt_param=gopt_param, &
     138             :                                     converged=converged, spgr=spgr)) EXIT
     139             : 
     140             :          ! Check for an external exit command
     141        3738 :          CALL external_control(should_stop, "GEO", globenv=globenv)
     142        3738 :          IF (should_stop) THEN
     143           0 :             CALL cp_opt_gopt_stop(optimizer)
     144           0 :             EXIT
     145             :          END IF
     146        7850 :          IF (its == gopt_param%max_iter) EXIT
     147             :       END DO
     148             : 
     149         392 :       IF ((its == gopt_param%max_iter) .AND. (.NOT. converged)) THEN
     150          18 :          CALL print_geo_opt_nc(gopt_env, output_unit)
     151             :       END IF
     152             : 
     153             :       ! Write final output information, if converged
     154         392 :       CALL cp_iterate(logger%iter_info, last=.TRUE., increment=0)
     155             :       CALL gopt_f_io_finalize(gopt_env, force_env, optimizer%x, converged, its, root_section, &
     156         392 :                               optimizer%para_env, optimizer%master, output_unit)
     157             : 
     158         392 :       CALL cp_opt_gopt_release(optimizer)
     159         392 :       DEALLOCATE (optimizer)
     160             :       CALL cp_print_key_finished_output(output_unit, logger, geo_section, &
     161         392 :                                         "PRINT%PROGRAM_RUN_INFO")
     162             : 
     163         392 :       CALL timestop(handle)
     164             : 
     165         392 :    END SUBROUTINE geoopt_lbfgs
     166             : 
     167             : END MODULE cp_lbfgs_geo

Generated by: LCOV version 1.15