Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2025 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief Handles all functions related to the CELL
10 : !> \par History
11 : !> 11.2008 Teodoro Laino [tlaino] - deeply cleaning cell_type from units
12 : !> 10.2014 Moved many routines to cell_types.F.
13 : !> \author Matthias KracK (16.01.2002, based on a earlier version of CJM, JGH)
14 : ! **************************************************************************************************
15 : MODULE cell_methods
16 : USE cell_types, ONLY: &
17 : cell_clone, cell_release, cell_sym_cubic, cell_sym_hexagonal_gamma_120, &
18 : cell_sym_hexagonal_gamma_60, cell_sym_monoclinic, cell_sym_monoclinic_gamma_ab, &
19 : cell_sym_none, cell_sym_orthorhombic, cell_sym_rhombohedral, cell_sym_tetragonal_ab, &
20 : cell_sym_tetragonal_ac, cell_sym_tetragonal_bc, cell_sym_triclinic, cell_type, get_cell, &
21 : plane_distance, use_perd_none, use_perd_x, use_perd_xy, use_perd_xyz, use_perd_xz, &
22 : use_perd_y, use_perd_yz, use_perd_z
23 : USE cp_log_handling, ONLY: cp_get_default_logger,&
24 : cp_logger_type
25 : USE cp_output_handling, ONLY: cp_print_key_finished_output,&
26 : cp_print_key_unit_nr
27 : USE cp_parser_methods, ONLY: parser_get_next_line,&
28 : parser_get_object,&
29 : parser_search_string
30 : USE cp_parser_types, ONLY: cp_parser_type,&
31 : parser_create,&
32 : parser_release
33 : USE cp_units, ONLY: cp_unit_from_cp2k,&
34 : cp_unit_to_cp2k
35 : USE input_constants, ONLY: do_cell_cif,&
36 : do_cell_cp2k,&
37 : do_cell_xsc
38 : USE input_cp2k_subsys, ONLY: create_cell_section
39 : USE input_enumeration_types, ONLY: enum_i2c,&
40 : enumeration_type
41 : USE input_keyword_types, ONLY: keyword_get,&
42 : keyword_type
43 : USE input_section_types, ONLY: &
44 : section_get_keyword, section_release, section_type, section_vals_get, &
45 : section_vals_get_subs_vals, section_vals_type, section_vals_val_get, section_vals_val_set, &
46 : section_vals_val_unset
47 : USE kinds, ONLY: default_path_length,&
48 : default_string_length,&
49 : dp
50 : USE machine, ONLY: default_output_unit
51 : USE mathconstants, ONLY: degree,&
52 : sqrt3
53 : USE mathlib, ONLY: angle,&
54 : det_3x3,&
55 : inv_3x3
56 : USE message_passing, ONLY: mp_para_env_type
57 : #include "./base/base_uses.f90"
58 :
59 : IMPLICIT NONE
60 :
61 : PRIVATE
62 :
63 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cell_methods'
64 :
65 : PUBLIC :: cell_create, &
66 : init_cell, &
67 : read_cell, &
68 : read_cell_cif, &
69 : set_cell_param, &
70 : write_cell, &
71 : write_cell_low
72 :
73 : CONTAINS
74 :
75 : ! **************************************************************************************************
76 : !> \brief allocates and initializes a cell
77 : !> \param cell the cell to initialize
78 : !> \param hmat the h matrix that defines the cell
79 : !> \param periodic periodicity of the cell
80 : !> \param tag ...
81 : !> \par History
82 : !> 09.2003 created [fawzi]
83 : !> \author Fawzi Mohamed
84 : ! **************************************************************************************************
85 61267 : SUBROUTINE cell_create(cell, hmat, periodic, tag)
86 :
87 : TYPE(cell_type), POINTER :: cell
88 : REAL(KIND=dp), DIMENSION(3, 3), INTENT(IN), &
89 : OPTIONAL :: hmat
90 : INTEGER, DIMENSION(3), INTENT(IN), OPTIONAL :: periodic
91 : CHARACTER(LEN=*), INTENT(IN), OPTIONAL :: tag
92 :
93 0 : CPASSERT(.NOT. ASSOCIATED(cell))
94 1838010 : ALLOCATE (cell)
95 61267 : cell%ref_count = 1
96 61267 : IF (PRESENT(periodic)) THEN
97 1340 : cell%perd = periodic
98 : ELSE
99 243728 : cell%perd = 1
100 : END IF
101 : cell%orthorhombic = .FALSE.
102 : cell%symmetry_id = cell_sym_none
103 61267 : IF (PRESENT(hmat)) CALL init_cell(cell, hmat)
104 61267 : IF (PRESENT(tag)) cell%tag = tag
105 :
106 61267 : END SUBROUTINE cell_create
107 :
108 : ! **************************************************************************************************
109 : !> \brief Initialise/readjust a simulation cell after hmat has been changed
110 : !> \param cell ...
111 : !> \param hmat ...
112 : !> \param periodic ...
113 : !> \date 16.01.2002
114 : !> \author Matthias Krack
115 : !> \version 1.0
116 : ! **************************************************************************************************
117 342418 : SUBROUTINE init_cell(cell, hmat, periodic)
118 :
119 : TYPE(cell_type), POINTER :: cell
120 : REAL(KIND=dp), DIMENSION(3, 3), INTENT(IN), &
121 : OPTIONAL :: hmat
122 : INTEGER, DIMENSION(3), INTENT(IN), OPTIONAL :: periodic
123 :
124 : REAL(KIND=dp), PARAMETER :: eps_hmat = 1.0E-14_dp
125 :
126 : INTEGER :: dim
127 : REAL(KIND=dp) :: a, acosa, acosah, acosg, alpha, asina, &
128 : asinah, asing, beta, gamma, norm, &
129 : norm_c
130 : REAL(KIND=dp), DIMENSION(3) :: abc
131 :
132 342418 : CPASSERT(ASSOCIATED(cell))
133 :
134 447142 : IF (PRESENT(hmat)) cell%hmat(:, :) = hmat(:, :)
135 342496 : IF (PRESENT(periodic)) cell%perd(:) = periodic(:)
136 :
137 342418 : cell%deth = ABS(det_3x3(cell%hmat))
138 :
139 342418 : IF (cell%deth < 1.0E-10_dp) THEN
140 0 : CALL write_cell_low(cell, "angstrom", default_output_unit)
141 : CALL cp_abort(__LOCATION__, &
142 : "An invalid set of cell vectors was specified. "// &
143 0 : "The cell volume is too small")
144 : END IF
145 :
146 346484 : SELECT CASE (cell%symmetry_id)
147 : CASE (cell_sym_cubic, &
148 : cell_sym_tetragonal_ab, &
149 : cell_sym_tetragonal_ac, &
150 : cell_sym_tetragonal_bc, &
151 : cell_sym_orthorhombic)
152 4066 : CALL get_cell(cell=cell, abc=abc)
153 4066 : abc(2) = plane_distance(0, 1, 0, cell=cell)
154 4066 : abc(3) = plane_distance(0, 0, 1, cell=cell)
155 4066 : SELECT CASE (cell%symmetry_id)
156 : CASE (cell_sym_cubic)
157 5376 : abc(1:3) = SUM(abc(1:3))/3.0_dp
158 : CASE (cell_sym_tetragonal_ab, &
159 : cell_sym_tetragonal_ac, &
160 : cell_sym_tetragonal_bc)
161 4066 : SELECT CASE (cell%symmetry_id)
162 : CASE (cell_sym_tetragonal_ab)
163 1322 : a = 0.5_dp*(abc(1) + abc(2))
164 1322 : abc(1) = a
165 1322 : abc(2) = a
166 : CASE (cell_sym_tetragonal_ac)
167 633 : a = 0.5_dp*(abc(1) + abc(3))
168 633 : abc(1) = a
169 633 : abc(3) = a
170 : CASE (cell_sym_tetragonal_bc)
171 612 : a = 0.5_dp*(abc(2) + abc(3))
172 612 : abc(2) = a
173 2567 : abc(3) = a
174 : END SELECT
175 : END SELECT
176 4066 : cell%hmat(1, 1) = abc(1); cell%hmat(1, 2) = 0.0_dp; cell%hmat(1, 3) = 0.0_dp
177 4066 : cell%hmat(2, 1) = 0.0_dp; cell%hmat(2, 2) = abc(2); cell%hmat(2, 3) = 0.0_dp
178 4066 : cell%hmat(3, 1) = 0.0_dp; cell%hmat(3, 2) = 0.0_dp; cell%hmat(3, 3) = abc(3)
179 : CASE (cell_sym_hexagonal_gamma_60, cell_sym_hexagonal_gamma_120)
180 2638 : CALL get_cell(cell=cell, abc=abc)
181 2638 : a = 0.5_dp*(abc(1) + abc(2))
182 2638 : acosg = 0.5_dp*a
183 2638 : asing = sqrt3*acosg
184 2638 : IF (cell%symmetry_id == cell_sym_hexagonal_gamma_120) acosg = -acosg
185 2638 : cell%hmat(1, 1) = a; cell%hmat(1, 2) = acosg; cell%hmat(1, 3) = 0.0_dp
186 2638 : cell%hmat(2, 1) = 0.0_dp; cell%hmat(2, 2) = asing; cell%hmat(2, 3) = 0.0_dp
187 2638 : cell%hmat(3, 1) = 0.0_dp; cell%hmat(3, 2) = 0.0_dp; cell%hmat(3, 3) = abc(3)
188 : CASE (cell_sym_rhombohedral)
189 665 : CALL get_cell(cell=cell, abc=abc)
190 2660 : a = SUM(abc(1:3))/3.0_dp
191 : alpha = (angle(cell%hmat(:, 3), cell%hmat(:, 2)) + &
192 : angle(cell%hmat(:, 1), cell%hmat(:, 3)) + &
193 665 : angle(cell%hmat(:, 1), cell%hmat(:, 2)))/3.0_dp
194 665 : acosa = a*COS(alpha)
195 665 : asina = a*SIN(alpha)
196 665 : acosah = a*COS(0.5_dp*alpha)
197 665 : asinah = a*SIN(0.5_dp*alpha)
198 665 : norm = acosa/acosah
199 665 : norm_c = SQRT(1.0_dp - norm*norm)
200 665 : cell%hmat(1, 1) = a; cell%hmat(1, 2) = acosa; cell%hmat(1, 3) = acosah*norm
201 665 : cell%hmat(2, 1) = 0.0_dp; cell%hmat(2, 2) = asina; cell%hmat(2, 3) = asinah*norm
202 665 : cell%hmat(3, 1) = 0.0_dp; cell%hmat(3, 2) = 0.0_dp; cell%hmat(3, 3) = a*norm_c
203 : CASE (cell_sym_monoclinic)
204 873 : CALL get_cell(cell=cell, abc=abc)
205 873 : beta = angle(cell%hmat(:, 1), cell%hmat(:, 3))
206 873 : cell%hmat(1, 1) = abc(1); cell%hmat(1, 2) = 0.0_dp; cell%hmat(1, 3) = abc(3)*COS(beta)
207 873 : cell%hmat(2, 1) = 0.0_dp; cell%hmat(2, 2) = abc(2); cell%hmat(2, 3) = 0.0_dp
208 873 : cell%hmat(3, 1) = 0.0_dp; cell%hmat(3, 2) = 0.0_dp; cell%hmat(3, 3) = abc(3)*SIN(beta)
209 : CASE (cell_sym_monoclinic_gamma_ab)
210 : ! Cell symmetry with a = b, alpha = beta = 90 degree and gammma not equal 90 degree
211 742 : CALL get_cell(cell=cell, abc=abc)
212 742 : a = 0.5_dp*(abc(1) + abc(2))
213 742 : gamma = angle(cell%hmat(:, 1), cell%hmat(:, 2))
214 742 : acosg = a*COS(gamma)
215 742 : asing = a*SIN(gamma)
216 742 : cell%hmat(1, 1) = a; cell%hmat(1, 2) = acosg; cell%hmat(1, 3) = 0.0_dp
217 742 : cell%hmat(2, 1) = 0.0_dp; cell%hmat(2, 2) = asing; cell%hmat(2, 3) = 0.0_dp
218 343160 : cell%hmat(3, 1) = 0.0_dp; cell%hmat(3, 2) = 0.0_dp; cell%hmat(3, 3) = abc(3)
219 : CASE (cell_sym_triclinic)
220 : ! Nothing to do
221 : END SELECT
222 :
223 : ! Do we have an (almost) orthorhombic cell?
224 : IF ((ABS(cell%hmat(1, 2)) < eps_hmat) .AND. (ABS(cell%hmat(1, 3)) < eps_hmat) .AND. &
225 : (ABS(cell%hmat(2, 1)) < eps_hmat) .AND. (ABS(cell%hmat(2, 3)) < eps_hmat) .AND. &
226 342418 : (ABS(cell%hmat(3, 1)) < eps_hmat) .AND. (ABS(cell%hmat(3, 2)) < eps_hmat)) THEN
227 316959 : cell%orthorhombic = .TRUE.
228 : ELSE
229 25459 : cell%orthorhombic = .FALSE.
230 : END IF
231 :
232 : ! Retain an exact orthorhombic cell
233 : ! (off-diagonal elements must remain zero identically to keep QS fast)
234 342418 : IF (cell%orthorhombic) THEN
235 316959 : cell%hmat(1, 2) = 0.0_dp
236 316959 : cell%hmat(1, 3) = 0.0_dp
237 316959 : cell%hmat(2, 1) = 0.0_dp
238 316959 : cell%hmat(2, 3) = 0.0_dp
239 316959 : cell%hmat(3, 1) = 0.0_dp
240 316959 : cell%hmat(3, 2) = 0.0_dp
241 : END IF
242 :
243 1369672 : dim = COUNT(cell%perd == 1)
244 342418 : IF ((dim == 1) .AND. (.NOT. cell%orthorhombic)) THEN
245 0 : CPABORT("Non-orthorhombic and not periodic")
246 : END IF
247 :
248 : ! Update deth and hmat_inv with enforced symmetry
249 342418 : cell%deth = ABS(det_3x3(cell%hmat))
250 342418 : IF (cell%deth < 1.0E-10_dp) THEN
251 : CALL cp_abort(__LOCATION__, &
252 : "An invalid set of cell vectors was obtained after applying "// &
253 0 : "the requested cell symmetry. The cell volume is too small")
254 : END IF
255 4451434 : cell%h_inv = inv_3x3(cell%hmat)
256 :
257 342418 : END SUBROUTINE init_cell
258 :
259 : ! **************************************************************************************************
260 : !> \brief ...
261 : !> \param cell ...
262 : !> \param cell_ref ...
263 : !> \param use_ref_cell ...
264 : !> \param cell_section ...
265 : !> \param check_for_ref ...
266 : !> \param para_env ...
267 : !> \par History
268 : !> 03.2005 created [teo]
269 : !> \author Teodoro Laino
270 : ! **************************************************************************************************
271 140511 : RECURSIVE SUBROUTINE read_cell(cell, cell_ref, use_ref_cell, cell_section, &
272 : check_for_ref, para_env)
273 :
274 : TYPE(cell_type), POINTER :: cell, cell_ref
275 : LOGICAL, INTENT(INOUT), OPTIONAL :: use_ref_cell
276 : TYPE(section_vals_type), OPTIONAL, POINTER :: cell_section
277 : LOGICAL, INTENT(IN), OPTIONAL :: check_for_ref
278 : TYPE(mp_para_env_type), POINTER :: para_env
279 :
280 : INTEGER :: my_per
281 20073 : INTEGER, DIMENSION(:), POINTER :: multiple_unit_cell
282 : LOGICAL :: cell_read_a, cell_read_abc, cell_read_b, &
283 : cell_read_c, cell_read_file, check, &
284 : my_check
285 20073 : REAL(KIND=dp), DIMENSION(:), POINTER :: cell_angles, cell_par
286 : TYPE(section_vals_type), POINTER :: cell_ref_section
287 :
288 20073 : my_check = .TRUE.
289 20073 : NULLIFY (cell_ref_section, cell_par, multiple_unit_cell)
290 20029 : IF (.NOT. ASSOCIATED(cell)) CALL cell_create(cell, tag="CELL")
291 20073 : IF (.NOT. ASSOCIATED(cell_ref)) CALL cell_create(cell_ref, tag="CELL_REF")
292 20073 : IF (PRESENT(check_for_ref)) my_check = check_for_ref
293 :
294 20073 : cell%deth = 0.0_dp
295 20073 : cell%orthorhombic = .FALSE.
296 80292 : cell%perd(:) = 1
297 20073 : cell%symmetry_id = cell_sym_none
298 260949 : cell%hmat(:, :) = 0.0_dp
299 260949 : cell%h_inv(:, :) = 0.0_dp
300 : cell_read_file = .FALSE.
301 : cell_read_a = .FALSE.
302 20073 : cell_read_b = .FALSE.
303 20073 : cell_read_c = .FALSE.
304 : ! Trying to read cell info from file
305 20073 : CALL section_vals_val_get(cell_section, "CELL_FILE_NAME", explicit=cell_read_file)
306 20073 : IF (cell_read_file) CALL read_cell_from_external_file(cell_section, para_env)
307 :
308 : ! Trying to read cell info from the separate A, B, C vectors
309 : ! If cell information is provided through file A,B,C contain the file information..
310 : ! a print warning is shown on screen..
311 20073 : CALL section_vals_val_get(cell_section, "A", explicit=cell_read_a)
312 20073 : IF (cell_read_a) THEN
313 2442 : CALL section_vals_val_get(cell_section, "A", r_vals=cell_par)
314 19536 : cell%hmat(:, 1) = cell_par(:)
315 : END IF
316 20073 : CALL section_vals_val_get(cell_section, "B", explicit=cell_read_b)
317 20073 : IF (cell_read_b) THEN
318 2442 : CALL section_vals_val_get(cell_section, "B", r_vals=cell_par)
319 19536 : cell%hmat(:, 2) = cell_par(:)
320 : END IF
321 20073 : CALL section_vals_val_get(cell_section, "C", explicit=cell_read_c)
322 20073 : IF (cell_read_c) THEN
323 2442 : CALL section_vals_val_get(cell_section, "C", r_vals=cell_par)
324 19536 : cell%hmat(:, 3) = cell_par(:)
325 : END IF
326 20073 : check = ((cell_read_a .EQV. cell_read_b) .AND. (cell_read_b .EQV. cell_read_c))
327 : IF (.NOT. check) &
328 : CALL cp_warn(__LOCATION__, &
329 : "Cell Information provided through vectors A, B and C. Not all three "// &
330 0 : "vectors were provided! Cell setup may be incomplete!")
331 :
332 : ! Very last option.. Trying to read cell info from ABC keyword
333 20073 : CALL section_vals_val_get(cell_section, "ABC", explicit=cell_read_abc)
334 20073 : IF (cell_read_abc) THEN
335 17631 : check = (cell_read_a .OR. cell_read_b .OR. cell_read_c)
336 : IF (check) &
337 : CALL cp_warn(__LOCATION__, &
338 : "Cell Information provided through vectors A, B and C in conjunction with ABC."// &
339 0 : " The definition of the ABC keyword will override the one provided by A,B and C.")
340 229203 : cell%hmat = 0.0_dp
341 17631 : CALL section_vals_val_get(cell_section, "ABC", r_vals=cell_par)
342 17631 : CALL section_vals_val_get(cell_section, "ALPHA_BETA_GAMMA", r_vals=cell_angles)
343 17631 : CALL set_cell_param(cell, cell_par, cell_angles, do_init_cell=.FALSE.)
344 : END IF
345 :
346 : ! Multiple unit cell
347 20073 : CALL section_vals_val_get(cell_section, "MULTIPLE_UNIT_CELL", i_vals=multiple_unit_cell)
348 79412 : IF (ANY(multiple_unit_cell /= 1)) CALL set_multiple_unit_cell(cell, multiple_unit_cell)
349 :
350 20073 : CALL section_vals_val_get(cell_section, "PERIODIC", i_val=my_per)
351 0 : SELECT CASE (my_per)
352 : CASE (use_perd_x)
353 0 : cell%perd = [1, 0, 0]
354 : CASE (use_perd_y)
355 0 : cell%perd = [0, 1, 0]
356 : CASE (use_perd_z)
357 32 : cell%perd = [0, 0, 1]
358 : CASE (use_perd_xy)
359 96 : cell%perd = [1, 1, 0]
360 : CASE (use_perd_xz)
361 48 : cell%perd = [1, 0, 1]
362 : CASE (use_perd_yz)
363 192 : cell%perd = [0, 1, 1]
364 : CASE (use_perd_xyz)
365 52524 : cell%perd = [1, 1, 1]
366 : CASE (use_perd_none)
367 27400 : cell%perd = [0, 0, 0]
368 : CASE DEFAULT
369 20073 : CPABORT("")
370 : END SELECT
371 :
372 : ! Load requested cell symmetry
373 20073 : CALL section_vals_val_get(cell_section, "SYMMETRY", i_val=cell%symmetry_id)
374 :
375 : ! Initialize cell
376 20073 : CALL init_cell(cell)
377 :
378 20073 : IF (my_check) THEN
379 : ! Recursive check for reference cell requested
380 19635 : cell_ref_section => section_vals_get_subs_vals(cell_section, "CELL_REF")
381 19635 : IF (parsed_cp2k_input(cell_ref_section, check_this_section=.TRUE.)) THEN
382 44 : IF (PRESENT(use_ref_cell)) use_ref_cell = .TRUE.
383 : CALL read_cell(cell_ref, cell_ref, use_ref_cell=use_ref_cell, &
384 : cell_section=cell_ref_section, check_for_ref=.FALSE., &
385 44 : para_env=para_env)
386 : ELSE
387 19591 : CALL cell_clone(cell, cell_ref, tag="CELL_REF")
388 19591 : IF (PRESENT(use_ref_cell)) use_ref_cell = .FALSE.
389 : END IF
390 : END IF
391 :
392 20073 : END SUBROUTINE read_cell
393 :
394 : ! **************************************************************************************************
395 : !> \brief utility function to ease the transition to the new input.
396 : !> returns true if the new input was parsed
397 : !> \param input_file the parsed input file
398 : !> \param check_this_section ...
399 : !> \return ...
400 : !> \author fawzi
401 : ! **************************************************************************************************
402 19635 : FUNCTION parsed_cp2k_input(input_file, check_this_section) RESULT(res)
403 :
404 : TYPE(section_vals_type), POINTER :: input_file
405 : LOGICAL, INTENT(IN), OPTIONAL :: check_this_section
406 : LOGICAL :: res
407 :
408 : LOGICAL :: my_check
409 : TYPE(section_vals_type), POINTER :: glob_section
410 :
411 19635 : my_check = .FALSE.
412 19635 : IF (PRESENT(check_this_section)) my_check = check_this_section
413 19635 : res = ASSOCIATED(input_file)
414 19635 : IF (res) THEN
415 19635 : CPASSERT(input_file%ref_count > 0)
416 19635 : IF (.NOT. my_check) THEN
417 0 : glob_section => section_vals_get_subs_vals(input_file, "GLOBAL")
418 0 : CALL section_vals_get(glob_section, explicit=res)
419 : ELSE
420 19635 : CALL section_vals_get(input_file, explicit=res)
421 : END IF
422 : END IF
423 :
424 19635 : END FUNCTION parsed_cp2k_input
425 :
426 : ! **************************************************************************************************
427 : !> \brief Sets the cell using the internal parameters (a,b,c) (alpha,beta,gamma)
428 : !> using the convention: a parallel to the x axis, b in the x-y plane and
429 : !> and c univoquely determined; gamma is the angle between a and b; beta
430 : !> is the angle between c and a and alpha is the angle between c and b
431 : !> \param cell ...
432 : !> \param cell_length ...
433 : !> \param cell_angle ...
434 : !> \param periodic ...
435 : !> \param do_init_cell ...
436 : !> \date 03.2008
437 : !> \author Teodoro Laino
438 : ! **************************************************************************************************
439 17657 : SUBROUTINE set_cell_param(cell, cell_length, cell_angle, periodic, do_init_cell)
440 :
441 : TYPE(cell_type), POINTER :: cell
442 : REAL(KIND=dp), DIMENSION(3), INTENT(IN) :: cell_length, cell_angle
443 : INTEGER, DIMENSION(3), INTENT(IN), OPTIONAL :: periodic
444 : LOGICAL, INTENT(IN) :: do_init_cell
445 :
446 : REAL(KIND=dp), PARAMETER :: eps = EPSILON(0.0_dp)
447 :
448 : REAL(KIND=dp) :: cos_alpha, cos_beta, cos_gamma, sin_gamma
449 :
450 17657 : CPASSERT(ASSOCIATED(cell))
451 70628 : CPASSERT(ALL(cell_angle /= 0.0_dp))
452 :
453 17657 : cos_gamma = COS(cell_angle(3)); IF (ABS(cos_gamma) < eps) cos_gamma = 0.0_dp
454 17657 : IF (ABS(ABS(cos_gamma) - 1.0_dp) < eps) cos_gamma = SIGN(1.0_dp, cos_gamma)
455 17657 : sin_gamma = SIN(cell_angle(3)); IF (ABS(sin_gamma) < eps) sin_gamma = 0.0_dp
456 17657 : IF (ABS(ABS(sin_gamma) - 1.0_dp) < eps) sin_gamma = SIGN(1.0_dp, sin_gamma)
457 17657 : cos_beta = COS(cell_angle(2)); IF (ABS(cos_beta) < eps) cos_beta = 0.0_dp
458 17657 : IF (ABS(ABS(cos_beta) - 1.0_dp) < eps) cos_beta = SIGN(1.0_dp, cos_beta)
459 17657 : cos_alpha = COS(cell_angle(1)); IF (ABS(cos_alpha) < eps) cos_alpha = 0.0_dp
460 17657 : IF (ABS(ABS(cos_alpha) - 1.0_dp) < eps) cos_alpha = SIGN(1.0_dp, cos_alpha)
461 :
462 70628 : cell%hmat(:, 1) = [1.0_dp, 0.0_dp, 0.0_dp]
463 70628 : cell%hmat(:, 2) = [cos_gamma, sin_gamma, 0.0_dp]
464 70628 : cell%hmat(:, 3) = [cos_beta, (cos_alpha - cos_gamma*cos_beta)/sin_gamma, 0.0_dp]
465 17657 : cell%hmat(3, 3) = SQRT(1.0_dp - cell%hmat(1, 3)**2 - cell%hmat(2, 3)**2)
466 :
467 70628 : cell%hmat(:, 1) = cell%hmat(:, 1)*cell_length(1)
468 70628 : cell%hmat(:, 2) = cell%hmat(:, 2)*cell_length(2)
469 70628 : cell%hmat(:, 3) = cell%hmat(:, 3)*cell_length(3)
470 :
471 17657 : IF (do_init_cell) THEN
472 26 : IF (PRESENT(periodic)) THEN
473 26 : CALL init_cell(cell=cell, periodic=periodic)
474 : ELSE
475 0 : CALL init_cell(cell=cell)
476 : END IF
477 : END IF
478 :
479 17657 : END SUBROUTINE set_cell_param
480 :
481 : ! **************************************************************************************************
482 : !> \brief Setup of the multiple unit_cell
483 : !> \param cell ...
484 : !> \param multiple_unit_cell ...
485 : !> \date 05.2009
486 : !> \author Teodoro Laino [tlaino]
487 : !> \version 1.0
488 : ! **************************************************************************************************
489 300 : SUBROUTINE set_multiple_unit_cell(cell, multiple_unit_cell)
490 :
491 : TYPE(cell_type), POINTER :: cell
492 : INTEGER, DIMENSION(:), POINTER :: multiple_unit_cell
493 :
494 300 : CPASSERT(ASSOCIATED(cell))
495 :
496 : ! Abort, if one of the value is set to zero
497 1200 : IF (ANY(multiple_unit_cell <= 0)) &
498 : CALL cp_abort(__LOCATION__, &
499 : "CELL%MULTIPLE_UNIT_CELL accepts only integer values larger than 0! "// &
500 0 : "A value of 0 or negative is meaningless!")
501 :
502 : ! Scale abc according to user request
503 1200 : cell%hmat(:, 1) = cell%hmat(:, 1)*multiple_unit_cell(1)
504 1200 : cell%hmat(:, 2) = cell%hmat(:, 2)*multiple_unit_cell(2)
505 1200 : cell%hmat(:, 3) = cell%hmat(:, 3)*multiple_unit_cell(3)
506 :
507 300 : END SUBROUTINE set_multiple_unit_cell
508 :
509 : ! **************************************************************************************************
510 : !> \brief Read cell information from an external file
511 : !> \param cell_section ...
512 : !> \param para_env ...
513 : !> \date 02.2008
514 : !> \author Teodoro Laino [tlaino] - University of Zurich
515 : !> \version 1.0
516 : ! **************************************************************************************************
517 12 : SUBROUTINE read_cell_from_external_file(cell_section, para_env)
518 :
519 : TYPE(section_vals_type), POINTER :: cell_section
520 : TYPE(mp_para_env_type), POINTER :: para_env
521 :
522 : CHARACTER(LEN=default_path_length) :: cell_file_name
523 : INTEGER :: i, idum, j, my_format, n_rep
524 : LOGICAL :: explicit, my_end
525 : REAL(KIND=dp) :: xdum
526 : REAL(KIND=dp), DIMENSION(3, 3) :: hmat
527 12 : REAL(KIND=dp), DIMENSION(:), POINTER :: cell_par
528 : TYPE(cell_type), POINTER :: cell
529 : TYPE(cp_parser_type) :: parser
530 :
531 12 : CALL section_vals_val_get(cell_section, "CELL_FILE_NAME", c_val=cell_file_name)
532 12 : CALL section_vals_val_get(cell_section, "CELL_FILE_FORMAT", i_val=my_format)
533 2 : SELECT CASE (my_format)
534 : CASE (do_cell_cp2k)
535 2 : CALL parser_create(parser, cell_file_name, para_env=para_env)
536 2 : CALL parser_get_next_line(parser, 1)
537 2 : my_end = .FALSE.
538 24 : DO WHILE (.NOT. my_end)
539 22 : READ (parser%input_line, *) idum, xdum, hmat(:, 1), hmat(:, 2), hmat(:, 3)
540 24 : CALL parser_get_next_line(parser, 1, at_end=my_end)
541 : END DO
542 2 : CALL parser_release(parser)
543 : ! Convert to CP2K units
544 8 : DO i = 1, 3
545 26 : DO j = 1, 3
546 24 : hmat(j, i) = cp_unit_to_cp2k(hmat(j, i), "angstrom")
547 : END DO
548 : END DO
549 : CASE (do_cell_xsc)
550 0 : CALL parser_create(parser, cell_file_name, para_env=para_env)
551 0 : CALL parser_get_next_line(parser, 1)
552 0 : READ (parser%input_line, *) idum, hmat(:, 1), hmat(:, 2), hmat(:, 3)
553 0 : CALL parser_release(parser)
554 : ! Convert to CP2K units
555 0 : DO i = 1, 3
556 0 : DO j = 1, 3
557 0 : hmat(j, i) = cp_unit_to_cp2k(hmat(j, i), "angstrom")
558 : END DO
559 : END DO
560 : CASE (do_cell_cif)
561 10 : NULLIFY (cell)
562 10 : CALL cell_create(cell)
563 10 : CALL read_cell_cif(cell_file_name, cell, para_env)
564 130 : hmat = cell%hmat
565 22 : CALL cell_release(cell)
566 : END SELECT
567 12 : CALL section_vals_val_unset(cell_section, "CELL_FILE_NAME")
568 12 : CALL section_vals_val_unset(cell_section, "CELL_FILE_FORMAT")
569 : ! Check if the cell was already defined
570 12 : explicit = .FALSE.
571 12 : CALL section_vals_val_get(cell_section, "A", n_rep_val=n_rep)
572 12 : explicit = explicit .OR. (n_rep == 1)
573 12 : CALL section_vals_val_get(cell_section, "B", n_rep_val=n_rep)
574 12 : explicit = explicit .OR. (n_rep == 1)
575 12 : CALL section_vals_val_get(cell_section, "C", n_rep_val=n_rep)
576 12 : explicit = explicit .OR. (n_rep == 1)
577 12 : CALL section_vals_val_get(cell_section, "ABC", n_rep_val=n_rep)
578 12 : explicit = explicit .OR. (n_rep == 1)
579 : ! Possibly print a warning
580 : IF (explicit) &
581 : CALL cp_warn(__LOCATION__, &
582 : "Cell specification (A,B,C or ABC) provided together with the external "// &
583 : "cell setup! Ignoring (A,B,C or ABC) and proceeding with info read from the "// &
584 0 : "external file! ")
585 : ! Copy cell information in the A, B, C fields..(we may need them later on..)
586 12 : ALLOCATE (cell_par(3))
587 48 : cell_par = hmat(:, 1)
588 12 : CALL section_vals_val_set(cell_section, "A", r_vals_ptr=cell_par)
589 12 : ALLOCATE (cell_par(3))
590 48 : cell_par = hmat(:, 2)
591 12 : CALL section_vals_val_set(cell_section, "B", r_vals_ptr=cell_par)
592 12 : ALLOCATE (cell_par(3))
593 48 : cell_par = hmat(:, 3)
594 12 : CALL section_vals_val_set(cell_section, "C", r_vals_ptr=cell_par)
595 : ! Unset possible keywords
596 12 : CALL section_vals_val_unset(cell_section, "ABC")
597 12 : CALL section_vals_val_unset(cell_section, "ALPHA_BETA_GAMMA")
598 :
599 84 : END SUBROUTINE read_cell_from_external_file
600 :
601 : ! **************************************************************************************************
602 : !> \brief Reads cell information from CIF file
603 : !> \param cif_file_name ...
604 : !> \param cell ...
605 : !> \param para_env ...
606 : !> \date 12.2008
607 : !> \par Format Information implemented:
608 : !> _cell_length_a
609 : !> _cell_length_b
610 : !> _cell_length_c
611 : !> _cell_angle_alpha
612 : !> _cell_angle_beta
613 : !> _cell_angle_gamma
614 : !>
615 : !> \author Teodoro Laino [tlaino]
616 : !> moved from topology_cif (1/2019 JHU)
617 : ! **************************************************************************************************
618 48 : SUBROUTINE read_cell_cif(cif_file_name, cell, para_env)
619 :
620 : CHARACTER(len=*) :: cif_file_name
621 : TYPE(cell_type), POINTER :: cell
622 : TYPE(mp_para_env_type), POINTER :: para_env
623 :
624 : CHARACTER(len=*), PARAMETER :: routineN = 'read_cell_cif'
625 :
626 : INTEGER :: handle
627 : INTEGER, DIMENSION(3) :: periodic
628 : LOGICAL :: found
629 : REAL(KIND=dp), DIMENSION(3) :: cell_angles, cell_lengths
630 : TYPE(cp_parser_type) :: parser
631 :
632 24 : CALL timeset(routineN, handle)
633 :
634 : CALL parser_create(parser, cif_file_name, &
635 24 : para_env=para_env, apply_preprocessing=.FALSE.)
636 :
637 : ! Parsing cell infos
638 96 : periodic = 1
639 : ! Check for _cell_length_a
640 : CALL parser_search_string(parser, "_cell_length_a", ignore_case=.FALSE., found=found, &
641 24 : begin_line=.FALSE., search_from_begin_of_file=.TRUE.)
642 24 : IF (.NOT. found) &
643 0 : CPABORT("The field (_cell_length_a) was not found in CIF file! ")
644 24 : CALL cif_get_real(parser, cell_lengths(1))
645 24 : cell_lengths(1) = cp_unit_to_cp2k(cell_lengths(1), "angstrom")
646 :
647 : ! Check for _cell_length_b
648 : CALL parser_search_string(parser, "_cell_length_b", ignore_case=.FALSE., found=found, &
649 24 : begin_line=.FALSE., search_from_begin_of_file=.TRUE.)
650 24 : IF (.NOT. found) &
651 0 : CPABORT("The field (_cell_length_b) was not found in CIF file! ")
652 24 : CALL cif_get_real(parser, cell_lengths(2))
653 24 : cell_lengths(2) = cp_unit_to_cp2k(cell_lengths(2), "angstrom")
654 :
655 : ! Check for _cell_length_c
656 : CALL parser_search_string(parser, "_cell_length_c", ignore_case=.FALSE., found=found, &
657 24 : begin_line=.FALSE., search_from_begin_of_file=.TRUE.)
658 24 : IF (.NOT. found) &
659 0 : CPABORT("The field (_cell_length_c) was not found in CIF file! ")
660 24 : CALL cif_get_real(parser, cell_lengths(3))
661 24 : cell_lengths(3) = cp_unit_to_cp2k(cell_lengths(3), "angstrom")
662 :
663 : ! Check for _cell_angle_alpha
664 : CALL parser_search_string(parser, "_cell_angle_alpha", ignore_case=.FALSE., found=found, &
665 24 : begin_line=.FALSE., search_from_begin_of_file=.TRUE.)
666 24 : IF (.NOT. found) &
667 0 : CPABORT("The field (_cell_angle_alpha) was not found in CIF file! ")
668 24 : CALL cif_get_real(parser, cell_angles(1))
669 24 : cell_angles(1) = cp_unit_to_cp2k(cell_angles(1), "deg")
670 :
671 : ! Check for _cell_angle_beta
672 : CALL parser_search_string(parser, "_cell_angle_beta", ignore_case=.FALSE., found=found, &
673 24 : begin_line=.FALSE., search_from_begin_of_file=.TRUE.)
674 24 : IF (.NOT. found) &
675 0 : CPABORT("The field (_cell_angle_beta) was not found in CIF file! ")
676 24 : CALL cif_get_real(parser, cell_angles(2))
677 24 : cell_angles(2) = cp_unit_to_cp2k(cell_angles(2), "deg")
678 :
679 : ! Check for _cell_angle_gamma
680 : CALL parser_search_string(parser, "_cell_angle_gamma", ignore_case=.FALSE., found=found, &
681 24 : begin_line=.FALSE., search_from_begin_of_file=.TRUE.)
682 24 : IF (.NOT. found) &
683 0 : CPABORT("The field (_cell_angle_gamma) was not found in CIF file! ")
684 24 : CALL cif_get_real(parser, cell_angles(3))
685 24 : cell_angles(3) = cp_unit_to_cp2k(cell_angles(3), "deg")
686 :
687 : ! Create cell
688 : CALL set_cell_param(cell, cell_lengths, cell_angles, periodic=periodic, &
689 24 : do_init_cell=.TRUE.)
690 :
691 24 : CALL parser_release(parser)
692 :
693 24 : CALL timestop(handle)
694 :
695 72 : END SUBROUTINE read_cell_cif
696 :
697 : ! **************************************************************************************************
698 : !> \brief Reads REAL from the CIF file.. This wrapper is needed in order to
699 : !> treat properly the accuracy specified in the CIF file, i.e. 3.45(6)
700 : !> \param parser ...
701 : !> \param r ...
702 : !> \date 12.2008
703 : !> \author Teodoro Laino [tlaino]
704 : ! **************************************************************************************************
705 144 : SUBROUTINE cif_get_real(parser, r)
706 :
707 : TYPE(cp_parser_type), INTENT(INOUT) :: parser
708 : REAL(KIND=dp), INTENT(OUT) :: r
709 :
710 : CHARACTER(LEN=default_string_length) :: s_tag
711 : INTEGER :: iln
712 :
713 144 : CALL parser_get_object(parser, s_tag)
714 144 : iln = LEN_TRIM(s_tag)
715 144 : IF (INDEX(s_tag, "(") /= 0) iln = INDEX(s_tag, "(") - 1
716 144 : READ (s_tag(1:iln), *) r
717 :
718 144 : END SUBROUTINE cif_get_real
719 :
720 : ! **************************************************************************************************
721 : !> \brief Write the cell parameters to the output unit.
722 : !> \param cell ...
723 : !> \param subsys_section ...
724 : !> \param tag ...
725 : !> \date 02.06.2000
726 : !> \par History
727 : !> - 11.2008 Teodoro Laino [tlaino] - rewrite and enabling user driven units
728 : !> \author Matthias Krack
729 : !> \version 1.0
730 : ! **************************************************************************************************
731 36737 : SUBROUTINE write_cell(cell, subsys_section, tag)
732 :
733 : TYPE(cell_type), POINTER :: cell
734 : TYPE(section_vals_type), POINTER :: subsys_section
735 : CHARACTER(LEN=*), INTENT(IN), OPTIONAL :: tag
736 :
737 : CHARACTER(LEN=default_string_length) :: label, unit_str
738 : INTEGER :: output_unit
739 : TYPE(cp_logger_type), POINTER :: logger
740 :
741 36737 : NULLIFY (logger)
742 36737 : logger => cp_get_default_logger()
743 36737 : IF (PRESENT(tag)) THEN
744 9736 : label = TRIM(tag)//"|"
745 : ELSE
746 27001 : label = TRIM(cell%tag)//"|"
747 : END IF
748 :
749 36737 : output_unit = cp_print_key_unit_nr(logger, subsys_section, "PRINT%CELL", extension=".Log")
750 36737 : CALL section_vals_val_get(subsys_section, "PRINT%CELL%UNIT", c_val=unit_str)
751 36737 : CALL write_cell_low(cell, unit_str, output_unit, label)
752 36737 : CALL cp_print_key_finished_output(output_unit, logger, subsys_section, "PRINT%CELL")
753 :
754 36737 : END SUBROUTINE write_cell
755 :
756 : ! **************************************************************************************************
757 : !> \brief Write the cell parameters to the output unit
758 : !> \param cell ...
759 : !> \param unit_str ...
760 : !> \param output_unit ...
761 : !> \param label ...
762 : !> \date 17.05.2023
763 : !> \par History
764 : !> - Extracted from write_cell (17.05.2023, MK)
765 : !> \version 1.0
766 : ! **************************************************************************************************
767 36737 : SUBROUTINE write_cell_low(cell, unit_str, output_unit, label)
768 :
769 : TYPE(cell_type), POINTER :: cell
770 : CHARACTER(LEN=*), INTENT(IN) :: unit_str
771 : INTEGER, INTENT(IN) :: output_unit
772 : CHARACTER(LEN=*), INTENT(IN), OPTIONAL :: label
773 :
774 : CHARACTER(LEN=12) :: tag
775 : CHARACTER(LEN=3) :: string
776 : CHARACTER(LEN=default_string_length) :: my_label
777 : REAL(KIND=dp) :: alpha, beta, gamma, val
778 : REAL(KIND=dp), DIMENSION(3) :: abc
779 : TYPE(enumeration_type), POINTER :: enum
780 : TYPE(keyword_type), POINTER :: keyword
781 : TYPE(section_type), POINTER :: section
782 :
783 36737 : NULLIFY (enum)
784 36737 : NULLIFY (keyword)
785 36737 : NULLIFY (section)
786 :
787 48105 : IF (output_unit > 0) THEN
788 11368 : CALL get_cell(cell=cell, abc=abc, alpha=alpha, beta=beta, gamma=gamma, tag=tag)
789 11368 : IF (PRESENT(label)) THEN
790 11368 : my_label = label
791 : ELSE
792 0 : my_label = TRIM(tag)//"|"
793 : END IF
794 11368 : val = cp_unit_from_cp2k(cell%deth, TRIM(unit_str)//"^3")
795 : WRITE (UNIT=output_unit, FMT="(/,T2,A,T61,F20.6)") &
796 11368 : TRIM(my_label)//" Volume ["//TRIM(unit_str)//"^3]:", val
797 11368 : val = cp_unit_from_cp2k(1.0_dp, TRIM(unit_str))
798 : WRITE (UNIT=output_unit, FMT="(T2,A,T30,3F10.3,3X,A6,F12.6)") &
799 45472 : TRIM(my_label)//" Vector a ["//TRIM(unit_str)//"]:", cell%hmat(:, 1)*val, &
800 11368 : "|a| = ", abc(1)*val, &
801 45472 : TRIM(my_label)//" Vector b ["//TRIM(unit_str)//"]:", cell%hmat(:, 2)*val, &
802 11368 : "|b| = ", abc(2)*val, &
803 45472 : TRIM(my_label)//" Vector c ["//TRIM(unit_str)//"]:", cell%hmat(:, 3)*val, &
804 22736 : "|c| = ", abc(3)*val
805 : WRITE (UNIT=output_unit, FMT="(T2,A,T69,F12.6)") &
806 11368 : TRIM(my_label)//" Angle (b,c), alpha [degree]: ", alpha, &
807 11368 : TRIM(my_label)//" Angle (a,c), beta [degree]: ", beta, &
808 22736 : TRIM(my_label)//" Angle (a,b), gamma [degree]: ", gamma
809 11368 : IF (cell%symmetry_id /= cell_sym_none) THEN
810 1464 : CALL create_cell_section(section)
811 1464 : keyword => section_get_keyword(section, "SYMMETRY")
812 1464 : CALL keyword_get(keyword, enum=enum)
813 : WRITE (UNIT=output_unit, FMT="(T2,A,T61,A20)") &
814 1464 : TRIM(my_label)//" Requested initial symmetry: ", &
815 2928 : ADJUSTR(TRIM(enum_i2c(enum, cell%symmetry_id)))
816 1464 : CALL section_release(section)
817 : END IF
818 11368 : IF (cell%orthorhombic) THEN
819 : WRITE (UNIT=output_unit, FMT="(T2,A,T78,A3)") &
820 8007 : TRIM(my_label)//" Numerically orthorhombic: ", "YES"
821 : ELSE
822 : WRITE (UNIT=output_unit, FMT="(T2,A,T78,A3)") &
823 3361 : TRIM(my_label)//" Numerically orthorhombic: ", " NO"
824 : END IF
825 45472 : IF (SUM(cell%perd(1:3)) == 0) THEN
826 : WRITE (UNIT=output_unit, FMT="(T2,A,T77,A4)") &
827 2337 : TRIM(my_label)//" Periodicity", "NONE"
828 : ELSE
829 9031 : string = ""
830 9031 : IF (cell%perd(1) == 1) string = TRIM(string)//"X"
831 9031 : IF (cell%perd(2) == 1) string = TRIM(string)//"Y"
832 9031 : IF (cell%perd(3) == 1) string = TRIM(string)//"Z"
833 : WRITE (UNIT=output_unit, FMT="(T2,A,T78,A3)") &
834 9031 : TRIM(my_label)//" Periodicity", ADJUSTR(string)
835 : END IF
836 : END IF
837 :
838 36737 : END SUBROUTINE write_cell_low
839 :
840 : END MODULE cell_methods
|