52 para_env => logger%para_env
53 CALL check_cube_values()
59 CALL check_pw_integral_and_poisson()
60 CALL check_mt2d_kernel()
61 print
'(A,ES16.7)',
'PW integral and MT2D kernel maximum scaled error: ', max_error
71 INTEGER :: alias, axis, ig, nalias
72 LOGICAL :: boundary(3)
73 REAL(dp) :: cell(3, 3), point(3), vectors(3, 8)
80 cell(axis, axis) = 6.0_dp
84 point = matmul(grid%dh, real([1, 2, -1], dp))
85 DO ig = 1,
SIZE(grid%g, 2)
87 boundary = abs(grid%g_hat(:, ig)) == 3
88 IF (nalias /= 2**count(boundary)) error stop
"Missing real Fourier boundary aliases."
91 phase = phase + exp(cmplx(0.0_dp, dot_product(vectors(:, alias), point), dp))/real(nalias, dp)
93 IF (abs(phase - exp(cmplx(0.0_dp, dot_product(grid%g(:, ig), point), dp))) > 3.e-14_dp)
THEN
94 error stop
"Fourier boundary aliases changed the native grid phase."
96 IF (boundary(1) .AND. boundary(2))
THEN
97 IF (abs(sum(vectors(1, :nalias)*vectors(2, :nalias))/real(nalias, dp)) > 3.e-14_dp)
THEN
98 error stop
"A mixed odd kernel survived independent Nyquist reflection."
108 SUBROUTINE check_cube_values()
109 CHARACTER(LEN=*),
PARAMETER :: fixture =
"pw_unittest_free_format.cube"
110 INTEGER,
PARAMETER :: n = 5
112 INTEGER :: first, i, j, unit_nr
113 REAL(KIND=
dp) :: cell(3, 3), reference(n**3)
117 reference = [(real(i,
dp), i=1,
SIZE(reference))]
118 reference(:2) = [-3.33004e-101_dp, -1.32487e-103_dp]
119 IF (para_env%is_source())
THEN
120 CALL open_file(fixture, unit_nr, file_status=
"REPLACE", file_action=
"WRITE")
121 WRITE (unit_nr,
'(A)')
"pw_unittest",
"",
"0 0 0 0",
"5 1 0 0",
"5 0 1 0",
"5 0 0 1"
123 WRITE (unit_nr,
'(A)')
""
124 WRITE (unit_nr,
'(A,*(I0,A1))')
"3.0D0 ", (i, achar(9), i=4,
SIZE(reference))
132 cell(i, i) = real(n,
dp)
134 CALL pw_grid_create(grid, para_env, cell, npts=[n, n, n], fft_usage=.false.)
136 CALL cube_to_pw(pw, fixture, 1.0_dp, para_env%num_pe > 1, silent=.true.)
137 DO i = grid%bounds_local(1, 1), grid%bounds_local(2, 1)
138 DO j = grid%bounds_local(1, 2), grid%bounds_local(2, 2)
139 first = ((i - grid%bounds(1, 1))*n + j - grid%bounds(1, 2))*n + 1
140 IF (.NOT. all(abs(pw%array(i, j, :)/reference(first:first + n - 1) - 1.0_dp) <= 4*epsilon(1.0_dp)))
THEN
141 error stop
"Incorrect cube values."
147 IF (para_env%is_source())
THEN
148 CALL open_file(fixture, unit_nr, file_status=
"OLD")
149 CALL close_file(unit_nr, file_status=
"DELETE")
151 END SUBROUTINE check_cube_values
158 SUBROUTINE check_copy(mode)
159 INTEGER,
INTENT(IN) :: mode
161 COMPLEX(dp),
PARAMETER :: untouched = (-999.0_dp, 17.0_dp)
163 COMPLEX(dp) :: expected
164 INTEGER :: dimensions(1), i, j, nsource, ntarget, &
169 rank = para_env%mepos
171 IF (mode == 6 .OR. (mode >= 7 .AND. rank == 0)) nsource = 0
173 IF (mode == 3 .OR. mode == 8) owner = mod(rank + 1, para_env%num_pe)
175 IF (mode == 4) ntarget = nsource - 1
176 IF (mode == 5) ntarget = nsource + 1
177 IF (mode == 8) ntarget = merge(0, 7, owner == 0)
178 ALLOCATE (source_grid%g_hat(3, nsource), target_grid%g_hat(3, ntarget))
179 ALLOCATE (source%array(nsource), destination%array(ntarget))
180 source%pw_grid => source_grid
181 destination%pw_grid => target_grid
182 dimensions = [para_env%num_pe]
183 CALL source_grid%para%group%create(para_env, 1, dimensions)
184 target_grid%para%group = source_grid%para%group
185 source_grid%ngpts_cut_local = nsource
186 target_grid%ngpts_cut_local = ntarget
188 source_grid%g_hat(:, i) = [i, rank, 0]
189 source%array(i) = cmplx(i + 11*rank, -i - 3*rank,
dp)
193 IF (mode == 1 .OR. (mode == 2 .AND. rank == 0)) j = ntarget + 1 - i
194 target_grid%g_hat(:, i) = [j, owner, 0]
196 destination%array(:) = untouched
199 j = target_grid%g_hat(1, i)
200 expected = cmplx(j + 11*owner, -j - 3*owner,
dp)
201 IF (mode == 5 .AND. j > nsource) expected = untouched
202 IF (abs(destination%array(i) - expected) > 1.e-14_dp) error stop
'Incorrect PW copy'
204 DEALLOCATE (source%array, destination%array)
205 CALL source_grid%para%group%free()
206 END SUBROUTINE check_copy
211 SUBROUTINE check_pw_integral_and_poisson()
212 REAL(dp),
PARAMETER :: step = 5.e-4_dp, &
213 strain_steps(4) = [step, -step, 2.0_dp*step, -2.0_dp*step]
215 COMPLEX(dp) :: a_mode, b_mode
216 INTEGER :: axis, ig, sign_step, span, sumtype, test
217 REAL(dp) :: cell(3, 3), derivative, energy, energy_dv, energy_v, energy_v_dv, expected, &
218 expected_real, kernel, reference, shifted_g(3), strain_energy(4), stress(3, 3), &
219 stress_dv(3, 3), stress_only(3, 3), stress_v(3, 3), stress_v_dv(3, 3)
222 potential, potential_v_dv
231 cell(axis, axis) = 7.0_dp**(1.0_dp/3.0_dp)
233 a_mode = cmplx(0.4_dp, -0.2_dp, dp)
234 b_mode = cmplx(-0.7_dp, 0.9_dp, dp)
239 CALL pw_grid_create(grid, comm, cell, grid_span=span, npts=[5, 5, 5], odd=.true., fft_usage=.false.)
242 CALL a_real%create(grid)
243 CALL b_real%create(grid)
244 CALL potential%create(grid)
245 CALL potential_v_dv%create(grid)
247 CALL gradient(axis)%create(grid)
248 CALL gradient_v_dv(axis)%create(grid)
250 ALLOCATE (poisson%pw_pools(1))
254 poisson%parameters%periodic = [1, 1, 0]
255 poisson%parameters%mt_alpha = 1.0_dp
262 expected_real = 0.0_dp
264 IF (grid%have_g0)
THEN
269 expected_real = 6.0_dp
270 reference = 6.0_dp*mt2d_kernel_reference([0.0_dp, 0.0_dp, 0.0_dp], 3, cell(3, 3))
272 DO ig = 1, grid%ngpts_cut_local
273 IF (any(grid%g_hat(2:3, ig) /= 0)) cycle
274 IF (grid%g_hat(1, ig) == 1)
THEN
277 ELSE IF (grid%g_hat(1, ig) == -1)
THEN
278 a%array(ig) = conjg(a_mode)
279 b%array(ig) = conjg(b_mode)
282 expected = expected + 2.0_dp*real(conjg(a_mode)*b_mode, dp)
283 expected_real = expected_real + 2.0_dp*real(a_mode, dp)*real(b_mode, dp)
284 kernel = mt2d_kernel_reference([2.0_dp*
pi/cell(1, 1), 0.0_dp, 0.0_dp], 3, cell(3, 3))
285 reference = reference + 2.0_dp*real(conjg(a_mode)*b_mode, dp)*kernel
287 a_real%array = real(a%array, dp)
288 b_real%array = real(b%array, dp)
292 CALL check_error(
pw_integral_ab(a, b, sumtype=sumtype), 7.0_dp*expected, 3.e-14_dp)
293 CALL check_error(
pw_integral_ab(a, b, sumtype=sumtype, just_sum=.true.), expected, 3.e-14_dp)
294 CALL check_error(
pw_integral_ab(a_real, b_real, sumtype=sumtype), 7.0_dp*expected_real, 3.e-14_dp)
295 CALL check_error(
pw_integral_ab(a_real, b_real, sumtype=sumtype, just_sum=.true.), &
296 expected_real, 3.e-14_dp)
297 CALL check_error(
pw_integral_ab(a, b_real, sumtype=sumtype), 7.0_dp*expected_real, 3.e-14_dp)
298 CALL check_error(
pw_integral_ab(a_real, b, sumtype=sumtype), 7.0_dp*expected_real, 3.e-14_dp)
301 CALL pw_poisson_solve(poisson, a, energy_dv, dvhartree=gradient, h_stress=stress_dv, aux_density=b)
302 CALL pw_poisson_solve(poisson, a, energy_v, potential, h_stress=stress_v, aux_density=b)
303 CALL pw_poisson_solve(poisson, a, energy_v_dv, potential_v_dv, dvhartree=gradient_v_dv, &
304 h_stress=stress_v_dv, aux_density=b)
306 CALL check_error(energy, 0.5_dp*7.0_dp*reference, 3.e-13_dp)
307 CALL check_error(energy_dv, energy, 3.e-13_dp)
308 CALL check_error(energy_v, energy, 3.e-13_dp)
309 CALL check_error(energy_v_dv, energy, 3.e-13_dp)
310 CALL check_error(maxval(abs(stress_dv - stress)), 0.0_dp, 3.e-11_dp)
311 CALL check_error(maxval(abs(stress_v - stress)), 0.0_dp, 3.e-11_dp)
312 CALL check_error(maxval(abs(stress_v_dv - stress)), 0.0_dp, 3.e-11_dp)
313 CALL check_error(maxval(abs(stress_only - stress)), 0.0_dp, 3.e-11_dp)
319 shifted_g = [2.0_dp*
pi/cell(1, 1)/(1.0_dp + strain_steps(sign_step)), 0.0_dp, 0.0_dp]
320 reference = 6.0_dp*mt2d_kernel_reference([0.0_dp, 0.0_dp, 0.0_dp], 3, cell(3, 3)) &
321 + 2.0_dp*real(conjg(a_mode)*b_mode, dp)*mt2d_kernel_reference(shifted_g, 3, cell(3, 3))
322 strain_energy(sign_step) = 0.5_dp*7.0_dp*reference/(1.0_dp + strain_steps(sign_step))
324 derivative = (8.0_dp*(strain_energy(1) - strain_energy(2)) &
325 - strain_energy(3) + strain_energy(4))/(12.0_dp*step)
326 CALL check_error(stress(1, 1) + derivative, 0.0_dp, 3.e-9_dp)
328 DO ig = 1, grid%ngpts_cut_local
329 kernel = mt2d_kernel_reference(grid%g(:, ig), 3, cell(3, 3))
330 CALL check_error(abs(potential%array(ig) - kernel*a%array(ig)), 0.0_dp, 3.e-13_dp)
331 CALL check_error(abs(potential_v_dv%array(ig) - kernel*a%array(ig)), 0.0_dp, 3.e-13_dp)
333 CALL check_error(abs(gradient(axis)%array(ig) &
334 - cmplx(0.0_dp, grid%g(axis, ig), dp)*kernel*a%array(ig)), 0.0_dp, 3.e-13_dp)
335 CALL check_error(abs(gradient_v_dv(axis)%array(ig) &
336 - cmplx(0.0_dp, grid%g(axis, ig), dp)*kernel*a%array(ig)), 0.0_dp, 3.e-13_dp)
340 CALL poisson%release()
343 CALL a_real%release()
344 CALL b_real%release()
345 CALL potential%release()
346 CALL potential_v_dv%release()
348 CALL gradient(axis)%release()
349 CALL gradient_v_dv(axis)%release()
353 END SUBROUTINE check_pw_integral_and_poisson
358 SUBROUTINE check_mt2d_kernel()
359 REAL(dp),
PARAMETER :: step = 3.e-4_dp, shifts(4) = [step, -step, 2.0_dp*step, -2.0_dp*step]
361 INTEGER :: alpha, beta, ip, iz, jp, miller(3), &
362 mode, modes(3, 5), sign_step
363 REAL(dp) :: derivative, g(3), h_inv(3, 3), kernel, &
364 lengths(3), shifted_g(3), &
365 shifted_kernel(4), strain(3, 3)
371 jp = mod(iz + 1, 3) + 1
372 lengths = [9.0_dp, 13.0_dp, 17.0_dp]
375 h_inv(alpha, alpha) = 1.0_dp/lengths(alpha)
377 h_inv(ip, jp) = -1.4_dp/(lengths(ip)*lengths(jp))
381 modes(iz, 2:3) = [1, 2]
383 modes(:, 5) = [1, 2, 1]
384 DO mode = 1,
SIZE(modes, 2)
385 miller = modes(:, mode)
386 g = 2.0_dp*
pi*matmul(transpose(h_inv), real(miller, dp))
388 CALL check_error(kernel, mt2d_kernel_reference(g, iz, lengths(iz)), 3.e-14_dp)
389 CALL check_error(maxval(abs(strain(iz, :))), 0.0_dp, 0.0_dp)
390 CALL check_error(maxval(abs(strain(:, iz))), 0.0_dp, 0.0_dp)
391 IF (mode <= 3)
CALL check_error(maxval(abs(strain)), 0.0_dp, 0.0_dp)
393 IF (alpha == iz) cycle
395 IF (beta == iz) cycle
399 IF (alpha == beta)
THEN
400 shifted_g(beta) = g(beta)/(1.0_dp + shifts(sign_step))
402 shifted_g(beta) = g(beta) - shifts(sign_step)*g(alpha)
404 shifted_kernel(sign_step) = mt2d_kernel_reference(shifted_g, iz, lengths(iz))
406 derivative = (8.0_dp*(shifted_kernel(1) - shifted_kernel(2)) &
407 - shifted_kernel(3) + shifted_kernel(4))/(12.0_dp*step)
408 CALL check_error(strain(alpha, beta), derivative, 3.e-9_dp)
413 END SUBROUTINE check_mt2d_kernel
422 FUNCTION mt2d_kernel_reference(g, iz, slab_size)
RESULT(kernel)
423 REAL(dp),
INTENT(IN) :: g(3)
424 INTEGER,
INTENT(IN) :: iz
425 REAL(dp),
INTENT(IN) :: slab_size
428 REAL(dp) :: g2, g_parallel(3), radius
431 IF (g2 == 0.0_dp)
THEN
432 kernel =
pi*slab_size**2/2.0_dp
435 g_parallel(iz) = 0.0_dp
436 radius = slab_size/2.0_dp
437 kernel =
fourpi/g2 -
fourpi/g2*cos(g(iz)*radius)*exp(-norm2(g_parallel)*radius)
439 END FUNCTION mt2d_kernel_reference
447 SUBROUTINE check_error(value, reference, tolerance)
448 REAL(dp),
INTENT(IN) ::
value, reference, tolerance
452 error = abs(
value - reference)/max(1.0_dp, abs(reference))
453 max_error = max(max_error, error)
454 IF (.NOT. (error <= tolerance))
THEN
455 print *,
'Coulomb check: value, reference, error, tolerance:',
value, reference, error, tolerance
456 error stop
'PW integral or MT2D kernel check failed'
458 END SUBROUTINE check_error
Utility routines to open and close files.
subroutine, public open_file(file_name, unit_number, file_status, file_form, file_action, file_position, file_pad, file_access, debug)
Opens a file and returns its logical unit number.
subroutine, public close_file(unit_number, file_status)
Closes an open file given by its logical unit number.
various routines to log and control the output. The idea is that decisions about where to log should ...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
Defines the basic variable types.
integer, parameter, public dp
subroutine, public cp2k_finalize()
...
subroutine, public cp2k_init()
...
Definition of mathematical constants and functions.
real(kind=dp), parameter, public pi
real(kind=dp), parameter, public fourpi
Interface to the message passing library MPI.
type(mp_comm_type), parameter, public mp_comm_world
subroutine, public mt2d_kernel_strain(g, special_dimension, slab_size, kernel, strain)
MT2D Coulomb kernel and its affine strain derivative in the periodic plane.
subroutine, public pw_copy_match(pw1, pw2)
copy a pw type variable
integer, parameter, public halfspace
integer, parameter, public fullspace
This module defines the grid data type and some basic operations on it.
subroutine, public pw_grid_release(pw_grid)
releases the given pw grid
subroutine, public pw_real_fourier_aliases(grid, index, vectors, count)
Physical wave vectors represented by one real full-box Fourier coefficient. Each even-grid boundary r...
integer, parameter, public do_accurate_sum
integer, parameter, public do_standard_sum
subroutine, public pw_poisson_set(poisson_env, cell_hmat, parameters, pw_pools, use_level, mt_super_ref_pw_grid, dct_pw_grid, force_rebuild)
sets cell, grids and parameters used by the poisson solver You should call this at least once (and se...
functions related to the poisson solver on regular grids
integer, parameter, public pw_poisson_mt
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
subroutine, public pw_pool_create(pool, pw_grid, max_cache)
creates a pool for pw
Generate Gaussian cube files.
subroutine, public cube_to_pw(grid, filename, scaling, parallel_read, silent)
Computes the external density on the grid hacked from external_read_density.
character(len= *), parameter, public cube_values_format
subroutine check_real_fourier_aliases()
Preserve independent reflection symmetry on even real-grid Fourier boundaries.
type of a logger, at the moment it contains just a print level starting at which level it should be l...
stores all the informations relevant to an mpi environment
environment for the poisson solver