LCOV - code coverage report
Current view: top level - src/motion - gopt_f_types.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 96.3 % 82 79
Test Date: 2026-11-24 01:22:08 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_cg_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              :                                               section_vals_val_get
      41              :    USE kinds,                           ONLY: default_string_length,&
      42              :                                               dp
      43              :    USE particle_list_types,             ONLY: particle_list_type
      44              :    USE space_groups_types,              ONLY: release_spgr_type,&
      45              :                                               spgr_type
      46              : #include "../base/base_uses.f90"
      47              : 
      48              :    IMPLICIT NONE
      49              :    PRIVATE
      50              : 
      51              :    LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
      52              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'gopt_f_types'
      53              : 
      54              :    PUBLIC :: gopt_f_type, gopt_f_create, gopt_f_retain, gopt_f_release
      55              : 
      56              : ! **************************************************************************************************
      57              : !> \brief calculates the potential energy of a system, and its derivatives
      58              : !> \par History
      59              : !>      none
      60              : ! **************************************************************************************************
      61              :    TYPE gopt_f_type
      62              :       INTEGER                                  :: ref_count = 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              :       LOGICAL                                  :: solid_state_dimer = .FALSE.
      68              :       CHARACTER(LEN=default_string_length)     :: label = "", tag = ""
      69              :       TYPE(force_env_type), POINTER            :: force_env => NULL()
      70              :       TYPE(global_environment_type), POINTER   :: globenv => NULL()
      71              :       ! Motion section must be references only for IO of the MOTION%PRINT..
      72              :       TYPE(section_vals_type), POINTER         :: motion_section => NULL(), geo_section => NULL()
      73              :       TYPE(dimer_env_type), POINTER            :: dimer_env => NULL()
      74              :       TYPE(gopt_f_type), POINTER               :: gopt_dimer_env => NULL()
      75              :       TYPE(gopt_param_type), POINTER           :: gopt_dimer_param => NULL()
      76              :       TYPE(cell_opt_env_type), POINTER         :: cell_env => NULL()
      77              :       TYPE(spgr_type), POINTER                 :: spgr => NULL()
      78              :       PROCEDURE(gopt_f_dimer_rotation_optimizer), POINTER, NOPASS :: dimer_rotation_optimizer => NULL()
      79              :       REAL(KIND=dp), DIMENSION(3, 3)           :: h_ref = 0.0_dp
      80              :       LOGICAL                                  :: require_consistent_energy_force = .FALSE.
      81              :    END TYPE gopt_f_type
      82              : 
      83              :    ABSTRACT INTERFACE
      84              : ! **************************************************************************************************
      85              : !> \brief ...
      86              : !> \param gopt_env ...
      87              : !> \param x0 ...
      88              : !> \param gopt_param ...
      89              : !> \param geo_section ...
      90              : ! **************************************************************************************************
      91              :       SUBROUTINE gopt_f_dimer_rotation_optimizer(gopt_env, x0, gopt_param, geo_section)
      92              :          IMPORT :: dp, gopt_f_type, gopt_param_type, section_vals_type
      93              :          TYPE(gopt_f_type), POINTER                         :: gopt_env
      94              :          REAL(KIND=dp), DIMENSION(:), POINTER               :: x0
      95              :          TYPE(gopt_param_type), POINTER                     :: gopt_param
      96              :          TYPE(section_vals_type), POINTER                   :: geo_section
      97              :       END SUBROUTINE gopt_f_dimer_rotation_optimizer
      98              :    END INTERFACE
      99              : 
     100              : CONTAINS
     101              : 
     102              : ! **************************************************************************************************
     103              : !> \brief ...
     104              : !> \param gopt_env the geometry optimization environment to be created
     105              : !>      force_env:
     106              : !> \param gopt_param ...
     107              : !> \param force_env ...
     108              : !> \param globenv ...
     109              : !> \param geo_opt_section ...
     110              : !> \param eval_opt_geo ...
     111              : !> \par History
     112              : !>      none
     113              : ! **************************************************************************************************
     114         1179 :    RECURSIVE SUBROUTINE gopt_f_create(gopt_env, gopt_param, force_env, globenv, geo_opt_section, &
     115              :                                       eval_opt_geo)
     116              : 
     117              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
     118              :       TYPE(gopt_param_type), POINTER                     :: gopt_param
     119              :       TYPE(force_env_type), POINTER                      :: force_env
     120              :       TYPE(global_environment_type), POINTER             :: globenv
     121              :       TYPE(section_vals_type), POINTER                   :: geo_opt_section
     122              :       LOGICAL, INTENT(IN), OPTIONAL                      :: eval_opt_geo
     123              : 
     124              :       INTEGER                                            :: nshell
     125              :       TYPE(cp_subsys_type), POINTER                      :: subsys
     126              :       TYPE(particle_list_type), POINTER                  :: particles, shell_particles
     127              :       TYPE(section_vals_type), POINTER                   :: dimer_section, rot_opt_section
     128              : 
     129         1179 :       CPASSERT(.NOT. ASSOCIATED(gopt_env))
     130        15327 :       ALLOCATE (gopt_env)
     131         1179 :       nshell = 0
     132              : 
     133         1179 :       NULLIFY (gopt_env%dimer_env, gopt_env%gopt_dimer_env, gopt_env%gopt_dimer_param, gopt_env%cell_env, gopt_env%spgr)
     134         1179 :       gopt_env%ref_count = 1
     135         1179 :       gopt_env%dimer_rotation = .FALSE.
     136         1179 :       gopt_env%do_line_search = .FALSE.
     137         5895 :       ALLOCATE (gopt_env%spgr)
     138              :       ! This routine reads the space-group flags once. The optimizer
     139              :       ! drivers consume spgr%* only.
     140              :       CALL section_vals_val_get(geo_opt_section, "KEEP_SPACE_GROUP", &
     141         1179 :                                 l_val=gopt_env%spgr%keep_space_group)
     142              :       CALL section_vals_val_get(geo_opt_section, "SHOW_SPACE_GROUP", &
     143         1179 :                                 l_val=gopt_env%spgr%show_space_group)
     144         1179 :       CALL force_env_retain(force_env)
     145         1179 :       gopt_env%force_env => force_env
     146         1179 :       gopt_env%motion_section => section_vals_get_subs_vals(force_env%root_section, "MOTION")
     147         1179 :       gopt_env%geo_section => geo_opt_section
     148         1179 :       gopt_env%globenv => globenv
     149         1179 :       gopt_env%eval_opt_geo = .TRUE.
     150         1179 :       IF (PRESENT(eval_opt_geo)) gopt_env%eval_opt_geo = eval_opt_geo
     151         1179 :       gopt_env%require_consistent_energy_force = .TRUE.
     152              : 
     153         1179 :       CALL force_env_get(force_env, subsys=subsys)
     154         1179 :       gopt_env%type_id = gopt_param%type_id
     155         2092 :       SELECT CASE (gopt_env%type_id)
     156              :       CASE (default_ts_method_id, default_minimization_method_id)
     157              :          CALL cp_subsys_get(subsys, &
     158              :                             particles=particles, &
     159          913 :                             shell_particles=shell_particles)
     160          913 :          IF (ASSOCIATED(shell_particles)) nshell = shell_particles%n_els
     161              :          ! The same number of shell and core particles is assumed
     162          913 :          gopt_env%label = "GEO_OPT"
     163          913 :          gopt_env%tag = "GEOMETRY"
     164         1203 :          SELECT CASE (gopt_param%type_id)
     165              :          CASE (default_ts_method_id)
     166           44 :             gopt_env%ts_method_id = gopt_param%ts_method_id
     167          913 :             SELECT CASE (gopt_param%ts_method_id)
     168              :             CASE (default_dimer_method_id)
     169              :                ! For the Dimer method we use the same framework of geometry optimizers
     170              :                ! already defined for cp2k..
     171           22 :                dimer_section => section_vals_get_subs_vals(geo_opt_section, "TRANSITION_STATE%DIMER")
     172           22 :                CALL dimer_env_create(gopt_env%dimer_env, subsys, globenv, dimer_section, force_env)
     173           22 :                gopt_env%solid_state_dimer = gopt_env%dimer_env%solid_state
     174              : 
     175              :                ! Setup the GEO_OPT environment for the rotation of the Dimer
     176           22 :                rot_opt_section => section_vals_get_subs_vals(dimer_section, "ROT_OPT")
     177           22 :                ALLOCATE (gopt_env%gopt_dimer_param)
     178              :                CALL gopt_param_read(gopt_env%gopt_dimer_param, rot_opt_section, &
     179           22 :                                     type_id=default_minimization_method_id)
     180           22 :                IF (gopt_param%method_id /= default_cg_method_id .OR. &
     181              :                    gopt_env%gopt_dimer_param%method_id /= default_cg_method_id) THEN
     182            0 :                   CALL cp_abort(__LOCATION__, "Dimer requires CG for translation and rotation.")
     183              :                END IF
     184           22 :                IF (gopt_env%solid_state_dimer .AND. gopt_env%spgr%keep_space_group) THEN
     185            0 :                   CALL cp_abort(__LOCATION__, "Solid-state Dimer does not support KEEP_SPACE_GROUP.")
     186              :                END IF
     187           22 :                gopt_env%gopt_dimer_param%type_id = default_ts_method_id
     188              : 
     189              :                CALL gopt_f_create(gopt_env%gopt_dimer_env, gopt_env%gopt_dimer_param, force_env=force_env, &
     190           22 :                                   globenv=globenv, geo_opt_section=rot_opt_section, eval_opt_geo=eval_opt_geo)
     191           22 :                CALL dimer_env_retain(gopt_env%dimer_env)
     192           22 :                gopt_env%gopt_dimer_env%dimer_env => gopt_env%dimer_env
     193           22 :                gopt_env%gopt_dimer_env%solid_state_dimer = gopt_env%solid_state_dimer
     194           22 :                gopt_env%gopt_dimer_env%label = "ROT_OPT"
     195           22 :                gopt_env%gopt_dimer_env%dimer_rotation = .TRUE.
     196              :                ! Dimer rotation optimizes a 3D unit vector, so space-group
     197              :                ! symmetry does not apply.
     198           22 :                gopt_env%gopt_dimer_env%spgr%keep_space_group = .FALSE.
     199           66 :                gopt_env%gopt_dimer_env%spgr%show_space_group = .FALSE.
     200              :             END SELECT
     201              :          END SELECT
     202              :       CASE (default_cell_method_id)
     203          246 :          gopt_env%label = "CELL_OPT"
     204          246 :          gopt_env%tag = "  CELL  "
     205        56088 :          ALLOCATE (gopt_env%cell_env)
     206          246 :          CALL cell_opt_env_create(gopt_env%cell_env, force_env, gopt_env%geo_section)
     207              :       CASE (default_shellcore_method_id)
     208           20 :          gopt_env%label = "SHELL_OPT"
     209           20 :          gopt_env%tag = "  SHELL-CORE  "
     210           20 :          gopt_env%shellcore_method_id = gopt_param%shellcore_method_id
     211              :          ! Shell-core relaxation bypasses the geometry-optimizer lifecycle.
     212           20 :          gopt_env%spgr%keep_space_group = .FALSE.
     213         1179 :          gopt_env%spgr%show_space_group = .FALSE.
     214              :       END SELECT
     215         1179 :    END SUBROUTINE gopt_f_create
     216              : 
     217              : ! **************************************************************************************************
     218              : !> \brief ...
     219              : !> \param gopt_env the geometry optimization environment to retain
     220              : !> \par History
     221              : !>      none
     222              : ! **************************************************************************************************
     223          106 :    SUBROUTINE gopt_f_retain(gopt_env)
     224              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
     225              : 
     226          106 :       CPASSERT(ASSOCIATED(gopt_env))
     227          106 :       CPASSERT(gopt_env%ref_count > 0)
     228          106 :       gopt_env%ref_count = gopt_env%ref_count + 1
     229          106 :    END SUBROUTINE gopt_f_retain
     230              : 
     231              : ! **************************************************************************************************
     232              : !> \brief ...
     233              : !> \param gopt_env the geometry optimization environment to release
     234              : !> \par History
     235              : !>      none
     236              : ! **************************************************************************************************
     237         2442 :    RECURSIVE SUBROUTINE gopt_f_release(gopt_env)
     238              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
     239              : 
     240         2442 :       IF (ASSOCIATED(gopt_env)) THEN
     241         1285 :          CPASSERT(gopt_env%ref_count > 0)
     242         1285 :          gopt_env%ref_count = gopt_env%ref_count - 1
     243         1285 :          IF (gopt_env%ref_count == 0) THEN
     244         1179 :             CALL force_env_release(gopt_env%force_env)
     245              :             NULLIFY (gopt_env%force_env, &
     246         1179 :                      gopt_env%globenv, &
     247         1179 :                      gopt_env%motion_section, &
     248         1179 :                      gopt_env%geo_section)
     249         1179 :             IF (ASSOCIATED(gopt_env%cell_env)) THEN
     250          246 :                CALL cell_opt_env_release(gopt_env%cell_env)
     251          246 :                DEALLOCATE (gopt_env%cell_env)
     252              :             END IF
     253         1179 :             CALL dimer_env_release(gopt_env%dimer_env)
     254         1179 :             CALL gopt_f_release(gopt_env%gopt_dimer_env)
     255         1179 :             IF (ASSOCIATED(gopt_env%gopt_dimer_param)) DEALLOCATE (gopt_env%gopt_dimer_param)
     256         1179 :             CALL release_spgr_type(gopt_env%spgr)
     257         1179 :             DEALLOCATE (gopt_env)
     258              :          END IF
     259              :       END IF
     260         2442 :    END SUBROUTINE gopt_f_release
     261              : 
     262            0 : END MODULE gopt_f_types
        

Generated by: LCOV version 2.0-1