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_manifold_utils, ONLY: &
17 : cell_manifold_init, cell_manifold_is_redundant, cell_manifold_pack_hmat, &
18 : cell_manifold_pin_semidefinite, cell_manifold_status_label, cell_manifold_type, comp_bx, &
19 : comp_cx, comp_cy, gfix_all, gfix_alpha, gfix_beta, gfix_gamma, gfix_v, info_licq, info_ok, &
20 : mask_all, n_constraint_bits, n_geom, n_slot
21 : USE cell_methods, ONLY: canonicalize_cell_matrix,&
22 : cell_create
23 : USE cell_opt_utils, ONLY: cell_opt_constraint_label,&
24 : parse_cell_constraint,&
25 : read_external_press_tensor,&
26 : symmetry_constraint_rows
27 : USE cell_types, ONLY: cell_clone,&
28 : cell_release,&
29 : cell_sym_none,&
30 : cell_type
31 : USE cp_log_handling, ONLY: cp_get_default_logger,&
32 : cp_logger_type
33 : USE cp_output_handling, ONLY: cp_print_key_finished_output,&
34 : cp_print_key_unit_nr
35 : USE cp_units, ONLY: cp_unit_from_cp2k
36 : USE force_env_types, ONLY: force_env_get,&
37 : force_env_type
38 : USE input_constants, ONLY: fix_none
39 : USE input_section_types, ONLY: section_vals_type,&
40 : section_vals_val_get,&
41 : section_vals_val_set
42 : USE kinds, ONLY: default_string_length,&
43 : dp
44 : USE string_utilities, ONLY: uppercase
45 : #include "../base/base_uses.f90"
46 :
47 : IMPLICIT NONE
48 : PRIVATE
49 :
50 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cell_opt_types'
51 : REAL(KIND=dp), PARAMETER, PRIVATE :: canonical_frame_warn_tol = 1.0E-6_dp
52 :
53 : PUBLIC :: cell_opt_env_type, &
54 : cell_opt_env_create, &
55 : cell_opt_env_release, &
56 : cell_opt_setup_manifold, &
57 : cell_opt_fold_symmetry_rows
58 :
59 : ! **************************************************************************************************
60 : !> \brief Type containing all informations abour the simulation cell optimization
61 : !> \par History
62 : !> none
63 : !> \author Teodoro Laino - created [tlaino] - 03.2008 - Zurich University
64 : ! **************************************************************************************************
65 : TYPE cell_opt_env_type
66 : ! Simulation cell optimization parameters
67 : ! Bit mask of cell components and angles from CONSTRAINT (comp_* and gfix_*, 0 = fix_none).
68 : INTEGER :: constraint_id = fix_none
69 : LOGICAL :: keep_angles = .FALSE., &
70 : keep_symmetry = .FALSE., &
71 : keep_volume = .FALSE.
72 : REAL(KIND=dp) :: pres_ext = 0.0_dp, pres_int = 0.0_dp, pres_tol = 0.0_dp, pres_constr = 0.0_dp
73 : REAL(KIND=dp), DIMENSION(3, 3) :: mtrx = 0.0_dp
74 : ! Fixed rotations between the user's Cartesian frame and the canonical
75 : ! frame used for the six independent cell optimization variables.
76 : REAL(KIND=dp), DIMENSION(3, 3) :: input_to_opt = 0.0_dp, &
77 : opt_to_input = 0.0_dp
78 : TYPE(cell_type), POINTER :: opt_cell => NULL()
79 : ! Active cell constraints and reduced-coordinate chart.
80 : TYPE(cell_manifold_type) :: manifold
81 : END TYPE cell_opt_env_type
82 :
83 : CONTAINS
84 :
85 : ! **************************************************************************************************
86 : !> \brief ...
87 : !> \param cell_env ...
88 : !> \param force_env ...
89 : !> \param geo_section ...
90 : !> \par History
91 : !> none
92 : !> \author Teodoro Laino - created [tlaino] - 03.2008 - Zurich University
93 : ! **************************************************************************************************
94 53136 : SUBROUTINE cell_opt_env_create(cell_env, force_env, geo_section)
95 : TYPE(cell_opt_env_type), INTENT(OUT) :: cell_env
96 : TYPE(force_env_type), POINTER :: force_env
97 : TYPE(section_vals_type), POINTER :: geo_section
98 :
99 : CHARACTER(LEN=default_string_length) :: label
100 : CHARACTER(LEN=default_string_length), &
101 246 : DIMENSION(:), POINTER :: canonical_tokens, constraint_tokens
102 : INTEGER :: i, output_unit
103 : LOGICAL :: constraint_explicit, &
104 : has_frame_dep_constraint
105 : TYPE(cell_type), POINTER :: cell
106 : TYPE(cp_logger_type), POINTER :: logger
107 :
108 246 : NULLIFY (cell_env%opt_cell, cell, canonical_tokens, constraint_tokens)
109 246 : CALL force_env_get(force_env, cell=cell)
110 246 : CALL cell_create(cell_env%opt_cell)
111 246 : CALL cell_clone(cell, cell_env%opt_cell, tag="CELL_OPT_CANONICAL")
112 246 : CALL canonicalize_cell_matrix(cell_env%opt_cell)
113 : ! Keep the physical cell in the input orientation while the optimizer
114 : ! works with the canonical cell and its six non-redundant components.
115 12792 : cell_env%input_to_opt = MATMUL(cell_env%opt_cell%hmat, cell%h_inv)
116 12792 : cell_env%opt_to_input = MATMUL(cell%hmat, cell_env%opt_cell%h_inv)
117 246 : CALL section_vals_val_get(geo_section, "KEEP_VOLUME", l_val=cell_env%keep_volume)
118 246 : CALL section_vals_val_get(geo_section, "KEEP_ANGLES", l_val=cell_env%keep_angles)
119 246 : CALL section_vals_val_get(geo_section, "KEEP_SYMMETRY", l_val=cell_env%keep_symmetry)
120 246 : CALL section_vals_val_get(geo_section, "PRESSURE_TOLERANCE", r_val=cell_env%pres_tol)
121 : CALL section_vals_val_get(geo_section, "CONSTRAINT", &
122 246 : c_vals=constraint_tokens, explicit=constraint_explicit)
123 : CALL parse_cell_constraint(constraint_tokens, cell, constraint_explicit, &
124 246 : cell_env%constraint_id)
125 :
126 246 : IF (cell_env%keep_symmetry .AND. cell_env%opt_cell%symmetry_id == cell_sym_none) THEN
127 : ! &SUBSYS/&CELL SYMMETRY defaults to NONE and is never auto-detected.
128 : CALL cp_warn(__LOCATION__, &
129 : "CELL_OPT: KEEP_SYMMETRY is active, but &SUBSYS/&CELL SYMMETRY "// &
130 : "is NONE. CELL_OPT applies no symmetry constraint. Set "// &
131 2 : "&SUBSYS/&CELL SYMMETRY to specify the crystal system.")
132 : END IF
133 :
134 : has_frame_dep_constraint = (.NOT. constraint_explicit .AND. &
135 246 : cell_env%constraint_id /= fix_none)
136 : ! Only for explicitly set values: an unset keyword must stay unparsed so
137 : ! the restart echo keeps omitting the line, as it always has.
138 246 : IF (constraint_explicit) THEN
139 114 : ALLOCATE (canonical_tokens(SIZE(constraint_tokens)))
140 138 : canonical_tokens = constraint_tokens
141 88 : CALL uppercase(canonical_tokens)
142 88 : DO i = 1, SIZE(canonical_tokens)
143 138 : SELECT CASE (TRIM(ADJUSTL(canonical_tokens(i))))
144 : CASE ("NONE", "A", "B", "C", "ALPHA", "BETA", "GAMMA")
145 16 : CONTINUE
146 : CASE DEFAULT
147 100 : has_frame_dep_constraint = .TRUE.
148 : END SELECT
149 : END DO
150 38 : CALL section_vals_val_set(geo_section, "CONSTRAINT", c_vals_ptr=canonical_tokens)
151 : END IF
152 :
153 6396 : IF (has_frame_dep_constraint .AND. &
154 : MAXVAL(ABS(cell%hmat - cell_env%opt_cell%hmat)) > &
155 : canonical_frame_warn_tol*MAX(1.0_dp, MAXVAL(ABS(cell%hmat)))) THEN
156 : CALL cp_warn(__LOCATION__, &
157 : "CELL_OPT: Axis and component constraints act on the canonical "// &
158 0 : "upper-triangular cell, not on the rotated input axes.")
159 : END IF
160 :
161 : ! Get the external pressure
162 : CALL read_external_press_tensor(geo_section, cell_env%opt_cell, cell_env%pres_ext, &
163 246 : cell_env%mtrx, cell_env%input_to_opt)
164 :
165 : ! Print cell optimisation setup
166 246 : NULLIFY (logger)
167 246 : logger => cp_get_default_logger()
168 246 : output_unit = cp_print_key_unit_nr(logger, geo_section, "PRINT%CELL", extension=".Log")
169 246 : IF (output_unit > 0) THEN
170 : WRITE (UNIT=output_unit, FMT="(/,T2,A,T61,F20.1)") &
171 123 : "CELL_OPT| Pressure tolerance [bar]: ", cp_unit_from_cp2k(cell_env%pres_tol, "bar")
172 123 : IF (cell_env%keep_volume) THEN
173 : WRITE (UNIT=output_unit, FMT="(T2,A,T78,A3)") &
174 5 : "CELL_OPT| Keep volume of cell: ", "YES"
175 : ELSE
176 : WRITE (UNIT=output_unit, FMT="(T2,A,T78,A3)") &
177 118 : "CELL_OPT| Keep volume of cell: ", " NO"
178 : END IF
179 123 : IF (cell_env%keep_angles) THEN
180 : WRITE (UNIT=output_unit, FMT="(T2,A,T78,A3)") &
181 24 : "CELL_OPT| Keep angles between the cell vectors: ", "YES"
182 : ELSE
183 : WRITE (UNIT=output_unit, FMT="(T2,A,T78,A3)") &
184 99 : "CELL_OPT| Keep angles between the cell vectors: ", " NO"
185 : END IF
186 123 : IF (cell_env%keep_symmetry) THEN
187 : WRITE (UNIT=output_unit, FMT="(T2,A,T78,A3)") &
188 23 : "CELL_OPT| Keep cell symmetry: ", "YES"
189 : ELSE
190 : WRITE (UNIT=output_unit, FMT="(T2,A,T78,A3)") &
191 100 : "CELL_OPT| Keep cell symmetry: ", " NO"
192 : END IF
193 123 : label = cell_opt_constraint_label(cell_env%constraint_id)
194 492 : IF (COUNT(cell%perd /= 0) == 2 .AND. .NOT. constraint_explicit) THEN
195 1 : label = TRIM(label)//" (auto)"
196 : END IF
197 : WRITE (UNIT=output_unit, FMT="(T2,A,T77,A)") &
198 123 : "CELL_OPT| Constraint: ", TRIM(label)
199 : END IF
200 246 : CALL cp_print_key_finished_output(output_unit, logger, geo_section, "PRINT%CELL")
201 :
202 492 : END SUBROUTINE cell_opt_env_create
203 :
204 : ! **************************************************************************************************
205 : !> \brief Initialize cell constraints from CONSTRAINT, KEEP_SYMMETRY, KEEP_ANGLES, and KEEP_VOLUME.
206 : !> \param cell_env cell optimization environment to populate
207 : !> \param emit_warnings optional flag to emit user warnings (default .TRUE.)
208 : ! **************************************************************************************************
209 268 : SUBROUTINE cell_opt_setup_manifold(cell_env, emit_warnings)
210 : TYPE(cell_opt_env_type), INTENT(INOUT) :: cell_env
211 : LOGICAL, INTENT(IN), OPTIONAL :: emit_warnings
212 :
213 : INTEGER :: angle_bits, info
214 : LOGICAL :: do_warn
215 : REAL(KIND=dp) :: q0(n_slot)
216 :
217 268 : do_warn = .TRUE.
218 268 : IF (PRESENT(emit_warnings)) do_warn = emit_warnings
219 268 : q0 = cell_manifold_pack_hmat(cell_env%opt_cell%hmat)
220 :
221 : ! Anchor q0 with explicit component rows, then fold explicit angle rows.
222 268 : CALL cell_manifold_init(cell_env%manifold, q0, IAND(cell_env%constraint_id, mask_all), info)
223 268 : IF (info /= info_ok) THEN
224 : CALL cp_abort(__LOCATION__, &
225 : "CELL_OPT: initial cell or CONSTRAINT is invalid ("// &
226 0 : TRIM(cell_manifold_status_label(info))//").")
227 : END IF
228 : CALL try_add_mask_bits(cell_env%manifold, IAND(cell_env%constraint_id, gfix_all), &
229 268 : "CONSTRAINT", do_warn)
230 :
231 268 : IF (cell_env%keep_symmetry) THEN
232 : CALL cell_opt_fold_symmetry_rows(cell_env, cell_env%opt_cell%symmetry_id, &
233 52 : emit_warnings=do_warn)
234 : END IF
235 :
236 268 : IF (cell_env%keep_angles) THEN
237 : ! On an orthorhombic cell, BX = CX = CY = 0 fixes all angles to 90 deg.
238 : ! Off-diagonal hmat entries stay 0.0_dp so cell%orthorhombic stays TRUE.
239 60 : IF (cell_env%opt_cell%orthorhombic) THEN
240 26 : angle_bits = comp_bx + comp_cx + comp_cy
241 : ELSE
242 34 : angle_bits = gfix_alpha + gfix_beta + gfix_gamma
243 : END IF
244 60 : CALL try_add_mask_bits(cell_env%manifold, angle_bits, "KEEP_ANGLES", do_warn)
245 : END IF
246 :
247 268 : IF (cell_env%keep_volume) THEN
248 14 : CALL try_add_mask_bits(cell_env%manifold, gfix_v, "KEEP_VOLUME", do_warn)
249 : END IF
250 :
251 268 : END SUBROUTINE cell_opt_setup_manifold
252 :
253 : ! **************************************************************************************************
254 : !> \brief Add independent constraint rows for crystal system symmetry_id.
255 : !> \param cell_env cell optimization environment
256 : !> \param symmetry_id crystal system identifier (cell_sym_*)
257 : !> \param emit_warnings optional flag to emit user warnings (default .TRUE.)
258 : !> \note This routine skips redundant rows and aborts on unresolvable singularities.
259 : !> When a row is second-order semidefinite at q0, it pins the implied
260 : !> directions and emits a warning. KEEP_SPACE_GROUP passes the point-group
261 : !> crystal system here because the point group fixes the same cell metric.
262 : ! **************************************************************************************************
263 68 : SUBROUTINE cell_opt_fold_symmetry_rows(cell_env, symmetry_id, emit_warnings)
264 : TYPE(cell_opt_env_type), INTENT(INOUT) :: cell_env
265 : INTEGER, INTENT(IN) :: symmetry_id
266 : LOGICAL, INTENT(IN), OPTIONAL :: emit_warnings
267 :
268 : INTEGER :: cand_eq_pair(2, n_geom), cand_n_eq, &
269 : cand_n_lin, i, info, n_next, &
270 : probe_eq_pair(2, n_geom), sym_bits
271 : LOGICAL :: do_warn
272 : REAL(KIND=dp) :: cand_lin_w(n_slot, n_slot), &
273 : probe_lin_w(n_slot, n_slot)
274 : TYPE(cell_manifold_type) :: m_probe
275 :
276 68 : do_warn = .TRUE.
277 68 : IF (PRESENT(emit_warnings)) do_warn = emit_warnings
278 :
279 : CALL symmetry_constraint_rows(symmetry_id, sym_bits, cand_lin_w, cand_n_lin, &
280 68 : cand_eq_pair, cand_n_eq)
281 68 : CALL try_add_mask_bits(cell_env%manifold, sym_bits, "symmetry component constraint", do_warn)
282 :
283 112 : DO i = 1, cand_n_lin
284 44 : n_next = cell_env%manifold%n_lin_rows + 1
285 44 : IF (n_next > n_slot) EXIT
286 1892 : probe_lin_w = cell_env%manifold%lin_w
287 308 : probe_lin_w(:, n_next) = cand_lin_w(:, i)
288 : CALL cell_manifold_init( &
289 : m_probe, cell_env%manifold%q0, cell_env%manifold%mask, info, &
290 : lin_w=probe_lin_w(:, 1:n_next), &
291 44 : eq_pair=cell_env%manifold%eq_pair(:, 1:cell_env%manifold%n_eq_rows))
292 112 : IF (info == info_ok) THEN
293 36 : cell_env%manifold = m_probe
294 : ELSE
295 : CALL resolve_degenerate_constraint(cell_env%manifold, info, &
296 : "symmetry linear constraint", 0, do_warn, &
297 8 : lin_w=cand_lin_w(:, i:i))
298 : END IF
299 : END DO
300 :
301 124 : DO i = 1, cand_n_eq
302 56 : n_next = cell_env%manifold%n_eq_rows + 1
303 56 : IF (n_next > n_geom - 1) EXIT
304 1232 : probe_eq_pair = cell_env%manifold%eq_pair
305 168 : probe_eq_pair(:, n_next) = cand_eq_pair(:, i)
306 : CALL cell_manifold_init( &
307 : m_probe, cell_env%manifold%q0, cell_env%manifold%mask, info, &
308 : lin_w=cell_env%manifold%lin_w(:, 1:cell_env%manifold%n_lin_rows), &
309 56 : eq_pair=probe_eq_pair(:, 1:n_next))
310 124 : IF (info == info_ok) THEN
311 52 : cell_env%manifold = m_probe
312 : ELSE
313 : CALL resolve_degenerate_constraint(cell_env%manifold, info, &
314 : "symmetry equality constraint", 0, do_warn, &
315 4 : eq_pair=cand_eq_pair(:, i:i))
316 : END IF
317 : END DO
318 :
319 12784 : END SUBROUTINE cell_opt_fold_symmetry_rows
320 :
321 : ! **************************************************************************************************
322 : !> \brief Add each independent bit from candidate_bits into m%mask.
323 : !> \param m active constraint state
324 : !> \param candidate_bits comp_* or gfix_* flags to test one bit at a time
325 : !> \param source_label constraint source label for warning and abort messages
326 : !> \param do_warn .TRUE. to emit a warning when this routine pins a row
327 : ! **************************************************************************************************
328 410 : SUBROUTINE try_add_mask_bits(m, candidate_bits, source_label, do_warn)
329 : TYPE(cell_manifold_type), INTENT(INOUT) :: m
330 : INTEGER, INTENT(IN) :: candidate_bits
331 : CHARACTER(LEN=*), INTENT(IN) :: source_label
332 : LOGICAL, INTENT(IN) :: do_warn
333 :
334 : INTEGER :: b, flag, info
335 : TYPE(cell_manifold_type) :: m_probe
336 :
337 5740 : DO b = 0, n_constraint_bits - 1
338 5330 : flag = ISHFT(1, b)
339 5740 : IF (IAND(candidate_bits, flag) /= 0 .AND. IAND(m%mask, flag) == 0) THEN
340 : CALL cell_manifold_init(m_probe, m%q0, IOR(m%mask, flag), info, &
341 : lin_w=m%lin_w(:, 1:m%n_lin_rows), &
342 298 : eq_pair=m%eq_pair(:, 1:m%n_eq_rows))
343 298 : IF (info == info_ok) THEN
344 244 : m = m_probe
345 : ELSE
346 54 : CALL resolve_degenerate_constraint(m, info, source_label, flag, do_warn)
347 : END IF
348 : END IF
349 : END DO
350 :
351 77080 : END SUBROUTINE try_add_mask_bits
352 :
353 : ! **************************************************************************************************
354 : !> \brief Resolve or reject a candidate constraint row that failed LICQ at q0.
355 : !> \param m active constraint state. This routine updates m when it pins a row.
356 : !> \param info failure code from cell_manifold_init
357 : !> \param source_label constraint source label for warning and abort messages
358 : !> \param mask candidate comp_* or gfix_* bit, or 0 for a lin_w or eq_pair test
359 : !> \param do_warn .TRUE. to emit a warning when this routine pins a row
360 : !> \param lin_w optional 6 x 1 column of candidate linear weights
361 : !> \param eq_pair optional 2 x 1 index pair for a candidate equality
362 : ! **************************************************************************************************
363 66 : SUBROUTINE resolve_degenerate_constraint(m, info, source_label, mask, do_warn, lin_w, eq_pair)
364 : TYPE(cell_manifold_type), INTENT(INOUT) :: m
365 : INTEGER, INTENT(IN) :: info
366 : CHARACTER(LEN=*), INTENT(IN) :: source_label
367 : INTEGER, INTENT(IN) :: mask
368 : LOGICAL, INTENT(IN) :: do_warn
369 : REAL(KIND=dp), INTENT(IN), OPTIONAL :: lin_w(:, :)
370 : INTEGER, INTENT(IN), OPTIONAL :: eq_pair(:, :)
371 :
372 : INTEGER :: old_mask, old_n_lin, pinned_bits
373 : LOGICAL :: is_pinned
374 :
375 66 : IF (info == info_licq) THEN
376 66 : old_mask = m%mask
377 66 : old_n_lin = m%n_lin_rows
378 186 : CALL cell_manifold_pin_semidefinite(m, mask, is_pinned, lin_w=lin_w, eq_pair=eq_pair)
379 66 : IF (is_pinned) THEN
380 16 : IF (do_warn) THEN
381 16 : pinned_bits = IAND(IAND(NOT(old_mask), m%mask), mask_all)
382 16 : IF (pinned_bits /= 0 .AND. m%n_lin_rows == old_n_lin) THEN
383 : CALL cp_warn(__LOCATION__, &
384 : "CELL_OPT: "//TRIM(source_label)//" is locally degenerate at "// &
385 : "the initial cell. CELL_OPT pins implied cell component(s): "// &
386 16 : TRIM(ADJUSTL(cell_opt_constraint_label(pinned_bits)))//".")
387 : ELSE
388 : CALL cp_warn(__LOCATION__, &
389 : "CELL_OPT: "//TRIM(source_label)//" is locally degenerate at "// &
390 0 : "the initial cell. CELL_OPT pins implied tangent direction(s).")
391 : END IF
392 : END IF
393 66 : RETURN
394 : END IF
395 142 : IF (cell_manifold_is_redundant(m, mask, lin_w=lin_w, eq_pair=eq_pair)) RETURN
396 : END IF
397 :
398 : CALL cp_abort(__LOCATION__, &
399 : "CELL_OPT: "//TRIM(source_label)//" is locally degenerate at "// &
400 : "the initial cell ("// &
401 0 : TRIM(cell_manifold_status_label(info))//").")
402 :
403 : END SUBROUTINE resolve_degenerate_constraint
404 :
405 : ! **************************************************************************************************
406 : !> \brief ...
407 : !> \param cell_env ...
408 : !> \par History
409 : !> none
410 : !> \author Teodoro Laino - created [tlaino] - 03.2008 - Zurich University
411 : ! **************************************************************************************************
412 246 : SUBROUTINE cell_opt_env_release(cell_env)
413 : TYPE(cell_opt_env_type), INTENT(INOUT) :: cell_env
414 :
415 246 : CALL cell_release(cell_env%opt_cell)
416 :
417 246 : END SUBROUTINE cell_opt_env_release
418 :
419 0 : END MODULE cell_opt_types
|