22#include "./base/base_uses.f90"
28 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'time_frequency_grids'
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
38 REAL(kind=
dp),
DIMENSION(:, :),
ALLOCATABLE :: cosine_frequency_to_time
39 REAL(kind=
dp),
DIMENSION(:, :),
ALLOCATABLE :: sine_time_to_frequency
43 sine_time_to_frequency = 2, &
44 cosine_frequency_to_time = 3
60 IF (
ALLOCATED(grid%frequency))
DEALLOCATE (grid%frequency)
61 IF (
ALLOCATED(grid%frequency_weights))
DEALLOCATE (grid%frequency_weights)
62 IF (
ALLOCATED(grid%imaginary_time))
DEALLOCATE (grid%imaginary_time)
63 IF (
ALLOCATED(grid%time_weights_at_zero_frequency))
DEALLOCATE (grid%time_weights_at_zero_frequency)
64 IF (
ALLOCATED(grid%cosine_time_to_frequency))
DEALLOCATE (grid%cosine_time_to_frequency)
65 IF (
ALLOCATED(grid%cosine_frequency_to_time))
DEALLOCATE (grid%cosine_frequency_to_time)
66 IF (
ALLOCATED(grid%sine_time_to_frequency))
DEALLOCATE (grid%sine_time_to_frequency)
81 REAL(kind=
dp),
DIMENSION(:, :, :),
INTENT(IN) :: samples_tau
82 COMPLEX(KIND=dp),
DIMENSION(:, :),
INTENT(OUT) :: values_omega
84 CHARACTER(LEN=*),
PARAMETER :: routinen =
'transform_time_to_frequency'
86 INTEGER :: handle, ifreq, itime, nstate
87 REAL(kind=
dp) :: weight_cos, weight_sin
88 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: samples_even, samples_odd
90 CALL timeset(routinen, handle)
92 nstate =
SIZE(samples_tau, 1)
93 cpassert(
SIZE(samples_tau, 3) == 2)
94 cpassert(
SIZE(values_omega, 1) == nstate)
95 cpassert(
SIZE(samples_tau, 2) ==
SIZE(grid%cosine_time_to_frequency, 2))
96 cpassert(
SIZE(values_omega, 2) <=
SIZE(grid%cosine_time_to_frequency, 1))
98 ALLOCATE (samples_even(nstate), samples_odd(nstate))
100 DO itime = 1,
SIZE(samples_tau, 2)
103 DO ifreq = 1,
SIZE(values_omega, 2)
104 weight_cos = grid%cosine_time_to_frequency(ifreq, itime)
105 weight_sin = grid%sine_time_to_frequency(ifreq, itime)
106 values_omega(:, ifreq) = values_omega(:, ifreq) + &
107 cmplx(weight_cos*samples_even, weight_sin*samples_odd, kind=
dp)
111 CALL timestop(handle)
122 INTEGER,
INTENT(IN) :: num_points
125 CHARACTER(LEN=*),
PARAMETER :: routinen =
'build_clenshaw_grid'
127 INTEGER :: handle, jquad
129 CALL timeset(routinen, handle)
131 cpassert(num_points > 0)
133 ALLOCATE (grid%frequency(num_points), grid%frequency_weights(num_points))
135 DO jquad = 1, num_points - 1
136 grid%frequency(jquad) = jquad*
pi/(2.0_dp*num_points)
137 grid%frequency_weights(jquad) =
pi/(num_points*sin(grid%frequency(jquad))**2)
139 grid%frequency(num_points) =
pi/2.0_dp
140 grid%frequency_weights(num_points) = &
141 pi/(2.0_dp*num_points*sin(grid%frequency(num_points))**2)
143 CALL timestop(handle)
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)
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
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
189 CHARACTER(LEN=*),
PARAMETER :: routinen =
'build_minimax_time_frequency_grid'
191 INTEGER :: external_ierr, handle, ierr
192 REAL(kind=
dp) :: e_range, max_error
193 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: points_and_weights
195 CALL timeset(routinen, handle)
197 IF (
PRESENT(used_external_backend)) used_external_backend = .false.
198 IF (prefer_external_backend)
THEN
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 grid%sine_time_to_frequency, external_ierr)
205 IF (external_ierr == 0)
THEN
206 max_fit_error = 0.0_dp
207 IF (
PRESENT(used_external_backend)) used_external_backend = .true.
208 CALL timestop(handle)
214 cpassert(num_points > 0)
215 cpassert(energy_min > 0.0_dp)
216 cpassert(energy_max >= energy_min)
217 cpassert(time_scaling > 0.0_dp)
218 cpassert(time_weight_scaling > 0.0_dp)
219 cpassert(.NOT. build_transforms .OR. (build_frequency .AND. build_time))
220 cpassert(.NOT. build_sine .OR. build_transforms)
223 e_range = energy_max/energy_min
224 ALLOCATE (points_and_weights(2*num_points))
226 IF (build_frequency)
THEN
227 IF (num_points <= 20)
THEN
233 ALLOCATE (grid%frequency(num_points))
234 ALLOCATE (grid%frequency_weights(num_points))
235 grid%frequency(:) = points_and_weights(1:num_points)*energy_min
236 grid%frequency_weights(:) = points_and_weights(num_points + 1:)*energy_min
237 IF (num_points >= 26) grid%frequency_weights(:) = grid%frequency_weights(:)*4.0_dp
241 IF (num_points <= 20)
THEN
247 ALLOCATE (grid%imaginary_time(num_points))
248 ALLOCATE (grid%time_weights_at_zero_frequency(num_points))
249 grid%imaginary_time(:) = points_and_weights(1:num_points)/time_scaling/energy_min
250 grid%time_weights_at_zero_frequency(:) = points_and_weights(num_points + 1:)/time_weight_scaling/energy_min
252 IF (build_transforms)
THEN
253 ALLOCATE (grid%cosine_time_to_frequency(num_points, num_points))
255 grid%cosine_time_to_frequency, grid%frequency, energy_min, energy_max, &
256 max_error, num_points_per_magnitude, regularization, rcond)
258 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 max_error, num_points_per_magnitude, regularization, rcond)
264 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 max_error, num_points_per_magnitude, regularization, rcond)
272 max_fit_error = max_error
274 CALL timestop(handle)
294 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)
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
307 CHARACTER(LEN=*),
PARAMETER :: routinen =
'fit_l_sq_weights'
309 INTEGER :: handle, iii, info, jjj, jquad, lwork, &
311 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 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: sing_values, vec_uty, weight_work, work, &
317 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: mat_a, mat_sinvvsinvsigma, &
318 mat_sinvvsinvt, mat_u
320 CALL timeset(routinen, handle)
323 cpassert(transform_kind <= cosine_frequency_to_time)
324 cpassert(
SIZE(tau_tj) == num_integ_points)
325 cpassert(
SIZE(omega_tj) == num_integ_points)
326 cpassert(
SIZE(weights, 1) == num_integ_points)
327 cpassert(
SIZE(weights, 2) == num_integ_points)
329 time_to_frequency = transform_kind /= cosine_frequency_to_time
330 sine_transform = transform_kind == sine_time_to_frequency
333 IF (
PRESENT(rcond)) my_rcond = rcond
336 num_x_nodes = (int(log10(e_max/e_min)) + 1)*num_points_per_magnitude
340 num_x_nodes = max(num_x_nodes, num_integ_points)
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 mat_sinvvsinvsigma(num_integ_points, num_x_nodes), vec_uty(num_x_nodes))
347 mat_sinvvsinvsigma = 0.0_dp
352 multiplicator = (e_max/e_min)**(1.0_dp/(real(num_x_nodes, kind=
dp) - 1.0_dp))
353 DO iii = 1, num_x_nodes
354 x_values(iii) = e_min*multiplicator**(iii - 1)
359 DO jquad = 1, num_integ_points
361 IF (time_to_frequency)
THEN
362 omega = omega_tj(jquad)
364 IF (sine_transform)
THEN
366 DO iii = 1, num_x_nodes
367 y_values(iii) = 2.0_dp*omega/(x_values(iii)**2 + omega**2)
371 DO iii = 1, num_x_nodes
372 y_values(iii) = 2.0_dp*x_values(iii)/(x_values(iii)**2 + omega**2)
376 DO jjj = 1, num_integ_points
377 DO iii = 1, num_x_nodes
378 IF (sine_transform)
THEN
379 mat_a(iii, jjj) = sin(omega*tau_tj(jjj))*exp(-x_values(iii)*tau_tj(jjj))
381 mat_a(iii, jjj) = cos(omega*tau_tj(jjj))*exp(-x_values(iii)*tau_tj(jjj))
389 DO iii = 1, num_x_nodes
390 y_values(iii) = exp(-x_values(iii)*tau)
393 DO jjj = 1, num_integ_points
394 omega = omega_tj(jjj)
395 DO iii = 1, num_x_nodes
396 x_value = x_values(iii)
397 mat_a(iii, jjj) = cos(tau*omega)*2.0_dp*x_value/(x_value**2 + omega**2)
403 CALL dgesdd(
'A', num_x_nodes, num_integ_points, mat_a, num_x_nodes, sing_values, mat_u, num_x_nodes, &
404 mat_sinvvsinvt, num_x_nodes, work, lwork, iwork, info)
409 IF (my_rcond > 0.0_dp)
THEN
410 s_cut = my_rcond*sing_values(1)
411 DO jjj = 1, num_integ_points
412 DO iii = 1, num_integ_points
413 IF (sing_values(jjj) > s_cut)
THEN
414 mat_sinvvsinvsigma(iii, jjj) = mat_sinvvsinvt(jjj, iii)/sing_values(jjj)
416 mat_sinvvsinvsigma(iii, jjj) = 0.0_dp
421 DO jjj = 1, num_integ_points
422 DO iii = 1, num_integ_points
423 mat_sinvvsinvsigma(iii, jjj) = mat_sinvvsinvt(jjj, iii)*sing_values(jjj) &
424 /(regularization**2 + sing_values(jjj)**2)
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 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 vec_uty, num_x_nodes, 0.0_dp, weight_work, num_integ_points)
434 IF (sine_transform)
THEN
435 weights(jquad, :) = weight_work(:)*sin(omega*tau_tj(:))
436 ELSE IF (time_to_frequency)
THEN
437 weights(jquad, :) = weight_work(:)*cos(omega*tau_tj(:))
439 weights(jquad, :) = weight_work(:)*cos(tau*omega_tj(:))
442 max_error_tmp = 0.0_dp
443 DO iii = 1, num_x_nodes
445 IF (time_to_frequency)
THEN
446 DO jjj = 1, num_integ_points
447 func_val = func_val + weights(jquad, jjj)*exp(-x_values(iii)*tau_tj(jjj))
450 DO jjj = 1, num_integ_points
451 omega = omega_tj(jjj)
452 func_val = func_val + weights(jquad, jjj)*2.0_dp*x_values(iii) &
453 /(x_values(iii)**2 + omega**2)
456 max_error_tmp = max(max_error_tmp, abs(y_values(iii) - func_val))
458 max_error = max(max_error, max_error_tmp)
462 CALL timestop(handle)
464 END SUBROUTINE fit_l_sq_weights
473 INTEGER,
INTENT(IN) :: nr, iw
475 REAL(kind=
dp),
PARAMETER :: rc_max = 1.0e+07_dp
477 INTEGER :: ierr, ir, num_integ_points
478 REAL(kind=
dp) :: max_error, multiplicator, rc
479 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: tau_tj, tj, x_tw
480 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: weights_cos_tf_t_to_w
482 multiplicator = rc_max**(1.0_dp/(real(nr, kind=
dp) - 1.0_dp))
484 DO num_integ_points = 1, 20
486 ALLOCATE (x_tw(2*num_integ_points))
488 ALLOCATE (tau_tj(num_integ_points))
490 ALLOCATE (weights_cos_tf_t_to_w(num_integ_points, num_integ_points))
491 weights_cos_tf_t_to_w = 0.0_dp
492 ALLOCATE (tj(num_integ_points))
497 rc = 2.0_dp*multiplicator**ir
502 tj(:) = x_tw(1:num_integ_points)
508 tau_tj(:) = x_tw(1:num_integ_points)/2.0_dp
511 weights_cos_tf_t_to_w, tj, 1.0_dp, rc, max_error, 200, 0.0_dp)
514 WRITE (iw,
'(T2, I3, F12.1, ES12.3)') num_integ_points, rc, max_error
519 DEALLOCATE (x_tw, tau_tj, weights_cos_tf_t_to_w, tj)
static void dgemm(const char transa, const char transb, const int m, const int n, const int k, const double alpha, const double *a, const int lda, const double *b, const int ldb, const double beta, double *c, const int ldc)
Convenient wrapper to hide Fortran nature of dgemm_, swapping a and b.
Interface to the Greenx library.
subroutine, public greenx_get_minimax_grid(unit_nr, num_integ_points, emin, emax, regularization_minimax, imaginary_time, time_weights_at_zero_frequency, frequency, frequency_weights, cosine_time_to_frequency, cosine_frequency_to_time, sine_time_to_frequency, ierr)
Get a minimax grid with complete transform matrices from GreenX when it is available.
Defines the basic variable types.
integer, parameter, public dp
Definition of mathematical constants and functions.
real(kind=dp), parameter, public pi
complex(kind=dp), parameter, public z_zero
Routines to calculate the minimax coefficients in order to approximate 1/x as a sum over exponential ...
subroutine, public get_exp_minimax_coeff_gw(k, e_range, aw)
...
Routines to calculate the minimax coefficients in order to approximate 1/x as a sum over exponential ...
subroutine, public get_exp_minimax_coeff(k, rc, aw, mm_error, which_coeffs)
Get best minimax approximation for given input parameters. Automatically chooses the most exact set o...
Routines to calculate the minimax coefficients for approximating 1/x as 1/x ~ 1/pi SUM_{i}...
subroutine, public get_rpa_minimax_coeff_larger_grid(k, e_range, aw)
...
subroutine, public get_rpa_minimax_coeff(k, e_range, aw, ierr, print_warning)
The a_i and w_i coefficient are stored in aw such that the first 1:K elements correspond to a_i and t...
Definition and construction of time/frequency grids for correlation methods.
integer, parameter cosine_time_to_frequency
subroutine, public build_clenshaw_grid(num_points, grid)
Build a Clenshaw-Curtis frequency grid.
subroutine, public transform_time_to_frequency(grid, samples_tau, values_omega)
Transform real positive/negative-time samples to complex positive-frequency values....
integer, parameter, public negative_time
integer, parameter, public positive_time
subroutine, public test_least_square_ft(nr, iw)
test the singular value decomposition for the computation of integration weights for the Fourier tran...
subroutine, public time_frequency_grid_release(grid)
Release all data owned by a time_frequency_grid_type object.
subroutine, public build_minimax_time_frequency_grid(num_points, energy_min, energy_max, regularization, num_points_per_magnitude, grid, build_frequency, build_time, build_transforms, build_sine, time_scaling, time_weight_scaling, max_fit_error, print_warning, unit_nr, prefer_external_backend, used_external_backend, rcond)
Build a minimax time/frequency grid through the common backend boundary.