Octopus
propagator_rk.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2006 M. Marques, A. Castro, A. Rubio, G. Bertsch
2!!
3!! This program is free software; you can redistribute it and/or modify
4!! it under the terms of the GNU General Public License as published by
5!! the Free Software Foundation; either version 2, or (at your option)
6!! any later version.
7!!
8!! This program is distributed in the hope that it will be useful,
9!! but WITHOUT ANY WARRANTY; without even the implied warranty of
10!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11!! GNU General Public License for more details.
12!!
13!! You should have received a copy of the GNU General Public License
14!! along with this program; if not, write to the Free Software
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17!!
18
19#include "global.h"
20
23 use comm_oct_m
24 use debug_oct_m
28 use forces_oct_m
30 use grid_oct_m
31 use global_oct_m
35 use ions_oct_m
36 use, intrinsic :: iso_fortran_env
39 use lda_u_oct_m
40 use mesh_oct_m
43 use mpi_oct_m
47 use parser_oct_m
53 use space_oct_m
56 use v_ks_oct_m
58 use xc_oct_m
59
60 implicit none
61
62 private
63
64 public :: &
69
70 class(mesh_t), pointer :: mesh_p
71 type(hamiltonian_elec_t), pointer :: hm_p
72 type(states_elec_t), pointer :: st_p
73 type(propagator_base_t), pointer :: tr_p
74 type(namespace_t), pointer :: namespace_p
75 type(electron_space_t), pointer :: space_p
76 type(partner_list_t), pointer :: ext_partners_p
77 integer :: dim_op
78 real(real64) :: t_op, dt_op
79 real(real64), allocatable :: vpsl1_op(:), vpsl2_op(:)
80 logical :: move_ions_op
81 type(xc_copied_potentials_t) :: vhxc1_op, vhxc2_op
82
83contains
84
85 subroutine td_explicit_runge_kutta4(ks, namespace, space, hm, gr, st, time, dt, ions_dyn, ions, ext_partners, qcchi)
86 type(v_ks_t), target, intent(inout) :: ks
87 type(namespace_t), intent(in) :: namespace
88 type(electron_space_t), intent(in) :: space
89 type(hamiltonian_elec_t), target, intent(inout) :: hm
90 type(grid_t), target, intent(in) :: gr
91 type(states_elec_t), target, intent(inout) :: st
92 real(real64), intent(in) :: time
93 real(real64), intent(in) :: dt
94 type(ion_dynamics_t), intent(inout) :: ions_dyn
95 type(ions_t), intent(inout) :: ions
96 type(partner_list_t), intent(in) :: ext_partners
97 type(opt_control_state_t), optional, target, intent(inout) :: qcchi
98
99 type(states_elec_t), pointer :: chi
100 real(real64), pointer :: q(:, :), p(:, :)
101
102 integer :: np_part, np, kp1, kp2, st1, st2, nspin, ik, ist, iatom, ib
103 complex(real64), allocatable :: zphi(:, :, :, :), zchi(:, :, :, :), dvpsi(:, :, :)
104 type(states_elec_t) :: hst, stphi, inh, hchi, stchi
105 logical :: propagate_chi
106 real(real64), allocatable :: pos0(:, :), vel0(:, :), &
107 posk(:, :), velk(:, :), &
108 pos(:, :), vel(:, :), &
109 posfinal(:, :), velfinal(:, :), &
110 pos0t(:, :), vel0t(:, :), &
111 poskt(:, :), velkt(:, :), &
112 post(:, :), velt(:, :), &
113 posfinalt(:, :), velfinalt(:, :), &
114 coforce(:, :)
115
117
118 if(ions_dyn%cell_relax()) then
119 call messages_not_implemented("CellDynamics=yes with explicit RK4 propagator")
120 end if
121
122 propagate_chi = present(qcchi)
123 if (propagate_chi) then
124 chi => opt_control_point_qs(qcchi)
125 q => opt_control_point_q(qcchi)
126 p => opt_control_point_p(qcchi)
127 end if
128
129 st1 = st%st_start
130 st2 = st%st_end
131 kp1 = st%d%kpt%start
132 kp2 = st%d%kpt%end
133 np_part = gr%np_part
134 np = gr%np
135 nspin = hm%d%nspin
136
137 safe_allocate(zphi(1:np_part, 1:st%d%dim, st1:st2, kp1:kp2))
138 if (propagate_chi) then
139 safe_allocate(zchi(1:np_part, 1:st%d%dim, st1:st2, kp1:kp2))
140 end if
141 if (ions_dyn%ions_move()) then
142 safe_allocate(pos(1:ions%space%dim, 1:ions%natoms))
143 safe_allocate(vel(1:ions%space%dim, 1:ions%natoms))
144 safe_allocate(pos0(1:ions%space%dim, 1:ions%natoms))
145 safe_allocate(vel0(1:ions%space%dim, 1:ions%natoms))
146 safe_allocate(posk(1:ions%space%dim, 1:ions%natoms))
147 safe_allocate(velk(1:ions%space%dim, 1:ions%natoms))
148 safe_allocate(posfinal(1:ions%space%dim, 1:ions%natoms))
149 safe_allocate(velfinal(1:ions%space%dim, 1:ions%natoms))
150
151 if (propagate_chi) then
152 safe_allocate(post(1:ions%space%dim, 1:ions%natoms))
153 safe_allocate(velt(1:ions%space%dim, 1:ions%natoms))
154 safe_allocate(pos0t(1:ions%space%dim, 1:ions%natoms))
155 safe_allocate(vel0t(1:ions%space%dim, 1:ions%natoms))
156 safe_allocate(poskt(1:ions%space%dim, 1:ions%natoms))
157 safe_allocate(velkt(1:ions%space%dim, 1:ions%natoms))
158 safe_allocate(posfinalt(1:ions%space%dim, 1:ions%natoms))
159 safe_allocate(velfinalt(1:ions%space%dim, 1:ions%natoms))
160 safe_allocate(coforce(1:ions%natoms, 1:ions%space%dim))
161 end if
162 end if
164 call states_elec_copy(hst, st)
165 call states_elec_copy(stphi, st)
166 call states_elec_get_state(st, gr, zphi)
168 if (propagate_chi) then
169 call states_elec_copy(hchi, chi)
170 call states_elec_copy(stchi, chi)
171 call states_elec_get_state(chi, gr, zchi)
172 end if
174 if (ions_dyn%ions_move()) then
175 pos0 = ions%pos
176 vel0 = ions%vel
177 posfinal = pos0
178 velfinal = vel0
179
180 if (propagate_chi) then
181 do iatom = 1, ions%natoms
182 pos0t(1:ions%space%dim, iatom) = q(iatom, 1:ions%space%dim)
183 vel0t(1:ions%space%dim, iatom) = p(iatom, 1:ions%space%dim) / ions%mass(iatom)
184 end do
185 posfinalt = pos0t
186 velfinalt = vel0t
187 end if
188 end if
189
190!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
191 ! Stage 1.
192 !
193
194 !
195 call states_elec_set_state(stphi, gr, zphi)
196 if (propagate_chi) then
197 call states_elec_set_state(stchi, gr, zchi)
198 end if
199 if (ions_dyn%ions_move()) then
200 pos = pos0
201 vel = vel0
202 if (propagate_chi) then
203 post = pos0t
204 velt = vel0t
205 end if
206 end if
207
208 call f_psi(time - dt)
209 if (propagate_chi) call f_chi(time - dt)
210 if (ions_dyn%ions_move()) call f_ions(time - dt)
211
212 call update_state(m_one/6.0_real64)
213
214!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
215 ! Stage 2.
216
217 call states_elec_set_state(stphi, gr, zphi)
218 if (propagate_chi) then
219 call states_elec_set_state(stchi, gr, zchi)
220 end if
221
222 do ik = stphi%d%kpt%start, stphi%d%kpt%end
223 do ib = stphi%group%block_start, stphi%group%block_end
224 call batch_axpy(gr%np, -m_half*m_zi*dt, hst%group%psib(ib, ik), stphi%group%psib(ib, ik))
225 if (propagate_chi) then
226 call batch_axpy(gr%np, -m_half*m_zi*dt, hchi%group%psib(ib, ik), stchi%group%psib(ib, ik))
227 end if
228 end do
229 end do
230
231 if (ions_dyn%ions_move()) then
232 pos = pos0 + m_half * posk
233 vel = vel0 + m_half * velk
234 if (propagate_chi) then
235 post = pos0t + m_half * poskt
236 velt = vel0t + m_half * velkt
237 end if
238 end if
239
240 call f_psi(time-m_half*dt)
241 if (propagate_chi) call f_chi(time-m_half*dt)
242 if (ions_dyn%ions_move()) call f_ions(time-m_half*dt)
244
245!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
246 ! Stage 3.
247
248 call states_elec_set_state(stphi, gr, zphi)
249 if (propagate_chi) then
250 call states_elec_set_state(stchi, gr, zchi)
251 end if
252
253 do ik = stphi%d%kpt%start, stphi%d%kpt%end
254 do ib = stphi%group%block_start, stphi%group%block_end
255 call batch_axpy(gr%np, -m_half*m_zi*dt, hst%group%psib(ib, ik), stphi%group%psib(ib, ik))
256 if (propagate_chi) then
257 call batch_axpy(gr%np, -m_half*m_zi*dt, hchi%group%psib(ib, ik), stchi%group%psib(ib, ik))
258 end if
259 end do
260 end do
261
262 if (ions_dyn%ions_move()) then
263 pos = pos0 + m_half * posk
264 vel = vel0 + m_half * velk
265 if (propagate_chi) then
266 post = pos0t + m_half * poskt
267 velt = vel0t + m_half * velkt
268 end if
269 end if
270
271 call f_psi(time-m_half*dt)
272 if (propagate_chi) call f_chi(time-m_half*dt)
273 if (ions_dyn%ions_move()) call f_ions(time-m_half*dt)
274
276
277!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
278 ! Stage 4.
279
280 call states_elec_set_state(stphi, gr, zphi)
281 if (propagate_chi) then
282 call states_elec_set_state(stchi, gr, zchi)
283 end if
284
285 do ik = stphi%d%kpt%start, stphi%d%kpt%end
286 do ib = stphi%group%block_start, stphi%group%block_end
287 call batch_axpy(gr%np, -m_zi*dt, hst%group%psib(ib, ik), stphi%group%psib(ib, ik))
288 if (propagate_chi) then
289 call batch_axpy(gr%np, -m_zi*dt, hchi%group%psib(ib, ik), stchi%group%psib(ib, ik))
290 end if
291 end do
292 end do
293
294 if (ions_dyn%ions_move()) then
295 pos = pos0 + posk
296 vel = vel0 + velk
297 if (propagate_chi) then
298 post = pos0t + poskt
299 velt = vel0t + velkt
300 end if
301 end if
302
303 call f_psi(time)
304 if (propagate_chi) call f_chi(time)
305 if (ions_dyn%ions_move()) call f_ions(time)
306
307 call update_state(m_one / 6.0_real64)
308
309!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
310 ! Collect the results.
311
312 call density_calc(st, gr, st%rho)
313 if (ions_dyn%ions_move()) then
314 ions%pos = posfinal
315 ions%vel = velfinal
316 call hamiltonian_elec_epot_generate(hm, namespace, space, gr, ions, ext_partners, st, time)
317 !call forces_calculate(gr, namespace, ions, hm, stphi, time, dt)
318 call ions%update_kinetic_energy()
319
320 if (propagate_chi) then
321 do iatom = 1, ions%natoms
322 q(iatom, 1:ions%space%dim) = posfinalt(1:ions%space%dim, iatom)
323 p(iatom, 1:ions%space%dim) = ions%mass(iatom) * velfinalt(1:ions%space%dim, iatom)
324 end do
325 end if
326 end if
327
328 call states_elec_end(hst)
329 call states_elec_end(stphi)
330 safe_deallocate_a(zphi)
331 if (propagate_chi) then
332 call states_elec_end(hchi)
333 call states_elec_end(stchi)
334 safe_deallocate_a(zchi)
335 nullify(chi)
336 nullify(p)
337 nullify(q)
338 end if
339
340 if (ions_dyn%ions_move()) then
341 safe_deallocate_a(pos)
342 safe_deallocate_a(vel)
343 safe_deallocate_a(pos0)
344 safe_deallocate_a(vel0)
345 safe_deallocate_a(posk)
346 safe_deallocate_a(velk)
347 safe_deallocate_a(posfinal)
348 safe_deallocate_a(velfinal)
349
350 if (propagate_chi) then
351 safe_deallocate_a(post)
352 safe_deallocate_a(velt)
353 safe_deallocate_a(pos0t)
354 safe_deallocate_a(vel0t)
355 safe_deallocate_a(poskt)
356 safe_deallocate_a(velkt)
357 safe_deallocate_a(posfinalt)
358 safe_deallocate_a(velfinalt)
359 safe_deallocate_a(coforce)
360 end if
361 end if
363
364 contains
365
366 subroutine f_psi(tau)
367 real(real64), intent(in) :: tau
368
369 if (ions_dyn%ions_move()) then
370 ions%pos = pos
371 ions%vel = vel
372 call hamiltonian_elec_epot_generate(hm, namespace, space, gr, ions, ext_partners, stphi, time = tau)
373 end if
374 if (.not. oct_exchange_enabled(hm%oct_exchange)) then
375 call density_calc(stphi, gr, stphi%rho)
376 call v_ks_calc(ks, namespace, space, hm, stphi, ions, ext_partners, &
377 calc_current = list_has_gauge_field(ext_partners), &
378 time = tau, calc_energy = .false., calc_eigenval = .false.)
379 else
380 call hm%update(gr, namespace, space, ext_partners, time = tau)
381 end if
382 call lda_u_update_occ_matrices(hm%lda_u, namespace, gr, st, hm%hm_base, hm%phase, hm%energy)
383 call zhamiltonian_elec_apply_all(hm, namespace, gr, stphi, hst)
384 end subroutine f_psi
385
386 subroutine f_ions(tau)
387 real(real64), intent(in) :: tau
388
389 call forces_calculate(gr, namespace, ions, hm, ext_partners, stphi, ks, t = tau, dt = dt)
390 do iatom = 1, ions%natoms
391 posk(:, iatom) = dt * vel(:, iatom)
392 velk(:, iatom) = dt * ions%tot_force(:, iatom) / ions%mass(iatom)
393 end do
394 if (propagate_chi) then
395 call forces_costate_calculate(gr, namespace, ions, hm, stphi, stchi, coforce, transpose(post))
396 do iatom = 1, ions%natoms
397 poskt(:, iatom) = dt * velt(:, iatom)
398 velkt(:, iatom) = dt * coforce(iatom, :) / ions%mass(iatom)
399 end do
400 end if
401 end subroutine f_ions
402
403 subroutine f_chi(tau)
404 real(real64), intent(in) :: tau
405
406 if (hm%theory_level /= independent_particles) call oct_exchange_set(hm%oct_exchange, stphi, gr)
407 call prepare_inh()
409
410 call propagation_ops_elec_update_hamiltonian(namespace, space, st, gr, hm, ext_partners, tau)
411
412 call zhamiltonian_elec_apply_all(hm, namespace, gr, stchi, hchi)
414
415
416 call apply_inh()
417 if (hm%theory_level /= independent_particles) call oct_exchange_remove(hm%oct_exchange)
418 if (ions_dyn%ions_move()) call hamiltonian_elec_remove_inh(hm)
419 end subroutine f_chi
420
421 subroutine apply_inh()
422 integer :: ib
423
424 if (hamiltonian_elec_inh_term(hm)) then
425 do ik = kp1, kp2
426 do ib = 1, st%group%block_start, st%group%block_end
427 call batch_axpy(np, m_zi, hm%inh_st%group%psib(ib, ik), hchi%group%psib(ib, ik))
428 end do
429 end do
430 end if
431 end subroutine apply_inh
432
433 subroutine prepare_inh()
434 integer :: idir
435 complex(real64), allocatable :: psi(:, :), inhpsi(:, :)
436 type(perturbation_ionic_t), pointer :: pert
437
438 if (ions_dyn%ions_move()) then
439 call states_elec_copy(inh, st)
440
441 safe_allocate(psi(1:gr%np_part, 1:st%d%dim))
442 safe_allocate(inhpsi(1:gr%np_part, 1:st%d%dim))
443 safe_allocate(dvpsi(1:gr%np_part, 1:st%d%dim, 1:space%dim))
444
445 do ik = 1, st%nik
446 do ist = 1, st%nst
447
448 inhpsi = m_z0
449 call states_elec_get_state(stphi, gr, ist, ik, psi)
450
451 do iatom = 1, ions%natoms
452 do idir = 1, space%dim
453 pert => perturbation_ionic_t(namespace, ions)
454 call pert%setup_atom(iatom)
455 call pert%setup_dir(idir)
456 call pert%zapply(namespace, space, gr, hm, ik, psi(:, :), dvpsi(:, :, idir))
457 dvpsi(:, :, idir) = - dvpsi(:, :, idir)
458 inhpsi(1:gr%np, 1:stphi%d%dim) = inhpsi(1:gr%np, 1:stphi%d%dim) &
459 + st%occ(ist, ik)*post(idir, iatom)*dvpsi(1:gr%np, 1:stphi%d%dim, idir)
460 safe_deallocate_p(pert)
461 end do
462 end do
463
464 call states_elec_set_state(inh, gr, ist, ik, inhpsi)
465
466 end do
467 end do
468
469 call hamiltonian_elec_set_inh(hm, inh)
470 call states_elec_end(inh)
471
472 safe_deallocate_a(psi)
473 safe_deallocate_a(inhpsi)
474 safe_deallocate_a(dvpsi)
475
476 end if
477 end subroutine prepare_inh
478
479 subroutine update_state(epsilon)
480 real(real64), intent(in) :: epsilon
481
482 do ik = stphi%d%kpt%start, stphi%d%kpt%end
483 do ib = stphi%group%block_start, stphi%group%block_end
484 call batch_axpy(gr%np, -m_zi*dt*epsilon, hst%group%psib(ib, ik), st%group%psib(ib, ik))
485 if (propagate_chi) then
486 call batch_axpy(gr%np, -m_zi*dt*epsilon, hchi%group%psib(ib, ik), chi%group%psib(ib, ik))
487 end if
488 end do
489 end do
490
491 if (ions_dyn%ions_move()) then
492 posfinal = posfinal + posk * epsilon
493 velfinal = velfinal + velk * epsilon
494 if (propagate_chi) then
495 posfinalt = posfinalt + poskt * epsilon
496 velfinalt = velfinalt + velkt * epsilon
497 end if
498 end if
499 end subroutine update_state
500
501 end subroutine td_explicit_runge_kutta4
502
503
504 subroutine td_runge_kutta2(ks, namespace, space, hm, gr, st, tr, time, dt, ions_dyn, ions, ext_partners)
505 type(v_ks_t), target, intent(inout) :: ks
506 type(namespace_t), target, intent(in) :: namespace
507 type(electron_space_t), target, intent(in) :: space
508 type(hamiltonian_elec_t), target, intent(inout) :: hm
509 type(grid_t), target, intent(in) :: gr
510 type(states_elec_t), target, intent(inout) :: st
511 type(propagator_base_t), target, intent(inout) :: tr
512 real(real64), intent(in) :: time
513 real(real64), intent(in) :: dt
514 type(ion_dynamics_t), intent(inout) :: ions_dyn
515 type(ions_t), intent(inout) :: ions
516 type(partner_list_t), target, intent(in) :: ext_partners
517
518 integer :: np_part, np, kp1, kp2, st1, st2, nspin, ik, ist, idim, j, ip
519 integer :: i
520 real(real64) :: dres
521 complex(real64), allocatable :: zphi(:, :, :, :)
522 complex(real64), allocatable :: zpsi(:), rhs(:)
523 complex(real64), allocatable :: k2(:, :, :, :), oldk2(:, :, :, :), rhs1(:, :, :, :)
524 complex(real64), allocatable :: inhpsi(:)
525 type(ion_state_t) :: ions_state
527 push_sub(td_runge_kutta2)
528
529 if(ions_dyn%cell_relax()) then
530 call messages_not_implemented("CellDynamics=yes with RK2 propagator")
531 end if
532
533
534 st1 = st%st_start
535 st2 = st%st_end
536 kp1 = st%d%kpt%start
537 kp2 = st%d%kpt%end
538 np_part = gr%np_part
539 np = gr%np
540 nspin = hm%d%nspin
541 move_ions_op = ions_dyn%ions_move()
542
543 sp_np = np
544 sp_dim = st%d%dim
545 sp_st1 = st1
546 sp_st2 = st2
547 sp_kp1 = kp1
548 sp_kp2 = kp2
549 sp_parallel = st%parallel_in_states .or. st%d%kpt%parallel
550 if (sp_parallel) call mpi_grp_copy(sp_grp, st%st_kpt_mpi_grp)
551
552 ! define pointer and variables for usage in td_rk2op, td_rk2opt routines
553 mesh_p => gr
554 hm_p => hm
555 tr_p => tr
556 st_p => st
557 namespace_p => namespace
558 space_p => space
559 ext_partners_p => ext_partners
560 dt_op = dt
561 t_op = time - dt/m_two
562 dim_op = st%d%dim
563
564 safe_allocate(k2(1:np_part, 1:st%d%dim, st1:st2, kp1:kp2))
565 safe_allocate(oldk2(1:np_part, 1:st%d%dim, st1:st2, kp1:kp2))
566 safe_allocate(zphi(1:gr%np_part, st%d%dim, st1:st2, kp1:kp2))
567 safe_allocate(rhs1(1:np_part, 1:st%d%dim, st1:st2, kp1:kp2))
568 safe_allocate(rhs(1:tr%tdsk_size))
569 safe_allocate(zpsi(1:tr%tdsk_size))
570 safe_allocate(vpsl1_op(1:np))
571
572 ! First, we get the state that we want to propagate. For the moment being, only one state.
573 do ik = kp1, kp2
574 do ist = st1, st2
575 call states_elec_get_state(st, gr, ist, ik, zphi(:, :, ist, ik))
576 end do
577 end do
578
579 if (oct_exchange_enabled(hm%oct_exchange)) then
580 call oct_exchange_prepare(hm%oct_exchange, gr, zphi, hm%xc, hm%psolver, namespace)
581 end if
582
583 call propagation_ops_elec_update_hamiltonian(namespace, space, st, gr, hm, ext_partners, time - dt)
584
585 rhs1 = m_z0
586 do ik = kp1, kp2
587 do ist = st1, st2
588 call zhamiltonian_elec_apply_single(hm_p, namespace, mesh_p, zphi(:, :, ist, ik), rhs1(:, :, ist, ik), ist, ik)
589 end do
590 end do
591 do ik = kp1, kp2
592 do ist = st1, st2
593 if (oct_exchange_enabled(hm%oct_exchange)) then
594 call oct_exchange_operator(hm%oct_exchange, namespace, gr, rhs1(:, :, ist, ik), ist, ik)
595 end if
596 end do
597 end do
598
599 rhs1 = zphi - m_zi * m_half * dt * rhs1
600
601 if (hamiltonian_elec_inh_term(hm)) then
602 safe_allocate(inhpsi(1:gr%np))
603 do ik = kp1, kp2
604 do ist = st1, st2
605 do idim = 1, st%d%dim
606 call states_elec_get_state(hm%inh_st, gr, idim, ist, ik, inhpsi)
607 do ip = 1, gr%np
608 rhs1(ip, idim, ist, ik) = rhs1(ip, idim, ist, ik) + dt * inhpsi(ip)
609 end do
610 end do
611 end do
612 end do
613 safe_deallocate_a(inhpsi)
614 end if
615
616 k2 = zphi
617
618 i = 1
619 do
620 oldk2 = k2
621
622 ! Set the Hamiltonian at the final time of the propagation
623 if (.not. oct_exchange_enabled(hm_p%oct_exchange)) then
624 do ik = kp1, kp2
625 do ist = st1, st2
626 call states_elec_set_state(st, gr, ist, ik, k2(:, :, ist, ik))
627 end do
628 end do
629 call density_calc(st, gr, st%rho)
630 call v_ks_calc(ks, namespace, space, hm, st, ions, ext_partners, &
631 calc_current = list_has_gauge_field(ext_partners),&
632 calc_energy = .false., calc_eigenval = .false.)
633 end if
634 if (ions_dyn%ions_move()) then
635 call ion_dynamics_save_state(ions_dyn, ions, ions_state)
636 call ion_dynamics_propagate(ions_dyn, ions, time, dt, namespace)
637 call hamiltonian_elec_epot_generate(hm, namespace, space, gr, ions, ext_partners, st, time = time)
638 vpsl1_op = hm%ep%vpsl
639 end if
640
641 call propagation_ops_elec_update_hamiltonian(namespace, space, st, gr, hm, ext_partners, time)
642
643 if (.not. oct_exchange_enabled(hm_p%oct_exchange)) then
644 if (i == 1) then
645 call hm%ks_pot%get_interpolated_potentials(tr%vks_old, 0, storage=vhxc1_op)
646 i = i + 1
647 else
648 call hm%ks_pot%store_potentials(vhxc1_op)
649 end if
650 t_op = time
651 else
652 call hm%ks_pot%store_potentials(vhxc1_op)
653 end if
654
655 if (ions_dyn%ions_move()) then
656 call ion_dynamics_restore_state(ions_dyn, ions, ions_state)
657 end if
658
659 j = 1
660 do ik = kp1, kp2
661 do ist = st1, st2
662 do idim = 1, st%d%dim
663 rhs(j:j+np-1) = rhs1(1:np, idim, ist, ik)
664 j = j + np
665 end do
666 end do
667 end do
668
669 ! Now we populate an initial guess for the equation.
670 j = 1
671 do ik = kp1, kp2
672 do ist = st1, st2
673 do idim = 1, st%d%dim
674 zpsi(j:j+np-1) = k2(1:np, idim, ist, ik)
675 j = j + np
676 end do
677 end do
678 end do
679
680 t_op = time - dt
681 call zsparskit_solver_run(namespace, tr%tdsk, td_rk2op, td_rk2opt, zpsi, rhs)
682
683 k2 = m_z0
684 j = 1
685 do ik = kp1, kp2
686 do ist = st1, st2
687 do idim = 1, st%d%dim
688 k2(1:np, idim, ist, ik) = zpsi(j:j+np-1)
689 j = j + np
690 end do
691 end do
692 end do
693
694 dres = m_zero
695 do ik = kp1, kp2
696 do ist = st1, st2
697 do idim = 1, st%d%dim
698 dres = dres + (zmf_nrm2(gr, k2(:, idim, ist, ik) - oldk2(:, idim, ist, ik), reduce = .false.))**2
699 end do
700 end do
701 end do
702
703 call comm_allreduce(st%dom_st_kpt_mpi_grp, dres)
704
705 if (sqrt(dres) < tr%scf_threshold) exit
706 end do
707
708 zphi = k2
709 do ik = kp1, kp2
710 do ist = st1, st2
711 call states_elec_set_state(st, gr, ist, ik, zphi(:, :, ist, ik))
712 end do
713 end do
714
715 call density_calc(st, gr, st%rho)
716
717 safe_deallocate_a(k2)
718 safe_deallocate_a(oldk2)
719 safe_deallocate_a(zphi)
720 safe_deallocate_a(rhs1)
721 safe_deallocate_a(zpsi)
722 safe_deallocate_a(rhs)
723 safe_deallocate_a(vpsl1_op)
724
725 pop_sub(td_runge_kutta2)
726 end subroutine td_runge_kutta2
727
728 !----------------------------------------------------------------------------
729
730 subroutine td_runge_kutta4(ks, namespace, space, hm, gr, st, tr, time, dt, ions_dyn, ions, ext_partners)
731 type(v_ks_t), target, intent(inout) :: ks
732 type(namespace_t), target, intent(in) :: namespace
733 type(electron_space_t), target, intent(in) :: space
734 type(hamiltonian_elec_t), target, intent(inout) :: hm
735 type(grid_t), target, intent(in) :: gr
736 type(states_elec_t), target, intent(inout) :: st
737 type(propagator_base_t), target, intent(inout) :: tr
738 real(real64), intent(in) :: time
739 real(real64), intent(in) :: dt
740 type(ion_dynamics_t), intent(inout) :: ions_dyn
741 type(ions_t), intent(inout) :: ions
742 type(partner_list_t), target, intent(in) :: ext_partners
743
744 integer :: idim, ip, ist, ik, j, kp1, kp2, st1, st2, nspin
745 real(real64) :: dres
746 complex(real64), allocatable :: inhpsi(:)
747 complex(real64), allocatable :: zpsi(:), rhs(:)
748 complex(real64), allocatable :: zphi(:, :, :, :)
749 type(ion_state_t) :: ions_state
750
751 real(real64) :: a(2, 2), c(2), b(2)
752 complex(real64), allocatable :: k1(:, :, :, :), k2(:, :, :, :), oldk1(:, :, :, :), &
753 oldk2(:, :, :, :), yn1(:, :, :, :), yn2(:, :, :, :), &
754 rhs1(:, :, :, :), rhs2(:, :, :, :)
755
756 push_sub(td_runge_kutta4)
757
758 if(ions_dyn%cell_relax()) then
759 call messages_not_implemented("CellDynamics=yes with RK4 propagator")
760 end if
761
762
763 c(1) = m_half - sqrt(m_three)/6.0_real64
764 c(2) = m_half + sqrt(m_three)/6.0_real64
765 b(1) = m_half
766 b(2) = m_half
767 a(1, 1) = m_fourth
768 a(1, 2) = m_fourth - sqrt(m_three)/6.0_real64
769 a(2, 1) = m_fourth + sqrt(m_three)/6.0_real64
770 a(2, 2) = m_fourth
771
772 st1 = st%st_start
773 st2 = st%st_end
774 kp1 = st%d%kpt%start
775 kp2 = st%d%kpt%end
776 nspin = hm%d%nspin
777 move_ions_op = ions_dyn%ions_move()
778
779 sp_np = gr%np
780 sp_dim = st%d%dim
781 sp_st1 = st1
782 sp_st2 = st2
783 sp_kp1 = kp1
784 sp_kp2 = kp2
785 sp_parallel = st%parallel_in_states .or. st%d%kpt%parallel
786 if (sp_parallel) call mpi_grp_copy(sp_grp, st%st_kpt_mpi_grp)
787
788 ! define pointer and variables for usage in td_rk4op, td_rk4opt routines
789 mesh_p => gr
790 hm_p => hm
791 tr_p => tr
792 st_p => st
793 namespace_p => namespace
794 space_p => space
795 ext_partners_p => ext_partners
796 dt_op = dt
797 t_op = time - dt/m_two
798 dim_op = st%d%dim
799
800 safe_allocate(vpsl1_op(1:gr%np))
801 safe_allocate(vpsl2_op(1:gr%np))
802 safe_allocate(k1(1:gr%np_part, 1:st%d%dim, st1:st2, kp1:kp2))
803 safe_allocate(k2(1:gr%np_part, 1:st%d%dim, st1:st2, kp1:kp2))
804 safe_allocate(oldk1(1:gr%np_part, 1:st%d%dim, st1:st2, kp1:kp2))
805 safe_allocate(oldk2(1:gr%np_part, 1:st%d%dim, st1:st2, kp1:kp2))
806 safe_allocate(yn1(1:gr%np_part, 1:st%d%dim, st1:st2, kp1:kp2))
807 safe_allocate(yn2(1:gr%np_part, 1:st%d%dim, st1:st2, kp1:kp2))
808 safe_allocate(rhs1(1:gr%np_part, 1:st%d%dim, st1:st2, kp1:kp2))
809 safe_allocate(rhs2(1:gr%np_part, 1:st%d%dim, st1:st2, kp1:kp2))
810 safe_allocate(rhs(1:tr%tdsk_size))
811 safe_allocate(zpsi(1:tr%tdsk_size))
812 safe_allocate(zphi(1:gr%np_part, st%d%dim, st1:st2, kp1:kp2))
813
814 ! First, we get the state that we want to propagate. For the moment being, only one state.
815 do ik = kp1, kp2
816 do ist = st1, st2
817 call states_elec_get_state(st, gr, ist, ik, zphi(:, :, ist, ik))
818 end do
819 end do
820 k1 = m_z0
821 k2 = m_z0
822
823 do
824
825 oldk1 = k1
826 oldk2 = k2
827
828 yn1 = zphi + a(1, 1) * k1 + a(1, 2) * k2
829 yn2 = zphi + a(2, 1) * k1 + a(2, 2) * k2
830
831 ! Set the Hamiltonian at time-dt + c(1) * dt
832 do ik = kp1, kp2
833 do ist = st1, st2
834 call states_elec_set_state(st, gr, ist, ik, yn1(:, :, ist, ik))
835 end do
836 end do
837 call density_calc(st, gr, st%rho)
838 call v_ks_calc(ks, namespace, space, hm, st, ions, ext_partners, &
839 calc_current = list_has_gauge_field(ext_partners), &
840 calc_energy = .false., calc_eigenval = .false.)
841 if (ions_dyn%ions_move()) then
842 call ion_dynamics_save_state(ions_dyn, ions, ions_state)
843 call ion_dynamics_propagate(ions_dyn, ions, time - dt + c(1)*dt, c(1)*dt, namespace)
844 call hamiltonian_elec_epot_generate(hm, namespace, space, gr, ions, ext_partners, st, time = time - dt + c(1)*dt)
845 vpsl1_op = hm%ep%vpsl
846 end if
847
848 call propagation_ops_elec_update_hamiltonian(namespace, space, st, gr, hm, ext_partners, time - dt + c(1)*dt)
849
850 call hm%ks_pot%store_potentials(vhxc1_op)
851 t_op = time - dt + c(1) * dt
852 rhs1 = m_z0
853 do ik = kp1, kp2
854 do ist = st1, st2
855 call zhamiltonian_elec_apply_single(hm_p, namespace, mesh_p, zphi(:, :, ist, ik), rhs1(:, :, ist, ik), ist, ik)
856 if (hamiltonian_elec_inh_term(hm)) then
857 safe_allocate(inhpsi(1:gr%np))
858 do idim = 1, st%d%dim
859 call states_elec_get_state(hm%inh_st, gr, idim, ist, ik, inhpsi)
860 do ip = 1, gr%np
861 rhs1(ip, idim, ist, ik) = rhs1(ip, idim, ist, ik) + m_zi * inhpsi(ip)
862 end do
863 end do
864 safe_deallocate_a(inhpsi)
865 end if
866 end do
867 end do
868 rhs1 = - m_zi * dt * rhs1
869 if (ions_dyn%ions_move()) then
870 call ion_dynamics_restore_state(ions_dyn, ions, ions_state)
871 end if
872
873 ! Set the Hamiltonian at time-dt + c(2) * dt
874 do ik = kp1, kp2
875 do ist = st1, st2
876 call states_elec_set_state(st, gr, ist, ik, yn2(:, :, ist, ik))
877 end do
878 end do
879 call density_calc(st, gr, st%rho)
880 call v_ks_calc(ks, namespace, space, hm, st, ions, ext_partners, &
881 calc_current = list_has_gauge_field(ext_partners), &
882 calc_energy = .false., calc_eigenval = .false.)
883 if (ions_dyn%ions_move()) then
884 call ion_dynamics_save_state(ions_dyn, ions, ions_state)
885 call ion_dynamics_propagate(ions_dyn, ions, time - dt + c(2)*dt, c(2)*dt, namespace)
886 call hamiltonian_elec_epot_generate(hm, namespace, space, gr, ions, ext_partners, st, time = time - dt + c(2)*dt)
887 vpsl2_op = hm%ep%vpsl
888 end if
889
890 call propagation_ops_elec_update_hamiltonian(namespace, space, st, gr, hm, ext_partners, time - dt + c(2)*dt)
891
892 call hm%ks_pot%store_potentials(vhxc2_op)
893 t_op = time - dt + c(2) * dt
894 rhs2 = m_z0
895 do ik = kp1, kp2
896 do ist = st1, st2
897 call zhamiltonian_elec_apply_single(hm_p, namespace, mesh_p, zphi(:, :, ist, ik), rhs2(:, :, ist, ik), ist, ik)
898 if (hamiltonian_elec_inh_term(hm)) then
899 safe_allocate(inhpsi(1:gr%np))
900 do idim = 1, st%d%dim
901 call states_elec_get_state(hm%inh_st, gr, idim, ist, ik, inhpsi)
902 do ip = 1, gr%np
903 rhs2(ip, idim, ist, ik) = rhs2(ip, idim, ist, ik) + m_zi * inhpsi(ip)
904 end do
905 end do
906 safe_deallocate_a(inhpsi)
907 end if
908 end do
909 end do
910 rhs2 = -m_zi * dt * rhs2
911 if (ions_dyn%is_active()) then
912 call ion_dynamics_restore_state(ions_dyn, ions, ions_state)
913 end if
914
915 j = 1
916 do ik = kp1, kp2
917 do ist = st1, st2
918 do idim = 1, st%d%dim
919 call lalg_copy(gr%np, rhs1(1:gr%np, idim, ist, ik), rhs(j:j+gr%np-1))
920 j = j + gr%np
921 end do
922 end do
923 end do
924 do ik = kp1, kp2
925 do ist = st1, st2
926 do idim = 1, st%d%dim
927 call lalg_copy(gr%np, rhs2(1:gr%np, idim, ist, ik), rhs(j:j+gr%np-1))
928 j = j + gr%np
929 end do
930 end do
931 end do
932
933 ! Now we populate an initial guess for the equation.
934 j = 1
935 do ik = kp1, kp2
936 do ist = st1, st2
937 do idim = 1, st%d%dim
938 call lalg_copy(gr%np, k1(1:gr%np, idim, ist, ik), zpsi(j:j+gr%np-1))
939 j = j + gr%np
940 end do
941 end do
942 end do
943 do ik = kp1, kp2
944 do ist = st1, st2
945 do idim = 1, st%d%dim
946 call lalg_copy(gr%np, k2(1:gr%np, idim, ist, ik), zpsi(j:j+gr%np-1))
947 j = j + gr%np
948 end do
949 end do
950 end do
951
952 t_op = time - dt
953 call zsparskit_solver_run(namespace, tr%tdsk, td_rk4op, td_rk4opt, zpsi, rhs)
954
955 k1 = m_z0
956 k2 = m_z0
957 j = 1
958 do ik = kp1, kp2
959 do ist = st1, st2
960 do idim = 1, st%d%dim
961 call lalg_copy(gr%np, zpsi(j:j+gr%np-1), k1(1:gr%np, idim, ist, ik))
962 j = j + gr%np
963 end do
964 end do
965 end do
966 do ik = kp1, kp2
967 do ist = st1, st2
968 do idim = 1, st%d%dim
969 call lalg_copy(gr%np, zpsi(j:j+gr%np-1), k2(1:gr%np, idim, ist, ik))
970 j = j + gr%np
971 end do
972 end do
973 end do
974
975 dres = m_zero
976 do ik = kp1, kp2
977 do ist = st1, st2
978 do idim = 1, st%d%dim
979 dres = dres + (zmf_nrm2(gr, k1(:, idim, ist, ik) - oldk1(:, idim, ist, ik)))**2
980 dres = dres + (zmf_nrm2(gr, k2(:, idim, ist, ik) - oldk2(:, idim, ist, ik)))**2
981 end do
982 end do
983 end do
984 if (sp_parallel) call comm_allreduce(sp_grp, dres)
985 !write(*, *) 'Residual = ', dres
986
987 if (sqrt(dres) < tr%scf_threshold) exit
988 end do
989
990
991 zphi = zphi + b(1) * k1 + b(2) * k2
992 do ik = kp1, kp2
993 do ist = st1, st2
994 call states_elec_set_state(st, gr, ist, ik, zphi(:, :, ist, ik))
995 end do
996 end do
997
998 call density_calc(st, gr, st%rho)
999
1000 safe_deallocate_a(rhs1)
1001 safe_deallocate_a(rhs2)
1002 safe_deallocate_a(k1)
1003 safe_deallocate_a(k2)
1004 safe_deallocate_a(oldk1)
1005 safe_deallocate_a(oldk2)
1006 safe_deallocate_a(yn1)
1007 safe_deallocate_a(yn2)
1008 safe_deallocate_a(vpsl1_op)
1009 safe_deallocate_a(vpsl2_op)
1010 safe_deallocate_a(zpsi)
1011 safe_deallocate_a(rhs)
1012
1013 pop_sub(td_runge_kutta4)
1014 end subroutine td_runge_kutta4
1015
1016 ! ---------------------------------------------------------
1018 subroutine td_rk4op(xre, xim, yre, yim)
1019 real(real64), intent(in) :: xre(:)
1020 real(real64), intent(in) :: xim(:)
1021 real(real64), intent(out) :: yre(:)
1022 real(real64), intent(out) :: yim(:)
1023
1024 integer :: idim, j, ik, ist, kp1, kp2, st1, st2, dim, k, jj
1025 complex(real64), allocatable :: zpsi(:, :)
1026 complex(real64), allocatable :: opzpsi(:, :)
1027 real(real64) :: a(2, 2), c(2)
1028 integer :: np_part, np
1029
1030 push_sub(td_rk4op)
1031
1032 np_part = mesh_p%np_part
1033 np = mesh_p%np
1034 st1 = st_p%st_start
1035 st2 = st_p%st_end
1036 kp1 = st_p%d%kpt%start
1037 kp2 = st_p%d%kpt%end
1038 dim = st_p%d%dim
1039
1040 safe_allocate(zpsi(1:np_part, 1:dim))
1041 safe_allocate(opzpsi(1:np_part, 1:dim))
1042
1043 a(1, 1) = m_fourth
1044 a(1, 2) = m_fourth - sqrt(m_three)/6.0_real64
1045 a(2, 1) = m_fourth + sqrt(m_three)/6.0_real64
1046 a(2, 2) = m_fourth
1047
1048 c(1) = m_half - sqrt(m_three)/6.0_real64
1049 c(2) = m_half + sqrt(m_three)/6.0_real64
1050
1051 zpsi = m_z0
1052
1053 call hm_p%ks_pot%restore_potentials(vhxc1_op)
1054 if (move_ions_op) hm_p%ep%vpsl = vpsl1_op
1055 call propagation_ops_elec_update_hamiltonian(namespace_p, space_p, st_p, mesh_p, hm_p, ext_partners_p, t_op + c(1)*dt_op)
1056 j = 1
1057 k = np * (kp2 - kp1 + 1) * (st2 - st1 + 1) * dim + 1
1058 do ik = kp1, kp2
1059 do ist = st1, st2
1060 jj = j
1061 do idim = 1, dim
1062 zpsi(1:np, idim) = a(1, 1) * cmplx(xre(j:j+np-1), xim(j:j+np-1), real64) + &
1063 a(1, 2) * cmplx(xre(k:k+np-1), xim(k:k+np-1), real64)
1064 j = j + np
1065 k = k + np
1066 end do
1067
1068 call zhamiltonian_elec_apply_single(hm_p, namespace_p, mesh_p, zpsi, opzpsi, ist, ik)
1069
1070 do idim = 1, dim
1071 yre(jj:jj+np-1) = xre(jj:jj+np-1) - aimag(dt_op * opzpsi(1:np, idim))
1072 yim(jj:jj+np-1) = xim(jj:jj+np-1) + real(dt_op * opzpsi(1:np, idim), real64)
1073 jj = jj + np
1074 end do
1075 end do
1076 end do
1077
1078 call hm_p%ks_pot%restore_potentials(vhxc2_op)
1079
1080 if (move_ions_op) hm_p%ep%vpsl = vpsl2_op
1081 call propagation_ops_elec_update_hamiltonian(namespace_p, space_p, st_p, mesh_p, hm_p, ext_partners_p, t_op + c(2)*dt_op)
1082 j = 1
1083 k = np * (kp2 - kp1 + 1) * (st2 - st1 + 1) * dim + 1
1084 do ik = kp1, kp2
1085 do ist = st1, st2
1086 jj = k
1087 do idim = 1, dim
1088 zpsi(1:np, idim) = a(2, 1) * cmplx(xre(j:j+np-1), xim(j:j+np-1), real64) + &
1089 a(2, 2) * cmplx(xre(k:k+np-1), xim(k:k+np-1), real64)
1090 j = j + np
1091 k = k + np
1092 end do
1093
1094 call zhamiltonian_elec_apply_single(hm_p, namespace_p, mesh_p, zpsi, opzpsi, ist, ik)
1095
1096 do idim = 1, dim
1097 yre(jj:jj+np-1) = xre(jj:jj+np-1) - aimag(dt_op * opzpsi(1:np, idim))
1098 yim(jj:jj+np-1) = xim(jj:jj+np-1) + real(dt_op * opzpsi(1:np, idim), real64)
1099 jj = jj + np
1100 end do
1101 end do
1102 end do
1103
1104 safe_deallocate_a(zpsi)
1105 safe_deallocate_a(opzpsi)
1106 pop_sub(td_rk4op)
1107 end subroutine td_rk4op
1108 ! ---------------------------------------------------------
1109
1110
1111 ! ---------------------------------------------------------
1113 subroutine td_rk4opt(xre, xim, yre, yim)
1114 real(real64), intent(in) :: xre(:)
1115 real(real64), intent(in) :: xim(:)
1116 real(real64), intent(out) :: yre(:)
1117 real(real64), intent(out) :: yim(:)
1118
1119 integer :: idim, j, ik, ist, kp1, kp2, st1, st2, dim, k, jj
1120 complex(real64), allocatable :: zpsi(:, :)
1121 complex(real64), allocatable :: opzpsi(:, :)
1122 real(real64) :: a(2, 2), c(2)
1123 integer :: np_part, np
1124
1125 push_sub(td_rk4opt)
1126
1127 np_part = mesh_p%np_part
1128 np = mesh_p%np
1129 st1 = st_p%st_start
1130 st2 = st_p%st_end
1131 kp1 = st_p%d%kpt%start
1132 kp2 = st_p%d%kpt%end
1133 dim = st_p%d%dim
1134
1135 safe_allocate(zpsi(1:np_part, 1:dim))
1136 safe_allocate(opzpsi(1:np_part, 1:dim))
1137
1138 a(1, 1) = m_fourth
1139 a(1, 2) = m_fourth - sqrt(m_three)/6.0_real64
1140 a(2, 1) = m_fourth + sqrt(m_three)/6.0_real64
1141 a(2, 2) = m_fourth
1142
1143 c(1) = m_half - sqrt(m_three)/6.0_real64
1144 c(2) = m_half + sqrt(m_three)/6.0_real64
1145
1146 zpsi = m_z0
1147
1148 call hm_p%ks_pot%restore_potentials(vhxc1_op)
1149 if (move_ions_op) hm_p%ep%vpsl = vpsl1_op
1150
1151 call propagation_ops_elec_update_hamiltonian(namespace_p, space_p, st_p, mesh_p, hm_p, ext_partners_p, t_op + c(1)*dt_op)
1152
1153 j = 1
1154 k = np * (kp2 - kp1 + 1) * (st2 - st1 + 1) * dim + 1
1155 do ik = kp1, kp2
1156 do ist = st1, st2
1157 jj = j
1158 do idim = 1, dim
1159 zpsi(1:np, idim) = a(1, 1) * cmplx(xre(j:j+np-1), -xim(j:j+np-1), real64) + &
1160 a(1, 2) * cmplx(xre(k:k+np-1), -xim(k:k+np-1), real64)
1161 j = j + np
1162 k = k + np
1163 end do
1164
1165 call zhamiltonian_elec_apply_single(hm_p, namespace_p, mesh_p, zpsi, opzpsi, ist, ik)
1166
1167 do idim = 1, dim
1168 yre(jj:jj+np-1) = xre(jj:jj+np-1) - aimag(dt_op * opzpsi(1:np, idim))
1169 yim(jj:jj+np-1) = xim(jj:jj+np-1) - real(dt_op * opzpsi(1:np, idim), real64)
1170 jj = jj + np
1171 end do
1172 end do
1173 end do
1174
1175 call hm_p%ks_pot%restore_potentials(vhxc2_op)
1176 if (move_ions_op) hm_p%ep%vpsl = vpsl2_op
1177
1178 call propagation_ops_elec_update_hamiltonian(namespace_p, space_p, st_p, mesh_p, hm_p, ext_partners_p, t_op + c(2)*dt_op)
1179
1180 j = 1
1181 k = np * (kp2 - kp1 + 1) * (st2 - st1 + 1) * dim + 1
1182 do ik = kp1, kp2
1183 do ist = st1, st2
1184 jj = k
1185 do idim = 1, dim
1186 zpsi(1:np, idim) = a(2, 1) * cmplx(xre(j:j+np-1), -xim(j:j+np-1), real64) + &
1187 a(2, 2) * cmplx(xre(k:k+np-1), -xim(k:k+np-1), real64)
1188 j = j + np
1189 k = k + np
1190 end do
1191
1192 call zhamiltonian_elec_apply_single(hm_p, namespace_p, mesh_p, zpsi, opzpsi, ist, ik)
1193
1194 do idim = 1, dim
1195 yre(jj:jj+np-1) = xre(jj:jj+np-1) - aimag(dt_op * opzpsi(1:np, idim))
1196 yim(jj:jj+np-1) = xim(jj:jj+np-1) - real(dt_op * opzpsi(1:np, idim), real64)
1197 jj = jj + np
1198 end do
1199 end do
1200 end do
1201
1202 safe_deallocate_a(zpsi)
1203 safe_deallocate_a(opzpsi)
1204 pop_sub(td_rk4opt)
1205 end subroutine td_rk4opt
1206 ! ---------------------------------------------------------
1207
1208
1209 ! ---------------------------------------------------------
1211 subroutine td_rk2op(xre, xim, yre, yim)
1212 real(real64), intent(in) :: xre(:)
1213 real(real64), intent(in) :: xim(:)
1214 real(real64), intent(out) :: yre(:)
1215 real(real64), intent(out) :: yim(:)
1216
1217 integer :: np_part, np, st1, st2, kp1, kp2, dim, idim, ik, ist, jj, j
1218 complex(real64), allocatable :: zpsi(:, :)
1219 complex(real64), allocatable :: opzpsi(:, :)
1220 complex(real64), allocatable :: zpsi_(:, :, :, :)
1221
1222 push_sub(td_rk2op)
1223
1224 np_part = mesh_p%np_part
1225 np = mesh_p%np
1226 st1 = st_p%st_start
1227 st2 = st_p%st_end
1228 kp1 = st_p%d%kpt%start
1229 kp2 = st_p%d%kpt%end
1230 dim = st_p%d%dim
1231
1232 safe_allocate(zpsi(1:np_part, 1:dim))
1233 safe_allocate(opzpsi(1:np_part, 1:dim))
1234 safe_allocate(zpsi_(1:np_part, 1:dim, st1:st2, kp1:kp2))
1235
1236 zpsi = m_z0
1237 opzpsi = m_z0
1238
1239 call hm_p%ks_pot%restore_potentials(vhxc1_op)
1240 if (move_ions_op) hm_p%ep%vpsl = vpsl1_op
1241 call propagation_ops_elec_update_hamiltonian(namespace_p, space_p, st_p, mesh_p, hm_p, ext_partners_p, t_op + dt_op)
1242
1243 if (oct_exchange_enabled(hm_p%oct_exchange)) then
1244 zpsi_ = m_z0
1245 j = 1
1246 do ik = kp1, kp2
1247 do ist = st1, st2
1248 jj = j
1249 do idim = 1, dim
1250 zpsi_(1:np, idim, ist, ik) = cmplx(xre(j:j+np-1), xim(j:j+np-1), real64)
1251 j = j + np
1252 end do
1253 end do
1254 end do
1255 call oct_exchange_prepare(hm_p%oct_exchange, mesh_p, zpsi_, hm_p%xc, hm_p%psolver, namespace_p)
1256 end if
1257
1258 j = 1
1259 do ik = kp1, kp2
1260 do ist = st1, st2
1261 jj = j
1262 do idim = 1, dim
1263 zpsi(1:np, idim) = cmplx(xre(j:j+np-1), xim(j:j+np-1), real64)
1264 j = j + np
1265 end do
1266 call zhamiltonian_elec_apply_single(hm_p, namespace_p, mesh_p, zpsi, opzpsi, ist, ik)
1267 do idim = 1, dim
1268 yre(jj:jj+np-1) = xre(jj:jj+np-1) - aimag(dt_op * m_half * opzpsi(1:np, idim))
1269 yim(jj:jj+np-1) = xim(jj:jj+np-1) + real(dt_op * m_half * opzpsi(1:np, idim), real64)
1270 jj = jj + np
1271 end do
1272 end do
1273 end do
1274
1275 if (oct_exchange_enabled(hm_p%oct_exchange)) then
1276 j = 1
1277 do ik = kp1, kp2
1278 do ist = st1, st2
1279 jj = j
1280 do idim = 1, dim
1281 zpsi(1:np, idim) = cmplx(xre(j:j+np-1), xim(j:j+np-1), real64)
1282 j = j + np
1283 end do
1284 opzpsi = m_z0
1285 call oct_exchange_operator(hm_p%oct_exchange, namespace_p, mesh_p, opzpsi, ist, ik)
1286
1287 do idim = 1, dim
1288 yre(jj:jj+np-1) = yre(jj:jj+np-1) - aimag(dt_op * m_half * opzpsi(1:np, idim))
1289 yim(jj:jj+np-1) = yim(jj:jj+np-1) + real(dt_op * m_half * opzpsi(1:np, idim), real64)
1290 jj = jj + np
1291 end do
1292 end do
1293 end do
1294 end if
1295
1296 safe_deallocate_a(zpsi)
1297 safe_deallocate_a(opzpsi)
1298
1299 pop_sub(td_rk2op)
1300 end subroutine td_rk2op
1301 ! ---------------------------------------------------------
1302
1303
1304 ! ---------------------------------------------------------
1306 subroutine td_rk2opt(xre, xim, yre, yim)
1307 real(real64), intent(in) :: xre(:)
1308 real(real64), intent(in) :: xim(:)
1309 real(real64), intent(out) :: yre(:)
1310 real(real64), intent(out) :: yim(:)
1311
1312 integer :: np_part, np, st1, st2, kp1, kp2, dim, idim, ik, ist, jj, j
1313 complex(real64), allocatable :: zpsi(:, :)
1314 complex(real64), allocatable :: opzpsi(:, :)
1315 complex(real64), allocatable :: zpsi_(:, :, :, :)
1316
1317 push_sub(td_rk2opt)
1318
1319 np_part = mesh_p%np_part
1320 np = mesh_p%np
1321 st1 = st_p%st_start
1322 st2 = st_p%st_end
1323 kp1 = st_p%d%kpt%start
1324 kp2 = st_p%d%kpt%end
1325 dim = st_p%d%dim
1326
1327 safe_allocate(zpsi(1:np_part, 1:dim))
1328 safe_allocate(opzpsi(1:np_part, 1:dim))
1329 safe_allocate(zpsi_(1:np_part, 1:dim, st1:st2, kp1:kp2))
1330
1331 zpsi = m_z0
1332 opzpsi = m_z0
1333
1334 call hm_p%ks_pot%restore_potentials(vhxc1_op)
1335 if (move_ions_op) hm_p%ep%vpsl = vpsl1_op
1336 call propagation_ops_elec_update_hamiltonian(namespace_p, space_p, st_p, mesh_p, hm_p, ext_partners_p, t_op + dt_op)
1337
1338 if (oct_exchange_enabled(hm_p%oct_exchange)) then
1339 zpsi_ = m_z0
1340 j = 1
1341 do ik = kp1, kp2
1342 do ist = st1, st2
1343 jj = j
1344 do idim = 1, dim
1345 zpsi_(1:np, idim, ist, ik) = cmplx(xre(j:j+np-1), -xim(j:j+np-1), real64)
1346 j = j + np
1347 end do
1348 end do
1349 end do
1350 call oct_exchange_prepare(hm_p%oct_exchange, mesh_p, zpsi_, hm_p%xc, hm_p%psolver, namespace_p)
1351 end if
1352
1353 j = 1
1354 do ik = kp1, kp2
1355 do ist = st1, st2
1356 jj = j
1357 do idim = 1, dim
1358 zpsi(1:np, idim) = cmplx(xre(j:j+np-1), -xim(j:j+np-1), real64)
1359 j = j + np
1360 end do
1361 call zhamiltonian_elec_apply_single(hm_p, namespace_p, mesh_p, zpsi, opzpsi, ist, ik)
1362
1363 do idim = 1, dim
1364 yre(jj:jj+np-1) = xre(jj:jj+np-1) - aimag(dt_op * m_half * opzpsi(1:np, idim))
1365 yim(jj:jj+np-1) = xim(jj:jj+np-1) - real(dt_op * m_half * opzpsi(1:np, idim), real64)
1366 jj = jj + np
1367 end do
1368 end do
1369 end do
1370
1371 if (oct_exchange_enabled(hm_p%oct_exchange)) then
1372 j = 1
1373 do ik = kp1, kp2
1374 do ist = st1, st2
1375 jj = j
1376 do idim = 1, dim
1377 zpsi(1:np, idim) = cmplx(xre(j:j+np-1), xim(j:j+np-1), real64)
1378 j = j + np
1379 end do
1380 opzpsi = m_z0
1381 call oct_exchange_operator(hm_p%oct_exchange, namespace_p, mesh_p, opzpsi, ist, ik)
1382
1383 do idim = 1, dim
1384 yre(jj:jj+np-1) = yre(jj:jj+np-1) - aimag(dt_op * m_half * opzpsi(1:np, idim))
1385 yim(jj:jj+np-1) = yim(jj:jj+np-1) - real(dt_op * m_half * opzpsi(1:np, idim), real64)
1386 jj = jj + np
1387 end do
1388 end do
1389 end do
1390 end if
1391
1392 safe_deallocate_a(zpsi)
1393 safe_deallocate_a(opzpsi)
1394 pop_sub(td_rk2opt)
1395 end subroutine td_rk2opt
1396 ! ---------------------------------------------------------
1397
1398 subroutine propagator_rk_end()
1400
1401 call xc_copied_potentials_end(vhxc1_op)
1402 call xc_copied_potentials_end(vhxc2_op)
1403
1404 pop_sub(propagator_rk_end)
1405 end subroutine propagator_rk_end
1406end module propagator_rk_oct_m
1407
1408!! Local Variables:
1409!! mode: f90
1410!! coding: utf-8
1411!! End:
batchified version of the BLAS axpy routine:
Definition: batch_ops.F90:154
Copies a vector x, to a vector y.
Definition: lalg_basic.F90:186
double sqrt(double __x) __attribute__((__nothrow__
This module implements common operations on batches of mesh functions.
Definition: batch_ops.F90:116
This module implements a calculator for the density and defines related functions.
Definition: density.F90:120
subroutine, public density_calc(st, gr, density, istin)
Computes the density from the orbitals in st.
Definition: density.F90:610
logical function, public list_has_gauge_field(partners)
subroutine, public forces_costate_calculate(gr, namespace, ions, hm, psi, chi, ff, qq)
Definition: forces.F90:218
subroutine, public forces_calculate(gr, namespace, ions, hm, ext_partners, st, ks, vhxc_old, t, dt)
Definition: forces.F90:339
real(real64), parameter, public m_two
Definition: global.F90:190
real(real64), parameter, public m_zero
Definition: global.F90:188
real(real64), parameter, public m_third
Definition: global.F90:195
real(real64), parameter, public m_fourth
Definition: global.F90:197
complex(real64), parameter, public m_z0
Definition: global.F90:198
complex(real64), parameter, public m_zi
Definition: global.F90:202
real(real64), parameter, public m_half
Definition: global.F90:194
real(real64), parameter, public m_one
Definition: global.F90:189
real(real64), parameter, public m_three
Definition: global.F90:191
This module implements the underlying real-space grid.
Definition: grid.F90:117
subroutine, public hamiltonian_elec_set_inh(hm, st)
subroutine, public hamiltonian_elec_adjoint(hm)
subroutine, public zhamiltonian_elec_apply_single(hm, namespace, mesh, psi, hpsi, ist, ik, terms, set_bc, set_phase)
subroutine, public hamiltonian_elec_epot_generate(this, namespace, space, gr, ions, ext_partners, st, time)
subroutine, public zhamiltonian_elec_apply_all(hm, namespace, mesh, st, hst)
subroutine, public hamiltonian_elec_remove_inh(hm)
pure logical function, public hamiltonian_elec_inh_term(hm)
subroutine, public hamiltonian_elec_not_adjoint(hm)
This module defines classes and functions for interaction partners.
subroutine, public ion_dynamics_restore_state(this, ions, state)
subroutine, public ion_dynamics_propagate(this, ions, time, dt, namespace)
Interface for the ion/cell dynamics.
subroutine, public ion_dynamics_save_state(this, ions, state)
A module to handle KS potential, without the external potential.
integer, parameter, public independent_particles
subroutine, public xc_copied_potentials_end(this)
Finalizer for the copied potentials.
subroutine, public lda_u_update_occ_matrices(this, namespace, mesh, st, hm_base, phase, energy)
Definition: lda_u.F90:798
This module defines various routines, operating on mesh functions.
integer, public sp_st2
logical, public sp_parallel
integer, public sp_dim
integer, public sp_kp2
integer, public sp_kp1
type(mpi_grp_t), public sp_grp
integer, public sp_st1
integer, public sp_np
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:118
subroutine, public messages_not_implemented(feature, namespace)
Definition: messages.F90:1113
subroutine mpi_grp_copy(mpi_grp_out, mpi_grp_in)
Definition: mpi.F90:407
logical function, public oct_exchange_enabled(this)
subroutine, public oct_exchange_remove(this)
subroutine, public oct_exchange_set(this, st, mesh)
subroutine, public oct_exchange_operator(this, namespace, mesh, hpsi, ist, ik)
subroutine, public oct_exchange_prepare(this, mesh, psi, xc, psolver, namespace)
This module holds the "opt_control_state_t" datatype, which contains a quantum-classical state.
real(real64) function, dimension(:, :), pointer, public opt_control_point_p(ocs)
real(real64) function, dimension(:, :), pointer, public opt_control_point_q(ocs)
type(states_elec_t) function, pointer, public opt_control_point_qs(ocs)
subroutine, public propagation_ops_elec_update_hamiltonian(namespace, space, st, mesh, hm, ext_partners, time)
subroutine, public td_runge_kutta2(ks, namespace, space, hm, gr, st, tr, time, dt, ions_dyn, ions, ext_partners)
subroutine, public td_runge_kutta4(ks, namespace, space, hm, gr, st, tr, time, dt, ions_dyn, ions, ext_partners)
subroutine td_rk2op(xre, xim, yre, yim)
operator for the RK2 propagator
subroutine td_rk4op(xre, xim, yre, yim)
operators for Crank-Nicolson scheme
subroutine, public td_explicit_runge_kutta4(ks, namespace, space, hm, gr, st, time, dt, ions_dyn, ions, ext_partners, qcchi)
subroutine, public propagator_rk_end()
subroutine td_rk2opt(xre, xim, yre, yim)
operator for the RK2 propagator
subroutine td_rk4opt(xre, xim, yre, yim)
Transpose of H (called e.g. by bi-conjugate gradient solver)
subroutine, public zsparskit_solver_run(namespace, sk, op, opt, sol, rhs)
Definition: sparskit.F90:784
subroutine, public states_elec_end(st)
finalize the states_elec_t object
subroutine, public states_elec_copy(stout, stin, exclude_wfns, exclude_eigenval, special)
make a (selective) copy of a states_elec_t object
subroutine, public v_ks_calc(ks, namespace, space, hm, st, ions, ext_partners, calc_eigenval, time, calc_energy, calc_current, force_semilocal)
Definition: v_ks.F90:736
Definition: xc.F90:114
subroutine f_ions(tau)
subroutine f_psi(tau)
subroutine apply_inh()
subroutine f_chi(tau)
subroutine update_state(epsilon)
subroutine prepare_inh()
Extension of space that contains the knowledge of the spin dimension.
Description of the grid, containing information on derivatives, stencil, and symmetries.
Definition: grid.F90:169
The states_elec_t class contains all electronic wave functions.