LCOV - code coverage report
Current view: top level - src/motion - cell_opt_utils.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 96.7 % 209 202
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 7 7

            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 Parse CELL_OPT constraints, build symmetry constraint rows, and
      10              : !>        compute the cell enthalpy gradient.
      11              : !> \par History
      12              : !>      03.2008 - Teodoro Laino [tlaino] - University of Zurich - Cell Optimization
      13              : ! **************************************************************************************************
      14              : MODULE cell_opt_utils
      15              :    USE cell_manifold_utils,             ONLY: &
      16              :         comp_ax, comp_bx, comp_by, comp_cx, comp_cy, comp_cz, geom_a, geom_alpha, geom_b, &
      17              :         geom_beta, geom_c, geom_gamma, gfix_alpha, gfix_beta, gfix_gamma, mask_a, mask_all, &
      18              :         mask_b, mask_c, mask_x, mask_xy, mask_xz, mask_y, mask_yz, mask_z, min_cell_length, &
      19              :         n_geom, n_slot, slot_ax, slot_bx, slot_by, slot_cx, slot_cy, slot_cz
      20              :    USE cell_types,                      ONLY: &
      21              :         cell_sym_cubic, cell_sym_hexagonal_gamma_120, cell_sym_hexagonal_gamma_60, &
      22              :         cell_sym_monoclinic, cell_sym_monoclinic_gamma_ab, cell_sym_none, cell_sym_orthorhombic, &
      23              :         cell_sym_rhombohedral, cell_sym_tetragonal_ab, cell_sym_tetragonal_ac, &
      24              :         cell_sym_tetragonal_bc, cell_sym_triclinic, cell_type
      25              :    USE input_constants,                 ONLY: fix_none
      26              :    USE input_section_types,             ONLY: section_vals_type,&
      27              :                                               section_vals_val_get
      28              :    USE kinds,                           ONLY: default_string_length,&
      29              :                                               dp
      30              :    USE mathconstants,                   ONLY: pi,&
      31              :                                               sqrt3
      32              :    USE space_groups_types,              ONLY: spgr_type
      33              :    USE string_utilities,                ONLY: uppercase
      34              : #include "../base/base_uses.f90"
      35              : 
      36              :    IMPLICIT NONE
      37              :    PRIVATE
      38              : 
      39              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cell_opt_utils'
      40              : 
      41              :    PUBLIC :: cell_opt_constraint_label, &
      42              :              comp_ax, comp_bx, comp_by, comp_cx, comp_cy, comp_cz, &
      43              :              get_dg_dh, &
      44              :              gfix_alpha, gfix_beta, gfix_gamma, &
      45              :              mask_a, mask_all, mask_b, mask_c, mask_x, mask_xy, mask_xz, mask_y, mask_yz, mask_z, &
      46              :              parse_cell_constraint, &
      47              :              parse_token, &
      48              :              read_external_press_tensor, &
      49              :              spgr_point_group_symmetry_id, &
      50              :              symmetry_constraint_rows
      51              : 
      52              : CONTAINS
      53              : 
      54              : ! **************************************************************************************************
      55              : !> \brief Reads the external pressure tensor
      56              : !> \param geo_section ...
      57              : !> \param cell ...
      58              : !> \param pres_ext ...
      59              : !> \param mtrx ...
      60              : !> \param input_to_opt rotation from the input Cartesian frame to the optimization frame
      61              : !> \author Teodoro Laino [tlaino] - University of Zurich - 03.2008
      62              : ! **************************************************************************************************
      63          246 :    SUBROUTINE read_external_press_tensor(geo_section, cell, pres_ext, mtrx, input_to_opt)
      64              :       TYPE(section_vals_type), POINTER                   :: geo_section
      65              :       TYPE(cell_type), POINTER                           :: cell
      66              :       REAL(KIND=dp), INTENT(OUT)                         :: pres_ext
      67              :       REAL(KIND=dp), DIMENSION(3, 3), INTENT(OUT)        :: mtrx
      68              :       REAL(KIND=dp), DIMENSION(3, 3), INTENT(IN)         :: input_to_opt
      69              : 
      70              :       REAL(KIND=dp), PARAMETER                           :: eps_aniso_press = 1.0E-5_dp
      71              : 
      72              :       INTEGER                                            :: i, ind, j
      73              :       LOGICAL                                            :: check
      74              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: pres_ext_tens
      75          246 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: pvals
      76              : 
      77          246 :       NULLIFY (pvals)
      78          246 :       mtrx = 0.0_dp
      79          246 :       pres_ext_tens = 0.0_dp
      80          246 :       pres_ext = 0.0_dp
      81          246 :       CALL section_vals_val_get(geo_section, "EXTERNAL_PRESSURE", r_vals=pvals)
      82          246 :       check = (SIZE(pvals) == 1) .OR. (SIZE(pvals) == 9)
      83          246 :       IF (.NOT. check) THEN
      84            0 :          CPABORT("EXTERNAL_PRESSURE can have 1 or 9 components only!")
      85              :       END IF
      86              : 
      87          246 :       IF (SIZE(pvals) == 9) THEN
      88              :          ind = 0
      89          504 :          DO i = 1, 3
      90         1638 :             DO j = 1, 3
      91         1134 :                ind = ind + 1
      92         1512 :                pres_ext_tens(j, i) = pvals(ind)
      93              :             END DO
      94              :          END DO
      95              :          ! Express the pressure tensor in the canonical optimization frame.
      96              :          pres_ext_tens = MATMUL(input_to_opt, &
      97         9954 :                                 MATMUL(pres_ext_tens, TRANSPOSE(input_to_opt)))
      98          504 :          DO i = 1, 3
      99          504 :             pres_ext = pres_ext + pres_ext_tens(i, i)
     100              :          END DO
     101          126 :          pres_ext = pres_ext/3.0_dp
     102          504 :          DO i = 1, 3
     103          504 :             pres_ext_tens(i, i) = pres_ext_tens(i, i) - pres_ext
     104              :          END DO
     105              :       ELSE
     106          120 :          pres_ext = pvals(1)
     107              :       END IF
     108              : 
     109         3198 :       IF (ANY(ABS(pres_ext_tens) > eps_aniso_press)) THEN
     110            0 :          mtrx = cell%deth*MATMUL(cell%h_inv, MATMUL(pres_ext_tens, TRANSPOSE(cell%h_inv)))
     111              :       END IF
     112              : 
     113          246 :    END SUBROUTINE read_external_press_tensor
     114              : 
     115              : ! **************************************************************************************************
     116              : !> \brief Parses the tokens of the CONSTRAINT keyword into a cell component mask
     117              : !> \param constraint_tokens value tokens of the CONSTRAINT keyword (char_t list)
     118              : !> \param cell simulation cell (only the periodicity is used)
     119              : !> \param explicit whether the CONSTRAINT keyword was given explicitly
     120              : !> \param mask union of the fixed cell components (comp_* and mask_* bits)
     121              : ! **************************************************************************************************
     122          440 :    SUBROUTINE parse_cell_constraint(constraint_tokens, cell, explicit, mask)
     123              :       CHARACTER(LEN=*), DIMENSION(:), INTENT(IN)         :: constraint_tokens
     124              :       TYPE(cell_type), POINTER                           :: cell
     125              :       LOGICAL, INTENT(IN)                                :: explicit
     126              :       INTEGER, INTENT(OUT)                               :: mask
     127              : 
     128              :       CHARACTER(LEN=default_string_length)               :: reject_reason, token
     129              :       INTEGER                                            :: axis_index, nonperiodic_axis, overlap, &
     130              :                                                             required_mask, tok_index, tok_mask
     131              :       LOGICAL                                            :: accepted
     132              : 
     133              :       ! The input tokenizer delivers a bare keyword as one blank token.
     134          440 :       IF (SIZE(constraint_tokens) == 0 .OR. &
     135          440 :           (SIZE(constraint_tokens) == 1 .AND. LEN_TRIM(constraint_tokens(1)) == 0)) THEN
     136              :          CALL cp_abort(__LOCATION__, &
     137            0 :                        "CELL_OPT CONSTRAINT requires a value.")
     138              :       END IF
     139              : 
     140          440 :       mask = 0
     141         1142 :       DO tok_index = 1, SIZE(constraint_tokens)
     142          702 :          token = ADJUSTL(constraint_tokens(tok_index))
     143          702 :          CALL uppercase(token)
     144          702 :          IF (token == "NONE") THEN
     145              :             ! NONE must be the first and only token: leftover tokens on the
     146              :             ! line or an already-accumulated mask mean it is being combined.
     147          232 :             IF (tok_index < SIZE(constraint_tokens) .OR. mask /= 0) THEN
     148              :                CALL cp_abort(__LOCATION__, &
     149              :                              "CELL_OPT CONSTRAINT NONE cannot be combined "// &
     150            0 :                              "with other tokens.")
     151              :             END IF
     152          232 :             mask = 0
     153          232 :             CYCLE
     154              :          END IF
     155          470 :          CALL parse_token(token, accepted, tok_mask, reject_reason)
     156          470 :          IF (.NOT. accepted) THEN
     157            0 :             CALL cp_abort(__LOCATION__, TRIM(reject_reason))
     158              :          END IF
     159          470 :          overlap = IAND(tok_mask, mask)
     160          470 :          IF (overlap == tok_mask) THEN
     161              :             CALL cp_warn(__LOCATION__, &
     162              :                          "Redundant CELL_OPT CONSTRAINT token: "//TRIM(token)// &
     163              :                          " (already constrained: "// &
     164            2 :                          TRIM(ADJUSTL(cell_opt_constraint_label(overlap)))//").")
     165          468 :          ELSE IF (overlap /= 0) THEN
     166              :             CALL cp_warn(__LOCATION__, &
     167              :                          "CELL_OPT CONSTRAINT token "//TRIM(token)//" partially overlaps: "// &
     168              :                          "already constrained "// &
     169              :                          TRIM(ADJUSTL(cell_opt_constraint_label(overlap)))// &
     170              :                          ", newly fixed "// &
     171            2 :                          TRIM(ADJUSTL(cell_opt_constraint_label(IAND(NOT(mask), tok_mask))))//".")
     172              :          END IF
     173         1380 :          mask = IOR(mask, tok_mask)
     174              :       END DO
     175              : 
     176              :       ! For 2D periodicity the non-periodic cell direction must be constrained.
     177              :       ! Complete the constraint automatically if the keyword was not given.
     178         1760 :       IF (COUNT(cell%perd /= 0) == 2) THEN
     179              :          nonperiodic_axis = 0
     180           80 :          DO axis_index = 1, 3
     181           80 :             IF (cell%perd(axis_index) == 0) nonperiodic_axis = axis_index
     182              :          END DO
     183           20 :          required_mask = 0
     184            6 :          SELECT CASE (nonperiodic_axis)
     185              :          CASE (1)
     186            6 :             required_mask = mask_x
     187              :          CASE (2)
     188            6 :             required_mask = mask_y
     189              :          CASE (3)
     190           20 :             required_mask = mask_z
     191              :          END SELECT
     192           20 :          IF (.NOT. explicit .AND. mask == 0) THEN
     193            8 :             mask = required_mask
     194              :             CALL cp_warn(__LOCATION__, &
     195            8 :                          "2D CELL_OPT: constraining non-periodic cell direction.")
     196           12 :          ELSE IF (IAND(mask, required_mask) /= required_mask) THEN
     197              :             CALL cp_abort(__LOCATION__, &
     198            0 :                           "2D CELL_OPT needs constrained non-periodic cell direction.")
     199              :          END IF
     200              :       END IF
     201              : 
     202          440 :    END SUBROUTINE parse_cell_constraint
     203              : 
     204              : ! **************************************************************************************************
     205              : !> \brief Returns the label of the cell constraint mask
     206              : !> \param mask bit mask of fixed cell components (comp_*, mask_*)
     207              : !> \return 4-character legacy label or space-separated component names
     208              : ! **************************************************************************************************
     209          535 :    PURE FUNCTION cell_opt_constraint_label(mask) RESULT(label)
     210              :       INTEGER, INTENT(IN)                                :: mask
     211              :       CHARACTER(LEN=default_string_length)               :: label
     212              : 
     213          866 :       SELECT CASE (mask)
     214              :       CASE (fix_none)
     215          331 :          label = "NONE"
     216              :       CASE (mask_x)
     217            4 :          label = "   X"
     218              :       CASE (mask_y)
     219            4 :          label = "   Y"
     220              :       CASE (mask_z)
     221           16 :          label = "   Z"
     222              :       CASE (mask_xy)
     223            7 :          label = "  XY"
     224              :       CASE (mask_xz)
     225            4 :          label = "  XZ"
     226              :       CASE (mask_yz)
     227            4 :          label = "  YZ"
     228              :       CASE DEFAULT
     229              :          ! Composite constraint: spell out the fixed components
     230          165 :          label = ""
     231          165 :          IF (IAND(mask, comp_ax) /= 0) label = TRIM(label)//" AX"
     232          165 :          IF (IAND(mask, comp_bx) /= 0) label = TRIM(label)//" BX"
     233          165 :          IF (IAND(mask, comp_by) /= 0) label = TRIM(label)//" BY"
     234          165 :          IF (IAND(mask, comp_cx) /= 0) label = TRIM(label)//" CX"
     235          165 :          IF (IAND(mask, comp_cy) /= 0) label = TRIM(label)//" CY"
     236          165 :          IF (IAND(mask, comp_cz) /= 0) label = TRIM(label)//" CZ"
     237          165 :          IF (IAND(mask, gfix_alpha) /= 0) label = TRIM(label)//" ALPHA"
     238          165 :          IF (IAND(mask, gfix_beta) /= 0) label = TRIM(label)//" BETA"
     239          165 :          IF (IAND(mask, gfix_gamma) /= 0) label = TRIM(label)//" GAMMA"
     240          700 :          label = ADJUSTL(label)
     241              :       END SELECT
     242              : 
     243          535 :    END FUNCTION cell_opt_constraint_label
     244              : 
     245              : ! **************************************************************************************************
     246              : !> \brief Translate a crystal system into cell constraint rows.
     247              : !> \param symmetry_id crystal system of the canonical cell
     248              : !> \param mask comp_* bits for canonical entries fixed to zero by symmetry
     249              : !> \param lin_w linear coupling normals, one normal per column
     250              : !> \param n_lin_rows number of active columns in lin_w
     251              : !> \param eq_pair index pairs [m, n] in 1..6 for geometric equalities, one pair per column
     252              : !> \param n_eq_rows number of active columns in eq_pair
     253              : !> \note Rhombohedral (a = b = c, alpha = beta = gamma) and monoclinic_gamma_ab
     254              : !>       (a = b with free gamma) produce curved geometric equalities. All other
     255              : !>       crystal systems produce constant linear rows with zero curvature.
     256              : ! **************************************************************************************************
     257           92 :    SUBROUTINE symmetry_constraint_rows(symmetry_id, mask, lin_w, n_lin_rows, eq_pair, n_eq_rows)
     258              :       INTEGER, INTENT(IN)                                :: symmetry_id
     259              :       INTEGER, INTENT(OUT)                               :: mask
     260              :       REAL(KIND=dp), DIMENSION(n_slot, n_slot), &
     261              :          INTENT(OUT)                                     :: lin_w
     262              :       INTEGER, INTENT(OUT)                               :: n_lin_rows
     263              :       INTEGER, DIMENSION(2, n_geom), INTENT(OUT)         :: eq_pair
     264              :       INTEGER, INTENT(OUT)                               :: n_eq_rows
     265              : 
     266              :       REAL(KIND=dp), PARAMETER                           :: half_sqrt3 = 0.5_dp*sqrt3
     267              : 
     268           92 :       mask = 0
     269           92 :       lin_w = 0.0_dp
     270           92 :       n_lin_rows = 0
     271           92 :       eq_pair = 0
     272           92 :       n_eq_rows = 0
     273              : 
     274           96 :       SELECT CASE (symmetry_id)
     275              :       CASE (cell_sym_orthorhombic)
     276            4 :          mask = comp_bx + comp_cx + comp_cy
     277              :       CASE (cell_sym_tetragonal_ab)
     278            6 :          mask = comp_bx + comp_cx + comp_cy
     279            6 :          n_lin_rows = 1
     280            6 :          lin_w(slot_ax, 1) = 1.0_dp
     281            6 :          lin_w(slot_by, 1) = -1.0_dp
     282              :       CASE (cell_sym_tetragonal_ac)
     283            4 :          mask = comp_bx + comp_cx + comp_cy
     284            4 :          n_lin_rows = 1
     285            4 :          lin_w(slot_ax, 1) = 1.0_dp
     286            4 :          lin_w(slot_cz, 1) = -1.0_dp
     287              :       CASE (cell_sym_tetragonal_bc)
     288            4 :          mask = comp_bx + comp_cx + comp_cy
     289            4 :          n_lin_rows = 1
     290            4 :          lin_w(slot_by, 1) = 1.0_dp
     291            4 :          lin_w(slot_cz, 1) = -1.0_dp
     292              :       CASE (cell_sym_cubic)
     293            8 :          mask = comp_bx + comp_cx + comp_cy
     294            8 :          n_lin_rows = 2
     295            8 :          lin_w(slot_ax, 1) = 1.0_dp
     296            8 :          lin_w(slot_by, 1) = -1.0_dp
     297            8 :          lin_w(slot_by, 2) = 1.0_dp
     298            8 :          lin_w(slot_cz, 2) = -1.0_dp
     299              :       CASE (cell_sym_hexagonal_gamma_60)
     300           10 :          mask = comp_cx + comp_cy
     301           10 :          n_lin_rows = 2
     302           10 :          lin_w(slot_ax, 1) = -0.5_dp
     303           10 :          lin_w(slot_bx, 1) = 1.0_dp
     304           10 :          lin_w(slot_ax, 2) = -half_sqrt3
     305           10 :          lin_w(slot_by, 2) = 1.0_dp
     306              :       CASE (cell_sym_hexagonal_gamma_120)
     307            6 :          mask = comp_cx + comp_cy
     308            6 :          n_lin_rows = 2
     309            6 :          lin_w(slot_ax, 1) = 0.5_dp
     310            6 :          lin_w(slot_bx, 1) = 1.0_dp
     311            6 :          lin_w(slot_ax, 2) = -half_sqrt3
     312            6 :          lin_w(slot_by, 2) = 1.0_dp
     313              :       CASE (cell_sym_rhombohedral)
     314           14 :          n_eq_rows = 4
     315           42 :          eq_pair(:, 1) = [geom_a, geom_b]
     316           42 :          eq_pair(:, 2) = [geom_b, geom_c]
     317           42 :          eq_pair(:, 3) = [geom_alpha, geom_beta]
     318           42 :          eq_pair(:, 4) = [geom_beta, geom_gamma]
     319              :       CASE (cell_sym_monoclinic)
     320           18 :          mask = comp_bx + comp_cy
     321              :       CASE (cell_sym_monoclinic_gamma_ab)
     322           10 :          mask = comp_cx + comp_cy
     323           10 :          n_eq_rows = 1
     324           30 :          eq_pair(:, 1) = [geom_a, geom_b]
     325              :       CASE (cell_sym_none, cell_sym_triclinic)
     326            0 :          CONTINUE
     327              :       CASE DEFAULT
     328           92 :          CPABORT("Unknown cell symmetry_id")
     329              :       END SELECT
     330              : 
     331           92 :    END SUBROUTINE symmetry_constraint_rows
     332              : 
     333              : ! **************************************************************************************************
     334              : !> \brief Identify the canonical crystal system preserved by a space group's point group.
     335              : !> \param spgr space group with integer lattice rotations in rotations_subset
     336              : !> \param cell symmetric start cell in the canonical frame
     337              : !> \return symmetry_id of the invariant metric form, or cell_sym_triclinic when
     338              : !>         the setting adds no canonical cell constraints
     339              : !> \note Averaging the metric G = hmat^T hmat over the point group produces the
     340              : !>       invariant metric form {G : R G R^T = G for all R}. Classifying the lengths
     341              : !>       and angles of this averaged metric maps the point group to the row sets of
     342              : !>       symmetry_constraint_rows. Non-canonical settings (such as a monoclinic
     343              : !>       unique axis along a or c) return cell_sym_triclinic and add zero cell rows.
     344              : ! **************************************************************************************************
     345           44 :    FUNCTION spgr_point_group_symmetry_id(spgr, cell) RESULT(symmetry_id)
     346              :       TYPE(spgr_type), INTENT(IN), POINTER               :: spgr
     347              :       TYPE(cell_type), POINTER                           :: cell
     348              :       INTEGER                                            :: symmetry_id
     349              : 
     350              :       REAL(KIND=dp), PARAMETER :: ang_120 = 2.0_dp*pi/3.0_dp, ang_60 = pi/3.0_dp, &
     351              :          ang_90 = 0.5_dp*pi, eq_angle_tol = 1.0E-6_dp, rel_len_tol = 1.0E-6_dp, &
     352              :          special_ang_tol = 1.0E-5_dp
     353              : 
     354              :       INTEGER                                            :: ir, n90, nop
     355              :       REAL(KIND=dp)                                      :: ang(3), gsym(3, 3), len_tol, lens(3), &
     356              :                                                             metric(3, 3), rot(3, 3)
     357              : 
     358           44 :       symmetry_id = cell_sym_triclinic
     359           44 :       nop = spgr%n_operations_subset
     360           44 :       IF (nop == 0) RETURN
     361              : 
     362         1680 :       metric = MATMUL(TRANSPOSE(cell%hmat), cell%hmat)
     363           42 :       gsym = 0.0_dp
     364          494 :       DO ir = 1, nop
     365              :          ! rotations_subset stores R_frac^T (r_out = rot^T r_in in spgr_apply_rotations_coord),
     366              :          ! so R_frac^T G R_frac takes the form rot * metric * rot^T.
     367         5876 :          rot = REAL(spgr%rotations_subset(:, :, ir), dp)
     368        41174 :          gsym = gsym + MATMUL(rot, MATMUL(metric, TRANSPOSE(rot)))
     369              :       END DO
     370          546 :       gsym = gsym/REAL(nop, dp)
     371              : 
     372          168 :       lens = [SQRT(gsym(1, 1)), SQRT(gsym(2, 2)), SQRT(gsym(3, 3))]
     373          168 :       IF (MINVAL(lens) < min_cell_length) RETURN
     374          160 :       len_tol = rel_len_tol*MAXVAL(lens)
     375           40 :       ang(1) = ACOS(MAX(-1.0_dp, MIN(1.0_dp, gsym(2, 3)/(lens(2)*lens(3)))))
     376           40 :       ang(2) = ACOS(MAX(-1.0_dp, MIN(1.0_dp, gsym(1, 3)/(lens(1)*lens(3)))))
     377           40 :       ang(3) = ACOS(MAX(-1.0_dp, MIN(1.0_dp, gsym(1, 2)/(lens(1)*lens(2)))))
     378          160 :       n90 = COUNT(ABS(ang - ang_90) < special_ang_tol)
     379              : 
     380           40 :       IF (n90 == 3) THEN
     381           12 :          IF (ABS(lens(1) - lens(2)) < len_tol .AND. ABS(lens(2) - lens(3)) < len_tol) THEN
     382              :             symmetry_id = cell_sym_cubic
     383            8 :          ELSE IF (ABS(lens(1) - lens(2)) < len_tol) THEN
     384              :             symmetry_id = cell_sym_tetragonal_ab
     385            6 :          ELSE IF (ABS(lens(1) - lens(3)) < len_tol) THEN
     386              :             symmetry_id = cell_sym_tetragonal_ac
     387            4 :          ELSE IF (ABS(lens(2) - lens(3)) < len_tol) THEN
     388              :             symmetry_id = cell_sym_tetragonal_bc
     389              :          ELSE
     390            2 :             symmetry_id = cell_sym_orthorhombic
     391              :          END IF
     392           28 :       ELSE IF (n90 == 2) THEN
     393           18 :          IF (ABS(ang(2) - ang_90) >= special_ang_tol) THEN
     394              :             ! CP2K monoclinic convention places the unique axis along b (beta /= 90 deg).
     395              :             symmetry_id = cell_sym_monoclinic
     396           10 :          ELSE IF (ABS(ang(3) - ang_90) >= special_ang_tol .AND. &
     397              :                   ABS(lens(1) - lens(2)) < len_tol) THEN
     398            8 :             IF (ABS(ang(3) - ang_120) < special_ang_tol) THEN
     399              :                symmetry_id = cell_sym_hexagonal_gamma_120
     400            4 :             ELSE IF (ABS(ang(3) - ang_60) < special_ang_tol) THEN
     401              :                symmetry_id = cell_sym_hexagonal_gamma_60
     402              :             ELSE
     403            2 :                symmetry_id = cell_sym_monoclinic_gamma_ab
     404              :             END IF
     405              :          END IF
     406              :       ELSE
     407              :          IF (ABS(ang(1) - ang(2)) < eq_angle_tol .AND. ABS(ang(2) - ang(3)) < eq_angle_tol .AND. &
     408              :              ABS(ang(1) - ang_90) >= special_ang_tol .AND. &
     409           10 :              ABS(lens(1) - lens(2)) < len_tol .AND. ABS(lens(2) - lens(3)) < len_tol) THEN
     410            8 :             symmetry_id = cell_sym_rhombohedral
     411              :          END IF
     412              :       END IF
     413              : 
     414              :    END FUNCTION spgr_point_group_symmetry_id
     415              : 
     416              : ! **************************************************************************************************
     417              : !> \brief Classify one CONSTRAINT token without aborting (decision half of the parser)
     418              : !> \param token one upper-case token of the CONSTRAINT value line
     419              : !> \param accepted .TRUE. when the token contributes a mask
     420              : !> \param token_mask the constraint mask contributed by an accepted token
     421              : !> \param reject_reason empty for accepted tokens, or the abort message otherwise
     422              : !> \note parse_cell_constraint applies this classification and turns a rejection
     423              : !>       into a cp_abort. Keeping the decision here keeps it unit-testable:
     424              : !>       the unit test checks the classification table without triggering aborts.
     425              : ! **************************************************************************************************
     426          478 :    PURE SUBROUTINE parse_token(token, accepted, token_mask, reject_reason)
     427              :       CHARACTER(LEN=*), INTENT(IN)                       :: token
     428              :       LOGICAL, INTENT(OUT)                               :: accepted
     429              :       INTEGER, INTENT(OUT)                               :: token_mask
     430              :       CHARACTER(LEN=default_string_length), INTENT(OUT)  :: reject_reason
     431              : 
     432          478 :       accepted = .TRUE.
     433          478 :       token_mask = 0
     434          478 :       reject_reason = ""
     435           86 :       SELECT CASE (token)
     436              :       CASE ("A", "AX")
     437           86 :          token_mask = mask_a
     438              :       CASE ("B")
     439           22 :          token_mask = mask_b
     440              :       CASE ("BX")
     441           56 :          token_mask = comp_bx
     442              :       CASE ("BY")
     443           66 :          token_mask = comp_by
     444              :       CASE ("C", "Z")
     445           24 :          token_mask = mask_c
     446              :       CASE ("CX")
     447           56 :          token_mask = comp_cx
     448              :       CASE ("CY")
     449           56 :          token_mask = comp_cy
     450              :       CASE ("CZ")
     451           64 :          token_mask = comp_cz
     452              :       CASE ("X")
     453            8 :          token_mask = mask_x
     454              :       CASE ("Y")
     455            6 :          token_mask = mask_y
     456              :       CASE ("XY")
     457            6 :          token_mask = mask_xy
     458              :       CASE ("XZ")
     459            4 :          token_mask = mask_xz
     460              :       CASE ("YZ")
     461            4 :          token_mask = mask_yz
     462              :       CASE ("ALPHA")
     463            4 :          token_mask = gfix_alpha
     464              :       CASE ("BETA")
     465            4 :          token_mask = gfix_beta
     466              :       CASE ("GAMMA")
     467            4 :          token_mask = gfix_gamma
     468              :       CASE ("AY", "AZ", "BZ")
     469            6 :          accepted = .FALSE.
     470              :          reject_reason = "CELL_OPT CONSTRAINT token "//TRIM(token)//" is always zero in "// &
     471              :                          "the cell convention (a lies along x, b lies in the xy plane) "// &
     472            6 :                          "and cannot be constrained."
     473              :       CASE DEFAULT
     474            2 :          accepted = .FALSE.
     475              :          reject_reason = "Unknown CELL_OPT CONSTRAINT token: "//TRIM(token)// &
     476              :                          ". Accepted tokens: NONE, the directions X, Y, Z,"// &
     477              :                          " XY, XZ, YZ, the vectors A, B, C, the components"// &
     478          478 :                          " AX, BX, BY, CX, CY, CZ, and the angles ALPHA, BETA, GAMMA."
     479              :       END SELECT
     480          478 :    END SUBROUTINE parse_token
     481              : 
     482              : ! **************************************************************************************************
     483              : !> \brief Computes the derivatives for the cell
     484              : !> \param gradient enthalpy gradient +d(E + pres_ext*V)/dq in the six canonical components
     485              : !> \param av_ptens ...
     486              : !> \param pres_ext ...
     487              : !> \param cell ...
     488              : !> \param mtrx ...
     489              : !> \param pres_int ...
     490              : !> \author Teodoro Laino [tlaino] - University of Zurich - 03.2008
     491              : ! **************************************************************************************************
     492         5989 :    SUBROUTINE get_dg_dh(gradient, av_ptens, pres_ext, cell, mtrx, pres_int)
     493              : 
     494              :       REAL(KIND=dp), DIMENSION(n_slot), INTENT(OUT)      :: gradient
     495              :       REAL(KIND=dp), DIMENSION(3, 3), INTENT(IN)         :: av_ptens
     496              :       REAL(KIND=dp), INTENT(IN)                          :: pres_ext
     497              :       TYPE(cell_type), POINTER                           :: cell
     498              :       REAL(KIND=dp), DIMENSION(3, 3), INTENT(IN)         :: mtrx
     499              :       REAL(KIND=dp), INTENT(OUT)                         :: pres_int
     500              : 
     501              :       INTEGER                                            :: i
     502              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: correction, pten_hinv_old, ptens
     503              : 
     504         5989 :       gradient = 0.0_dp
     505              : 
     506         5989 :       ptens = av_ptens
     507              : 
     508              :       ! Evaluating the internal pressure
     509         5989 :       pres_int = 0.0_dp
     510        23956 :       DO i = 1, 3
     511        23956 :          pres_int = pres_int + ptens(i, i)
     512              :       END DO
     513         5989 :       pres_int = pres_int/3.0_dp
     514              : 
     515         5989 :       ptens(1, 1) = av_ptens(1, 1) - pres_ext
     516         5989 :       ptens(2, 2) = av_ptens(2, 2) - pres_ext
     517         5989 :       ptens(3, 3) = av_ptens(3, 3) - pres_ext
     518              : 
     519       311428 :       pten_hinv_old = cell%deth*MATMUL(cell%h_inv, ptens)
     520       239560 :       correction = MATMUL(mtrx, cell%hmat)
     521              : 
     522         5989 :       gradient(slot_ax) = pten_hinv_old(1, 1) - correction(1, 1)
     523         5989 :       gradient(slot_bx) = pten_hinv_old(2, 1) - correction(2, 1)
     524         5989 :       gradient(slot_by) = pten_hinv_old(2, 2) - correction(2, 2)
     525         5989 :       gradient(slot_cx) = pten_hinv_old(3, 1) - correction(3, 1)
     526         5989 :       gradient(slot_cy) = pten_hinv_old(3, 2) - correction(3, 2)
     527         5989 :       gradient(slot_cz) = pten_hinv_old(3, 3) - correction(3, 3)
     528              : 
     529        41923 :       gradient = -gradient
     530              : 
     531         5989 :    END SUBROUTINE get_dg_dh
     532              : 
     533              : END MODULE cell_opt_utils
        

Generated by: LCOV version 2.0-1