(git:27c966b)
Loading...
Searching...
No Matches
skala_periodic_partition_unittest.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
9 USE cell_types, ONLY: cell_type
10 USE kinds, ONLY: dp
11 USE libcp2k, ONLY: cp2k_finalize,&
13 USE mathlib, ONLY: inv_3x3
18
19 IMPLICIT NONE
20 TYPE(cell_type), POINTER :: cell, perturbed
21 TYPE(skala_periodic_partition_type) :: layout
22 REAL(dp) :: atoms(3, 2), trial(3, 2), hmat(3, 3), point(3), shifted(3)
23 REAL(dp) :: value, plus, minus, direct, total, datom(3, 2), dstrain(3, 3), mu
24 REAL(dp) :: unity_error, derivative_error, translation_error
25 REAL(dp), PARAMETER :: h = 2.e-6_dp
26 INTEGER :: mask, periodicity(3), a, d, j, k, l, TARGET, sample
27
28 CALL cp2k_init()
29 ALLOCATE (cell, perturbed)
30 hmat = reshape([4.0_dp, 0.1_dp, 0.2_dp, 0.7_dp, 5.0_dp, 0.3_dp, -0.2_dp, 0.4_dp, 6.0_dp], [3, 3])
31 atoms(:, 1) = [0.2_dp, 0.3_dp, -0.1_dp]
32 atoms(:, 2) = [1.8_dp, 2.2_dp, 1.3_dp]
33 unity_error = 0.0_dp
34 derivative_error = 0.0_dp
35 translation_error = 0.0_dp
36 DO mask = 0, 7
37 periodicity = [merge(1, 0, btest(mask, 0)), merge(1, 0, btest(mask, 1)), merge(1, 0, btest(mask, 2))]
38 cell%hmat = hmat
39 cell%h_inv = inv_3x3(hmat)
40 cell%perd = periodicity
41 perturbed%perd = periodicity
42 CALL build_periodic_atom_image_layout(atoms, cell, 1, periodicity, layout, TARGET)
43 DO sample = 1, 3
44 point = [1.1_dp, 1.3_dp, 1.7_dp]
45 IF (sample == 2) point = matmul(hmat, [0.5_dp, 0.2_dp, 0.1_dp])
46 IF (sample == 3) point = matmul(hmat, [1.5_dp, 0.2_dp, 0.1_dp]) + atoms(:, 1)
47 CALL periodic_atom_image_partition_from_layout(point, layout, TARGET, value, datom, dstrain)
48 CALL periodic_atom_image_partition(point, atoms, cell, 1, direct, image_periodicity=periodicity)
49 IF (abs(direct - value) > 2.e-14_dp) error stop 'Cached partition changed the value'
50 IF (mask == 0) THEN
51 mu = (norm2(point - atoms(:, 1)) - norm2(point - atoms(:, 2)))/norm2(atoms(:, 1) - atoms(:, 2))
52 DO j = 1, 3
53 mu = 1.5_dp*mu - 0.5_dp*mu**3
54 END DO
55 IF (abs(value - 0.5_dp*(1.0_dp - mu)) > 2.e-14_dp) error stop 'Nonperiodic Becke weight changed'
56 IF (maxval(abs(dstrain)) > 2.e-14_dp) error stop 'Molecular partition has an image-strain response'
57 END IF
58 total = 0.0_dp
59 DO a = 1, 2
60 DO l = -3*periodicity(3), 3*periodicity(3)
61 DO k = -3*periodicity(2), 3*periodicity(2)
62 DO j = -3*periodicity(1), 3*periodicity(1)
63 shifted = point - matmul(hmat, real([j, k, l], dp))
64 CALL periodic_atom_image_partition(shifted, atoms, cell, a, direct, image_periodicity=periodicity)
65 total = total + direct
66 END DO
67 END DO
68 END DO
69 END DO
70 unity_error = max(unity_error, abs(total - 1.0_dp))
71 trial = atoms
72 trial(:, 2) = atoms(:, 2) + matmul(hmat, real([1, -1, 2]*periodicity, dp))
73 CALL periodic_atom_image_partition(point, trial, cell, 1, direct, image_periodicity=periodicity)
74 translation_error = max(translation_error, abs(direct - value))
75 DO a = 1, 2
76 DO d = 1, 3
77 trial = atoms
78 trial(d, a) = atoms(d, a) + h
79 CALL periodic_atom_image_partition(point, trial, cell, 1, plus, image_periodicity=periodicity)
80 trial(d, a) = atoms(d, a) - h
81 CALL periodic_atom_image_partition(point, trial, cell, 1, minus, image_periodicity=periodicity)
82 derivative_error = max(derivative_error, abs((plus - minus)/(2*h) - datom(d, a)))
83 END DO
84 END DO
85 DO d = 1, 3
86 DO j = 1, 3
87 perturbed%hmat = hmat
88 perturbed%hmat(d, :) = hmat(d, :) + h*hmat(j, :)
89 perturbed%h_inv = inv_3x3(perturbed%hmat)
90 CALL periodic_atom_image_partition(point, atoms, perturbed, 1, plus, image_periodicity=periodicity)
91 perturbed%hmat(d, :) = hmat(d, :) - h*hmat(j, :)
92 perturbed%h_inv = inv_3x3(perturbed%hmat)
93 CALL periodic_atom_image_partition(point, atoms, perturbed, 1, minus, image_periodicity=periodicity)
94 derivative_error = max(derivative_error, abs((plus - minus)/(2*h) - dstrain(d, j)))
95 END DO
96 END DO
97 END DO
98 END DO
99 WRITE (*, '(A,ES16.7)') 'PERIODIC PARTITION UNITY ERROR ', unity_error
100 WRITE (*, '(A,ES16.7)') 'PERIODIC PARTITION TRANSLATION ERROR ', translation_error
101 WRITE (*, '(A,ES16.7)') 'PERIODIC PARTITION ATOM/STRAIN ERROR ', derivative_error
102 IF (unity_error > 2.e-13_dp) error stop 'Periodic atom-image weights do not sum to one'
103 IF (translation_error > 2.e-13_dp) error stop 'Moving an atom by a lattice vector changed its image family'
104 IF (derivative_error > 3.e-8_dp) error stop 'Periodic partition derivatives fail finite differences'
105 DEALLOCATE (cell, perturbed)
106 CALL cp2k_finalize()
Handles all functions related to the CELL.
Definition cell_types.F:15
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
CP2K C/C++ interface.
Definition libcp2k.F:20
subroutine, public cp2k_finalize()
...
Definition libcp2k.F:139
subroutine, public cp2k_init()
...
Definition libcp2k.F:104
Collection of simple mathematical functions and subroutines.
Definition mathlib.F:15
pure real(kind=dp) function, dimension(3, 3), public inv_3x3(a)
Returns the inverse of the 3 x 3 matrix a.
Definition mathlib.F:526
Smooth atom-image weights normalized over translated, equivalent environments.
subroutine, public build_periodic_atom_image_layout(atom_coords, cell, target_atom, image_periodicity, layout, target_image)
Prepare geometry shared by the points of an atom block.
subroutine, public periodic_atom_image_partition_from_layout(grid_point, layout, target_image, weight, dweight_datom, dweight_dstrain)
Evaluate a reference-atom weight and optional base-atom and explicit image-strain responses.
subroutine, public periodic_atom_image_partition(grid_point, atom_coords, cell, target_atom, weight, dweight_datom, dweight_dstrain, image_periodicity)
Convenience evaluation without retaining a layout between grid points.
program skala_periodic_partition_unittest
Type defining parameters related to the simulation cell.
Definition cell_types.F:60