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 Adaptive Clenshaw-Curtis quadrature algorithm to integrate a complex-valued function in
10 : !> a complex plane
11 : !> \par History
12 : !> * 05.2017 created [Sergey Chulkov]
13 : ! **************************************************************************************************
14 : MODULE negf_integr_cc
15 : USE cp_cfm_basic_linalg, ONLY: cp_cfm_scale,&
16 : cp_cfm_scale_and_add
17 : USE cp_cfm_types, ONLY: cp_cfm_create,&
18 : cp_cfm_get_info,&
19 : cp_cfm_release,&
20 : cp_cfm_type
21 : USE cp_fm_basic_linalg, ONLY: cp_fm_trace
22 : USE cp_fm_struct, ONLY: cp_fm_struct_equivalent,&
23 : cp_fm_struct_type
24 : USE cp_fm_types, ONLY: cp_fm_create,&
25 : cp_fm_get_info,&
26 : cp_fm_release,&
27 : cp_fm_type
28 : USE fft_api, ONLY: fft_1d_local,&
29 : fft_allocate,&
30 : fft_deallocate
31 : USE kahan_sum, ONLY: accurate_sum
32 : USE kinds, ONLY: dp
33 : USE mathconstants, ONLY: z_one,&
34 : z_zero
35 : USE negf_integr_utils, ONLY: contour_shape_arc,&
36 : contour_shape_linear,&
37 : equidistant_nodes_a_b,&
38 : rescale_nodes_cos,&
39 : rescale_normalised_nodes
40 : #include "./base/base_uses.f90"
41 :
42 : IMPLICIT NONE
43 : PRIVATE
44 :
45 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'negf_integr_cc'
46 : LOGICAL, PARAMETER, PRIVATE :: debug_this_module = .TRUE.
47 :
48 : INTEGER, PARAMETER, PUBLIC :: cc_interval_full = 0, &
49 : cc_interval_half = 1
50 :
51 : INTEGER, PARAMETER, PUBLIC :: cc_shape_linear = contour_shape_linear, &
52 : cc_shape_arc = contour_shape_arc
53 :
54 : PUBLIC :: ccquad_type
55 :
56 : PUBLIC :: ccquad_init, &
57 : ccquad_release, &
58 : ccquad_double_number_of_points, &
59 : ccquad_reduce_and_append_zdata, &
60 : ccquad_refine_integral
61 :
62 : ! **************************************************************************************************
63 : !> \brief Adaptive Clenshaw-Curtis environment.
64 : ! **************************************************************************************************
65 : TYPE ccquad_type
66 : !> integration lower and upper bounds
67 : COMPLEX(kind=dp) :: a = z_zero, b = z_zero
68 : !> integration interval:
69 : !> cc_interval_full -- [a .. b],
70 : !> grid density: 'a' .. . . . . . .. 'b';
71 : !> cc_interval_half -- [a .. 2b-a], assuming int_{b}^{2b-a} f(x) dx = 0,
72 : !> grid density: 'a' .. . . . 'b'
73 : INTEGER :: interval_id = -1
74 : !> integration shape
75 : INTEGER :: shape_id = -1
76 : !> estimated error
77 : REAL(kind=dp) :: error = -1.0_dp
78 : !> approximate integral value
79 : TYPE(cp_cfm_type), POINTER :: integral => NULL()
80 : !> error estimate for every element of the 'integral' matrix
81 : TYPE(cp_fm_type), POINTER :: error_fm => NULL()
82 : !> weights associated with matrix elements; the 'error' variable contains the value Trace(error_fm * weights)
83 : TYPE(cp_fm_type), POINTER :: weights => NULL()
84 : !> integrand value at grid points. Due to symmetry of Clenshaw-Curtis quadratures,
85 : !> we only need to keep the left half-interval
86 : TYPE(cp_cfm_type), ALLOCATABLE, DIMENSION(:) :: zdata_cache
87 : REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: tnodes
88 : END TYPE ccquad_type
89 :
90 : CONTAINS
91 :
92 : ! **************************************************************************************************
93 : !> \brief Initialise a Clenshaw-Curtis quadrature environment variable.
94 : !> \param cc_env environment variable to initialise
95 : !> \param xnodes points at which an integrand needs to be computed (initialised on exit)
96 : !> \param nnodes initial number of points to compute (initialised on exit)
97 : !> \param a integral lower bound
98 : !> \param b integral upper bound
99 : !> \param interval_id full [-1 .. 1] or half [-1 .. 0] interval
100 : !> \param shape_id shape of a curve along which the integral will be evaluated
101 : !> \param weights weights associated with matrix elements; used to compute cumulative error
102 : !> \param tnodes_restart list of nodes over the interval [-1 .. 1] from a previous integral evaluation.
103 : !> If present, the same set of 'xnodes' will be used to compute this integral.
104 : !> \par History
105 : !> * 05.2017 created [Sergey Chulkov]
106 : !> \note Clenshaw-Curtis quadratures are defined on the interval [-1 .. 1] and have non-uniforms node
107 : !> distribution which is symmetric and much sparse about 0. When the half-interval [-1 .. 0]
108 : !> is requested, the integrand value on another subinterval (0 .. 1] is assumed to be zero.
109 : !> Half interval mode is typically useful for rapidly decaying integrands (e.g. multiplied by
110 : !> Fermi function), so we do not actually need a fine grid spacing on this tail.
111 : ! **************************************************************************************************
112 0 : SUBROUTINE ccquad_init(cc_env, xnodes, nnodes, a, b, interval_id, shape_id, weights, tnodes_restart)
113 : TYPE(ccquad_type), INTENT(out) :: cc_env
114 : INTEGER, INTENT(inout) :: nnodes
115 : COMPLEX(kind=dp), DIMENSION(nnodes), INTENT(out) :: xnodes
116 : COMPLEX(kind=dp), INTENT(in) :: a, b
117 : INTEGER, INTENT(in) :: interval_id, shape_id
118 : TYPE(cp_fm_type), INTENT(IN) :: weights
119 : REAL(kind=dp), DIMENSION(nnodes), INTENT(in), &
120 : OPTIONAL :: tnodes_restart
121 :
122 : CHARACTER(len=*), PARAMETER :: routineN = 'ccquad_init'
123 :
124 : INTEGER :: handle, icol, ipoint, irow, ncols, &
125 : nnodes_half, nrows
126 : REAL(kind=dp), CONTIGUOUS, DIMENSION(:, :), &
127 0 : POINTER :: w_data, w_data_my
128 : TYPE(cp_fm_struct_type), POINTER :: fm_struct
129 :
130 0 : CALL timeset(routineN, handle)
131 :
132 0 : CPASSERT(nnodes > 2)
133 :
134 : ! ensure that MOD(nnodes-1, 2) == 0
135 0 : nnodes = 2*((nnodes - 1)/2) + 1
136 :
137 0 : cc_env%interval_id = interval_id
138 0 : cc_env%shape_id = shape_id
139 0 : cc_env%a = a
140 0 : cc_env%b = b
141 0 : cc_env%error = HUGE(0.0_dp)
142 :
143 0 : NULLIFY (cc_env%integral, cc_env%error_fm, cc_env%weights)
144 0 : ALLOCATE (cc_env%weights)
145 0 : CALL cp_fm_get_info(weights, local_data=w_data, nrow_local=nrows, ncol_local=ncols, matrix_struct=fm_struct)
146 0 : CALL cp_fm_create(cc_env%weights, fm_struct)
147 0 : CALL cp_fm_get_info(cc_env%weights, local_data=w_data_my)
148 :
149 : ! use the explicit loop to avoid temporary arrays
150 0 : DO icol = 1, ncols
151 0 : DO irow = 1, nrows
152 0 : w_data_my(irow, icol) = ABS(w_data(irow, icol))
153 : END DO
154 : END DO
155 :
156 0 : SELECT CASE (interval_id)
157 : CASE (cc_interval_full)
158 0 : nnodes_half = nnodes/2 + 1
159 : CASE (cc_interval_half)
160 0 : nnodes_half = nnodes
161 : CASE DEFAULT
162 0 : CPABORT("Unimplemented interval type")
163 : END SELECT
164 :
165 0 : ALLOCATE (cc_env%tnodes(nnodes))
166 :
167 0 : IF (PRESENT(tnodes_restart)) THEN
168 0 : cc_env%tnodes(1:nnodes) = tnodes_restart(1:nnodes)
169 : ELSE
170 0 : CALL equidistant_nodes_a_b(-1.0_dp, 0.0_dp, nnodes_half, cc_env%tnodes)
171 :
172 : ! rescale all but the end-points, as they are transformed into themselves (-1.0 -> -1.0; 0.0 -> 0.0).
173 : ! Moreover, by applying this rescaling transformation to the end-points we cannot guarantee the exact
174 : ! result due to rounding errors in evaluation of COS function.
175 0 : IF (nnodes_half > 2) THEN
176 0 : CALL rescale_nodes_cos(nnodes_half - 2, cc_env%tnodes(2:))
177 : END IF
178 :
179 0 : SELECT CASE (interval_id)
180 : CASE (cc_interval_full)
181 : ! reflect symmetric nodes
182 0 : DO ipoint = nnodes_half - 1, 1, -1
183 0 : cc_env%tnodes(nnodes_half + ipoint) = -cc_env%tnodes(nnodes_half - ipoint)
184 : END DO
185 : CASE (cc_interval_half)
186 : ! rescale half-interval : [-1 .. 0] -> [-1 .. 1]
187 0 : cc_env%tnodes(1:nnodes_half) = 2.0_dp*cc_env%tnodes(1:nnodes_half) + 1.0_dp
188 : END SELECT
189 : END IF
190 :
191 0 : CALL rescale_normalised_nodes(nnodes, cc_env%tnodes, a, b, shape_id, xnodes)
192 :
193 0 : CALL timestop(handle)
194 0 : END SUBROUTINE ccquad_init
195 :
196 : ! **************************************************************************************************
197 : !> \brief Release a Clenshaw-Curtis quadrature environment variable.
198 : !> \param cc_env environment variable to release (modified on exit)
199 : !> \par History
200 : !> * 05.2017 created [Sergey Chulkov]
201 : ! **************************************************************************************************
202 0 : SUBROUTINE ccquad_release(cc_env)
203 : TYPE(ccquad_type), INTENT(inout) :: cc_env
204 :
205 : CHARACTER(len=*), PARAMETER :: routineN = 'ccquad_release'
206 :
207 : INTEGER :: handle, ipoint
208 :
209 0 : CALL timeset(routineN, handle)
210 :
211 0 : IF (ASSOCIATED(cc_env%error_fm)) THEN
212 0 : CALL cp_fm_release(cc_env%error_fm)
213 0 : DEALLOCATE (cc_env%error_fm)
214 : NULLIFY (cc_env%error_fm)
215 : END IF
216 :
217 0 : IF (ASSOCIATED(cc_env%weights)) THEN
218 0 : CALL cp_fm_release(cc_env%weights)
219 0 : DEALLOCATE (cc_env%weights)
220 : NULLIFY (cc_env%weights)
221 : END IF
222 :
223 0 : IF (ASSOCIATED(cc_env%integral)) THEN
224 0 : CALL cp_cfm_release(cc_env%integral)
225 0 : DEALLOCATE (cc_env%integral)
226 : NULLIFY (cc_env%integral)
227 : END IF
228 :
229 0 : IF (ALLOCATED(cc_env%zdata_cache)) THEN
230 0 : DO ipoint = SIZE(cc_env%zdata_cache), 1, -1
231 0 : CALL cp_cfm_release(cc_env%zdata_cache(ipoint))
232 : END DO
233 :
234 0 : DEALLOCATE (cc_env%zdata_cache)
235 : END IF
236 :
237 0 : IF (ALLOCATED(cc_env%tnodes)) DEALLOCATE (cc_env%tnodes)
238 :
239 0 : CALL timestop(handle)
240 0 : END SUBROUTINE ccquad_release
241 :
242 : ! **************************************************************************************************
243 : !> \brief Get the next set of points at which the integrand needs to be computed. These points are
244 : !> then can be used to refine the integral approximation.
245 : !> \param cc_env environment variable (modified on exit)
246 : !> \param xnodes_next set of additional points (allocated and initialised on exit)
247 : !> \par History
248 : !> * 05.2017 created [Sergey Chulkov]
249 : ! **************************************************************************************************
250 0 : SUBROUTINE ccquad_double_number_of_points(cc_env, xnodes_next)
251 : TYPE(ccquad_type), INTENT(inout) :: cc_env
252 : COMPLEX(kind=dp), ALLOCATABLE, DIMENSION(:), &
253 : INTENT(inout) :: xnodes_next
254 :
255 : CHARACTER(len=*), PARAMETER :: routineN = 'ccquad_double_number_of_points'
256 :
257 : INTEGER :: handle, ipoint, nnodes_exist, &
258 : nnodes_half, nnodes_next
259 0 : REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: tnodes, tnodes_old
260 :
261 0 : CALL timeset(routineN, handle)
262 :
263 0 : CPASSERT(.NOT. ALLOCATED(xnodes_next))
264 0 : CPASSERT(ASSOCIATED(cc_env%integral))
265 0 : CPASSERT(ASSOCIATED(cc_env%error_fm))
266 0 : CPASSERT(ALLOCATED(cc_env%zdata_cache))
267 :
268 : ! due to symmetry of Clenshaw-Curtis quadratures, we only need to keep the left half-interval [-1 .. 0]
269 0 : nnodes_exist = SIZE(cc_env%zdata_cache)
270 : ! new nodes will be placed between the existed ones, so the number of nodes
271 : ! on the left half-interval [-1 .. 0] is equal to nnodes_exist - 1
272 0 : nnodes_half = nnodes_exist - 1
273 :
274 0 : SELECT CASE (cc_env%interval_id)
275 : CASE (cc_interval_full)
276 : ! double number of nodes as we have 2 half-intervals [-1 .. 0] and [0 .. 1]
277 0 : nnodes_next = 2*nnodes_half
278 : CASE (cc_interval_half)
279 0 : nnodes_next = nnodes_half
280 : CASE DEFAULT
281 0 : CPABORT("Unimplemented interval type")
282 : END SELECT
283 :
284 0 : ALLOCATE (xnodes_next(nnodes_next))
285 0 : ALLOCATE (tnodes(nnodes_next))
286 :
287 : CALL equidistant_nodes_a_b(0.5_dp/REAL(nnodes_half, kind=dp) - 1.0_dp, &
288 : -0.5_dp/REAL(nnodes_half, kind=dp), &
289 0 : nnodes_half, tnodes)
290 :
291 0 : CALL rescale_nodes_cos(nnodes_half, tnodes)
292 :
293 0 : SELECT CASE (cc_env%interval_id)
294 : CASE (cc_interval_full)
295 : ! reflect symmetric nodes
296 0 : DO ipoint = 1, nnodes_half
297 0 : tnodes(nnodes_half + ipoint) = -tnodes(nnodes_half - ipoint + 1)
298 : END DO
299 : CASE (cc_interval_half)
300 : ! rescale half-interval : [-1 .. 0] -> [-1 .. 1]
301 0 : tnodes(1:nnodes_half) = 2.0_dp*tnodes(1:nnodes_half) + 1.0_dp
302 : END SELECT
303 :
304 : ! append new tnodes to the cache
305 0 : CALL MOVE_ALLOC(cc_env%tnodes, tnodes_old)
306 0 : nnodes_exist = SIZE(tnodes_old)
307 :
308 0 : ALLOCATE (cc_env%tnodes(nnodes_exist + nnodes_next))
309 0 : cc_env%tnodes(1:nnodes_exist) = tnodes_old(1:nnodes_exist)
310 0 : cc_env%tnodes(nnodes_exist + 1:nnodes_exist + nnodes_next) = tnodes(1:nnodes_next)
311 0 : DEALLOCATE (tnodes_old)
312 :
313 : ! rescale nodes [-1 .. 1] -> [a .. b] according to the shape
314 0 : CALL rescale_normalised_nodes(nnodes_next, tnodes, cc_env%a, cc_env%b, cc_env%shape_id, xnodes_next)
315 :
316 0 : DEALLOCATE (tnodes)
317 0 : CALL timestop(handle)
318 0 : END SUBROUTINE ccquad_double_number_of_points
319 :
320 : ! **************************************************************************************************
321 : !> \brief Prepare Clenshaw-Curtis environment for the subsequent refinement of the integral.
322 : !> \param cc_env environment variable (modified on exit)
323 : !> \param zdata_next additional integrand value at additional points (modified on exit)
324 : !> \par History
325 : !> * 05.2017 created [Sergey Chulkov]
326 : !> \note Due to symmetry of Clenshaw-Curtis quadratures (weight(x) == weight(-x)), we do not need to
327 : !> keep all the matrices from 'zdata_next', only 'zdata_next(x) + zdata_next(-x)' is needed.
328 : !> In order to reduce the number of matrix allocations, we move some of the matrices from the
329 : !> end of the 'zdata_new' array to the 'cc_env%zdata_cache' array, and nullify the corresponding
330 : !> pointers at 'zdata_next' array. So the calling subroutine need to release the remained
331 : !> matrices or reuse them but taking into account the missed ones.
332 : ! **************************************************************************************************
333 0 : SUBROUTINE ccquad_reduce_and_append_zdata(cc_env, zdata_next)
334 : TYPE(ccquad_type), INTENT(inout) :: cc_env
335 : TYPE(cp_cfm_type), DIMENSION(:), INTENT(inout) :: zdata_next
336 :
337 : CHARACTER(len=*), PARAMETER :: routineN = 'ccquad_reduce_and_append_zdata'
338 : TYPE(cp_cfm_type), PARAMETER :: cfm_null = cp_cfm_type()
339 :
340 0 : COMPLEX(kind=dp), ALLOCATABLE, DIMENSION(:) :: zscale
341 : INTEGER :: handle, ipoint, nnodes_exist, &
342 : nnodes_half, nnodes_next
343 0 : TYPE(cp_cfm_type), ALLOCATABLE, DIMENSION(:) :: zdata_tmp
344 :
345 0 : CALL timeset(routineN, handle)
346 :
347 0 : nnodes_next = SIZE(zdata_next)
348 0 : CPASSERT(nnodes_next > 0)
349 :
350 : ! compute weights of new points on a complex contour according to their values of the 't' parameter
351 0 : nnodes_exist = SIZE(cc_env%tnodes)
352 0 : CPASSERT(nnodes_exist >= nnodes_next)
353 :
354 0 : ALLOCATE (zscale(nnodes_next))
355 : CALL rescale_normalised_nodes(nnodes_next, cc_env%tnodes(nnodes_exist - nnodes_next + 1:nnodes_exist), &
356 0 : cc_env%a, cc_env%b, cc_env%shape_id, weights=zscale)
357 :
358 0 : IF (cc_env%interval_id == cc_interval_half) zscale(:) = 2.0_dp*zscale(:)
359 :
360 : ! rescale integrand values
361 0 : DO ipoint = 1, nnodes_next
362 0 : CALL cp_cfm_scale(zscale(ipoint), zdata_next(ipoint))
363 : END DO
364 0 : DEALLOCATE (zscale)
365 :
366 : ! squash points with the same clenshaw-curtis weights together
367 0 : IF (ALLOCATED(cc_env%zdata_cache)) THEN
368 0 : nnodes_exist = SIZE(cc_env%zdata_cache)
369 : ELSE
370 : nnodes_exist = 0
371 : END IF
372 :
373 0 : SELECT CASE (cc_env%interval_id)
374 : CASE (cc_interval_full)
375 0 : IF (ALLOCATED(cc_env%zdata_cache)) THEN
376 0 : CPASSERT(nnodes_exist == nnodes_next/2 + 1)
377 0 : nnodes_half = nnodes_exist - 1
378 : ELSE
379 0 : CPASSERT(MOD(nnodes_next, 2) == 1)
380 0 : nnodes_half = nnodes_next/2 + 1
381 : END IF
382 : CASE (cc_interval_half)
383 0 : IF (ALLOCATED(cc_env%zdata_cache)) THEN
384 0 : CPASSERT(nnodes_exist == nnodes_next + 1)
385 : END IF
386 :
387 0 : nnodes_half = nnodes_next
388 : END SELECT
389 :
390 0 : IF (cc_env%interval_id == cc_interval_full) THEN
391 0 : DO ipoint = nnodes_next/2, 1, -1
392 0 : CALL cp_cfm_scale_and_add(z_one, zdata_next(ipoint), z_one, zdata_next(nnodes_next - ipoint + 1))
393 : END DO
394 : END IF
395 :
396 0 : IF (ALLOCATED(cc_env%zdata_cache)) THEN
397 : ! note that nnodes_half+1 == nnodes_exist for both half- and full-intervals
398 0 : ALLOCATE (zdata_tmp(nnodes_half + nnodes_exist))
399 :
400 0 : DO ipoint = 1, nnodes_half
401 0 : zdata_tmp(2*ipoint - 1) = cc_env%zdata_cache(ipoint)
402 0 : zdata_tmp(2*ipoint) = zdata_next(ipoint)
403 0 : zdata_next(ipoint) = cfm_null
404 : END DO
405 0 : zdata_tmp(nnodes_half + nnodes_exist) = cc_env%zdata_cache(nnodes_exist)
406 :
407 0 : CALL MOVE_ALLOC(zdata_tmp, cc_env%zdata_cache)
408 : ELSE
409 0 : CALL cp_cfm_scale(2.0_dp, zdata_next(nnodes_half))
410 :
411 0 : ALLOCATE (cc_env%zdata_cache(nnodes_half))
412 :
413 0 : DO ipoint = 1, nnodes_half
414 0 : cc_env%zdata_cache(ipoint) = zdata_next(ipoint)
415 0 : zdata_next(ipoint) = cfm_null
416 : END DO
417 : END IF
418 :
419 0 : CALL timestop(handle)
420 0 : END SUBROUTINE ccquad_reduce_and_append_zdata
421 :
422 : ! **************************************************************************************************
423 : !> \brief Refine approximated integral.
424 : !> \param cc_env environment variable (modified on exit)
425 : !> \par History
426 : !> * 05.2017 created [Sergey Chulkov]
427 : ! **************************************************************************************************
428 0 : SUBROUTINE ccquad_refine_integral(cc_env)
429 : TYPE(ccquad_type), INTENT(inout) :: cc_env
430 :
431 : CHARACTER(len=*), PARAMETER :: routineN = 'ccquad_refine_integral'
432 :
433 : COMPLEX(kind=dp), CONTIGUOUS, DIMENSION(:, :, :), &
434 0 : POINTER :: ztmp, ztmp_dct
435 : INTEGER :: handle, icol, ipoint, irow, ncols_local, nintervals, nintervals_half, &
436 : nintervals_half_plus_1, nintervals_half_plus_2, nintervals_plus_2, nrows_local
437 : LOGICAL :: equiv
438 : REAL(kind=dp) :: rscale
439 0 : REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: weights
440 : TYPE(cp_fm_struct_type), POINTER :: fm_struct
441 :
442 0 : CALL timeset(routineN, handle)
443 :
444 0 : CPASSERT(ALLOCATED(cc_env%zdata_cache))
445 :
446 0 : nintervals_half_plus_1 = SIZE(cc_env%zdata_cache)
447 0 : nintervals_half = nintervals_half_plus_1 - 1
448 0 : nintervals_half_plus_2 = nintervals_half_plus_1 + 1
449 0 : nintervals = 2*nintervals_half
450 0 : nintervals_plus_2 = nintervals + 2
451 0 : CPASSERT(nintervals_half > 1)
452 :
453 0 : IF (.NOT. ASSOCIATED(cc_env%integral)) THEN
454 0 : CALL cp_cfm_get_info(cc_env%zdata_cache(1), matrix_struct=fm_struct)
455 0 : equiv = cp_fm_struct_equivalent(fm_struct, cc_env%weights%matrix_struct)
456 0 : CPASSERT(equiv)
457 :
458 0 : ALLOCATE (cc_env%integral)
459 0 : CALL cp_cfm_create(cc_env%integral, fm_struct)
460 : NULLIFY (cc_env%error_fm)
461 0 : ALLOCATE (cc_env%error_fm)
462 0 : CALL cp_fm_create(cc_env%error_fm, fm_struct)
463 : END IF
464 :
465 : IF (debug_this_module) THEN
466 0 : DO ipoint = 1, nintervals_half_plus_1
467 0 : equiv = cp_fm_struct_equivalent(cc_env%zdata_cache(ipoint)%matrix_struct, cc_env%integral%matrix_struct)
468 0 : CPASSERT(equiv)
469 : END DO
470 : END IF
471 :
472 0 : CALL cp_cfm_get_info(cc_env%integral, nrow_local=nrows_local, ncol_local=ncols_local)
473 :
474 0 : ALLOCATE (weights(nintervals_half))
475 :
476 : ! omit the trivial weights(1) = 0.5
477 0 : DO ipoint = 2, nintervals_half
478 0 : rscale = REAL(2*(ipoint - 1), kind=dp)
479 0 : weights(ipoint) = 1.0_dp/(1.0_dp - rscale*rscale)
480 : END DO
481 : ! weights(1) <- weights(intervals_half + 1)
482 0 : rscale = REAL(nintervals, kind=dp)
483 0 : weights(1) = 1.0_dp/(1.0_dp - rscale*rscale)
484 :
485 : ! 1.0 / nintervals
486 0 : rscale = 1.0_dp/rscale
487 :
488 0 : CALL fft_allocate(ztmp, [nintervals, nrows_local, ncols_local])
489 0 : CALL fft_allocate(ztmp_dct, [nintervals, nrows_local, ncols_local])
490 :
491 : !$OMP PARALLEL DO DEFAULT(NONE), PRIVATE(icol, ipoint, irow), &
492 0 : !$OMP SHARED(cc_env, ncols_local, nintervals_half, nintervals_half_plus_1, nintervals_half_plus_2, nrows_local, ztmp)
493 : DO icol = 1, ncols_local
494 : DO irow = 1, nrows_local
495 : DO ipoint = 1, nintervals_half_plus_1
496 : ztmp(ipoint, irow, icol) = cc_env%zdata_cache(ipoint)%local_data(irow, icol)
497 : END DO
498 :
499 : DO ipoint = 2, nintervals_half
500 : ztmp(nintervals_half + ipoint, irow, icol) = ztmp(nintervals_half_plus_2 - ipoint, irow, icol)
501 : END DO
502 : END DO
503 : END DO
504 : !$OMP END PARALLEL DO
505 :
506 0 : CALL fft_1d_local(.FALSE., nintervals, nrows_local*ncols_local, .FALSE., .FALSE., nintervals, nintervals, ztmp, ztmp_dct)
507 :
508 : !$OMP PARALLEL DO DEFAULT(NONE), PRIVATE(icol, ipoint, irow), &
509 : !$OMP SHARED(cc_env, rscale, ncols_local, nintervals_half, nintervals_half_plus_1, nintervals_plus_2), &
510 0 : !$OMP SHARED(nrows_local, weights, ztmp_dct)
511 : DO icol = 1, ncols_local
512 : DO irow = 1, nrows_local
513 : ztmp_dct(1, irow, icol) = 0.5_dp*ztmp_dct(1, irow, icol)
514 : DO ipoint = 2, nintervals_half
515 : ztmp_dct(ipoint, irow, icol) = 0.5_dp*weights(ipoint)*(ztmp_dct(ipoint, irow, icol) + &
516 : ztmp_dct(nintervals_plus_2 - ipoint, irow, icol))
517 : END DO
518 : ztmp_dct(nintervals_half_plus_1, irow, icol) = weights(1)*ztmp_dct(nintervals_half_plus_1, irow, icol)
519 :
520 : cc_env%integral%local_data(irow, icol) = rscale*accurate_sum(ztmp_dct(1:nintervals_half_plus_1, irow, icol))
521 : cc_env%error_fm%local_data(irow, icol) = rscale*ABS(ztmp_dct(nintervals_half_plus_1, irow, icol))
522 : END DO
523 : END DO
524 : !$OMP END PARALLEL DO
525 :
526 0 : CALL fft_deallocate(ztmp)
527 0 : CALL fft_deallocate(ztmp_dct)
528 :
529 0 : CALL cp_fm_trace(cc_env%error_fm, cc_env%weights, cc_env%error)
530 :
531 0 : DEALLOCATE (weights)
532 0 : CALL timestop(handle)
533 0 : END SUBROUTINE ccquad_refine_integral
534 :
535 0 : END MODULE negf_integr_cc
|