LCOV - code coverage report
Current view: top level - src/motion - dimer_types.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 89.2 % 240 214
Test Date: 2026-11-24 01:22:08 Functions: 61.5 % 13 8

            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 types used for a Dimer Method calculations
      10              : !> \par History
      11              : !>      Luca Bellucci 11.2017 added kdimer and beta
      12              : !> \author Luca Bellucci and Teodoro Laino - created [tlaino] - 01.2008
      13              : ! **************************************************************************************************
      14              : MODULE dimer_types
      15              : 
      16              :    USE cell_types,                      ONLY: &
      17              :         cell_sym_none, cell_transform_input_cartesian, cell_type, use_perd_x, use_perd_xy, &
      18              :         use_perd_xyz, use_perd_xz, use_perd_y, use_perd_yz, use_perd_z
      19              :    USE cp_log_handling,                 ONLY: cp_logger_get_default_io_unit,&
      20              :                                               cp_to_string
      21              :    USE cp_parser_methods,               ONLY: parser_get_next_line,&
      22              :                                               parser_search_string
      23              :    USE cp_parser_types,                 ONLY: cp_parser_type,&
      24              :                                               parser_create,&
      25              :                                               parser_release
      26              :    USE cp_subsys_types,                 ONLY: cp_subsys_get,&
      27              :                                               cp_subsys_type
      28              :    USE force_env_types,                 ONLY: force_env_type,&
      29              :                                               use_fist_force,&
      30              :                                               use_qs_force
      31              :    USE global_types,                    ONLY: global_environment_type
      32              :    USE input_constants,                 ONLY: dimer_init_molden,&
      33              :                                               dimer_init_random,&
      34              :                                               do_first_rotation_step
      35              :    USE input_section_types,             ONLY: section_vals_get,&
      36              :                                               section_vals_get_subs_vals,&
      37              :                                               section_vals_type,&
      38              :                                               section_vals_val_get,&
      39              :                                               section_vals_val_set
      40              :    USE kinds,                           ONLY: default_path_length,&
      41              :                                               dp
      42              :    USE message_passing,                 ONLY: mp_para_env_type
      43              :    USE molecule_kind_list_types,        ONLY: molecule_kind_list_type
      44              :    USE molecule_kind_types,             ONLY: fixd_constraint_type,&
      45              :                                               get_molecule_kind,&
      46              :                                               molecule_kind_type
      47              :    USE solid_state_coordinates,         ONLY: solid_state_frame,&
      48              :                                               solid_state_frame_type,&
      49              :                                               ss_cell_dof
      50              :    USE virial_types,                    ONLY: virial_type
      51              : #include "../base/base_uses.f90"
      52              : 
      53              :    IMPLICIT NONE
      54              :    PRIVATE
      55              : 
      56              :    LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
      57              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'dimer_types'
      58              : 
      59              :    PUBLIC :: dimer_env_type, &
      60              :              dimer_env_create, &
      61              :              dimer_env_retain, &
      62              :              dimer_env_release, &
      63              :              dimer_fixed_atom_control, &
      64              :              dimer_init_vector, &
      65              :              dimer_remove_translation, &
      66              :              vib_get_index_weight
      67              : 
      68              : ! **************************************************************************************************
      69              : !> \brief Type containing all informations abour the rotation of the Dimer
      70              : !> \par History
      71              : !>      none
      72              : !> \author Luca Bellucci and Teodoro Laino - created [tlaino] - 01.2008
      73              : ! **************************************************************************************************
      74              :    TYPE dimer_rotational_type
      75              :       ! Rotational parameters
      76              :       INTEGER                                    :: rotation_step = 0
      77              :       LOGICAL                                    :: interpolate_gradient = .FALSE.
      78              :       REAL(KIND=dp)                              :: angle_tol = 0.0_dp, angle1 = 0.0_dp, angle2 = 0.0_dp, &
      79              :                                                     dCdp = 0.0_dp, curvature = 0.0_dp
      80              :       REAL(KIND=dp), POINTER, DIMENSION(:)       :: g0 => NULL(), g1 => NULL(), g1p => NULL()
      81              :    END TYPE dimer_rotational_type
      82              : 
      83              : ! **************************************************************************************************
      84              : !> \brief Type containing all informations abour the translation of the Dimer
      85              : !> \par History
      86              : !>      none
      87              : !> \author Luca Bellucci and Teodoro Laino - created [tlaino] - 01.2008
      88              : ! **************************************************************************************************
      89              :    TYPE dimer_translational_type
      90              :       ! Translational parameters
      91              :       REAL(KIND=dp), POINTER, DIMENSION(:)       :: tls_vec => NULL()
      92              :    END TYPE dimer_translational_type
      93              : 
      94              : ! **************************************************************************************************
      95              : !> \brief Conjugate Directions type
      96              : !> \par History
      97              : !>      none
      98              : !> \author Luca Bellucci and Teodoro Laino - created [tlaino] - 01.2008
      99              : ! **************************************************************************************************
     100              :    TYPE dimer_cg_rot_type
     101              :       REAL(KIND=dp)                              :: norm_theta = 0.0_dp, norm_theta_old = 0.0_dp, norm_h = 0.0_dp
     102              :       REAL(KIND=dp), POINTER, DIMENSION(:)       :: nvec_old => NULL()
     103              :    END TYPE dimer_cg_rot_type
     104              : 
     105              : ! **************************************************************************************************
     106              : !> \brief Defines the environment for a Dimer Method calculation
     107              : !> \par History
     108              : !>      none
     109              : !> \author Luca Bellucci and Teodoro Laino - created [tlaino] - 01.2008
     110              : ! **************************************************************************************************
     111              :    TYPE dimer_env_type
     112              :       INTEGER                                    :: ref_count = 0
     113              :       REAL(KIND=dp)                              :: dr = 0.0_dp
     114              :       REAL(KIND=dp), POINTER, DIMENSION(:)       :: nvec => NULL()
     115              :       REAL(KIND=dp)                              :: beta = 0.0_dp
     116              :       TYPE(dimer_rotational_type)                :: rot = dimer_rotational_type()
     117              :       TYPE(dimer_translational_type)             :: tsl = dimer_translational_type()
     118              :       TYPE(dimer_cg_rot_type)                    :: cg_rot = dimer_cg_rot_type()
     119              :       LOGICAL                                    :: kdimer = .FALSE.
     120              :       LOGICAL                                    :: solid_state = .FALSE.
     121              :       REAL(KIND=dp)                              :: external_pressure = 0.0_dp, enthalpy = 0.0_dp
     122              :       REAL(KIND=dp), ALLOCATABLE                  :: centre_force(:)
     123              :       TYPE(solid_state_frame_type)                :: frame
     124              :       TYPE(virial_type)                           :: centre_virial
     125              :    END TYPE dimer_env_type
     126              : 
     127              : CONTAINS
     128              : 
     129              : ! **************************************************************************************************
     130              : !> \brief ...
     131              : !> \param dimer_env ...
     132              : !> \param subsys ...
     133              : !> \param globenv ...
     134              : !> \param dimer_section ...
     135              : !> \param force_env ...
     136              : !> \par History
     137              : !>      Luca Bellucci 11.2017 added K-DIMER and BETA
     138              : !>      2016/03/03 [LTong] changed input natom to subsys
     139              : !> \author Luca Bellucci and Teodoro Laino - created [tlaino] - 01.2008
     140              : ! **************************************************************************************************
     141           22 :    SUBROUTINE dimer_env_create(dimer_env, subsys, globenv, dimer_section, force_env)
     142              :       TYPE(dimer_env_type), POINTER                      :: dimer_env
     143              :       TYPE(cp_subsys_type), POINTER                      :: subsys
     144              :       TYPE(global_environment_type), POINTER             :: globenv
     145              :       TYPE(section_vals_type), POINTER                   :: dimer_section
     146              :       TYPE(force_env_type), POINTER                      :: force_env
     147              : 
     148              :       INTEGER                                            :: i, j, k, natom, nvar, unit_nr
     149              :       REAL(KIND=dp)                                      :: norm, xval(3)
     150              :       TYPE(cell_type), POINTER                           :: cell
     151              : 
     152           22 :       NULLIFY (cell)
     153           22 :       unit_nr = cp_logger_get_default_io_unit()
     154           22 :       CPASSERT(.NOT. ASSOCIATED(dimer_env))
     155         5566 :       ALLOCATE (dimer_env)
     156           22 :       dimer_env%ref_count = 1
     157              :       ! Setup NVEC
     158              :       ! get natom
     159           22 :       CALL cp_subsys_get(subsys=subsys, cell=cell, natom=natom)
     160           22 :       CALL section_vals_val_get(dimer_section, "SOLID_STATE", l_val=dimer_env%solid_state)
     161           22 :       nvar = 3*natom
     162           22 :       IF (dimer_env%solid_state) THEN
     163            4 :          CALL setup_solid_state_dimer(dimer_env, dimer_section, force_env, subsys, cell, natom)
     164           12 :          ALLOCATE (dimer_env%centre_force(nvar))
     165            4 :          nvar = nvar + ss_cell_dof
     166              :       END IF
     167              :       ! Allocate the working arrays
     168           66 :       ALLOCATE (dimer_env%rot%g0(nvar))
     169           44 :       ALLOCATE (dimer_env%rot%g1(nvar))
     170           44 :       ALLOCATE (dimer_env%rot%g1p(nvar))
     171              :       ! Read dimer vector from input
     172              :       CALL dimer_init_vector(dimer_env, dimer_section, natom, &
     173           22 :                              unit_nr, globenv, force_env)
     174           22 :       IF (unit_nr > 0) THEN
     175              :          WRITE (unit_nr, "(/,T2,A,T9,A,T71,I10)") &
     176           11 :             "DIMER|", "Dimension of dimer vector", nvar
     177           51 :          DO j = 1, nvar/3
     178              :             WRITE (unit_nr, "(T2,A,T9,3(F12.6,3X))") &
     179          171 :                "DIMER|", dimer_env%nvec(3*j - 2:3*j)
     180              :          END DO
     181              :       END IF
     182           22 :       IF (dimer_env%solid_state) THEN
     183            4 :          CALL dimer_remove_translation(dimer_env%nvec, natom)
     184              :       ELSE
     185              :          ! Transform input cell
     186           82 :          DO i = 1, natom
     187          256 :             xval(:) = dimer_env%nvec(3*i - 2:3*i)
     188           64 :             CALL cell_transform_input_cartesian(cell, xval)
     189          274 :             dimer_env%nvec(3*i - 2:3*i) = xval(:)
     190              :          END DO
     191              :          ! Check for translation in the dimer vector and remove them
     192           18 :          IF (natom > 1) THEN
     193           16 :             xval = 0.0_dp
     194           78 :             DO j = 1, natom
     195          264 :                DO k = 1, 3
     196          186 :                   i = (j - 1)*3 + k
     197          248 :                   xval(k) = xval(k) + dimer_env%nvec(i)
     198              :                END DO
     199              :             END DO
     200           16 :             IF (unit_nr > 0) THEN
     201              :                WRITE (unit_nr, "(/,T2,A,T9,A)") &
     202            8 :                   "DIMER|", "Overall translation to be removed from the initial dimer vector"
     203              :                WRITE (unit_nr, "(T2,A,T9,3(F12.6,3X))") &
     204            8 :                   "DIMER|", xval(1:3)
     205              :             END IF
     206              :             ! Subtract net translations
     207           64 :             xval = xval/REAL(natom*3, KIND=dp)
     208           78 :             DO j = 1, natom
     209          264 :                DO k = 1, 3
     210          186 :                   i = (j - 1)*3 + k
     211          248 :                   dimer_env%nvec(i) = dimer_env%nvec(i) - xval(k)
     212              :                END DO
     213              :             END DO
     214              :          END IF
     215              :          ! set nvec components to zero for the corresponding constraints
     216           18 :          CALL dimer_fixed_atom_control(dimer_env%nvec, subsys, unit_nr)
     217              :       END IF
     218              :       ! Normalize dimer vector
     219          262 :       norm = SQRT(SUM(dimer_env%nvec**2))
     220           22 :       IF (norm <= EPSILON(0.0_dp)) THEN
     221            0 :          CPABORT("The norm of the dimer vector is 0! Calculation cannot proceed further.")
     222              :       END IF
     223           22 :       IF (unit_nr > 0) THEN
     224              :          WRITE (unit_nr, "(T2,A,T9,A,T69,F12.6)") &
     225           11 :             "DIMER|", "Norm of dimer vector to be normalized by rescaling", norm
     226              :       END IF
     227          262 :       dimer_env%nvec = dimer_env%nvec/norm
     228           22 :       dimer_env%rot%rotation_step = do_first_rotation_step
     229           22 :       CALL section_vals_val_get(dimer_section, "DR", r_val=dimer_env%dr)
     230              :       CALL section_vals_val_get(dimer_section, "INTERPOLATE_GRADIENT", &
     231           22 :                                 l_val=dimer_env%rot%interpolate_gradient)
     232              :       CALL section_vals_val_get(dimer_section, "ANGLE_TOLERANCE", &
     233           22 :                                 r_val=dimer_env%rot%angle_tol)
     234              :       CALL section_vals_val_get(dimer_section, "K-DIMER", &
     235           22 :                                 l_val=dimer_env%kdimer)
     236           22 :       IF (dimer_env%solid_state .AND. dimer_env%kdimer) THEN
     237            0 :          CPABORT("K-DIMER is not supported for solid-state searches.")
     238              :       END IF
     239              :       CALL section_vals_val_get(dimer_section, "BETA", &
     240           22 :                                 r_val=dimer_env%beta)
     241              :       ! initialise values
     242           22 :       dimer_env%cg_rot%norm_h = 1.0_dp
     243          262 :       dimer_env%rot%g0 = 0.0_dp
     244          262 :       dimer_env%rot%g1 = 0.0_dp
     245          262 :       dimer_env%rot%g1p = 0.0_dp
     246           44 :       ALLOCATE (dimer_env%cg_rot%nvec_old(nvar))
     247           22 :    END SUBROUTINE dimer_env_create
     248              : 
     249              : ! **************************************************************************************************
     250              : !> \brief Initialize the solid-state chart and persist the reference cell for restarts.
     251              : !> \param dimer_env dimer environment
     252              : !> \param section dimer section
     253              : !> \param force_env force environment
     254              : !> \param subsys subsystem
     255              : !> \param cell physical cell
     256              : !> \param natom number of atoms
     257              : ! **************************************************************************************************
     258           16 :    SUBROUTINE setup_solid_state_dimer(dimer_env, section, force_env, subsys, cell, natom)
     259              :       TYPE(dimer_env_type), INTENT(INOUT)                :: dimer_env
     260              :       TYPE(section_vals_type), POINTER                   :: section
     261              :       TYPE(force_env_type), INTENT(IN)                   :: force_env
     262              :       TYPE(cp_subsys_type), POINTER                      :: subsys
     263              :       TYPE(cell_type), INTENT(IN)                        :: cell
     264              :       INTEGER, INTENT(IN)                                :: natom
     265              : 
     266              :       INTEGER                                            :: nparticle, nval
     267              :       LOGICAL                                            :: constrained
     268              :       REAL(KIND=dp)                                      :: weight
     269            4 :       REAL(KIND=dp), POINTER                             :: values(:)
     270              :       TYPE(virial_type), POINTER                         :: virial
     271              : 
     272            4 :       IF (force_env%in_use /= use_qs_force .AND. force_env%in_use /= use_fist_force) THEN
     273            0 :          CPABORT("Solid-state Dimer currently supports Quickstep and FIST.")
     274              :       END IF
     275            4 :       CALL cp_subsys_get(subsys, virial=virial, nparticle=nparticle)
     276            4 :       IF (.NOT. virial%pv_availability .OR. virial%pv_numer .OR. virial%pv_diagonal) THEN
     277            0 :          CALL cp_abort(__LOCATION__, "Solid-state Dimer requires FORCE_EVAL/STRESS_TENSOR ANALYTICAL.")
     278              :       END IF
     279           16 :       IF (ANY(cell%perd /= 1) .OR. cell%symmetry_id /= cell_sym_none .OR. cell%input_cell_canonicalized) THEN
     280              :          CALL cp_abort(__LOCATION__, &
     281            0 :                        "Solid-state Dimer requires PERIODIC XYZ, no SUBSYS/CELL symmetry, and no automatic cell rotation.")
     282              :       END IF
     283            4 :       CALL section_vals_get(section_vals_get_subs_vals(force_env%root_section, "MOTION%CONSTRAINT"), explicit=constrained)
     284            4 :       IF (constrained .OR. nparticle /= natom) THEN
     285            0 :          CPABORT("Solid-state Dimer does not support constraints or shells.")
     286              :       END IF
     287            4 :       CALL section_vals_val_get(section, "CELL_WEIGHT", r_val=weight)
     288            4 :       IF (.NOT. (weight > 0.0_dp)) THEN
     289            0 :          CPABORT("Solid-state Dimer requires a positive CELL_WEIGHT.")
     290              :       END IF
     291            4 :       CALL section_vals_val_get(section, "EXTERNAL_PRESSURE", r_val=dimer_env%external_pressure)
     292            4 :       CALL section_vals_val_get(section, "REFERENCE_CELL", n_rep_val=nval)
     293            4 :       IF (nval == 0) THEN
     294            4 :          CALL solid_state_frame(dimer_env%frame, cell%hmat, natom, weight)
     295            4 :          ALLOCATE (values(9))
     296            4 :          values(:) = RESHAPE(cell%hmat, [9])
     297            4 :          CALL section_vals_val_set(section, "REFERENCE_CELL", r_vals_ptr=values)
     298              :       ELSE
     299            0 :          CALL section_vals_val_get(section, "REFERENCE_CELL", r_vals=values)
     300            0 :          CALL solid_state_frame(dimer_env%frame, RESHAPE(values, [3, 3]), natom, weight)
     301              :       END IF
     302            4 :    END SUBROUTINE setup_solid_state_dimer
     303              : 
     304              : ! **************************************************************************************************
     305              : !> \brief Remove uniform atomic translations without changing the lattice direction.
     306              : !> \param v joint positions or forces written as an atomic block followed by the cell block
     307              : !> \param natom number of atoms
     308              : ! **************************************************************************************************
     309           84 :    SUBROUTINE dimer_remove_translation(v, natom)
     310              :       REAL(KIND=dp), INTENT(INOUT)                       :: v(:)
     311              :       INTEGER, INTENT(IN)                                :: natom
     312              : 
     313              :       INTEGER                                            :: d
     314              : 
     315          336 :       DO d = 1, 3
     316         1344 :          v(d:3*natom:3) = v(d:3*natom:3) - SUM(v(d:3*natom:3))/natom
     317              :       END DO
     318           84 :    END SUBROUTINE dimer_remove_translation
     319              : 
     320              : ! **************************************************************************************************
     321              : !> \brief ...
     322              : !> \param dimer_env ...
     323              : !> \par History
     324              : !>      none
     325              : !> \author Luca Bellucci and Teodoro Laino - created [tlaino] - 01.2008
     326              : ! **************************************************************************************************
     327           22 :    SUBROUTINE dimer_env_retain(dimer_env)
     328              :       TYPE(dimer_env_type), POINTER                      :: dimer_env
     329              : 
     330           22 :       CPASSERT(ASSOCIATED(dimer_env))
     331           22 :       CPASSERT(dimer_env%ref_count > 0)
     332           22 :       dimer_env%ref_count = dimer_env%ref_count + 1
     333           22 :    END SUBROUTINE dimer_env_retain
     334              : 
     335              : ! **************************************************************************************************
     336              : !> \brief ...
     337              : !> \param dimer_env ...
     338              : !> \par History
     339              : !>      none
     340              : !> \author Luca Bellucci and Teodoro Laino - created [tlaino] - 01.2008
     341              : ! **************************************************************************************************
     342         1179 :    SUBROUTINE dimer_env_release(dimer_env)
     343              :       TYPE(dimer_env_type), POINTER                      :: dimer_env
     344              : 
     345         1179 :       IF (ASSOCIATED(dimer_env)) THEN
     346           44 :          CPASSERT(dimer_env%ref_count > 0)
     347           44 :          dimer_env%ref_count = dimer_env%ref_count - 1
     348           44 :          IF (dimer_env%ref_count == 0) THEN
     349           22 :             IF (ASSOCIATED(dimer_env%nvec)) THEN
     350           22 :                DEALLOCATE (dimer_env%nvec)
     351              :             END IF
     352           22 :             IF (ASSOCIATED(dimer_env%rot%g0)) THEN
     353           22 :                DEALLOCATE (dimer_env%rot%g0)
     354              :             END IF
     355           22 :             IF (ASSOCIATED(dimer_env%rot%g1)) THEN
     356           22 :                DEALLOCATE (dimer_env%rot%g1)
     357              :             END IF
     358           22 :             IF (ASSOCIATED(dimer_env%rot%g1p)) THEN
     359           22 :                DEALLOCATE (dimer_env%rot%g1p)
     360              :             END IF
     361           22 :             IF (ASSOCIATED(dimer_env%cg_rot%nvec_old)) THEN
     362           22 :                DEALLOCATE (dimer_env%cg_rot%nvec_old)
     363              :             END IF
     364              :             ! No need to deallocate tls_vec (just a pointer to aother local array)
     365           22 :             NULLIFY (dimer_env%tsl%tls_vec)
     366           22 :             DEALLOCATE (dimer_env)
     367              :          END IF
     368              :       END IF
     369         1179 :    END SUBROUTINE dimer_env_release
     370              : 
     371              : ! **************************************************************************************************
     372              : !> \brief Set parts of a given array vec to zero according to fixed atom constraints.
     373              : !>        When atoms are (partially) fixed then the relevant components of
     374              : !>        nvec should be set to zero.  Furthermore, the relevant components
     375              : !>        of the gradient in CG should also be set to zero.
     376              : !> \param vec : vector to be modified
     377              : !> \param subsys : subsys type object used by CP2k
     378              : !> \param unit : unit to write message to
     379              : !> \par History
     380              : !>      2016/03/03 [LTong] created
     381              : !> \author Lianheng Tong [LTong]
     382              : ! **************************************************************************************************
     383          390 :    SUBROUTINE dimer_fixed_atom_control(vec, subsys, unit)
     384              :       REAL(KIND=dp), DIMENSION(:), INTENT(INOUT)         :: vec
     385              :       TYPE(cp_subsys_type), POINTER                      :: subsys
     386              :       INTEGER, INTENT(IN), OPTIONAL                      :: unit
     387              : 
     388              :       INTEGER                                            :: ii, ikind, ind, iparticle, nfixed_atoms, &
     389              :                                                             nkinds
     390          390 :       TYPE(fixd_constraint_type), DIMENSION(:), POINTER  :: fixd_list
     391              :       TYPE(molecule_kind_list_type), POINTER             :: molecule_kinds
     392          390 :       TYPE(molecule_kind_type), DIMENSION(:), POINTER    :: molecule_kind_set
     393              :       TYPE(molecule_kind_type), POINTER                  :: molecule_kind
     394              : 
     395          390 :       NULLIFY (molecule_kinds, molecule_kind, fixd_list)
     396              : 
     397              :       ! need to get constraint information from molecule information
     398              :       CALL cp_subsys_get(subsys=subsys, &
     399          390 :                          molecule_kinds=molecule_kinds)
     400          390 :       molecule_kind_set => molecule_kinds%els
     401              : 
     402              :       ! get total number of fixed atoms
     403              :       ! nkinds is the kinds of molecules, not atoms
     404          390 :       nkinds = molecule_kinds%n_els
     405          990 :       DO ikind = 1, nkinds
     406          600 :          molecule_kind => molecule_kind_set(ikind)
     407              :          CALL get_molecule_kind(molecule_kind, &
     408              :                                 nfixd=nfixed_atoms, &
     409          600 :                                 fixd_list=fixd_list)
     410          990 :          IF (ASSOCIATED(fixd_list)) THEN
     411           42 :             IF (PRESENT(unit)) THEN
     412            6 :                IF (unit > 0) THEN
     413              :                   WRITE (unit, "(/,T2,A,T9,A,T71,I10)") &
     414            3 :                      "DIMER|", "Number of fixed atoms to adjust dimer vector", nfixed_atoms
     415              :                END IF
     416              :             END IF
     417           56 :             DO ii = 1, nfixed_atoms
     418           56 :                IF (.NOT. fixd_list(ii)%restraint%active) THEN
     419           14 :                   iparticle = fixd_list(ii)%fixd
     420           14 :                   ind = (iparticle - 1)*3
     421              :                   ! apply constraint to nvec
     422           14 :                   SELECT CASE (fixd_list(ii)%itype)
     423              :                   CASE (use_perd_x)
     424            0 :                      vec(ind + 1) = 0.0_dp
     425              :                   CASE (use_perd_y)
     426            0 :                      vec(ind + 2) = 0.0_dp
     427              :                   CASE (use_perd_z)
     428            0 :                      vec(ind + 3) = 0.0_dp
     429              :                   CASE (use_perd_xy)
     430            0 :                      vec(ind + 1) = 0.0_dp
     431            0 :                      vec(ind + 2) = 0.0_dp
     432              :                   CASE (use_perd_xz)
     433            0 :                      vec(ind + 1) = 0.0_dp
     434            0 :                      vec(ind + 3) = 0.0_dp
     435              :                   CASE (use_perd_yz)
     436            0 :                      vec(ind + 2) = 0.0_dp
     437            0 :                      vec(ind + 3) = 0.0_dp
     438              :                   CASE (use_perd_xyz)
     439           14 :                      vec(ind + 1) = 0.0_dp
     440           14 :                      vec(ind + 2) = 0.0_dp
     441           14 :                      vec(ind + 3) = 0.0_dp
     442              :                   END SELECT
     443              :                END IF ! .NOT.fixd_list(ii)%restraint%active
     444              :             END DO ! ii
     445              :          END IF ! ASSOCIATED(fixd_list)
     446              :       END DO ! ikind
     447          390 :    END SUBROUTINE dimer_fixed_atom_control
     448              : 
     449              : ! **************************************************************************************************
     450              : !> \brief Read dimer vector from input and store into dimer_env%nvec
     451              : !> \param dimer_env ...
     452              : !> \param dimer_section ...
     453              : !> \param natom ...
     454              : !> \param unit ...
     455              : !> \param globenv ...
     456              : !> \param force_env ...
     457              : !> \par History
     458              : !>      2026/05  created
     459              : !> \author HE Zilong
     460              : ! **************************************************************************************************
     461           22 :    SUBROUTINE dimer_init_vector(dimer_env, dimer_section, natom, unit, globenv, force_env)
     462              :       TYPE(dimer_env_type), INTENT(INOUT), POINTER       :: dimer_env
     463              :       TYPE(section_vals_type), INTENT(INOUT), POINTER    :: dimer_section
     464              :       INTEGER, INTENT(IN)                                :: natom, unit
     465              :       TYPE(global_environment_type), INTENT(IN), POINTER :: globenv
     466              :       TYPE(force_env_type), POINTER                      :: force_env
     467              : 
     468              :       CHARACTER(LEN=17)                                  :: vib_name
     469              :       CHARACTER(LEN=default_path_length)                 :: molden_name
     470              :       INTEGER                                            :: dimer_init_method, i, ierr, isize, j, &
     471              :                                                             n_rep_val, nvec_size, vib_list_size
     472           22 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: vib_id_list
     473              :       LOGICAL                                            :: explicit, found
     474           44 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: array_r, vib_wt_list
     475           22 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: array
     476              :       TYPE(cp_parser_type)                               :: parser
     477              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     478              :       TYPE(section_vals_type), POINTER                   :: nvec_section
     479              : 
     480           22 :       para_env => force_env%para_env
     481           44 :       nvec_section => section_vals_get_subs_vals(dimer_section, "DIMER_VECTOR")
     482           22 :       NULLIFY (array)
     483           22 :       nvec_size = natom*3
     484           22 :       IF (dimer_env%solid_state) nvec_size = nvec_size + ss_cell_dof
     485              : 
     486           66 :       ALLOCATE (dimer_env%nvec(nvec_size))
     487          262 :       dimer_env%nvec(:) = 0.0_dp
     488           22 :       CALL section_vals_get(nvec_section, explicit=explicit)
     489           22 :       IF (explicit) THEN
     490            8 :          IF (unit > 0) WRITE (unit, "(/,T2,A,T9,A)") &
     491            4 :             "DIMER|", "Initial dimer vector read from input DIMER_VECTOR section"
     492            8 :          isize = 0
     493            8 :          CALL section_vals_val_get(nvec_section, "_DEFAULT_KEYWORD_", n_rep_val=n_rep_val)
     494           26 :          DO i = 1, n_rep_val
     495           18 :             CALL section_vals_val_get(nvec_section, "_DEFAULT_KEYWORD_", r_vals=array, i_rep_val=i)
     496           98 :             DO j = 1, SIZE(array)
     497           72 :                isize = isize + 1
     498           90 :                IF (isize <= nvec_size) THEN
     499           72 :                   dimer_env%nvec(isize) = array(j)
     500              :                ELSE
     501            0 :                   CPABORT("Too many components in DIMER_VECTOR.")
     502              :                END IF
     503              :             END DO
     504              :          END DO
     505            8 :          IF (isize /= nvec_size) THEN
     506            0 :             CPABORT("Incorrect number of components in DIMER_VECTOR.")
     507              :          END IF
     508              :       ELSE
     509           14 :          CALL section_vals_val_get(dimer_section, "INITIALIZATION_METHOD", i_val=dimer_init_method)
     510           12 :          SELECT CASE (dimer_init_method)
     511              :          CASE (dimer_init_random)
     512           12 :             IF (unit > 0) WRITE (unit, "(/,T2,A,T9,A)") &
     513            6 :                "DIMER|", "Initial dimer vector generated randomly"
     514           12 :             CALL globenv%gaussian_rng_stream%fill(dimer_env%nvec)
     515              :          CASE (dimer_init_molden)
     516            2 :             IF (dimer_env%solid_state) THEN
     517              :                CALL cp_abort(__LOCATION__, &
     518            0 :                              "MOLDEN modes do not include the six solid-state cell components.")
     519              :             END IF
     520            2 :             CALL section_vals_val_get(dimer_section, "VIB_MOLDEN_NAME", explicit=explicit)
     521            2 :             IF (.NOT. explicit) THEN
     522            0 :                CPABORT("INITIALIZATION_METHOD MOLDEN requires VIB_MOLDEN_NAME")
     523              :             END IF
     524            2 :             CALL section_vals_val_get(dimer_section, "VIB_MOLDEN_NAME", c_val=molden_name)
     525            2 :             IF (unit > 0) THEN
     526              :                WRITE (unit, "(/,T2,A,T9,A)") &
     527            1 :                   "DIMER|", "Initial dimer vector by linear combination of normal modes from:"
     528              :                WRITE (unit, "(T2,A,T9,A)") &
     529            1 :                   "DIMER|", TRIM(ADJUSTL(molden_name))
     530              :             END IF
     531              :             ! Build lists of indices and weights for linear combination
     532              :             CALL vib_get_index_weight(dimer_section, vib_id_list, vib_wt_list, &
     533            2 :                                       vib_list_size, unit)
     534              :             ! Find vibrational modes in molden and do linear combination
     535            4 :             ALLOCATE (array_r(nvec_size))
     536            2 :             CALL parser_create(parser, molden_name, para_env=para_env, apply_preprocessing=.FALSE.)
     537            4 :             Vib_modes: DO i = 1, vib_list_size
     538              :                ! Format below is from molden_utils.F
     539            2 :                WRITE (UNIT=vib_name, FMT='(T2,A,1X,I6)') "vibration", vib_id_list(i)
     540              :                CALL parser_search_string(parser, TRIM(vib_name), &
     541              :                                          ignore_case=.FALSE., found=found, &
     542            2 :                                          begin_line=.TRUE., search_from_begin_of_file=.TRUE.)
     543            2 :                IF (.NOT. found) CALL cp_abort(__LOCATION__, &
     544            0 :                                               "Could not found <"//vib_name//"> from molden file")
     545           12 :                DO j = 1, natom
     546           10 :                   CALL parser_get_next_line(parser, 1)
     547              :                   READ (UNIT=parser%input_line, FMT=*, IOSTAT=ierr) &
     548           10 :                      array_r(3*j - 2), array_r(3*j - 1), array_r(3*j)
     549           12 :                   IF (ierr /= 0) THEN
     550              :                      CALL cp_abort(__LOCATION__, &
     551              :                                    "Error while reading MOLDEN file: cannot parse the line "// &
     552              :                                    TRIM(ADJUSTL(cp_to_string(j)))//" of <"//vib_name//"> "// &
     553            0 :                                    "for components of the normal mode")
     554              :                   END IF
     555              :                END DO
     556           34 :                dimer_env%nvec(:) = dimer_env%nvec(:) + array_r(:)*vib_wt_list(i)
     557              :             END DO Vib_modes
     558            2 :             CALL parser_release(parser)
     559            2 :             IF (ALLOCATED(array_r)) DEALLOCATE (array_r)
     560            2 :             IF (ALLOCATED(vib_id_list)) DEALLOCATE (vib_id_list)
     561            4 :             IF (ALLOCATED(vib_wt_list)) DEALLOCATE (vib_wt_list)
     562              :          CASE DEFAULT
     563           14 :             CPABORT("Invalid or not yet implemented dimer initialization method")
     564              :          END SELECT
     565              :       END IF
     566              : 
     567          110 :    END SUBROUTINE dimer_init_vector
     568              : 
     569              : ! **************************************************************************************************
     570              : !> \brief Read VIB_INDEX and VIB_WEIGHT under DIMER section
     571              : !> \param dimer_section ...
     572              : !> \param vib_id_list ...
     573              : !> \param vib_wt_list ...
     574              : !> \param vib_list_size ...
     575              : !> \param unit ...
     576              : !> \par History
     577              : !>      2026/05  created
     578              : !> \author HE Zilong
     579              : ! **************************************************************************************************
     580            2 :    SUBROUTINE vib_get_index_weight(dimer_section, vib_id_list, vib_wt_list, vib_list_size, unit)
     581              :       TYPE(section_vals_type), INTENT(IN), POINTER       :: dimer_section
     582              :       INTEGER, ALLOCATABLE, DIMENSION(:), INTENT(OUT)    :: vib_id_list
     583              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:), &
     584              :          INTENT(OUT)                                     :: vib_wt_list
     585              :       INTEGER, INTENT(OUT)                               :: vib_list_size
     586              :       INTEGER, INTENT(IN)                                :: unit
     587              : 
     588              :       INTEGER                                            :: i, isize, n_rep_val, vib_id_size, &
     589              :                                                             vib_wt_size
     590            2 :       INTEGER, DIMENSION(:), POINTER                     :: tmpilist
     591            2 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: tmprlist
     592              : 
     593            2 :       vib_id_size = 0
     594            2 :       CALL section_vals_val_get(dimer_section, "VIB_INDEX", n_rep_val=n_rep_val)
     595            4 :       DO i = 1, n_rep_val
     596              :          CALL section_vals_val_get(dimer_section, "VIB_INDEX", &
     597            2 :                                    i_vals=tmpilist, i_rep_val=i)
     598            4 :          vib_id_size = vib_id_size + SIZE(tmpilist)
     599              :       END DO
     600            6 :       ALLOCATE (vib_id_list(vib_id_size))
     601            2 :       isize = 0
     602            4 :       DO i = 1, n_rep_val
     603              :          CALL section_vals_val_get(dimer_section, "VIB_INDEX", &
     604            2 :                                    i_vals=tmpilist, i_rep_val=i)
     605            4 :          vib_id_list(isize + 1:isize + SIZE(tmpilist)) = tmpilist
     606            4 :          isize = isize + SIZE(tmpilist)
     607              :       END DO
     608              : 
     609            2 :       vib_wt_size = 0
     610            2 :       CALL section_vals_val_get(dimer_section, "VIB_WEIGHT", n_rep_val=n_rep_val)
     611            4 :       DO i = 1, n_rep_val
     612              :          CALL section_vals_val_get(dimer_section, "VIB_WEIGHT", &
     613            2 :                                    r_vals=tmprlist, i_rep_val=i)
     614            4 :          vib_wt_size = vib_wt_size + SIZE(tmprlist)
     615              :       END DO
     616            6 :       ALLOCATE (vib_wt_list(vib_wt_size))
     617            2 :       isize = 0
     618            4 :       DO i = 1, n_rep_val
     619              :          CALL section_vals_val_get(dimer_section, "VIB_WEIGHT", &
     620            2 :                                    r_vals=tmprlist, i_rep_val=i)
     621            4 :          vib_wt_list(isize + 1:isize + SIZE(tmprlist)) = tmprlist
     622            4 :          isize = isize + SIZE(tmprlist)
     623              :       END DO
     624              : 
     625            2 :       IF (vib_id_size /= vib_wt_size) THEN
     626              :          CALL cp_abort(__LOCATION__, &
     627              :                        "Inconsistent count of values speficied in input between "// &
     628              :                        "VIB_INDEX ("//TRIM(ADJUSTL(cp_to_string(vib_id_size)))//") "// &
     629            0 :                        "and VIB_WEIGHT ("//TRIM(ADJUSTL(cp_to_string(vib_wt_size)))//")")
     630              :       ELSE
     631            2 :          vib_list_size = vib_id_size
     632              :       END IF
     633            2 :       IF (unit > 0) THEN
     634              :          WRITE (unit, "(/,T2,A,T9,A)") &
     635            1 :             "DIMER|", "Indices and weights of vibrational modes in linear combination"
     636            2 :          DO i = 1, vib_list_size
     637              :             WRITE (unit, "(T2,A,T10,I6,T18,F12.6)") &
     638            2 :                "DIMER|", vib_id_list(i), vib_wt_list(i)
     639              :          END DO
     640              :       END IF
     641              : 
     642            4 :    END SUBROUTINE vib_get_index_weight
     643              : 
     644            0 : END MODULE dimer_types
        

Generated by: LCOV version 2.0-1