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

            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 Routines for Geometry optimization using  Conjugate Gradients
      10              : !> \author Teodoro Laino [teo]
      11              : !>      10.2005
      12              : ! **************************************************************************************************
      13              : MODULE cg_optimizer
      14              : 
      15              :    USE cell_types,                      ONLY: cell_type
      16              :    USE cg_utils,                        ONLY: cg_linmin,&
      17              :                                               get_conjugate_direction
      18              :    USE cp_external_control,             ONLY: external_control
      19              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      20              :                                               cp_logger_type
      21              :    USE cp_output_handling,              ONLY: cp_iterate,&
      22              :                                               cp_print_key_finished_output,&
      23              :                                               cp_print_key_unit_nr
      24              :    USE cp_subsys_types,                 ONLY: cp_subsys_type
      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: &
      29              :         cp_eval_at, gopt_f_ii, gopt_f_io, gopt_f_io_finalize, gopt_f_io_init, &
      30              :         gopt_f_project_coord, gopt_f_project_vector, print_geo_opt_header, print_geo_opt_nc
      31              :    USE gopt_f_types,                    ONLY: gopt_f_type
      32              :    USE gopt_param_types,                ONLY: gopt_param_type
      33              :    USE input_constants,                 ONLY: default_ts_method_id
      34              :    USE input_section_types,             ONLY: section_vals_type,&
      35              :                                               section_vals_val_set
      36              :    USE kinds,                           ONLY: dp
      37              :    USE machine,                         ONLY: m_walltime
      38              : #include "../base/base_uses.f90"
      39              : 
      40              :    IMPLICIT NONE
      41              :    PRIVATE
      42              : 
      43              :    PUBLIC :: geoopt_cg
      44              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cg_optimizer'
      45              : 
      46              : CONTAINS
      47              : 
      48              : ! **************************************************************************************************
      49              : !> \brief Driver for conjugate gradient optimization technique
      50              : !> \param force_env ...
      51              : !> \param gopt_param ...
      52              : !> \param globenv ...
      53              : !> \param geo_section ...
      54              : !> \param gopt_env ...
      55              : !> \param x0 ...
      56              : !> \param do_update ...
      57              : !> \par History
      58              : !>      10.2005 created [tlaino]
      59              : !> \author Teodoro Laino
      60              : ! **************************************************************************************************
      61          520 :    RECURSIVE SUBROUTINE geoopt_cg(force_env, gopt_param, globenv, geo_section, &
      62              :                                   gopt_env, x0, do_update)
      63              : 
      64              :       TYPE(force_env_type), POINTER                      :: force_env
      65              :       TYPE(gopt_param_type), POINTER                     :: gopt_param
      66              :       TYPE(global_environment_type), POINTER             :: globenv
      67              :       TYPE(section_vals_type), POINTER                   :: geo_section
      68              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
      69              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: x0
      70              :       LOGICAL, INTENT(OUT), OPTIONAL                     :: do_update
      71              : 
      72              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'geoopt_cg'
      73              : 
      74              :       INTEGER                                            :: handle, output_unit
      75              :       LOGICAL                                            :: my_do_update
      76              :       TYPE(cp_logger_type), POINTER                      :: logger
      77              : 
      78          260 :       CALL timeset(routineN, handle)
      79              : 
      80          260 :       logger => cp_get_default_logger()
      81              : 
      82              :       output_unit = cp_print_key_unit_nr(logger, geo_section, "PRINT%PROGRAM_RUN_INFO", &
      83          260 :                                          extension=".geoLog")
      84          260 :       CALL print_geo_opt_header(gopt_env, output_unit, "CONJUGATE GRADIENTS")
      85              : 
      86              :       CALL cp_cg_main(force_env, x0, gopt_param, output_unit, globenv, &
      87          260 :                       gopt_env, do_update=my_do_update)
      88              : 
      89              :       CALL cp_print_key_finished_output(output_unit, logger, geo_section, &
      90          260 :                                         "PRINT%PROGRAM_RUN_INFO")
      91          260 :       IF (PRESENT(do_update)) do_update = my_do_update
      92              : 
      93          260 :       CALL timestop(handle)
      94              : 
      95          260 :    END SUBROUTINE geoopt_cg
      96              : 
      97              : ! **************************************************************************************************
      98              : !> \brief This really performs the conjugate gradients optimization
      99              : !> \param force_env ...
     100              : !> \param x0 ...
     101              : !> \param gopt_param ...
     102              : !> \param output_unit ...
     103              : !> \param globenv ...
     104              : !> \param gopt_env ...
     105              : !> \param do_update ...
     106              : !> \par History
     107              : !>      10.2005 created [tlaino]
     108              : !> \author Teodoro Laino
     109              : ! **************************************************************************************************
     110          260 :    RECURSIVE SUBROUTINE cp_cg_main(force_env, x0, gopt_param, output_unit, globenv, &
     111              :                                    gopt_env, do_update)
     112              :       TYPE(force_env_type), POINTER                      :: force_env
     113              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: x0
     114              :       TYPE(gopt_param_type), POINTER                     :: gopt_param
     115              :       INTEGER, INTENT(IN)                                :: output_unit
     116              :       TYPE(global_environment_type), POINTER             :: globenv
     117              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
     118              :       LOGICAL, INTENT(OUT), OPTIONAL                     :: do_update
     119              : 
     120              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'cp_cg_main'
     121              : 
     122              :       CHARACTER(LEN=5)                                   :: wildcard
     123              :       INTEGER                                            :: handle, iter_nr, its, max_steep_steps, &
     124              :                                                             maxiter
     125              :       LOGICAL                                            :: conv, evaluate_before_io, &
     126              :                                                             Fletcher_Reeves, &
     127              :                                                             save_consistent_energy_force, &
     128              :                                                             should_stop
     129              :       REAL(KIND=dp)                                      :: emin, eold, opt_energy, res_lim, t_diff, &
     130              :                                                             t_now, t_old
     131          260 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: xold
     132          260 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: g, h, xi
     133              :       TYPE(cell_type), POINTER                           :: cell
     134              :       TYPE(cp_logger_type), POINTER                      :: logger
     135              :       TYPE(cp_subsys_type), POINTER                      :: subsys
     136              :       TYPE(section_vals_type), POINTER                   :: root_section
     137              : 
     138          260 :       CALL timeset(routineN, handle)
     139          260 :       t_old = m_walltime()
     140          260 :       NULLIFY (logger, g, h, xi)
     141          260 :       root_section => force_env%root_section
     142          260 :       logger => cp_get_default_logger()
     143          260 :       conv = .FALSE.
     144          260 :       maxiter = gopt_param%max_iter
     145          260 :       max_steep_steps = gopt_param%max_steep_steps
     146          260 :       Fletcher_Reeves = gopt_param%Fletcher_Reeves
     147              :       ! Dimer evaluations update the rotational state, so retain their original timing.
     148          260 :       evaluate_before_io = gopt_env%type_id /= default_ts_method_id
     149          260 :       res_lim = gopt_param%restart_limit
     150          780 :       ALLOCATE (g(SIZE(x0)))
     151          520 :       ALLOCATE (h(SIZE(x0)))
     152          520 :       ALLOCATE (xi(SIZE(x0)))
     153          520 :       ALLOCATE (xold(SIZE(x0)))
     154          260 :       CALL force_env_get(force_env, cell=cell, subsys=subsys)
     155              : 
     156              :       ! applies rotation matrices to coordinates
     157          260 :       CALL gopt_f_project_coord(gopt_env, x0)
     158              : 
     159              :       ! Evaluate energy and forces at the first step
     160              :       ![NB] consistent energies and forces not required for CG, but some line minimizers might set it
     161          260 :       save_consistent_energy_force = gopt_env%require_consistent_energy_force
     162          260 :       gopt_env%require_consistent_energy_force = .FALSE.
     163              : 
     164              :       CALL cp_eval_at(gopt_env, x0, opt_energy, xi, master=gopt_env%force_env%para_env%mepos, &
     165          260 :                       para_env=gopt_env%force_env%para_env)
     166              : 
     167          260 :       gopt_env%require_consistent_energy_force = save_consistent_energy_force
     168              : 
     169              :       ! Symmetrize coordinates and forces
     170          260 :       CALL gopt_f_project_coord(gopt_env, x0)
     171          260 :       CALL gopt_f_project_vector(gopt_env, xi)
     172              : 
     173       144560 :       g = -xi
     174       144560 :       h = g
     175       144560 :       xi = h
     176          260 :       emin = HUGE(0.0_dp)
     177          260 :       CALL cp_iterate(logger%iter_info, increment=0, iter_nr_out=iter_nr)
     178              :       ! Main Loop
     179          260 :       wildcard = "   SD"
     180          260 :       t_now = m_walltime()
     181          260 :       t_diff = t_now - t_old
     182          260 :       t_old = t_now
     183          260 :       CALL gopt_f_io_init(gopt_env, output_unit, opt_energy, wildcard, used_time=t_diff, its=iter_nr)
     184          260 :       eold = opt_energy
     185         2076 :       DO its = iter_nr + 1, maxiter
     186         2076 :          CALL cp_iterate(logger%iter_info, last=(its == maxiter))
     187         2076 :          CALL section_vals_val_set(gopt_env%geo_section, "STEP_START_VAL", i_val=its)
     188         2076 :          CALL gopt_f_ii(its, output_unit)
     189              : 
     190              :          ! Symmetrize coordinates and forces
     191         2076 :          CALL gopt_f_project_coord(gopt_env, x0)
     192         2076 :          CALL gopt_f_project_vector(gopt_env, g)
     193         2076 :          CALL gopt_f_project_vector(gopt_env, xi)
     194              : 
     195       387130 :          xold(:) = x0
     196              : 
     197              :          ! Line minimization
     198         2076 :          CALL cg_linmin(gopt_env, x0, xi, g, opt_energy, output_unit, gopt_param, globenv)
     199              : 
     200              :          ! Applies rotation matrices to coordinates
     201         2076 :          CALL gopt_f_project_coord(gopt_env, x0)
     202              : 
     203         2076 :          IF (evaluate_before_io) THEN
     204              :             ! Keep the accepted point and the physical state consistent even on external stop.
     205         1232 :             CALL cg_eval_current(gopt_env, x0, opt_energy, xi)
     206              :          END IF
     207              : 
     208              :          ! Check for an external exit command
     209         2076 :          CALL external_control(should_stop, "GEO", globenv=globenv)
     210         2076 :          IF (should_stop) EXIT
     211              : 
     212              :          ! Some IO and Convergence check
     213         2076 :          t_now = m_walltime()
     214         2076 :          t_diff = t_now - t_old
     215         2076 :          t_old = t_now
     216              :          CALL gopt_f_io(gopt_env, force_env, root_section, its, opt_energy, &
     217              :                         output_unit, eold, emin, wildcard, gopt_param, SIZE(x0), x0 - xold, xi, conv, &
     218       387130 :                         used_time=t_diff)
     219         2076 :          eold = opt_energy
     220         2076 :          emin = MIN(emin, opt_energy)
     221              : 
     222         2076 :          IF (conv .OR. (its == maxiter)) EXIT
     223         1816 :          IF (.NOT. evaluate_before_io) THEN
     224          698 :             CALL cg_eval_current(gopt_env, x0, opt_energy, xi)
     225              :          END IF
     226              : 
     227              :          ! Get Conjugate Directions:  updates the searching direction (h)
     228         1816 :          wildcard = "   CG"
     229         1816 :          CALL get_conjugate_direction(gopt_env, Fletcher_Reeves, g, xi, h)
     230              : 
     231              :          ! Symmetrize coordinates and forces
     232         1816 :          CALL gopt_f_project_vector(gopt_env, g)
     233         1816 :          CALL gopt_f_project_vector(gopt_env, h)
     234              : 
     235              :          ! Reset Condition or Steepest Descent Requested
     236              :          ! ABS(DOT_PRODUCT(g, h))/SQRT((DOT_PRODUCT(g, g)*DOT_PRODUCT(h, h))) > res_lim ...
     237              :          IF ((DOT_PRODUCT(g, h)*DOT_PRODUCT(g, h)) > (res_lim*res_lim*DOT_PRODUCT(g, g)*DOT_PRODUCT(h, h)) &
     238       942734 :              .OR. its + 1 <= max_steep_steps) THEN
     239              :             ! Steepest Descent
     240          564 :             wildcard = "   SD"
     241       108344 :             h = -xi
     242              :          END IF
     243       629700 :          g = -xi
     244       632036 :          xi = h
     245              :       END DO
     246              : 
     247          260 :       IF (its == maxiter .AND. (.NOT. conv)) THEN
     248           76 :          CALL print_geo_opt_nc(gopt_env, output_unit)
     249              :       END IF
     250              : 
     251              :       ! Write final particle information and restart, if converged
     252          260 :       IF (PRESENT(do_update)) do_update = conv
     253          260 :       CALL cp_iterate(logger%iter_info, last=.TRUE., increment=0)
     254              :       CALL gopt_f_io_finalize(gopt_env, force_env, x0, conv, its, root_section, &
     255          260 :                               gopt_env%force_env%para_env, gopt_env%force_env%para_env%mepos, output_unit)
     256              : 
     257          260 :       DEALLOCATE (xold)
     258          260 :       DEALLOCATE (g)
     259          260 :       DEALLOCATE (h)
     260          260 :       DEALLOCATE (xi)
     261              : 
     262          260 :       CALL timestop(handle)
     263              : 
     264          520 :    END SUBROUTINE cp_cg_main
     265              : 
     266              : ! **************************************************************************************************
     267              : !> \brief Evaluate the current CG point with the force-consistency setting used by CG.
     268              : !> \param gopt_env Optimization environment
     269              : !> \param x Current coordinates, possibly projected by cp_eval_at
     270              : !> \param energy Energy at the evaluated coordinates
     271              : !> \param gradient Gradient at the evaluated coordinates
     272              : ! **************************************************************************************************
     273         1930 :    SUBROUTINE cg_eval_current(gopt_env, x, energy, gradient)
     274              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
     275              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: x
     276              :       REAL(KIND=dp), INTENT(OUT)                         :: energy
     277              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: gradient
     278              : 
     279              :       LOGICAL                                            :: save_consistent_energy_force
     280              : 
     281         1930 :       save_consistent_energy_force = gopt_env%require_consistent_energy_force
     282         1930 :       gopt_env%require_consistent_energy_force = .FALSE.
     283              :       CALL cp_eval_at(gopt_env, x, energy, gradient, master=gopt_env%force_env%para_env%mepos, &
     284         1930 :                       para_env=gopt_env%force_env%para_env)
     285         1930 :       gopt_env%require_consistent_energy_force = save_consistent_energy_force
     286              : 
     287         1930 :       CALL gopt_f_project_vector(gopt_env, gradient)
     288         1930 :    END SUBROUTINE cg_eval_current
     289              : 
     290              : END MODULE cg_optimizer
        

Generated by: LCOV version 2.0-1