(git:3d5d2e5)
Loading...
Searching...
No Matches
ps_implicit_methods.F
Go to the documentation of this file.
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 The implicit (generalized) Poisson solver
10!> \par History
11!> 06.2014 created [Hossein Bani-Hashemian]
12!> 11.2015 - dealt with missing grid points of periodic grids while performing dct;
13!> - revised solver for Neumann and mixed boundary setups.
14!> \author Hossein Bani-Hashemian
15! **************************************************************************************************
18 cite_reference
22 USE dct, ONLY: &
30 USE kahan_sum, ONLY: accurate_sum
31 USE kinds, ONLY: dp,&
32 int_8
33 USE mathconstants, ONLY: fourpi,&
34 pi
35 USE ps_implicit_types, ONLY: mixed_bc,&
41 USE pw_methods, ONLY: pw_axpy,&
42 pw_copy,&
44 pw_scale,&
50 USE pw_pool_types, ONLY: pw_pool_create,&
53 USE pw_types, ONLY: pw_c1d_gs_type,&
55#include "../base/base_uses.f90"
56
57 IMPLICIT NONE
58 PRIVATE
59 CHARACTER(LEN=*), PARAMETER, PRIVATE :: moduleN = 'ps_implicit_methods'
60
61 PUBLIC ps_implicit_create, &
66
67 INTERFACE ps_implicit_compute_ehartree
68 MODULE PROCEDURE compute_ehartree_periodic_bc, &
69 compute_ehartree_mixed_bc
70 END INTERFACE ps_implicit_compute_ehartree
71
72 REAL(dp), PRIVATE, PARAMETER :: large_error = 1.0e4_dp
73
74CONTAINS
75
76! **************************************************************************************************
77!> \brief Creates implicit Poisson solver environment
78!> \param pw_pool pool of pw grid
79!> \param poisson_params poisson_env parameters
80!> \param dct_pw_grid discrete cosine transform (extended) grid
81!> \param green green function for FFT based inverse Laplacian
82!> \param ps_implicit_env implicit env to be created
83!> \par History
84!> 06.2014 created [Hossein Bani-Hashemian]
85!> \author Mohammad Hossein Bani-Hashemian
86! **************************************************************************************************
87 SUBROUTINE ps_implicit_create(pw_pool, poisson_params, dct_pw_grid, green, ps_implicit_env)
88
89 TYPE(pw_pool_type), INTENT(IN), POINTER :: pw_pool
90 TYPE(pw_poisson_parameter_type), INTENT(INOUT) :: poisson_params
91 TYPE(pw_grid_type), INTENT(IN), POINTER :: dct_pw_grid
92 TYPE(greens_fn_type), INTENT(IN), POINTER :: green
93 TYPE(ps_implicit_type), INTENT(INOUT), POINTER :: ps_implicit_env
94
95 CHARACTER(LEN=*), PARAMETER :: routinen = 'ps_implicit_create'
96
97 INTEGER :: boundary_condition, handle, j, &
98 n_contacts, neumann_directions
99 TYPE(pw_pool_type), POINTER :: pw_pool_xpndd
100
101 CALL timeset(routinen, handle)
102
103 CALL cite_reference(banihashemian2016)
104
105 IF (.NOT. ASSOCIATED(ps_implicit_env)) THEN
106 ALLOCATE (ps_implicit_env)
107
108 ps_implicit_env%do_dbc_cube = poisson_params%dbc_params%do_dbc_cube
109 boundary_condition = poisson_params%ps_implicit_params%boundary_condition
110 neumann_directions = poisson_params%ps_implicit_params%neumann_directions
111
112! create dielectric
113 NULLIFY (ps_implicit_env%dielectric)
114 SELECT CASE (boundary_condition)
116 CALL dielectric_create(ps_implicit_env%dielectric, pw_pool, poisson_params%dielectric_params)
117 CASE (neumann_bc, mixed_bc)
118 CALL pw_pool_create(pw_pool_xpndd, pw_grid=dct_pw_grid)
119 CALL dielectric_create(ps_implicit_env%dielectric, pw_pool_xpndd, poisson_params%dielectric_params)
120 CALL pw_pool_release(pw_pool_xpndd)
121 END SELECT
122
123! initial guess
124 NULLIFY (ps_implicit_env%initial_guess)
125
126! v_eps
127 NULLIFY (ps_implicit_env%v_eps)
128 ALLOCATE (ps_implicit_env%v_eps)
129 CALL pw_pool%create_pw(ps_implicit_env%v_eps)
130 CALL pw_zero(ps_implicit_env%v_eps)
131
132! constraint charge
133 NULLIFY (ps_implicit_env%cstr_charge)
134 SELECT CASE (boundary_condition)
135 CASE (mixed_periodic_bc)
136 ALLOCATE (ps_implicit_env%cstr_charge)
137 CALL pw_pool%create_pw(ps_implicit_env%cstr_charge)
138 CALL pw_zero(ps_implicit_env%cstr_charge)
139 CASE (mixed_bc)
140 CALL pw_pool_create(pw_pool_xpndd, pw_grid=dct_pw_grid)
141 ALLOCATE (ps_implicit_env%cstr_charge)
142 CALL pw_pool_xpndd%create_pw(ps_implicit_env%cstr_charge)
143 CALL pw_zero(ps_implicit_env%cstr_charge)
144 CALL pw_pool_release(pw_pool_xpndd)
145 END SELECT
146
147! initialize energies
148 ps_implicit_env%ehartree = 0.0_dp
149 ps_implicit_env%electric_enthalpy = 0.0_dp
150! times called
151 ps_implicit_env%times_called = 0
152
153! dct env
154 IF (boundary_condition == mixed_bc .OR. boundary_condition == neumann_bc) THEN
155 CALL dct_type_init(pw_pool%pw_grid, neumann_directions, ps_implicit_env%dct_env)
156 END IF
157
158! prepare dirichlet bc
159 CALL dirichlet_boundary_region_setup(pw_pool, poisson_params, ps_implicit_env%contacts)
160 CALL ps_implicit_prepare_blocks(pw_pool, dct_pw_grid, green, poisson_params, ps_implicit_env)
161 ! release tiles if they are not supposed to be written into cube files
162 IF ((boundary_condition == mixed_periodic_bc .OR. boundary_condition == mixed_bc) .AND. &
163 (.NOT. poisson_params%dbc_params%do_dbc_cube)) THEN
164 n_contacts = SIZE(ps_implicit_env%contacts)
165 DO j = 1, n_contacts
166 CALL dbc_tile_release(ps_implicit_env%contacts(j)%dirichlet_bc, pw_pool)
167 END DO
168 END IF
169
170 END IF
171
172 CALL timestop(handle)
173
174 END SUBROUTINE ps_implicit_create
175
176! **************************************************************************************************
177!> \brief implicit Poisson solver for periodic boundary conditions
178!> \param poisson_env poisson environment
179!> \param density electron density
180!> \param v_new electrostatic potential
181!> \param ehartree Hartree energy
182!> \par History
183!> 07.2014 created [Hossein Bani-Hashemian]
184!> \author Mohammad Hossein Bani-Hashemian
185! **************************************************************************************************
186 SUBROUTINE implicit_poisson_solver_periodic(poisson_env, density, v_new, ehartree)
187
188 TYPE(pw_poisson_type), INTENT(IN) :: poisson_env
189 TYPE(pw_r3d_rs_type), INTENT(IN) :: density
190 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: v_new
191 REAL(dp), INTENT(OUT), OPTIONAL :: ehartree
192
193 CHARACTER(LEN=*), PARAMETER :: routinen = 'implicit_poisson_solver_periodic'
194
195 INTEGER :: handle, iter, max_iter, outp_unit, &
196 times_called
197 LOGICAL :: reached_max_iter, reached_tol, &
198 use_zero_initial_guess
199 REAL(dp) :: nabs_error, omega, pres_error, tol
200 TYPE(dielectric_type), POINTER :: dielectric
201 TYPE(greens_fn_type), POINTER :: green
202 TYPE(ps_implicit_type), POINTER :: ps_implicit_env
203 TYPE(pw_pool_type), POINTER :: pw_pool
204 TYPE(pw_r3d_rs_type) :: g, pxqainvxres, qainvxres, res_new, &
205 res_old, v_old
206
207 CALL timeset(routinen, handle)
208
209 pw_pool => poisson_env%pw_pools(poisson_env%pw_level)%pool
210 dielectric => poisson_env%implicit_env%dielectric
211 green => poisson_env%green_fft
212 ps_implicit_env => poisson_env%implicit_env
213
214 tol = poisson_env%parameters%ps_implicit_params%tol
215 omega = poisson_env%parameters%ps_implicit_params%omega
216 max_iter = poisson_env%parameters%ps_implicit_params%max_iter
217 use_zero_initial_guess = poisson_env%parameters%ps_implicit_params%zero_initial_guess
218 times_called = ps_implicit_env%times_called
219
220! check if this is the first scf iteration
221 IF (times_called == 0) CALL ps_implicit_initial_guess_create(ps_implicit_env, pw_pool)
222
223 CALL pw_pool%create_pw(g)
224 CALL pw_pool%create_pw(v_old)
225 CALL pw_pool%create_pw(res_old)
226 CALL pw_pool%create_pw(res_new)
227 CALL pw_pool%create_pw(qainvxres)
228 CALL pw_pool%create_pw(pxqainvxres)
229
230 IF (use_zero_initial_guess) THEN
231 CALL pw_zero(v_old)
232 ELSE
233 CALL pw_copy(ps_implicit_env%initial_guess, v_old)
234 END IF
235
236 g%array = fourpi*density%array/dielectric%eps%array
237
238! res_old = g - \Delta(v_old) - P(v_old)
239 CALL apply_poisson_operator_fft(pw_pool, green, dielectric, v_old, res_old)
240 CALL pw_scale(res_old, -1.0_dp)
241 CALL pw_axpy(g, res_old)
242
243! evaluate \Delta^-1(res_old)
244 CALL apply_inv_laplace_operator_fft(pw_pool, green, res_old, qainvxres)
245
246 iter = 1
247 DO
248
249! v_new = v_old + \omega * QAinvxres_old
250 CALL pw_scale(qainvxres, omega)
251 CALL pw_copy(qainvxres, v_new)
252 CALL pw_axpy(v_old, v_new)
253
254! res_new = res_old - \omega * ( \Delta(QAinvxres_old) + P(QAinvxres_old) )
255! = (1 - \omega) * res_old - \omega * PxQAinvxres
256 CALL apply_p_operator(pw_pool, dielectric, qainvxres, pxqainvxres)
257 CALL pw_copy(pxqainvxres, res_new)
258 CALL pw_scale(res_new, -1.0_dp)
259 CALL pw_axpy(res_old, res_new, 1.0_dp - omega)
260
261! compute the error
262 CALL ps_implicit_compute_error_fft(pw_pool, green, res_new, v_old, v_new, qainvxres, &
263 pres_error, nabs_error)
264! output
265 CALL ps_implicit_output(iter, pres_error, nabs_error, outp_unit)
266 IF (PRESENT(ehartree)) THEN
267 CALL ps_implicit_compute_ehartree(density, v_new, ehartree)
268 CALL ps_implicit_report_ehartree(ps_implicit_env, outp_unit, ehartree)
269 ps_implicit_env%ehartree = ehartree
270 ELSE
271 IF (outp_unit > 0) WRITE (outp_unit, '(A1,/)')
272 END IF
273
274 iter = iter + 1
275 reached_max_iter = iter > max_iter
276 reached_tol = pres_error <= tol
277 IF (pres_error > large_error) THEN
278 cpabort("Poisson solver did not converge.")
279 END IF
280 ps_implicit_env%times_called = ps_implicit_env%times_called + 1
281 IF (reached_max_iter .OR. reached_tol) EXIT
282
283! v_old = v_new, res_old = res_new
284 CALL pw_copy(v_new, v_old)
285 CALL pw_copy(res_new, res_old)
286
287 END DO
288 CALL ps_implicit_print_convergence_msg(iter, max_iter, outp_unit)
289
290 IF ((times_called /= 0) .AND. (.NOT. use_zero_initial_guess)) THEN
291 CALL pw_copy(v_new, ps_implicit_env%initial_guess)
292 END IF
293
294 IF (PRESENT(ehartree)) ehartree = ps_implicit_env%ehartree
295! compute the extra contribution to the Hamiltonian due to the presence of dielectric
296 block
297 TYPE(pw_r3d_rs_type) :: v_eps
298 v_eps%pw_grid => ps_implicit_env%v_eps%pw_grid
299 v_eps%array => ps_implicit_env%v_eps%array
300 CALL ps_implicit_compute_veps(pw_pool, dielectric, v_new, v_eps)
301 END block
302
303 CALL pw_pool%give_back_pw(g)
304 CALL pw_pool%give_back_pw(v_old)
305 CALL pw_pool%give_back_pw(res_old)
306 CALL pw_pool%give_back_pw(res_new)
307 CALL pw_pool%give_back_pw(qainvxres)
308 CALL pw_pool%give_back_pw(pxqainvxres)
309
310 CALL timestop(handle)
311
313
314! **************************************************************************************************
315!> \brief implicit Poisson solver: zero-average solution of the Poisson equation
316!> subject to homogeneous Neumann boundary conditions
317!> \param poisson_env poisson environment
318!> \param density electron density
319!> \param v_new electrostatic potential
320!> \param ehartree Hartree energy
321!> \par History
322!> 02.2015 created [Hossein Bani-Hashemian]
323!> 11.2015 revised [Hossein Bani-Hashemian]
324!> \author Mohammad Hossein Bani-Hashemian
325! **************************************************************************************************
326 SUBROUTINE implicit_poisson_solver_neumann(poisson_env, density, v_new, ehartree)
327
328 TYPE(pw_poisson_type), INTENT(IN) :: poisson_env
329 TYPE(pw_r3d_rs_type), INTENT(IN) :: density
330 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: v_new
331 REAL(dp), INTENT(OUT), OPTIONAL :: ehartree
332
333 CHARACTER(LEN=*), PARAMETER :: routinen = 'implicit_poisson_solver_neumann'
334
335 INTEGER :: handle, iter, max_iter, &
336 neumann_directions, outp_unit, &
337 times_called
338 LOGICAL :: reached_max_iter, reached_tol, &
339 use_zero_initial_guess
340 REAL(dp) :: nabs_error, omega, pres_error, tol, &
341 vol_scfac
342 TYPE(dct_type), POINTER :: dct_env
343 TYPE(dielectric_type), POINTER :: dielectric
344 TYPE(greens_fn_type), POINTER :: green
345 TYPE(ps_implicit_type), POINTER :: ps_implicit_env
346 TYPE(pw_pool_type), POINTER :: pw_pool, pw_pool_xpndd
347 TYPE(pw_r3d_rs_type) :: density_xpndd, g, pxqainvxres, &
348 qainvxres, res_new, res_old, &
349 v_eps_xpndd, v_new_xpndd, v_old
350
351 CALL timeset(routinen, handle)
352
353 pw_pool => poisson_env%pw_pools(poisson_env%pw_level)%pool
354 dielectric => poisson_env%implicit_env%dielectric
355 green => poisson_env%green_fft
356 ps_implicit_env => poisson_env%implicit_env
357 dct_env => ps_implicit_env%dct_env
358
359 tol = poisson_env%parameters%ps_implicit_params%tol
360 omega = poisson_env%parameters%ps_implicit_params%omega
361 max_iter = poisson_env%parameters%ps_implicit_params%max_iter
362 use_zero_initial_guess = poisson_env%parameters%ps_implicit_params%zero_initial_guess
363 neumann_directions = poisson_env%parameters%ps_implicit_params%neumann_directions
364 times_called = ps_implicit_env%times_called
365
366 SELECT CASE (neumann_directions)
367 CASE (neumannxyz)
368 vol_scfac = 8.0_dp
369 CASE (neumannxy, neumannxz, neumannyz)
370 vol_scfac = 4.0_dp
371 CASE (neumannx, neumanny, neumannz)
372 vol_scfac = 2.0_dp
373 END SELECT
374
375 CALL pw_pool_create(pw_pool_xpndd, pw_grid=poisson_env%dct_pw_grid)
376
377! check if this is the first scf iteration
378 IF (times_called == 0) CALL ps_implicit_initial_guess_create(ps_implicit_env, pw_pool_xpndd)
379
380 CALL pw_pool_xpndd%create_pw(g)
381 CALL pw_pool_xpndd%create_pw(v_old)
382 CALL pw_pool_xpndd%create_pw(res_old)
383 CALL pw_pool_xpndd%create_pw(res_new)
384 CALL pw_pool_xpndd%create_pw(qainvxres)
385 CALL pw_pool_xpndd%create_pw(pxqainvxres)
386 CALL pw_pool_xpndd%create_pw(density_xpndd)
387 CALL pw_pool_xpndd%create_pw(v_new_xpndd)
388 CALL pw_pool_xpndd%create_pw(v_eps_xpndd)
389
390 IF (use_zero_initial_guess) THEN
391 CALL pw_zero(v_old)
392 ELSE
393 CALL pw_copy(ps_implicit_env%initial_guess, v_old)
394 END IF
395
396 CALL pw_expand(neumann_directions, &
397 dct_env%recv_msgs_bnds, dct_env%dests_expand, dct_env%srcs_expand, &
398 dct_env%flipg_stat, dct_env%bounds_shftd, density, density_xpndd)
399 CALL pw_expand(neumann_directions, &
400 dct_env%recv_msgs_bnds, dct_env%dests_expand, dct_env%srcs_expand, &
401 dct_env%flipg_stat, dct_env%bounds_shftd, v_new, v_new_xpndd)
402
403 g%array = fourpi*density_xpndd%array/dielectric%eps%array
404
405! res_old = g - \Delta(v_old) - P(v_old)
406 CALL apply_poisson_operator_dct(pw_pool_xpndd, green, dielectric, v_old, res_old)
407 CALL pw_scale(res_old, -1.0_dp)
408 CALL pw_axpy(g, res_old)
409
410! evaluate \Delta^-1(res_old)
411 CALL apply_inv_laplace_operator_dct(pw_pool_xpndd, green, res_old, qainvxres)
412
413 iter = 1
414 DO
415
416! v_new = v_old + \omega * QAinvxres_old
417 CALL pw_scale(qainvxres, omega)
418 CALL pw_copy(qainvxres, v_new_xpndd)
419 CALL pw_axpy(v_old, v_new_xpndd)
420
421! res_new = res_old - \omega * ( \Delta(QAinvxres_old) + P(QAinvxres_old) )
422! = (1 - \omega) * res_old - \omega * PxQAinvxres
423 CALL apply_p_operator(pw_pool_xpndd, dielectric, qainvxres, pxqainvxres)
424 CALL pw_copy(pxqainvxres, res_new)
425 CALL pw_scale(res_new, -1.0_dp)
426 CALL pw_axpy(res_old, res_new, 1.0_dp - omega)
427
428! compute the error
429 CALL ps_implicit_compute_error_dct(pw_pool_xpndd, green, res_new, v_old, v_new_xpndd, qainvxres, &
430 pres_error, nabs_error)
431! output
432 CALL ps_implicit_output(iter, pres_error, nabs_error, outp_unit)
433 IF (PRESENT(ehartree)) THEN
434 CALL ps_implicit_compute_ehartree(density_xpndd, v_new_xpndd, ehartree)
435 CALL ps_implicit_report_ehartree(ps_implicit_env, outp_unit, ehartree/vol_scfac)
436 ps_implicit_env%ehartree = ehartree/vol_scfac
437 ELSE
438 IF (outp_unit > 0) WRITE (outp_unit, '(A1,/)')
439 END IF
440
441 iter = iter + 1
442 reached_max_iter = iter > max_iter
443 reached_tol = pres_error <= tol
444 IF (pres_error > large_error) THEN
445 cpabort("Poisson solver did not converge.")
446 END IF
447 ps_implicit_env%times_called = ps_implicit_env%times_called + 1
448 IF (reached_max_iter .OR. reached_tol) EXIT
449
450! v_old = v_new, res_old = res_new
451 CALL pw_copy(v_new_xpndd, v_old)
452 CALL pw_copy(res_new, res_old)
453
454 END DO
455 CALL ps_implicit_print_convergence_msg(iter, max_iter, outp_unit)
456
457 CALL pw_shrink(neumann_directions, dct_env%dests_shrink, dct_env%srcs_shrink, &
458 dct_env%bounds_local_shftd, v_new_xpndd, v_new)
459
460 IF ((times_called /= 0) .AND. (.NOT. use_zero_initial_guess)) THEN
461 CALL pw_copy(v_new_xpndd, ps_implicit_env%initial_guess)
462 END IF
463
464 IF (PRESENT(ehartree)) ehartree = ps_implicit_env%ehartree
465! compute the extra contribution to the Hamiltonian due to the presence of dielectric
466! veps has to be computed for the expanded data and then shrunk otherwise we loose accuracy
467 CALL ps_implicit_compute_veps(pw_pool_xpndd, dielectric, v_new_xpndd, v_eps_xpndd)
468 block
469 TYPE(pw_r3d_rs_type) :: v_eps
470 v_eps%pw_grid => ps_implicit_env%v_eps%pw_grid
471 v_eps%array => ps_implicit_env%v_eps%array
472 CALL pw_shrink(neumann_directions, dct_env%dests_shrink, dct_env%srcs_shrink, &
473 dct_env%bounds_local_shftd, v_eps_xpndd, v_eps)
474 END block
475
476 CALL pw_pool_xpndd%give_back_pw(g)
477 CALL pw_pool_xpndd%give_back_pw(v_old)
478 CALL pw_pool_xpndd%give_back_pw(res_old)
479 CALL pw_pool_xpndd%give_back_pw(res_new)
480 CALL pw_pool_xpndd%give_back_pw(qainvxres)
481 CALL pw_pool_xpndd%give_back_pw(pxqainvxres)
482 CALL pw_pool_xpndd%give_back_pw(density_xpndd)
483 CALL pw_pool_xpndd%give_back_pw(v_new_xpndd)
484 CALL pw_pool_xpndd%give_back_pw(v_eps_xpndd)
485 CALL pw_pool_release(pw_pool_xpndd)
486
487 CALL timestop(handle)
488
490
491! **************************************************************************************************
492!> \brief implicit Poisson solver for mixed-periodic boundary conditions (periodic + Dirichlet)
493!> \param poisson_env poisson environment
494!> \param density electron density
495!> \param v_new electrostatic potential
496!> \param electric_enthalpy electric enthalpy
497!> \par History
498!> 07.2014 created [Hossein Bani-Hashemian]
499!> \author Mohammad Hossein Bani-Hashemian
500! **************************************************************************************************
501 SUBROUTINE implicit_poisson_solver_mixed_periodic(poisson_env, density, v_new, electric_enthalpy)
502
503 TYPE(pw_poisson_type), INTENT(IN) :: poisson_env
504 TYPE(pw_r3d_rs_type), INTENT(IN) :: density
505 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: v_new
506 REAL(dp), INTENT(OUT), OPTIONAL :: electric_enthalpy
507
508 CHARACTER(LEN=*), PARAMETER :: routinen = 'implicit_poisson_solver_mixed_periodic'
509
510 INTEGER :: data_size, handle, iter, j, lb1, lb2, lb3, max_iter, n_contacts, n_tiles_tot, ng, &
511 ngpts_local, nt, nt_tot, outp_unit, times_called, ub1, ub2, ub3
512 INTEGER(KIND=int_8) :: ngpts
513 INTEGER, DIMENSION(2, 3) :: bounds_local
514 INTEGER, DIMENSION(3) :: npts_local
515 LOGICAL :: reached_max_iter, reached_tol, &
516 use_zero_initial_guess
517 REAL(dp) :: axvbar_avg, ehartree, eta, g_avg, &
518 gminusaxvbar_avg, nabs_error, omega, &
519 pres_error, tol
520 REAL(dp), ALLOCATABLE, DIMENSION(:) :: btxlambda_new, btxlambda_old, bxv_bar, bxv_new, &
521 lambda0, lambda_new, lambda_newneta, lambda_old, qsxlambda, v_bar1d, v_d, v_new1d, w
522 REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: b, bt, qs, rinv
523 REAL(dp), ALLOCATABLE, DIMENSION(:, :, :) :: btxlambda_new3d, btxlambda_old3d
524 TYPE(dielectric_type), POINTER :: dielectric
525 TYPE(greens_fn_type), POINTER :: green
526 TYPE(ps_implicit_type), POINTER :: ps_implicit_env
527 TYPE(pw_grid_type), POINTER :: pw_grid
528 TYPE(pw_pool_type), POINTER :: pw_pool
529 TYPE(pw_r3d_rs_type) :: axvbar, g, pxqainvxres, qainvxres, &
530 res_new, res_old, v_old
531
532 CALL timeset(routinen, handle)
533
534 pw_pool => poisson_env%pw_pools(poisson_env%pw_level)%pool
535 pw_grid => pw_pool%pw_grid
536 dielectric => poisson_env%implicit_env%dielectric
537 green => poisson_env%green_fft
538 ps_implicit_env => poisson_env%implicit_env
539
540 ngpts_local = pw_grid%ngpts_local
541 ngpts = pw_grid%ngpts
542 npts_local = pw_grid%npts_local
543 bounds_local = pw_grid%bounds_local
544 tol = poisson_env%parameters%ps_implicit_params%tol
545 omega = poisson_env%parameters%ps_implicit_params%omega
546 max_iter = poisson_env%parameters%ps_implicit_params%max_iter
547 use_zero_initial_guess = poisson_env%parameters%ps_implicit_params%zero_initial_guess
548 times_called = ps_implicit_env%times_called
549
550 n_contacts = SIZE(ps_implicit_env%contacts)
551 n_tiles_tot = 0
552 DO j = 1, n_contacts
553 n_tiles_tot = n_tiles_tot + ps_implicit_env%contacts(j)%dirichlet_bc%n_tiles
554 END DO
555
556 IF (pw_grid%para%blocked) THEN
557 data_size = product(npts_local)
558 ELSE IF (pw_grid%para%ray_distribution) THEN
559 data_size = ngpts_local
560 ELSE ! parallel run with np = 1
561 data_size = product(npts_local)
562 END IF
563
564! check if this is the first scf iteration
565 IF (times_called == 0) CALL ps_implicit_initial_guess_create(ps_implicit_env, pw_pool)
566
567 ALLOCATE (b(n_tiles_tot, data_size))
568 ALLOCATE (bt(data_size, n_tiles_tot))
569 ALLOCATE (qs(n_tiles_tot, n_tiles_tot))
570 ALLOCATE (rinv(n_tiles_tot + 1, n_tiles_tot + 1))
571
572 b(:, :) = ps_implicit_env%B
573 bt(:, :) = ps_implicit_env%Bt
574 qs(:, :) = ps_implicit_env%QS
575 rinv(:, :) = ps_implicit_env%Rinv
576 CALL get_voltage(poisson_env%parameters%dbc_params%time, ps_implicit_env%v_D, ps_implicit_env%osc_frac, &
577 ps_implicit_env%frequency, ps_implicit_env%phase, v_d)
578
579 lb1 = bounds_local(1, 1); ub1 = bounds_local(2, 1)
580 lb2 = bounds_local(1, 2); ub2 = bounds_local(2, 2)
581 lb3 = bounds_local(1, 3); ub3 = bounds_local(2, 3)
582
583 ALLOCATE (lambda0(n_tiles_tot), lambda_old(n_tiles_tot), lambda_new(n_tiles_tot))
584 ALLOCATE (btxlambda_old(data_size), btxlambda_new(data_size))
585 ALLOCATE (btxlambda_old3d(lb1:ub1, lb2:ub2, lb3:ub3), btxlambda_new3d(lb1:ub1, lb2:ub2, lb3:ub3))
586 ALLOCATE (qsxlambda(n_tiles_tot))
587 ALLOCATE (w(n_tiles_tot + 1))
588 ALLOCATE (lambda_newneta(n_tiles_tot + 1))
589 ALLOCATE (v_bar1d(data_size))
590 ALLOCATE (bxv_bar(n_tiles_tot))
591
592 ALLOCATE (v_new1d(data_size))
593 ALLOCATE (bxv_new(n_tiles_tot))
594
595 CALL pw_pool%create_pw(g)
596 CALL pw_pool%create_pw(v_old)
597 CALL pw_pool%create_pw(res_old)
598 CALL pw_pool%create_pw(res_new)
599 CALL pw_pool%create_pw(qainvxres)
600 CALL pw_pool%create_pw(pxqainvxres)
601 CALL pw_pool%create_pw(axvbar)
602
603 IF (use_zero_initial_guess) THEN
604 CALL pw_zero(v_old)
605 lambda0 = 0.0_dp
606 ELSE
607 CALL pw_copy(ps_implicit_env%initial_guess, v_old)
608 lambda0(:) = ps_implicit_env%initial_lambda
609 END IF
610
611 g%array = fourpi*density%array/dielectric%eps%array
612 g_avg = accurate_sum(g%array)/ngpts
613
614 lambda_old(:) = lambda0
615
616! res_old = g - \Delta(v_old) - P(v_old) - B^t * \lambda_old
617 CALL apply_poisson_operator_fft(pw_pool, green, dielectric, v_old, res_old)
618 CALL pw_scale(res_old, -1.0_dp)
619 CALL pw_axpy(g, res_old)
620 IF (data_size /= 0) THEN
621 CALL dgemv('N', data_size, n_tiles_tot, 1.0_dp, bt, data_size, lambda_old, 1, 0.0_dp, btxlambda_old, 1)
622 END IF
623 CALL convert_1dto3d(ps_implicit_env%idx_1dto3d, btxlambda_old, btxlambda_old3d)
624 res_old%array = res_old%array - btxlambda_old3d
625
626! evaluate \Delta^-1(res_old)
627 CALL apply_inv_laplace_operator_fft(pw_pool, green, res_old, qainvxres)
628
629 iter = 1
630 DO
631
632! v_new (v_bar) = v_old + \omega * QAinvxres_old
633 CALL pw_scale(qainvxres, omega)
634 CALL pw_copy(qainvxres, v_new)
635 CALL pw_axpy(v_old, v_new)
636
637! evaluate 1^t * (g - \Delta(\bar{v}) - P(\bar{v}))
638! = 1^t * (g - P(\bar{v}))
639 CALL apply_p_operator(pw_pool, dielectric, v_new, axvbar)
640 axvbar_avg = accurate_sum(axvbar%array)/ngpts
641 gminusaxvbar_avg = g_avg - axvbar_avg
642 CALL pw_grid%para%group%sum(gminusaxvbar_avg)
643
644! evaluate Q_S * \lambda + v_D - B * \bar{v}
645 CALL dgemv('N', n_tiles_tot, n_tiles_tot, 1.0_dp, qs, n_tiles_tot, lambda_old, 1, 0.0_dp, qsxlambda, 1)
646 v_bar1d(ps_implicit_env%idx_1dto3d) = reshape(v_new%array, [data_size])
647 CALL dgemv('N', n_tiles_tot, data_size, 1.0_dp, b, n_tiles_tot, v_bar1d, 1, 0.0_dp, bxv_bar, 1)
648 CALL pw_grid%para%group%sum(bxv_bar)
649! solve R [\lambda; \eta] = [Q_S * \lambda + v_D - B * \bar{v}; 1^t * (g - \Delta(\bar{v}) - P(\bar{v}))]
650 w = 0.0_dp
651 w(:) = [qsxlambda + v_d - bxv_bar, gminusaxvbar_avg]
652 CALL dgemv('N', n_tiles_tot + 1, n_tiles_tot + 1, 1.0_dp, rinv, n_tiles_tot + 1, w, 1, 0.0_dp, lambda_newneta, 1)
653 lambda_new(:) = lambda_newneta(1:n_tiles_tot)
654 eta = lambda_newneta(n_tiles_tot + 1)
655
656! v_new = v_bar + 1 * \eta
657 v_new%array = v_new%array + eta/ngpts
658
659! evaluate B^t * \lambda_new
660 IF (data_size /= 0) THEN
661 CALL dgemv('N', data_size, n_tiles_tot, 1.0_dp, bt, data_size, lambda_new, 1, 0.0_dp, btxlambda_new, 1)
662 END IF
663 CALL convert_1dto3d(ps_implicit_env%idx_1dto3d, btxlambda_new, btxlambda_new3d)
664
665! res_new = res_old - \omega * ( \Delta(QAinvxres_old) + P(QAinvxres_old) ) - B^t * ( \lambda_new - \lambda_old )
666! = (1 - \omega) * res_old - \omega * P(QAinvxres_old) - B^t * ( \lambda_new - \lambda_old )
667 CALL pw_zero(res_new)
668 CALL apply_p_operator(pw_pool, dielectric, qainvxres, pxqainvxres)
669 CALL pw_axpy(pxqainvxres, res_new, -1.0_dp)
670 CALL pw_axpy(res_old, res_new, 1.0_dp - omega)
671 res_new%array = res_new%array + btxlambda_old3d - btxlambda_new3d
672
673! compute the error
674 CALL ps_implicit_compute_error_fft(pw_pool, green, res_new, v_old, v_new, qainvxres, &
675 pres_error, nabs_error)
676! output
677 CALL ps_implicit_output(iter, pres_error, nabs_error, outp_unit)
678 IF (PRESENT(electric_enthalpy)) THEN
679 CALL ps_implicit_compute_ehartree(dielectric, density, btxlambda_new3d, v_new, ehartree, electric_enthalpy)
680 CALL ps_implicit_report_ehartree(ps_implicit_env, outp_unit, ehartree)
681 ps_implicit_env%ehartree = ehartree
682 ps_implicit_env%electric_enthalpy = electric_enthalpy
683 ELSE
684 IF (outp_unit > 0) WRITE (outp_unit, '(A1,/)')
685 END IF
686
687! verbose output
688 IF (poisson_env%parameters%dbc_params%verbose_output) THEN
689 v_new1d(ps_implicit_env%idx_1dto3d) = reshape(v_new%array, [data_size])
690 CALL dgemv('N', n_tiles_tot, data_size, 1.0_dp, b, n_tiles_tot, v_new1d, 1, 0.0_dp, bxv_new, 1)
691 CALL pw_grid%para%group%sum(bxv_new)
692 IF (outp_unit > 0) THEN
693 WRITE (outp_unit, '(T3,A,A)') "======== verbose ", repeat('=', 61)
694 WRITE (outp_unit, '(T20,A)') "Drgn tile vhartree lambda "
695 WRITE (outp_unit, '(T19,A)') repeat('-', 46)
696 nt_tot = 1
697 DO ng = 1, n_contacts
698 DO nt = 1, ps_implicit_env%contacts(ng)%dirichlet_bc%n_tiles
699 WRITE (outp_unit, '(T17,I6,5X,I6,3X,E13.4,E13.4)') ng, nt, bxv_new(nt_tot), lambda_new(nt_tot)
700 nt_tot = nt_tot + 1
701 END DO
702 END DO
703 WRITE (outp_unit, '(T3,A)') repeat('=', 78)
704 END IF
705 END IF
706
707! check the convergence
708 iter = iter + 1
709 reached_max_iter = iter > max_iter
710 reached_tol = pres_error <= tol
711 ps_implicit_env%times_called = ps_implicit_env%times_called + 1
712 IF (pres_error > large_error) THEN
713 cpabort("Poisson solver did not converge.")
714 END IF
715 IF (reached_max_iter .OR. reached_tol) EXIT
716
717! update
718 CALL pw_copy(v_new, v_old)
719 lambda_old(:) = lambda_new
720 CALL pw_copy(res_new, res_old)
721 btxlambda_old3d(:, :, :) = btxlambda_new3d
722
723 END DO
724 CALL ps_implicit_print_convergence_msg(iter, max_iter, outp_unit)
725
726 IF ((times_called /= 0) .AND. (.NOT. use_zero_initial_guess)) THEN
727 CALL pw_copy(v_new, ps_implicit_env%initial_guess)
728 ps_implicit_env%initial_lambda(:) = lambda_new
729 END IF
730
731 ps_implicit_env%cstr_charge%array = btxlambda_new3d
732 IF (PRESENT(electric_enthalpy)) electric_enthalpy = ps_implicit_env%electric_enthalpy
733! compute the extra contribution to the Hamiltonian due to the presence of dielectric
734 block
735 TYPE(pw_r3d_rs_type) :: tmp
736 tmp%pw_grid => ps_implicit_env%v_eps%pw_grid
737 tmp%array => ps_implicit_env%v_eps%array
738 CALL ps_implicit_compute_veps(pw_pool, dielectric, v_new, tmp)
739 END block
740
741 CALL pw_pool%give_back_pw(g)
742 CALL pw_pool%give_back_pw(v_old)
743 CALL pw_pool%give_back_pw(res_old)
744 CALL pw_pool%give_back_pw(res_new)
745 CALL pw_pool%give_back_pw(qainvxres)
746 CALL pw_pool%give_back_pw(pxqainvxres)
747 CALL pw_pool%give_back_pw(axvbar)
748
749 CALL timestop(handle)
750
752
753! **************************************************************************************************
754!> \brief implicit Poisson solver for mixed boundary conditions (Neumann + Dirichlet)
755!> \param poisson_env poisson environment
756!> \param density electron density
757!> \param v_new electrostatic potential
758!> \param electric_enthalpy electric enthalpy
759!> \par History
760!> 10.2014 created [Hossein Bani-Hashemian]
761!> 11.2015 revised [Hossein Bani-Hashemian]
762!> \author Mohammad Hossein Bani-Hashemian
763! **************************************************************************************************
764 SUBROUTINE implicit_poisson_solver_mixed(poisson_env, density, v_new, electric_enthalpy)
765
766 TYPE(pw_poisson_type), INTENT(IN) :: poisson_env
767 TYPE(pw_r3d_rs_type), INTENT(IN) :: density
768 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: v_new
769 REAL(dp), INTENT(OUT), OPTIONAL :: electric_enthalpy
770
771 CHARACTER(LEN=*), PARAMETER :: routinen = 'implicit_poisson_solver_mixed'
772
773 INTEGER :: data_size, handle, iter, j, lb1, lb2, lb3, max_iter, n_contacts, n_tiles_tot, &
774 neumann_directions, ng, ngpts_local, nt, nt_tot, outp_unit, times_called, ub1, ub2, ub3
775 INTEGER(KIND=int_8) :: ngpts
776 INTEGER, DIMENSION(2, 3) :: bounds_local
777 INTEGER, DIMENSION(3) :: npts_local
778 LOGICAL :: reached_max_iter, reached_tol, &
779 use_zero_initial_guess
780 REAL(dp) :: axvbar_avg, ehartree, eta, g_avg, &
781 gminusaxvbar_avg, nabs_error, omega, &
782 pres_error, tol, vol_scfac
783 REAL(dp), ALLOCATABLE, DIMENSION(:) :: btxlambda_new, btxlambda_old, bxv_bar, bxv_new, &
784 lambda0, lambda_new, lambda_newneta, lambda_old, qsxlambda, v_bar1d, v_d, v_new1d, w
785 REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: b, bt, qs, rinv
786 REAL(dp), ALLOCATABLE, DIMENSION(:, :, :) :: btxlambda_new3d, btxlambda_old3d
787 TYPE(dct_type), POINTER :: dct_env
788 TYPE(dielectric_type), POINTER :: dielectric
789 TYPE(greens_fn_type), POINTER :: green
790 TYPE(ps_implicit_type), POINTER :: ps_implicit_env
791 TYPE(pw_grid_type), POINTER :: dct_pw_grid, pw_grid
792 TYPE(pw_pool_type), POINTER :: pw_pool, pw_pool_xpndd
793 TYPE(pw_r3d_rs_type) :: axvbar, density_xpndd, g, pxqainvxres, &
794 qainvxres, res_new, res_old, &
795 v_eps_xpndd, v_new_xpndd, v_old
796
797 CALL timeset(routinen, handle)
798
799 pw_pool => poisson_env%pw_pools(poisson_env%pw_level)%pool
800 pw_grid => pw_pool%pw_grid
801 dielectric => poisson_env%implicit_env%dielectric
802 green => poisson_env%green_fft
803 ps_implicit_env => poisson_env%implicit_env
804 dct_env => ps_implicit_env%dct_env
805
806 dct_pw_grid => poisson_env%dct_pw_grid
807 ngpts_local = dct_pw_grid%ngpts_local
808 ngpts = dct_pw_grid%ngpts
809 npts_local = dct_pw_grid%npts_local
810 bounds_local = dct_pw_grid%bounds_local
811 tol = poisson_env%parameters%ps_implicit_params%tol
812 omega = poisson_env%parameters%ps_implicit_params%omega
813 max_iter = poisson_env%parameters%ps_implicit_params%max_iter
814 use_zero_initial_guess = poisson_env%parameters%ps_implicit_params%zero_initial_guess
815 neumann_directions = poisson_env%parameters%ps_implicit_params%neumann_directions
816 times_called = ps_implicit_env%times_called
817
818 SELECT CASE (neumann_directions)
819 CASE (neumannxyz)
820 vol_scfac = 8.0_dp
821 CASE (neumannxy, neumannxz, neumannyz)
822 vol_scfac = 4.0_dp
823 CASE (neumannx, neumanny, neumannz)
824 vol_scfac = 2.0_dp
825 END SELECT
826
827 n_contacts = SIZE(ps_implicit_env%contacts)
828 n_tiles_tot = 0
829 DO j = 1, n_contacts
830 n_tiles_tot = n_tiles_tot + ps_implicit_env%contacts(j)%dirichlet_bc%n_tiles
831 END DO
832
833 IF (dct_pw_grid%para%blocked) THEN
834 data_size = product(npts_local)
835 ELSE IF (dct_pw_grid%para%ray_distribution) THEN
836 data_size = ngpts_local
837 ELSE ! parallel run with np = 1
838 data_size = product(npts_local)
839 END IF
840
841 CALL pw_pool_create(pw_pool_xpndd, pw_grid=dct_pw_grid)
842
843! check if this is the first scf iteration
844 IF (times_called == 0) CALL ps_implicit_initial_guess_create(ps_implicit_env, pw_pool_xpndd)
845
846 ALLOCATE (b(n_tiles_tot, data_size))
847 ALLOCATE (bt(data_size, n_tiles_tot))
848 ALLOCATE (qs(n_tiles_tot, n_tiles_tot))
849 ALLOCATE (rinv(n_tiles_tot + 1, n_tiles_tot + 1))
850
851 b(:, :) = ps_implicit_env%B
852 bt(:, :) = ps_implicit_env%Bt
853 qs(:, :) = ps_implicit_env%QS
854 rinv(:, :) = ps_implicit_env%Rinv
855 CALL get_voltage(poisson_env%parameters%dbc_params%time, ps_implicit_env%v_D, ps_implicit_env%osc_frac, &
856 ps_implicit_env%frequency, ps_implicit_env%phase, v_d)
857
858 lb1 = bounds_local(1, 1); ub1 = bounds_local(2, 1)
859 lb2 = bounds_local(1, 2); ub2 = bounds_local(2, 2)
860 lb3 = bounds_local(1, 3); ub3 = bounds_local(2, 3)
861
862 ALLOCATE (lambda0(n_tiles_tot), lambda_old(n_tiles_tot), lambda_new(n_tiles_tot))
863 ALLOCATE (btxlambda_old(data_size), btxlambda_new(data_size))
864 ALLOCATE (btxlambda_old3d(lb1:ub1, lb2:ub2, lb3:ub3), btxlambda_new3d(lb1:ub1, lb2:ub2, lb3:ub3))
865 ALLOCATE (qsxlambda(n_tiles_tot))
866 ALLOCATE (w(n_tiles_tot + 1))
867 ALLOCATE (lambda_newneta(n_tiles_tot + 1))
868 ALLOCATE (v_bar1d(data_size))
869 ALLOCATE (bxv_bar(n_tiles_tot))
870
871 ALLOCATE (v_new1d(data_size))
872 ALLOCATE (bxv_new(n_tiles_tot))
873
874 CALL pw_pool_xpndd%create_pw(g)
875 CALL pw_pool_xpndd%create_pw(v_old)
876 CALL pw_pool_xpndd%create_pw(res_old)
877 CALL pw_pool_xpndd%create_pw(res_new)
878 CALL pw_pool_xpndd%create_pw(qainvxres)
879 CALL pw_pool_xpndd%create_pw(pxqainvxres)
880 CALL pw_pool_xpndd%create_pw(axvbar)
881 CALL pw_pool_xpndd%create_pw(density_xpndd)
882 CALL pw_pool_xpndd%create_pw(v_new_xpndd)
883 CALL pw_pool_xpndd%create_pw(v_eps_xpndd)
884
885 IF (use_zero_initial_guess) THEN
886 CALL pw_zero(v_old)
887 lambda0 = 0.0_dp
888 ELSE
889 CALL pw_copy(ps_implicit_env%initial_guess, v_old)
890 lambda0(:) = ps_implicit_env%initial_lambda
891 END IF
892
893 CALL pw_expand(neumann_directions, &
894 dct_env%recv_msgs_bnds, dct_env%dests_expand, dct_env%srcs_expand, &
895 dct_env%flipg_stat, dct_env%bounds_shftd, density, density_xpndd)
896 CALL pw_expand(neumann_directions, &
897 dct_env%recv_msgs_bnds, dct_env%dests_expand, dct_env%srcs_expand, &
898 dct_env%flipg_stat, dct_env%bounds_shftd, v_new, v_new_xpndd)
899
900 g%array = fourpi*density_xpndd%array/dielectric%eps%array
901 g_avg = accurate_sum(g%array)/ngpts
902
903 lambda_old(:) = lambda0
904
905! res_old = g - \Delta(v_old) - P(v_old) - B^t * \lambda_old
906 CALL apply_poisson_operator_dct(pw_pool_xpndd, green, dielectric, v_old, res_old)
907 CALL pw_scale(res_old, -1.0_dp)
908 CALL pw_axpy(g, res_old)
909 IF (data_size /= 0) THEN
910 CALL dgemv('N', data_size, n_tiles_tot, 1.0_dp, bt, data_size, lambda_old, 1, 0.0_dp, btxlambda_old, 1)
911 END IF
912 CALL convert_1dto3d(ps_implicit_env%idx_1dto3d, btxlambda_old, btxlambda_old3d)
913 res_old%array = res_old%array - btxlambda_old3d
914
915! evaluate \Delta^-1(res_old)
916 CALL apply_inv_laplace_operator_dct(pw_pool_xpndd, green, res_old, qainvxres)
917
918 iter = 1
919 DO
920
921! v_new (v_bar) = v_old + \omega * QAinvxres_old
922 CALL pw_scale(qainvxres, omega)
923 CALL pw_copy(qainvxres, v_new_xpndd)
924 CALL pw_axpy(v_old, v_new_xpndd)
925
926! evaluate 1^t * (g - \Delta(\bar{v}) - P(\bar{v}))
927! = 1^t * (g - P(\bar{v}))
928 CALL apply_p_operator(pw_pool_xpndd, dielectric, v_new_xpndd, axvbar)
929 axvbar_avg = accurate_sum(axvbar%array)/ngpts
930 gminusaxvbar_avg = g_avg - axvbar_avg
931 CALL dct_pw_grid%para%group%sum(gminusaxvbar_avg)
932
933! evaluate Q_S * \lambda + v_D - B * \bar{v}
934 CALL dgemv('N', n_tiles_tot, n_tiles_tot, 1.0_dp, qs, n_tiles_tot, lambda_old, 1, 0.0_dp, qsxlambda, 1)
935 v_bar1d(ps_implicit_env%idx_1dto3d) = reshape(v_new_xpndd%array, [data_size])
936 CALL dgemv('N', n_tiles_tot, data_size, 1.0_dp, b, n_tiles_tot, v_bar1d, 1, 0.0_dp, bxv_bar, 1)
937 CALL dct_pw_grid%para%group%sum(bxv_bar)
938! solve R [\lambda; \eta] = [Q_S * \lambda + v_D - B * \bar{v}; 1^t * (g - \Delta(\bar{v}) - P(\bar{v}))]
939 w = 0.0_dp
940 w(:) = [qsxlambda + v_d - bxv_bar, gminusaxvbar_avg]
941 CALL dgemv('N', n_tiles_tot + 1, n_tiles_tot + 1, 1.0_dp, rinv, n_tiles_tot + 1, w, 1, 0.0_dp, lambda_newneta, 1)
942 lambda_new(:) = lambda_newneta(1:n_tiles_tot)
943 eta = lambda_newneta(n_tiles_tot + 1)
944
945! v_new = v_bar + 1 * \eta
946 v_new_xpndd%array = v_new_xpndd%array + eta/ngpts
947
948! evaluate B^t * \lambda_new
949 IF (data_size /= 0) THEN
950 CALL dgemv('N', data_size, n_tiles_tot, 1.0_dp, bt, data_size, lambda_new, 1, 0.0_dp, btxlambda_new, 1)
951 END IF
952 CALL convert_1dto3d(ps_implicit_env%idx_1dto3d, btxlambda_new, btxlambda_new3d)
953
954! res_new = res_old - \omega * ( \Delta(QAinvxres_old) + P(QAinvxres_old) ) - B^t * ( \lambda_new - \lambda_old )
955! = (1 - \omega) * res_old - \omega * P(QAinvxres_old) - B^t * ( \lambda_new - \lambda_old )
956 CALL pw_zero(res_new)
957 CALL apply_p_operator(pw_pool_xpndd, dielectric, qainvxres, pxqainvxres)
958 CALL pw_axpy(pxqainvxres, res_new, -1.0_dp)
959 CALL pw_axpy(res_old, res_new, 1.0_dp - omega)
960 res_new%array = res_new%array - btxlambda_new3d + btxlambda_old3d
961
962! compute the error
963 CALL ps_implicit_compute_error_dct(pw_pool_xpndd, green, res_new, v_old, v_new_xpndd, qainvxres, &
964 pres_error, nabs_error)
965! output
966 CALL ps_implicit_output(iter, pres_error, nabs_error, outp_unit)
967 IF (PRESENT(electric_enthalpy)) THEN
968 CALL ps_implicit_compute_ehartree(dielectric, density_xpndd, btxlambda_new3d, v_new_xpndd, &
969 ehartree, electric_enthalpy)
970 CALL ps_implicit_report_ehartree(ps_implicit_env, outp_unit, ehartree/vol_scfac)
971 ps_implicit_env%ehartree = ehartree/vol_scfac
972 ps_implicit_env%electric_enthalpy = electric_enthalpy/vol_scfac
973 ELSE
974 IF (outp_unit > 0) WRITE (outp_unit, '(A1,/)')
975 END IF
976
977! verbose output
978 IF (poisson_env%parameters%dbc_params%verbose_output) THEN
979 v_new1d(ps_implicit_env%idx_1dto3d) = reshape(v_new_xpndd%array, [data_size])
980 CALL dgemv('N', n_tiles_tot, data_size, 1.0_dp, b, n_tiles_tot, v_new1d, 1, 0.0_dp, bxv_new, 1)
981 CALL pw_grid%para%group%sum(bxv_new)
982 IF (outp_unit > 0) THEN
983 WRITE (outp_unit, '(T3,A)') "======== verbose "//repeat('=', 61)
984 WRITE (outp_unit, '(T20,A)') "Drgn tile vhartree lambda "
985 WRITE (outp_unit, '(T19,A)') repeat('-', 46)
986 nt_tot = 1
987 DO ng = 1, n_contacts
988 DO nt = 1, ps_implicit_env%contacts(ng)%dirichlet_bc%n_tiles
989 WRITE (outp_unit, '(T17,I6,5X,I6,3X,E13.4,E13.4)') ng, nt, bxv_new(nt_tot), lambda_new(nt_tot)
990 nt_tot = nt_tot + 1
991 END DO
992 END DO
993 WRITE (outp_unit, '(T3,A)') repeat('=', 78)
994 END IF
995 END IF
996
997! check the convergence
998 iter = iter + 1
999 reached_max_iter = iter > max_iter
1000 reached_tol = pres_error <= tol
1001 ps_implicit_env%times_called = ps_implicit_env%times_called + 1
1002 IF (pres_error > large_error) THEN
1003 cpabort("Poisson solver did not converge.")
1004 END IF
1005 IF (reached_max_iter .OR. reached_tol) EXIT
1006
1007! update
1008 CALL pw_copy(v_new_xpndd, v_old)
1009 lambda_old(:) = lambda_new
1010 CALL pw_copy(res_new, res_old)
1011 btxlambda_old3d(:, :, :) = btxlambda_new3d
1012
1013 END DO
1014 CALL ps_implicit_print_convergence_msg(iter, max_iter, outp_unit)
1015
1016 CALL pw_shrink(neumann_directions, dct_env%dests_shrink, dct_env%srcs_shrink, &
1017 dct_env%bounds_local_shftd, v_new_xpndd, v_new)
1018
1019 IF ((times_called /= 0) .AND. (.NOT. use_zero_initial_guess)) THEN
1020 CALL pw_copy(v_new_xpndd, ps_implicit_env%initial_guess)
1021 ps_implicit_env%initial_lambda(:) = lambda_new
1022 END IF
1023
1024 ps_implicit_env%cstr_charge%array = btxlambda_new3d
1025 IF (PRESENT(electric_enthalpy)) electric_enthalpy = ps_implicit_env%electric_enthalpy
1026! compute the extra contribution to the Hamiltonian due to the presence of dielectric
1027 CALL ps_implicit_compute_veps(pw_pool_xpndd, dielectric, v_new_xpndd, v_eps_xpndd)
1028 CALL pw_shrink(neumann_directions, dct_env%dests_shrink, dct_env%srcs_shrink, &
1029 dct_env%bounds_local_shftd, v_eps_xpndd, ps_implicit_env%v_eps)
1030
1031 CALL pw_pool_xpndd%give_back_pw(g)
1032 CALL pw_pool_xpndd%give_back_pw(v_old)
1033 CALL pw_pool_xpndd%give_back_pw(res_old)
1034 CALL pw_pool_xpndd%give_back_pw(res_new)
1035 CALL pw_pool_xpndd%give_back_pw(qainvxres)
1036 CALL pw_pool_xpndd%give_back_pw(pxqainvxres)
1037 CALL pw_pool_xpndd%give_back_pw(axvbar)
1038 CALL pw_pool_xpndd%give_back_pw(density_xpndd)
1039 CALL pw_pool_xpndd%give_back_pw(v_new_xpndd)
1040 CALL pw_pool_xpndd%give_back_pw(v_eps_xpndd)
1041 CALL pw_pool_release(pw_pool_xpndd)
1042
1043 CALL timestop(handle)
1044
1045 END SUBROUTINE implicit_poisson_solver_mixed
1046
1047! **************************************************************************************************
1048!> \brief allocates and zeroises initial guess for implicit (iterative) Poisson solver
1049!> \param ps_implicit_env the implicit env containing the initial guess
1050!> \param pw_pool pool of pw grid
1051!> \par History
1052!> 06.2014 created [Hossein Bani-Hashemian]
1053!> \author Mohammad Hossein Bani-Hashemian
1054! **************************************************************************************************
1055 SUBROUTINE ps_implicit_initial_guess_create(ps_implicit_env, pw_pool)
1056
1057 TYPE(ps_implicit_type), INTENT(INOUT), POINTER :: ps_implicit_env
1058 TYPE(pw_pool_type), INTENT(IN), POINTER :: pw_pool
1059
1060 CHARACTER(LEN=*), PARAMETER :: routinen = 'ps_implicit_initial_guess_create'
1061
1062 INTEGER :: handle, n_tiles_tot
1063
1064 CALL timeset(routinen, handle)
1065
1066 n_tiles_tot = SIZE(ps_implicit_env%v_D)
1067 NULLIFY (ps_implicit_env%initial_guess)
1068 ALLOCATE (ps_implicit_env%initial_guess)
1069 CALL pw_pool%create_pw(ps_implicit_env%initial_guess)
1070 CALL pw_zero(ps_implicit_env%initial_guess)
1071 ALLOCATE (ps_implicit_env%initial_lambda(n_tiles_tot))
1072 ps_implicit_env%initial_lambda = 0.0_dp
1073
1074 CALL timestop(handle)
1075
1076 END SUBROUTINE ps_implicit_initial_guess_create
1077
1078! **************************************************************************************************
1079!> \brief prepare blocks B, Bt, QS, R^-1, v_D
1080!> \param pw_pool_orig original pw grid
1081!> \param dct_pw_grid DCT (extended) grid
1082!> \param green green functions for FFT based inverse Laplacian
1083!> \param poisson_params paramaters of the poisson_env
1084!> \param ps_implicit_env the implicit_env that stores the blocks
1085!> \par History
1086!> 10.2014 created [Hossein Bani-Hashemian]
1087!> \author Mohammad Hossein Bani-Hashemian
1088! **************************************************************************************************
1089 SUBROUTINE ps_implicit_prepare_blocks(pw_pool_orig, dct_pw_grid, green, &
1090 poisson_params, ps_implicit_env)
1091
1092 TYPE(pw_pool_type), INTENT(IN), POINTER :: pw_pool_orig
1093 TYPE(pw_grid_type), INTENT(IN), POINTER :: dct_pw_grid
1094 TYPE(greens_fn_type), INTENT(IN) :: green
1095 TYPE(pw_poisson_parameter_type), INTENT(IN) :: poisson_params
1096 TYPE(ps_implicit_type), INTENT(INOUT), POINTER :: ps_implicit_env
1097
1098 CHARACTER(LEN=*), PARAMETER :: routinen = 'ps_implicit_prepare_blocks'
1099
1100 INTEGER :: data_size, handle, i, indx1, indx2, info, j, k, l, lb1, lb2, lb3, n_contacts, &
1101 n_tiles, n_tiles_tot, neumann_directions, ngpts_local, ub1, ub2, ub3, unit_nr
1102 INTEGER(KIND=int_8) :: ngpts
1103 INTEGER, ALLOCATABLE, DIMENSION(:) :: ipiv
1104 INTEGER, DIMENSION(2, 3) :: bounds, bounds_local
1105 INTEGER, DIMENSION(3) :: npts, npts_local
1106 LOGICAL :: done_preparing
1107 REAL(dp) :: tile_volume, vol_scfac
1108 REAL(dp), ALLOCATABLE, DIMENSION(:) :: bxunit_vec, test_vec, work_arr
1109 REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: qainvxbt, r
1110 TYPE(cp_logger_type), POINTER :: logger
1111 TYPE(dct_type), POINTER :: dct_env
1112 TYPE(pw_grid_type), POINTER :: pw_grid_orig
1113 TYPE(pw_pool_type), POINTER :: pw_pool_xpndd
1114 TYPE(pw_r3d_rs_type) :: pw_in, pw_out
1115
1116 CALL timeset(routinen, handle)
1117
1118 pw_grid_orig => pw_pool_orig%pw_grid
1119
1120 logger => cp_get_default_logger()
1121 IF (logger%para_env%is_source()) THEN
1122 unit_nr = cp_logger_get_default_unit_nr(logger, local=.true.)
1123 ELSE
1124 unit_nr = -1
1125 END IF
1126
1127 SELECT CASE (poisson_params%ps_implicit_params%boundary_condition)
1128 CASE (mixed_bc)
1129
1130 ngpts_local = dct_pw_grid%ngpts_local
1131 ngpts = dct_pw_grid%ngpts
1132 npts_local = dct_pw_grid%npts_local
1133 npts = dct_pw_grid%npts
1134 bounds_local = dct_pw_grid%bounds_local
1135 bounds = dct_pw_grid%bounds
1136 dct_env => ps_implicit_env%dct_env
1137
1138 neumann_directions = poisson_params%ps_implicit_params%neumann_directions
1139
1140 SELECT CASE (neumann_directions)
1141 CASE (neumannxyz)
1142 vol_scfac = 8.0_dp
1143 CASE (neumannxy, neumannxz, neumannyz)
1144 vol_scfac = 4.0_dp
1145 CASE (neumannx, neumanny, neumannz)
1146 vol_scfac = 2.0_dp
1147 END SELECT
1148
1149! evaluate indices for converting 3D arrays into 1D arrays
1150 lb1 = bounds_local(1, 1); ub1 = bounds_local(2, 1)
1151 lb2 = bounds_local(1, 2); ub2 = bounds_local(2, 2)
1152 lb3 = bounds_local(1, 3); ub3 = bounds_local(2, 3)
1153
1154 IF (dct_pw_grid%para%blocked) THEN
1155 data_size = product(npts_local)
1156 ELSE IF (dct_pw_grid%para%ray_distribution) THEN
1157 data_size = ngpts_local
1158 ELSE ! parallel run with np = 1
1159 data_size = product(npts_local)
1160 END IF
1161
1162 ALLOCATE (ps_implicit_env%idx_1dto3d(data_size))
1163 l = 1
1164 ! Give up on OpenMP for now (at least the Intel compiler has an issue here)
1165 ! An automatic OpenMP parallelization of this loop might be tricky
1166 ! because of the l incrementation
1167 DO k = lb3, ub3
1168 DO j = lb2, ub2
1169 DO i = lb1, ub1
1170 ps_implicit_env%idx_1dto3d(l) = (i - lb1 + 1) + &
1171 (j - lb2)*npts_local(1) + &
1172 (k - lb3)*npts_local(1)*npts_local(2)
1173 l = l + 1
1174 END DO
1175 END DO
1176 END DO
1177
1178 n_contacts = SIZE(ps_implicit_env%contacts)
1179 n_tiles_tot = 0
1180 DO j = 1, n_contacts
1181 n_tiles_tot = n_tiles_tot + ps_implicit_env%contacts(j)%dirichlet_bc%n_tiles
1182 END DO
1183
1184 ALLOCATE (ps_implicit_env%B(n_tiles_tot, data_size))
1185 ALLOCATE (ps_implicit_env%Bt(data_size, n_tiles_tot))
1186 ALLOCATE (ps_implicit_env%QS(n_tiles_tot, n_tiles_tot))
1187 ALLOCATE (ps_implicit_env%Rinv(n_tiles_tot + 1, n_tiles_tot + 1))
1188 ALLOCATE (ps_implicit_env%v_D(n_tiles_tot))
1189 ALLOCATE (ps_implicit_env%osc_frac(n_tiles_tot))
1190 ALLOCATE (ps_implicit_env%frequency(n_tiles_tot))
1191 ALLOCATE (ps_implicit_env%phase(n_tiles_tot))
1192
1193 ALLOCATE (qainvxbt(data_size, n_tiles_tot))
1194 ALLOCATE (bxunit_vec(n_tiles_tot))
1195 ALLOCATE (test_vec(n_tiles_tot))
1196 ALLOCATE (r(n_tiles_tot + 1, n_tiles_tot + 1))
1197 ALLOCATE (work_arr(n_tiles_tot + 1), ipiv(n_tiles_tot + 1)) ! LAPACK work and ipiv arrays
1198
1199! prepare pw_pool for evaluating inverse Laplacian of tile_pw's using DCT
1200 CALL pw_pool_create(pw_pool_xpndd, pw_grid=dct_pw_grid)
1201
1202! set up B, B^t, (\Delta^-1)*B^t
1203 indx1 = 1
1204 DO j = 1, n_contacts
1205 n_tiles = ps_implicit_env%contacts(j)%dirichlet_bc%n_tiles
1206 indx2 = indx1 + n_tiles - 1
1207 DO i = 1, n_tiles
1208
1209 CALL pw_pool_xpndd%create_pw(pw_in)
1210 CALL pw_expand(neumann_directions, &
1211 dct_env%recv_msgs_bnds, dct_env%dests_expand, dct_env%srcs_expand, &
1212 dct_env%flipg_stat, dct_env%bounds_shftd, &
1213 ps_implicit_env%contacts(j)%dirichlet_bc%tiles(i)%tile%tile_pw, pw_in)
1214
1215 tile_volume = ps_implicit_env%contacts(j)%dirichlet_bc%tiles(i)%tile%volume
1216 CALL pw_scale(pw_in, 1.0_dp/(vol_scfac*tile_volume)) ! normalize tile_pw
1217 ps_implicit_env%Bt(ps_implicit_env%idx_1dto3d, indx1 + i - 1) = reshape(pw_in%array, [data_size])
1218
1219 CALL pw_pool_xpndd%create_pw(pw_out)
1220 CALL apply_inv_laplace_operator_dct(pw_pool_xpndd, green, pw_in, pw_out)
1221 qainvxbt(ps_implicit_env%idx_1dto3d, indx1 + i - 1) = reshape(pw_out%array, [data_size])
1222 ! the electrostatic potential has opposite sign by internal convention
1223 ps_implicit_env%v_D(indx1 + i - 1) = -1.0_dp*ps_implicit_env%contacts(j)%dirichlet_bc%v_D
1224 ps_implicit_env%osc_frac(indx1 + i - 1) = ps_implicit_env%contacts(j)%dirichlet_bc%osc_frac
1225 ps_implicit_env%frequency(indx1 + i - 1) = ps_implicit_env%contacts(j)%dirichlet_bc%frequency
1226 ps_implicit_env%phase(indx1 + i - 1) = ps_implicit_env%contacts(j)%dirichlet_bc%phase
1227
1228 CALL pw_pool_xpndd%give_back_pw(pw_in)
1229 CALL pw_pool_xpndd%give_back_pw(pw_out)
1230 END DO
1231 indx1 = indx2 + 1
1232 END DO
1233 ps_implicit_env%B(:, :) = transpose(ps_implicit_env%Bt)
1234
1235! evaluate QS = - B*(\Delta^-1)*B^t
1236 IF (data_size /= 0) THEN
1237 CALL dgemm('N', 'N', n_tiles_tot, n_tiles_tot, data_size, &
1238 -1.0_dp, ps_implicit_env%B, n_tiles_tot, qainvxbt, &
1239 data_size, 0.0_dp, ps_implicit_env%QS, n_tiles_tot)
1240 END IF
1241 CALL pw_grid_orig%para%group%sum(ps_implicit_env%QS)
1242
1243! evaluate B*1
1244 bxunit_vec(:) = sum(ps_implicit_env%B, 2)/ngpts
1245 CALL pw_grid_orig%para%group%sum(bxunit_vec)
1246! set up R = [QS B*1; (B*1)^t 0]
1247 r = 0.0_dp
1248 r(1:n_tiles_tot, 1:n_tiles_tot) = ps_implicit_env%QS
1249 r(1:n_tiles_tot, n_tiles_tot + 1) = bxunit_vec
1250 r(n_tiles_tot + 1, 1:n_tiles_tot) = bxunit_vec
1251! evaluate R^(-1)
1252 ps_implicit_env%Rinv(:, :) = r
1253 CALL dgetrf(n_tiles_tot + 1, n_tiles_tot + 1, ps_implicit_env%Rinv, n_tiles_tot + 1, ipiv, info)
1254 IF (info /= 0) THEN
1255 CALL cp_abort(__location__, &
1256 "R is (nearly) singular! Either two Dirichlet constraints are identical or "// &
1257 "you need to reduce the number of tiles.")
1258 END IF
1259 CALL dgetri(n_tiles_tot + 1, ps_implicit_env%Rinv, n_tiles_tot + 1, ipiv, work_arr, n_tiles_tot + 1, info)
1260 IF (info /= 0) THEN
1261 cpabort("Inversion of R failed!")
1262 END IF
1263
1264 DEALLOCATE (qainvxbt, bxunit_vec, r, work_arr, ipiv)
1265 CALL pw_pool_release(pw_pool_xpndd)
1266
1267 done_preparing = .true.
1268 CALL pw_grid_orig%para%group%sum(done_preparing)
1269 IF ((unit_nr > 0) .AND. done_preparing) THEN
1270 WRITE (unit_nr, "(T3,A,/,T3,A,/,A)") "POISSON| ... Done. ", repeat('-', 78)
1271 END IF
1272
1273 CASE (mixed_periodic_bc)
1274
1275 ngpts_local = pw_grid_orig%ngpts_local
1276 ngpts = pw_grid_orig%ngpts
1277 npts_local = pw_grid_orig%npts_local
1278 npts = pw_grid_orig%npts
1279 bounds_local = pw_grid_orig%bounds_local
1280 bounds = pw_grid_orig%bounds
1281 dct_env => ps_implicit_env%dct_env
1282
1283! evaluate indices for converting 3D arrays into 1D arrays
1284 lb1 = bounds_local(1, 1); ub1 = bounds_local(2, 1)
1285 lb2 = bounds_local(1, 2); ub2 = bounds_local(2, 2)
1286 lb3 = bounds_local(1, 3); ub3 = bounds_local(2, 3)
1287
1288 IF (pw_grid_orig%para%blocked) THEN
1289 data_size = product(npts_local)
1290 ELSE IF (pw_grid_orig%para%ray_distribution) THEN
1291 data_size = ngpts_local
1292 ELSE ! parallel run with np = 1
1293 data_size = product(npts_local)
1294 END IF
1295
1296 ALLOCATE (ps_implicit_env%idx_1dto3d(data_size))
1297 l = 1
1298 ! Give up on OpenMP for now (at least the Intel compiler has an issue here)
1299 ! An automatic OpenMP parallelization of this loop might be tricky
1300 ! because of the l incrementation
1301 DO k = lb3, ub3
1302 DO j = lb2, ub2
1303 DO i = lb1, ub1
1304 ps_implicit_env%idx_1dto3d(l) = (i - lb1 + 1) + &
1305 (j - lb2)*npts_local(1) + &
1306 (k - lb3)*npts_local(1)*npts_local(2)
1307 l = l + 1
1308 END DO
1309 END DO
1310 END DO
1311
1312 n_contacts = SIZE(ps_implicit_env%contacts)
1313 n_tiles_tot = 0
1314 DO j = 1, n_contacts
1315 n_tiles_tot = n_tiles_tot + ps_implicit_env%contacts(j)%dirichlet_bc%n_tiles
1316 END DO
1317
1318 ALLOCATE (ps_implicit_env%B(n_tiles_tot, data_size))
1319 ALLOCATE (ps_implicit_env%Bt(data_size, n_tiles_tot))
1320 ALLOCATE (ps_implicit_env%QS(n_tiles_tot, n_tiles_tot))
1321 ALLOCATE (ps_implicit_env%Rinv(n_tiles_tot + 1, n_tiles_tot + 1))
1322 ALLOCATE (ps_implicit_env%v_D(n_tiles_tot))
1323 ALLOCATE (ps_implicit_env%osc_frac(n_tiles_tot))
1324 ALLOCATE (ps_implicit_env%frequency(n_tiles_tot))
1325 ALLOCATE (ps_implicit_env%phase(n_tiles_tot))
1326
1327 ALLOCATE (qainvxbt(data_size, n_tiles_tot))
1328 ALLOCATE (bxunit_vec(n_tiles_tot))
1329 ALLOCATE (test_vec(n_tiles_tot))
1330 ALLOCATE (r(n_tiles_tot + 1, n_tiles_tot + 1))
1331 ALLOCATE (work_arr(n_tiles_tot + 1), ipiv(n_tiles_tot + 1))
1332
1333! set up B, B^t, (\Delta^-1)*B^t
1334 indx1 = 1
1335 DO j = 1, n_contacts
1336 n_tiles = ps_implicit_env%contacts(j)%dirichlet_bc%n_tiles
1337 indx2 = indx1 + n_tiles - 1
1338 DO i = 1, n_tiles
1339 CALL pw_pool_orig%create_pw(pw_in)
1340 CALL pw_copy(ps_implicit_env%contacts(j)%dirichlet_bc%tiles(i)%tile%tile_pw, pw_in)
1341
1342 tile_volume = ps_implicit_env%contacts(j)%dirichlet_bc%tiles(i)%tile%volume
1343 CALL pw_scale(pw_in, 1.0_dp/tile_volume) ! normalize tile_pw
1344 ps_implicit_env%Bt(ps_implicit_env%idx_1dto3d, indx1 + i - 1) = reshape(pw_in%array, [data_size])
1345
1346 CALL pw_pool_orig%create_pw(pw_out)
1347 CALL apply_inv_laplace_operator_fft(pw_pool_orig, green, pw_in, pw_out)
1348 qainvxbt(ps_implicit_env%idx_1dto3d, indx1 + i - 1) = reshape(pw_out%array, [data_size])
1349 ! the electrostatic potential has opposite sign by internal convention
1350 ps_implicit_env%v_D(indx1 + i - 1) = -1.0_dp*ps_implicit_env%contacts(j)%dirichlet_bc%v_D
1351 ps_implicit_env%osc_frac(indx1 + i - 1) = ps_implicit_env%contacts(j)%dirichlet_bc%osc_frac
1352 ps_implicit_env%frequency(indx1 + i - 1) = ps_implicit_env%contacts(j)%dirichlet_bc%frequency
1353 ps_implicit_env%phase(indx1 + i - 1) = ps_implicit_env%contacts(j)%dirichlet_bc%phase
1354
1355 CALL pw_pool_orig%give_back_pw(pw_in)
1356 CALL pw_pool_orig%give_back_pw(pw_out)
1357 END DO
1358 indx1 = indx2 + 1
1359 END DO
1360 ps_implicit_env%B(:, :) = transpose(ps_implicit_env%Bt)
1361
1362! evaluate QS = - B*(\Delta^-1)*B^t
1363 IF (data_size /= 0) THEN
1364 CALL dgemm('N', 'N', n_tiles_tot, n_tiles_tot, data_size, &
1365 -1.0_dp, ps_implicit_env%B, n_tiles_tot, qainvxbt, &
1366 data_size, 0.0_dp, ps_implicit_env%QS, n_tiles_tot)
1367 END IF
1368 CALL pw_grid_orig%para%group%sum(ps_implicit_env%QS)
1369
1370! evaluate B*1
1371 bxunit_vec(:) = sum(ps_implicit_env%B, 2)/ngpts
1372 CALL pw_grid_orig%para%group%sum(bxunit_vec)
1373! set up R = [QS B*1; (B*1)^t 0]
1374 r = 0.0_dp
1375 r(1:n_tiles_tot, 1:n_tiles_tot) = ps_implicit_env%QS
1376 r(1:n_tiles_tot, n_tiles_tot + 1) = bxunit_vec
1377 r(n_tiles_tot + 1, 1:n_tiles_tot) = bxunit_vec
1378! evaluate R^(-1)
1379 ps_implicit_env%Rinv(:, :) = r
1380 CALL dgetrf(n_tiles_tot + 1, n_tiles_tot + 1, ps_implicit_env%Rinv, n_tiles_tot + 1, ipiv, info)
1381 IF (info /= 0) THEN
1382 CALL cp_abort(__location__, &
1383 "R is (nearly) singular! Either two Dirichlet constraints are identical or "// &
1384 "you need to reduce the number of tiles.")
1385 END IF
1386 CALL dgetri(n_tiles_tot + 1, ps_implicit_env%Rinv, n_tiles_tot + 1, ipiv, work_arr, n_tiles_tot + 1, info)
1387 IF (info /= 0) THEN
1388 cpabort("Inversion of R failed!")
1389 END IF
1390
1391 DEALLOCATE (qainvxbt, bxunit_vec, r, work_arr, ipiv)
1392
1393 done_preparing = .true.
1394 CALL pw_grid_orig%para%group%sum(done_preparing)
1395 IF ((unit_nr > 0) .AND. done_preparing) THEN
1396 WRITE (unit_nr, "(T3,A,/,T3,A,/,A)") "POISSON| ... Done. ", repeat('-', 78)
1397 END IF
1398
1399 CASE (periodic_bc, neumann_bc)
1400
1401 ALLOCATE (ps_implicit_env%idx_1dto3d(1))
1402 ALLOCATE (ps_implicit_env%B(1, 1))
1403 ALLOCATE (ps_implicit_env%Bt(1, 1))
1404 ALLOCATE (ps_implicit_env%QS(1, 1))
1405 ALLOCATE (ps_implicit_env%Rinv(1, 1))
1406 ALLOCATE (ps_implicit_env%v_D(1))
1407 ALLOCATE (ps_implicit_env%osc_frac(1))
1408 ALLOCATE (ps_implicit_env%frequency(1))
1409 ALLOCATE (ps_implicit_env%phase(1))
1410
1411 ps_implicit_env%idx_1dto3d = 1
1412 ps_implicit_env%B = 0.0_dp
1413 ps_implicit_env%Bt = 0.0_dp
1414 ps_implicit_env%QS = 0.0_dp
1415 ps_implicit_env%Rinv = 0.0_dp
1416 ps_implicit_env%v_D = 0.0_dp
1417
1418 CASE DEFAULT
1419 CALL cp_abort(__location__, &
1420 "Please specify the type of boundary conditions using the "// &
1421 "input file keyword BOUNDARY_CONDITIONS.")
1422 END SELECT
1423
1424 CALL timestop(handle)
1425
1426 END SUBROUTINE ps_implicit_prepare_blocks
1427
1428! **************************************************************************************************
1429!> \brief Evaluates the action of the operator P on a given matrix v, defined
1430!> as: P(v) := - \nabla_r(\ln(\eps)) \cdot \nabla_r(v)
1431!> \param pw_pool pool of pw grid
1432!> \param dielectric dielectric_type containing eps
1433!> \param v input matrix
1434!> \param Pxv action of the operator P on v
1435!> \par History
1436!> 07.2014 created [Hossein Bani-Hashemian]
1437!> \author Mohammad Hossein Bani-Hashemian
1438! **************************************************************************************************
1439 SUBROUTINE apply_p_operator(pw_pool, dielectric, v, Pxv)
1440
1441 TYPE(pw_pool_type), POINTER :: pw_pool
1442 TYPE(dielectric_type), INTENT(IN), POINTER :: dielectric
1443 TYPE(pw_r3d_rs_type), INTENT(IN) :: v
1444 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: pxv
1445
1446 CHARACTER(LEN=*), PARAMETER :: routinen = 'apply_P_operator'
1447
1448 INTEGER :: handle, i
1449 TYPE(pw_r3d_rs_type), DIMENSION(3) :: dv
1450
1451 CALL timeset(routinen, handle)
1452
1453 DO i = 1, 3
1454 CALL pw_pool%create_pw(dv(i))
1455 END DO
1456
1457 CALL derive_fft(v, dv, pw_pool)
1458 associate(dln_eps => dielectric%dln_eps)
1459 pxv%array = -(dv(1)%array*dln_eps(1)%array + &
1460 dv(2)%array*dln_eps(2)%array + &
1461 dv(3)%array*dln_eps(3)%array)
1462 END associate
1463
1464 DO i = 1, 3
1465 CALL pw_pool%give_back_pw(dv(i))
1466 END DO
1467
1468 CALL timestop(handle)
1469
1470 END SUBROUTINE apply_p_operator
1471
1472! **************************************************************************************************
1473!> \brief Evaluates the action of the inverse of the Laplace operator on a given 3d matrix
1474!> \param pw_pool pool of pw grid
1475!> \param green green functions for FFT based inverse Laplacian
1476!> \param pw_in pw_in (density)
1477!> \param pw_out pw_out (potential)
1478!> \par History
1479!> 07.2014 created [Hossein Bani-Hashemian]
1480!> \author Mohammad Hossein Bani-Hashemian
1481! **************************************************************************************************
1482 SUBROUTINE apply_inv_laplace_operator_fft(pw_pool, green, pw_in, pw_out)
1483
1484 TYPE(pw_pool_type), INTENT(IN), POINTER :: pw_pool
1485 TYPE(greens_fn_type), INTENT(IN) :: green
1486 TYPE(pw_r3d_rs_type), INTENT(IN) :: pw_in
1487 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: pw_out
1488
1489 CHARACTER(LEN=*), PARAMETER :: routinen = 'apply_inv_laplace_operator_fft'
1490
1491 INTEGER :: handle, ig, ng
1492 REAL(dp) :: prefactor
1493 TYPE(pw_c1d_gs_type) :: pw_in_gs
1494 TYPE(pw_grid_type), POINTER :: pw_grid
1495
1496 CALL timeset(routinen, handle)
1497
1498! here I divide by fourpi to cancel out the prefactor fourpi in influence_fn
1499 prefactor = 1.0_dp/fourpi
1500
1501 pw_grid => pw_pool%pw_grid
1502 ng = SIZE(pw_grid%gsq)
1503
1504 CALL pw_pool%create_pw(pw_in_gs)
1505
1506 CALL pw_transfer(pw_in, pw_in_gs)
1507 DO ig = 1, ng
1508 pw_in_gs%array(ig) = prefactor*pw_in_gs%array(ig)*green%influence_fn%array(ig)
1509 END DO
1510 CALL pw_transfer(pw_in_gs, pw_out)
1511
1512 CALL pw_pool%give_back_pw(pw_in_gs)
1513
1514 CALL timestop(handle)
1515
1516 END SUBROUTINE apply_inv_laplace_operator_fft
1517
1518! **************************************************************************************************
1519!> \brief Evaluates the action of the inverse of the Laplace operator on a given
1520!> 3d matrix using DCT-I
1521!> \param pw_pool pool of pw grid
1522!> \param green the greens_fn_type data holding a valid dct_influence_fn
1523!> \param pw_in pw_in (density)
1524!> \param pw_out pw_out (potential)
1525!> \par History
1526!> 07.2014 created [Hossein Bani-Hashemian]
1527!> 11.2015 revised [Hossein Bani-Hashemian]
1528!> \author Mohammad Hossein Bani-Hashemian
1529! **************************************************************************************************
1530 SUBROUTINE apply_inv_laplace_operator_dct(pw_pool, green, pw_in, pw_out)
1531
1532 TYPE(pw_pool_type), INTENT(IN), POINTER :: pw_pool
1533 TYPE(greens_fn_type), INTENT(IN) :: green
1534 TYPE(pw_r3d_rs_type), INTENT(IN) :: pw_in
1535 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: pw_out
1536
1537 CHARACTER(LEN=*), PARAMETER :: routinen = 'apply_inv_laplace_operator_dct'
1538
1539 INTEGER :: handle, ig, ng
1540 REAL(dp) :: prefactor
1541 TYPE(pw_c1d_gs_type) :: pw_in_gs
1542 TYPE(pw_grid_type), POINTER :: pw_grid
1543
1544 CALL timeset(routinen, handle)
1545
1546! here I divide by fourpi to cancel out the prefactor fourpi in influence_fn
1547 prefactor = 1.0_dp/fourpi
1548
1549 pw_grid => pw_pool%pw_grid
1550 ng = SIZE(pw_grid%gsq)
1551
1552 CALL pw_pool%create_pw(pw_in_gs)
1553
1554 CALL pw_transfer(pw_in, pw_in_gs)
1555 DO ig = 1, ng
1556 pw_in_gs%array(ig) = prefactor*pw_in_gs%array(ig)*green%dct_influence_fn%array(ig)
1557 END DO
1558 CALL pw_transfer(pw_in_gs, pw_out)
1559
1560 CALL pw_pool%give_back_pw(pw_in_gs)
1561
1562 CALL timestop(handle)
1563
1564 END SUBROUTINE apply_inv_laplace_operator_dct
1565
1566! **************************************************************************************************
1567!> \brief Evaluates the action of the Laplace operator on a given 3d matrix
1568!> \param pw_pool pool of pw grid
1569!> \param green green functions for FFT based inverse Laplacian
1570!> \param pw_in pw_in (potential)
1571!> \param pw_out pw_out (density)
1572!> \par History
1573!> 07.2014 created [Hossein Bani-Hashemian]
1574!> \author Mohammad Hossein Bani-Hashemian
1575! **************************************************************************************************
1576 SUBROUTINE apply_laplace_operator_fft(pw_pool, green, pw_in, pw_out)
1577
1578 TYPE(pw_pool_type), INTENT(IN), POINTER :: pw_pool
1579 TYPE(greens_fn_type), INTENT(IN) :: green
1580 TYPE(pw_r3d_rs_type), INTENT(IN) :: pw_in
1581 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: pw_out
1582
1583 CHARACTER(LEN=*), PARAMETER :: routinen = 'apply_laplace_operator_fft'
1584
1585 INTEGER :: g0_index, handle, ig, ng
1586 LOGICAL :: have_g0
1587 REAL(dp) :: prefactor
1588 TYPE(pw_c1d_gs_type) :: pw_in_gs
1589 TYPE(pw_grid_type), POINTER :: pw_grid
1590
1591 CALL timeset(routinen, handle)
1592
1593! here I multiply by fourpi to cancel out the prefactor fourpi in influence_fn
1594 prefactor = fourpi
1595
1596 pw_grid => pw_pool%pw_grid
1597 ng = SIZE(pw_in%pw_grid%gsq)
1598 have_g0 = green%influence_fn%pw_grid%have_g0
1599
1600 CALL pw_pool%create_pw(pw_in_gs)
1601
1602 CALL pw_transfer(pw_in, pw_in_gs)
1603
1604 IF (have_g0) THEN
1605 g0_index = green%influence_fn%pw_grid%first_gne0 - 1
1606 pw_in_gs%array(g0_index) = 0.0_dp
1607 END IF
1608 DO ig = green%influence_fn%pw_grid%first_gne0, ng
1609 pw_in_gs%array(ig) = prefactor*(pw_in_gs%array(ig)/green%influence_fn%array(ig))
1610 END DO
1611
1612 CALL pw_transfer(pw_in_gs, pw_out)
1613
1614 CALL pw_pool%give_back_pw(pw_in_gs)
1615
1616 CALL timestop(handle)
1617
1618 END SUBROUTINE apply_laplace_operator_fft
1619
1620! **************************************************************************************************
1621!> \brief Evaluates the action of the Laplace operator on a given 3d matrix using DCT-I
1622!> \param pw_pool pool of pw grid
1623!> \param green the greens_fn_type data holding a valid dct_influence_fn
1624!> \param pw_in pw_in (potential)
1625!> \param pw_out pw_out (density)
1626!> \par History
1627!> 07.2014 created [Hossein Bani-Hashemian]
1628!> 11.2015 revised [Hossein Bani-Hashemian]
1629!> \author Mohammad Hossein Bani-Hashemian
1630! **************************************************************************************************
1631 SUBROUTINE apply_laplace_operator_dct(pw_pool, green, pw_in, pw_out)
1632
1633 TYPE(pw_pool_type), INTENT(IN), POINTER :: pw_pool
1634 TYPE(greens_fn_type), INTENT(IN) :: green
1635 TYPE(pw_r3d_rs_type), INTENT(IN) :: pw_in
1636 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: pw_out
1637
1638 CHARACTER(LEN=*), PARAMETER :: routinen = 'apply_laplace_operator_dct'
1639
1640 INTEGER :: g0_index, handle, ig, ng
1641 LOGICAL :: have_g0
1642 REAL(dp) :: prefactor
1643 TYPE(pw_c1d_gs_type) :: pw_in_gs
1644 TYPE(pw_grid_type), POINTER :: pw_grid
1645
1646 CALL timeset(routinen, handle)
1647
1648! here I multiply by fourpi to cancel out the prefactor fourpi in influence_fn
1649 prefactor = fourpi
1650
1651 pw_grid => pw_pool%pw_grid
1652 ng = SIZE(pw_in%pw_grid%gsq)
1653 have_g0 = green%dct_influence_fn%pw_grid%have_g0
1654
1655 CALL pw_pool%create_pw(pw_in_gs)
1656
1657 CALL pw_transfer(pw_in, pw_in_gs)
1658
1659 IF (have_g0) THEN
1660 g0_index = green%dct_influence_fn%pw_grid%first_gne0 - 1
1661 pw_in_gs%array(g0_index) = 0.0_dp
1662 END IF
1663 DO ig = green%dct_influence_fn%pw_grid%first_gne0, ng
1664 pw_in_gs%array(ig) = prefactor*(pw_in_gs%array(ig)/green%dct_influence_fn%array(ig))
1665 END DO
1666
1667 CALL pw_transfer(pw_in_gs, pw_out)
1668
1669 CALL pw_pool%give_back_pw(pw_in_gs)
1670
1671 CALL timestop(handle)
1672
1673 END SUBROUTINE apply_laplace_operator_dct
1674
1675! **************************************************************************************************
1676!> \brief Evaluates the action of the generalized Poisson operator on a given 3d matrix.
1677!> \param pw_pool pool of pw grid
1678!> \param green green functions for FFT based inverse Laplacian
1679!> \param dielectric dielectric environment
1680!> \param v potential
1681!> \param density density
1682!> \par History
1683!> 07.2014 created [Hossein Bani-Hashemian]
1684!> \author Mohammad Hossein Bani-Hashemian
1685! **************************************************************************************************
1686 SUBROUTINE apply_poisson_operator_fft(pw_pool, green, dielectric, v, density)
1687
1688 TYPE(pw_pool_type), INTENT(IN), POINTER :: pw_pool
1689 TYPE(greens_fn_type), INTENT(IN) :: green
1690 TYPE(dielectric_type), INTENT(IN), POINTER :: dielectric
1691 TYPE(pw_r3d_rs_type), INTENT(IN) :: v
1692 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: density
1693
1694 CHARACTER(LEN=*), PARAMETER :: routinen = 'apply_poisson_operator_fft'
1695
1696 INTEGER :: handle
1697 TYPE(pw_r3d_rs_type) :: pxv
1698
1699 CALL timeset(routinen, handle)
1700
1701 CALL pw_pool%create_pw(pxv)
1702
1703 CALL apply_p_operator(pw_pool, dielectric, v, pxv)
1704 CALL apply_laplace_operator_fft(pw_pool, green, v, density)
1705 CALL pw_axpy(pxv, density)
1706
1707 CALL pw_pool%give_back_pw(pxv)
1708
1709 CALL timestop(handle)
1710
1711 END SUBROUTINE apply_poisson_operator_fft
1712
1713! **************************************************************************************************
1714!> \brief Evaluates the action of the generalized Poisson operator on a given
1715!> 3d matrix using DCT-I.
1716!> \param pw_pool pool of pw grid
1717!> \param green the greens_fn_type data holding a valid dct_influence_fn
1718!> \param dielectric dielectric environment
1719!> \param v potential
1720!> \param density density
1721!> \par History
1722!> 07.2014 created [Hossein Bani-Hashemian]
1723!> 11.2015 revised [Hossein Bani-Hashemian]
1724!> \author Mohammad Hossein Bani-Hashemian
1725! **************************************************************************************************
1726 SUBROUTINE apply_poisson_operator_dct(pw_pool, green, dielectric, v, density)
1727
1728 TYPE(pw_pool_type), INTENT(IN), POINTER :: pw_pool
1729 TYPE(greens_fn_type), INTENT(IN) :: green
1730 TYPE(dielectric_type), INTENT(IN), POINTER :: dielectric
1731 TYPE(pw_r3d_rs_type), INTENT(IN) :: v
1732 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: density
1733
1734 CHARACTER(LEN=*), PARAMETER :: routinen = 'apply_poisson_operator_dct'
1735
1736 INTEGER :: handle
1737 TYPE(pw_r3d_rs_type) :: pxv
1738
1739 CALL timeset(routinen, handle)
1740
1741 CALL pw_pool%create_pw(pxv)
1742
1743 CALL apply_p_operator(pw_pool, dielectric, v, pxv)
1744 CALL apply_laplace_operator_dct(pw_pool, green, v, density)
1745 CALL pw_axpy(pxv, density)
1746
1747 CALL pw_pool%give_back_pw(pxv)
1748
1749 CALL timestop(handle)
1750
1751 END SUBROUTINE apply_poisson_operator_dct
1752
1753! **************************************************************************************************
1754!> \brief Computes the extra contribution (v_eps)
1755!> v_eps = - \frac{1}{8*\pi} * |\nabla_r(v)|^2 * \frac{d \eps}{d \rho}
1756!> to the functional derivative of the Hartree energy wrt the density, being
1757!> attributed to the dependency of the dielectric constant to the charge density.
1758!> [see V. M. Sanchez, M. Sued, and D. A. Scherlis, J. Chem. Phys. 131, 174108 (2009)]
1759!> \param pw_pool pool of the original plane-wave grid
1760!> \param dielectric dielectric environment
1761!> \param v Hartree potential
1762!> \param v_eps v_eps
1763!> \par History
1764!> 08.2014 created [Hossein Bani-Hashemian]
1765!> \author Mohammad Hossein Bani-Hashemian
1766! **************************************************************************************************
1767 SUBROUTINE ps_implicit_compute_veps(pw_pool, dielectric, v, v_eps)
1768
1769 TYPE(pw_pool_type), INTENT(IN), POINTER :: pw_pool
1770 TYPE(dielectric_type), INTENT(IN), POINTER :: dielectric
1771 TYPE(pw_r3d_rs_type), INTENT(IN) :: v
1772 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: v_eps
1773
1774 CHARACTER(LEN=*), PARAMETER :: routinen = 'ps_implicit_compute_veps'
1775
1776 INTEGER :: handle, i
1777 REAL(dp) :: eightpi
1778 TYPE(pw_r3d_rs_type) :: dv2
1779 TYPE(pw_r3d_rs_type), DIMENSION(3) :: dv
1780
1781 CALL timeset(routinen, handle)
1782
1783 eightpi = 2*fourpi
1784
1785 CALL pw_pool%create_pw(dv2)
1786 DO i = 1, 3
1787 CALL pw_pool%create_pw(dv(i))
1788 END DO
1789
1790 CALL derive_fft(v, dv, pw_pool)
1791
1792! evaluate |\nabla_r(v)|^2
1793 dv2%array = dv(1)%array**2 + dv(2)%array**2 + dv(3)%array**2
1794
1795 v_eps%array = -(1.0_dp/eightpi)*(dv2%array*dielectric%deps_drho%array)
1796
1797 CALL pw_pool%give_back_pw(dv2)
1798 DO i = 1, 3
1799 CALL pw_pool%give_back_pw(dv(i))
1800 END DO
1801
1802 CALL timestop(handle)
1803
1804 END SUBROUTINE ps_implicit_compute_veps
1805
1806! **************************************************************************************************
1807!> \brief Computes the Hartree energy
1808!> \param density electronic density
1809!> \param v Hartree potential
1810!> \param ehartree Hartree energy
1811!> \par History
1812!> 06.2015 created [Hossein Bani-Hashemian]
1813!> \author Mohammad Hossein Bani-Hashemian
1814! **************************************************************************************************
1815 SUBROUTINE compute_ehartree_periodic_bc(density, v, ehartree)
1816
1817 TYPE(pw_r3d_rs_type), INTENT(IN) :: density, v
1818 REAL(dp), INTENT(OUT) :: ehartree
1819
1820 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_ehartree_periodic_bc'
1821
1822 INTEGER :: handle
1823
1824 CALL timeset(routinen, handle)
1825
1826! E_H = \frac{1}{2} * \int \rho * v dr
1827 ehartree = 0.5_dp*pw_integral_ab(density, v)
1828
1829 CALL timestop(handle)
1830
1831 END SUBROUTINE compute_ehartree_periodic_bc
1832
1833! **************************************************************************************************
1834!> \brief Computes the Hartree energy
1835!> \param dielectric dielectric environment
1836!> \param density electronic density
1837!> \param Btxlambda B^t * \lambda (\lambda is the vector of Lagrange multipliers
1838!> and B^t is the transpose of the boundary operator
1839!> \param v Hartree potential
1840!> \param ehartree Hartree energy
1841!> \param electric_enthalpy electric enthalpy
1842!> \par History
1843!> 06.2015 created [Hossein Bani-Hashemian]
1844!> \author Mohammad Hossein Bani-Hashemian
1845! **************************************************************************************************
1846 SUBROUTINE compute_ehartree_mixed_bc(dielectric, density, Btxlambda, v, ehartree, electric_enthalpy)
1847
1848 TYPE(dielectric_type), INTENT(IN), POINTER :: dielectric
1849 TYPE(pw_r3d_rs_type), INTENT(IN) :: density
1850 REAL(dp), ALLOCATABLE, DIMENSION(:, :, :), &
1851 INTENT(IN) :: btxlambda
1852 TYPE(pw_r3d_rs_type), INTENT(IN) :: v
1853 REAL(dp), INTENT(OUT) :: ehartree, electric_enthalpy
1854
1855 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_ehartree_mixed_bc'
1856
1857 INTEGER :: handle
1858 REAL(dp) :: dvol, ehartree_rho, ehartree_rho_cstr
1859 TYPE(pw_grid_type), POINTER :: pw_grid
1860
1861 CALL timeset(routinen, handle)
1862
1863 pw_grid => v%pw_grid
1864
1865 dvol = pw_grid%dvol
1866
1867! E_H = \frac{1}{2} * \int \rho * v dr + \frac{1}{8 \pi} * \int Btxlambda * v dr
1868! the sign of the second term depends on the sign chosen for the Lagrange multiplier
1869! term in the variational form
1870 ehartree_rho = accurate_sum(density%array*v%array)
1871 ehartree_rho_cstr = accurate_sum(dielectric%eps%array*btxlambda*v%array/fourpi)
1872 ehartree_rho = 0.5_dp*ehartree_rho*dvol
1873 ehartree_rho_cstr = 0.5_dp*ehartree_rho_cstr*dvol
1874 CALL pw_grid%para%group%sum(ehartree_rho)
1875 CALL pw_grid%para%group%sum(ehartree_rho_cstr)
1876 electric_enthalpy = ehartree_rho + ehartree_rho_cstr
1877 ehartree = ehartree_rho - ehartree_rho_cstr
1878
1879 CALL timestop(handle)
1880
1881 END SUBROUTINE compute_ehartree_mixed_bc
1882
1883! **************************************************************************************************
1884!> \brief Computes the (normalized) preconditioned residual norm error and the
1885!> normalized absolute error
1886!> \param pw_pool pool of the original plane-wave grid
1887!> \param green greens functions for FFT based inverse Laplacian
1888!> \param res_new residual
1889!> \param v_old old v
1890!> \param v_new new v
1891!> \param QAinvxres_new Delta^-1(res_new)
1892!> \param pres_error preconditioned residual norm error
1893!> \param nabs_error normalized absolute error
1894!> \par History
1895!> 07.2014 created [Hossein Bani-Hashemian]
1896!> \author Mohammad Hossein Bani-Hashemian
1897! **************************************************************************************************
1898 SUBROUTINE ps_implicit_compute_error_fft(pw_pool, green, res_new, v_old, v_new, &
1899 QAinvxres_new, pres_error, nabs_error)
1900
1901 TYPE(pw_pool_type), INTENT(IN), POINTER :: pw_pool
1902 TYPE(greens_fn_type), INTENT(IN) :: green
1903 TYPE(pw_r3d_rs_type), INTENT(IN) :: res_new, v_old, v_new
1904 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: qainvxres_new
1905 REAL(dp), INTENT(OUT) :: pres_error, nabs_error
1906
1907 CHARACTER(LEN=*), PARAMETER :: routinen = 'ps_implicit_compute_error_fft'
1908
1909 INTEGER :: handle
1910 REAL(dp) :: vol
1911
1912 CALL timeset(routinen, handle)
1913
1914 vol = pw_pool%pw_grid%vol
1915
1916! evaluate \Delta^-1(res) = \Delta^-1 (g - \Delta(v_new) - P(v_new) + Bt \lambda)
1917 CALL apply_inv_laplace_operator_fft(pw_pool, green, res_new, qainvxres_new)
1918! (normalized) preconditioned residual norm error :
1919 pres_error = accurate_sum(qainvxres_new%array(:, :, :)**2)
1920 CALL pw_pool%pw_grid%para%group%sum(pres_error)
1921 pres_error = sqrt(pres_error)/vol
1922
1923! normalized absolute error :
1924! nabs_error := \frac{\| v_old - v_new \|}{volume}
1925 nabs_error = accurate_sum(abs(v_old%array - v_new%array)**2)
1926 CALL pw_pool%pw_grid%para%group%sum(nabs_error)
1927 nabs_error = sqrt(nabs_error)/vol
1928
1929 CALL timestop(handle)
1930
1931 END SUBROUTINE ps_implicit_compute_error_fft
1932
1933! **************************************************************************************************
1934!> \brief Computes the (normalized) preconditioned residual norm error and the
1935!> normalized absolute error
1936!> \param pw_pool pool of the original plane-wave grid
1937!> \param green the greens_fn_type data holding a valid dct_influence_fn
1938!> \param res_new residual
1939!> \param v_old old v
1940!> \param v_new new v
1941!> \param QAinvxres_new Delta^-1(res_new)
1942!> \param pres_error preconditioned residual norm error
1943!> \param nabs_error normalized absolute error
1944!> \par History
1945!> 07.2014 created [Hossein Bani-Hashemian]
1946!> 11.2015 revised [Hossein Bani-Hashemian]
1947!> \author Mohammad Hossein Bani-Hashemian
1948! **************************************************************************************************
1949 SUBROUTINE ps_implicit_compute_error_dct(pw_pool, green, res_new, v_old, v_new, &
1950 QAinvxres_new, pres_error, nabs_error)
1951
1952 TYPE(pw_pool_type), INTENT(IN), POINTER :: pw_pool
1953 TYPE(greens_fn_type), INTENT(IN) :: green
1954 TYPE(pw_r3d_rs_type), INTENT(IN) :: res_new, v_old, v_new
1955 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: qainvxres_new
1956 REAL(dp), INTENT(OUT) :: pres_error, nabs_error
1957
1958 CHARACTER(LEN=*), PARAMETER :: routinen = 'ps_implicit_compute_error_dct'
1959
1960 INTEGER :: handle
1961 REAL(dp) :: vol
1962
1963 CALL timeset(routinen, handle)
1964
1965 vol = pw_pool%pw_grid%vol
1966
1967! evaluate \Delta^-1(res) = \Delta^-1 (g - \Delta(v_new) - P(v_new) + Bt \lambda)
1968 CALL apply_inv_laplace_operator_dct(pw_pool, green, res_new, qainvxres_new)
1969! (normalized) preconditioned residual norm error :
1970 pres_error = accurate_sum(qainvxres_new%array(:, :, :)**2)
1971 CALL pw_pool%pw_grid%para%group%sum(pres_error)
1972 pres_error = sqrt(pres_error)/vol
1973
1974! normalized absolute error :
1975! nabs_error := \frac{\| v_old - v_new \|}{volume}
1976 nabs_error = accurate_sum(abs(v_old%array - v_new%array)**2)
1977 CALL pw_pool%pw_grid%para%group%sum(nabs_error)
1978 nabs_error = sqrt(nabs_error)/vol
1979
1980 CALL timestop(handle)
1981
1982 END SUBROUTINE ps_implicit_compute_error_dct
1983
1984! **************************************************************************************************
1985!> \brief output of the implicit (iterative) Poisson solver
1986!> \param iter current iteration
1987!> \param pres_error preconditioned residual norm error
1988!> \param nabs_error normalized absolute error
1989!> \param outp_unit output unit
1990!> \par History
1991!> 07.2014 created [Hossein Bani-Hashemian]
1992!> \author Mohammad Hossein Bani-Hashemian
1993! **************************************************************************************************
1994 SUBROUTINE ps_implicit_output(iter, pres_error, nabs_error, outp_unit)
1995
1996 INTEGER, INTENT(IN) :: iter
1997 REAL(dp), INTENT(IN) :: pres_error, nabs_error
1998 INTEGER, INTENT(OUT) :: outp_unit
1999
2000 CHARACTER(LEN=*), PARAMETER :: routinen = 'ps_implicit_output'
2001 INTEGER, PARAMETER :: low_print_level = 1
2002
2003 INTEGER :: handle
2004 TYPE(cp_logger_type), POINTER :: logger
2005
2006 CALL timeset(routinen, handle)
2007
2008 logger => cp_get_default_logger()
2009 IF (logger%para_env%is_source()) THEN
2010 outp_unit = cp_logger_get_default_unit_nr(logger, local=.true.)
2011 ELSE
2012 outp_unit = -1
2013 END IF
2014
2015 IF (logger%iter_info%print_level > low_print_level) THEN
2016 IF ((outp_unit > 0) .AND. (iter == 1)) THEN
2017 WRITE (outp_unit, '(T3,A)') &
2018 "POISSON| iter pres error nabs error E_hartree delta E"
2019 END IF
2020
2021 IF (outp_unit > 0) THEN
2022 WRITE (outp_unit, '(T3,A,I6,5X,E13.4,3X,E13.4)', advance='NO') &
2023 "POISSON| ", iter, pres_error, nabs_error
2024 END IF
2025 END IF
2026
2027 CALL timestop(handle)
2028
2029 END SUBROUTINE ps_implicit_output
2030
2031! **************************************************************************************************
2032!> \brief reports the Hartree energy in every iteration
2033!> \param ps_implicit_env the implicit poisson solver environment
2034!> \param outp_unit output unit
2035!> \param ehartree Hartree energy
2036!> \par History
2037!> 07.2014 created [Hossein Bani-Hashemian]
2038!> \author Mohammad Hossein Bani-Hashemian
2039! **************************************************************************************************
2040 SUBROUTINE ps_implicit_report_ehartree(ps_implicit_env, outp_unit, ehartree)
2041
2042 TYPE(ps_implicit_type) :: ps_implicit_env
2043 INTEGER, INTENT(IN) :: outp_unit
2044 REAL(dp), INTENT(IN) :: ehartree
2045
2046 CHARACTER(LEN=*), PARAMETER :: routinen = 'ps_implicit_report_ehartree'
2047 INTEGER, PARAMETER :: low_print_level = 1
2048
2049 INTEGER :: handle
2050 TYPE(cp_logger_type), POINTER :: logger
2051
2052 logger => cp_get_default_logger()
2053 CALL timeset(routinen, handle)
2054 IF (logger%iter_info%print_level > low_print_level) THEN
2055 IF (outp_unit > 0) WRITE (outp_unit, '(F19.10,E10.2)') &
2056 ehartree, ehartree - ps_implicit_env%ehartree
2057 END IF
2058 CALL timestop(handle)
2059
2060 END SUBROUTINE ps_implicit_report_ehartree
2061
2062! **************************************************************************************************
2063!> \brief reports the final number of iteration
2064!> \param iter the iteration number after exiting the main loop
2065!> \param max_iter maximum number of iterations
2066!> \param outp_unit output unit
2067!> \par History
2068!> 02.2016 created [Hossein Bani-Hashemian]
2069!> \author Mohammad Hossein Bani-Hashemian
2070! **************************************************************************************************
2071 SUBROUTINE ps_implicit_print_convergence_msg(iter, max_iter, outp_unit)
2072
2073 INTEGER, INTENT(IN) :: iter, max_iter, outp_unit
2074
2075 CHARACTER(LEN=*), PARAMETER :: routinen = 'ps_implicit_print_convergence_msg'
2076
2077 CHARACTER(LEN=12) :: msg
2078 INTEGER :: handle, last_iter
2079
2080 CALL timeset(routinen, handle)
2081
2082 last_iter = iter - 1
2083
2084 IF (outp_unit > 0) THEN
2085 IF (last_iter == max_iter) THEN
2086 WRITE (outp_unit, '(T3,A)') &
2087 "POISSON| No convergence achieved within the maximum number of iterations."
2088 END IF
2089 IF (last_iter < max_iter) THEN
2090 IF (last_iter == 1) THEN
2091 msg = " iteration."
2092 ELSE
2093 msg = " iterations."
2094 END IF
2095 WRITE (outp_unit, '(T3,A,I0,A)') &
2096 "POISSON| Poisson solver converged in ", last_iter, msg
2097 END IF
2098 END IF
2099 CALL timestop(handle)
2100
2101 END SUBROUTINE ps_implicit_print_convergence_msg
2102
2103! **************************************************************************************************
2104!> \brief converts a 1D array to a 3D array (contiguous layout)
2105!> \param idx_1dto3d mapping of indices
2106!> \param arr1d input 1D array
2107!> \param arr3d input 3D array
2108! **************************************************************************************************
2109 SUBROUTINE convert_1dto3d(idx_1dto3d, arr1d, arr3d)
2110
2111 INTEGER, DIMENSION(:), INTENT(IN) :: idx_1dto3d
2112 REAL(dp), DIMENSION(:), INTENT(IN) :: arr1d
2113 REAL(dp), ALLOCATABLE, DIMENSION(:, :, :), &
2114 INTENT(INOUT) :: arr3d
2115
2116 CHARACTER(LEN=*), PARAMETER :: routinen = 'convert_1dto3d'
2117
2118 INTEGER :: handle, i, j, k, l, lb1, lb2, lb3, &
2119 npts1, npts2, npts3, ub1, ub2, ub3
2120
2121 CALL timeset(routinen, handle)
2122
2123 lb1 = lbound(arr3d, 1); ub1 = ubound(arr3d, 1)
2124 lb2 = lbound(arr3d, 2); ub2 = ubound(arr3d, 2)
2125 lb3 = lbound(arr3d, 3); ub3 = ubound(arr3d, 3)
2126
2127 npts1 = ub1 - lb1 + 1
2128 npts2 = ub2 - lb2 + 1
2129 npts3 = ub3 - lb3 + 1
2130
2131 DO l = 1, SIZE(idx_1dto3d)
2132 k = ((idx_1dto3d(l) - 1)/(npts1*npts2)) + lb3
2133 j = ((idx_1dto3d(l) - 1) - (k - lb3)*npts1*npts2)/npts1 + lb2
2134 i = idx_1dto3d(l) - ((j - lb2)*npts1 + (k - lb3)*npts1*npts2) + lb1 - 1
2135 arr3d(i, j, k) = arr1d(l)
2136 END DO
2137
2138 CALL timestop(handle)
2139
2140 END SUBROUTINE convert_1dto3d
2141
2142! **************************************************************************************************
2143!> \brief Returns the voltage of a tile. In case an alternating field is used, the oltage is a function of time
2144!> \param time ...
2145!> \param v_D ...
2146!> \param osc_frac ...
2147!> \param frequency ...
2148!> \param phase ...
2149!> \param v_D_new ...
2150! **************************************************************************************************
2151 SUBROUTINE get_voltage(time, v_D, osc_frac, frequency, phase, v_D_new)
2152
2153 REAL(dp), INTENT(IN) :: time
2154 REAL(dp), DIMENSION(:), INTENT(IN) :: v_d, osc_frac, frequency, phase
2155 REAL(dp), ALLOCATABLE, DIMENSION(:), INTENT(OUT) :: v_d_new
2156
2157 CHARACTER(LEN=*), PARAMETER :: routinen = 'get_voltage'
2158
2159 INTEGER :: handle, i
2160
2161 CALL timeset(routinen, handle)
2162
2163 ALLOCATE (v_d_new(SIZE(v_d)))
2164
2165 DO i = 1, SIZE(v_d)
2166 v_d_new(i) = v_d(i)*(1 - osc_frac(i)) + &
2167 v_d(i)*osc_frac(i)*cos(2*pi*time*frequency(i) + phase(i))
2168 END DO
2169
2170 CALL timestop(handle)
2171
2172 END SUBROUTINE get_voltage
2173
2174END MODULE ps_implicit_methods
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.
collects all references to literature in CP2K as new algorithms / method are included from literature...
integer, save, public banihashemian2016
various routines to log and control the output. The idea is that decisions about where to log should ...
recursive integer function, public cp_logger_get_default_unit_nr(logger, local, skip_not_ionode)
asks the default unit number of the given logger. try to use cp_logger_get_unit_nr
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
the type I Discrete Cosine Transform (DCT-I)
Definition dct.F:16
integer, parameter, public neumannx
Definition dct.F:63
integer, parameter, public neumannxy
Definition dct.F:63
subroutine, public dct_type_init(pw_grid, neumann_directions, dct_env)
Initializes a dct_type.
Definition dct.F:83
integer, parameter, public neumannxz
Definition dct.F:63
subroutine, public pw_shrink(neumann_directions, dests_shrink, srcs_shrink, bounds_local_shftd, pw_in, pw_shrinked)
shrinks an evenly symmetric pw_r3d_rs_type data to a pw_r3d_rs_type data that is 8 times smaller (the...
Definition dct.F:700
integer, parameter, public neumannxyz
Definition dct.F:63
integer, parameter, public neumannz
Definition dct.F:63
integer, parameter, public neumannyz
Definition dct.F:63
integer, parameter, public neumanny
Definition dct.F:63
subroutine, public pw_expand(neumann_directions, recv_msgs_bnds, dests_expand, srcs_expand, flipg_stat, bounds_shftd, pw_in, pw_expanded)
expands a pw_r3d_rs_type data to an evenly symmetric pw_r3d_rs_type data that is 8 times larger than ...
Definition dct.F:516
methods for evaluating the dielectric constant
subroutine, public dielectric_create(dielectric, pw_pool, dielectric_params)
allocates memory for a dielectric data type
subroutine, public derive_fft(f, df, pw_pool)
computes the derivative of a function using FFT
dielectric constant data type
subroutines for defining and creating Dirichlet type subdomains
subroutine, public dirichlet_boundary_region_setup(pw_pool, poisson_params, dbcs)
Sets up the Dirichlet boundary condition.
Dirichlet boundary condition data types.
subroutine, public dbc_tile_release(dbc, pw_pool)
releases tiles
sums arrays of real/complex numbers with much reduced round-off as compared to a naive implementation...
Definition kahan_sum.F:29
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public int_8
Definition kinds.F:54
integer, parameter, public dp
Definition kinds.F:34
Definition of mathematical constants and functions.
real(kind=dp), parameter, public pi
real(kind=dp), parameter, public fourpi
The implicit (generalized) Poisson solver.
subroutine, public implicit_poisson_solver_periodic(poisson_env, density, v_new, ehartree)
implicit Poisson solver for periodic boundary conditions
subroutine, public implicit_poisson_solver_mixed(poisson_env, density, v_new, electric_enthalpy)
implicit Poisson solver for mixed boundary conditions (Neumann + Dirichlet)
subroutine, public implicit_poisson_solver_neumann(poisson_env, density, v_new, ehartree)
implicit Poisson solver: zero-average solution of the Poisson equation subject to homogeneous Neumann...
subroutine, public implicit_poisson_solver_mixed_periodic(poisson_env, density, v_new, electric_enthalpy)
implicit Poisson solver for mixed-periodic boundary conditions (periodic + Dirichlet)
subroutine, public ps_implicit_create(pw_pool, poisson_params, dct_pw_grid, green, ps_implicit_env)
Creates implicit Poisson solver environment.
Types containing essential information for running implicit (iterative) Poisson solver.
integer, parameter, public neumann_bc
integer, parameter, public mixed_bc
integer, parameter, public mixed_periodic_bc
integer, parameter, public periodic_bc
functions related to the poisson solver on regular grids
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
subroutine, public pw_pool_release(pool)
releases the given pool (see cp2k/doc/ReferenceCounting.html)
subroutine, public pw_pool_create(pool, pw_grid, max_cache)
creates a pool for pw
type of a logger, at the moment it contains just a print level starting at which level it should be l...
contains all the informations needed by the fft based poisson solvers
parameters for the poisson solver independet of input_section
environment for the poisson solver
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...