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

Generated by: LCOV version 2.0-1