LCOV - code coverage report
Current view: top level - src/motion - gopt_f_types.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:2c0d679) Lines: 98.6 % 73 72
Test Date: 2026-09-25 00:58:37 Functions: 60.0 % 5 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 contains a functional that calculates the energy and its derivatives
      10              : !>      for the geometry optimizer
      11              : !> \par History
      12              : !>      01.2008 - Luca Bellucci and Teodoro Laino - Generalizing for Dimer Method.
      13              : !>      03.2008 - Teodoro Laino [tlaino] - University of Zurich - Cell Optimization
      14              : ! **************************************************************************************************
      15              : MODULE gopt_f_types
      16              :    USE cell_opt_types,                  ONLY: cell_opt_env_create,&
      17              :                                               cell_opt_env_release,&
      18              :                                               cell_opt_env_type
      19              :    USE cp_subsys_types,                 ONLY: cp_subsys_get,&
      20              :                                               cp_subsys_type
      21              :    USE dimer_types,                     ONLY: dimer_env_create,&
      22              :                                               dimer_env_release,&
      23              :                                               dimer_env_retain,&
      24              :                                               dimer_env_type
      25              :    USE force_env_types,                 ONLY: force_env_get,&
      26              :                                               force_env_release,&
      27              :                                               force_env_retain,&
      28              :                                               force_env_type
      29              :    USE global_types,                    ONLY: global_environment_type
      30              :    USE gopt_param_types,                ONLY: gopt_param_read,&
      31              :                                               gopt_param_type
      32              :    USE input_constants,                 ONLY: default_cell_method_id,&
      33              :                                               default_dimer_method_id,&
      34              :                                               default_minimization_method_id,&
      35              :                                               default_shellcore_method_id,&
      36              :                                               default_ts_method_id
      37              :    USE input_section_types,             ONLY: section_vals_get_subs_vals,&
      38              :                                               section_vals_type
      39              :    USE kinds,                           ONLY: default_string_length,&
      40              :                                               dp
      41              :    USE particle_list_types,             ONLY: particle_list_type
      42              :    USE space_groups_types,              ONLY: release_spgr_type,&
      43              :                                               spgr_type
      44              : #include "../base/base_uses.f90"
      45              : 
      46              :    IMPLICIT NONE
      47              :    PRIVATE
      48              : 
      49              :    LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
      50              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'gopt_f_types'
      51              : 
      52              :    PUBLIC :: gopt_f_type, gopt_f_create, gopt_f_retain, gopt_f_release
      53              : 
      54              : ! **************************************************************************************************
      55              : !> \brief calculates the potential energy of a system, and its derivatives
      56              : !> \par History
      57              : !>      none
      58              : ! **************************************************************************************************
      59              :    TYPE gopt_f_type
      60              :       INTEGER                                  :: ref_count = 0
      61              :       INTEGER                                  :: nfree = 0
      62              :       INTEGER                                  :: type_id = default_cell_method_id
      63              :       INTEGER                                  :: ts_method_id = 0
      64              :       INTEGER                                  :: shellcore_method_id = 0
      65              :       LOGICAL                                  :: dimer_rotation = .FALSE., do_line_search = .FALSE., eval_opt_geo = .FALSE.
      66              :       CHARACTER(LEN=default_string_length)     :: label = "", tag = ""
      67              :       TYPE(force_env_type), POINTER            :: force_env => NULL()
      68              :       TYPE(global_environment_type), POINTER   :: globenv => NULL()
      69              :       ! Motion section must be references only for IO of the MOTION%PRINT..
      70              :       TYPE(section_vals_type), POINTER         :: motion_section => NULL(), geo_section => NULL()
      71              :       TYPE(dimer_env_type), POINTER            :: dimer_env => NULL()
      72              :       TYPE(gopt_f_type), POINTER               :: gopt_dimer_env => NULL()
      73              :       TYPE(gopt_param_type), POINTER           :: gopt_dimer_param => NULL()
      74              :       TYPE(cell_opt_env_type), POINTER         :: cell_env => NULL()
      75              :       TYPE(spgr_type), POINTER                 :: spgr => NULL()
      76              :       PROCEDURE(gopt_f_dimer_rotation_optimizer), POINTER, NOPASS :: dimer_rotation_optimizer => NULL()
      77              :       REAL(KIND=dp), DIMENSION(3, 3)           :: h_ref = 0.0_dp
      78              :       LOGICAL                                  :: require_consistent_energy_force = .FALSE.
      79              :    END TYPE gopt_f_type
      80              : 
      81              :    ABSTRACT INTERFACE
      82              : ! **************************************************************************************************
      83              : !> \brief ...
      84              : !> \param gopt_env ...
      85              : !> \param x0 ...
      86              : !> \param gopt_param ...
      87              : !> \param geo_section ...
      88              : ! **************************************************************************************************
      89              :       SUBROUTINE gopt_f_dimer_rotation_optimizer(gopt_env, x0, gopt_param, geo_section)
      90              :          IMPORT :: dp, gopt_f_type, gopt_param_type, section_vals_type
      91              :          TYPE(gopt_f_type), POINTER                         :: gopt_env
      92              :          REAL(KIND=dp), DIMENSION(:), POINTER               :: x0
      93              :          TYPE(gopt_param_type), POINTER                     :: gopt_param
      94              :          TYPE(section_vals_type), POINTER                   :: geo_section
      95              :       END SUBROUTINE gopt_f_dimer_rotation_optimizer
      96              :    END INTERFACE
      97              : 
      98              : CONTAINS
      99              : 
     100              : ! **************************************************************************************************
     101              : !> \brief ...
     102              : !> \param gopt_env the geometry optimization environment to be created
     103              : !>      force_env:
     104              : !> \param gopt_param ...
     105              : !> \param force_env ...
     106              : !> \param globenv ...
     107              : !> \param geo_opt_section ...
     108              : !> \param eval_opt_geo ...
     109              : !> \par History
     110              : !>      none
     111              : ! **************************************************************************************************
     112         1143 :    RECURSIVE SUBROUTINE gopt_f_create(gopt_env, gopt_param, force_env, globenv, geo_opt_section, &
     113              :                                       eval_opt_geo)
     114              : 
     115              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
     116              :       TYPE(gopt_param_type), POINTER                     :: gopt_param
     117              :       TYPE(force_env_type), POINTER                      :: force_env
     118              :       TYPE(global_environment_type), POINTER             :: globenv
     119              :       TYPE(section_vals_type), POINTER                   :: geo_opt_section
     120              :       LOGICAL, INTENT(IN), OPTIONAL                      :: eval_opt_geo
     121              : 
     122              :       INTEGER                                            :: nshell
     123              :       TYPE(cp_subsys_type), POINTER                      :: subsys
     124              :       TYPE(particle_list_type), POINTER                  :: particles, shell_particles
     125              :       TYPE(section_vals_type), POINTER                   :: dimer_section, rot_opt_section
     126              : 
     127         1143 :       CPASSERT(.NOT. ASSOCIATED(gopt_env))
     128        14859 :       ALLOCATE (gopt_env)
     129         1143 :       nshell = 0
     130              : 
     131         1143 :       NULLIFY (gopt_env%dimer_env, gopt_env%gopt_dimer_env, gopt_env%gopt_dimer_param, gopt_env%cell_env, gopt_env%spgr)
     132         1143 :       gopt_env%ref_count = 1
     133         1143 :       gopt_env%dimer_rotation = .FALSE.
     134         1143 :       gopt_env%do_line_search = .FALSE.
     135         5715 :       ALLOCATE (gopt_env%spgr)
     136         1143 :       CALL force_env_retain(force_env)
     137         1143 :       gopt_env%force_env => force_env
     138         1143 :       gopt_env%motion_section => section_vals_get_subs_vals(force_env%root_section, "MOTION")
     139         1143 :       gopt_env%geo_section => geo_opt_section
     140         1143 :       gopt_env%globenv => globenv
     141         1143 :       gopt_env%eval_opt_geo = .TRUE.
     142         1143 :       IF (PRESENT(eval_opt_geo)) gopt_env%eval_opt_geo = eval_opt_geo
     143         1143 :       gopt_env%require_consistent_energy_force = .TRUE.
     144              : 
     145         1143 :       CALL force_env_get(force_env, subsys=subsys)
     146         1143 :       gopt_env%type_id = gopt_param%type_id
     147         2050 :       SELECT CASE (gopt_env%type_id)
     148              :       CASE (default_ts_method_id, default_minimization_method_id)
     149              :          CALL cp_subsys_get(subsys, &
     150              :                             particles=particles, &
     151          907 :                             shell_particles=shell_particles)
     152          907 :          IF (ASSOCIATED(shell_particles)) nshell = shell_particles%n_els
     153              :          ! The same number of shell and core particles is assumed
     154          907 :          gopt_env%nfree = particles%n_els + nshell
     155          907 :          gopt_env%label = "GEO_OPT"
     156          907 :          gopt_env%tag = "GEOMETRY"
     157         1159 :          SELECT CASE (gopt_param%type_id)
     158              :          CASE (default_ts_method_id)
     159           36 :             gopt_env%ts_method_id = gopt_param%ts_method_id
     160          907 :             SELECT CASE (gopt_param%ts_method_id)
     161              :             CASE (default_dimer_method_id)
     162              :                ! For the Dimer method we use the same framework of geometry optimizers
     163              :                ! already defined for cp2k..
     164           18 :                dimer_section => section_vals_get_subs_vals(geo_opt_section, "TRANSITION_STATE%DIMER")
     165           18 :                CALL dimer_env_create(gopt_env%dimer_env, subsys, globenv, dimer_section, force_env)
     166              : 
     167              :                ! Setup the GEO_OPT environment for the rotation of the Dimer
     168           18 :                rot_opt_section => section_vals_get_subs_vals(dimer_section, "ROT_OPT")
     169           18 :                ALLOCATE (gopt_env%gopt_dimer_param)
     170              :                CALL gopt_param_read(gopt_env%gopt_dimer_param, rot_opt_section, &
     171           18 :                                     type_id=default_minimization_method_id)
     172           18 :                gopt_env%gopt_dimer_param%type_id = default_ts_method_id
     173              : 
     174              :                CALL gopt_f_create(gopt_env%gopt_dimer_env, gopt_env%gopt_dimer_param, force_env=force_env, &
     175           18 :                                   globenv=globenv, geo_opt_section=rot_opt_section, eval_opt_geo=eval_opt_geo)
     176           18 :                CALL dimer_env_retain(gopt_env%dimer_env)
     177           18 :                gopt_env%gopt_dimer_env%dimer_env => gopt_env%dimer_env
     178           18 :                gopt_env%gopt_dimer_env%label = "ROT_OPT"
     179           54 :                gopt_env%gopt_dimer_env%dimer_rotation = .TRUE.
     180              :             END SELECT
     181              :          END SELECT
     182              :       CASE (default_cell_method_id)
     183          216 :          gopt_env%nfree = 6
     184          216 :          gopt_env%label = "CELL_OPT"
     185          216 :          gopt_env%tag = "  CELL  "
     186         8640 :          ALLOCATE (gopt_env%cell_env)
     187          216 :          CALL cell_opt_env_create(gopt_env%cell_env, force_env, gopt_env%geo_section)
     188              :       CASE (default_shellcore_method_id)
     189           20 :          gopt_env%nfree = subsys%shell_particles%n_els
     190           20 :          gopt_env%label = "SHELL_OPT"
     191           20 :          gopt_env%tag = "  SHELL-CORE  "
     192         1143 :          gopt_env%shellcore_method_id = gopt_param%shellcore_method_id
     193              :       END SELECT
     194         1143 :    END SUBROUTINE gopt_f_create
     195              : 
     196              : ! **************************************************************************************************
     197              : !> \brief ...
     198              : !> \param gopt_env the geometry optimization environment to retain
     199              : !> \par History
     200              : !>      none
     201              : ! **************************************************************************************************
     202           90 :    SUBROUTINE gopt_f_retain(gopt_env)
     203              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
     204              : 
     205           90 :       CPASSERT(ASSOCIATED(gopt_env))
     206           90 :       CPASSERT(gopt_env%ref_count > 0)
     207           90 :       gopt_env%ref_count = gopt_env%ref_count + 1
     208           90 :    END SUBROUTINE gopt_f_retain
     209              : 
     210              : ! **************************************************************************************************
     211              : !> \brief ...
     212              : !> \param gopt_env the geometry optimization environment to release
     213              : !> \par History
     214              : !>      none
     215              : ! **************************************************************************************************
     216         2358 :    RECURSIVE SUBROUTINE gopt_f_release(gopt_env)
     217              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
     218              : 
     219         2358 :       IF (ASSOCIATED(gopt_env)) THEN
     220         1233 :          CPASSERT(gopt_env%ref_count > 0)
     221         1233 :          gopt_env%ref_count = gopt_env%ref_count - 1
     222         1233 :          IF (gopt_env%ref_count == 0) THEN
     223         1143 :             CALL force_env_release(gopt_env%force_env)
     224              :             NULLIFY (gopt_env%force_env, &
     225         1143 :                      gopt_env%globenv, &
     226         1143 :                      gopt_env%motion_section, &
     227         1143 :                      gopt_env%geo_section)
     228         1143 :             IF (ASSOCIATED(gopt_env%cell_env)) THEN
     229          216 :                CALL cell_opt_env_release(gopt_env%cell_env)
     230          216 :                DEALLOCATE (gopt_env%cell_env)
     231              :             END IF
     232         1143 :             CALL dimer_env_release(gopt_env%dimer_env)
     233         1143 :             CALL gopt_f_release(gopt_env%gopt_dimer_env)
     234         1143 :             IF (ASSOCIATED(gopt_env%gopt_dimer_param)) DEALLOCATE (gopt_env%gopt_dimer_param)
     235         1143 :             CALL release_spgr_type(gopt_env%spgr)
     236         1143 :             DEALLOCATE (gopt_env)
     237              :          END IF
     238              :       END IF
     239         2358 :    END SUBROUTINE gopt_f_release
     240              : 
     241            0 : END MODULE gopt_f_types
        

Generated by: LCOV version 2.0-1