(git:d597271)
Loading...
Searching...
No Matches
cell_opt_types.F
Go to the documentation of this file.
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! **************************************************************************************************
15
26 USE cell_types, ONLY: cell_clone,&
36 USE input_constants, ONLY: fix_none
40 USE kinds, ONLY: default_string_length,&
41 dp
42#include "../base/base_uses.f90"
43
44 IMPLICIT NONE
45 PRIVATE
46
47 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cell_opt_types'
48
49 PUBLIC :: cell_opt_env_type, &
52
53! **************************************************************************************************
54!> \brief Type containing all informations abour the simulation cell optimization
55!> \par History
56!> none
57!> \author Teodoro Laino - created [tlaino] - 03.2008 - Zurich University
58! **************************************************************************************************
60 ! Simulation cell optimization parameters
61 ! constraint_id is a mask of fixed cell components (comp_* / mask_*):
62 ! a set bit means fixed, 0 (= fix_none) leaves all components relaxable
63 INTEGER :: constraint_id = fix_none
64 LOGICAL :: keep_angles = .false., &
65 keep_symmetry = .false., &
66 keep_volume = .false.
67 REAL(kind=dp) :: pres_ext = 0.0_dp, pres_int = 0.0_dp, pres_tol = 0.0_dp, pres_constr = 0.0_dp
68 REAL(kind=dp), DIMENSION(3, 3) :: mtrx = 0.0_dp
69 ! Fixed rotations between the user's Cartesian frame and the canonical
70 ! frame used for the six independent cell optimization variables.
71 REAL(kind=dp), DIMENSION(3, 3) :: input_to_opt = 0.0_dp, &
72 opt_to_input = 0.0_dp
73 TYPE(cell_type), POINTER :: opt_cell => null()
74 END TYPE cell_opt_env_type
75
76CONTAINS
77
78! **************************************************************************************************
79!> \brief ...
80!> \param cell_env ...
81!> \param force_env ...
82!> \param geo_section ...
83!> \par History
84!> none
85!> \author Teodoro Laino - created [tlaino] - 03.2008 - Zurich University
86! **************************************************************************************************
87 SUBROUTINE cell_opt_env_create(cell_env, force_env, geo_section)
88 TYPE(cell_opt_env_type), INTENT(OUT) :: cell_env
89 TYPE(force_env_type), POINTER :: force_env
90 TYPE(section_vals_type), POINTER :: geo_section
91
92 CHARACTER(LEN=default_string_length) :: constraint_string, label
93 INTEGER :: output_unit
94 LOGICAL :: constraint_explicit
95 TYPE(cell_type), POINTER :: cell
96 TYPE(cp_logger_type), POINTER :: logger
97
98 NULLIFY (cell_env%opt_cell, cell)
99 CALL force_env_get(force_env, cell=cell)
100 CALL cell_create(cell_env%opt_cell)
101 CALL cell_clone(cell, cell_env%opt_cell, tag="CELL_OPT_CANONICAL")
102 CALL canonicalize_cell_matrix(cell_env%opt_cell)
103 ! Keep the physical cell in the input orientation while the optimizer
104 ! works with the canonical cell and its six non-redundant components.
105 cell_env%input_to_opt = matmul(cell_env%opt_cell%hmat, cell%h_inv)
106 cell_env%opt_to_input = matmul(cell%hmat, cell_env%opt_cell%h_inv)
107 CALL section_vals_val_get(geo_section, "KEEP_VOLUME", l_val=cell_env%keep_volume)
108 CALL section_vals_val_get(geo_section, "KEEP_ANGLES", l_val=cell_env%keep_angles)
109 CALL section_vals_val_get(geo_section, "KEEP_SYMMETRY", l_val=cell_env%keep_symmetry)
110 CALL section_vals_val_get(geo_section, "PRESSURE_TOLERANCE", r_val=cell_env%pres_tol)
111 CALL section_vals_val_get(geo_section, "CONSTRAINT", &
112 c_val=constraint_string, explicit=constraint_explicit)
113 CALL parse_cell_constraint(constraint_string, cell, constraint_explicit, &
114 cell_env%constraint_id)
115 ! Canonicalize the echoed value so restart files record the effective
116 ! constraint instead of the user's spelling (case and token order).
117 ! Only for explicitly set values: an unset keyword must stay unparsed so
118 ! the restart echo keeps omitting the line, as it always has.
119 IF (constraint_explicit) THEN
120 CALL section_vals_val_set(geo_section, "CONSTRAINT", &
121 c_val=trim(adjustl( &
122 cell_opt_constraint_label(cell_env%constraint_id))))
123 END IF
124
125 CALL cell_opt_validate_constraint(cell_env)
126
127 ! The masked pressure criterion is structurally off target for this
128 ! combination on a non-orthorhombic cell. The run stays legal: the
129 ! geometry criteria still work, and the pressure criterion may still
130 ! be met when the pinned shear stresses vanish by symmetry. The
131 ! decision lives in pres_crit_shear_pinned, which the unit test
132 ! checks over all 64 masks.
133 IF (pres_crit_shear_pinned(cell_env%constraint_id, &
134 cell_env%opt_cell%orthorhombic, &
135 cell_env%keep_angles)) THEN
136 IF (iand(cell_env%constraint_id, mask_shear) /= 0) THEN
137 CALL cp_warn(__location__, &
138 "CELL_OPT: CONSTRAINT ("// &
139 trim(cell_opt_constraint_label(cell_env%constraint_id))// &
140 ") pins a shear component of a non-orthorhombic cell. "// &
141 "The masked pressure criterion is met at the constrained minimum "// &
142 "only if the stresses conjugate to the pinned components vanish "// &
143 "there, for example when the pinned values are the stress-free "// &
144 "ones; otherwise it can stay off the external pressure. Check "// &
145 "the per-step ""Pressure is converged"" line: with L-BFGS the "// &
146 """run CONVERGED"" banner can appear while it is NO. The "// &
147 "projected-stress criterion planned in #6031 removes this limitation.")
148 ELSE
149 CALL cp_warn(__location__, &
150 "CELL_OPT: CONSTRAINT ("// &
151 trim(cell_opt_constraint_label(cell_env%constraint_id))// &
152 ") fixes a diagonal component while KEEP_ANGLES pins the "// &
153 "angles of a non-orthorhombic cell. The masked pressure criterion "// &
154 "is met at the constrained minimum only if the stresses conjugate "// &
155 "to the pinned components vanish there, for example when the "// &
156 "pinned values are the stress-free ones; otherwise it can stay "// &
157 "off the external pressure. Check the per-step ""Pressure is "// &
158 "converged"" line: with L-BFGS the ""run CONVERGED"" banner can "// &
159 "appear while it is NO. The projected-stress criterion planned "// &
160 "in #6031 removes this limitation.")
161 END IF
162 END IF
163
164 ! Get the external pressure
165 CALL read_external_press_tensor(geo_section, cell_env%opt_cell, cell_env%pres_ext, &
166 cell_env%mtrx, cell_env%input_to_opt)
167
168 ! Print cell optimisation setup
169 NULLIFY (logger)
170 logger => cp_get_default_logger()
171 output_unit = cp_print_key_unit_nr(logger, geo_section, "PRINT%CELL", extension=".Log")
172 IF (output_unit > 0) THEN
173 WRITE (unit=output_unit, fmt="(/,T2,A,T61,F20.1)") &
174 "CELL_OPT| Pressure tolerance [bar]: ", cp_unit_from_cp2k(cell_env%pres_tol, "bar")
175 IF (cell_env%keep_volume) THEN
176 WRITE (unit=output_unit, fmt="(T2,A,T78,A3)") &
177 "CELL_OPT| Keep volume of cell: ", "YES"
178 ELSE
179 WRITE (unit=output_unit, fmt="(T2,A,T78,A3)") &
180 "CELL_OPT| Keep volume of cell: ", " NO"
181 END IF
182 IF (cell_env%keep_angles) THEN
183 WRITE (unit=output_unit, fmt="(T2,A,T78,A3)") &
184 "CELL_OPT| Keep angles between the cell vectors: ", "YES"
185 ELSE
186 WRITE (unit=output_unit, fmt="(T2,A,T78,A3)") &
187 "CELL_OPT| Keep angles between the cell vectors: ", " NO"
188 END IF
189 IF (cell_env%keep_symmetry) THEN
190 WRITE (unit=output_unit, fmt="(T2,A,T78,A3)") &
191 "CELL_OPT| Keep cell symmetry: ", "YES"
192 ELSE
193 WRITE (unit=output_unit, fmt="(T2,A,T78,A3)") &
194 "CELL_OPT| Keep cell symmetry: ", " NO"
195 END IF
196 label = cell_opt_constraint_label(cell_env%constraint_id)
197 IF (count(cell%perd /= 0) == 2 .AND. .NOT. constraint_explicit) THEN
198 label = trim(label)//" (auto)"
199 END IF
200 WRITE (unit=output_unit, fmt="(T2,A,T77,A)") &
201 "CELL_OPT| Constraint: ", trim(label)
202 END IF
203 CALL cp_print_key_finished_output(output_unit, logger, geo_section, "PRINT%CELL")
204
205 END SUBROUTINE cell_opt_env_create
206
207! **************************************************************************************************
208!> \brief Abort on constraint combinations that corrupt the KEEP_* projections
209!> \param cell_env ...
210! **************************************************************************************************
211 SUBROUTINE cell_opt_validate_constraint(cell_env)
212 TYPE(cell_opt_env_type), INTENT(IN) :: cell_env
213
214 LOGICAL :: bad_constraint
215
216 ! Reject CONSTRAINT combinations that would silently corrupt the
217 ! KEEP_ANGLES projection or the KEEP_SYMMETRY symmetrization.
218 ! Slot order of apply_cell_constraints: AX, BX, BY, CX, CY, CZ.
219 ! KEEP_ANGLES projects the gradient slots {BX,BY} onto b and {CX,CY,CZ} onto c.
220 ! Gradient slots that a legacy value zeroes:
221 ! x = {AX,BX,CX} B partial, C partial -> breaks parallelism
222 ! y = {BX,BY,CY} B complete, C partial -> breaks parallelism
223 ! z = {CX,CY,CZ} B untouched, C complete -> safe
224 ! xy = {AX..CY} B complete, C partial -> breaks parallelism
225 ! xz = {AX,BX,CX,CY,CZ} B partial, C complete -> breaks parallelism
226 ! yz = {BX..CZ} B complete, C complete -> safe
227 ! A partial group breaks the parallelism. A complete or untouched group keeps it.
228 IF (cell_env%keep_angles) THEN
229 ! The decision lives in keep_angles_conflict, which the unit test
230 ! checks over all 64 masks.
231 IF (keep_angles_conflict(cell_env%constraint_id, &
232 cell_env%opt_cell%orthorhombic)) THEN
233 CALL cp_abort(__location__, &
234 "CELL_OPT: CONSTRAINT ("// &
235 trim(cell_opt_constraint_label(cell_env%constraint_id))// &
236 ") fixes only part of the b or c cell vector and would "// &
237 "destroy the KEEP_ANGLES projection.")
238 END IF
239 END IF
240
241 IF (cell_env%keep_symmetry .AND. cell_env%constraint_id /= fix_none) THEN
242 ! The decision lives in keep_symmetry_conflict, which the unit test
243 ! checks over all 64 masks for every crystal system.
244 bad_constraint = keep_symmetry_conflict(cell_env%constraint_id, &
245 cell_env%opt_cell%symmetry_id)
246 IF (bad_constraint) THEN
247 CALL cp_abort(__location__, &
248 "CELL_OPT: CONSTRAINT ("// &
249 trim(cell_opt_constraint_label(cell_env%constraint_id))// &
250 ") fixes only part of the components coupled by "// &
251 "KEEP_SYMMETRY ("// &
253 keep_symmetry_coupled_set(cell_env%opt_cell%symmetry_id)))// &
254 ") and would break the cell symmetry.")
255 END IF
256 END IF
257
258 END SUBROUTINE cell_opt_validate_constraint
259
260! **************************************************************************************************
261!> \brief ...
262!> \param cell_env ...
263!> \par History
264!> none
265!> \author Teodoro Laino - created [tlaino] - 03.2008 - Zurich University
266! **************************************************************************************************
267 SUBROUTINE cell_opt_env_release(cell_env)
268 TYPE(cell_opt_env_type), INTENT(INOUT) :: cell_env
269
270 CALL cell_release(cell_env%opt_cell)
271
272 END SUBROUTINE cell_opt_env_release
273
274END MODULE cell_opt_types
Handles all functions related to the CELL.
subroutine, public canonicalize_cell_matrix(cell)
Canonicalize a general cell matrix without changing lengths and angles.
subroutine, public cell_create(cell, hmat, periodic, tag)
allocates and initializes a cell
Contains type used for a Simulation Cell Optimization.
subroutine, public cell_opt_env_release(cell_env)
...
subroutine, public cell_opt_env_create(cell_env, force_env, geo_section)
...
contains a functional that calculates the energy and its derivatives for the geometry optimizer
pure logical function, public keep_symmetry_conflict(mask, symmetry_id)
Whether a CONSTRAINT mask conflicts with the KEEP_SYMMETRY projection.
pure integer function, public keep_symmetry_coupled_set(symmetry_id)
The set of cell components coupled by a crystal system (for diagnostics).
subroutine, public read_external_press_tensor(geo_section, cell, pres_ext, mtrx, input_to_opt)
Reads the external pressure tensor.
pure logical function, public pres_crit_shear_pinned(mask, orthorhombic, keep_angles)
Whether the masked pressure criterion is structurally off target.
integer, parameter, public mask_shear
pure logical function, public keep_angles_conflict(mask, orthorhombic)
Whether a CONSTRAINT mask conflicts with the KEEP_ANGLES projection.
subroutine, public parse_cell_constraint(constraint_string, cell, explicit, mask)
Parses the tokens of the CONSTRAINT keyword into a cell component mask.
pure character(len=default_string_length) function, public cell_opt_constraint_label(mask)
Returns the label of the consolidated cell constraint mask.
Handles all functions related to the CELL.
Definition cell_types.F:15
subroutine, public cell_release(cell)
releases the given cell (see doc/ReferenceCounting.html)
Definition cell_types.F:704
subroutine, public cell_clone(cell_in, cell_out, tag)
Clone cell variable.
Definition cell_types.F:142
various routines to log and control the output. The idea is that decisions about where to log should ...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
routines to handle the output, The idea is to remove the decision of wheter to output and what to out...
integer function, public cp_print_key_unit_nr(logger, basis_section, print_key_path, extension, middle_name, local, log_filename, ignore_should_output, file_form, file_position, file_action, file_status, do_backup, on_file, is_new_file, mpi_io, fout)
...
subroutine, public cp_print_key_finished_output(unit_nr, logger, basis_section, print_key_path, local, ignore_should_output, on_file, mpi_io)
should be called after you finish working with a unit obtained with cp_print_key_unit_nr,...
unit conversion facility
Definition cp_units.F:30
real(kind=dp) function, public cp_unit_from_cp2k(value, unit_str, defaults, power)
converts from the internal cp2k units to the given unit
Definition cp_units.F:1251
Interface for the force calculations.
recursive subroutine, public force_env_get(force_env, in_use, fist_env, qs_env, meta_env, fp_env, subsys, para_env, potential_energy, additional_potential, kinetic_energy, harmonic_shell, kinetic_shell, cell, sub_force_env, qmmm_env, qmmmx_env, eip_env, pwdft_env, globenv, input, force_env_section, method_name_id, root_section, mixed_env, nnp_env, embed_env, ipi_env)
returns various attributes about the force environment
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public fix_none
objects that represent the structure of input sections and the data contained in an input section
subroutine, public section_vals_val_set(section_vals, keyword_name, i_rep_section, i_rep_val, val, l_val, i_val, r_val, c_val, l_vals_ptr, i_vals_ptr, r_vals_ptr, c_vals_ptr)
sets the requested value
subroutine, public section_vals_val_get(section_vals, keyword_name, i_rep_section, i_rep_val, n_rep_val, val, l_val, i_val, r_val, c_val, l_vals, i_vals, r_vals, c_vals, explicit)
returns the requested value
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
Type containing all informations abour the simulation cell optimization.
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
type of a logger, at the moment it contains just a print level starting at which level it should be l...
wrapper to abstract the force evaluation of the various methods