LCOV - code coverage report
Current view: top level - src/xc - skala_periodic_partition_unittest.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 100.0 % 71 71
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 2 2

            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            2 : PROGRAM skala_periodic_partition_unittest
       9            2 :    USE cell_types,                      ONLY: cell_type
      10              :    USE kinds,                           ONLY: dp
      11              :    USE libcp2k,                         ONLY: cp2k_finalize,&
      12              :                                               cp2k_init
      13              :    USE mathlib,                         ONLY: inv_3x3
      14              :    USE skala_periodic_partition,        ONLY: build_periodic_atom_image_layout,&
      15              :                                               periodic_atom_image_partition,&
      16              :                                               periodic_atom_image_partition_from_layout,&
      17              :                                               skala_periodic_partition_type
      18              : 
      19              :    IMPLICIT NONE
      20              :    TYPE(cell_type), POINTER :: cell, perturbed
      21           56 :    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            2 :    CALL cp2k_init()
      29          254 :    ALLOCATE (cell, perturbed)
      30            2 :    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            8 :    atoms(:, 1) = [0.2_dp, 0.3_dp, -0.1_dp]
      32            8 :    atoms(:, 2) = [1.8_dp, 2.2_dp, 1.3_dp]
      33            2 :    unity_error = 0.0_dp
      34            2 :    derivative_error = 0.0_dp
      35            2 :    translation_error = 0.0_dp
      36           18 :    DO mask = 0, 7
      37           88 :       periodicity = [MERGE(1, 0, BTEST(mask, 0)), MERGE(1, 0, BTEST(mask, 1)), MERGE(1, 0, BTEST(mask, 2))]
      38          208 :       cell%hmat = hmat
      39          208 :       cell%h_inv = inv_3x3(hmat)
      40           64 :       cell%perd = periodicity
      41           64 :       perturbed%perd = periodicity
      42           16 :       CALL build_periodic_atom_image_layout(atoms, cell, 1, periodicity, layout, TARGET)
      43           66 :       DO sample = 1, 3
      44           48 :          point = [1.1_dp, 1.3_dp, 1.7_dp]
      45          240 :          IF (sample == 2) point = MATMUL(hmat, [0.5_dp, 0.2_dp, 0.1_dp])
      46          288 :          IF (sample == 3) point = MATMUL(hmat, [1.5_dp, 0.2_dp, 0.1_dp]) + atoms(:, 1)
      47           48 :          CALL periodic_atom_image_partition_from_layout(point, layout, TARGET, value, datom, dstrain)
      48           48 :          CALL periodic_atom_image_partition(point, atoms, cell, 1, direct, image_periodicity=periodicity)
      49           48 :          IF (ABS(direct - value) > 2.e-14_dp) ERROR STOP 'Cached partition changed the value'
      50           48 :          IF (mask == 0) THEN
      51           60 :             mu = (NORM2(point - atoms(:, 1)) - NORM2(point - atoms(:, 2)))/NORM2(atoms(:, 1) - atoms(:, 2))
      52           24 :             DO j = 1, 3
      53           24 :                mu = 1.5_dp*mu - 0.5_dp*mu**3
      54              :             END DO
      55            6 :             IF (ABS(value - 0.5_dp*(1.0_dp - mu)) > 2.e-14_dp) ERROR STOP 'Nonperiodic Becke weight changed'
      56           78 :             IF (MAXVAL(ABS(dstrain)) > 2.e-14_dp) ERROR STOP 'Molecular partition has an image-strain response'
      57              :          END IF
      58           48 :          total = 0.0_dp
      59          144 :          DO a = 1, 2
      60          528 :             DO l = -3*periodicity(3), 3*periodicity(3)
      61         2016 :                DO k = -3*periodicity(2), 3*periodicity(2)
      62         8064 :                   DO j = -3*periodicity(1), 3*periodicity(1)
      63       116736 :                      shifted = point - MATMUL(hmat, REAL([j, k, l], dp))
      64         6144 :                      CALL periodic_atom_image_partition(shifted, atoms, cell, a, direct, image_periodicity=periodicity)
      65         7680 :                      total = total + direct
      66              :                   END DO
      67              :                END DO
      68              :             END DO
      69              :          END DO
      70           48 :          unity_error = MAX(unity_error, ABS(total - 1.0_dp))
      71           48 :          trial = atoms
      72         1056 :          trial(:, 2) = atoms(:, 2) + MATMUL(hmat, REAL([1, -1, 2]*periodicity, dp))
      73           48 :          CALL periodic_atom_image_partition(point, trial, cell, 1, direct, image_periodicity=periodicity)
      74           48 :          translation_error = MAX(translation_error, ABS(direct - value))
      75          144 :          DO a = 1, 2
      76          432 :             DO d = 1, 3
      77          288 :                trial = atoms
      78          288 :                trial(d, a) = atoms(d, a) + h
      79          288 :                CALL periodic_atom_image_partition(point, trial, cell, 1, plus, image_periodicity=periodicity)
      80          288 :                trial(d, a) = atoms(d, a) - h
      81          288 :                CALL periodic_atom_image_partition(point, trial, cell, 1, minus, image_periodicity=periodicity)
      82          384 :                derivative_error = MAX(derivative_error, ABS((plus - minus)/(2*h) - datom(d, a)))
      83              :             END DO
      84              :          END DO
      85          208 :          DO d = 1, 3
      86          624 :             DO j = 1, 3
      87         5616 :                perturbed%hmat = hmat
      88         1728 :                perturbed%hmat(d, :) = hmat(d, :) + h*hmat(j, :)
      89         5616 :                perturbed%h_inv = inv_3x3(perturbed%hmat)
      90          432 :                CALL periodic_atom_image_partition(point, atoms, perturbed, 1, plus, image_periodicity=periodicity)
      91         1728 :                perturbed%hmat(d, :) = hmat(d, :) - h*hmat(j, :)
      92         5616 :                perturbed%h_inv = inv_3x3(perturbed%hmat)
      93          432 :                CALL periodic_atom_image_partition(point, atoms, perturbed, 1, minus, image_periodicity=periodicity)
      94          576 :                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            2 :    WRITE (*, '(A,ES16.7)') 'PERIODIC PARTITION UNITY ERROR ', unity_error
     100            2 :    WRITE (*, '(A,ES16.7)') 'PERIODIC PARTITION TRANSLATION ERROR ', translation_error
     101            2 :    WRITE (*, '(A,ES16.7)') 'PERIODIC PARTITION ATOM/STRAIN ERROR ', derivative_error
     102            2 :    IF (unity_error > 2.e-13_dp) ERROR STOP 'Periodic atom-image weights do not sum to one'
     103            2 :    IF (translation_error > 2.e-13_dp) ERROR STOP 'Moving an atom by a lattice vector changed its image family'
     104            2 :    IF (derivative_error > 3.e-8_dp) ERROR STOP 'Periodic partition derivatives fail finite differences'
     105            2 :    DEALLOCATE (cell, perturbed)
     106            2 :    CALL cp2k_finalize()
     107            2 : END PROGRAM skala_periodic_partition_unittest
        

Generated by: LCOV version 2.0-1