LCOV - code coverage report
Current view: top level - src/motion - gopt_f_types.F (source / functions) Hit Total Coverage
Test: CP2K Regtests (git:262480d) Lines: 71 72 98.6 %
Date: 2024-11-22 07:00:40 Functions: 3 5 60.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 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, ts_method_id=0, cell_method_id=0, shellcore_method_id=0
      64             :       LOGICAL                                  :: dimer_rotation = .FALSE., do_line_search = .FALSE., eval_opt_geo = .FALSE.
      65             :       CHARACTER(LEN=default_string_length)     :: label = "", tag = ""
      66             :       TYPE(force_env_type), POINTER            :: force_env => NULL()
      67             :       TYPE(global_environment_type), POINTER   :: globenv => NULL()
      68             :       ! Motion section must be references only for IO of the MOTION%PRINT..
      69             :       TYPE(section_vals_type), POINTER         :: motion_section => NULL(), geo_section => NULL()
      70             :       TYPE(dimer_env_type), POINTER            :: dimer_env => NULL()
      71             :       TYPE(gopt_f_type), POINTER               :: gopt_dimer_env => NULL()
      72             :       TYPE(gopt_param_type), POINTER           :: gopt_dimer_param => NULL()
      73             :       TYPE(cell_opt_env_type), POINTER         :: cell_env => NULL()
      74             :       TYPE(spgr_type), POINTER                 :: spgr => NULL()
      75             :       REAL(KIND=dp), DIMENSION(3, 3)           :: h_ref = 0.0_dp
      76             :       LOGICAL                                  :: require_consistent_energy_force = .FALSE.
      77             :    END TYPE gopt_f_type
      78             : 
      79             : CONTAINS
      80             : 
      81             : ! **************************************************************************************************
      82             : !> \brief ...
      83             : !> \param gopt_env the geometry optimization environment to be created
      84             : !>      force_env:
      85             : !> \param gopt_param ...
      86             : !> \param force_env ...
      87             : !> \param globenv ...
      88             : !> \param geo_opt_section ...
      89             : !> \param eval_opt_geo ...
      90             : !> \par History
      91             : !>      none
      92             : ! **************************************************************************************************
      93        2006 :    RECURSIVE SUBROUTINE gopt_f_create(gopt_env, gopt_param, force_env, globenv, geo_opt_section, &
      94             :                                       eval_opt_geo)
      95             : 
      96             :       TYPE(gopt_f_type), POINTER                         :: gopt_env
      97             :       TYPE(gopt_param_type), POINTER                     :: gopt_param
      98             :       TYPE(force_env_type), POINTER                      :: force_env
      99             :       TYPE(global_environment_type), POINTER             :: globenv
     100             :       TYPE(section_vals_type), POINTER                   :: geo_opt_section
     101             :       LOGICAL, INTENT(IN), OPTIONAL                      :: eval_opt_geo
     102             : 
     103             :       INTEGER                                            :: natom, nshell
     104             :       TYPE(cp_subsys_type), POINTER                      :: subsys
     105             :       TYPE(particle_list_type), POINTER                  :: particles, shell_particles
     106             :       TYPE(section_vals_type), POINTER                   :: dimer_section, rot_opt_section
     107             : 
     108        2006 :       CPASSERT(.NOT. ASSOCIATED(gopt_env))
     109       26078 :       ALLOCATE (gopt_env)
     110        2006 :       nshell = 0
     111             : 
     112             :       NULLIFY (gopt_env%dimer_env, gopt_env%gopt_dimer_env, gopt_env%gopt_dimer_param, gopt_env%cell_env, gopt_env%spgr)
     113        2006 :       gopt_env%ref_count = 1
     114             :       gopt_env%dimer_rotation = .FALSE.
     115             :       gopt_env%do_line_search = .FALSE.
     116       10030 :       ALLOCATE (gopt_env%spgr)
     117        2006 :       CALL force_env_retain(force_env)
     118        2006 :       gopt_env%force_env => force_env
     119        2006 :       gopt_env%motion_section => section_vals_get_subs_vals(force_env%root_section, "MOTION")
     120        2006 :       gopt_env%geo_section => geo_opt_section
     121        2006 :       gopt_env%globenv => globenv
     122        2006 :       gopt_env%eval_opt_geo = .TRUE.
     123        2006 :       IF (PRESENT(eval_opt_geo)) gopt_env%eval_opt_geo = eval_opt_geo
     124        2006 :       gopt_env%require_consistent_energy_force = .TRUE.
     125             : 
     126        2006 :       CALL force_env_get(force_env, subsys=subsys)
     127        2006 :       gopt_env%type_id = gopt_param%type_id
     128        3782 :       SELECT CASE (gopt_env%type_id)
     129             :       CASE (default_ts_method_id, default_minimization_method_id)
     130             :          CALL cp_subsys_get(subsys, &
     131             :                             particles=particles, &
     132        1776 :                             shell_particles=shell_particles)
     133        1776 :          IF (ASSOCIATED(shell_particles)) nshell = shell_particles%n_els
     134             :          ! The same number of shell and core particles is assumed
     135        1776 :          gopt_env%nfree = particles%n_els + nshell
     136        1776 :          gopt_env%label = "GEO_OPT"
     137        1776 :          gopt_env%tag = "GEOMETRY"
     138        1776 :          SELECT CASE (gopt_param%type_id)
     139             :          CASE (default_ts_method_id)
     140          44 :             gopt_env%ts_method_id = gopt_param%ts_method_id
     141          44 :             SELECT CASE (gopt_param%ts_method_id)
     142             :             CASE (default_dimer_method_id)
     143             :                ! For the Dimer method we use the same framework of geometry optimizers
     144             :                ! already defined for cp2k..
     145          22 :                natom = force_env_get_natom(force_env)
     146          22 :                dimer_section => section_vals_get_subs_vals(geo_opt_section, "TRANSITION_STATE%DIMER")
     147          22 :                CALL dimer_env_create(gopt_env%dimer_env, subsys, globenv, dimer_section)
     148             : 
     149             :                ! Setup the GEO_OPT environment for the rotation of the Dimer
     150          22 :                rot_opt_section => section_vals_get_subs_vals(dimer_section, "ROT_OPT")
     151          22 :                ALLOCATE (gopt_env%gopt_dimer_param)
     152             :                CALL gopt_param_read(gopt_env%gopt_dimer_param, rot_opt_section, &
     153          22 :                                     type_id=default_minimization_method_id)
     154          22 :                gopt_env%gopt_dimer_param%type_id = default_ts_method_id
     155             : 
     156             :                CALL gopt_f_create(gopt_env%gopt_dimer_env, gopt_env%gopt_dimer_param, force_env=force_env, &
     157          22 :                                   globenv=globenv, geo_opt_section=rot_opt_section, eval_opt_geo=eval_opt_geo)
     158          22 :                CALL dimer_env_retain(gopt_env%dimer_env)
     159          22 :                gopt_env%gopt_dimer_env%dimer_env => gopt_env%dimer_env
     160          22 :                gopt_env%gopt_dimer_env%label = "ROT_OPT"
     161          22 :                gopt_env%gopt_dimer_env%dimer_rotation = .TRUE.
     162             :             END SELECT
     163             :          END SELECT
     164             :       CASE (default_cell_method_id)
     165         210 :          gopt_env%nfree = 6
     166         210 :          gopt_env%label = "CELL_OPT"
     167         210 :          gopt_env%tag = "  CELL  "
     168         210 :          gopt_env%cell_method_id = gopt_param%cell_method_id
     169        5670 :          ALLOCATE (gopt_env%cell_env)
     170         210 :          CALL cell_opt_env_create(gopt_env%cell_env, force_env, gopt_env%geo_section)
     171             :       CASE (default_shellcore_method_id)
     172          20 :          gopt_env%nfree = subsys%shell_particles%n_els
     173          20 :          gopt_env%label = "SHELL_OPT"
     174          20 :          gopt_env%tag = "  SHELL-CORE  "
     175        2006 :          gopt_env%shellcore_method_id = gopt_param%shellcore_method_id
     176             :       END SELECT
     177        2006 :    END SUBROUTINE gopt_f_create
     178             : 
     179             : ! **************************************************************************************************
     180             : !> \brief ...
     181             : !> \param gopt_env the geometry optimization environment to retain
     182             : !> \par History
     183             : !>      none
     184             : ! **************************************************************************************************
     185         392 :    SUBROUTINE gopt_f_retain(gopt_env)
     186             :       TYPE(gopt_f_type), POINTER                         :: gopt_env
     187             : 
     188         392 :       CPASSERT(ASSOCIATED(gopt_env))
     189         392 :       CPASSERT(gopt_env%ref_count > 0)
     190         392 :       gopt_env%ref_count = gopt_env%ref_count + 1
     191         392 :    END SUBROUTINE gopt_f_retain
     192             : 
     193             : ! **************************************************************************************************
     194             : !> \brief ...
     195             : !> \param gopt_env the geometry optimization environment to release
     196             : !> \par History
     197             : !>      none
     198             : ! **************************************************************************************************
     199        4382 :    RECURSIVE SUBROUTINE gopt_f_release(gopt_env)
     200             :       TYPE(gopt_f_type), POINTER                         :: gopt_env
     201             : 
     202        4382 :       IF (ASSOCIATED(gopt_env)) THEN
     203        2398 :          CPASSERT(gopt_env%ref_count > 0)
     204        2398 :          gopt_env%ref_count = gopt_env%ref_count - 1
     205        2398 :          IF (gopt_env%ref_count == 0) THEN
     206        2006 :             CALL force_env_release(gopt_env%force_env)
     207             :             NULLIFY (gopt_env%force_env, &
     208        2006 :                      gopt_env%globenv, &
     209        2006 :                      gopt_env%motion_section, &
     210        2006 :                      gopt_env%geo_section)
     211        2006 :             IF (ASSOCIATED(gopt_env%cell_env)) THEN
     212         210 :                CALL cell_opt_env_release(gopt_env%cell_env)
     213         210 :                DEALLOCATE (gopt_env%cell_env)
     214             :             END IF
     215        2006 :             CALL dimer_env_release(gopt_env%dimer_env)
     216        2006 :             CALL gopt_f_release(gopt_env%gopt_dimer_env)
     217        2006 :             IF (ASSOCIATED(gopt_env%gopt_dimer_param)) DEALLOCATE (gopt_env%gopt_dimer_param)
     218        2006 :             CALL release_spgr_type(gopt_env%spgr)
     219        2006 :             DEALLOCATE (gopt_env)
     220             :          END IF
     221             :       END IF
     222        4382 :    END SUBROUTINE gopt_f_release
     223             : 
     224           0 : END MODULE gopt_f_types

Generated by: LCOV version 1.15