LCOV - code coverage report
Current view: top level - src - optbas_frontier_orbitals_utils_unittest.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:123285f) Lines: 90.9 % 22 20
Test Date: 2026-11-04 00:58:17 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              : !> \brief Unit tests for the frontier-orbital objective
       9              : !> \author Jan Wilhelm
      10              : ! **************************************************************************************************
      11            2 : PROGRAM optbas_frontier_orbitals_utils_unittest
      12            2 :    USE kinds,                           ONLY: dp
      13              :    USE optbas_frontier_orbitals_utils,  ONLY: compute_frontier_orbital_loss_function,&
      14              :                                               frontier_orbitals_result_type,&
      15              :                                               smooth_frontier_orbital_weight
      16              : 
      17              :    IMPLICIT NONE
      18              : 
      19              :    REAL(KIND=dp), PARAMETER :: tolerance = 1.0E-12_dp
      20              :    REAL(KIND=dp), PARAMETER :: eigenvalues(4) = [-1.0_dp, -0.5_dp, 0.1_dp, 0.2_dp]
      21              :    INTEGER :: i, j, orientation
      22              :    REAL(KIND=dp) :: candidate(4), overlap(4, 4), scale, losses(6), expected(6)
      23              :    TYPE(frontier_orbitals_result_type) :: result
      24              : 
      25              :    ! Equal virtual weights give L_occ=L_vir=L_empty=1-scale**2.
      26            8 :    DO i = 0, 2
      27            6 :       scale = 0.5_dp*REAL(i, KIND=dp)
      28            6 :       candidate = eigenvalues
      29           18 :       candidate(3:4) = candidate(3:4) + 0.2_dp*scale
      30            6 :       overlap = 0.0_dp
      31           30 :       DO j = 1, 4
      32           30 :          overlap(j, j) = scale
      33              :       END DO
      34           24 :       expected(1:3) = 1.0_dp - scale**2
      35           24 :       expected(4:6) = scale**2
      36           20 :       DO orientation = 1, 2
      37              :          CALL compute_frontier_orbital_loss_function(overlap, eigenvalues, candidate, 2, &
      38           12 :                                                      10.0_dp, 0.1_dp, 0.2_dp, RESULT)
      39              :          losses = [result%loss_occupied_subspace, result%loss_virtual_subspace, &
      40              :                    result%loss_empty_subspace, result%empty_subspace_overlap, &
      41           84 :                    result%virtual_subspace_similarity, result%loss_gap]
      42           84 :          IF (.NOT. ALL(ABS(losses - expected) <= tolerance)) ERROR STOP "Incorrect frontier-orbital objective"
      43              :          ! Permutations and signs within each subspace leave the objective unchanged.
      44          492 :          overlap = overlap(:, [2, 1, 4, 3])
      45           66 :          overlap(:, 1) = -overlap(:, 1)
      46              :       END DO
      47              :    END DO
      48            2 :    IF (.NOT. (smooth_frontier_orbital_weight(0.999_dp, 1.0_dp, 0.1_dp) > &
      49            0 :               smooth_frontier_orbital_weight(1.001_dp, 1.0_dp, 0.1_dp))) ERROR STOP "Nonmonotonic virtual weight"
      50            2 :    IF (.NOT. (ABS(smooth_frontier_orbital_weight(1.0_dp, 1.0_dp, 0.1_dp) - 0.5_dp) <= tolerance)) THEN
      51            0 :       ERROR STOP "Incorrect virtual-weight cutoff midpoint"
      52              :    END IF
      53              : 
      54            2 : END PROGRAM optbas_frontier_orbitals_utils_unittest
        

Generated by: LCOV version 2.0-1