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 Rotation-free atom and cell coordinates for solid-state transition searches
10 : ! **************************************************************************************************
11 : MODULE solid_state_coordinates
12 : USE kinds, ONLY: dp
13 : USE mathlib, ONLY: det_3x3,&
14 : inv_3x3
15 : #include "../base/base_uses.f90"
16 :
17 : IMPLICIT NONE
18 : PRIVATE
19 :
20 : INTEGER, PARAMETER, PUBLIC :: ss_cell_dof = 6
21 : INTEGER, PARAMETER :: row(ss_cell_dof) = [1, 2, 3, 1, 1, 2], col(ss_cell_dof) = [1, 2, 3, 2, 3, 3]
22 :
23 : TYPE, PUBLIC :: solid_state_frame_type
24 : REAL(KIND=dp) :: rotation(3, 3) = 0.0_dp, reference(3, 3) = 0.0_dp, width = 0.0_dp
25 : END TYPE solid_state_frame_type
26 :
27 : PUBLIC :: solid_state_frame, solid_state_pack, solid_state_unpack, solid_state_force
28 :
29 : CONTAINS
30 :
31 : ! **************************************************************************************************
32 : !> \brief Fix the orientation and length scale from the reference cell (columns are cell vectors).
33 : !> \param frame reference coordinate frame
34 : !> \param h reference cell matrix
35 : !> \param natom number of atoms
36 : !> \param weight dimensionless scale for cell motion
37 : ! **************************************************************************************************
38 250 : SUBROUTINE solid_state_frame(frame, h, natom, weight)
39 : TYPE(solid_state_frame_type), INTENT(OUT) :: frame
40 : REAL(KIND=dp), INTENT(IN) :: h(3, 3)
41 : INTEGER, INTENT(IN) :: natom
42 : REAL(KIND=dp), INTENT(IN) :: weight
43 :
44 : INTEGER :: i, j
45 : REAL(KIND=dp) :: volume
46 :
47 10 : volume = det_3x3(h)
48 10 : IF (volume <= 0.0_dp) CALL cp_abort(__LOCATION__, &
49 0 : "Solid-state transition searches require right-handed cells.")
50 10 : CPASSERT(natom > 0 .AND. weight > 0.0_dp)
51 130 : frame%reference = 0.0_dp
52 40 : DO j = 1, 3
53 120 : frame%rotation(:, j) = h(:, j)
54 60 : DO i = 1, j - 1
55 120 : frame%reference(i, j) = DOT_PRODUCT(frame%rotation(:, i), frame%rotation(:, j))
56 150 : frame%rotation(:, j) = frame%rotation(:, j) - frame%reference(i, j)*frame%rotation(:, i)
57 : END DO
58 120 : frame%reference(j, j) = NORM2(frame%rotation(:, j))
59 30 : IF (frame%reference(j, j) <= 1.0E-10_dp) THEN
60 0 : CPABORT("Degenerate solid-state reference cell.")
61 : END IF
62 130 : frame%rotation(:, j) = frame%rotation(:, j)/frame%reference(j, j)
63 : END DO
64 10 : frame%width = weight*SQRT(REAL(natom, dp))*(volume/natom)**(1.0_dp/3.0_dp)
65 10 : END SUBROUTINE solid_state_frame
66 :
67 : ! **************************************************************************************************
68 : !> \brief Convert a cell and Cartesian positions to atom and cell coordinates in length units.
69 : !> \param frame reference coordinate frame
70 : !> \param h cell matrix
71 : !> \param r Cartesian positions, flattened by atom
72 : !> \param x joint coordinates: reference Cartesian positions and scaled cell deformation
73 : ! **************************************************************************************************
74 10 : SUBROUTINE solid_state_pack(frame, h, r, x)
75 : TYPE(solid_state_frame_type), INTENT(IN) :: frame
76 : REAL(KIND=dp), INTENT(IN) :: h(3, 3), r(:)
77 : REAL(KIND=dp), INTENT(OUT) :: x(:)
78 :
79 : INTEGER :: i, n
80 : REAL(KIND=dp) :: deformation(3, 3), inverse(3, 3)
81 :
82 10 : n = SIZE(r)
83 10 : CPASSERT(MOD(n, 3) == 0 .AND. SIZE(x) == n + ss_cell_dof)
84 1350 : deformation = MATMUL(TRANSPOSE(frame%rotation), MATMUL(h, inv_3x3(frame%reference)))
85 130 : IF (MAX(ABS(deformation(2, 1)), ABS(deformation(3, 1)), ABS(deformation(3, 2))) > &
86 : 1.0E-8_dp*MAX(1.0_dp, MAXVAL(ABS(deformation)))) THEN
87 : CALL cp_abort(__LOCATION__, &
88 0 : "Solid-state replicas must have a common cell orientation and lattice correspondence.")
89 : END IF
90 40 : DO i = 1, 3
91 40 : IF (deformation(i, i) <= 0.0_dp) THEN
92 0 : CPABORT("Solid-state cell vectors must point consistently.")
93 : END IF
94 : END DO
95 670 : inverse = MATMUL(frame%reference, inv_3x3(h))
96 28 : DO i = 1, n, 3
97 298 : x(i:i + 2) = MATMUL(inverse, r(i:i + 2))
98 : END DO
99 70 : DO i = 1, ss_cell_dof
100 70 : x(n + i) = frame%width*deformation(row(i), col(i))
101 : END DO
102 40 : DO i = 1, 3
103 40 : x(n + i) = frame%width*LOG(deformation(i, i))
104 : END DO
105 10 : END SUBROUTINE solid_state_pack
106 :
107 : ! **************************************************************************************************
108 : !> \brief Recover the cell and Cartesian positions without changing the reference orientation.
109 : !> \param frame reference coordinate frame
110 : !> \param x joint coordinates in length units
111 : !> \param h cell matrix
112 : !> \param r Cartesian positions, flattened by atom
113 : ! **************************************************************************************************
114 214 : SUBROUTINE solid_state_unpack(frame, x, h, r)
115 : TYPE(solid_state_frame_type), INTENT(IN) :: frame
116 : REAL(KIND=dp), INTENT(IN) :: x(:)
117 : REAL(KIND=dp), INTENT(OUT) :: h(3, 3), r(:)
118 :
119 : INTEGER :: i, n
120 : REAL(KIND=dp) :: transform(3, 3)
121 :
122 214 : n = SIZE(r)
123 214 : CPASSERT(MOD(n, 3) == 0 .AND. SIZE(x) == n + ss_cell_dof)
124 8560 : transform = MATMUL(frame%rotation, cell_deformation(frame, x(n + 1:)))
125 8560 : h = MATMUL(transform, frame%reference)
126 602 : DO i = 1, n, 3
127 6422 : r(i:i + 2) = MATMUL(transform, x(i:i + 2))
128 : END DO
129 214 : END SUBROUTINE solid_state_unpack
130 :
131 : ! **************************************************************************************************
132 : !> \brief Pull back minus the derivative of E+pV to the joint coordinates.
133 : !> \param frame reference coordinate frame
134 : !> \param x joint coordinates in length units
135 : !> \param f Cartesian forces, flattened by atom
136 : !> \param virial configurational virial, including the affine motion at fixed fractional coordinates
137 : !> \param pv external hydrostatic pressure times current volume
138 : !> \param force force in the joint coordinates
139 : ! **************************************************************************************************
140 72 : SUBROUTINE solid_state_force(frame, x, f, virial, pv, force)
141 : TYPE(solid_state_frame_type), INTENT(IN) :: frame
142 : REAL(KIND=dp), INTENT(IN) :: x(:), f(:), virial(3, 3), pv
143 : REAL(KIND=dp), INTENT(OUT) :: force(:)
144 :
145 : INTEGER :: i, n
146 : REAL(KIND=dp) :: deformation(3, 3), transform(3, 3), &
147 : w(3, 3)
148 :
149 72 : n = SIZE(f)
150 72 : CPASSERT(MOD(n, 3) == 0 .AND. SIZE(x) == n + ss_cell_dof)
151 72 : CPASSERT(SIZE(force) == SIZE(x))
152 72 : deformation = cell_deformation(frame, x(n + 1:))
153 2880 : transform = MATMUL(frame%rotation, deformation)
154 212 : DO i = 1, n, 3
155 212 : force(i:i + 2) = MATMUL(TRANSPOSE(transform), f(i:i + 2))
156 : END DO
157 : ! The atomic coordinates are proportional to fractional coordinates. The
158 : ! virial already includes their affine motion. Do not add f*r a second time.
159 5688 : w = MATMUL(TRANSPOSE(frame%rotation), MATMUL(virial, frame%rotation))
160 288 : DO i = 1, 3
161 288 : w(i, i) = w(i, i) - pv
162 : END DO
163 4608 : w = MATMUL(w, TRANSPOSE(inv_3x3(deformation)))/frame%width
164 504 : DO i = 1, ss_cell_dof
165 504 : force(n + i) = w(row(i), col(i))
166 : END DO
167 288 : DO i = 1, 3
168 288 : force(n + i) = force(n + i)*deformation(i, i)
169 : END DO
170 72 : END SUBROUTINE solid_state_force
171 :
172 : ! **************************************************************************************************
173 : !> \brief Construct the upper-triangular deformation from its six coordinates.
174 : !> \param frame reference coordinate frame
175 : !> \param q scaled cell deformation coordinates
176 : !> \return the deformation matrix
177 : ! **************************************************************************************************
178 286 : FUNCTION cell_deformation(frame, q) RESULT(deformation)
179 : TYPE(solid_state_frame_type), INTENT(IN) :: frame
180 : REAL(KIND=dp), INTENT(IN) :: q(ss_cell_dof)
181 : REAL(KIND=dp) :: deformation(3, 3)
182 :
183 : INTEGER :: i
184 :
185 2002 : IF (ANY(ABS(q) > 30.0_dp*frame%width)) THEN
186 : CALL cp_abort(__LOCATION__, &
187 0 : "Solid-state cell step too large. Reduce the optimization step size.")
188 : END IF
189 3718 : deformation = 0.0_dp
190 2002 : DO i = 1, ss_cell_dof
191 2002 : deformation(row(i), col(i)) = q(i)/frame%width
192 : END DO
193 1144 : DO i = 1, 3
194 1144 : deformation(i, i) = EXP(deformation(i, i))
195 : END DO
196 286 : END FUNCTION cell_deformation
197 296 : END MODULE solid_state_coordinates
|