LCOV - code coverage report
Current view: top level - src - time_frequency_grids.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 86.1 % 194 167
Test Date: 2026-11-24 01:22:08 Functions: 62.5 % 8 5

            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 Definition and construction of time/frequency grids for correlation methods.
      10              : !> \par History
      11              : !>      05.2019 Refactored from rpa_ri_gpw [Frederick Stein]
      12              : ! **************************************************************************************************
      13              : MODULE time_frequency_grids
      14              :    USE greenx_interface,                ONLY: greenx_get_minimax_grid
      15              :    USE kinds,                           ONLY: dp
      16              :    USE mathconstants,                   ONLY: pi,&
      17              :                                               z_zero
      18              :    USE minimax_exp,                     ONLY: get_exp_minimax_coeff
      19              :    USE minimax_exp_gw,                  ONLY: get_exp_minimax_coeff_gw
      20              :    USE minimax_rpa,                     ONLY: get_rpa_minimax_coeff,&
      21              :                                               get_rpa_minimax_coeff_larger_grid
      22              : #include "./base/base_uses.f90"
      23              : 
      24              :    IMPLICIT NONE
      25              : 
      26              :    PRIVATE
      27              : 
      28              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'time_frequency_grids'
      29              : 
      30              :    TYPE time_frequency_grid_type
      31              :       REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: frequency
      32              :       REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: frequency_weights
      33              :       REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: imaginary_time
      34              :       REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: time_weights_at_zero_frequency
      35              :       ! Complete discrete transforms, including cosine/sine factors.
      36              :       ! Matrix rows index output grid points, columns index input grid points.
      37              :       REAL(KIND=dp), DIMENSION(:, :), ALLOCATABLE :: cosine_time_to_frequency
      38              :       REAL(KIND=dp), DIMENSION(:, :), ALLOCATABLE :: cosine_frequency_to_time
      39              :       REAL(KIND=dp), DIMENSION(:, :), ALLOCATABLE :: sine_time_to_frequency
      40              :    END TYPE time_frequency_grid_type
      41              : 
      42              :    INTEGER, PARAMETER :: cosine_time_to_frequency = 1, &
      43              :                          sine_time_to_frequency = 2, &
      44              :                          cosine_frequency_to_time = 3
      45              :    INTEGER, PARAMETER, PUBLIC :: positive_time = 1, negative_time = 2
      46              : 
      47              :    PUBLIC :: build_clenshaw_grid, build_minimax_time_frequency_grid, test_least_square_ft, &
      48              :              time_frequency_grid_release, time_frequency_grid_type, transform_time_to_frequency
      49              : 
      50              : CONTAINS
      51              : 
      52              : ! **************************************************************************************************
      53              : !> \brief Release all data owned by a time_frequency_grid_type object.
      54              : !> \param grid The grid object to release.
      55              : ! **************************************************************************************************
      56         1324 :    SUBROUTINE time_frequency_grid_release(grid)
      57              : 
      58              :       TYPE(time_frequency_grid_type), INTENT(INOUT)      :: grid
      59              : 
      60         1324 :       IF (ALLOCATED(grid%frequency)) DEALLOCATE (grid%frequency)
      61         1324 :       IF (ALLOCATED(grid%frequency_weights)) DEALLOCATE (grid%frequency_weights)
      62         1324 :       IF (ALLOCATED(grid%imaginary_time)) DEALLOCATE (grid%imaginary_time)
      63         1324 :       IF (ALLOCATED(grid%time_weights_at_zero_frequency)) DEALLOCATE (grid%time_weights_at_zero_frequency)
      64         1324 :       IF (ALLOCATED(grid%cosine_time_to_frequency)) DEALLOCATE (grid%cosine_time_to_frequency)
      65         1324 :       IF (ALLOCATED(grid%cosine_frequency_to_time)) DEALLOCATE (grid%cosine_frequency_to_time)
      66         1324 :       IF (ALLOCATED(grid%sine_time_to_frequency)) DEALLOCATE (grid%sine_time_to_frequency)
      67              : 
      68         1324 :    END SUBROUTINE time_frequency_grid_release
      69              : 
      70              : ! **************************************************************************************************
      71              : !> \brief Transform real positive/negative-time samples to complex positive-frequency values.
      72              : !>        The even part uses the cosine transform and the odd part the sine transform.
      73              : !> \param grid Valid grid with complete cosine/sine time-to-frequency matrices of matching dimensions.
      74              : !> \param samples_tau Real samples indexed (state, time, branch), with branches
      75              : !>        positive_time and negative_time. The samples include their physical signs.
      76              : !> \param values_omega Complex output indexed (state, frequency), evaluated at the leading
      77              : !>        SIZE(values_omega, 2) positive-frequency nodes of grid.
      78              : ! **************************************************************************************************
      79         1000 :    SUBROUTINE transform_time_to_frequency(grid, samples_tau, values_omega)
      80              :       TYPE(time_frequency_grid_type), INTENT(IN)         :: grid
      81              :       REAL(KIND=dp), DIMENSION(:, :, :), INTENT(IN)      :: samples_tau
      82              :       COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(OUT)     :: values_omega
      83              : 
      84              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'transform_time_to_frequency'
      85              : 
      86              :       INTEGER                                            :: handle, ifreq, itime, nstate
      87              :       REAL(KIND=dp)                                      :: weight_cos, weight_sin
      88         1000 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: samples_even, samples_odd
      89              : 
      90         1000 :       CALL timeset(routineN, handle)
      91              : 
      92         1000 :       nstate = SIZE(samples_tau, 1)
      93         1000 :       CPASSERT(SIZE(samples_tau, 3) == 2)
      94         1000 :       CPASSERT(SIZE(values_omega, 1) == nstate)
      95         1000 :       CPASSERT(SIZE(samples_tau, 2) == SIZE(grid%cosine_time_to_frequency, 2))
      96         1000 :       CPASSERT(SIZE(values_omega, 2) <= SIZE(grid%cosine_time_to_frequency, 1))
      97              : 
      98         4000 :       ALLOCATE (samples_even(nstate), samples_odd(nstate))
      99       125504 :       values_omega = z_zero
     100        15888 :       DO itime = 1, SIZE(samples_tau, 2)
     101       132196 :          samples_even(:) = 0.5_dp*(samples_tau(:, itime, positive_time) + samples_tau(:, itime, negative_time))
     102       132196 :          samples_odd(:) = 0.5_dp*(samples_tau(:, itime, positive_time) - samples_tau(:, itime, negative_time))
     103       341384 :          DO ifreq = 1, SIZE(values_omega, 2)
     104       325496 :             weight_cos = grid%cosine_time_to_frequency(ifreq, itime)
     105       325496 :             weight_sin = grid%sine_time_to_frequency(ifreq, itime)
     106              :             values_omega(:, ifreq) = values_omega(:, ifreq) + &
     107      2690564 :                                      CMPLX(weight_cos*samples_even, weight_sin*samples_odd, KIND=dp)
     108              :          END DO
     109              :       END DO
     110              : 
     111         1000 :       CALL timestop(handle)
     112              : 
     113         2000 :    END SUBROUTINE transform_time_to_frequency
     114              : 
     115              : ! **************************************************************************************************
     116              : !> \brief Build a Clenshaw-Curtis frequency grid.
     117              : !> \param num_points Number of integration points.
     118              : !> \param grid Grid object to fill.
     119              : ! **************************************************************************************************
     120          136 :    SUBROUTINE build_clenshaw_grid(num_points, grid)
     121              : 
     122              :       INTEGER, INTENT(IN)                                :: num_points
     123              :       TYPE(time_frequency_grid_type), INTENT(OUT)        :: grid
     124              : 
     125              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'build_clenshaw_grid'
     126              : 
     127              :       INTEGER                                            :: handle, jquad
     128              : 
     129          136 :       CALL timeset(routineN, handle)
     130              : 
     131          136 :       CPASSERT(num_points > 0)
     132              : 
     133          544 :       ALLOCATE (grid%frequency(num_points), grid%frequency_weights(num_points))
     134              : 
     135         5810 :       DO jquad = 1, num_points - 1
     136         5674 :          grid%frequency(jquad) = jquad*pi/(2.0_dp*num_points)
     137         5810 :          grid%frequency_weights(jquad) = pi/(num_points*SIN(grid%frequency(jquad))**2)
     138              :       END DO
     139          136 :       grid%frequency(num_points) = pi/2.0_dp
     140              :       grid%frequency_weights(num_points) = &
     141          136 :          pi/(2.0_dp*num_points*SIN(grid%frequency(num_points))**2)
     142              : 
     143          136 :       CALL timestop(handle)
     144              : 
     145          136 :    END SUBROUTINE build_clenshaw_grid
     146              : 
     147              : ! **************************************************************************************************
     148              : !> \brief Build a minimax time/frequency grid through the common backend boundary.
     149              : !> \param num_points Number of minimax points.
     150              : !> \param energy_min Lower end of the physical energy interval.
     151              : !> \param energy_max Upper end of the physical energy interval.
     152              : !> \param regularization Regularization used for the fitted transform weights.
     153              : !> \param num_points_per_magnitude Number of fitting points per decade.
     154              : !> \param grid Grid object to fill.
     155              : !> \param build_frequency Whether to construct the frequency components.
     156              : !> \param build_time Whether to construct the imaginary-time components.
     157              : !> \param build_transforms Whether to construct the complete time/frequency transform matrices.
     158              : !> \param build_sine Whether to construct the sine time-to-frequency matrix.
     159              : !> \param time_scaling Scaling applied to the imaginary-time abscissas.
     160              : !> \param time_weight_scaling Scaling applied to the imaginary-time weights.
     161              : !> \param max_fit_error Maximum fitting error across all requested transforms.
     162              : !> \param print_warning Whether the minimax coefficient routine prints warnings.
     163              : !> \param unit_nr Output unit used by an external backend.
     164              : !> \param prefer_external_backend Whether to try an available external backend first.
     165              : !> \param used_external_backend Whether the external backend supplied the grid.
     166              : !> \param rcond If present and > 0, relative SVD cutoff of the fitted transform weights, used
     167              : !>        instead of the regularization.
     168              : ! **************************************************************************************************
     169          526 :    SUBROUTINE build_minimax_time_frequency_grid(num_points, energy_min, energy_max, regularization, &
     170              :                                                 num_points_per_magnitude, grid, build_frequency, build_time, &
     171              :                                                 build_transforms, build_sine, time_scaling, time_weight_scaling, &
     172              :                                                 max_fit_error, print_warning, unit_nr, prefer_external_backend, &
     173              :                                                 used_external_backend, rcond)
     174              : 
     175              :       INTEGER, INTENT(IN)                                :: num_points
     176              :       REAL(KIND=dp), INTENT(IN)                          :: energy_min, energy_max, regularization
     177              :       INTEGER, INTENT(IN)                                :: num_points_per_magnitude
     178              :       TYPE(time_frequency_grid_type), INTENT(OUT)        :: grid
     179              :       LOGICAL, INTENT(IN)                                :: build_frequency, build_time, &
     180              :                                                             build_transforms, build_sine
     181              :       REAL(KIND=dp), INTENT(IN)                          :: time_scaling, time_weight_scaling
     182              :       REAL(KIND=dp), INTENT(OUT)                         :: max_fit_error
     183              :       LOGICAL, INTENT(IN)                                :: print_warning
     184              :       INTEGER, INTENT(IN)                                :: unit_nr
     185              :       LOGICAL, INTENT(IN)                                :: prefer_external_backend
     186              :       LOGICAL, INTENT(OUT), OPTIONAL                     :: used_external_backend
     187              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: rcond
     188              : 
     189              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'build_minimax_time_frequency_grid'
     190              : 
     191              :       INTEGER                                            :: external_ierr, handle, ierr
     192              :       REAL(KIND=dp)                                      :: e_range, max_error
     193          526 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: points_and_weights
     194              : 
     195          526 :       CALL timeset(routineN, handle)
     196              : 
     197          526 :       IF (PRESENT(used_external_backend)) used_external_backend = .FALSE.
     198          526 :       IF (prefer_external_backend) THEN
     199              :          CALL greenx_get_minimax_grid(unit_nr, num_points, energy_min, energy_max, regularization, &
     200              :                                       grid%imaginary_time, grid%time_weights_at_zero_frequency, &
     201              :                                       grid%frequency, grid%frequency_weights, &
     202              :                                       grid%cosine_time_to_frequency, &
     203              :                                       grid%cosine_frequency_to_time, &
     204          216 :                                       grid%sine_time_to_frequency, external_ierr)
     205          216 :          IF (external_ierr == 0) THEN
     206           80 :             max_fit_error = 0.0_dp
     207           80 :             IF (PRESENT(used_external_backend)) used_external_backend = .TRUE.
     208           80 :             CALL timestop(handle)
     209              :             RETURN
     210              :          END IF
     211          136 :          CALL time_frequency_grid_release(grid)
     212              :       END IF
     213              : 
     214          446 :       CPASSERT(num_points > 0)
     215          446 :       CPASSERT(energy_min > 0.0_dp)
     216          446 :       CPASSERT(energy_max >= energy_min)
     217          446 :       CPASSERT(time_scaling > 0.0_dp)
     218          446 :       CPASSERT(time_weight_scaling > 0.0_dp)
     219          446 :       CPASSERT(.NOT. build_transforms .OR. (build_frequency .AND. build_time))
     220          446 :       CPASSERT(.NOT. build_sine .OR. build_transforms)
     221          446 :       max_error = 0.0_dp
     222              : 
     223          446 :       e_range = energy_max/energy_min
     224         1338 :       ALLOCATE (points_and_weights(2*num_points))
     225              : 
     226          446 :       IF (build_frequency) THEN
     227          382 :          IF (num_points <= 20) THEN
     228          366 :             CALL get_rpa_minimax_coeff(num_points, e_range, points_and_weights, ierr, print_warning)
     229              :          ELSE
     230           16 :             CALL get_rpa_minimax_coeff_larger_grid(num_points, e_range, points_and_weights)
     231              :          END IF
     232              : 
     233         1146 :          ALLOCATE (grid%frequency(num_points))
     234          764 :          ALLOCATE (grid%frequency_weights(num_points))
     235         5492 :          grid%frequency(:) = points_and_weights(1:num_points)*energy_min
     236         5492 :          grid%frequency_weights(:) = points_and_weights(num_points + 1:)*energy_min
     237          894 :          IF (num_points >= 26) grid%frequency_weights(:) = grid%frequency_weights(:)*4.0_dp
     238              :       END IF
     239              : 
     240          446 :       IF (build_time) THEN
     241          416 :          IF (num_points <= 20) THEN
     242          400 :             CALL get_exp_minimax_coeff(num_points, e_range, points_and_weights)
     243              :          ELSE
     244           16 :             CALL get_exp_minimax_coeff_gw(num_points, e_range, points_and_weights)
     245              :          END IF
     246              : 
     247         1248 :          ALLOCATE (grid%imaginary_time(num_points))
     248          832 :          ALLOCATE (grid%time_weights_at_zero_frequency(num_points))
     249         5650 :          grid%imaginary_time(:) = points_and_weights(1:num_points)/time_scaling/energy_min
     250         5650 :          grid%time_weights_at_zero_frequency(:) = points_and_weights(num_points + 1:)/time_weight_scaling/energy_min
     251              : 
     252          416 :          IF (build_transforms) THEN
     253         1408 :             ALLOCATE (grid%cosine_time_to_frequency(num_points, num_points))
     254              :             CALL fit_l_sq_weights(cosine_time_to_frequency, num_points, grid%imaginary_time, &
     255              :                                   grid%cosine_time_to_frequency, grid%frequency, energy_min, energy_max, &
     256          352 :                                   max_error, num_points_per_magnitude, regularization, rcond)
     257              : 
     258         1056 :             ALLOCATE (grid%cosine_frequency_to_time(num_points, num_points))
     259              :             CALL fit_l_sq_weights(cosine_frequency_to_time, num_points, grid%imaginary_time, &
     260              :                                   grid%cosine_frequency_to_time, grid%frequency, energy_min, energy_max, &
     261          352 :                                   max_error, num_points_per_magnitude, regularization, rcond)
     262              : 
     263          352 :             IF (build_sine) THEN
     264          960 :                ALLOCATE (grid%sine_time_to_frequency(num_points, num_points))
     265              :                CALL fit_l_sq_weights(sine_time_to_frequency, num_points, grid%imaginary_time, &
     266              :                                      grid%sine_time_to_frequency, grid%frequency, energy_min, energy_max, &
     267          320 :                                      max_error, num_points_per_magnitude, regularization, rcond)
     268              :             END IF
     269              :          END IF
     270              :       END IF
     271              : 
     272          446 :       max_fit_error = max_error
     273              : 
     274          446 :       CALL timestop(handle)
     275              : 
     276          972 :    END SUBROUTINE build_minimax_time_frequency_grid
     277              : 
     278              : ! **************************************************************************************************
     279              : !> \brief Calculate a complete least-squares time/frequency transform matrix.
     280              : !> \param transform_kind Type of transform to fit.
     281              : !> \param num_integ_points Number of integration points.
     282              : !> \param tau_tj Imaginary-time integration points.
     283              : !> \param weights Complete transform coefficients, including cosine/sine factors.
     284              : !>        Rows index output grid points, columns index input grid points.
     285              : !> \param omega_tj Frequency integration points.
     286              : !> \param E_min Lower end of the fitting interval.
     287              : !> \param E_max Upper end of the fitting interval.
     288              : !> \param max_error Maximum fitting error.
     289              : !> \param num_points_per_magnitude Number of fitting points per decade.
     290              : !> \param regularization Regularization parameter for the pseudoinverse.
     291              : !> \param rcond If present and > 0, singular values below rcond times the largest one are discarded
     292              : !>        (truncated SVD) instead of applying the regularization.
     293              : ! **************************************************************************************************
     294         1024 :    SUBROUTINE fit_l_sq_weights(transform_kind, num_integ_points, tau_tj, weights, omega_tj, E_min, E_max, &
     295              :                                max_error, num_points_per_magnitude, regularization, rcond)
     296              : 
     297              :       INTEGER, INTENT(IN)                                :: transform_kind, num_integ_points
     298              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: tau_tj
     299              :       REAL(KIND=dp), DIMENSION(:, :), INTENT(OUT)        :: weights
     300              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: omega_tj
     301              :       REAL(KIND=dp), INTENT(IN)                          :: E_min, E_max
     302              :       REAL(KIND=dp), INTENT(OUT)                         :: max_error
     303              :       INTEGER, INTENT(IN)                                :: num_points_per_magnitude
     304              :       REAL(KIND=dp), INTENT(IN)                          :: regularization
     305              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: rcond
     306              : 
     307              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'fit_l_sq_weights'
     308              : 
     309              :       INTEGER                                            :: handle, iii, info, jjj, jquad, lwork, &
     310              :                                                             num_x_nodes
     311         1024 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: iwork
     312              :       LOGICAL                                            :: sine_transform, time_to_frequency
     313              :       REAL(KIND=dp)                                      :: func_val, max_error_tmp, multiplicator, &
     314              :                                                             my_rcond, omega, s_cut, tau, x_value
     315         1024 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: sing_values, vec_UTy, weight_work, work, &
     316         1024 :                                                             x_values, y_values
     317         1024 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: mat_A, mat_SinvVSinvSigma, &
     318         1024 :                                                             mat_SinvVSinvT, mat_U
     319              : 
     320         1024 :       CALL timeset(routineN, handle)
     321              : 
     322         1024 :       CPASSERT(transform_kind >= cosine_time_to_frequency)
     323         1024 :       CPASSERT(transform_kind <= cosine_frequency_to_time)
     324         1024 :       CPASSERT(SIZE(tau_tj) == num_integ_points)
     325         1024 :       CPASSERT(SIZE(omega_tj) == num_integ_points)
     326         1024 :       CPASSERT(SIZE(weights, 1) == num_integ_points)
     327         1024 :       CPASSERT(SIZE(weights, 2) == num_integ_points)
     328              : 
     329         1024 :       time_to_frequency = transform_kind /= cosine_frequency_to_time
     330         1024 :       sine_transform = transform_kind == sine_time_to_frequency
     331              : 
     332         1024 :       my_rcond = 0.0_dp
     333         1024 :       IF (PRESENT(rcond)) my_rcond = rcond
     334              : 
     335              :       ! take num_points_per_magnitude points per 10-interval
     336         1024 :       num_x_nodes = (INT(LOG10(E_max/E_min)) + 1)*num_points_per_magnitude
     337              : 
     338              :       ! take at least as many x points as integration points to have clear
     339              :       ! input for the singular value decomposition
     340         1024 :       num_x_nodes = MAX(num_x_nodes, num_integ_points)
     341              : 
     342              :       ALLOCATE (x_values(num_x_nodes), y_values(num_x_nodes), mat_A(num_x_nodes, num_integ_points), &
     343              :                 weight_work(num_integ_points), sing_values(num_integ_points), mat_U(num_x_nodes, num_x_nodes), &
     344              :                 mat_SinvVSinvT(num_x_nodes, num_integ_points), work(8*num_integ_points*num_integ_points + &
     345              :                                                                   12*num_integ_points + 2*num_x_nodes), iwork(8*num_integ_points), &
     346        22528 :                 mat_SinvVSinvSigma(num_integ_points, num_x_nodes), vec_UTy(num_x_nodes))
     347         1024 :       mat_SinvVSinvSigma = 0.0_dp
     348              : 
     349              :       ! double the value nessary for 'A' to achieve good performance
     350         1024 :       lwork = SIZE(work)
     351              : 
     352         1024 :       multiplicator = (E_max/E_min)**(1.0_dp/(REAL(num_x_nodes, KIND=dp) - 1.0_dp))
     353       313824 :       DO iii = 1, num_x_nodes
     354       313824 :          x_values(iii) = E_min*multiplicator**(iii - 1)
     355              :       END DO
     356              : 
     357         1024 :       max_error = 0.0_dp
     358              : 
     359        15858 :       DO jquad = 1, num_integ_points
     360              : 
     361        14834 :          IF (time_to_frequency) THEN
     362         9860 :             omega = omega_tj(jquad)
     363              : 
     364         9860 :             IF (sine_transform) THEN
     365              :                ! y=2*omega/(x^2+omega^2)
     366      1364086 :                DO iii = 1, num_x_nodes
     367      1364086 :                   y_values(iii) = 2.0_dp*omega/(x_values(iii)**2 + omega**2)
     368              :                END DO
     369              :             ELSE
     370              :                ! y=2*x/(x^2+omega^2)
     371      1395374 :                DO iii = 1, num_x_nodes
     372      1395374 :                   y_values(iii) = 2.0_dp*x_values(iii)/(x_values(iii)**2 + omega**2)
     373              :                END DO
     374              :             END IF
     375              : 
     376       190864 :             DO jjj = 1, num_integ_points
     377     49234864 :                DO iii = 1, num_x_nodes
     378     49225004 :                   IF (sine_transform) THEN
     379     24477600 :                      mat_A(iii, jjj) = SIN(omega*tau_tj(jjj))*EXP(-x_values(iii)*tau_tj(jjj))
     380              :                   ELSE
     381     24566400 :                      mat_A(iii, jjj) = COS(omega*tau_tj(jjj))*EXP(-x_values(iii)*tau_tj(jjj))
     382              :                   END IF
     383              :                END DO
     384              :             END DO
     385              :          ELSE
     386         4974 :             tau = tau_tj(jquad)
     387              : 
     388              :             ! y=exp(-x*|tau|)
     389      1395374 :             DO iii = 1, num_x_nodes
     390      1395374 :                y_values(iii) = EXP(-x_values(iii)*tau)
     391              :             END DO
     392              : 
     393        95600 :             DO jjj = 1, num_integ_points
     394        90626 :                omega = omega_tj(jjj)
     395     24662000 :                DO iii = 1, num_x_nodes
     396     24566400 :                   x_value = x_values(iii)
     397     24657026 :                   mat_A(iii, jjj) = COS(tau*omega)*2.0_dp*x_value/(x_value**2 + omega**2)
     398              :                END DO
     399              :             END DO
     400              :          END IF
     401              : 
     402              :          ! Singular value decomposition of mat_A
     403              :          CALL DGESDD('A', num_x_nodes, num_integ_points, mat_A, num_x_nodes, sing_values, mat_U, num_x_nodes, &
     404        14834 :                      mat_SinvVSinvT, num_x_nodes, work, lwork, iwork, info)
     405        14834 :          CPASSERT(info == 0)
     406              : 
     407              :          ! integration weights = V Sigma U^T y; with rcond > 0, singular values below
     408              :          ! rcond*s_max are discarded (truncated SVD) instead of the Tikhonov regularization
     409        14834 :          IF (my_rcond > 0.0_dp) THEN
     410         7236 :             s_cut = my_rcond*sing_values(1)
     411       142320 :             DO jjj = 1, num_integ_points
     412      2971812 :                DO iii = 1, num_integ_points
     413      2964576 :                   IF (sing_values(jjj) > s_cut) THEN
     414      1702748 :                      mat_SinvVSinvSigma(iii, jjj) = mat_SinvVSinvT(jjj, iii)/sing_values(jjj)
     415              :                   ELSE
     416      1126744 :                      mat_SinvVSinvSigma(iii, jjj) = 0.0_dp
     417              :                   END IF
     418              :                END DO
     419              :             END DO
     420              :          ELSE
     421       144144 :             DO jjj = 1, num_integ_points
     422      2980106 :                DO iii = 1, num_integ_points
     423              :                   mat_SinvVSinvSigma(iii, jjj) = mat_SinvVSinvT(jjj, iii)*sing_values(jjj) &
     424      2972508 :                                                  /(regularization**2 + sing_values(jjj)**2)
     425              :                END DO
     426              :             END DO
     427              :          END IF
     428              : 
     429              :          CALL DGEMM('T', 'N', num_x_nodes, 1, num_x_nodes, 1.0_dp, mat_U, num_x_nodes, y_values, num_x_nodes, &
     430        14834 :                     0.0_dp, vec_UTy, num_x_nodes)
     431              :          CALL DGEMM('N', 'N', num_integ_points, 1, num_x_nodes, 1.0_dp, mat_SinvVSinvSigma, num_integ_points, &
     432        14834 :                     vec_UTy, num_x_nodes, 0.0_dp, weight_work, num_integ_points)
     433              : 
     434        14834 :          IF (sine_transform) THEN
     435        95264 :             weights(jquad, :) = weight_work(:)*SIN(omega*tau_tj(:))
     436         9948 :          ELSE IF (time_to_frequency) THEN
     437        95600 :             weights(jquad, :) = weight_work(:)*COS(omega*tau_tj(:))
     438              :          ELSE
     439        95600 :             weights(jquad, :) = weight_work(:)*COS(tau*omega_tj(:))
     440              :          END IF
     441              : 
     442        14834 :          max_error_tmp = 0.0_dp
     443      4154834 :          DO iii = 1, num_x_nodes
     444      4140000 :             func_val = 0.0_dp
     445      4140000 :             IF (time_to_frequency) THEN
     446     51793600 :                DO jjj = 1, num_integ_points
     447     51793600 :                   func_val = func_val + weights(jquad, jjj)*EXP(-x_values(iii)*tau_tj(jjj))
     448              :                END DO
     449              :             ELSE
     450     25956800 :                DO jjj = 1, num_integ_points
     451     24566400 :                   omega = omega_tj(jjj)
     452              :                   func_val = func_val + weights(jquad, jjj)*2.0_dp*x_values(iii) &
     453     25956800 :                              /(x_values(iii)**2 + omega**2)
     454              :                END DO
     455              :             END IF
     456      4154834 :             max_error_tmp = MAX(max_error_tmp, ABS(y_values(iii) - func_val))
     457              :          END DO
     458        15858 :          max_error = MAX(max_error, max_error_tmp)
     459              : 
     460              :       END DO
     461              : 
     462         1024 :       CALL timestop(handle)
     463              : 
     464         2048 :    END SUBROUTINE fit_l_sq_weights
     465              : 
     466              : ! **************************************************************************************************
     467              : !> \brief test the singular value decomposition for the computation of integration weights for the
     468              : !>         Fourier transform between time and frequency grid in cubic-scaling RPA
     469              : !> \param nR ...
     470              : !> \param iw ...
     471              : ! **************************************************************************************************
     472            0 :    SUBROUTINE test_least_square_ft(nR, iw)
     473              :       INTEGER, INTENT(IN)                                :: nR, iw
     474              : 
     475              :       REAL(KIND=dp), PARAMETER                           :: Rc_max = 1.0E+07_dp
     476              : 
     477              :       INTEGER                                            :: ierr, iR, num_integ_points
     478              :       REAL(KIND=dp)                                      :: max_error, multiplicator, Rc
     479            0 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: tau_tj, tj, x_tw
     480            0 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: weights_cos_tf_t_to_w
     481              : 
     482            0 :       multiplicator = Rc_max**(1.0_dp/(REAL(nR, KIND=dp) - 1.0_dp))
     483              : 
     484            0 :       DO num_integ_points = 1, 20
     485              : 
     486            0 :          ALLOCATE (x_tw(2*num_integ_points))
     487            0 :          x_tw = 0.0_dp
     488            0 :          ALLOCATE (tau_tj(num_integ_points))
     489            0 :          tau_tj = 0.0_dp
     490            0 :          ALLOCATE (weights_cos_tf_t_to_w(num_integ_points, num_integ_points))
     491            0 :          weights_cos_tf_t_to_w = 0.0_dp
     492            0 :          ALLOCATE (tj(num_integ_points))
     493            0 :          tj = 0.0_dp
     494              : 
     495            0 :          DO iR = 0, nR - 1
     496              : 
     497            0 :             Rc = 2.0_dp*multiplicator**iR
     498              : 
     499            0 :             ierr = 0
     500            0 :             CALL get_rpa_minimax_coeff(num_integ_points, Rc, x_tw, ierr, print_warning=.FALSE.)
     501              : 
     502            0 :             tj(:) = x_tw(1:num_integ_points)
     503              : 
     504            0 :             x_tw = 0.0_dp
     505              : 
     506            0 :             CALL get_exp_minimax_coeff(num_integ_points, Rc, x_tw)
     507              : 
     508            0 :             tau_tj(:) = x_tw(1:num_integ_points)/2.0_dp
     509              : 
     510              :             CALL fit_l_sq_weights(cosine_time_to_frequency, num_integ_points, tau_tj, &
     511            0 :                                   weights_cos_tf_t_to_w, tj, 1.0_dp, Rc, max_error, 200, 0.0_dp)
     512              : 
     513            0 :             IF (iw > 0) THEN
     514            0 :                WRITE (iw, '(T2, I3, F12.1, ES12.3)') num_integ_points, Rc, max_error
     515              :             END IF
     516              : 
     517              :          END DO
     518              : 
     519            0 :          DEALLOCATE (x_tw, tau_tj, weights_cos_tf_t_to_w, tj)
     520              : 
     521              :       END DO
     522              : 
     523            0 :    END SUBROUTINE test_least_square_ft
     524              : 
     525            0 : END MODULE time_frequency_grids
        

Generated by: LCOV version 2.0-1