(git:27c966b)
Loading...
Searching...
No Matches
cell_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 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 from cell_types.F here.
13!> \author Matthias KracK (16.01.2002, based on a earlier version of CJM, JGH)
14! **************************************************************************************************
16 USE cp_units, ONLY: cp_unit_to_cp2k
17 USE kinds, ONLY: dp
18 USE mathconstants, ONLY: degree
19 USE mathlib, ONLY: angle
20#include "../base/base_uses.f90"
21
22 IMPLICIT NONE
23
24 PRIVATE
25
26 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cell_types'
27
28 ! Impose cell symmetry
29 INTEGER, PARAMETER, PUBLIC :: cell_sym_none = 0, &
41
42 INTEGER, PARAMETER, PUBLIC :: use_perd_none = 0, &
43 use_perd_x = 1, &
44 use_perd_y = 2, &
45 use_perd_z = 3, &
46 use_perd_xy = 4, &
47 use_perd_xz = 5, &
48 use_perd_yz = 6, &
49 use_perd_xyz = 7
50
51 CHARACTER(LEN=3), DIMENSION(7), &
52 PARAMETER, PUBLIC :: periodicity_string = [" X", " Y", " Z", &
53 " XY", " XZ", " YZ", &
54 "XYZ"]
55
56! **************************************************************************************************
57!> \brief Type defining parameters related to the simulation cell
58!> \version 1.0
59! **************************************************************************************************
61 CHARACTER(LEN=12) :: tag = "CELL"
62 INTEGER :: ref_count = -1, &
63 symmetry_id = use_perd_none
64 LOGICAL :: orthorhombic = .false. ! actually means a diagonal hmat
65 LOGICAL :: input_cell_canonicalized = .false.
66 REAL(kind=dp) :: deth = 0.0_dp
67 INTEGER, DIMENSION(3) :: perd = -1
68 REAL(kind=dp), DIMENSION(3, 3) :: hmat = 0.0_dp, &
69 h_inv = 0.0_dp, &
70 input_hmat = 0.0_dp, &
71 input_to_canonical = 0.0_dp, &
72 input_recip_to_canonical = 0.0_dp
73 END TYPE cell_type
74
76 TYPE(cell_type), POINTER :: cell => null()
77 END TYPE cell_p_type
78
79 ! Public data types
80 PUBLIC :: cell_type, &
82
83 ! Public subroutines
84 PUBLIC :: cell_clone, &
85 cell_copy, &
90 get_cell, &
93
94#if defined (__PLUMED2)
95 PUBLIC :: pbc_cp2k_plumed_getset_cell
96#endif
97
98 ! Public functions
99 PUBLIC :: plane_distance, &
100 pbc, &
101 pbc_stable, &
104
105 INTERFACE pbc
106 MODULE PROCEDURE pbc1, pbc2, pbc3, pbc4
107 END INTERFACE
108
109CONTAINS
110
111! **************************************************************************************************
112!> \brief Select a stable periodic image index close to half-cell boundaries.
113!> \param s Scaled coordinate
114!> \return image_shift ...
115! **************************************************************************************************
116 PURE ELEMENTAL FUNCTION pbc_image_shift(s) RESULT(image_shift)
117
118 REAL(KIND=dp), INTENT(IN) :: s
119 REAL(KIND=dp) :: image_shift
120
121 REAL(KIND=dp) :: half_boundary, tolerance
122
123 image_shift = anint(s)
124 half_boundary = anint(s - 0.5_dp) + 0.5_dp
125 tolerance = min(1.0e-8_dp, 64.0_dp*epsilon(1.0_dp)*max(1.0_dp, abs(s)))
126 IF (abs(s - half_boundary) <= tolerance) THEN
127 ! Select the lower side for every lattice-equivalent half-cell boundary.
128 image_shift = half_boundary + 0.5_dp
129 END IF
130
131 END FUNCTION pbc_image_shift
132
133! **************************************************************************************************
134!> \brief Clone cell variable
135!> \param cell_in Cell variable to be clone
136!> \param cell_out Cloned cell variable
137!> \param tag Optional new tag for cloned cell variable
138!> \par History
139!> - Optional tag added (17.05.2023, MK)
140! **************************************************************************************************
141 SUBROUTINE cell_clone(cell_in, cell_out, tag)
142
143 TYPE(cell_type), POINTER :: cell_in, cell_out
144 CHARACTER(LEN=*), INTENT(IN), OPTIONAL :: tag
145
146 cell_out = cell_in
147 cell_out%ref_count = 1
148 IF (PRESENT(tag)) cell_out%tag = tag
149
150 END SUBROUTINE cell_clone
151
152! **************************************************************************************************
153!> \brief Copy cell variable
154!> \param cell_in Cell variable to be copied
155!> \param cell_out Copy of cell variable
156!> \param tag Optional new tag
157!> \par History
158!> - Optional tag added (17.05.2023, MK)
159! **************************************************************************************************
160 SUBROUTINE cell_copy(cell_in, cell_out, tag)
161
162 TYPE(cell_type), POINTER :: cell_in, cell_out
163 CHARACTER(LEN=*), INTENT(IN), OPTIONAL :: tag
164
165 cell_out%deth = cell_in%deth
166 cell_out%perd = cell_in%perd
167 cell_out%hmat = cell_in%hmat
168 cell_out%h_inv = cell_in%h_inv
169 cell_out%input_cell_canonicalized = cell_in%input_cell_canonicalized
170 cell_out%input_hmat = cell_in%input_hmat
171 cell_out%input_to_canonical = cell_in%input_to_canonical
172 cell_out%input_recip_to_canonical = cell_in%input_recip_to_canonical
173 cell_out%orthorhombic = cell_in%orthorhombic
174 cell_out%symmetry_id = cell_in%symmetry_id
175 IF (PRESENT(tag)) THEN
176 cell_out%tag = tag
177 ELSE
178 cell_out%tag = cell_in%tag
179 END IF
180
181 END SUBROUTINE cell_copy
182
183! **************************************************************************************************
184!> \brief Read cell info from a line (parsed from a file)
185!> \param input_line ...
186!> \param cell_itimes ...
187!> \param cell_time ...
188!> \param h ...
189!> \param vol ...
190!> \date 19.02.2008
191!> \author Teodoro Laino [tlaino] - University of Zurich
192!> \version 1.0
193! **************************************************************************************************
194 SUBROUTINE parse_cell_line(input_line, cell_itimes, cell_time, h, vol)
195
196 CHARACTER(LEN=*), INTENT(IN) :: input_line
197 INTEGER, INTENT(OUT) :: cell_itimes
198 REAL(kind=dp), INTENT(OUT) :: cell_time
199 REAL(kind=dp), DIMENSION(3, 3), INTENT(OUT) :: h
200 REAL(kind=dp), INTENT(OUT) :: vol
201
202 INTEGER :: i, j
203
204 READ (input_line, *) cell_itimes, cell_time, &
205 h(1, 1), h(2, 1), h(3, 1), h(1, 2), h(2, 2), h(3, 2), h(1, 3), h(2, 3), h(3, 3), vol
206 DO i = 1, 3
207 DO j = 1, 3
208 h(j, i) = cp_unit_to_cp2k(h(j, i), "angstrom")
209 END DO
210 END DO
211
212 END SUBROUTINE parse_cell_line
213
214! **************************************************************************************************
215!> \brief Get informations about a simulation cell.
216!> \param cell ...
217!> \param alpha ...
218!> \param beta ...
219!> \param gamma ...
220!> \param deth ...
221!> \param orthorhombic ...
222!> \param abc ...
223!> \param periodic ...
224!> \param h ...
225!> \param h_inv ...
226!> \param symmetry_id ...
227!> \param tag ...
228!> \date 16.01.2002
229!> \author Matthias Krack
230!> \version 1.0
231! **************************************************************************************************
232 SUBROUTINE get_cell(cell, alpha, beta, gamma, deth, orthorhombic, abc, periodic, &
233 h, h_inv, symmetry_id, tag)
234
235 TYPE(cell_type), POINTER :: cell
236 REAL(kind=dp), INTENT(OUT), OPTIONAL :: alpha, beta, gamma, deth
237 LOGICAL, INTENT(OUT), OPTIONAL :: orthorhombic
238 REAL(kind=dp), DIMENSION(3), INTENT(OUT), OPTIONAL :: abc
239 INTEGER, DIMENSION(3), INTENT(OUT), OPTIONAL :: periodic
240 REAL(kind=dp), DIMENSION(3, 3), INTENT(OUT), &
241 OPTIONAL :: h, h_inv
242 INTEGER, INTENT(OUT), OPTIONAL :: symmetry_id
243 CHARACTER(LEN=*), INTENT(OUT), OPTIONAL :: tag
244
245 cpassert(ASSOCIATED(cell))
246
247 IF (PRESENT(deth)) deth = cell%deth ! the volume
248 IF (PRESENT(orthorhombic)) orthorhombic = cell%orthorhombic
249 IF (PRESENT(periodic)) periodic(:) = cell%perd(:)
250 IF (PRESENT(h)) h(:, :) = cell%hmat(:, :)
251 IF (PRESENT(h_inv)) h_inv(:, :) = cell%h_inv(:, :)
252
253 ! Calculate the lengths of the cell vectors a, b, and c
254 IF (PRESENT(abc)) THEN
255 abc(1) = sqrt(cell%hmat(1, 1)*cell%hmat(1, 1) + &
256 cell%hmat(2, 1)*cell%hmat(2, 1) + &
257 cell%hmat(3, 1)*cell%hmat(3, 1))
258 abc(2) = sqrt(cell%hmat(1, 2)*cell%hmat(1, 2) + &
259 cell%hmat(2, 2)*cell%hmat(2, 2) + &
260 cell%hmat(3, 2)*cell%hmat(3, 2))
261 abc(3) = sqrt(cell%hmat(1, 3)*cell%hmat(1, 3) + &
262 cell%hmat(2, 3)*cell%hmat(2, 3) + &
263 cell%hmat(3, 3)*cell%hmat(3, 3))
264 END IF
265
266 ! Angles between the cell vectors a, b, and c
267 ! alpha = <(b,c)
268 IF (PRESENT(alpha)) alpha = angle(cell%hmat(:, 2), cell%hmat(:, 3))*degree
269 ! beta = <(a,c)
270 IF (PRESENT(beta)) beta = angle(cell%hmat(:, 1), cell%hmat(:, 3))*degree
271 ! gamma = <(a,b)
272 IF (PRESENT(gamma)) gamma = angle(cell%hmat(:, 1), cell%hmat(:, 2))*degree
273 IF (PRESENT(symmetry_id)) symmetry_id = cell%symmetry_id
274 IF (PRESENT(tag)) tag = cell%tag
275
276 END SUBROUTINE get_cell
277
278! **************************************************************************************************
279!> \brief Transform a Cartesian real-space vector from the user input cell frame
280!> into CP2K's canonical internal cell frame.
281!> \param cell ...
282!> \param vector ...
283! **************************************************************************************************
284 SUBROUTINE cell_transform_input_cartesian(cell, vector)
285
286 TYPE(cell_type), POINTER :: cell
287 REAL(kind=dp), DIMENSION(3), INTENT(INOUT) :: vector
288
289 cpassert(ASSOCIATED(cell))
290
291 IF (cell%input_cell_canonicalized) vector = matmul(cell%input_to_canonical, vector)
292
293 END SUBROUTINE cell_transform_input_cartesian
294
295! **************************************************************************************************
296!> \brief Transform a Cartesian reciprocal-space vector from the user input cell
297!> frame into CP2K's canonical internal cell frame.
298!> \param cell ...
299!> \param vector ...
300! **************************************************************************************************
301 SUBROUTINE cell_transform_input_reciprocal(cell, vector)
302
303 TYPE(cell_type), POINTER :: cell
304 REAL(kind=dp), DIMENSION(3), INTENT(INOUT) :: vector
305
306 cpassert(ASSOCIATED(cell))
307
308 IF (cell%input_cell_canonicalized) vector = matmul(cell%input_recip_to_canonical, vector)
309
311
312! **************************************************************************************************
313!> \brief Calculate the distance between two lattice planes as defined by
314!> a triple of Miller indices (hkl).
315!> \param h ...
316!> \param k ...
317!> \param l ...
318!> \param cell ...
319!> \return ...
320!> \date 18.11.2004
321!> \author Matthias Krack
322!> \version 1.0
323! **************************************************************************************************
324 FUNCTION plane_distance(h, k, l, cell) RESULT(distance)
325
326 INTEGER, INTENT(IN) :: h, k, l
327 TYPE(cell_type), POINTER :: cell
328 REAL(kind=dp) :: distance
329
330 REAL(kind=dp) :: a, alpha, b, beta, c, cosa, cosb, cosg, &
331 d, gamma, x, y, z
332 REAL(kind=dp), DIMENSION(3) :: abc
333
334 x = real(h, kind=dp)
335 y = real(k, kind=dp)
336 z = real(l, kind=dp)
337
338 CALL get_cell(cell=cell, abc=abc)
339
340 a = abc(1)
341 b = abc(2)
342 c = abc(3)
343
344 IF (cell%orthorhombic) THEN
345
346 d = (x/a)**2 + (y/b)**2 + (z/c)**2
347
348 ELSE
349
350 CALL get_cell(cell=cell, &
351 alpha=alpha, &
352 beta=beta, &
353 gamma=gamma)
354
355 alpha = alpha/degree
356 beta = beta/degree
358
359 cosa = cos(alpha)
360 cosb = cos(beta)
361 cosg = cos(gamma)
362
363 d = ((x*b*c*sin(alpha))**2 + &
364 (y*c*a*sin(beta))**2 + &
365 (z*a*b*sin(gamma))**2 + &
366 2.0_dp*a*b*c*(x*y*c*(cosa*cosb - cosg) + &
367 z*x*b*(cosg*cosa - cosb) + &
368 y*z*a*(cosb*cosg - cosa)))/ &
369 ((a*b*c)**2*(1.0_dp - cosa**2 - cosb**2 - cosg**2 + &
370 2.0_dp*cosa*cosb*cosg))
371
372 END IF
373
374 distance = 1.0_dp/sqrt(d)
375
376 END FUNCTION plane_distance
377
378! **************************************************************************************************
379!> \brief Apply the periodic boundary conditions defined by a simulation
380!> cell to a position vector r.
381!> \param r ...
382!> \param cell ...
383!> \return ...
384!> \date 16.01.2002
385!> \author Matthias Krack
386!> \version 1.0
387! **************************************************************************************************
388 FUNCTION pbc1(r, cell) RESULT(r_pbc)
389
390 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: r
391 TYPE(cell_type), POINTER :: cell
392 REAL(kind=dp), DIMENSION(3) :: r_pbc
393
394 REAL(kind=dp), DIMENSION(3) :: s
395
396 cpassert(ASSOCIATED(cell))
397
398 IF (cell%orthorhombic) THEN
399 r_pbc(1) = r(1) - cell%hmat(1, 1)*cell%perd(1)*anint(cell%h_inv(1, 1)*r(1))
400 r_pbc(2) = r(2) - cell%hmat(2, 2)*cell%perd(2)*anint(cell%h_inv(2, 2)*r(2))
401 r_pbc(3) = r(3) - cell%hmat(3, 3)*cell%perd(3)*anint(cell%h_inv(3, 3)*r(3))
402 ELSE
403 s(1) = cell%h_inv(1, 1)*r(1) + cell%h_inv(1, 2)*r(2) + cell%h_inv(1, 3)*r(3)
404 s(2) = cell%h_inv(2, 1)*r(1) + cell%h_inv(2, 2)*r(2) + cell%h_inv(2, 3)*r(3)
405 s(3) = cell%h_inv(3, 1)*r(1) + cell%h_inv(3, 2)*r(2) + cell%h_inv(3, 3)*r(3)
406 s(1) = s(1) - cell%perd(1)*anint(s(1))
407 s(2) = s(2) - cell%perd(2)*anint(s(2))
408 s(3) = s(3) - cell%perd(3)*anint(s(3))
409 r_pbc(1) = cell%hmat(1, 1)*s(1) + cell%hmat(1, 2)*s(2) + cell%hmat(1, 3)*s(3)
410 r_pbc(2) = cell%hmat(2, 1)*s(1) + cell%hmat(2, 2)*s(2) + cell%hmat(2, 3)*s(3)
411 r_pbc(3) = cell%hmat(3, 1)*s(1) + cell%hmat(3, 2)*s(2) + cell%hmat(3, 3)*s(3)
412 END IF
413
414 END FUNCTION pbc1
415
416! **************************************************************************************************
417!> \brief Distribute an atom-pair contribution equally among its nearest candidate images.
418!> Images within 1.0E-6 of the minimum distance receive normalized equal weights.
419!> \param ra Position of the first atom.
420!> \param rb Position of the second atom.
421!> \param cell Simulation cell used to wrap the positions and translate the images.
422!> \param index_to_cell Candidate lattice translations, indexed (direction, image).
423!> \param weights Normalized image weights; non-nearest images receive zero.
424! **************************************************************************************************
425 SUBROUTINE minimum_image_weights(ra, rb, cell, index_to_cell, weights)
426 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: ra, rb
427 TYPE(cell_type), INTENT(IN), POINTER :: cell
428 INTEGER, DIMENSION(:, :), INTENT(IN) :: index_to_cell
429 REAL(kind=dp), DIMENSION(:), INTENT(OUT) :: weights
430
431 INTEGER :: i_image
432 REAL(kind=dp), DIMENSION(3) :: ra_pbc, rab, rb_pbc
433 REAL(kind=dp), DIMENSION(SIZE(index_to_cell, 2)) :: distances
434
435 cpassert(SIZE(index_to_cell, 1) == 3)
436 cpassert(SIZE(index_to_cell, 2) > 0)
437 cpassert(SIZE(weights) == SIZE(index_to_cell, 2))
438
439 ra_pbc = pbc(ra, cell)
440 rb_pbc = pbc(rb, cell)
441 DO i_image = 1, SIZE(index_to_cell, 2)
442 rab = ra_pbc - (rb_pbc + matmul(cell%hmat, real(index_to_cell(:, i_image), kind=dp)))
443 distances(i_image) = norm2(rab)
444 END DO
445
446 weights = merge(1.0_dp, 0.0_dp, distances <= minval(distances) + 1.0e-6_dp)
447 weights = weights/sum(weights)
448
449 END SUBROUTINE minimum_image_weights
450
451! **************************************************************************************************
452!> \brief Apply a stable periodic-image convention for k-point Bloch gauges.
453!> \param r ...
454!> \param cell ...
455!> \return r_pbc ...
456! **************************************************************************************************
457 FUNCTION pbc_stable(r, cell) RESULT(r_pbc)
458
459 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: r
460 TYPE(cell_type), POINTER :: cell
461 REAL(kind=dp), DIMENSION(3) :: r_pbc
462
463 REAL(kind=dp), DIMENSION(3) :: s
464
465 cpassert(ASSOCIATED(cell))
466
467 IF (cell%orthorhombic) THEN
468 r_pbc(1) = r(1) - cell%hmat(1, 1)*cell%perd(1)* &
469 pbc_image_shift(cell%h_inv(1, 1)*r(1))
470 r_pbc(2) = r(2) - cell%hmat(2, 2)*cell%perd(2)* &
471 pbc_image_shift(cell%h_inv(2, 2)*r(2))
472 r_pbc(3) = r(3) - cell%hmat(3, 3)*cell%perd(3)* &
473 pbc_image_shift(cell%h_inv(3, 3)*r(3))
474 ELSE
475 s(1) = cell%h_inv(1, 1)*r(1) + cell%h_inv(1, 2)*r(2) + cell%h_inv(1, 3)*r(3)
476 s(2) = cell%h_inv(2, 1)*r(1) + cell%h_inv(2, 2)*r(2) + cell%h_inv(2, 3)*r(3)
477 s(3) = cell%h_inv(3, 1)*r(1) + cell%h_inv(3, 2)*r(2) + cell%h_inv(3, 3)*r(3)
478 s(1) = s(1) - cell%perd(1)*pbc_image_shift(s(1))
479 s(2) = s(2) - cell%perd(2)*pbc_image_shift(s(2))
480 s(3) = s(3) - cell%perd(3)*pbc_image_shift(s(3))
481 r_pbc(1) = cell%hmat(1, 1)*s(1) + cell%hmat(1, 2)*s(2) + cell%hmat(1, 3)*s(3)
482 r_pbc(2) = cell%hmat(2, 1)*s(1) + cell%hmat(2, 2)*s(2) + cell%hmat(2, 3)*s(3)
483 r_pbc(3) = cell%hmat(3, 1)*s(1) + cell%hmat(3, 2)*s(2) + cell%hmat(3, 3)*s(3)
484 END IF
485
486 END FUNCTION pbc_stable
487
488! **************************************************************************************************
489!> \brief Apply the periodic boundary conditions defined by a simulation
490!> cell to a position vector r subtracting nl from the periodic images
491!> \param r ...
492!> \param cell ...
493!> \param nl ...
494!> \return ...
495!> \date 16.01.2002
496!> \author Matthias Krack
497!> \version 1.0
498! **************************************************************************************************
499 FUNCTION pbc2(r, cell, nl) RESULT(r_pbc)
500
501 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: r
502 TYPE(cell_type), POINTER :: cell
503 INTEGER, DIMENSION(3), INTENT(IN) :: nl
504 REAL(kind=dp), DIMENSION(3) :: r_pbc
505
506 REAL(kind=dp), DIMENSION(3) :: s
507
508 cpassert(ASSOCIATED(cell))
509
510 IF (cell%orthorhombic) THEN
511 r_pbc(1) = r(1) - cell%hmat(1, 1)*cell%perd(1)* &
512 REAL(nint(cell%h_inv(1, 1)*r(1)) - nl(1), dp)
513 r_pbc(2) = r(2) - cell%hmat(2, 2)*cell%perd(2)* &
514 REAL(nint(cell%h_inv(2, 2)*r(2)) - nl(2), dp)
515 r_pbc(3) = r(3) - cell%hmat(3, 3)*cell%perd(3)* &
516 REAL(nint(cell%h_inv(3, 3)*r(3)) - nl(3), dp)
517 ELSE
518 s(1) = cell%h_inv(1, 1)*r(1) + cell%h_inv(1, 2)*r(2) + cell%h_inv(1, 3)*r(3)
519 s(2) = cell%h_inv(2, 1)*r(1) + cell%h_inv(2, 2)*r(2) + cell%h_inv(2, 3)*r(3)
520 s(3) = cell%h_inv(3, 1)*r(1) + cell%h_inv(3, 2)*r(2) + cell%h_inv(3, 3)*r(3)
521 s(1) = s(1) - cell%perd(1)*real(nint(s(1)) - nl(1), dp)
522 s(2) = s(2) - cell%perd(2)*real(nint(s(2)) - nl(2), dp)
523 s(3) = s(3) - cell%perd(3)*real(nint(s(3)) - nl(3), dp)
524 r_pbc(1) = cell%hmat(1, 1)*s(1) + cell%hmat(1, 2)*s(2) + cell%hmat(1, 3)*s(3)
525 r_pbc(2) = cell%hmat(2, 1)*s(1) + cell%hmat(2, 2)*s(2) + cell%hmat(2, 3)*s(3)
526 r_pbc(3) = cell%hmat(3, 1)*s(1) + cell%hmat(3, 2)*s(2) + cell%hmat(3, 3)*s(3)
527 END IF
528
529 END FUNCTION pbc2
530
531! **************************************************************************************************
532!> \brief Apply the periodic boundary conditions defined by the simulation
533!> cell cell to the vector pointing from atom a to atom b.
534!> \param ra ...
535!> \param rb ...
536!> \param cell ...
537!> \return ...
538!> \date 11.03.2004
539!> \author Matthias Krack
540!> \version 1.0
541! **************************************************************************************************
542 FUNCTION pbc3(ra, rb, cell) RESULT(rab_pbc)
543
544 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: ra, rb
545 TYPE(cell_type), POINTER :: cell
546 REAL(kind=dp), DIMENSION(3) :: rab_pbc
547
548 INTEGER :: icell, jcell, kcell
549 INTEGER, DIMENSION(3) :: periodic
550 REAL(kind=dp) :: rab2, rab2_pbc
551 REAL(kind=dp), DIMENSION(3) :: r, ra_pbc, rab, rb_image, rb_pbc, s2r
552
553 CALL get_cell(cell=cell, periodic=periodic)
554
555 ra_pbc(:) = pbc(ra(:), cell)
556 rb_pbc(:) = pbc(rb(:), cell)
557
558 rab2_pbc = huge(1.0_dp)
559
560 DO icell = -periodic(1), periodic(1)
561 DO jcell = -periodic(2), periodic(2)
562 DO kcell = -periodic(3), periodic(3)
563 r = real([icell, jcell, kcell], dp)
564 CALL scaled_to_real(s2r, r, cell)
565 rb_image(:) = rb_pbc(:) + s2r
566 rab(:) = rb_image(:) - ra_pbc(:)
567 rab2 = rab(1)*rab(1) + rab(2)*rab(2) + rab(3)*rab(3)
568 IF (rab2 < rab2_pbc) THEN
569 rab2_pbc = rab2
570 rab_pbc(:) = rab(:)
571 END IF
572 END DO
573 END DO
574 END DO
575
576 END FUNCTION pbc3
577
578 !if positive_range == true, r(i) (or s(i)) in range [0, hmat(i,i)],
579 !else, r(i) (s(i)) in range [-hmat(i,i)/2, hmat(i,i)/2]
580! **************************************************************************************************
581!> \brief ...
582!> \param r ...
583!> \param cell ...
584!> \param positive_range ...
585!> \return ...
586! **************************************************************************************************
587 FUNCTION pbc4(r, cell, positive_range) RESULT(r_pbc)
588
589 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: r
590 TYPE(cell_type), POINTER :: cell
591 LOGICAL :: positive_range
592 REAL(kind=dp), DIMENSION(3) :: r_pbc
593
594 REAL(kind=dp), DIMENSION(3) :: s
595
596 cpassert(ASSOCIATED(cell))
597
598 IF (positive_range) THEN
599 IF (cell%orthorhombic) THEN
600 r_pbc(1) = r(1) - cell%hmat(1, 1)*cell%perd(1)*floor(cell%h_inv(1, 1)*r(1))
601 r_pbc(2) = r(2) - cell%hmat(2, 2)*cell%perd(2)*floor(cell%h_inv(2, 2)*r(2))
602 r_pbc(3) = r(3) - cell%hmat(3, 3)*cell%perd(3)*floor(cell%h_inv(3, 3)*r(3))
603 ELSE
604 s(1) = cell%h_inv(1, 1)*r(1) + cell%h_inv(1, 2)*r(2) + cell%h_inv(1, 3)*r(3)
605 s(2) = cell%h_inv(2, 1)*r(1) + cell%h_inv(2, 2)*r(2) + cell%h_inv(2, 3)*r(3)
606 s(3) = cell%h_inv(3, 1)*r(1) + cell%h_inv(3, 2)*r(2) + cell%h_inv(3, 3)*r(3)
607 s(1) = s(1) - cell%perd(1)*floor(s(1))
608 s(2) = s(2) - cell%perd(2)*floor(s(2))
609 s(3) = s(3) - cell%perd(3)*floor(s(3))
610 r_pbc(1) = cell%hmat(1, 1)*s(1) + cell%hmat(1, 2)*s(2) + cell%hmat(1, 3)*s(3)
611 r_pbc(2) = cell%hmat(2, 1)*s(1) + cell%hmat(2, 2)*s(2) + cell%hmat(2, 3)*s(3)
612 r_pbc(3) = cell%hmat(3, 1)*s(1) + cell%hmat(3, 2)*s(2) + cell%hmat(3, 3)*s(3)
613 END IF
614 ELSE
615 r_pbc = pbc1(r, cell)
616 END IF
617
618 END FUNCTION pbc4
619
620! **************************************************************************************************
621!> \brief Transform real to scaled cell coordinates.
622!> s=h_inv*r
623!> \param s ...
624!> \param r ...
625!> \param cell ...
626!> \date 16.01.2002
627!> \author Matthias Krack
628!> \version 1.0
629! **************************************************************************************************
630 SUBROUTINE real_to_scaled(s, r, cell)
631
632 REAL(kind=dp), DIMENSION(3), INTENT(OUT) :: s
633 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: r
634 TYPE(cell_type), POINTER :: cell
635
636 cpassert(ASSOCIATED(cell))
637
638 IF (cell%orthorhombic) THEN
639 s(1) = cell%h_inv(1, 1)*r(1)
640 s(2) = cell%h_inv(2, 2)*r(2)
641 s(3) = cell%h_inv(3, 3)*r(3)
642 ELSE
643 s(1) = cell%h_inv(1, 1)*r(1) + cell%h_inv(1, 2)*r(2) + cell%h_inv(1, 3)*r(3)
644 s(2) = cell%h_inv(2, 1)*r(1) + cell%h_inv(2, 2)*r(2) + cell%h_inv(2, 3)*r(3)
645 s(3) = cell%h_inv(3, 1)*r(1) + cell%h_inv(3, 2)*r(2) + cell%h_inv(3, 3)*r(3)
646 END IF
647
648 END SUBROUTINE real_to_scaled
649
650! **************************************************************************************************
651!> \brief Transform scaled cell coordinates real coordinates.
652!> r=h*s
653!> \param r ...
654!> \param s ...
655!> \param cell ...
656!> \date 16.01.2002
657!> \author Matthias Krack
658!> \version 1.0
659! **************************************************************************************************
660 SUBROUTINE scaled_to_real(r, s, cell)
661
662 REAL(kind=dp), DIMENSION(3), INTENT(OUT) :: r
663 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: s
664 TYPE(cell_type), POINTER :: cell
665
666 cpassert(ASSOCIATED(cell))
667
668 IF (cell%orthorhombic) THEN
669 r(1) = cell%hmat(1, 1)*s(1)
670 r(2) = cell%hmat(2, 2)*s(2)
671 r(3) = cell%hmat(3, 3)*s(3)
672 ELSE
673 r(1) = cell%hmat(1, 1)*s(1) + cell%hmat(1, 2)*s(2) + cell%hmat(1, 3)*s(3)
674 r(2) = cell%hmat(2, 1)*s(1) + cell%hmat(2, 2)*s(2) + cell%hmat(2, 3)*s(3)
675 r(3) = cell%hmat(3, 1)*s(1) + cell%hmat(3, 2)*s(2) + cell%hmat(3, 3)*s(3)
676 END IF
677
678 END SUBROUTINE scaled_to_real
679! **************************************************************************************************
680!> \brief retains the given cell (see doc/ReferenceCounting.html)
681!> \param cell the cell to retain
682!> \par History
683!> 09.2003 created [fawzi]
684!> \author Fawzi Mohamed
685! **************************************************************************************************
686 SUBROUTINE cell_retain(cell)
687
688 TYPE(cell_type), POINTER :: cell
689
690 cpassert(ASSOCIATED(cell))
691 cpassert(cell%ref_count > 0)
692 cell%ref_count = cell%ref_count + 1
693
694 END SUBROUTINE cell_retain
695
696! **************************************************************************************************
697!> \brief releases the given cell (see doc/ReferenceCounting.html)
698!> \param cell the cell to release
699!> \par History
700!> 09.2003 created [fawzi]
701!> \author Fawzi Mohamed
702! **************************************************************************************************
703 SUBROUTINE cell_release(cell)
704
705 TYPE(cell_type), POINTER :: cell
706
707 IF (ASSOCIATED(cell)) THEN
708 cpassert(cell%ref_count > 0)
709 cell%ref_count = cell%ref_count - 1
710 IF (cell%ref_count == 0) THEN
711 DEALLOCATE (cell)
712 END IF
713 NULLIFY (cell)
714 END IF
715
716 END SUBROUTINE cell_release
717
718#if defined (__PLUMED2)
719! **************************************************************************************************
720!> \brief For the interface with plumed, pass a cell pointer and retrieve it
721!> later. It's a hack, but avoids passing the cell back and forth
722!> across the Fortran/C++ interface
723!> \param cell ...
724!> \param set ...
725!> \date 28.02.2013
726!> \author RK
727!> \version 1.0
728! **************************************************************************************************
729 SUBROUTINE pbc_cp2k_plumed_getset_cell(cell, set)
730
731 TYPE(cell_type), POINTER :: cell
732 LOGICAL :: set
733
734 TYPE(cell_type), POINTER, SAVE :: stored_cell
735
736 IF (set) THEN
737 stored_cell => cell
738 ELSE
739 cell => stored_cell
740 END IF
741
742 END SUBROUTINE pbc_cp2k_plumed_getset_cell
743#endif
744
745END MODULE cell_types
Handles all functions related to the CELL.
Definition cell_types.F:15
subroutine, public cell_transform_input_reciprocal(cell, vector)
Transform a Cartesian reciprocal-space vector from the user input cell frame into CP2K's canonical in...
Definition cell_types.F:302
subroutine, public scaled_to_real(r, s, cell)
Transform scaled cell coordinates real coordinates. r=h*s.
Definition cell_types.F:661
integer, parameter, public use_perd_xyz
Definition cell_types.F:42
subroutine, public parse_cell_line(input_line, cell_itimes, cell_time, h, vol)
Read cell info from a line (parsed from a file).
Definition cell_types.F:195
integer, parameter, public cell_sym_monoclinic
Definition cell_types.F:29
integer, parameter, public use_perd_y
Definition cell_types.F:42
integer, parameter, public cell_sym_triclinic
Definition cell_types.F:29
integer, parameter, public cell_sym_tetragonal_ab
Definition cell_types.F:29
integer, parameter, public use_perd_xz
Definition cell_types.F:42
integer, parameter, public cell_sym_rhombohedral
Definition cell_types.F:29
subroutine, public real_to_scaled(s, r, cell)
Transform real to scaled cell coordinates. s=h_inv*r.
Definition cell_types.F:631
subroutine, public cell_release(cell)
releases the given cell (see doc/ReferenceCounting.html)
Definition cell_types.F:704
integer, parameter, public use_perd_x
Definition cell_types.F:42
subroutine, public cell_clone(cell_in, cell_out, tag)
Clone cell variable.
Definition cell_types.F:142
integer, parameter, public cell_sym_tetragonal_ac
Definition cell_types.F:29
integer, parameter, public use_perd_z
Definition cell_types.F:42
integer, parameter, public use_perd_yz
Definition cell_types.F:42
subroutine, public get_cell(cell, alpha, beta, gamma, deth, orthorhombic, abc, periodic, h, h_inv, symmetry_id, tag)
Get informations about a simulation cell.
Definition cell_types.F:234
integer, parameter, public use_perd_none
Definition cell_types.F:42
subroutine, public cell_retain(cell)
retains the given cell (see doc/ReferenceCounting.html)
Definition cell_types.F:687
integer, parameter, public cell_sym_hexagonal_gamma_60
Definition cell_types.F:29
character(len=3), dimension(7), parameter, public periodicity_string
Definition cell_types.F:51
integer, parameter, public cell_sym_orthorhombic
Definition cell_types.F:29
integer, parameter, public cell_sym_none
Definition cell_types.F:29
integer, parameter, public cell_sym_hexagonal_gamma_120
Definition cell_types.F:29
subroutine, public cell_copy(cell_in, cell_out, tag)
Copy cell variable.
Definition cell_types.F:161
integer, parameter, public cell_sym_monoclinic_gamma_ab
Definition cell_types.F:29
integer, parameter, public cell_sym_cubic
Definition cell_types.F:29
subroutine, public minimum_image_weights(ra, rb, cell, index_to_cell, weights)
Distribute an atom-pair contribution equally among its nearest candidate images. Images within 1....
Definition cell_types.F:426
integer, parameter, public use_perd_xy
Definition cell_types.F:42
subroutine, public cell_transform_input_cartesian(cell, vector)
Transform a Cartesian real-space vector from the user input cell frame into CP2K's canonical internal...
Definition cell_types.F:285
real(kind=dp) function, dimension(3), public pbc_stable(r, cell)
Apply a stable periodic-image convention for k-point Bloch gauges.
Definition cell_types.F:458
integer, parameter, public cell_sym_tetragonal_bc
Definition cell_types.F:29
real(kind=dp) function, public plane_distance(h, k, l, cell)
Calculate the distance between two lattice planes as defined by a triple of Miller indices (hkl).
Definition cell_types.F:325
unit conversion facility
Definition cp_units.F:30
real(kind=dp) function, public cp_unit_to_cp2k(value, unit_str, defaults, power)
converts to the internal cp2k units to the given unit
Definition cp_units.F:1282
Calculation of the incomplete Gamma function F_n(t) for multi-center integrals over Cartesian Gaussia...
Definition gamma.F:15
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Definition of mathematical constants and functions.
real(kind=dp), parameter, public degree
Collection of simple mathematical functions and subroutines.
Definition mathlib.F:15
pure real(kind=dp) function, public angle(a, b)
Calculation of the angle between the vectors a and b. The angle is returned in radians.
Definition mathlib.F:186
Type defining parameters related to the simulation cell.
Definition cell_types.F:60