LCOV - code coverage report
Current view: top level - src/motion - cell_opt_types.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:21ef868) Lines: 67.2 % 58 39
Test Date: 2026-08-14 07:04:57 Functions: 66.7 % 3 2

            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 type used for a Simulation Cell Optimization
      10              : !> \par History
      11              : !>      none
      12              : !> \author Teodoro Laino - created [tlaino] - 03.2008 - Zurich University
      13              : ! **************************************************************************************************
      14              : MODULE cell_opt_types
      15              : 
      16              :    USE cell_methods,                    ONLY: canonicalize_cell_matrix,&
      17              :                                               cell_create
      18              :    USE cell_opt_utils,                  ONLY: read_external_press_tensor
      19              :    USE cell_types,                      ONLY: cell_clone,&
      20              :                                               cell_release,&
      21              :                                               cell_type
      22              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      23              :                                               cp_logger_type
      24              :    USE cp_output_handling,              ONLY: cp_print_key_finished_output,&
      25              :                                               cp_print_key_unit_nr
      26              :    USE cp_units,                        ONLY: cp_unit_from_cp2k
      27              :    USE force_env_types,                 ONLY: force_env_get,&
      28              :                                               force_env_type
      29              :    USE input_constants,                 ONLY: fix_none,&
      30              :                                               fix_x,&
      31              :                                               fix_xy,&
      32              :                                               fix_xz,&
      33              :                                               fix_y,&
      34              :                                               fix_yz,&
      35              :                                               fix_z
      36              :    USE input_section_types,             ONLY: section_vals_type,&
      37              :                                               section_vals_val_get
      38              :    USE kinds,                           ONLY: dp
      39              : #include "../base/base_uses.f90"
      40              : 
      41              :    IMPLICIT NONE
      42              :    PRIVATE
      43              : 
      44              :    LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .FALSE.
      45              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cell_opt_types'
      46              : 
      47              :    PUBLIC :: cell_opt_env_type, &
      48              :              cell_opt_env_create, &
      49              :              cell_opt_env_release
      50              : 
      51              : ! **************************************************************************************************
      52              : !> \brief Type containing all informations abour the simulation cell optimization
      53              : !> \par History
      54              : !>      none
      55              : !> \author Teodoro Laino - created [tlaino] - 03.2008 - Zurich University
      56              : ! **************************************************************************************************
      57              :    TYPE cell_opt_env_type
      58              :       ! Simulation cell optimization parameters
      59              :       INTEGER                                    :: constraint_id = fix_none
      60              :       LOGICAL                                    :: keep_angles = .FALSE., &
      61              :                                                     keep_symmetry = .FALSE., &
      62              :                                                     keep_volume = .FALSE.
      63              :       REAL(KIND=dp)                              :: pres_ext = 0.0_dp, pres_int = 0.0_dp, pres_tol = 0.0_dp, pres_constr = 0.0_dp
      64              :       REAL(KIND=dp), DIMENSION(3, 3)             :: mtrx = 0.0_dp
      65              :       ! Fixed rotations between the user's Cartesian frame and the canonical
      66              :       ! frame used for the six independent cell optimization variables.
      67              :       REAL(KIND=dp), DIMENSION(3, 3)             :: input_to_opt = 0.0_dp, &
      68              :                                                     opt_to_input = 0.0_dp
      69              :       TYPE(cell_type), POINTER                   :: opt_cell => NULL()
      70              :    END TYPE cell_opt_env_type
      71              : 
      72              : CONTAINS
      73              : 
      74              : ! **************************************************************************************************
      75              : !> \brief ...
      76              : !> \param cell_env ...
      77              : !> \param force_env ...
      78              : !> \param geo_section ...
      79              : !> \par History
      80              : !>      none
      81              : !> \author Teodoro Laino - created [tlaino] - 03.2008 - Zurich University
      82              : ! **************************************************************************************************
      83         8190 :    SUBROUTINE cell_opt_env_create(cell_env, force_env, geo_section)
      84              :       TYPE(cell_opt_env_type), INTENT(OUT)               :: cell_env
      85              :       TYPE(force_env_type), POINTER                      :: force_env
      86              :       TYPE(section_vals_type), POINTER                   :: geo_section
      87              : 
      88              :       CHARACTER(LEN=4)                                   :: label
      89              :       INTEGER                                            :: output_unit, required_constraint_id
      90              :       LOGICAL                                            :: constraint_explicit, valid_constraint
      91              :       TYPE(cell_type), POINTER                           :: cell
      92              :       TYPE(cp_logger_type), POINTER                      :: logger
      93              : 
      94          210 :       NULLIFY (cell_env%opt_cell, cell)
      95          210 :       CALL force_env_get(force_env, cell=cell)
      96          210 :       CALL cell_create(cell_env%opt_cell)
      97          210 :       CALL cell_clone(cell, cell_env%opt_cell, tag="CELL_OPT_CANONICAL")
      98          210 :       CALL canonicalize_cell_matrix(cell_env%opt_cell)
      99              :       ! Keep the physical cell in the input orientation while the optimizer
     100              :       ! works with the canonical cell and its six non-redundant components.
     101        11130 :       cell_env%input_to_opt = MATMUL(cell_env%opt_cell%hmat, cell%h_inv)
     102        11130 :       cell_env%opt_to_input = MATMUL(cell%hmat, cell_env%opt_cell%h_inv)
     103          210 :       CALL section_vals_val_get(geo_section, "KEEP_VOLUME", l_val=cell_env%keep_volume)
     104          210 :       CALL section_vals_val_get(geo_section, "KEEP_ANGLES", l_val=cell_env%keep_angles)
     105          210 :       CALL section_vals_val_get(geo_section, "KEEP_SYMMETRY", l_val=cell_env%keep_symmetry)
     106          210 :       CALL section_vals_val_get(geo_section, "PRESSURE_TOLERANCE", r_val=cell_env%pres_tol)
     107              :       CALL section_vals_val_get(geo_section, "CONSTRAINT", &
     108          210 :                                 i_val=cell_env%constraint_id, explicit=constraint_explicit)
     109          840 :       IF (COUNT(cell%perd /= 0) == 2) THEN
     110            0 :          required_constraint_id = fix_none
     111            0 :          IF (cell%perd(1) == 0) required_constraint_id = fix_x
     112            0 :          IF (cell%perd(2) == 0) required_constraint_id = fix_y
     113            0 :          IF (cell%perd(3) == 0) required_constraint_id = fix_z
     114              : 
     115            0 :          SELECT CASE (required_constraint_id)
     116              :          CASE (fix_x)
     117              :             valid_constraint = cell_env%constraint_id == fix_x .OR. &
     118              :                                cell_env%constraint_id == fix_xy .OR. &
     119            0 :                                cell_env%constraint_id == fix_xz
     120              :          CASE (fix_y)
     121              :             valid_constraint = cell_env%constraint_id == fix_y .OR. &
     122              :                                cell_env%constraint_id == fix_xy .OR. &
     123            0 :                                cell_env%constraint_id == fix_yz
     124              :          CASE (fix_z)
     125              :             valid_constraint = cell_env%constraint_id == fix_z .OR. &
     126              :                                cell_env%constraint_id == fix_xz .OR. &
     127            0 :                                cell_env%constraint_id == fix_yz
     128              :          CASE DEFAULT
     129            0 :             valid_constraint = .FALSE.
     130              :          END SELECT
     131              : 
     132            0 :          IF (cell_env%constraint_id == fix_none .AND. .NOT. constraint_explicit) THEN
     133            0 :             cell_env%constraint_id = required_constraint_id
     134              :             CALL cp_warn(__LOCATION__, &
     135            0 :                          "2D CELL_OPT: constraining non-periodic cell direction.")
     136            0 :          ELSE IF (.NOT. valid_constraint) THEN
     137              :             CALL cp_abort(__LOCATION__, &
     138            0 :                           "2D CELL_OPT needs constrained non-periodic cell direction.")
     139              :          END IF
     140              :       END IF
     141              : 
     142              :       ! Get the external pressure
     143              :       CALL read_external_press_tensor(geo_section, cell_env%opt_cell, cell_env%pres_ext, &
     144          210 :                                       cell_env%mtrx, cell_env%input_to_opt)
     145              : 
     146              :       ! Print cell optimisation setup
     147          210 :       NULLIFY (logger)
     148          210 :       logger => cp_get_default_logger()
     149          210 :       output_unit = cp_print_key_unit_nr(logger, geo_section, "PRINT%CELL", extension=".Log")
     150          210 :       IF (output_unit > 0) THEN
     151              :          WRITE (UNIT=output_unit, FMT="(/,T2,A,T61,F20.1)") &
     152          105 :             "CELL_OPT| Pressure tolerance [bar]: ", cp_unit_from_cp2k(cell_env%pres_tol, "bar")
     153          105 :          IF (cell_env%keep_volume) THEN
     154              :             WRITE (UNIT=output_unit, FMT="(T2,A,T78,A3)") &
     155            3 :                "CELL_OPT| Keep volume of cell: ", "YES"
     156              :          ELSE
     157              :             WRITE (UNIT=output_unit, FMT="(T2,A,T78,A3)") &
     158          102 :                "CELL_OPT| Keep volume of cell: ", " NO"
     159              :          END IF
     160          105 :          IF (cell_env%keep_angles) THEN
     161              :             WRITE (UNIT=output_unit, FMT="(T2,A,T78,A3)") &
     162           17 :                "CELL_OPT| Keep angles between the cell vectors: ", "YES"
     163              :          ELSE
     164              :             WRITE (UNIT=output_unit, FMT="(T2,A,T78,A3)") &
     165           88 :                "CELL_OPT| Keep angles between the cell vectors: ", " NO"
     166              :          END IF
     167          105 :          IF (cell_env%keep_symmetry) THEN
     168              :             WRITE (UNIT=output_unit, FMT="(T2,A,T78,A3)") &
     169           21 :                "CELL_OPT| Keep cell symmetry: ", "YES"
     170              :          ELSE
     171              :             WRITE (UNIT=output_unit, FMT="(T2,A,T78,A3)") &
     172           84 :                "CELL_OPT| Keep cell symmetry: ", " NO"
     173              :          END IF
     174          105 :          SELECT CASE (cell_env%constraint_id)
     175              :          CASE (fix_x)
     176            0 :             label = "   X"
     177              :          CASE (fix_y)
     178            0 :             label = "   Y"
     179              :          CASE (fix_z)
     180            1 :             label = "   Z"
     181              :          CASE (fix_xy)
     182            1 :             label = "  XY"
     183              :          CASE (fix_xz)
     184            0 :             label = "  XZ"
     185              :          CASE (fix_yz)
     186            0 :             label = "  YZ"
     187              :          CASE (fix_none)
     188          105 :             label = "NONE"
     189              :          END SELECT
     190              :          WRITE (UNIT=output_unit, FMT="(T2,A,T77,A4)") &
     191          105 :             "CELL_OPT| Constraint: ", label
     192              :       END IF
     193          210 :       CALL cp_print_key_finished_output(output_unit, logger, geo_section, "PRINT%CELL")
     194              : 
     195          210 :    END SUBROUTINE cell_opt_env_create
     196              : 
     197              : ! **************************************************************************************************
     198              : !> \brief ...
     199              : !> \param cell_env ...
     200              : !> \par History
     201              : !>      none
     202              : !> \author Teodoro Laino - created [tlaino] - 03.2008 - Zurich University
     203              : ! **************************************************************************************************
     204          210 :    SUBROUTINE cell_opt_env_release(cell_env)
     205              :       TYPE(cell_opt_env_type), INTENT(INOUT)             :: cell_env
     206              : 
     207          210 :       CALL cell_release(cell_env%opt_cell)
     208              : 
     209          210 :    END SUBROUTINE cell_opt_env_release
     210              : 
     211            0 : END MODULE cell_opt_types
        

Generated by: LCOV version 2.0-1