Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief Routine for the real time propagation output.
10 : !> \author Florian Schiffmann (02.09)
11 : ! **************************************************************************************************
12 :
13 : MODULE rt_propagation_output
14 : USE atomic_kind_types, ONLY: atomic_kind_type
15 : USE cell_types, ONLY: cell_type
16 : USE cp_control_types, ONLY: dft_control_type,&
17 : rtp_control_type
18 : USE cp_dbcsr_api, ONLY: &
19 : dbcsr_add, dbcsr_convert_offsets_to_sizes, dbcsr_copy, dbcsr_create, &
20 : dbcsr_deallocate_matrix, dbcsr_desymmetrize, dbcsr_distribution_type, dbcsr_filter, &
21 : dbcsr_get_info, dbcsr_get_occupation, dbcsr_init_p, dbcsr_iterator_blocks_left, &
22 : dbcsr_iterator_next_block, dbcsr_iterator_start, dbcsr_iterator_stop, dbcsr_iterator_type, &
23 : dbcsr_multiply, dbcsr_p_type, dbcsr_scale, dbcsr_set, dbcsr_type, &
24 : dbcsr_type_antisymmetric, dbcsr_type_no_symmetry, dbcsr_type_symmetric
25 : USE cp_dbcsr_contrib, ONLY: dbcsr_checksum,&
26 : dbcsr_trace
27 : USE cp_dbcsr_cp2k_link, ONLY: cp_dbcsr_alloc_block_from_nbl
28 : USE cp_dbcsr_io, ONLY: dbcsr_binary_write
29 : USE cp_dbcsr_operations, ONLY: cp_dbcsr_sm_fm_multiply,&
30 : dbcsr_allocate_matrix_set,&
31 : dbcsr_deallocate_matrix_set
32 : USE cp_fm_basic_linalg, ONLY: cp_fm_scale_and_add
33 : USE cp_fm_struct, ONLY: cp_fm_struct_create,&
34 : cp_fm_struct_double,&
35 : cp_fm_struct_release,&
36 : cp_fm_struct_type
37 : USE cp_fm_types, ONLY: cp_fm_create,&
38 : cp_fm_get_info,&
39 : cp_fm_release,&
40 : cp_fm_set_all,&
41 : cp_fm_type
42 : USE cp_log_handling, ONLY: cp_get_default_logger,&
43 : cp_logger_get_default_io_unit,&
44 : cp_logger_get_default_unit_nr,&
45 : cp_logger_type,&
46 : cp_to_string
47 : USE cp_output_handling, ONLY: cp_iter_string,&
48 : cp_p_file,&
49 : cp_print_key_finished_output,&
50 : cp_print_key_should_output,&
51 : cp_print_key_unit_nr,&
52 : cp_printkey_is_on
53 : USE cp_realspace_grid_cube, ONLY: cp_pw_to_cube
54 : USE efield_utils, ONLY: make_field
55 : USE greenx_interface, ONLY: greenx_refine_ft
56 : USE input_constants, ONLY: ehrenfest,&
57 : real_time_propagation
58 : USE input_section_types, ONLY: section_get_ivals,&
59 : section_vals_get_subs_vals,&
60 : section_vals_type
61 : USE kahan_sum, ONLY: accurate_sum
62 : USE kinds, ONLY: default_path_length,&
63 : dp
64 : USE machine, ONLY: m_flush
65 : USE mathconstants, ONLY: twopi
66 : USE message_passing, ONLY: mp_comm_type
67 : USE parallel_gemm_api, ONLY: parallel_gemm
68 : USE particle_list_types, ONLY: particle_list_type
69 : USE particle_methods, ONLY: get_particle_set
70 : USE particle_types, ONLY: particle_type
71 : USE physcon, ONLY: evolt,&
72 : femtoseconds
73 : USE pw_env_types, ONLY: pw_env_get,&
74 : pw_env_type
75 : USE pw_methods, ONLY: pw_zero
76 : USE pw_pool_types, ONLY: pw_pool_type
77 : USE pw_types, ONLY: pw_c1d_gs_type,&
78 : pw_r3d_rs_type
79 : USE qs_energy_types, ONLY: qs_energy_type
80 : USE qs_environment_types, ONLY: get_qs_env,&
81 : qs_environment_type
82 : USE qs_kind_types, ONLY: get_qs_kind_set,&
83 : qs_kind_type
84 : USE qs_linres_current, ONLY: calculate_jrho_resp
85 : USE qs_linres_types, ONLY: current_env_type
86 : USE qs_mo_io, ONLY: write_rt_mos_to_restart
87 : USE qs_moments, ONLY: build_local_moment_matrix
88 : USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type
89 : USE qs_operators_ao, ONLY: build_lin_mom_matrix
90 : USE qs_rho_types, ONLY: qs_rho_get,&
91 : qs_rho_type
92 : USE qs_scf_post_gpw, ONLY: qs_scf_post_moments,&
93 : write_mo_dependent_results,&
94 : write_mo_free_results
95 : USE qs_scf_post_tb, ONLY: scf_post_calculation_tb
96 : USE qs_scf_types, ONLY: qs_scf_env_type
97 : USE qs_subsys_types, ONLY: qs_subsys_get,&
98 : qs_subsys_type
99 : USE rt_projection_mo_utils, ONLY: compute_and_write_proj_mo
100 : USE rt_propagation_ft, ONLY: multi_fft
101 : USE rt_propagation_types, ONLY: get_rtp,&
102 : rt_prop_type
103 : USE rt_propagation_utils, ONLY: write_rtp_mo_cubes,&
104 : write_rtp_mos_to_output_unit
105 : USE rt_propagation_velocity_gauge, ONLY: velocity_gauge_nl_commutator
106 : #include "../base/base_uses.f90"
107 :
108 : IMPLICIT NONE
109 :
110 : PRIVATE
111 :
112 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'rt_propagation_output'
113 :
114 : PUBLIC :: rt_prop_output, &
115 : rt_convergence, &
116 : rt_convergence_density, &
117 : report_density_occupation, &
118 : print_moments, &
119 : calc_local_moment, &
120 : print_ft, &
121 : print_rt_file
122 :
123 : INTEGER, PARAMETER, PUBLIC :: rt_file_comp_both = 0, &
124 : rt_file_comp_real = 1, &
125 : rt_file_comp_imag = 2
126 :
127 : CONTAINS
128 :
129 : ! **************************************************************************************************
130 : !> \brief ...
131 : !> \param qs_env ...
132 : !> \param run_type ...
133 : !> \param delta_iter ...
134 : !> \param used_time ...
135 : ! **************************************************************************************************
136 2754 : SUBROUTINE rt_prop_output(qs_env, run_type, delta_iter, used_time)
137 : TYPE(qs_environment_type), POINTER :: qs_env
138 : INTEGER, INTENT(in) :: run_type
139 : REAL(dp), INTENT(in), OPTIONAL :: delta_iter, used_time
140 :
141 : INTEGER :: i, n_electrons, n_proj, natom, nspin, &
142 : output_unit, spin, unit_nr
143 2754 : INTEGER, ALLOCATABLE, DIMENSION(:) :: first_sgf, last_sgf
144 2754 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: row_blk_sizes
145 : INTEGER, DIMENSION(2) :: nelectron_spin
146 : LOGICAL :: new_file
147 : REAL(dp) :: orthonormality, strace, tot_rho_r, trace
148 : REAL(kind=dp), DIMENSION(3) :: field, reference_point, vec_pot
149 2754 : REAL(KIND=dp), DIMENSION(:), POINTER :: qs_tot_rho_r
150 2754 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: j_int
151 2754 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
152 : TYPE(cell_type), POINTER :: cell
153 2754 : TYPE(cp_fm_type), DIMENSION(:), POINTER :: mos_new
154 : TYPE(cp_logger_type), POINTER :: logger
155 : TYPE(dbcsr_distribution_type), POINTER :: dbcsr_dist
156 2754 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_cim, matrix_cre, matrix_s, P_im, &
157 2754 : P_re, p_xyz, rho_new
158 : TYPE(dbcsr_type), POINTER :: tmp_ao
159 : TYPE(dft_control_type), POINTER :: dft_control
160 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
161 2754 : POINTER :: sab_all, sab_orb, sap_ppnl
162 2754 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
163 2754 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
164 : TYPE(qs_rho_type), POINTER :: rho
165 : TYPE(rt_prop_type), POINTER :: rtp
166 : TYPE(section_vals_type), POINTER :: dft_section, input, rtp_section
167 :
168 2754 : NULLIFY (logger, dft_control)
169 :
170 5508 : logger => cp_get_default_logger()
171 : CALL get_qs_env(qs_env, &
172 : rtp=rtp, &
173 : matrix_s=matrix_s, &
174 : input=input, &
175 : rho=rho, &
176 : particle_set=particle_set, &
177 : atomic_kind_set=atomic_kind_set, &
178 : qs_kind_set=qs_kind_set, &
179 : dft_control=dft_control, sab_all=sab_all, sab_orb=sab_orb, &
180 : sap_ppnl=sap_ppnl, cell=cell, &
181 2754 : dbcsr_dist=dbcsr_dist, nelectron_spin=nelectron_spin)
182 :
183 2754 : rtp_section => section_vals_get_subs_vals(input, "DFT%REAL_TIME_PROPAGATION")
184 :
185 2754 : CALL get_qs_kind_set(qs_kind_set, nelectron=n_electrons)
186 2754 : n_electrons = n_electrons - dft_control%charge
187 :
188 2754 : CALL qs_rho_get(rho_struct=rho, tot_rho_r=qs_tot_rho_r)
189 :
190 2754 : tot_rho_r = accurate_sum(qs_tot_rho_r)
191 :
192 : output_unit = cp_print_key_unit_nr(logger, rtp_section, "PRINT%PROGRAM_RUN_INFO", &
193 2754 : extension=".scfLog")
194 :
195 2754 : IF (output_unit > 0) THEN
196 : WRITE (output_unit, FMT="(/,(T3,A,T40,I5))") &
197 1377 : "Information at iteration step:", rtp%iter
198 : WRITE (UNIT=output_unit, FMT="((T3,A,T41,2F20.10))") &
199 1377 : "Total electronic density (r-space): ", &
200 1377 : tot_rho_r, &
201 : tot_rho_r + &
202 2754 : REAL(n_electrons, dp)
203 : WRITE (UNIT=output_unit, FMT="((T3,A,T59,F22.14))") &
204 1377 : "Total energy:", rtp%energy_new
205 1377 : IF (run_type == ehrenfest) THEN
206 : WRITE (UNIT=output_unit, FMT="((T3,A,T61,F20.14))") &
207 752 : "Energy difference to previous iteration step:", rtp%energy_new - rtp%energy_old
208 : END IF
209 1377 : IF (run_type == real_time_propagation) THEN
210 : WRITE (UNIT=output_unit, FMT="((T3,A,T61,F20.14))") &
211 625 : "Energy difference to initial state:", rtp%energy_new - rtp%energy_old
212 : END IF
213 1377 : IF (PRESENT(delta_iter)) THEN
214 : WRITE (UNIT=output_unit, FMT="((T3,A,T61,E20.6))") &
215 1377 : "Convergence:", delta_iter
216 : END IF
217 1377 : IF (rtp%converged) THEN
218 364 : IF (run_type == real_time_propagation) THEN
219 : WRITE (UNIT=output_unit, FMT="((T3,A,T61,F12.2))") &
220 188 : "Time needed for propagation:", used_time
221 : END IF
222 : WRITE (UNIT=output_unit, FMT="(/,(T3,A,3X,F16.14))") &
223 364 : "CONVERGENCE REACHED", rtp%energy_new - rtp%energy_old
224 : END IF
225 : END IF
226 :
227 2754 : IF (rtp%converged) THEN
228 728 : IF (.NOT. rtp%linear_scaling) THEN
229 544 : CALL get_rtp(rtp=rtp, mos_new=mos_new)
230 : CALL rt_calculate_orthonormality(orthonormality, &
231 544 : mos_new, matrix_s(1)%matrix)
232 544 : IF (output_unit > 0) THEN
233 : WRITE (output_unit, FMT="(/,(T3,A,T60,F20.10))") &
234 272 : "Max deviation from orthonormalization:", orthonormality
235 : END IF
236 : END IF
237 : END IF
238 :
239 2754 : IF (output_unit > 0) THEN
240 1377 : CALL m_flush(output_unit)
241 : END IF
242 : CALL cp_print_key_finished_output(output_unit, logger, rtp_section, &
243 2754 : "PRINT%PROGRAM_RUN_INFO")
244 :
245 2754 : IF (rtp%converged) THEN
246 728 : dft_section => section_vals_get_subs_vals(input, "DFT")
247 728 : IF (BTEST(cp_print_key_should_output(logger%iter_info, &
248 : dft_section, "REAL_TIME_PROPAGATION%PRINT%FIELD"), cp_p_file)) THEN
249 30 : CALL print_field_applied(qs_env, dft_section)
250 : END IF
251 728 : CALL make_moment(qs_env)
252 728 : IF (BTEST(cp_print_key_should_output(logger%iter_info, &
253 : dft_section, "REAL_TIME_PROPAGATION%PRINT%E_CONSTITUENTS"), cp_p_file)) THEN
254 4 : CALL print_rtp_energy_components(qs_env, dft_section)
255 : END IF
256 728 : IF (.NOT. dft_control%qs_control%dftb) THEN
257 608 : CALL write_available_results(qs_env=qs_env, rtp=rtp)
258 : END IF
259 :
260 728 : IF (rtp%linear_scaling) THEN
261 184 : CALL get_rtp(rtp=rtp, rho_new=rho_new)
262 :
263 : ! Probably have to rebuild the moment matrix, since atoms can also move, in principle
264 184 : IF (dft_control%rtp_control%save_local_moments) THEN
265 : ! Save the field value
266 36 : IF (dft_control%apply_efield_field) THEN
267 0 : CALL make_field(dft_control, field, qs_env%sim_step, qs_env%sim_time)
268 0 : rtp%fields(:, rtp%istep + rtp%i_start + 1) = CMPLX(field(:), 0.0, kind=dp)
269 : END IF
270 36 : IF (.NOT. dft_control%rtp_control%fixed_ions) THEN
271 0 : CALL build_local_moment_matrix(qs_env, rtp%local_moments, 1, reference_point)
272 : END IF
273 : ! TODO : Is symmetric rho possible?
274 : ! Spin + complex parts
275 : ! Extensions setup
276 : CALL calc_local_moment(rtp%local_moments, rho_new, &
277 36 : rtp%local_moments_work, rtp%moments(:, :, rtp%istep + rtp%i_start + 1))
278 : ! Time 1 is zero (start) time
279 36 : rtp%times(rtp%istep + rtp%i_start + 1) = qs_env%sim_time
280 36 : output_unit = cp_logger_get_default_io_unit(logger)
281 : CALL print_moments(section_vals_get_subs_vals(rtp_section, "PRINT%MOMENTS"), output_unit, &
282 36 : rtp%moments(:, :, rtp%istep + rtp%i_start + 1), qs_env%sim_time, rtp%track_imag_density)
283 : END IF
284 :
285 184 : IF (BTEST(cp_print_key_should_output(logger%iter_info, &
286 : dft_section, "REAL_TIME_PROPAGATION%PRINT%RESTART"), cp_p_file)) THEN
287 98 : CALL write_rt_p_to_restart(rho_new, .FALSE.)
288 : END IF
289 184 : IF (BTEST(cp_print_key_should_output(logger%iter_info, &
290 : dft_section, "REAL_TIME_PROPAGATION%PRINT%RESTART_HISTORY"), cp_p_file)) THEN
291 2 : CALL write_rt_p_to_restart(rho_new, .TRUE.)
292 : END IF
293 184 : IF (.NOT. dft_control%qs_control%dftb) THEN
294 : !Not sure if these things could also work with dftb or not
295 184 : IF (BTEST(cp_print_key_should_output(logger%iter_info, &
296 : dft_section, "REAL_TIME_PROPAGATION%PRINT%CURRENT"), cp_p_file)) THEN
297 64 : DO spin = 1, SIZE(rho_new)/2
298 64 : CALL rt_current(qs_env, rho_new(2*spin)%matrix, dft_section, spin, SIZE(rho_new)/2)
299 : END DO
300 : END IF
301 : END IF
302 : ELSE
303 544 : CALL get_rtp(rtp=rtp, mos_new=mos_new)
304 544 : IF (.NOT. dft_control%qs_control%dftb .AND. .NOT. dft_control%qs_control%xtb) THEN
305 364 : IF (rtp%track_imag_density) THEN
306 286 : NULLIFY (P_im, p_xyz)
307 286 : CALL dbcsr_allocate_matrix_set(p_xyz, 3)
308 :
309 : ! Linear momentum operator
310 : ! prepare for allocation
311 286 : natom = SIZE(particle_set, 1)
312 858 : ALLOCATE (first_sgf(natom))
313 572 : ALLOCATE (last_sgf(natom))
314 : CALL get_particle_set(particle_set, qs_kind_set, &
315 : first_sgf=first_sgf, &
316 286 : last_sgf=last_sgf)
317 572 : ALLOCATE (row_blk_sizes(natom))
318 286 : CALL dbcsr_convert_offsets_to_sizes(first_sgf, row_blk_sizes, last_sgf)
319 286 : DEALLOCATE (first_sgf)
320 286 : DEALLOCATE (last_sgf)
321 :
322 286 : ALLOCATE (p_xyz(1)%matrix)
323 : CALL dbcsr_create(matrix=p_xyz(1)%matrix, &
324 : name="p_xyz", &
325 : dist=dbcsr_dist, matrix_type=dbcsr_type_antisymmetric, &
326 : row_blk_size=row_blk_sizes, col_blk_size=row_blk_sizes, &
327 286 : mutable_work=.TRUE.)
328 286 : CALL cp_dbcsr_alloc_block_from_nbl(p_xyz(1)%matrix, sab_orb)
329 286 : CALL dbcsr_set(p_xyz(1)%matrix, 0.0_dp)
330 858 : DO i = 2, 3
331 572 : ALLOCATE (p_xyz(i)%matrix)
332 572 : CALL dbcsr_copy(p_xyz(i)%matrix, p_xyz(1)%matrix, "p_xyz-"//TRIM(ADJUSTL(cp_to_string(i))))
333 858 : CALL dbcsr_set(p_xyz(i)%matrix, 0.0_dp)
334 : END DO
335 286 : CALL build_lin_mom_matrix(qs_env, p_xyz)
336 286 : DEALLOCATE (row_blk_sizes)
337 :
338 286 : nspin = SIZE(mos_new)/2
339 286 : CALL qs_rho_get(rho, rho_ao_im=P_im)
340 858 : ALLOCATE (j_int(nspin, 3))
341 2098 : j_int = 0.0_dp
342 :
343 286 : NULLIFY (tmp_ao)
344 286 : CALL dbcsr_init_p(tmp_ao)
345 286 : CALL dbcsr_create(tmp_ao, template=matrix_s(1)%matrix, matrix_type=dbcsr_type_no_symmetry, name="tmp")
346 286 : CALL cp_dbcsr_alloc_block_from_nbl(tmp_ao, sab_all)
347 286 : CALL dbcsr_set(tmp_ao, 0.0_dp)
348 :
349 1144 : DO i = 1, 3
350 858 : strace = 0.0_dp
351 2098 : DO spin = 1, nspin
352 954 : CALL dbcsr_set(tmp_ao, 0.0_dp)
353 : CALL dbcsr_multiply("T", "N", 1.0_dp, P_im(spin)%matrix, p_xyz(i)%matrix, &
354 954 : 0.0_dp, tmp_ao)
355 954 : CALL dbcsr_trace(tmp_ao, trace)
356 954 : strace = strace + trace
357 : ! dft_control%rtp_control%vec_pot(1)
358 1812 : j_int(spin, i) = -trace + dft_control%rtp_control%vec_pot(i)*nelectron_spin(spin)
359 : !! j_int(spin, i) = strace
360 : END DO
361 : END DO
362 :
363 : ! Nonlocal pseudopotential correction to the integrated current:
364 : ! the velocity operator contains i*[Vnl, r], which with C_d = [Vnl~, r_d]
365 : ! contributes j_nl(d) = -Tr[P_re^T C_im(d)] - Tr[P_im^T C_re(d)].
366 : ! Absent in all-electron calculations (no nonlocal projectors).
367 286 : IF (ASSOCIATED(sap_ppnl)) THEN
368 : ! the commutator is taken with the gauge-transformed Vnl only if
369 : ! the propagation Hamiltonian uses it
370 144 : vec_pot = 0.0_dp
371 144 : IF (dft_control%rtp_control%velocity_gauge .AND. &
372 : dft_control%rtp_control%nl_gauge_transform) THEN
373 120 : vec_pot = dft_control%rtp_control%vec_pot
374 : END IF
375 :
376 144 : NULLIFY (matrix_cre, matrix_cim)
377 144 : CALL dbcsr_allocate_matrix_set(matrix_cre, 3)
378 144 : CALL dbcsr_allocate_matrix_set(matrix_cim, 3)
379 576 : DO i = 1, 3
380 432 : CALL dbcsr_init_p(matrix_cre(i)%matrix)
381 : CALL dbcsr_create(matrix_cre(i)%matrix, template=matrix_s(1)%matrix, &
382 : matrix_type=dbcsr_type_antisymmetric, &
383 432 : name="nl commutator real part")
384 432 : CALL cp_dbcsr_alloc_block_from_nbl(matrix_cre(i)%matrix, sab_orb)
385 432 : CALL dbcsr_set(matrix_cre(i)%matrix, 0.0_dp)
386 432 : CALL dbcsr_init_p(matrix_cim(i)%matrix)
387 : CALL dbcsr_create(matrix_cim(i)%matrix, template=matrix_s(1)%matrix, &
388 : matrix_type=dbcsr_type_symmetric, &
389 432 : name="nl commutator imaginary part")
390 432 : CALL cp_dbcsr_alloc_block_from_nbl(matrix_cim(i)%matrix, sab_orb)
391 576 : CALL dbcsr_set(matrix_cim(i)%matrix, 0.0_dp)
392 : END DO
393 :
394 144 : CALL velocity_gauge_nl_commutator(qs_env, matrix_cre, matrix_cim, vec_pot)
395 :
396 144 : CALL qs_rho_get(rho, rho_ao=P_re)
397 576 : DO i = 1, 3
398 1056 : DO spin = 1, nspin
399 480 : CALL dbcsr_set(tmp_ao, 0.0_dp)
400 : CALL dbcsr_multiply("T", "N", 1.0_dp, P_re(spin)%matrix, matrix_cim(i)%matrix, &
401 480 : 0.0_dp, tmp_ao)
402 480 : CALL dbcsr_trace(tmp_ao, trace)
403 480 : j_int(spin, i) = j_int(spin, i) - trace
404 480 : CALL dbcsr_set(tmp_ao, 0.0_dp)
405 : CALL dbcsr_multiply("T", "N", 1.0_dp, P_im(spin)%matrix, matrix_cre(i)%matrix, &
406 480 : 0.0_dp, tmp_ao)
407 480 : CALL dbcsr_trace(tmp_ao, trace)
408 1392 : j_int(spin, i) = j_int(spin, i) - trace
409 : END DO
410 : END DO
411 :
412 144 : CALL dbcsr_deallocate_matrix_set(matrix_cre)
413 144 : CALL dbcsr_deallocate_matrix_set(matrix_cim)
414 : END IF
415 :
416 286 : IF (BTEST(cp_print_key_should_output(logger%iter_info, &
417 : dft_section, "REAL_TIME_PROPAGATION%PRINT%CURRENT_INT"), cp_p_file)) THEN
418 :
419 16 : output_unit = cp_logger_get_default_io_unit(logger)
420 : unit_nr = cp_print_key_unit_nr(logger, dft_section, &
421 16 : "REAL_TIME_PROPAGATION%PRINT%CURRENT_INT", extension=".dat", is_new_file=new_file)
422 :
423 16 : IF (output_unit > 0) THEN
424 : WRITE (UNIT=output_unit, FMT="(T2,A,E24.16)") &
425 62 : "RTP_CURRENT| CheckSum j_int=", SQRT(SUM(j_int**2))
426 8 : IF (new_file) THEN
427 4 : IF (nspin == 2) THEN
428 : WRITE (UNIT=unit_nr, FMT='("#",5X,A,4X,A,2X,A,2(10X,A),4X,A,2(10X,A))') &
429 1 : "Step Nr.", "Time[fs]", "ALPHA jint[X]", "jint[Y]", "jint[Z]", &
430 2 : "BETA jint[X]", "jint[Y]", "jint[Z]"
431 : ELSE
432 3 : WRITE (UNIT=unit_nr, FMT='("#",5X,A,4X,A,8X,A,2(10X,A))') "Step Nr.", "Time[fs]", &
433 6 : "jint[X]", "jint[Y]", "jint[Z]"
434 : END IF
435 : END IF
436 :
437 8 : IF (nspin == 2) THEN
438 2 : WRITE (UNIT=unit_nr, FMT="(I10,F16.6,6(F16.8,1X))") qs_env%sim_step, qs_env%sim_time*femtoseconds, &
439 16 : j_int(1, 1:3), j_int(2, 1:3)
440 : ELSE
441 6 : WRITE (UNIT=unit_nr, FMT="(I10,F16.6,3(F16.8,1X))") qs_env%sim_step, qs_env%sim_time*femtoseconds, &
442 30 : j_int(1, 1:3)
443 : END IF
444 : END IF
445 : CALL cp_print_key_finished_output(unit_nr, logger, dft_section, &
446 16 : "REAL_TIME_PROPAGATION%PRINT%CURRENT_INT")
447 : END IF
448 286 : DEALLOCATE (j_int)
449 :
450 286 : IF (BTEST(cp_print_key_should_output(logger%iter_info, &
451 : dft_section, "REAL_TIME_PROPAGATION%PRINT%CURRENT"), cp_p_file)) THEN
452 24 : DO spin = 1, nspin
453 24 : CALL rt_current(qs_env, P_im(spin)%matrix, dft_section, spin, nspin)
454 : END DO
455 : END IF
456 286 : CALL dbcsr_deallocate_matrix(tmp_ao)
457 286 : CALL dbcsr_deallocate_matrix_set(p_xyz)
458 : END IF
459 :
460 : ! projection of molecular orbitals
461 364 : IF (dft_control%rtp_control%is_proj_mo) THEN
462 100 : DO n_proj = 1, SIZE(dft_control%rtp_control%proj_mo_list)
463 : CALL compute_and_write_proj_mo(qs_env, mos_new, &
464 100 : dft_control%rtp_control%proj_mo_list(n_proj)%proj_mo, n_proj)
465 : END DO
466 : END IF
467 : END IF
468 : CALL write_rt_mos_to_restart(qs_env%mos, mos_new, particle_set, &
469 544 : dft_section, qs_kind_set)
470 : END IF
471 : END IF
472 :
473 2754 : rtp%energy_old = rtp%energy_new
474 :
475 2754 : IF (.NOT. rtp%converged .AND. rtp%iter >= dft_control%rtp_control%max_iter) THEN
476 : CALL cp_abort(__LOCATION__, "EMD did not converge, either increase MAX_ITER "// &
477 0 : "or use a smaller TIMESTEP")
478 : END IF
479 :
480 5508 : END SUBROUTINE rt_prop_output
481 :
482 : ! **************************************************************************************************
483 : !> \brief computes the effective orthonormality of a set of mos given an s-matrix
484 : !> orthonormality is the max deviation from unity of the C^T S C
485 : !> \param orthonormality ...
486 : !> \param mos_new ...
487 : !> \param matrix_s ...
488 : !> \author Florian Schiffmann (02.09)
489 : ! **************************************************************************************************
490 544 : SUBROUTINE rt_calculate_orthonormality(orthonormality, mos_new, matrix_s)
491 : REAL(KIND=dp), INTENT(out) :: orthonormality
492 : TYPE(cp_fm_type), DIMENSION(:), POINTER :: mos_new
493 : TYPE(dbcsr_type), OPTIONAL, POINTER :: matrix_s
494 :
495 : CHARACTER(len=*), PARAMETER :: routineN = 'rt_calculate_orthonormality'
496 :
497 : INTEGER :: handle, i, im, ispin, j, k, n, &
498 : ncol_local, nrow_local, nspin, re
499 544 : INTEGER, DIMENSION(:), POINTER :: col_indices, row_indices
500 : REAL(KIND=dp) :: alpha, max_alpha, max_beta
501 : TYPE(cp_fm_struct_type), POINTER :: tmp_fm_struct
502 : TYPE(cp_fm_type) :: overlap_re, svec_im, svec_re
503 :
504 544 : NULLIFY (tmp_fm_struct)
505 :
506 544 : CALL timeset(routineN, handle)
507 :
508 544 : nspin = SIZE(mos_new)/2
509 544 : max_alpha = 0.0_dp
510 544 : max_beta = 0.0_dp
511 1198 : DO ispin = 1, nspin
512 654 : re = ispin*2 - 1
513 654 : im = ispin*2
514 : ! get S*C
515 654 : CALL cp_fm_create(svec_re, mos_new(im)%matrix_struct)
516 654 : CALL cp_fm_create(svec_im, mos_new(im)%matrix_struct)
517 : CALL cp_fm_get_info(mos_new(im), &
518 654 : nrow_global=n, ncol_global=k)
519 : CALL cp_dbcsr_sm_fm_multiply(matrix_s, mos_new(re), &
520 654 : svec_re, k)
521 : CALL cp_dbcsr_sm_fm_multiply(matrix_s, mos_new(im), &
522 654 : svec_im, k)
523 :
524 : ! get C^T (S*C)
525 : CALL cp_fm_struct_create(tmp_fm_struct, nrow_global=k, ncol_global=k, &
526 : para_env=mos_new(re)%matrix_struct%para_env, &
527 654 : context=mos_new(re)%matrix_struct%context)
528 654 : CALL cp_fm_create(overlap_re, tmp_fm_struct)
529 :
530 654 : CALL cp_fm_struct_release(tmp_fm_struct)
531 :
532 : CALL parallel_gemm('T', 'N', k, k, n, 1.0_dp, mos_new(re), &
533 654 : svec_re, 0.0_dp, overlap_re)
534 : CALL parallel_gemm('T', 'N', k, k, n, 1.0_dp, mos_new(im), &
535 654 : svec_im, 1.0_dp, overlap_re)
536 :
537 654 : CALL cp_fm_release(svec_re)
538 654 : CALL cp_fm_release(svec_im)
539 :
540 : CALL cp_fm_get_info(overlap_re, nrow_local=nrow_local, ncol_local=ncol_local, &
541 654 : row_indices=row_indices, col_indices=col_indices)
542 1945 : DO i = 1, nrow_local
543 11676 : DO j = 1, ncol_local
544 9731 : alpha = overlap_re%local_data(i, j)
545 9731 : IF (row_indices(i) == col_indices(j)) alpha = alpha - 1.0_dp
546 11022 : max_alpha = MAX(max_alpha, ABS(alpha))
547 : END DO
548 : END DO
549 3160 : CALL cp_fm_release(overlap_re)
550 : END DO
551 544 : CALL mos_new(1)%matrix_struct%para_env%max(max_alpha)
552 544 : CALL mos_new(1)%matrix_struct%para_env%max(max_beta)
553 544 : orthonormality = max_alpha
554 :
555 544 : CALL timestop(handle)
556 :
557 544 : END SUBROUTINE rt_calculate_orthonormality
558 :
559 : ! **************************************************************************************************
560 : !> \brief computes the convergence criterion for RTP and EMD
561 : !> \param rtp ...
562 : !> \param matrix_s Overlap matrix without the derivatives
563 : !> \param delta_mos ...
564 : !> \param delta_eps ...
565 : !> \author Florian Schiffmann (02.09)
566 : ! **************************************************************************************************
567 :
568 1940 : SUBROUTINE rt_convergence(rtp, matrix_s, delta_mos, delta_eps)
569 : TYPE(rt_prop_type), POINTER :: rtp
570 : TYPE(dbcsr_type), POINTER :: matrix_s
571 : TYPE(cp_fm_type), DIMENSION(:), INTENT(IN) :: delta_mos
572 : REAL(dp), INTENT(out) :: delta_eps
573 :
574 : CHARACTER(len=*), PARAMETER :: routineN = 'rt_convergence'
575 : REAL(KIND=dp), PARAMETER :: one = 1.0_dp, zero = 0.0_dp
576 :
577 : INTEGER :: handle, i, icol, im, ispin, j, lcol, &
578 : lrow, nao, newdim, nmo, nspin, re
579 : LOGICAL :: double_col, double_row
580 : REAL(KIND=dp) :: alpha, max_alpha
581 : TYPE(cp_fm_struct_type), POINTER :: newstruct, newstruct1, tmp_fm_struct
582 : TYPE(cp_fm_type) :: work, work1, work2
583 1940 : TYPE(cp_fm_type), DIMENSION(:), POINTER :: mos_new
584 :
585 1940 : NULLIFY (tmp_fm_struct)
586 :
587 1940 : CALL timeset(routineN, handle)
588 :
589 1940 : CALL get_rtp(rtp=rtp, mos_new=mos_new)
590 :
591 1940 : nspin = SIZE(delta_mos)/2
592 1940 : max_alpha = 0.0_dp
593 :
594 6688 : DO i = 1, SIZE(mos_new)
595 6688 : CALL cp_fm_scale_and_add(-one, delta_mos(i), one, mos_new(i))
596 : END DO
597 :
598 4314 : DO ispin = 1, nspin
599 2374 : re = ispin*2 - 1
600 2374 : im = ispin*2
601 :
602 2374 : double_col = .TRUE.
603 2374 : double_row = .FALSE.
604 : CALL cp_fm_struct_double(newstruct, &
605 : delta_mos(re)%matrix_struct, &
606 : delta_mos(re)%matrix_struct%context, &
607 : double_col, &
608 2374 : double_row)
609 :
610 2374 : CALL cp_fm_create(work, matrix_struct=newstruct)
611 2374 : CALL cp_fm_create(work1, matrix_struct=newstruct)
612 :
613 : CALL cp_fm_get_info(delta_mos(re), ncol_local=lcol, ncol_global=nmo, &
614 2374 : nrow_global=nao)
615 2374 : CALL cp_fm_get_info(work, ncol_global=newdim)
616 :
617 2374 : CALL cp_fm_set_all(work, zero, zero)
618 :
619 11900 : DO icol = 1, lcol
620 111008 : work%local_data(:, icol) = delta_mos(re)%local_data(:, icol)
621 113382 : work%local_data(:, icol + lcol) = delta_mos(im)%local_data(:, icol)
622 : END DO
623 :
624 2374 : CALL cp_dbcsr_sm_fm_multiply(matrix_s, work, work1, ncol=newdim)
625 :
626 2374 : CALL cp_fm_release(work)
627 :
628 : CALL cp_fm_struct_create(tmp_fm_struct, nrow_global=nmo, ncol_global=nmo, &
629 : para_env=delta_mos(re)%matrix_struct%para_env, &
630 2374 : context=delta_mos(re)%matrix_struct%context)
631 : CALL cp_fm_struct_double(newstruct1, &
632 : tmp_fm_struct, &
633 : delta_mos(re)%matrix_struct%context, &
634 : double_col, &
635 2374 : double_row)
636 :
637 2374 : CALL cp_fm_create(work, matrix_struct=newstruct1)
638 2374 : CALL cp_fm_create(work2, matrix_struct=newstruct1)
639 :
640 : CALL parallel_gemm("T", "N", nmo, newdim, nao, one, delta_mos(re), &
641 2374 : work1, zero, work)
642 :
643 : CALL parallel_gemm("T", "N", nmo, newdim, nao, one, delta_mos(im), &
644 2374 : work1, zero, work2)
645 :
646 2374 : CALL cp_fm_get_info(work, nrow_local=lrow)
647 7137 : DO i = 1, lrow
648 44240 : DO j = 1, lcol
649 : alpha = SQRT((work%local_data(i, j) + work2%local_data(i, j + lcol))**2 + &
650 37103 : (work%local_data(i, j + lcol) - work2%local_data(i, j))**2)
651 41866 : max_alpha = MAX(max_alpha, ABS(alpha))
652 : END DO
653 : END DO
654 :
655 2374 : CALL cp_fm_release(work)
656 2374 : CALL cp_fm_release(work1)
657 2374 : CALL cp_fm_release(work2)
658 2374 : CALL cp_fm_struct_release(tmp_fm_struct)
659 2374 : CALL cp_fm_struct_release(newstruct)
660 11436 : CALL cp_fm_struct_release(newstruct1)
661 :
662 : END DO
663 :
664 1940 : CALL delta_mos(1)%matrix_struct%para_env%max(max_alpha)
665 1940 : delta_eps = SQRT(max_alpha)
666 :
667 1940 : CALL timestop(handle)
668 :
669 1940 : END SUBROUTINE rt_convergence
670 :
671 : ! **************************************************************************************************
672 : !> \brief computes the convergence criterion for RTP and EMD based on the density matrix
673 : !> \param rtp ...
674 : !> \param delta_P ...
675 : !> \param delta_eps ...
676 : !> \author Samuel Andermatt (02.14)
677 : ! **************************************************************************************************
678 :
679 1628 : SUBROUTINE rt_convergence_density(rtp, delta_P, delta_eps)
680 :
681 : TYPE(rt_prop_type), POINTER :: rtp
682 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: delta_P
683 : REAL(dp), INTENT(out) :: delta_eps
684 :
685 : CHARACTER(len=*), PARAMETER :: routineN = 'rt_convergence_density'
686 : REAL(KIND=dp), PARAMETER :: one = 1.0_dp, zero = 0.0_dp
687 :
688 : INTEGER :: col_atom, handle, i, ispin, row_atom
689 : REAL(dp) :: alpha, max_alpha
690 814 : REAL(dp), DIMENSION(:, :), POINTER :: block_values
691 : TYPE(dbcsr_iterator_type) :: iter
692 814 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_new
693 : TYPE(dbcsr_type), POINTER :: tmp
694 : TYPE(mp_comm_type) :: group
695 :
696 814 : CALL timeset(routineN, handle)
697 :
698 814 : CALL get_rtp(rtp=rtp, rho_new=rho_new)
699 :
700 2934 : DO i = 1, SIZE(rho_new)
701 2934 : CALL dbcsr_add(delta_P(i)%matrix, rho_new(i)%matrix, one, -one)
702 : END DO
703 : !get the maximum value of delta_P
704 2934 : DO i = 1, SIZE(delta_P)
705 : !square all entries of both matrices
706 2120 : CALL dbcsr_iterator_start(iter, delta_P(i)%matrix)
707 12550 : DO WHILE (dbcsr_iterator_blocks_left(iter))
708 10430 : CALL dbcsr_iterator_next_block(iter, row_atom, col_atom, block_values)
709 808178 : block_values = block_values*block_values
710 : END DO
711 5054 : CALL dbcsr_iterator_stop(iter)
712 : END DO
713 : NULLIFY (tmp)
714 814 : ALLOCATE (tmp)
715 814 : CALL dbcsr_create(tmp, template=delta_P(1)%matrix, matrix_type="N")
716 1874 : DO ispin = 1, SIZE(delta_P)/2
717 1060 : CALL dbcsr_desymmetrize(delta_P(2*ispin - 1)%matrix, tmp)
718 1874 : CALL dbcsr_add(delta_P(2*ispin)%matrix, tmp, one, one)
719 : END DO
720 : !the absolute values are now in the even entries of delta_P
721 814 : max_alpha = zero
722 1874 : DO ispin = 1, SIZE(delta_P)/2
723 1060 : CALL dbcsr_iterator_start(iter, delta_P(2*ispin)%matrix)
724 6372 : DO WHILE (dbcsr_iterator_blocks_left(iter))
725 5312 : CALL dbcsr_iterator_next_block(iter, row_atom, col_atom, block_values)
726 408540 : alpha = MAXVAL(block_values)
727 6372 : IF (alpha > max_alpha) max_alpha = alpha
728 : END DO
729 2934 : CALL dbcsr_iterator_stop(iter)
730 : END DO
731 814 : CALL dbcsr_get_info(delta_P(1)%matrix, group=group)
732 814 : CALL group%max(max_alpha)
733 814 : delta_eps = SQRT(max_alpha)
734 814 : CALL dbcsr_deallocate_matrix(tmp)
735 814 : CALL timestop(handle)
736 :
737 814 : END SUBROUTINE rt_convergence_density
738 :
739 : ! **************************************************************************************************
740 : !> \brief interface to qs_moments. Does only work for nonperiodic dipole
741 : !> \param qs_env ...
742 : !> \author Florian Schiffmann (02.09)
743 : ! **************************************************************************************************
744 :
745 728 : SUBROUTINE make_moment(qs_env)
746 :
747 : TYPE(qs_environment_type), POINTER :: qs_env
748 :
749 : CHARACTER(len=*), PARAMETER :: routineN = 'make_moment'
750 :
751 : INTEGER :: handle, output_unit
752 : TYPE(cp_logger_type), POINTER :: logger
753 : TYPE(dft_control_type), POINTER :: dft_control
754 :
755 728 : CALL timeset(routineN, handle)
756 :
757 728 : NULLIFY (dft_control)
758 :
759 728 : logger => cp_get_default_logger()
760 728 : output_unit = cp_logger_get_default_io_unit(logger)
761 728 : CALL get_qs_env(qs_env, dft_control=dft_control)
762 728 : IF (dft_control%qs_control%dftb) THEN
763 120 : CALL scf_post_calculation_tb(qs_env, "DFTB", .FALSE.)
764 608 : ELSE IF (dft_control%qs_control%xtb) THEN
765 60 : CALL scf_post_calculation_tb(qs_env, "xTB", .FALSE.)
766 : ELSE
767 548 : CALL qs_scf_post_moments(qs_env%input, logger, qs_env, output_unit)
768 : END IF
769 728 : CALL timestop(handle)
770 :
771 728 : END SUBROUTINE make_moment
772 :
773 : ! **************************************************************************************************
774 : !> \brief Reports the sparsity pattern of the complex density matrix
775 : !> \param filter_eps ...
776 : !> \param rho ...
777 : !> \author Samuel Andermatt (09.14)
778 : ! **************************************************************************************************
779 :
780 184 : SUBROUTINE report_density_occupation(filter_eps, rho)
781 :
782 : REAL(KIND=dp) :: filter_eps
783 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho
784 :
785 : CHARACTER(len=*), PARAMETER :: routineN = 'report_density_occupation'
786 :
787 : INTEGER :: handle, i, im, ispin, re, unit_nr
788 : REAL(KIND=dp) :: eps, occ
789 : TYPE(cp_logger_type), POINTER :: logger
790 184 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: tmp
791 :
792 184 : CALL timeset(routineN, handle)
793 :
794 184 : logger => cp_get_default_logger()
795 184 : unit_nr = cp_logger_get_default_io_unit(logger)
796 184 : NULLIFY (tmp)
797 184 : CALL dbcsr_allocate_matrix_set(tmp, SIZE(rho))
798 692 : DO i = 1, SIZE(rho)
799 508 : CALL dbcsr_init_p(tmp(i)%matrix)
800 508 : CALL dbcsr_create(tmp(i)%matrix, template=rho(i)%matrix)
801 692 : CALL dbcsr_copy(tmp(i)%matrix, rho(i)%matrix)
802 : END DO
803 438 : DO ispin = 1, SIZE(rho)/2
804 254 : re = 2*ispin - 1
805 254 : im = 2*ispin
806 254 : eps = MAX(filter_eps, 1.0E-11_dp)
807 2354 : DO WHILE (eps < 1.1_dp)
808 2100 : CALL dbcsr_filter(tmp(re)%matrix, eps)
809 2100 : occ = dbcsr_get_occupation(tmp(re)%matrix)
810 3150 : IF (unit_nr > 0) WRITE (unit_nr, FMT="((T3,A,I1,A,F15.12,A,T61,F20.10))") "Occupation of rho spin ", &
811 2100 : ispin, " eps ", eps, " real: ", occ
812 2100 : eps = eps*10
813 : END DO
814 254 : eps = MAX(filter_eps, 1.0E-11_dp)
815 2538 : DO WHILE (eps < 1.1_dp)
816 2100 : CALL dbcsr_filter(tmp(im)%matrix, eps)
817 2100 : occ = dbcsr_get_occupation(tmp(im)%matrix)
818 3150 : IF (unit_nr > 0) WRITE (unit_nr, FMT="((T3,A,I1,A,F15.12,A,T61,F20.10))") "Occupation of rho spin ", &
819 2100 : ispin, " eps ", eps, " imag: ", occ
820 2100 : eps = eps*10.0_dp
821 : END DO
822 : END DO
823 184 : CALL dbcsr_deallocate_matrix_set(tmp)
824 184 : CALL timestop(handle)
825 :
826 184 : END SUBROUTINE report_density_occupation
827 :
828 : ! **************************************************************************************************
829 : !> \brief Writes the density matrix and the atomic positions to a restart file
830 : !> \param rho_new ...
831 : !> \param history ...
832 : !> \author Samuel Andermatt (09.14)
833 : ! **************************************************************************************************
834 :
835 100 : SUBROUTINE write_rt_p_to_restart(rho_new, history)
836 :
837 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_new
838 : LOGICAL :: history
839 :
840 : CHARACTER(LEN=*), PARAMETER :: routineN = 'write_rt_p_to_restart'
841 :
842 : CHARACTER(LEN=default_path_length) :: file_name, project_name
843 : INTEGER :: handle, im, ispin, re, unit_nr
844 : REAL(KIND=dp) :: cs_pos
845 : TYPE(cp_logger_type), POINTER :: logger
846 :
847 100 : CALL timeset(routineN, handle)
848 100 : logger => cp_get_default_logger()
849 100 : IF (logger%para_env%is_source()) THEN
850 50 : unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
851 : ELSE
852 : unit_nr = -1
853 : END IF
854 :
855 100 : project_name = logger%iter_info%project_name
856 238 : DO ispin = 1, SIZE(rho_new)/2
857 138 : re = 2*ispin - 1
858 138 : im = 2*ispin
859 138 : IF (history) THEN
860 : WRITE (file_name, '(A,I0,A)') &
861 2 : TRIM(project_name)//"_LS_DM_SPIN_RE", ispin, "_"//TRIM(cp_iter_string(logger%iter_info))//"_RESTART.dm"
862 : ELSE
863 136 : WRITE (file_name, '(A,I0,A)') TRIM(project_name)//"_LS_DM_SPIN_RE", ispin, "_RESTART.dm"
864 : END IF
865 138 : cs_pos = dbcsr_checksum(rho_new(re)%matrix, pos=.TRUE.)
866 138 : IF (unit_nr > 0) THEN
867 69 : WRITE (unit_nr, '(T2,A,E20.8)') "Writing restart DM "//TRIM(file_name)//" with checksum: ", cs_pos
868 : END IF
869 138 : CALL dbcsr_binary_write(rho_new(re)%matrix, file_name)
870 138 : IF (history) THEN
871 : WRITE (file_name, '(A,I0,A)') &
872 2 : TRIM(project_name)//"_LS_DM_SPIN_IM", ispin, "_"//TRIM(cp_iter_string(logger%iter_info))//"_RESTART.dm"
873 : ELSE
874 136 : WRITE (file_name, '(A,I0,A)') TRIM(project_name)//"_LS_DM_SPIN_IM", ispin, "_RESTART.dm"
875 : END IF
876 138 : cs_pos = dbcsr_checksum(rho_new(im)%matrix, pos=.TRUE.)
877 138 : IF (unit_nr > 0) THEN
878 69 : WRITE (unit_nr, '(T2,A,E20.8)') "Writing restart DM "//TRIM(file_name)//" with checksum: ", cs_pos
879 : END IF
880 238 : CALL dbcsr_binary_write(rho_new(im)%matrix, file_name)
881 : END DO
882 :
883 100 : CALL timestop(handle)
884 :
885 100 : END SUBROUTINE write_rt_p_to_restart
886 :
887 : ! **************************************************************************************************
888 : !> \brief Collocation of the current and printing of it in a cube file
889 : !> \param qs_env ...
890 : !> \param P_im ...
891 : !> \param dft_section ...
892 : !> \param spin ...
893 : !> \param nspin ...
894 : !> \author Samuel Andermatt (06.15)
895 : ! **************************************************************************************************
896 48 : SUBROUTINE rt_current(qs_env, P_im, dft_section, spin, nspin)
897 : TYPE(qs_environment_type), POINTER :: qs_env
898 : TYPE(dbcsr_type), POINTER :: P_im
899 : TYPE(section_vals_type), POINTER :: dft_section
900 : INTEGER :: spin, nspin
901 :
902 : CHARACTER(len=*), PARAMETER :: routineN = 'rt_current'
903 :
904 : CHARACTER(len=1) :: char_spin
905 : CHARACTER(len=14) :: ext
906 : CHARACTER(len=2) :: sdir
907 : INTEGER :: dir, handle, print_unit
908 48 : INTEGER, DIMENSION(:), POINTER :: stride
909 : LOGICAL :: mpi_io
910 : TYPE(cp_logger_type), POINTER :: logger
911 : TYPE(current_env_type) :: current_env
912 : TYPE(dbcsr_type), POINTER :: tmp, zero
913 : TYPE(particle_list_type), POINTER :: particles
914 : TYPE(pw_c1d_gs_type) :: gs
915 : TYPE(pw_env_type), POINTER :: pw_env
916 : TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
917 : TYPE(pw_r3d_rs_type) :: rs
918 : TYPE(qs_subsys_type), POINTER :: subsys
919 :
920 48 : CALL timeset(routineN, handle)
921 :
922 48 : logger => cp_get_default_logger()
923 48 : CALL get_qs_env(qs_env=qs_env, subsys=subsys, pw_env=pw_env)
924 48 : CALL qs_subsys_get(subsys, particles=particles)
925 48 : CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
926 :
927 48 : NULLIFY (zero, tmp)
928 48 : ALLOCATE (zero, tmp)
929 48 : CALL dbcsr_create(zero, template=P_im)
930 48 : CALL dbcsr_copy(zero, P_im)
931 48 : CALL dbcsr_set(zero, 0.0_dp)
932 48 : CALL dbcsr_create(tmp, template=P_im)
933 48 : CALL dbcsr_copy(tmp, P_im)
934 48 : IF (nspin == 1) THEN
935 32 : CALL dbcsr_scale(tmp, 0.5_dp)
936 : END IF
937 48 : current_env%gauge = -1
938 48 : current_env%gauge_init = .FALSE.
939 48 : CALL auxbas_pw_pool%create_pw(rs)
940 48 : CALL auxbas_pw_pool%create_pw(gs)
941 :
942 : NULLIFY (stride)
943 48 : ALLOCATE (stride(3))
944 :
945 192 : DO dir = 1, 3
946 :
947 144 : CALL pw_zero(rs)
948 144 : CALL pw_zero(gs)
949 :
950 144 : CALL calculate_jrho_resp(zero, tmp, zero, zero, dir, dir, rs, gs, qs_env, current_env, retain_rsgrid=.TRUE.)
951 :
952 576 : stride = section_get_ivals(dft_section, "REAL_TIME_PROPAGATION%PRINT%CURRENT%STRIDE")
953 :
954 144 : IF (dir == 1) THEN
955 48 : sdir = "-x"
956 96 : ELSE IF (dir == 2) THEN
957 48 : sdir = "-y"
958 : ELSE
959 48 : sdir = "-z"
960 : END IF
961 144 : WRITE (char_spin, "(I1)") spin
962 :
963 144 : ext = "-SPIN-"//char_spin//sdir//".cube"
964 144 : mpi_io = .TRUE.
965 : print_unit = cp_print_key_unit_nr(logger, dft_section, "REAL_TIME_PROPAGATION%PRINT%CURRENT", &
966 : extension=ext, file_status="REPLACE", file_action="WRITE", &
967 144 : log_filename=.FALSE., mpi_io=mpi_io)
968 :
969 : CALL cp_pw_to_cube(rs, print_unit, "EMD current", particles=particles, stride=stride, &
970 144 : mpi_io=mpi_io)
971 :
972 : CALL cp_print_key_finished_output(print_unit, logger, dft_section, "REAL_TIME_PROPAGATION%PRINT%CURRENT", &
973 192 : mpi_io=mpi_io)
974 :
975 : END DO
976 :
977 48 : CALL auxbas_pw_pool%give_back_pw(rs)
978 48 : CALL auxbas_pw_pool%give_back_pw(gs)
979 :
980 48 : CALL dbcsr_deallocate_matrix(zero)
981 48 : CALL dbcsr_deallocate_matrix(tmp)
982 :
983 48 : DEALLOCATE (stride)
984 :
985 48 : CALL timestop(handle)
986 :
987 3504 : END SUBROUTINE rt_current
988 :
989 : ! **************************************************************************************************
990 : !> \brief Interface routine to trigger writing of results available from normal
991 : !> SCF. Can write MO-dependent and MO free results (needed for call from
992 : !> the linear scaling code)
993 : !> Update: trigger also some of prints for time-dependent runs
994 : !> \param qs_env ...
995 : !> \param rtp ...
996 : !> \par History
997 : !> 2022-11 Update [Guillaume Le Breton]
998 : ! **************************************************************************************************
999 608 : SUBROUTINE write_available_results(qs_env, rtp)
1000 : TYPE(qs_environment_type), POINTER :: qs_env
1001 : TYPE(rt_prop_type), POINTER :: rtp
1002 :
1003 : CHARACTER(len=*), PARAMETER :: routineN = 'write_available_results'
1004 :
1005 : INTEGER :: handle
1006 : TYPE(qs_scf_env_type), POINTER :: scf_env
1007 :
1008 608 : CALL timeset(routineN, handle)
1009 :
1010 608 : CALL get_qs_env(qs_env, scf_env=scf_env)
1011 608 : IF (rtp%linear_scaling) THEN
1012 184 : CALL write_mo_free_results(qs_env)
1013 : ELSE
1014 424 : CALL write_mo_free_results(qs_env)
1015 424 : CALL write_mo_dependent_results(qs_env, scf_env)
1016 : ! Time-dependent MO print
1017 424 : CALL write_rtp_mos_to_output_unit(qs_env, rtp)
1018 424 : CALL write_rtp_mo_cubes(qs_env, rtp)
1019 : END IF
1020 :
1021 608 : CALL timestop(handle)
1022 :
1023 608 : END SUBROUTINE write_available_results
1024 :
1025 : ! **************************************************************************************************
1026 : !> \brief Print the field applied to the system. Either the electric
1027 : !> field or the vector potential depending on the gauge used
1028 : !> \param qs_env ...
1029 : !> \param dft_section ...
1030 : !> \par History
1031 : !> 2023-01 Created [Guillaume Le Breton]
1032 : ! **************************************************************************************************
1033 30 : SUBROUTINE print_field_applied(qs_env, dft_section)
1034 : TYPE(qs_environment_type), POINTER :: qs_env
1035 : TYPE(section_vals_type), POINTER :: dft_section
1036 :
1037 : CHARACTER(LEN=3), DIMENSION(3) :: rlab
1038 : CHARACTER(LEN=default_path_length) :: filename
1039 : INTEGER :: i, i_step, output_unit, unit_nr
1040 : LOGICAL :: new_file
1041 : REAL(kind=dp) :: field(3)
1042 : TYPE(cp_logger_type), POINTER :: logger
1043 : TYPE(dft_control_type), POINTER :: dft_control
1044 : TYPE(rt_prop_type), POINTER :: rtp
1045 :
1046 30 : NULLIFY (dft_control)
1047 :
1048 30 : logger => cp_get_default_logger()
1049 30 : output_unit = cp_logger_get_default_io_unit(logger)
1050 :
1051 30 : CALL get_qs_env(qs_env, dft_control=dft_control, rtp=rtp)
1052 :
1053 30 : i_step = rtp%istep
1054 :
1055 : unit_nr = cp_print_key_unit_nr(logger, dft_section, &
1056 30 : "REAL_TIME_PROPAGATION%PRINT%FIELD", extension=".dat", is_new_file=new_file)
1057 :
1058 30 : IF (output_unit > 0) THEN
1059 60 : rlab = [CHARACTER(LEN=3) :: "X", "Y", "Z"]
1060 15 : IF (unit_nr /= output_unit) THEN
1061 15 : INQUIRE (UNIT=unit_nr, NAME=filename)
1062 : WRITE (UNIT=output_unit, FMT="(/,T2,A,2(/,T3,A),/)") &
1063 15 : "FIELD", "The field applied is written to the file:", &
1064 30 : TRIM(filename)
1065 : ELSE
1066 0 : WRITE (UNIT=output_unit, FMT="(/,T2,A)") "FIELD APPLIED [a.u.]"
1067 : WRITE (UNIT=output_unit, FMT="(T5,3(A,A,E16.8,1X))") &
1068 0 : (TRIM(rlab(i)), "=", dft_control%rtp_control%field(i), i=1, 3)
1069 : END IF
1070 :
1071 15 : IF (new_file) THEN
1072 2 : IF (dft_control%apply_efield_field) THEN
1073 1 : WRITE (UNIT=unit_nr, FMT='("#",5X,A,8X,A,3(6X,A))') "Step Nr.", "Time[fs]", " Field X", " Field Y", " Field Z"
1074 1 : ELSE IF (dft_control%apply_vector_potential) THEN
1075 0 : WRITE (UNIT=unit_nr, FMT='("#",5X,A,8X,A,6(6X,A))') "Step Nr.", "Time[fs]", " Field X", " Field Y", " Field Z", &
1076 0 : " Vec. Pot. X", " Vec. Pot. Y", " Vec. Pot. Z"
1077 : END IF
1078 : END IF
1079 :
1080 15 : field = 0.0_dp
1081 15 : IF (dft_control%apply_efield_field) THEN
1082 4 : CALL make_field(dft_control, field, qs_env%sim_step, qs_env%sim_time)
1083 4 : WRITE (UNIT=unit_nr, FMT="(I10,F16.6,3(F16.8,1X))") qs_env%sim_step, qs_env%sim_time*femtoseconds, &
1084 8 : field(1), field(2), field(3)
1085 : ! DO i=1,3
1086 : ! IF (ABS(field(i))< 10E-10) field(i) = 0.0_dp
1087 : ! END IF
1088 11 : ELSE IF (dft_control%apply_vector_potential) THEN
1089 9 : WRITE (UNIT=unit_nr, FMT="(I10,F16.6,6(F16.8,1X))") qs_env%sim_step, qs_env%sim_time*femtoseconds, &
1090 9 : dft_control%rtp_control%field(1), dft_control%rtp_control%field(2), dft_control%rtp_control%field(3), &
1091 18 : dft_control%rtp_control%vec_pot(1), dft_control%rtp_control%vec_pot(2), dft_control%rtp_control%vec_pot(3)
1092 : END IF
1093 :
1094 : END IF
1095 :
1096 : CALL cp_print_key_finished_output(unit_nr, logger, dft_section, &
1097 30 : "REAL_TIME_PROPAGATION%PRINT%FIELD")
1098 :
1099 30 : END SUBROUTINE print_field_applied
1100 :
1101 : ! **************************************************************************************************
1102 : !> \brief Print the components of the total energy used in an RTP calculation
1103 : !> \param qs_env ...
1104 : !> \param dft_section ...
1105 : !> \par History
1106 : !> 2024-02 Created [ANB]
1107 : ! **************************************************************************************************
1108 4 : SUBROUTINE print_rtp_energy_components(qs_env, dft_section)
1109 : TYPE(qs_environment_type), POINTER :: qs_env
1110 : TYPE(section_vals_type), POINTER :: dft_section
1111 :
1112 : CHARACTER(LEN=default_path_length) :: filename
1113 : INTEGER :: i_step, output_unit, unit_nr
1114 : LOGICAL :: new_file
1115 : TYPE(cp_logger_type), POINTER :: logger
1116 : TYPE(dft_control_type), POINTER :: dft_control
1117 : TYPE(qs_energy_type), POINTER :: energy
1118 : TYPE(rt_prop_type), POINTER :: rtp
1119 :
1120 4 : NULLIFY (dft_control, energy, rtp)
1121 :
1122 4 : logger => cp_get_default_logger()
1123 4 : output_unit = cp_logger_get_default_io_unit(logger)
1124 :
1125 4 : CALL get_qs_env(qs_env, dft_control=dft_control, rtp=rtp, energy=energy)
1126 4 : i_step = rtp%istep
1127 :
1128 : unit_nr = cp_print_key_unit_nr(logger, dft_section, &
1129 : "REAL_TIME_PROPAGATION%PRINT%E_CONSTITUENTS", extension=".ener", &
1130 4 : file_action="WRITE", is_new_file=new_file)
1131 :
1132 4 : IF (output_unit > 0) THEN
1133 2 : IF (unit_nr /= output_unit) THEN
1134 2 : INQUIRE (UNIT=unit_nr, NAME=filename)
1135 : WRITE (UNIT=output_unit, FMT="(/,T2,A,2(/,T3,A),/)") &
1136 2 : "ENERGY_CONSTITUENTS", "Total Energy constituents written to file:", &
1137 4 : TRIM(filename)
1138 : ELSE
1139 0 : WRITE (UNIT=output_unit, FMT="(/,T2,A)") "ENERGY_CONSTITUENTS"
1140 : END IF
1141 :
1142 2 : IF (new_file) THEN
1143 : ! NOTE that these are not all terms contributing to the total energy for RTP, only a selection of those
1144 : ! most significant / impactful. Therefore the printed components likely will not add up to the total energy.
1145 1 : WRITE (UNIT=unit_nr, FMT='("#",5X,A,8X,A,10(6X,A))') "Step Nr.", "Time[fs]", &
1146 1 : "Total ener.[a.u.]", "core[a.u.] ", " overlap [a.u.]", "hartree[a.u.]", " exc. [a.u.] ", &
1147 2 : " hartree 1c[a.u.]", "exc 1c[a.u.] ", "exc admm[a.u.]", "exc 1c admm[a.u.]", "efield LG"
1148 :
1149 : END IF
1150 : WRITE (UNIT=unit_nr, FMT="(I10,F20.6,10(F20.9))") &
1151 2 : qs_env%sim_step, qs_env%sim_time*femtoseconds, &
1152 2 : energy%total, energy%core, energy%core_overlap, energy%hartree, energy%exc, &
1153 4 : energy%hartree_1c, energy%exc1, energy%exc_aux_fit, energy%exc1_aux_fit, energy%efield_core
1154 :
1155 : END IF
1156 :
1157 : CALL cp_print_key_finished_output(unit_nr, logger, dft_section, &
1158 4 : "REAL_TIME_PROPAGATION%PRINT%E_CONSTITUENTS")
1159 :
1160 4 : END SUBROUTINE print_rtp_energy_components
1161 :
1162 : ! **************************************************************************************************
1163 : !> \brief Print the dipole moments into a file
1164 : !> \param moments_section Section of the input defining the file/stream to print the moments to
1165 : !> \param info_unit Unit where standard output from the program is written - for add. identifiers
1166 : !> \param moments Actual moment values (for specific time step)
1167 : !> \param time Current simulation time
1168 : !> \param imag_opt Whether to calculate the imaginary part
1169 : !> \param append_opt ...
1170 : !> \par History
1171 : !> 10.2025 Created [Marek]
1172 : ! **************************************************************************************************
1173 1616 : SUBROUTINE print_moments(moments_section, info_unit, moments, time, imag_opt, append_opt)
1174 : TYPE(section_vals_type), POINTER :: moments_section
1175 : INTEGER :: info_unit
1176 : COMPLEX(kind=dp), DIMENSION(:, :) :: moments
1177 : REAL(kind=dp), OPTIONAL :: time
1178 : LOGICAL, OPTIONAL :: imag_opt, append_opt
1179 :
1180 : CHARACTER(len=14), DIMENSION(4) :: file_extensions
1181 : CHARACTER(len=21) :: prefix
1182 : COMPLEX(kind=dp), DIMENSION(3, 1) :: moment_t
1183 : INTEGER :: i, ndir, nspin, print_unit
1184 : LOGICAL :: append, imaginary
1185 : TYPE(cp_logger_type), POINTER :: logger
1186 :
1187 : ! Index 1 : spin, Index 2 : direction
1188 :
1189 1616 : nspin = SIZE(moments, 1)
1190 1616 : ndir = SIZE(moments, 2)
1191 :
1192 1616 : IF (nspin < 1) CPABORT("Zero spin index size in print moments!")
1193 1616 : IF (ndir < 1) CPABORT("Zero direction index size in print moments!")
1194 :
1195 1616 : imaginary = .TRUE.
1196 1616 : IF (PRESENT(imag_opt)) imaginary = imag_opt
1197 :
1198 1616 : append = .TRUE.
1199 1616 : IF (PRESENT(append_opt)) append = append_opt
1200 :
1201 : ! Get the program run info unit and target unit
1202 : ! If these are the same (most likely the case of __STD_OUT__), add
1203 : ! extra identifier to the printed output
1204 1616 : file_extensions(1) = "_SPIN_A_RE.dat"
1205 1616 : file_extensions(2) = "_SPIN_A_IM.dat"
1206 1616 : file_extensions(3) = "_SPIN_B_RE.dat"
1207 1616 : file_extensions(4) = "_SPIN_B_IM.dat"
1208 1616 : logger => cp_get_default_logger()
1209 3394 : DO i = 1, nspin
1210 7112 : moment_t(:, 1) = moments(i, :)
1211 : ! Real part
1212 1778 : print_unit = cp_print_key_unit_nr(logger, moments_section, extension=file_extensions(2*i - 1))
1213 1778 : IF (print_unit == info_unit) THEN
1214 : ! print with prefix
1215 1151 : prefix = " MOMENTS_TRACE_RE|"
1216 1151 : IF (append) THEN
1217 : ! Print without headers
1218 : CALL print_rt_file(print_unit, xvals=[time], yvals=moment_t, &
1219 : prefix=prefix, prefix_format="(A18)", &
1220 2236 : xscale_opt=femtoseconds, comp_opt=rt_file_comp_real)
1221 : ELSE
1222 : ! Print with headers
1223 : CALL print_rt_file(print_unit, &
1224 : headers=["# Time [fs]", " re(mom_t) x [at.u.]", &
1225 : " re(mom_t) y [at.u.]", " re(mom_t) z [at.u.]"], &
1226 : xvals=[time], yvals=moment_t, &
1227 : prefix=prefix, prefix_format="(A18)", &
1228 198 : xscale_opt=femtoseconds, comp_opt=rt_file_comp_real)
1229 : END IF
1230 : ELSE
1231 : ! Print without prefix
1232 627 : IF (append) THEN
1233 : ! Print without headers
1234 : CALL print_rt_file(print_unit, xvals=[time], yvals=moment_t, &
1235 1200 : xscale_opt=femtoseconds, comp_opt=rt_file_comp_real)
1236 : ELSE
1237 : ! Print with headers
1238 : CALL print_rt_file(print_unit, &
1239 : headers=["# Time [fs]", " re(mom_t) x [at.u.]", &
1240 : " re(mom_t) y [at.u.]", " re(mom_t) z [at.u.]"], &
1241 : xvals=[time], yvals=moment_t, &
1242 162 : xscale_opt=femtoseconds, comp_opt=rt_file_comp_real)
1243 : END IF
1244 : END IF
1245 1778 : CALL cp_print_key_finished_output(print_unit, logger, moments_section)
1246 : ! Same for imaginary part
1247 3394 : IF (imaginary) THEN
1248 1688 : print_unit = cp_print_key_unit_nr(logger, moments_section, extension=file_extensions(2*i))
1249 1688 : IF (print_unit == info_unit) THEN
1250 : ! print with prefix
1251 1106 : prefix = " MOMENTS_TRACE_IM|"
1252 1106 : IF (append) THEN
1253 : ! Print without headers
1254 : CALL print_rt_file(print_unit, xvals=[time], yvals=moment_t, &
1255 : prefix=prefix, prefix_format="(A18)", &
1256 2146 : xscale_opt=femtoseconds, comp_opt=rt_file_comp_imag)
1257 : ELSE
1258 : ! Print with headers
1259 : CALL print_rt_file(print_unit, &
1260 : headers=["# Time [fs]", " im(mom_t) x [at.u.]", &
1261 : " im(mom_t) y [at.u.]", " im(mom_t) z [at.u.]"], &
1262 : xvals=[time], yvals=moment_t, &
1263 : prefix=prefix, prefix_format="(A18)", &
1264 198 : xscale_opt=femtoseconds, comp_opt=rt_file_comp_imag)
1265 : END IF
1266 : ELSE
1267 : ! Print without prefix
1268 582 : IF (append) THEN
1269 : ! Print without headers
1270 : CALL print_rt_file(print_unit, xvals=[time], yvals=moment_t, &
1271 1110 : xscale_opt=femtoseconds, comp_opt=rt_file_comp_imag)
1272 : ELSE
1273 : ! Print with headers
1274 : CALL print_rt_file(print_unit, &
1275 : headers=["# Time [fs]", " im(mom_t) x [at.u.]", &
1276 : " im(mom_t) y [at.u.]", " im(mom_t) z [at.u.]"], &
1277 : xvals=[time], yvals=moment_t, &
1278 162 : xscale_opt=femtoseconds, comp_opt=rt_file_comp_imag)
1279 : END IF
1280 : END IF
1281 1688 : CALL cp_print_key_finished_output(print_unit, logger, moments_section)
1282 : END IF
1283 : END DO
1284 :
1285 1616 : END SUBROUTINE print_moments
1286 :
1287 : ! **************************************************************************************************
1288 : !> \brief Calculate the values of real/imaginary parts of moments in all directions
1289 : !> \param moment_matrices Local matrix representations of dipole (position) operator
1290 : !> \param density_matrices Density matrices (spin and real+complex parts)
1291 : !> \param work Extra dbcsr matrix for work
1292 : !> \param moment Resulting moments (spin and direction)
1293 : !> \param imag_opt Whether to calculate the imaginary part of the moment
1294 : !> \par History
1295 : !> 10.2025 Created [Marek]
1296 : ! **************************************************************************************************
1297 54 : SUBROUTINE calc_local_moment(moment_matrices, density_matrices, work, moment, imag_opt)
1298 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: moment_matrices, density_matrices
1299 : TYPE(dbcsr_type) :: work
1300 : COMPLEX(kind=dp), DIMENSION(:, :) :: moment
1301 : LOGICAL, OPTIONAL :: imag_opt
1302 :
1303 : INTEGER :: i, k, nspin
1304 : LOGICAL :: imag
1305 : REAL(kind=dp) :: real_moment
1306 :
1307 54 : imag = .FALSE.
1308 54 : IF (PRESENT(imag_opt)) imag = imag_opt
1309 54 : nspin = SIZE(density_matrices)/2
1310 :
1311 144 : DO i = 1, nspin
1312 414 : DO k = 1, 3
1313 : CALL dbcsr_multiply("N", "N", -1.0_dp, &
1314 : density_matrices(2*i - 1)%matrix, moment_matrices(k)%matrix, &
1315 270 : 0.0_dp, work)
1316 270 : CALL dbcsr_trace(work, real_moment)
1317 270 : moment(i, k) = CMPLX(real_moment, 0.0, kind=dp)
1318 360 : IF (imag) THEN
1319 : CALL dbcsr_multiply("N", "N", -1.0_dp, &
1320 : density_matrices(2*i)%matrix, moment_matrices(k)%matrix, &
1321 0 : 0.0_dp, work)
1322 0 : CALL dbcsr_trace(work, real_moment)
1323 0 : moment(i, k) = moment(i, k) + CMPLX(0.0, real_moment, kind=dp)
1324 : END IF
1325 : END DO
1326 : END DO
1327 :
1328 54 : END SUBROUTINE calc_local_moment
1329 :
1330 : ! **************************************************************************************************
1331 : !> \brief Calculate and print the Fourier transforms + polarizabilites from moment trace
1332 : !> \param rtp_section The RTP input section (needed to access PRINT configurations)
1333 : !> \param moments Moment trace
1334 : !> \param times Corresponding times
1335 : !> \param fields Corresponding fields
1336 : !> \param rtc rt_control_type that includes metadata
1337 : !> \param info_opt ...
1338 : !> \param cell If present, used to change the delta peak representation to be in units of reciprocal lattice
1339 : !> \par History
1340 : !> 10.2025 Created [Marek]
1341 : ! **************************************************************************************************
1342 90 : SUBROUTINE print_ft(rtp_section, moments, times, fields, rtc, info_opt, cell)
1343 : TYPE(section_vals_type), POINTER :: rtp_section
1344 : COMPLEX(kind=dp), DIMENSION(:, :, :), POINTER :: moments
1345 : REAL(kind=dp), DIMENSION(:), POINTER :: times
1346 : COMPLEX(kind=dp), DIMENSION(:, :), POINTER :: fields
1347 : TYPE(rtp_control_type), POINTER :: rtc
1348 : INTEGER, OPTIONAL :: info_opt
1349 : TYPE(cell_type), OPTIONAL, POINTER :: cell
1350 :
1351 : CHARACTER(len=*), PARAMETER :: routineN = 'print_ft'
1352 :
1353 : CHARACTER(len=11), DIMENSION(2) :: file_extensions
1354 90 : CHARACTER(len=20), ALLOCATABLE, DIMENSION(:) :: headers
1355 : CHARACTER(len=21) :: prefix
1356 : CHARACTER(len=5) :: prefix_format
1357 90 : COMPLEX(kind=dp), ALLOCATABLE, DIMENSION(:) :: omegas_complex, omegas_pade
1358 90 : COMPLEX(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: field_results, field_results_pade, &
1359 90 : pol_results, pol_results_pade, pol_results_pade_spin_total, pol_results_spin_total, &
1360 90 : results, results_pade, results_pade_spin_total, results_spin_total, value_series
1361 : INTEGER :: ft_unit, handle, i, idx_omega_zero, &
1362 : info_unit, k, k_static, n, n_elems, &
1363 : n_pade, nspin
1364 : LOGICAL :: do_moments_ft, do_polarizability
1365 : REAL(kind=dp) :: damping, t0
1366 90 : REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: omegas, omegas_pade_real
1367 : REAL(kind=dp), DIMENSION(3) :: delta_vec
1368 : TYPE(cp_logger_type), POINTER :: logger
1369 : TYPE(section_vals_type), POINTER :: moment_ft_section, pol_section
1370 :
1371 90 : CALL timeset(routineN, handle)
1372 : ! For results, using spin * direction for first index, e.g. for nspin = 2
1373 : ! results(1,:) = (spin=1 and direction=1,:),
1374 : ! results(5,:) = (spin=2 and direction=2,:)
1375 :
1376 90 : logger => cp_get_default_logger()
1377 :
1378 90 : moment_ft_section => section_vals_get_subs_vals(rtp_section, "PRINT%MOMENTS_FT")
1379 90 : pol_section => section_vals_get_subs_vals(rtp_section, "PRINT%POLARIZABILITY")
1380 :
1381 90 : nspin = SIZE(moments, 1)
1382 90 : n = SIZE(times)
1383 90 : n_elems = SIZE(rtc%print_pol_elements, 1)
1384 :
1385 90 : info_unit = -1
1386 90 : IF (PRESENT(info_opt)) info_unit = info_opt
1387 :
1388 : ! NOTE : Allows for at most 2 spin species
1389 90 : file_extensions(1) = "_SPIN_A.dat"
1390 90 : file_extensions(2) = "_SPIN_B.dat"
1391 :
1392 : ! Determine whether MOMENTS_FT and/or polarizability needs to be calculated
1393 90 : do_moments_ft = cp_printkey_is_on(logger%iter_info, moment_ft_section)
1394 90 : do_polarizability = cp_printkey_is_on(logger%iter_info, pol_section)
1395 90 : do_polarizability = do_polarizability .AND. (n_elems > 0)
1396 :
1397 90 : damping = rtc%ft_damping
1398 90 : t0 = rtc%ft_t0
1399 :
1400 : ! Determine field ft if polarizability required
1401 90 : IF (do_polarizability) THEN
1402 204 : ALLOCATE (field_results(3, n))
1403 68 : IF (rtc%apply_delta_pulse) THEN
1404 : ! Constant real FT
1405 64 : IF (PRESENT(cell)) THEN
1406 : delta_vec(:) = (REAL(rtc%delta_pulse_direction(1), kind=dp)*cell%h_inv(1, :) + &
1407 : REAL(rtc%delta_pulse_direction(2), kind=dp)*cell%h_inv(2, :) + &
1408 : REAL(rtc%delta_pulse_direction(3), kind=dp)*cell%h_inv(3, :)) &
1409 0 : *twopi*rtc%delta_pulse_scale
1410 : ELSE
1411 256 : delta_vec(:) = REAL(rtc%delta_pulse_direction(:), kind=dp)*rtc%delta_pulse_scale
1412 : END IF
1413 256 : DO k = 1, 3
1414 4180 : field_results(k, :) = CMPLX(delta_vec(k), 0.0, kind=dp)
1415 : END DO
1416 : ELSE
1417 : ! Do explicit FT
1418 : CALL multi_fft(times, fields, field_results, &
1419 4 : damping_opt=damping, t0_opt=t0, subtract_initial_opt=.TRUE.)
1420 : END IF
1421 : END IF
1422 :
1423 90 : IF (do_moments_ft .OR. do_polarizability) THEN
1424 : ! We need to transform at least the moments
1425 : ! NOTE : Might be able to save some memory by only doing FT of actually
1426 : ! required moments, but for now, doing FT of all moment directions
1427 272 : ALLOCATE (results(3*nspin, n))
1428 204 : ALLOCATE (omegas(n))
1429 204 : ALLOCATE (value_series(3*nspin, n))
1430 144 : DO i = 1, nspin
1431 372 : DO k = 1, 3
1432 5284 : value_series(3*(i - 1) + k, :) = moments(i, k, :)
1433 : END DO
1434 : END DO
1435 : ! TODO : Choose whether the initial subtraction is applied in &FT section?
1436 : CALL multi_fft(times, value_series, results, omegas, &
1437 68 : damping_opt=damping, t0_opt=t0, subtract_initial_opt=.TRUE.)
1438 68 : DEALLOCATE (value_series)
1439 144 : DO i = 1, nspin
1440 : ! Output to FT file, if needed
1441 : ft_unit = cp_print_key_unit_nr(logger, moment_ft_section, extension=file_extensions(i), &
1442 76 : file_form="FORMATTED", file_position="REWIND")
1443 : ! Print header
1444 76 : IF (ft_unit > 0) THEN
1445 38 : ALLOCATE (headers(7))
1446 38 : headers(2) = " x,real [at.u.]"
1447 38 : headers(3) = " x,imag [at.u.]"
1448 38 : headers(4) = " y,real [at.u.]"
1449 38 : headers(5) = " y,imag [at.u.]"
1450 38 : headers(6) = " z,real [at.u.]"
1451 38 : headers(7) = " z,imag [at.u.]"
1452 38 : IF (info_unit == ft_unit) THEN
1453 0 : headers(1) = "# Energy [eV]"
1454 0 : prefix = " MOMENTS_FT|"
1455 0 : prefix_format = "(A12)"
1456 : CALL print_rt_file(ft_unit, headers, omegas, results(3*(i - 1) + 1:3*(i - 1) + 3, :), &
1457 0 : prefix, prefix_format, evolt)
1458 : ELSE
1459 38 : headers(1) = "# omega [at.u.]"
1460 38 : CALL print_rt_file(ft_unit, headers, omegas, results(3*(i - 1) + 1:3*(i - 1) + 3, :))
1461 : END IF
1462 38 : DEALLOCATE (headers)
1463 : END IF
1464 144 : CALL cp_print_key_finished_output(ft_unit, logger, moment_ft_section)
1465 : END DO
1466 : ! Spin-summed total moments FT (open shell only; inert for nspin=1)
1467 68 : IF (nspin > 1) THEN
1468 24 : ALLOCATE (results_spin_total(3, n))
1469 8 : results_spin_total(:, :) = (0.0_dp, 0.0_dp)
1470 24 : DO i = 1, nspin
1471 72 : DO k = 1, 3
1472 952 : results_spin_total(k, :) = results_spin_total(k, :) + results(3*(i - 1) + k, :)
1473 : END DO
1474 : END DO
1475 : ft_unit = cp_print_key_unit_nr(logger, moment_ft_section, extension="_SPIN_TOTAL.dat", &
1476 8 : file_form="FORMATTED", file_position="REWIND")
1477 8 : IF (ft_unit > 0) THEN
1478 4 : ALLOCATE (headers(7))
1479 4 : headers(2) = " x,real [at.u.]"
1480 4 : headers(3) = " x,imag [at.u.]"
1481 4 : headers(4) = " y,real [at.u.]"
1482 4 : headers(5) = " y,imag [at.u.]"
1483 4 : headers(6) = " z,real [at.u.]"
1484 4 : headers(7) = " z,imag [at.u.]"
1485 4 : IF (info_unit == ft_unit) THEN
1486 0 : headers(1) = "# Energy [eV]"
1487 0 : prefix = " MOMENTS_FT|"
1488 0 : prefix_format = "(A12)"
1489 : CALL print_rt_file(ft_unit, headers, omegas, results_spin_total, &
1490 0 : prefix, prefix_format, evolt)
1491 : ELSE
1492 4 : headers(1) = "# omega [at.u.]"
1493 4 : CALL print_rt_file(ft_unit, headers, omegas, results_spin_total)
1494 : END IF
1495 4 : DEALLOCATE (headers)
1496 : END IF
1497 8 : CALL cp_print_key_finished_output(ft_unit, logger, moment_ft_section)
1498 8 : DEALLOCATE (results_spin_total)
1499 : END IF
1500 : END IF
1501 :
1502 90 : IF (rtc%pade_requested .AND. (do_moments_ft .OR. do_polarizability)) THEN
1503 6 : ALLOCATE (omegas_complex(SIZE(omegas)))
1504 64 : omegas_complex(:) = CMPLX(omegas(:), 0.0, kind=dp)
1505 2 : n_pade = INT((rtc%pade_e_max - rtc%pade_e_min)/rtc%pade_e_step)
1506 6 : ALLOCATE (omegas_pade(n_pade))
1507 6 : ALLOCATE (omegas_pade_real(n_pade))
1508 : ! Construct omegas_pade and omegas_complex
1509 2000 : DO i = 1, n_pade
1510 1998 : omegas_pade_real(i) = (i - 1)*rtc%pade_e_step + rtc%pade_e_min
1511 2000 : omegas_pade(i) = CMPLX(omegas_pade_real(i), 0.0, kind=dp)
1512 : END DO
1513 8 : ALLOCATE (results_pade(nspin*3, n_pade), source=CMPLX(0.0, 0.0, kind=dp))
1514 4 : DO i = 1, nspin
1515 8 : DO k = 1, 3
1516 : CALL greenx_refine_ft(rtc%pade_fit_e_min, rtc%pade_fit_e_max, omegas_complex, results(3*(i - 1) + k, :), &
1517 8 : omegas_pade, results_pade(3*(i - 1) + k, :))
1518 : END DO
1519 : ! Print to a file
1520 : ft_unit = cp_print_key_unit_nr(logger, moment_ft_section, extension="_PADE"//file_extensions(i), &
1521 2 : file_form="FORMATTED", file_position="REWIND")
1522 4 : IF (ft_unit > 0) THEN
1523 1 : ALLOCATE (headers(7))
1524 1 : headers(2) = " x,real,pade [at.u.]"
1525 1 : headers(3) = " x,imag,pade [at.u.]"
1526 1 : headers(4) = " y,real,pade [at.u.]"
1527 1 : headers(5) = " y,imag,pade [at.u.]"
1528 1 : headers(6) = " z,real,pade [at.u.]"
1529 1 : headers(7) = " z,imag,pade [at.u.]"
1530 1 : IF (info_unit == ft_unit) THEN
1531 0 : headers(1) = "# Energy [eV]"
1532 0 : prefix = " MOMENTS_FT_PADE|"
1533 0 : prefix_format = "(A17)"
1534 : CALL print_rt_file(ft_unit, headers, omegas_pade_real, results_pade(3*(i - 1) + 1:3*(i - 1) + 3, :), &
1535 0 : prefix, prefix_format, evolt)
1536 : ELSE
1537 1 : headers(1) = "# omega [at.u.]"
1538 1 : CALL print_rt_file(ft_unit, headers, omegas_pade_real, results_pade(3*(i - 1) + 1:3*(i - 1) + 3, :))
1539 : END IF
1540 1 : DEALLOCATE (headers)
1541 : END IF
1542 : END DO
1543 : ! Spin-summed total moments-FT Padé (open shell only; inert for nspin=1)
1544 2 : IF (nspin > 1) THEN
1545 0 : ALLOCATE (results_pade_spin_total(3, n_pade))
1546 0 : results_pade_spin_total(:, :) = (0.0_dp, 0.0_dp)
1547 0 : DO i = 1, nspin
1548 0 : DO k = 1, 3
1549 0 : results_pade_spin_total(k, :) = results_pade_spin_total(k, :) + results_pade(3*(i - 1) + k, :)
1550 : END DO
1551 : END DO
1552 : ft_unit = cp_print_key_unit_nr(logger, moment_ft_section, extension="_PADE_SPIN_TOTAL.dat", &
1553 0 : file_form="FORMATTED", file_position="REWIND")
1554 0 : IF (ft_unit > 0) THEN
1555 0 : ALLOCATE (headers(7))
1556 0 : headers(2) = " x,real,pade [at.u.]"
1557 0 : headers(3) = " x,imag,pade [at.u.]"
1558 0 : headers(4) = " y,real,pade [at.u.]"
1559 0 : headers(5) = " y,imag,pade [at.u.]"
1560 0 : headers(6) = " z,real,pade [at.u.]"
1561 0 : headers(7) = " z,imag,pade [at.u.]"
1562 0 : IF (info_unit == ft_unit) THEN
1563 0 : headers(1) = "# Energy [eV]"
1564 0 : prefix = " MOMENTS_FT_PADE|"
1565 0 : prefix_format = "(A17)"
1566 : CALL print_rt_file(ft_unit, headers, omegas_pade_real, results_pade_spin_total, &
1567 0 : prefix, prefix_format, evolt)
1568 : ELSE
1569 0 : headers(1) = "# omega [at.u.]"
1570 0 : CALL print_rt_file(ft_unit, headers, omegas_pade_real, results_pade_spin_total)
1571 : END IF
1572 0 : DEALLOCATE (headers)
1573 : END IF
1574 0 : DEALLOCATE (results_pade_spin_total)
1575 : END IF
1576 : END IF
1577 :
1578 90 : IF (do_polarizability) THEN
1579 : ! get the polarizability elements, as required
1580 272 : ALLOCATE (pol_results(n_elems, n))
1581 144 : DO i = 1, nspin
1582 304 : DO k = 1, n_elems
1583 : ! NOTE - field is regularized to small value
1584 : pol_results(k, :) = results(3*(i - 1) + &
1585 : rtc%print_pol_elements(k, 1), :)/ &
1586 : (field_results(rtc%print_pol_elements(k, 2), :) + &
1587 5284 : 1.0e-10_dp*field_results(rtc%print_pol_elements(k, 2), 2))
1588 : END DO
1589 : ! Print to the file
1590 : ft_unit = cp_print_key_unit_nr(logger, pol_section, extension=file_extensions(i), &
1591 76 : file_form="FORMATTED", file_position="REWIND")
1592 76 : IF (ft_unit > 0) THEN
1593 114 : ALLOCATE (headers(2*n_elems + 1))
1594 152 : DO k = 1, n_elems
1595 114 : WRITE (headers(2*k), "(A16,I2,I2)") "real pol. elem.", &
1596 114 : rtc%print_pol_elements(k, 1), &
1597 228 : rtc%print_pol_elements(k, 2)
1598 114 : WRITE (headers(2*k + 1), "(A16,I2,I2)") "imag pol. elem.", &
1599 114 : rtc%print_pol_elements(k, 1), &
1600 266 : rtc%print_pol_elements(k, 2)
1601 : END DO
1602 : ! Write header
1603 38 : IF (info_unit == ft_unit) THEN
1604 2 : headers(1) = "# Energy [eV]"
1605 2 : prefix = " POLARIZABILITY|"
1606 2 : prefix_format = "(A16)"
1607 : CALL print_rt_file(ft_unit, headers, omegas, pol_results, &
1608 2 : prefix, prefix_format, evolt)
1609 : ELSE
1610 36 : headers(1) = "# omega [at.u.]"
1611 36 : CALL print_rt_file(ft_unit, headers, omegas, pol_results)
1612 : END IF
1613 38 : DEALLOCATE (headers)
1614 : END IF
1615 76 : CALL cp_print_key_finished_output(ft_unit, logger, pol_section)
1616 : ! Static polarizability alpha(0): pol_results at the FFT-grid omega
1617 : ! closest to zero. Re is alpha(0); Im should be machine-zero (sanity).
1618 144 : IF (info_unit > 0) THEN
1619 868 : idx_omega_zero = MINLOC(ABS(omegas), DIM=1)
1620 38 : IF (i == 1) THEN
1621 : WRITE (info_unit, '(A,T22,A,T28,A,T36,A,T59,A)') &
1622 34 : " STATIC_POL|", "spin", "element", "Re [a.u.]", "Im [a.u.]"
1623 : END IF
1624 152 : DO k_static = 1, n_elems
1625 : WRITE (info_unit, '(A,T22,I4,T28,I3,",",I3,T36,ES22.10E3,T59,ES22.10E3)') &
1626 114 : " STATIC_POL|", i, &
1627 114 : rtc%print_pol_elements(k_static, 1), &
1628 114 : rtc%print_pol_elements(k_static, 2), &
1629 114 : REAL(pol_results(k_static, idx_omega_zero), kind=dp), &
1630 266 : AIMAG(pol_results(k_static, idx_omega_zero))
1631 : END DO
1632 : END IF
1633 : END DO
1634 : ! Spin-summed total polarizability (open shell only; inert for nspin=1).
1635 : ! Field is spin-independent, so (sum_s moments_s)/field == sum_s (moments_s/field).
1636 68 : IF (nspin > 1) THEN
1637 24 : ALLOCATE (pol_results_spin_total(n_elems, n))
1638 8 : pol_results_spin_total(:, :) = (0.0_dp, 0.0_dp)
1639 32 : DO k = 1, n_elems
1640 72 : DO i = 1, nspin
1641 : pol_results_spin_total(k, :) = pol_results_spin_total(k, :) + &
1642 960 : results(3*(i - 1) + rtc%print_pol_elements(k, 1), :)
1643 : END DO
1644 : pol_results_spin_total(k, :) = pol_results_spin_total(k, :)/ &
1645 : (field_results(rtc%print_pol_elements(k, 2), :) + &
1646 476 : 1.0e-10_dp*field_results(rtc%print_pol_elements(k, 2), 2))
1647 : END DO
1648 : ft_unit = cp_print_key_unit_nr(logger, pol_section, extension="_SPIN_TOTAL.dat", &
1649 8 : file_form="FORMATTED", file_position="REWIND")
1650 8 : IF (ft_unit > 0) THEN
1651 12 : ALLOCATE (headers(2*n_elems + 1))
1652 16 : DO k = 1, n_elems
1653 12 : WRITE (headers(2*k), "(A16,I2,I2)") "real pol. elem.", &
1654 12 : rtc%print_pol_elements(k, 1), &
1655 24 : rtc%print_pol_elements(k, 2)
1656 12 : WRITE (headers(2*k + 1), "(A16,I2,I2)") "imag pol. elem.", &
1657 12 : rtc%print_pol_elements(k, 1), &
1658 28 : rtc%print_pol_elements(k, 2)
1659 : END DO
1660 4 : IF (info_unit == ft_unit) THEN
1661 0 : headers(1) = "# Energy [eV]"
1662 0 : prefix = " POLARIZABILITY|"
1663 0 : prefix_format = "(A16)"
1664 : CALL print_rt_file(ft_unit, headers, omegas, pol_results_spin_total, &
1665 0 : prefix, prefix_format, evolt)
1666 : ELSE
1667 4 : headers(1) = "# omega [at.u.]"
1668 4 : CALL print_rt_file(ft_unit, headers, omegas, pol_results_spin_total)
1669 : END IF
1670 4 : DEALLOCATE (headers)
1671 : END IF
1672 8 : CALL cp_print_key_finished_output(ft_unit, logger, pol_section)
1673 : ! Static polarizability total (header row already emitted by the per-spin block)
1674 8 : IF (info_unit > 0) THEN
1675 78 : idx_omega_zero = MINLOC(ABS(omegas), DIM=1)
1676 16 : DO k_static = 1, n_elems
1677 : WRITE (info_unit, '(A,T22,A,T28,I3,",",I3,T36,ES22.10E3,T59,ES22.10E3)') &
1678 12 : " STATIC_POL|", "TOT", &
1679 12 : rtc%print_pol_elements(k_static, 1), &
1680 12 : rtc%print_pol_elements(k_static, 2), &
1681 12 : REAL(pol_results_spin_total(k_static, idx_omega_zero), kind=dp), &
1682 28 : AIMAG(pol_results_spin_total(k_static, idx_omega_zero))
1683 : END DO
1684 : END IF
1685 8 : DEALLOCATE (pol_results_spin_total)
1686 : END IF
1687 : END IF
1688 :
1689 : ! Padé polarizability
1690 90 : IF (rtc%pade_requested .AND. do_polarizability) THEN
1691 : ! Start with the field pade
1692 6 : ALLOCATE (field_results_pade(3, n_pade))
1693 2 : IF (rtc%apply_delta_pulse) THEN
1694 8 : DO k = 1, 3
1695 6002 : field_results_pade(k, :) = CMPLX(delta_vec(k), 0.0, kind=dp)
1696 : END DO
1697 : ELSE
1698 0 : DO k = 1, 3
1699 : CALL greenx_refine_ft(rtc%pade_fit_e_min, rtc%pade_fit_e_max, &
1700 : omegas_complex, field_results(k, :), &
1701 0 : omegas_pade, field_results_pade(k, :))
1702 : END DO
1703 : END IF
1704 : ! Allocate polarisation pade
1705 8 : ALLOCATE (pol_results_pade(n_elems, n_pade))
1706 : ! Refine
1707 4 : DO i = 1, nspin
1708 8 : DO k = 1, n_elems
1709 : ! NOTE : Regularization to small value
1710 : pol_results_pade(k, :) = results_pade(3*(i - 1) + rtc%print_pol_elements(k, 1), :)/( &
1711 : field_results_pade(rtc%print_pol_elements(k, 2), :) + &
1712 6002 : field_results_pade(rtc%print_pol_elements(k, 2), 2)*1.0e-10_dp)
1713 : END DO
1714 : ! Print to the file
1715 : ft_unit = cp_print_key_unit_nr(logger, pol_section, extension="_PADE"//file_extensions(i), &
1716 2 : file_form="FORMATTED", file_position="REWIND")
1717 2 : IF (ft_unit > 0) THEN
1718 3 : ALLOCATE (headers(2*n_elems + 1))
1719 4 : DO k = 1, n_elems
1720 3 : WRITE (headers(2*k), "(A16,I2,I2)") "re,pade,pol.", &
1721 3 : rtc%print_pol_elements(k, 1), &
1722 6 : rtc%print_pol_elements(k, 2)
1723 3 : WRITE (headers(2*k + 1), "(A16,I2,I2)") "im,pade,pol.", &
1724 3 : rtc%print_pol_elements(k, 1), &
1725 7 : rtc%print_pol_elements(k, 2)
1726 : END DO
1727 : ! Write header
1728 1 : IF (info_unit == ft_unit) THEN
1729 1 : headers(1) = "# Energy [eV]"
1730 1 : prefix = " POLARIZABILITY_PADE|"
1731 1 : prefix_format = "(A21)"
1732 : CALL print_rt_file(ft_unit, headers, omegas_pade_real, pol_results_pade, &
1733 1 : prefix, prefix_format, evolt)
1734 : ELSE
1735 0 : headers(1) = "# omega [at.u.]"
1736 0 : CALL print_rt_file(ft_unit, headers, omegas_pade_real, pol_results_pade)
1737 : END IF
1738 1 : DEALLOCATE (headers)
1739 : END IF
1740 4 : CALL cp_print_key_finished_output(ft_unit, logger, pol_section)
1741 : END DO
1742 : ! Spin-summed total Padé polarizability (open shell only; inert for nspin=1)
1743 2 : IF (nspin > 1) THEN
1744 0 : ALLOCATE (pol_results_pade_spin_total(n_elems, n_pade))
1745 0 : pol_results_pade_spin_total(:, :) = (0.0_dp, 0.0_dp)
1746 0 : DO k = 1, n_elems
1747 0 : DO i = 1, nspin
1748 : pol_results_pade_spin_total(k, :) = pol_results_pade_spin_total(k, :) + &
1749 0 : results_pade(3*(i - 1) + rtc%print_pol_elements(k, 1), :)
1750 : END DO
1751 : pol_results_pade_spin_total(k, :) = pol_results_pade_spin_total(k, :)/( &
1752 : field_results_pade(rtc%print_pol_elements(k, 2), :) + &
1753 0 : field_results_pade(rtc%print_pol_elements(k, 2), 2)*1.0e-10_dp)
1754 : END DO
1755 : ft_unit = cp_print_key_unit_nr(logger, pol_section, extension="_PADE_SPIN_TOTAL.dat", &
1756 0 : file_form="FORMATTED", file_position="REWIND")
1757 0 : IF (ft_unit > 0) THEN
1758 0 : ALLOCATE (headers(2*n_elems + 1))
1759 0 : DO k = 1, n_elems
1760 0 : WRITE (headers(2*k), "(A16,I2,I2)") "re,pade,pol.", &
1761 0 : rtc%print_pol_elements(k, 1), &
1762 0 : rtc%print_pol_elements(k, 2)
1763 0 : WRITE (headers(2*k + 1), "(A16,I2,I2)") "im,pade,pol.", &
1764 0 : rtc%print_pol_elements(k, 1), &
1765 0 : rtc%print_pol_elements(k, 2)
1766 : END DO
1767 0 : IF (info_unit == ft_unit) THEN
1768 0 : headers(1) = "# Energy [eV]"
1769 0 : prefix = " POLARIZABILITY_PADE|"
1770 0 : prefix_format = "(A21)"
1771 : CALL print_rt_file(ft_unit, headers, omegas_pade_real, pol_results_pade_spin_total, &
1772 0 : prefix, prefix_format, evolt)
1773 : ELSE
1774 0 : headers(1) = "# omega [at.u.]"
1775 0 : CALL print_rt_file(ft_unit, headers, omegas_pade_real, pol_results_pade_spin_total)
1776 : END IF
1777 0 : DEALLOCATE (headers)
1778 : END IF
1779 0 : CALL cp_print_key_finished_output(ft_unit, logger, pol_section)
1780 0 : DEALLOCATE (pol_results_pade_spin_total)
1781 : END IF
1782 2 : DEALLOCATE (field_results_pade)
1783 2 : DEALLOCATE (pol_results_pade)
1784 : END IF
1785 :
1786 90 : IF (rtc%pade_requested .AND. (do_moments_ft .OR. do_polarizability)) THEN
1787 2 : DEALLOCATE (omegas_complex)
1788 2 : DEALLOCATE (omegas_pade)
1789 2 : DEALLOCATE (omegas_pade_real)
1790 2 : DEALLOCATE (results_pade)
1791 : END IF
1792 :
1793 90 : IF (do_polarizability) THEN
1794 68 : DEALLOCATE (pol_results)
1795 68 : DEALLOCATE (field_results)
1796 : END IF
1797 :
1798 90 : IF (do_moments_ft .OR. do_polarizability) THEN
1799 68 : DEALLOCATE (results)
1800 68 : DEALLOCATE (omegas)
1801 : END IF
1802 :
1803 90 : CALL timestop(handle)
1804 :
1805 90 : END SUBROUTINE print_ft
1806 :
1807 : ! **************************************************************************************************
1808 : !> \brief ...
1809 : !> \param rt_unit ...
1810 : !> \param headers ...
1811 : !> \param xvals ...
1812 : !> \param yvals ...
1813 : !> \param prefix ...
1814 : !> \param prefix_format ...
1815 : !> \param xscale_opt ...
1816 : !> \param comp_opt ...
1817 : ! **************************************************************************************************
1818 5114 : SUBROUTINE print_rt_file(rt_unit, headers, xvals, yvals, prefix, prefix_format, xscale_opt, comp_opt)
1819 : INTEGER, INTENT(IN) :: rt_unit
1820 : CHARACTER(len=20), DIMENSION(:), INTENT(IN), &
1821 : OPTIONAL :: headers
1822 : REAL(kind=dp), DIMENSION(:), INTENT(IN) :: xvals
1823 : COMPLEX(kind=dp), DIMENSION(:, :), INTENT(IN) :: yvals
1824 : CHARACTER(len=21), INTENT(IN), OPTIONAL :: prefix
1825 : CHARACTER(len=5), INTENT(IN), OPTIONAL :: prefix_format
1826 : REAL(kind=dp), INTENT(IN), OPTIONAL :: xscale_opt
1827 : INTEGER, OPTIONAL :: comp_opt
1828 :
1829 : INTEGER :: do_comp, i, j, ncols, nrows
1830 : LOGICAL :: do_headers, do_prefix
1831 : REAL(kind=dp) :: xscale
1832 :
1833 5114 : do_prefix = .FALSE.
1834 5114 : IF (PRESENT(prefix)) THEN
1835 2260 : IF (PRESENT(prefix_format)) THEN
1836 : do_prefix = .TRUE.
1837 : ELSE
1838 0 : CPABORT("Printing of prefix with missing format!")
1839 : END IF
1840 : END IF
1841 :
1842 5114 : xscale = 1.0_dp
1843 5114 : IF (PRESENT(xscale_opt)) xscale = xscale_opt
1844 :
1845 5114 : ncols = SIZE(yvals, 1)
1846 5114 : nrows = SIZE(yvals, 2)
1847 :
1848 : ! Check whether printing complex data (default) or just a component
1849 5114 : do_comp = rt_file_comp_both
1850 5114 : IF (PRESENT(comp_opt)) do_comp = comp_opt
1851 :
1852 5114 : do_headers = PRESENT(headers)
1853 : ! Check whether enough headers for yvals and xvals is present
1854 5114 : IF (do_headers) THEN
1855 : IF ((do_comp /= rt_file_comp_both .AND. SIZE(headers) < ncols + 1) &
1856 216 : .OR. (do_comp == rt_file_comp_both .AND. SIZE(headers) < 2*ncols + 1)) THEN
1857 0 : CPABORT("Not enought headers to print the file!")
1858 : END IF
1859 : END IF
1860 :
1861 5114 : IF (SIZE(xvals) < nrows) THEN
1862 0 : CPABORT("Not enough xvals to print all yvals!")
1863 : END IF
1864 :
1865 5114 : IF (rt_unit > 0) THEN
1866 : ! Print headers
1867 672 : IF (do_headers) THEN
1868 : ! If prefix is present, write prefix
1869 94 : IF (do_prefix) THEN
1870 9 : WRITE (rt_unit, prefix_format, advance="no") prefix
1871 : END IF
1872 94 : WRITE (rt_unit, "(A20)", advance="no") headers(1)
1873 : ! Print the rest of the headers
1874 86 : SELECT CASE (do_comp)
1875 : CASE (rt_file_comp_both)
1876 : ! Complex case
1877 516 : DO j = 1, 2*ncols - 1
1878 516 : WRITE (rt_unit, "(A20)", advance="no") headers(j + 1)
1879 : END DO
1880 86 : WRITE (rt_unit, "(A20)") headers(2*ncols + 1)
1881 : CASE DEFAULT
1882 : ! For other cases, just one component is printed
1883 24 : DO j = 1, ncols - 1
1884 24 : WRITE (rt_unit, "(A20)", advance="no") headers(j + 1)
1885 : END DO
1886 102 : WRITE (rt_unit, "(A20)") headers(ncols + 1)
1887 : END SELECT
1888 : END IF
1889 : ! Done with the headers, print actual data
1890 5064 : DO i = 1, nrows
1891 : ! If prefix is present, write prefix
1892 4392 : IF (do_prefix) THEN
1893 1605 : WRITE (rt_unit, prefix_format, advance="no") prefix
1894 : END IF
1895 4392 : WRITE (rt_unit, "(E20.8E3)", advance="no") xvals(i)*xscale
1896 13176 : DO j = 1, ncols - 1
1897 4392 : SELECT CASE (do_comp)
1898 : CASE (rt_file_comp_real)
1899 : WRITE (rt_unit, "(E20.8E3)", advance="no") &
1900 586 : REAL(yvals(j, i))
1901 : CASE (rt_file_comp_imag)
1902 : WRITE (rt_unit, "(E20.8E3)", advance="no") &
1903 586 : AIMAG(yvals(j, i))
1904 : CASE DEFAULT
1905 : ! Print both components
1906 : WRITE (rt_unit, "(E20.8E3,E20.8E3)", advance="no") &
1907 8784 : REAL(yvals(j, i)), AIMAG(yvals(j, i))
1908 : END SELECT
1909 : END DO
1910 : ! Print the final column(s)
1911 672 : SELECT CASE (do_comp)
1912 : CASE (rt_file_comp_real)
1913 293 : WRITE (rt_unit, "(E20.8E3)") REAL(yvals(j, i))
1914 : CASE (rt_file_comp_imag)
1915 293 : WRITE (rt_unit, "(E20.8E3)") AIMAG(yvals(j, i))
1916 : CASE DEFAULT
1917 : ! Print both components
1918 : WRITE (rt_unit, "(E20.8E3,E20.8E3)") &
1919 4392 : REAL(yvals(j, i)), AIMAG(yvals(j, i))
1920 : END SELECT
1921 : END DO
1922 : END IF
1923 5114 : END SUBROUTINE print_rt_file
1924 :
1925 : END MODULE rt_propagation_output
|