LCOV - code coverage report
Current view: top level - src - gw_nevanlinna.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 91.3 % 208 190
Test Date: 2026-11-24 01:22:08 Functions: 68.8 % 16 11

            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              : ! **************************************************************************************************
       9              : !> \brief Nevanlinna analytic continuation of the GW correlation self-energy from the imaginary
      10              : !>        to the real frequency axis, following J. Fei, C.-N. Yeh, E. Gull, PRL 126, 056402 (2021).
      11              : !>        Minus the correlation self-energy maps the upper half plane into itself. After a Moebius
      12              : !>        map onto the unit disk the interpolation becomes a Schur problem whose solution is
      13              : !>        causal by construction. The Schur recursion loses digits quickly and is therefore done
      14              : !>        in double-double arithmetic (about 31 significant digits, module double_double), so that
      15              : !>        no quadruple-precision support of the compiler is needed; the evaluation is stable in
      16              : !>        double precision.
      17              : !> \par History
      18              : !>      11.2026 created
      19              : ! **************************************************************************************************
      20              : MODULE gw_nevanlinna
      21              :    USE double_double,                   ONLY: &
      22              :         zdd, zdd_abs2, zdd_add, zdd_conjg, zdd_div, zdd_i, zdd_mul, zdd_one, zdd_sub, zdd_to_dp, &
      23              :         zdd_type, zdd_zero
      24              :    USE kinds,                           ONLY: dp
      25              :    USE mathlib,                         ONLY: diag_complex
      26              : #include "./base/base_uses.f90"
      27              : 
      28              :    IMPLICIT NONE
      29              : 
      30              :    PRIVATE
      31              : 
      32              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'gw_nevanlinna'
      33              : 
      34              :    ! imaginary shifts delta*max|Sigma| tried in turn until the samples admit an interpolant
      35              :    REAL(KIND=dp), DIMENSION(9), PARAMETER :: nevanlinna_deltas = [0.0_dp, 1.0E-14_dp, 1.0E-12_dp, &
      36              :                                                                   1.0E-10_dp, 1.0E-8_dp, 1.0E-6_dp, &
      37              :                                                                   1.0E-4_dp, 1.0E-3_dp, 1.0E-2_dp]
      38              :    REAL(KIND=dp), PARAMETER :: tiny_schur = 1.0E-40_dp
      39              : 
      40              :    PUBLIC :: gw_nevanlinna_env_type, nevanlinna_type, nevanlinna_create, nevanlinna_evaluate, &
      41              :              nevanlinna_evaluate_real_grid, nevanlinna_release
      42              :    PUBLIC :: nevanlinna_window_grid_type, nevanlinna_prepare_window_grid, nevanlinna_create_windows
      43              : 
      44              : ! **************************************************************************************************
      45              : !> \brief Parameters of the quasiparticle solution with the Nevanlinna continuation; they are set
      46              : !>        in set_heuristic_parameters (gw_utils.F) and stay zero for the Padé continuation
      47              : ! **************************************************************************************************
      48              :    TYPE gw_nevanlinna_env_type
      49              :       ! relative SVD cutoff of the least-squares minimax weights of Σ^c(iτ) -> Σ^c(iω); the
      50              :       ! truncated SVD keeps Σ^c(iω) causal, which the Nevanlinna interpolant requires
      51              :       REAL(KIND=dp)                                      :: rcond_weights = 0.0_dp
      52              :       ! fit windows: the lowest k >= window_min_points imaginary frequencies, with the highest one
      53              :       ! in [window_freq_min, window_freq_max]; the quasiparticle energy is the median over windows
      54              :       INTEGER                                            :: window_min_points = 0
      55              :       REAL(KIND=dp)                                      :: window_freq_min = 0.0_dp, &
      56              :                                                             window_freq_max = 0.0_dp
      57              :       ! a level is marked unstable if its window solutions spread more than window_spread
      58              :       REAL(KIND=dp)                                      :: window_spread = 0.0_dp
      59              :       ! Newton search from the DFT eigenvalue with steps of at most qp_newton_max_step, then the
      60              :       ! solution of largest weight Z within qp_maxz_radius of the Newton solution
      61              :       REAL(KIND=dp)                                      :: qp_newton_max_step = 0.0_dp, &
      62              :                                                             qp_maxz_radius = 0.0_dp
      63              :    END TYPE gw_nevanlinna_env_type
      64              : 
      65              : ! **************************************************************************************************
      66              : !> \brief Schur parameters of one Nevanlinna interpolant
      67              : ! **************************************************************************************************
      68              :    TYPE nevanlinna_type
      69              :       LOGICAL                                            :: solvable = .FALSE.
      70              :       LOGICAL                                            :: mirrored = .FALSE.
      71              :       INTEGER                                            :: n = 0
      72              :       COMPLEX(KIND=dp), DIMENSION(:), ALLOCATABLE        :: xis, phis
      73              :    END TYPE nevanlinna_type
      74              : 
      75              : ! **************************************************************************************************
      76              : !> \brief Frequency-dependent Schur factors shared by the bands and fit windows of a k-point.
      77              : ! **************************************************************************************************
      78              :    TYPE nevanlinna_window_grid_type
      79              :       PRIVATE
      80              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: omega
      81              :       TYPE(zdd_type), ALLOCATABLE, DIMENSION(:)          :: xis
      82              :       TYPE(zdd_type), ALLOCATABLE, DIMENSION(:, :)       :: factors
      83              :    END TYPE nevanlinna_window_grid_type
      84              : 
      85              : CONTAINS
      86              : 
      87              : ! **************************************************************************************************
      88              : !> \brief Builds the Nevanlinna interpolant of y(x). If the samples are not causal within the
      89              : !>        accuracy of the data, a small imaginary shift -i*delta*max|y| is added, with delta taken
      90              : !>        from a fixed ladder; nev%solvable is .FALSE. if no delta of the ladder works.
      91              : !> \param nev the interpolant
      92              : !> \param x sample points, all in the upper or all in the lower half plane
      93              : !> \param y values of the correlation self-energy at x
      94              : !> \param pick_min optional smallest relative eigenvalue of the unshifted Pick matrix
      95              : ! **************************************************************************************************
      96         7172 :    SUBROUTINE nevanlinna_create(nev, x, y, pick_min)
      97              :       TYPE(nevanlinna_type), INTENT(OUT)                 :: nev
      98              :       COMPLEX(KIND=dp), DIMENSION(:), INTENT(IN)         :: x, y
      99              :       REAL(KIND=dp), INTENT(OUT), OPTIONAL               :: pick_min
     100              : 
     101              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'nevanlinna_create'
     102              : 
     103         3586 :       COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:)        :: xx, yy
     104         3586 :       COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :)     :: pick, pick_vec
     105              :       INTEGER                                            :: handle, i, i_delta, k, n, n_valid
     106         3586 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: order
     107              :       REAL(KIND=dp)                                      :: scale
     108         3586 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: pick_eig
     109         3586 :       TYPE(zdd_type), ALLOCATABLE, DIMENSION(:)          :: phis, xis
     110              : 
     111         3586 :       CALL timeset(routineN, handle)
     112              : 
     113         3586 :       IF (PRESENT(pick_min)) pick_min = 0.0_dp
     114         3586 :       n = SIZE(x)
     115         3586 :       nev%n = n
     116       141066 :       ALLOCATE (nev%xis(n), nev%phis(n), xis(n), phis(n), xx(n), yy(n), order(n))
     117        54396 :       nev%xis(:) = (0.0_dp, 0.0_dp)
     118        54396 :       nev%phis(:) = (0.0_dp, 0.0_dp)
     119              : 
     120              :       ! occupied levels are sampled in the lower half plane: continue the mirror image
     121        15917 :       nev%mirrored = ALL(AIMAG(x) < 0.0_dp)
     122         3586 :       IF (nev%mirrored) THEN
     123        13407 :          xx(:) = CONJG(x)
     124        13407 :          yy(:) = CONJG(y)
     125              :       ELSE
     126        40989 :          xx(:) = x
     127        40989 :          yy(:) = y
     128              :       END IF
     129              : 
     130       108792 :       IF (n == 0 .OR. .NOT. (ALL(ABS(xx) < HUGE(1.0_dp)) .AND. ALL(ABS(yy) < HUGE(1.0_dp)))) THEN
     131            0 :          DEALLOCATE (xis, phis, xx, yy, order)
     132            0 :          CALL timestop(handle)
     133            0 :          RETURN
     134              :       END IF
     135              : 
     136         3586 :       CALL sort_by_imag(xx, order)
     137              : 
     138        54396 :       scale = MAXVAL(ABS(yy))
     139         3586 :       IF (scale == 0.0_dp) scale = 1.0_dp
     140              : 
     141        54396 :       DO i = 1, n
     142        54396 :          xis(i) = moebius(zdd(xx(order(i))))
     143              :       END DO
     144              : 
     145        32172 :       DO i_delta = 1, SIZE(nevanlinna_deltas)
     146       471778 :          DO i = 1, n
     147       471778 :             phis(i) = shifted_sample(yy(order(i)), nevanlinna_deltas(i_delta)*scale)
     148              :          END DO
     149              : 
     150        30049 :          IF (i_delta == 1 .AND. PRESENT(pick_min)) THEN
     151              :             ! smallest eigenvalue of the Pick matrix of the unshifted samples, relative to the
     152              :             ! largest one; negative values quantify how far the data are from causal
     153        28688 :             ALLOCATE (pick(n, n), pick_vec(n, n), pick_eig(n))
     154        54396 :             DO k = 1, n
     155      1131306 :                DO i = 1, n
     156              :                   pick(i, k) = zdd_to_dp(zdd_div(zdd_sub(zdd_one, zdd_mul(phis(i), zdd_conjg(phis(k)))), &
     157      1127720 :                                                  zdd_sub(zdd_one, zdd_mul(xis(i), zdd_conjg(xis(k))))))
     158              :                END DO
     159              :             END DO
     160      2259026 :             pick(:, :) = 0.5_dp*(pick + CONJG(TRANSPOSE(pick)))
     161         3586 :             CALL diag_complex(pick, pick_vec, pick_eig)
     162       105206 :             pick_min = MINVAL(pick_eig)/MAX(MAXVAL(ABS(pick_eig)), TINY(1.0_dp))
     163         3586 :             DEALLOCATE (pick, pick_vec, pick_eig)
     164              :          END IF
     165              : 
     166        30049 :          n_valid = schur_prefix(xis, phis)
     167        30049 :          nev%solvable = n_valid == n
     168        32172 :          IF (nev%solvable) EXIT
     169              :       END DO
     170              : 
     171        54396 :       DO i = 1, n
     172        50810 :          nev%xis(i) = zdd_to_dp(xis(i))
     173        54396 :          IF (nev%solvable) nev%phis(i) = zdd_to_dp(phis(i))
     174              :       END DO
     175              : 
     176         3586 :       DEALLOCATE (xis, phis, xx, yy, order)
     177              : 
     178         3586 :       CALL timestop(handle)
     179              : 
     180         7172 :    END SUBROUTINE nevanlinna_create
     181              : 
     182              : ! **************************************************************************************************
     183              : !> \brief Prepare the Moebius nodes and triangular table of Blaschke factors for a prefix grid.
     184              : !> \param grid shared frequency data
     185              : !> \param omega imaginary frequencies in window order
     186              : ! **************************************************************************************************
     187          570 :    SUBROUTINE nevanlinna_prepare_window_grid(grid, omega)
     188              :       TYPE(nevanlinna_window_grid_type), INTENT(OUT)     :: grid
     189              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: omega
     190              : 
     191              :       INTEGER                                            :: j, k, n
     192              : 
     193          570 :       n = SIZE(omega)
     194         1710 :       ALLOCATE (grid%omega(n))
     195         9030 :       grid%omega(:) = ABS(omega)
     196              :       ! With a zero frequency the occupied-state mirror convention changes. An unordered
     197              :       ! grid can also give different sorted prefixes. Use the single-window path in either case.
     198         9030 :       IF (.NOT. ALL(grid%omega > 0.0_dp .AND. grid%omega < HUGE(1.0_dp))) RETURN
     199         8460 :       IF (ANY(grid%omega(2:n) < grid%omega(1:n - 1))) RETURN
     200        10170 :       ALLOCATE (grid%xis(n))
     201         9030 :       grid%xis(:) = moebius(zdd(CMPLX(0.0_dp, grid%omega, KIND=dp)))
     202         9030 :       IF (ANY(zdd_abs2(grid%xis) >= 1.0_dp)) RETURN
     203       179448 :       ALLOCATE (grid%factors(n, n))
     204         9030 :       DO k = 1, n
     205        89154 :          DO j = 1, k - 1
     206        88584 :             grid%factors(j, k) = blaschke_factor(grid%xis(j), grid%xis(k))
     207              :          END DO
     208              :       END DO
     209              :    END SUBROUTINE nevanlinna_prepare_window_grid
     210              : 
     211              : ! **************************************************************************************************
     212              : !> \brief Fit nested windows for a single band, reusing Schur parameters at equal absolute shifts.
     213              : !> \param fits one interpolant per window, with solvable false if all shifts failed
     214              : !> \param grid frequency data shared across bands
     215              : !> \param y self-energy samples in the same order as the grid
     216              : !> \param occupied whether the samples were taken on the negative imaginary axis
     217              : !> \param sizes positive window lengths in increasing order
     218              : ! **************************************************************************************************
     219        24190 :    SUBROUTINE nevanlinna_create_windows(fits, grid, y, occupied, sizes)
     220              :       TYPE(nevanlinna_type), DIMENSION(:), INTENT(OUT)   :: fits
     221              :       TYPE(nevanlinna_window_grid_type), INTENT(IN)      :: grid
     222              :       COMPLEX(KIND=dp), DIMENSION(:), INTENT(IN)         :: y
     223              :       LOGICAL, INTENT(IN)                                :: occupied
     224              :       INTEGER, DIMENSION(:), INTENT(IN)                  :: sizes
     225              : 
     226              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'nevanlinna_create_windows'
     227              :       COMPLEX(KIND=dp), PARAMETER                        :: ii = (0.0_dp, 1.0_dp)
     228              : 
     229         4300 :       COMPLEX(KIND=dp), DIMENSION(SIZE(y))               :: yy
     230              :       INTEGER                                            :: handle, i, i_delta, n, n_valid
     231              :       REAL(KIND=dp)                                      :: previous_shift, shift, sign_freq
     232         4300 :       REAL(KIND=dp), DIMENSION(SIZE(sizes))              :: window_scale
     233         4300 :       REAL(KIND=dp), DIMENSION(SIZE(y))                  :: abs_y
     234        35929 :       TYPE(zdd_type), DIMENSION(SIZE(y))                 :: phis
     235              : 
     236         2150 :       CALL timeset(routineN, handle)
     237        31629 :       abs_y(:) = ABS(y)
     238        31629 :       IF (.NOT. ALLOCATED(grid%factors) .OR. .NOT. ALL(abs_y < HUGE(1.0_dp))) THEN
     239              :          ! A bad sample in a longer window must not invalidate its shorter prefixes.
     240            0 :          sign_freq = MERGE(-1.0_dp, 1.0_dp, occupied)
     241            0 :          DO i = 1, SIZE(sizes)
     242            0 :             n = sizes(i)
     243            0 :             CALL nevanlinna_create(fits(i), ii*(sign_freq*grid%omega(1:n)), y(1:n))
     244              :          END DO
     245            0 :          CALL timestop(handle)
     246              :          RETURN
     247              :       END IF
     248              : 
     249        31629 :       yy(:) = y
     250         9051 :       IF (occupied) yy(:) = CONJG(y)
     251        12095 :       DO i = 1, SIZE(sizes)
     252         9945 :          n = sizes(i)
     253         9945 :          fits(i)%n = n
     254         9945 :          fits(i)%mirrored = occupied
     255       165219 :          window_scale(i) = MAXVAL(abs_y(1:n))
     256        12095 :          IF (window_scale(i) == 0.0_dp) window_scale(i) = 1.0_dp
     257              :       END DO
     258              : 
     259        19875 :       DO i_delta = 1, SIZE(nevanlinna_deltas)
     260       106323 :          previous_shift = -1.0_dp
     261       106323 :          n_valid = 0
     262       106323 :          DO i = SIZE(sizes), 1, -1
     263        88189 :             IF (fits(i)%solvable) CYCLE
     264        87944 :             n = sizes(i)
     265        87944 :             shift = nevanlinna_deltas(i_delta)*window_scale(i)
     266              :             ! A shorter window at this exact shift has the same parameters up to the
     267              :             ! first failed Schur step, even if the longer window needs another shift.
     268        87944 :             IF (shift /= previous_shift) THEN
     269       537283 :                phis(1:n) = shifted_sample(yy(1:n), shift)
     270        34259 :                n_valid = schur_prefix(grid%xis(1:n), phis(1:n), grid%factors)
     271        34259 :                previous_shift = shift
     272              :             END IF
     273        87944 :             IF (n > n_valid) CYCLE
     274         1627 :             fits(i)%solvable = .TRUE.
     275         6508 :             ALLOCATE (fits(i)%xis(n), fits(i)%phis(n))
     276        18349 :             fits(i)%xis(:) = zdd_to_dp(grid%xis(1:n))
     277       123045 :             fits(i)%phis(:) = zdd_to_dp(phis(1:n))
     278              :          END DO
     279        21747 :          IF (ALL(fits%solvable)) EXIT
     280              :       END DO
     281         2150 :       CALL timestop(handle)
     282              :    END SUBROUTINE nevanlinna_create_windows
     283              : 
     284              : ! **************************************************************************************************
     285              : !> \brief Evaluates the interpolant on the real axis
     286              : !> \param nev the interpolant
     287              : !> \param x_val real frequency
     288              : !> \param func_val continued correlation self-energy at x_val
     289              : ! **************************************************************************************************
     290       225882 :    SUBROUTINE nevanlinna_evaluate(nev, x_val, func_val)
     291              :       TYPE(nevanlinna_type), INTENT(IN)                  :: nev
     292              :       REAL(KIND=dp), INTENT(IN)                          :: x_val
     293              :       COMPLEX(KIND=dp), INTENT(OUT)                      :: func_val
     294              : 
     295              :       COMPLEX(KIND=dp), PARAMETER                        :: ii = (0.0_dp, 1.0_dp), &
     296              :                                                             one = (1.0_dp, 0.0_dp), &
     297              :                                                             zero = (0.0_dp, 0.0_dp)
     298              : 
     299              :       COMPLEX(KIND=dp)                                   :: aa, bb, bj, cc, cphi, dd, na, nb, nc, &
     300              :                                                             nd, phi, th, xi, xz
     301              :       INTEGER                                            :: j
     302              :       REAL(KIND=dp)                                      :: sc
     303              : 
     304              :       ! product of the Moebius transformations of the Schur recursion, applied to theta = 0
     305       225882 :       xz = (CMPLX(x_val, 0.0_dp, KIND=dp) - ii)/(CMPLX(x_val, 0.0_dp, KIND=dp) + ii)
     306       225882 :       aa = one
     307       225882 :       bb = zero
     308       225882 :       cc = zero
     309       225882 :       dd = one
     310      2239742 :       DO j = 1, nev%n
     311      2013860 :          xi = nev%xis(j)
     312      2013860 :          phi = nev%phis(j)
     313      2013860 :          bj = (xz - xi)/(one - CONJG(xi)*xz)
     314      2013860 :          cphi = CONJG(phi)
     315      2013860 :          na = aa*bj + bb*cphi*bj
     316      2013860 :          nb = aa*phi + bb
     317      2013860 :          nc = cc*bj + dd*cphi*bj
     318      2013860 :          nd = cc*phi + dd
     319              :          ! rescale to avoid overflow; only the ratio bb/dd is used
     320      2013860 :          sc = MAX(ABS(na), ABS(nb), ABS(nc), ABS(nd))
     321      2013860 :          IF (sc == 0.0_dp) sc = 1.0_dp
     322      2013860 :          aa = na/sc
     323      2013860 :          bb = nb/sc
     324      2013860 :          cc = nc/sc
     325      2239742 :          dd = nd/sc
     326              :       END DO
     327       225882 :       th = bb/dd
     328              :       ! inverse Moebius map, and Sigma = -(Nevanlinna function)
     329       225882 :       func_val = -ii*(one + th)/(one - th)
     330       225882 :       IF (nev%mirrored) func_val = CONJG(func_val)
     331              : 
     332       225882 :    END SUBROUTINE nevanlinna_evaluate
     333              : 
     334              : ! **************************************************************************************************
     335              : !> \brief Evaluates Re Sigma on a grid of real frequencies
     336              : !> \param nev the interpolant
     337              : !> \param x real frequencies
     338              : !> \param values real part of the continued correlation self-energy at x
     339              : ! **************************************************************************************************
     340         3090 :    SUBROUTINE nevanlinna_evaluate_real_grid(nev, x, values)
     341              :       TYPE(nevanlinna_type), INTENT(IN)                  :: nev
     342              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: x
     343              :       REAL(KIND=dp), DIMENSION(:), INTENT(OUT)           :: values
     344              : 
     345              :       COMPLEX(KIND=dp), PARAMETER                        :: ii = (0.0_dp, 1.0_dp)
     346              :       INTEGER, PARAMETER                                 :: block_size = 64, rescale_interval = 8
     347              :       REAL(KIND=dp), PARAMETER :: scalar_tol = SQRT(EPSILON(1.0_dp))
     348              : 
     349              :       COMPLEX(KIND=dp)                                   :: alpha, beta, c, p, phi, q, sigma
     350              :       COMPLEX(KIND=dp), DIMENSION(block_size)            :: u, v
     351              :       INTEGER                                            :: first, i, j, n
     352              :       REAL(KIND=dp)                                      :: scale
     353              :       REAL(KIND=dp), DIMENSION(block_size)               :: inv_scale, x_scaled
     354              : 
     355         3090 :       IF (nev%n == 0) THEN
     356            0 :          values(:) = 0.0_dp
     357            0 :          RETURN
     358              :       END IF
     359              :       ! Near a node on the unit circle the two forms of the Blaschke factor can lose
     360              :       ! different digits. Use the scalar arithmetic for these ill-conditioned fits.
     361        26986 :       IF (ANY(ABS(nev%xis(1:nev%n - 1)) > 1.0_dp - scalar_tol)) THEN
     362            0 :          DO i = 1, SIZE(x)
     363            0 :             CALL nevanlinna_evaluate(nev, x(i), sigma)
     364            0 :             values(i) = REAL(sigma, KIND=dp)
     365              :          END DO
     366              :          RETURN
     367              :       END IF
     368        77250 :       DO first = 1, SIZE(x), block_size
     369        74160 :          n = MIN(block_size, SIZE(x) - first + 1)
     370              :          ! Represent x by (x_scaled,inv_scale) to keep the factors bounded at large x.
     371      4712250 :          DO i = 1, n
     372      4638090 :             scale = MAX(1.0_dp, ABS(x(first + i - 1)))
     373      4638090 :             x_scaled(i) = x(first + i - 1)/scale
     374      4712250 :             inv_scale(i) = 1.0_dp/scale
     375              :          END DO
     376              :          ! Apply the product to (0,1) from right to left. The last factor gives (phi_n,1).
     377      4712250 :          u(1:n) = nev%phis(nev%n)
     378      4712250 :          v(1:n) = 1.0_dp
     379       647664 :          DO j = nev%n - 1, 1, -1
     380       573504 :             alpha = 1.0_dp - nev%xis(j)
     381       573504 :             beta = -ii*(1.0_dp + nev%xis(j))
     382       573504 :             phi = nev%phis(j)
     383              :             ! At real x the Blaschke factor is c/conjg(c), c = (1-xi)*x - i*(1+xi).
     384              :             ! A common real scale cancels from u/v. Bound c before multiplying the vector.
     385       573504 :             !$OMP SIMD PRIVATE(c,scale,p,q)
     386              :             DO i = 1, n
     387     35867896 :                c = alpha*x_scaled(i) + beta*inv_scale(i)
     388     35867896 :                scale = MAX(ABS(REAL(c, KIND=dp)), ABS(AIMAG(c)), TINY(1.0_dp))
     389     35867896 :                c = c/scale
     390     35867896 :                p = c*u(i)
     391     35867896 :                q = CONJG(c)*v(i)
     392     35867896 :                u(i) = p + phi*q
     393     35867896 :                v(i) = q + CONJG(phi)*p
     394              :             END DO
     395              :             ! The common scale drops out of the final ratio; bound its drift in long fits.
     396       647664 :             IF (MOD(j, rescale_interval) == 0) THEN
     397       573504 :                !$OMP SIMD PRIVATE(scale)
     398              :                DO i = 1, n
     399              :                   scale = MAX(ABS(REAL(u(i), KIND=dp)), ABS(AIMAG(u(i))), &
     400      2006837 :                               ABS(REAL(v(i), KIND=dp)), ABS(AIMAG(v(i))), TINY(1.0_dp))
     401      2006837 :                   u(i) = u(i)/scale
     402      2006837 :                   v(i) = v(i)/scale
     403              :                END DO
     404              :             END IF
     405              :          END DO
     406      4715340 :          DO i = 1, n
     407              :             ! Near a pole use the scalar evaluator's order of operations for the sign test.
     408      4712250 :             IF (ABS(v(i) - u(i)) <= scalar_tol*ABS(v(i))) THEN
     409            0 :                CALL nevanlinna_evaluate(nev, x(first + i - 1), sigma)
     410            0 :                values(first + i - 1) = REAL(sigma, KIND=dp)
     411              :             ELSE
     412              :                ! Re[-i*(v+u)/(v-u)] is unchanged by the occupied-state conjugation.
     413      4638090 :                values(first + i - 1) = AIMAG((v(i) + u(i))/(v(i) - u(i)))
     414              :             END IF
     415              :          END DO
     416              :       END DO
     417              : 
     418              :    END SUBROUTINE nevanlinna_evaluate_real_grid
     419              : 
     420              : ! **************************************************************************************************
     421              : !> \brief Releases the interpolant
     422              : !> \param nev the interpolant
     423              : ! **************************************************************************************************
     424        15614 :    SUBROUTINE nevanlinna_release(nev)
     425              :       TYPE(nevanlinna_type), INTENT(INOUT)               :: nev
     426              : 
     427        15614 :       IF (ALLOCATED(nev%xis)) DEALLOCATE (nev%xis)
     428        15614 :       IF (ALLOCATED(nev%phis)) DEALLOCATE (nev%phis)
     429        15614 :       nev%solvable = .FALSE.
     430        15614 :       nev%n = 0
     431              : 
     432        15614 :    END SUBROUTINE nevanlinna_release
     433              : 
     434              : ! **************************************************************************************************
     435              : !> \brief Moebius map of the upper half plane onto the unit disk
     436              : !> \param w point in the upper half plane
     437              : !> \return (w - i)/(w + i)
     438              : ! **************************************************************************************************
     439      1004023 :    ELEMENTAL FUNCTION moebius(w) RESULT(h)
     440              :       TYPE(zdd_type), INTENT(IN)                         :: w
     441              :       TYPE(zdd_type)                                     :: h
     442              : 
     443      1004023 :       h = zdd_div(zdd_sub(w, zdd_i), zdd_add(w, zdd_i))
     444              : 
     445      1004023 :    END FUNCTION moebius
     446              : 
     447              : ! **************************************************************************************************
     448              : !> \brief Map the shifted -Sigma sample to the unit disk.
     449              : !> \param y self-energy sample in the upper half plane
     450              : !> \param shift absolute imaginary shift
     451              : !> \return Moebius image of the shifted sample
     452              : ! **************************************************************************************************
     453       944753 :    ELEMENTAL FUNCTION shifted_sample(y, shift) RESULT(lam)
     454              :       COMPLEX(KIND=dp), INTENT(IN)                       :: y
     455              :       REAL(KIND=dp), INTENT(IN)                          :: shift
     456              :       TYPE(zdd_type)                                     :: lam
     457              : 
     458       944753 :       lam = moebius(zdd(-(y - CMPLX(0.0_dp, shift, KIND=dp))))
     459       944753 :    END FUNCTION shifted_sample
     460              : 
     461              : ! **************************************************************************************************
     462              : !> \brief Blaschke factor at a pair of Moebius nodes.
     463              : !> \param xi Schur node
     464              : !> \param x evaluation node
     465              : !> \return Blaschke factor
     466              : ! **************************************************************************************************
     467       567070 :    PURE FUNCTION blaschke_factor(xi, x) RESULT(bfac)
     468              :       TYPE(zdd_type), INTENT(IN)                         :: xi, x
     469              :       TYPE(zdd_type)                                     :: bfac
     470              : 
     471       567070 :       bfac = zdd_div(zdd_sub(x, xi), zdd_sub(zdd_one, zdd_mul(zdd_conjg(xi), x)))
     472       567070 :    END FUNCTION blaschke_factor
     473              : 
     474              : ! **************************************************************************************************
     475              : !> \brief Schur recursion through the first sample outside the unit disk.
     476              : !> \param xis Moebius images of the sample points, sorted by imaginary part
     477              : !> \param phis Moebius images of the function values on input, Schur parameters of the valid prefix on output
     478              : !> \param factors optional precomputed Blaschke factors
     479              : !> \return length of the valid prefix
     480              : ! **************************************************************************************************
     481        64308 :    FUNCTION schur_prefix(xis, phis, factors) RESULT(n_valid)
     482              :       TYPE(zdd_type), DIMENSION(:), INTENT(IN)           :: xis
     483              :       TYPE(zdd_type), DIMENSION(:), INTENT(INOUT)        :: phis
     484              :       TYPE(zdd_type), DIMENSION(:, :), INTENT(IN), &
     485              :          OPTIONAL                                        :: factors
     486              :       INTEGER                                            :: n_valid
     487              : 
     488              :       INTEGER                                            :: j, k
     489              :       TYPE(zdd_type)                                     :: bfac, den, phi
     490              : 
     491        64308 :       n_valid = 0
     492              :       ! Finish one node before starting the next so a rejected prefix never updates the tail.
     493       384612 :       DO k = 1, SIZE(xis)
     494       382295 :          phi = phis(k)
     495      1919782 :          DO j = 1, k - 1
     496      1537487 :             IF (PRESENT(factors)) THEN
     497      1050541 :                bfac = factors(j, k)
     498              :             ELSE
     499       486946 :                bfac = blaschke_factor(xis(j), xis(k))
     500              :             END IF
     501      1537487 :             den = zdd_sub(zdd_one, zdd_mul(zdd_conjg(phis(j)), phi))
     502      1919782 :             IF (zdd_abs2(den) < tiny_schur**2 .OR. zdd_abs2(bfac) < tiny_schur**2) THEN
     503            0 :                phi = zdd_zero
     504              :             ELSE
     505      1537487 :                phi = zdd_div(zdd_div(zdd_sub(phi, phis(j)), den), bfac)
     506              :             END IF
     507              :          END DO
     508       382295 :          IF (zdd_abs2(phi) >= 1.0_dp) EXIT
     509       320304 :          phis(k) = phi
     510       384612 :          n_valid = k
     511              :       END DO
     512              : 
     513        64308 :    END FUNCTION schur_prefix
     514              : 
     515              : ! **************************************************************************************************
     516              : !> \brief Insertion sort of sample points by increasing imaginary part
     517              : !> \param z sample points
     518              : !> \param order permutation that sorts z
     519              : ! **************************************************************************************************
     520         3586 :    SUBROUTINE sort_by_imag(z, order)
     521              :       COMPLEX(KIND=dp), DIMENSION(:), INTENT(IN)         :: z
     522              :       INTEGER, DIMENSION(:), INTENT(OUT)                 :: order
     523              : 
     524              :       INTEGER                                            :: i, j, tmp
     525              : 
     526        54396 :       DO i = 1, SIZE(z)
     527        54396 :          order(i) = i
     528              :       END DO
     529        50810 :       DO i = 2, SIZE(z)
     530        47224 :          tmp = order(i)
     531        47224 :          j = i - 1
     532        47224 :          DO WHILE (j >= 1)
     533        47224 :             IF (AIMAG(z(order(j))) <= AIMAG(z(tmp))) EXIT
     534            0 :             order(j + 1) = order(j)
     535        47224 :             j = j - 1
     536              :          END DO
     537        50810 :          order(j + 1) = tmp
     538              :       END DO
     539              : 
     540         3586 :    END SUBROUTINE sort_by_imag
     541              : 
     542            0 : END MODULE gw_nevanlinna
        

Generated by: LCOV version 2.0-1