Octopus
spectrum.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
21module spectrum_oct_m
22 use batch_oct_m
23 use iso_c_binding
25 use debug_oct_m
26 use fft_oct_m
27 use global_oct_m
28 use io_oct_m
29 use kick_oct_m
30 use, intrinsic :: iso_fortran_env
32 use math_oct_m
36 use parser_oct_m
37 use pcm_oct_m
39 use string_oct_m
40 use types_oct_m
41 use unit_oct_m
44
45 implicit none
46
47 private
48 public :: &
49 spectrum_t, &
70
71 integer, public, parameter :: &
72 SPECTRUM_DAMP_NONE = 0, &
77
78 integer, public, parameter :: &
79 SPECTRUM_TRANSFORM_LAPLACE = 1, &
82
83 integer, public, parameter :: &
84 SPECTRUM_ABSORPTION = 1, &
86 spectrum_p_power = 3, &
88
89 integer, public, parameter :: &
90 SPECTRUM_FOURIER = 1, &
92
93 type spectrum_t
94 real(real64) :: start_time
95 real(real64) :: end_time
96 real(real64) :: energy_step
97 real(real64) :: min_energy
98 real(real64) :: max_energy
99 integer :: damp
100 integer :: transform
101 real(real64) :: damp_factor
102 integer :: spectype
103 integer :: method
104 real(real64) :: noise
105 logical, private :: sigma_diag
106 end type spectrum_t
107
110 integer :: niter_
111 real(real64) :: time_step_, energy_step_
112 complex(real64), allocatable :: func_(:),func_ar_(:,:),pos_(:,:),tret_(:), funcw_(:)
113 type(fft_t), save :: fft_handler
114 integer :: is_, ie_, default
115
117 module procedure spectrum_read_header_line
118 module procedure spectrum_read_header_integer
119 end interface spectrum_read_header
120
121contains
122
123 ! ---------------------------------------------------------
124 subroutine spectrum_init(spectrum, namespace, default_energy_step, default_max_energy)
125 type(spectrum_t), intent(inout) :: spectrum
126 type(namespace_t), intent(in) :: namespace
127 real(real64), optional, intent(in) :: default_energy_step
128 real(real64), optional, intent(in) :: default_max_energy
129
130 real(real64) :: fdefault
131
132 push_sub(spectrum_init)
133
134 call messages_print_with_emphasis(msg="Spectrum Options", namespace=namespace)
135
136 !%Variable PropagationSpectrumType
137 !%Type integer
138 !%Default AbsorptionSpectrum
139 !%Section Utilities::oct-propagation_spectrum
140 !%Description
141 !% Type of spectrum to calculate.
142 !%Option AbsorptionSpectrum 1
143 !% Photoabsorption spectrum.
144 !%Option EnergyLossSpectrum 2
145 !% Dynamic structure factor (also known as energy-loss function or spectrum).
146 !%Option DipolePower 3
147 !% Power spectrum of the dipole moment.
148 !%Option RotatoryStrength 4
149 !% Rotatory strength spectrum.
150 !%End
151
152 call parse_variable(namespace, 'PropagationSpectrumType', spectrum_absorption, spectrum%spectype)
153
154 if (.not. varinfo_valid_option('PropagationSpectrumType', spectrum%spectype)) then
155 call messages_input_error(namespace, 'PropagationSpectrumType')
156 end if
157 call messages_print_var_option('PropagationSpectrumType', spectrum%spectype, namespace=namespace)
158
159 !%Variable SpectrumMethod
160 !%Type integer
161 !%Default fourier
162 !%Section Utilities::oct-propagation_spectrum
163 !%Description
164 !% Decides which method is used to obtain the spectrum.
165 !%Option fourier 1
166 !% The standard Fourier transform. Further specified by <tt>PropagationSpectrumTransform</tt>.
167 !%Option compressed_sensing 2
168 !% (Experimental) Uses the compressed sensing technique.
169 !%End
170 call parse_variable(namespace, 'SpectrumMethod', spectrum_fourier, spectrum%method)
171 if (.not. varinfo_valid_option('SpectrumMethod', spectrum%method)) then
172 call messages_input_error(namespace, 'SpectrumMethod')
173 end if
174 call messages_print_var_option('SpectrumMethod', spectrum%method, namespace=namespace)
175
176 if (spectrum%method == spectrum_compressed_sensing) then
177 call messages_experimental('compressed sensing', namespace=namespace)
179 !%Variable SpectrumSignalNoise
180 !%Type float
181 !%Default 0.0
182 !%Section Utilities::oct-propagation_spectrum
183 !%Description
184 !% For compressed sensing, the signal to process, the
185 !% time-dependent dipole in this case, is assumed to have some
186 !% noise that is given by this dimensionless quantity.
187 !%End
188 call parse_variable(namespace, 'SpectrumSignalNoise', 0.0_real64, spectrum%noise)
189 call messages_print_var_value('SpectrumSignalNoise', spectrum%noise, namespace=namespace)
190 end if
193 !%Variable PropagationSpectrumDampMode
194 !%Type integer
195 !%Section Utilities::oct-propagation_spectrum
196 !%Description
197 !% Decides which damping/filtering is to be applied in order to
198 !% calculate spectra by calculating a Fourier transform. The
199 !% default is polynomial damping, except when <tt>SpectrumMethod = compressed_sensing</tt>.
200 !% In that case the default is none.
201 !%Option none 0
202 !% No filtering at all.
203 !%Option exponential 1
204 !% Exponential filtering, corresponding to a Lorentzian-shaped spectrum.
205 !%Option polynomial 2
206 !% Third-order polynomial damping.
207 !%Option gaussian 3
208 !% Gaussian damping.
209 !%End
211 if (spectrum%method == spectrum_compressed_sensing) default = spectrum_damp_none
212
213 call parse_variable(namespace, 'PropagationSpectrumDampMode', default, spectrum%damp)
214
215 if (.not. varinfo_valid_option('PropagationSpectrumDampMode', spectrum%damp)) then
216 call messages_input_error(namespace, 'PropagationSpectrumDampMode')
217 end if
218 call messages_print_var_option('PropagationSpectrumDampMode', spectrum%damp, namespace=namespace)
220 if (spectrum%method == spectrum_compressed_sensing .and. spectrum%damp /= spectrum_damp_none) then
221 message(1) = 'Using damping with compressed sensing, this is not required'
222 message(2) = 'and can introduce noise in the spectra.'
223 call messages_warning(2, namespace=namespace)
224 end if
225
226 !%Variable PropagationSpectrumTransform
227 !%Type integer
228 !%Default sine
229 !%Section Utilities::oct-propagation_spectrum
230 !%Description
231 !% Decides which transform to perform, if <tt>SpectrumMethod = fourier</tt>.
232 !%Option sine 2
233 !% Sine transform: <math>\int dt \sin(wt) f(t)</math>. Produces the imaginary part of the polarizability.
234 !%Option cosine 3
235 !% Cosine transform: <math>\int dt \cos(wt) f(t)</math>. Produces the real part of the polarizability.
236 !%Option laplace 1
237 !% Real exponential transform: <math>\int dt e^{-wt} f(t)</math>. Produces the real part of the polarizability at imaginary
238 !% frequencies, <i>e.g.</i> for Van der Waals <math>C_6</math> coefficients.
239 !% This is the only allowed choice for complex scaling.
240 !%End
241 call parse_variable(namespace, 'PropagationSpectrumTransform', spectrum_transform_sin, spectrum%transform)
242 if (.not. varinfo_valid_option('PropagationSpectrumTransform', spectrum%transform)) then
243 call messages_input_error(namespace, 'PropagationSpectrumTransform')
244 end if
245 call messages_print_var_option('PropagationSpectrumTransform', spectrum%transform, namespace=namespace)
246
247 !%Variable PropagationSpectrumStartTime
248 !%Type float
249 !%Default 0.0
250 !%Section Utilities::oct-propagation_spectrum
251 !%Description
252 !% Processing is done for the given function in a time-window that starts at the
253 !% value of this variable.
254 !%End
255 call parse_variable(namespace, 'PropagationSpectrumStartTime', m_zero, spectrum%start_time, units_inp%time)
256 call messages_print_var_value('PropagationSpectrumStartTime', spectrum%start_time, unit = units_out%time, namespace=namespace)
257
258 !%Variable PropagationSpectrumEndTime
259 !%Type float
260 !%Default -1.0 au
261 !%Section Utilities::oct-propagation_spectrum
262 !%Description
263 !% Processing is done for the given function in a time-window that ends at the
264 !% value of this variable. If set to a negative value, the maximum value from
265 !% the corresponding multipole file will used.
266 !%End
267 call parse_variable(namespace, 'PropagationSpectrumEndTime', -m_one, spectrum%end_time, units_inp%time)
268 call messages_print_var_value('PropagationSpectrumEndTime', spectrum%end_time, unit = units_out%time, namespace=namespace)
269
270 !%Variable PropagationSpectrumEnergyStep
271 !%Type float
272 !%Default 0.01 eV
273 !%Section Utilities::oct-propagation_spectrum
274 !%Description
275 !% Sampling rate for the spectrum. If you supply a number equal or smaller than zero, then
276 !% the sampling rate will be <math>2 \pi / T</math>, where <math>T</math> is the total propagation time.
277 !%End
278 fdefault = 0.01_real64/(m_two*p_ry)
279 if (present(default_energy_step)) fdefault = default_energy_step
280 call parse_variable(namespace, 'PropagationSpectrumEnergyStep', fdefault, spectrum%energy_step, units_inp%energy)
281 call messages_print_var_value('PropagationSpectrumEnergyStep', spectrum%energy_step, unit = units_out%energy, &
282 namespace=namespace)
283
284 !%Variable PropagationSpectrumMinEnergy
285 !%Type float
286 !%Default 0
287 !%Section Utilities::oct-propagation_spectrum
288 !%Description
289 !% The Fourier transform is calculated for energies larger than this value.
290 !%End
291 call parse_variable(namespace, 'PropagationSpectrumMinEnergy', m_zero, spectrum%min_energy, units_inp%energy)
292 call messages_print_var_value('PropagationSpectrumMinEnergy', spectrum%min_energy, unit = units_out%energy, &
293 namespace=namespace)
294
295
296 !%Variable PropagationSpectrumMaxEnergy
297 !%Type float
298 !%Default 20 eV
299 !%Section Utilities::oct-propagation_spectrum
300 !%Description
301 !% The Fourier transform is calculated for energies smaller than this value.
302 !%End
303 fdefault = 20.0_real64/(m_two*p_ry)
304 if (present(default_max_energy)) fdefault = default_max_energy
305 call parse_variable(namespace, 'PropagationSpectrumMaxEnergy', fdefault, spectrum%max_energy, units_inp%energy)
306 call messages_print_var_value('PropagationSpectrumMaxEnergy', spectrum%max_energy, unit = units_out%energy, &
307 namespace=namespace)
308
309 !%Variable PropagationSpectrumDampFactor
310 !%Type float
311 !%Default -1.0
312 !%Section Utilities::oct-propagation_spectrum
313 !%Description
314 !% If <tt>PropagationSpectrumDampMode = exponential, gaussian</tt>, the damping parameter of the exponential
315 !% is fixed through this variable.
316 !% Default value ensure that the damping function adquires a 0.0001 value at the end of the propagation time.
317 !%End
318 call parse_variable(namespace, 'PropagationSpectrumDampFactor', -m_one, spectrum%damp_factor, units_inp%time**(-1))
319
320 call messages_print_var_value('PropagationSpectrumDampFactor', spectrum%damp_factor, unit = units_out%time**(-1), &
321 namespace=namespace)
322
323 !%Variable PropagationSpectrumSigmaDiagonalization
324 !%Type logical
325 !%Default .false.
326 !%Section Utilities::oct-propagation_spectrum
327 !%Description
328 !% If <tt>PropagationSpectrumSigmaDiagonalization = yes</tt>, the polarizability tensor is diagonalizied.
329 !% This variable is only used if the cross_section_tensor is computed.
330 !%End
331 call parse_variable(namespace, 'PropagationSpectrumSigmaDiagonalization', .false., spectrum%sigma_diag)
332 call messages_print_var_value('PropagationSpectrumSigmaDiagonalization', spectrum%sigma_diag, namespace=namespace)
333
334 call messages_print_with_emphasis(namespace=namespace)
335
336 pop_sub(spectrum_init)
337 end subroutine spectrum_init
338
339
340 ! ---------------------------------------------------------
341 subroutine spectrum_cross_section_tensor(spectrum, namespace, out_file, in_file)
342 type(spectrum_t), intent(inout) :: spectrum
343 type(namespace_t), intent(in) :: namespace
344 integer, intent(in) :: out_file
345 integer, intent(in) :: in_file(:)
346
347 integer :: nspin, energy_steps, ie, is, equiv_axes, n_files, trash
348 real(real64), allocatable :: sigma(:, :, :, :), sigmap(:, :, :, :), sigmau(:, :, :), &
349 sigmav(:, :, :), sigmaw(:, :, :), ip(:, :)
350 real(real64) :: dw, dump
351 type(kick_t) :: kick
352
354
355 n_files = size(in_file)
356 equiv_axes = 3 - n_files + 1
357
358 call spectrum_cross_section_info(namespace, in_file(1), nspin, kick, energy_steps, dw)
359 ! on subsequent calls, do not overwrite energy_steps and dw
360 call io_skip_header(in_file(1))
361
362 safe_allocate(sigma(1:3, 1:3, 1:energy_steps, 1:nspin))
363 safe_allocate(sigmap(1:3, 1:3, 1:energy_steps, 1:nspin))
364 safe_allocate(sigmau(1:3, 1:energy_steps, 1:nspin))
365 safe_allocate(sigmav(1:3, 1:energy_steps, 1:nspin))
366 safe_allocate(sigmaw(1:3, 1:energy_steps, 1:nspin))
367 safe_allocate( ip(1:3, 1:3))
368
369 select case (equiv_axes)
370
371 case (3)
372
373 do ie = 1, energy_steps
374 read(in_file(1), *) dump, (sigmau(1:3, ie, is), is = 1, nspin)
375 end do
376
377 ! The first row of sigma is the vector that we have just read, but properly projected...
378 do is = 1, nspin
379 do ie = 1, energy_steps
380 sigmap(1, 1, ie, is) = sum(sigmau(1:3, ie, is) * kick%pol(1:3, 1))
381 sigmap(1, 2, ie, is) = sum(sigmau(1:3, ie, is) * kick%pol(1:3, 2))
382 sigmap(1, 3, ie, is) = sum(sigmau(1:3, ie, is) * kick%pol(1:3, 3))
383 end do
384 end do
385
386 ! The diagonal parts are also equal:
387 sigmap(2, 2, :, :) = sigmap(1, 1, :, :)
388 sigmap(3, 3, :, :) = sigmap(1, 1, :, :)
389
390 ! The (2,1) term and (3,1) term are equal by symmetry:
391 sigmap(2, 1, :, :) = sigmap(1, 2, :, :)
392 sigmap(3, 1, :, :) = sigmap(1, 3, :, :)
393
394 ! But for the (2,3) term we need the wprime vector....
395 do is = 1, nspin
396 do ie = 1, energy_steps
397 sigmap(2, 3, ie, is) = sum(sigmau(1:3, ie, is) * kick%wprime(1:3))
398 sigmap(3, 2, ie, is) = sigmap(2, 3, ie, is)
399 end do
400 end do
401
402 case (2)
403
404 call spectrum_cross_section_info(namespace, in_file(2), ie, kick, trash, dump)
405 call io_skip_header(in_file(2))
406
407 do ie = 1, energy_steps
408 read(in_file(1), *) dump, (sigmau(1:3, ie, is), is = 1, nspin)
409 read(in_file(2), *) dump, (sigmaw(1:3, ie, is), is = 1, nspin)
410 end do
411
412 ! The first row of sigma is the vector that we have just read, but properly projected...
413 do is = 1, nspin
414 do ie = 1, energy_steps
415 sigmap(1, 1, ie, is) = sum(sigmau(1:3, ie, is) * kick%pol(1:3, 1))
416 sigmap(1, 2, ie, is) = sum(sigmau(1:3, ie, is) * kick%pol(1:3, 2))
417 sigmap(1, 3, ie, is) = sum(sigmau(1:3, ie, is) * kick%pol(1:3, 3))
418 end do
419 end do
420
421 ! The third row of sigma is also the vector that we have just read, but properly projected...
422 do is = 1, nspin
423 do ie = 1, energy_steps
424 sigmap(3, 1, ie, is) = sum(sigmaw(1:3, ie, is) * kick%pol(1:3, 1))
425 sigmap(3, 2, ie, is) = sum(sigmaw(1:3, ie, is) * kick%pol(1:3, 2))
426 sigmap(3, 3, ie, is) = sum(sigmaw(1:3, ie, is) * kick%pol(1:3, 3))
427 end do
428 end do
429
430 ! The diagonal (2,2) is equal by symmetry to (1,1)
431 sigmap(2, 2, :, :) = sigmap(1, 1, :, :)
432
433 ! The (2,1) term and (1,2) term are equal; (2,3) and (3,2), also.
434 sigmap(2, 1, :, :) = sigmap(1, 2, :, :)
435 sigmap(2, 3, :, :) = sigmap(3, 2, :, :)
437 case default
438
439 call spectrum_cross_section_info(namespace, in_file(2), ie, kick, trash, dump)
440 call spectrum_cross_section_info(namespace, in_file(3), ie, kick, trash, dump)
441 call io_skip_header(in_file(2))
442 call io_skip_header(in_file(3))
443
444 do ie = 1, energy_steps
445 read(in_file(1), *) dump, (sigmau(1:3, ie, is), is = 1, nspin)
446 read(in_file(2), *) dump, (sigmav(1:3, ie, is), is = 1, nspin)
447 read(in_file(3), *) dump, (sigmaw(1:3, ie, is), is = 1, nspin)
448 end do
449
450 do is = 1, nspin
451 do ie = 1, energy_steps
452 sigmap(1, 1, ie, is) = sum(sigmau(1:3, ie, is) * kick%pol(1:3, 1))
453 sigmap(1, 2, ie, is) = sum(sigmau(1:3, ie, is) * kick%pol(1:3, 2))
454 sigmap(1, 3, ie, is) = sum(sigmau(1:3, ie, is) * kick%pol(1:3, 3))
455 end do
456 end do
457 do is = 1, nspin
458 do ie = 1, energy_steps
459 sigmap(2, 1, ie, is) = sum(sigmav(1:3, ie, is) * kick%pol(1:3, 1))
460 sigmap(2, 2, ie, is) = sum(sigmav(1:3, ie, is) * kick%pol(1:3, 2))
461 sigmap(2, 3, ie, is) = sum(sigmav(1:3, ie, is) * kick%pol(1:3, 3))
462 end do
463 end do
464 do is = 1, nspin
465 do ie = 1, energy_steps
466 sigmap(3, 1, ie, is) = sum(sigmaw(1:3, ie, is) * kick%pol(1:3, 1))
467 sigmap(3, 2, ie, is) = sum(sigmaw(1:3, ie, is) * kick%pol(1:3, 2))
468 sigmap(3, 3, ie, is) = sum(sigmaw(1:3, ie, is) * kick%pol(1:3, 3))
469 end do
470 end do
471
472 end select
473
474 ! And now, perform the necessary transformation.
475 ip(1:3, 1:3) = kick%pol(1:3, 1:3)
476 call lalg_inverse(3, ip, 'dir')
477 do is = 1, nspin
478 do ie = 1, energy_steps
479 sigma(:, :, ie, is) = matmul(transpose(ip), matmul(sigmap(:, :, ie, is), ip))
480 end do
481 end do
482
483 ! Finally, write down the result
484 call spectrum_cross_section_tensor_write(out_file, sigma, nspin, spectrum%energy_step, &
485 spectrum%min_energy, energy_steps, kick)
486
487 ! Diagonalize sigma tensor
488 if (spectrum%sigma_diag) then
489 call spectrum_sigma_diagonalize(namespace, sigma, nspin, spectrum%energy_step, spectrum%min_energy, energy_steps, kick)
490 end if
491
492 safe_deallocate_a(sigma)
493 safe_deallocate_a(sigmap)
494 safe_deallocate_a(sigmau)
495 safe_deallocate_a(sigmav)
496 safe_deallocate_a(sigmaw)
497 safe_deallocate_a(ip)
498
500 end subroutine spectrum_cross_section_tensor
501
502
503 ! ---------------------------------------------------------
504 subroutine spectrum_cross_section_tensor_write(out_file, sigma, nspin, energy_step, min_energy, energy_steps, kick)
505 integer, intent(in) :: out_file
506 real(real64), intent(in) :: sigma(:, :, :, :)
507 integer, intent(in) :: nspin
508 real(real64), intent(in) :: energy_step, min_energy
509 integer, intent(in) :: energy_steps
510 type(kick_t), optional, intent(in) :: kick
511
512 integer :: is, idir, jdir, ie, ii
513 real(real64) :: average, anisotropy
514 real(real64), allocatable :: pp(:,:), pp2(:,:), ip(:,:)
515 logical :: spins_singlet, spins_triplet
516 character(len=20) :: header_string
517
519
520 spins_singlet = .true.
521 spins_triplet = .false.
522 if (present(kick)) then
523 write(out_file, '(a15,i2)') '# nspin ', nspin
524 call kick_write(kick, out_file)
525 select case (kick_get_type(kick))
526 case (kick_spin_mode)
527 spins_triplet = .true.
528 spins_singlet = .false.
530 spins_triplet = .true.
531 end select
532 end if
533
534 write(out_file, '(a1, a20)', advance = 'no') '#', str_center("Energy", 20)
535 write(out_file, '(a20)', advance = 'no') str_center("(1/3)*Tr[sigma]", 20)
536 write(out_file, '(a20)', advance = 'no') str_center("Anisotropy[sigma]", 20)
537 if (spins_triplet .and. spins_singlet) then
538 write(out_file, '(a20)', advance = 'no') str_center("(1/3)*Tr[sigma-]", 20)
539 end if
540 do is = 1, nspin
541 do idir = 1, 3
542 do jdir = 1, 3
543 write(header_string,'(a6,i1,a1,i1,a1,i1,a1)') 'sigma(', idir, ',', jdir, ',', is, ')'
544 write(out_file, '(a20)', advance = 'no') str_center(trim(header_string), 20)
545 end do
546 end do
547 end do
548 write(out_file, '(1x)')
549 write(out_file, '(a1,a20)', advance = 'no') '#', str_center('[' // trim(units_abbrev(units_out%energy)) // ']', 20)
550 if (spins_triplet .and. spins_singlet) then
551 write(out_file, '(a20)', advance = 'no') str_center('[' // trim(units_abbrev(units_out%length**2)) // ']', 20)
552 end if
553 do ii = 1, 2 + nspin * 9
554 write(out_file, '(a20)', advance = 'no') str_center('[' // trim(units_abbrev(units_out%length**2)) // ']', 20)
555 end do
556 write(out_file, '(1x)')
557
558 ! The anisotropy (Delta alpha) of a second-rank symmetric tensor alpha (such
559 ! as the cross section) is defined as:
560 !
561 ! (Delta alpha)^2 = (1/3) * (3 * Tr(alpha^2) - (Tr(a))^2)
562 !
563 ! The reason for this definition is that it is identically equal to:
564 !
565 ! (Delta alpha)^2 = (1/3) * ( (alpha_1-alpha_2)^2 + (alpha_1-alpha_3)^2 + (alpha_2-alpha_3)^2)
566 !
567 ! where {alpha_1, alpha_2, alpha_3} are the eigenvalues of alpha. An "isotropic" tensor
568 ! is characterized by having three equal eigenvalues, which leads to zero anisotropy. The
569 ! more different that the eigenvalues are, the larger the anisotropy is.
570
571 safe_allocate(pp(1:3, 1:3))
572 if (spins_triplet .and. spins_singlet) then
573 safe_allocate(pp2(1:3, 1:3))
574 end if
575 safe_allocate(ip(1:3, 1:3))
576
577 do ie = 1, energy_steps
578
579 pp(:, :) = sigma(:, :, ie, 1)
580 if (nspin >= 2) then
581 if (spins_singlet .and. spins_triplet) then
582 pp2(:, :) = pp(:, :) - sigma(:, :, ie, 2)
583 pp(:, :) = pp(:, :) + sigma(:, :, ie, 2)
584 elseif (spins_triplet .and. .not. spins_singlet) then
585 pp(:, :) = pp(:, :) - sigma(:, :, ie, 2)
586 elseif (spins_singlet .and. .not. spins_triplet) then
587 pp(:, :) = pp(:, :) + sigma(:, :, ie, 2)
588 end if
589 end if
590
591 average = m_third * (pp(1, 1) + pp(2, 2) + pp(3, 3))
592 ip = matmul(pp, pp)
593 anisotropy = m_third * (m_three * ( ip(1, 1) + ip(2, 2) + ip(3, 3)) - (m_three * average)**2)
594
595 ! Note that the cross-section elements do not have to be transformed to the proper units, since
596 ! they have been read from the "cross_section_vector.x", where they are already in the proper units.
597 write(out_file,'(3e20.8)', advance = 'no') units_from_atomic(units_out%energy, ((ie - 1) * energy_step + min_energy)), &
598 average, sqrt(max(anisotropy, m_zero))
600 if (spins_singlet .and. spins_triplet) then
601 average = m_third * (pp2(1, 1) + pp2(2, 2) + pp2(3, 3))
602 write(out_file,'(1e20.8)', advance = 'no') average
603 end if
604
605 do is = 1, nspin
606 write(out_file,'(9e20.8)', advance = 'no') sigma(1:3, 1:3, ie, is)
607 end do
608 write(out_file, '(1x)')
609 end do
610
611 safe_deallocate_a(pp)
612 if (spins_triplet .and. spins_singlet) then
613 safe_deallocate_a(pp2)
614 end if
615 safe_deallocate_a(ip)
618
619
620 ! ---------------------------------------------------------
621 subroutine spectrum_cross_section(spectrum, namespace, in_file, out_file, ref_file)
622 type(spectrum_t), intent(inout) :: spectrum
623 type(namespace_t), intent(in) :: namespace
624 integer, intent(in) :: in_file
625 integer, intent(in) :: out_file
626 integer, optional, intent(in) :: ref_file
627
628 character(len=20) :: header_string
629 integer :: nspin, ref_nspin, lmax, ref_lmax, time_steps, &
630 ref_time_steps, istart, iend, ntiter, it, ii, isp, no_e, ie, idir
631 real(real64) :: dt, ref_dt, energy, ewsum, polsum
632 type(kick_t) :: kick, ref_kick
633 real(real64), allocatable :: dipole(:, :, :), ref_dipole(:, :, :), sigma(:, :, :), sf(:, :)
634 type(unit_system_t) :: file_units, ref_file_units
635 type(batch_t) :: dipoleb, sigmab
636
637 type(pcm_min_t) :: pcm
638
639 push_sub(spectrum_cross_section)
640
641 ! This function gives us back the unit connected to the "multipoles" file, the header information,
642 ! the number of time steps, and the time step.
643 call spectrum_mult_info(namespace, in_file, nspin, kick, time_steps, dt, file_units, lmax=lmax)
644
645 if (present(ref_file)) then
646 call spectrum_mult_info(namespace, ref_file, ref_nspin, ref_kick, &
647 ref_time_steps, ref_dt, ref_file_units, lmax = ref_lmax)
648 if ((nspin /= ref_nspin) .or. &
649 (time_steps /= ref_time_steps) .or. &
650 (.not.(abs(dt-ref_dt)< 1e-10_real64)) .or. &
651 (lmax /= ref_lmax)) then
652 write(message(1),'(a)') 'The multipoles and reference multipoles files do not match.'
653 call messages_fatal(1, namespace=namespace)
654 end if
655 end if
656
657 ! Now we cannot process files that do not contain the dipole, or that contain more than the dipole.
658 if (lmax /= 1) then
659 message(1) = 'Multipoles file should contain the dipole -- and only the dipole.'
660 call messages_fatal(1, namespace=namespace)
661 end if
662
663 if (kick%function_mode /= kick_function_dipole) then
664 message(1) = "Kick function must have been dipole to run this utility."
665 call messages_fatal(1, namespace=namespace)
666 end if
667
668 if (kick%pol_dir < 1) then
669 message(1) = "Kick polarization direction is not set. Probably no kick was used."
670 call messages_fatal(1, namespace=namespace)
671 end if
672
673 ! Find out the iteration numbers corresponding to the time limits.
674 call spectrum_fix_time_limits(spectrum, time_steps, dt, istart, iend, ntiter)
675
676 safe_allocate(dipole(0:time_steps, 1:3, 1:nspin))
677 call spectrum_read_dipole(namespace, in_file, dipole)
678
679 if (present(ref_file)) then
680 safe_allocate(ref_dipole(0:time_steps, 1:3, 1:nspin))
681 call spectrum_read_dipole(namespace, ref_file, ref_dipole)
682 end if
683
684 ! parsing and re-printing to output useful PCM data
685 call pcm_min_input_parsing_for_spectrum(pcm, namespace)
686
687 ! adding the dipole generated by the PCM polarization charges due solute
688 if (pcm%localf) then
689 call spectrum_add_pcm_dipole(namespace, dipole, time_steps, nspin)
690 end if
691
692 ! Now subtract the initial dipole.
693 if (present(ref_file)) then
694 dipole = dipole - ref_dipole
695 else
696 do it = 1, time_steps
697 dipole(it, :, :) = dipole(it, :, :) - dipole(0, :, :)
698 end do
699 dipole(0, :, :) = m_zero
700 end if
701
702 if (spectrum%energy_step <= m_zero) spectrum%energy_step = m_two * m_pi / (dt*time_steps)
703
704 ! Get the number of energy steps.
705 no_e = spectrum_nenergy_steps(spectrum)
706 safe_allocate(sigma(1:no_e, 1:3, 1:nspin))
707
708 if (pcm%localf) then
709
710 ! for numerical reasons, we cannot add the difference (d(t)-d(t0)) of PCM dipoles here -- although it would look cleaner
711
712 ! in the PCM local field case \sigma(\omega) \propto \Im{\alpha(\omega)\epsilon(\omega)} not just \propto \Im{\alpha(\omega)}
713 ! since the dielectric function is complex as well, we need to compute both the real and imaginary part of the polarizability
714 call spectrum_times_pcm_epsilon(spectrum, pcm, dipole, sigma, nspin, istart, iend, kick%time, dt, no_e)
715
716 write(out_file,'(a59)') "# Cross-section spectrum contains full local field effects."
717
718 else
719
720 call batch_init(dipoleb, 3, 1, nspin, dipole)
721 call batch_init(sigmab, 3, 1, nspin, sigma)
722
723 call spectrum_signal_damp(spectrum%damp, spectrum%damp_factor, istart + 1, iend + 1, kick%time, dt, dipoleb)
724 call spectrum_fourier_transform(spectrum%method, spectrum%transform, spectrum%noise, &
725 istart + 1, iend + 1, kick%time, dt, dipoleb, spectrum%min_energy, spectrum%max_energy, spectrum%energy_step, sigmab)
726
727 call dipoleb%end()
728 call sigmab%end()
729
730 end if
731
732 if (pcm%run_pcm) then
733 call spectrum_over_pcm_refraction_index(spectrum, pcm, sigma, nspin, no_e)
734 end if
735
736
737 safe_deallocate_a(dipole)
738 if (present(ref_file)) then
739 safe_deallocate_a(ref_dipole)
740 end if
741
742 safe_allocate(sf(1:no_e, nspin))
743
744 if (abs(kick%delta_strength) < 1e-12_real64) kick%delta_strength = m_one
745 do ie = 1, no_e
746 energy = (ie-1) * spectrum%energy_step + spectrum%min_energy
747 do isp = 1, nspin
748 sf(ie, isp) = sum(sigma(ie, 1:3, isp)*kick%pol(1:3, kick%pol_dir))
749 end do
750 sf(ie, 1:nspin) = -sf(ie, 1:nspin) * (energy * m_two) / (m_pi * kick%delta_strength)
751 sigma(ie, 1:3, 1:nspin) = -sigma(ie, 1:3, 1:nspin)*(m_four*m_pi*energy/p_c)/kick%delta_strength
752 end do
753
754 ! The formulae below are only correct in this particular case.
755 if (kick_get_type(kick) == kick_density_mode .and. spectrum%transform == spectrum_transform_sin) then
756 ewsum = sum(sf(1, 1:nspin))
757 polsum = m_zero
758
759 do ie = 2, no_e
760 energy = (ie-1) * spectrum%energy_step + spectrum%min_energy
761 ewsum = ewsum + sum(sf(ie, 1:nspin))
762 polsum = polsum + sum(sf(ie, 1:nspin)) / energy**2
763 end do
764
765 ewsum = ewsum * spectrum%energy_step
766 polsum = polsum * spectrum%energy_step
767 end if
768
769 write(out_file, '(a15,i2)') '# nspin ', nspin
770 call kick_write(kick, out_file)
771 write(out_file, '(a)') '#%'
772 write(out_file, '(a,i8)') '# Number of time steps = ', time_steps
773 call spectrum_write_info(spectrum, out_file)
774 write(out_file, '(a)') '#%'
775 if (kick_get_type(kick) == kick_density_mode .and. spectrum%transform == spectrum_transform_sin) then
776 write(out_file, '(a,f16.6)') '# Electronic sum rule = ', ewsum
777 write(out_file, '(a,f16.6,1x,a)') '# Static polarizability (from sum rule) = ', &
778 units_from_atomic(units_out%length**3, polsum), trim(units_abbrev(units_out%length))
779 write(out_file, '(a)') '#%'
780 end if
781
782 write(out_file, '(a1,a20)', advance = 'no') '#', str_center("Energy", 20)
783 do isp = 1, nspin
784 do idir = 1, 3
785 write(header_string,'(a6,i1,a8,i1,a1)') 'sigma(', idir, ', nspin=', isp, ')'
786 write(out_file, '(a20)', advance = 'no') str_center(trim(header_string), 20)
787 end do
788 end do
789 do isp = 1, nspin
790 write(header_string,'(a18,i1,a1)') 'StrengthFunction(', isp, ')'
791 write(out_file, '(a20)', advance = 'no') str_center(trim(header_string), 20)
792 end do
793 write(out_file, '(1x)')
794 write(out_file, '(a1,a20)', advance = 'no') '#', str_center('['//trim(units_abbrev(units_out%energy)) // ']', 20)
795 do ii = 1, nspin * 3
796 write(out_file, '(a20)', advance = 'no') str_center('[' // trim(units_abbrev(units_out%length**2)) // ']', 20)
797 end do
798 do isp = 1, nspin
799 write(out_file, '(a20)', advance = 'no') str_center('[' // trim(units_abbrev(unit_one/units_out%energy)) // ']', 20)
800 end do
801 write(out_file, '(1x)')
802
803 do ie = 1, no_e
804 write(out_file,'(e20.8)', advance = 'no') units_from_atomic(units_out%energy, &
805 (ie-1) * spectrum%energy_step + spectrum%min_energy)
806 do isp = 1, nspin
807 write(out_file,'(3e20.8)', advance = 'no') (units_from_atomic(units_out%length**2, sigma(ie, idir, isp)), &
808 idir = 1, 3)
809 end do
810 do isp = 1, nspin
811 write(out_file,'(e20.8)', advance = 'no') units_from_atomic(unit_one/units_out%energy, sf(ie, isp))
812 end do
813 write(out_file, '(1x)')
814 end do
815
816 safe_deallocate_a(sigma)
818 end subroutine spectrum_cross_section
819
820 ! ---------------------------------------------------------
821
822 subroutine spectrum_read_dipole(namespace, in_file, dipole)
823 type(namespace_t), intent(in) :: namespace
824 integer, intent(in) :: in_file
825 real(real64), intent(out) :: dipole(0:, :, :)
826
827 integer :: nspin, lmax, time_steps, trash, it, idir, ispin
828 real(real64) :: dt, dump
829 type(kick_t) :: kick
830 type(unit_system_t) :: file_units
831
832 push_sub(spectrum_read_dipole)
833
834 ! This function gives us back the unit connected to the "multipoles" file, the header information,
835 ! the number of time steps, and the time step.
836 call spectrum_mult_info(namespace, in_file, nspin, kick, time_steps, dt, file_units, lmax = lmax)
837
838 ! Read the dipole.
839 call io_skip_header(in_file)
840
841 do it = 0, time_steps
842 dipole(it, :, :) = m_zero
843 read(in_file, *) trash, dump, (dump, (dipole(it, idir, ispin), idir = 1, kick%dim), ispin = 1, nspin)
844 end do
845 dipole(:,:,:) = units_to_atomic(file_units%length, dipole(:,:,:))
846
847 pop_sub(spectrum_read_dipole)
848
849 end subroutine spectrum_read_dipole
850
851 ! ---------------------------------------------------------
852
853 subroutine spectrum_add_pcm_dipole(namespace, dipole, time_steps, nspin)
854 type(namespace_t), intent(in) :: namespace
855 real(real64), intent(inout) :: dipole(0:, :, :)
856 integer, intent(in) :: time_steps
857 integer, intent(in) :: nspin
858
859 type(pcm_t) :: pcm
860 real(real64) :: dipole_pcm(1:3)
861 integer :: ia, it
862
863 ! unit io variables
864 integer :: asc_unit_test
865 integer :: cavity_unit
866 integer :: asc_vs_t_unit, asc_vs_t_unit_check
867 integer :: dipole_vs_t_unit_check, dipole_vs_t_unit_check1
868 integer :: iocheck
869 integer :: aux_int
870 real(real64) :: aux_float, aux_float1, aux_vec(1:3)
871 character(len=23) :: asc_vs_t_unit_format
872 character(len=16) :: asc_vs_t_unit_format_tail
873
875
876 ! reading PCM cavity from standard output file in two steps
877
878 ! first step - counting tesserae
879 asc_unit_test = io_open(pcm_dir//'ASC_e.dat', namespace, action='read')
880 pcm%n_tesserae = 0
881 iocheck = 1
882 do while(iocheck >= 0)
883 read(asc_unit_test,*,iostat=iocheck) aux_vec(1:3), aux_float, aux_int
884 if (iocheck >= 0) pcm%n_tesserae = pcm%n_tesserae + 1
885 end do
886 call io_close(asc_unit_test)
887
888 ! intermezzo - allocating PCM tessellation and polarization charges arrays
889 safe_allocate(pcm%tess(1:pcm%n_tesserae))
890 safe_allocate(pcm%q_e(1:pcm%n_tesserae))
891 safe_allocate(pcm%q_e_in(1:pcm%n_tesserae)) ! with auxiliary role
892
893 ! second step - reading of PCM tessellation arrays from standard output file
894 ! writing the cavity to debug-purpose file
895 asc_unit_test = io_open(pcm_dir//'ASC_e.dat', namespace, action='read')
896 cavity_unit = io_open(pcm_dir//'cavity_check.xyz', namespace, action='write')
897 write(cavity_unit,'(I3)') pcm%n_tesserae
898 write(cavity_unit,*)
899 do ia = 1, pcm%n_tesserae
900 read(asc_unit_test,*) pcm%tess(ia)%point(1:3), aux_float, aux_int
901 write(cavity_unit,'(A1,3(1X,F14.8))') 'H', pcm%tess(ia)%point(1:3)
902 end do
903 call io_close(asc_unit_test)
904 call io_close(cavity_unit)
905
906 write (asc_vs_t_unit_format_tail,'(I5,A11)') pcm%n_tesserae,'(1X,F14.8))'
907 write (asc_vs_t_unit_format,'(A)') '(F14.8,'//trim(adjustl(asc_vs_t_unit_format_tail))
908
909 ! Now, summary: * read the time-dependent PCM polarization charges due to solute electrons, pcm%q_e
910 ! * compute the real-time dipole generated by pcm%q_e, dipole_pcm
911 ! * add it to the real-time molecular dipole
912 ! * write the total dipole and its PCM contribution to debug-purpose files
913 ! N.B. we assume nuclei fixed in time
914
915 ! opening time-dependent PCM charges standard and debug-purpose file
916 asc_vs_t_unit = io_open(pcm_dir//'ASC_e_vs_t.dat', namespace, action='read', form='formatted')
917 asc_vs_t_unit_check = io_open(pcm_dir//'ASC_e_vs_t_check.dat', namespace, action='write', form='formatted')
918
919 ! opening time-dependent PCM and total dipole debug-purpose files
920 dipole_vs_t_unit_check = io_open(pcm_dir//'dipole_e_vs_t_check.dat', namespace, action='write', form='formatted')
921 dipole_vs_t_unit_check1 = io_open(pcm_dir//'dipole_e_vs_t_check1.dat', namespace, action='write', form='formatted')
922
923 ! reading PCM charges for the zeroth-iteration - not used - pcm%q_e_in is only auxiliary here
924 read(asc_vs_t_unit,trim(adjustl(asc_vs_t_unit_format))) aux_float1, ( pcm%q_e_in(ia) , ia=1,pcm%n_tesserae)
925
926 do it = 1, time_steps
927
928 ! reading real-time PCM charges due to electrons per timestep
929 read(asc_vs_t_unit,trim(adjustl(asc_vs_t_unit_format))) aux_float, ( pcm%q_e(ia) , ia=1,pcm%n_tesserae)
930
931 ! computing real-time PCM dipole per timestep
932 call pcm_dipole(dipole_pcm(1:3), -pcm%q_e(1:pcm%n_tesserae), pcm%tess, pcm%n_tesserae)
933
934 ! adding PCM dipole to the molecular dipole per timestep
935 dipole(it, 1, 1:nspin) = dipole(it, 1, 1:nspin) + dipole_pcm(1)
936 dipole(it, 2, 1:nspin) = dipole(it, 2, 1:nspin) + dipole_pcm(2)
937 dipole(it, 3, 1:nspin) = dipole(it, 3, 1:nspin) + dipole_pcm(3)
938
939 ! since we always have a kick for the optical spectrum in Octopus
940 ! the first-iteration dipole should be equal to the zeroth-iteration one
941 ! in any case, made them equal by hand
942 if (it == 1) then
943 dipole(0, 1, 1:nspin) = dipole(1, 1, 1:nspin)
944 dipole(0, 2, 1:nspin) = dipole(1, 2, 1:nspin)
945 dipole(0, 3, 1:nspin) = dipole(1, 3, 1:nspin)
946 end if
947
948 ! writing real-time PCM charges and dipole, and the total dipole for debug purposes
949 write(asc_vs_t_unit_check,trim(adjustl(asc_vs_t_unit_format))) aux_float, (pcm%q_e(ia), ia=1,pcm%n_tesserae)
950 write(dipole_vs_t_unit_check,'(F14.8,3(1X,F14.8))') aux_float, dipole_pcm
951 write(dipole_vs_t_unit_check1,'(F14.8,3(1X,F14.8))') aux_float, dipole(it,:,1)
952
953 end do
954
955 ! closing PCM and debug files
956 call io_close(asc_vs_t_unit)
957 call io_close(asc_vs_t_unit_check)
958 call io_close(dipole_vs_t_unit_check)
959 call io_close(dipole_vs_t_unit_check1)
960
961 ! deallocating PCM arrays
962 safe_deallocate_a(pcm%tess)
963 safe_deallocate_a(pcm%q_e)
964 safe_deallocate_a(pcm%q_e_in)
965
967
968 end subroutine spectrum_add_pcm_dipole
969
970 ! ---------------------------------------------------------
971
972 subroutine spectrum_times_pcm_epsilon(spectrum, pcm, dipole, sigma, nspin, istart, iend, kick_time, dt, no_e)
973 type(spectrum_t), intent(in) :: spectrum
974 type(pcm_min_t) , intent(in) :: pcm
975 real(real64), allocatable, intent(inout) :: sigma(:, :, :)
976 real(real64), allocatable, intent(in) :: dipole(:, :, :)
977 integer, intent(in) :: nspin
978 real(real64), intent(in) :: kick_time
979 integer, intent(in) :: istart, iend
980 real(real64), intent(in) :: dt
981 integer, intent(in) :: no_e
982
983 real(real64), allocatable :: sigmap(:, :, :)
984 type(batch_t) :: dipoleb, sigmab
985
986 integer :: ie
987
988 complex(real64), allocatable :: eps(:)
989
991
992 ! imaginary part of the polarizability
993
994 call batch_init(dipoleb, 3, 1, nspin, dipole)
995 call batch_init(sigmab, 3, 1, nspin, sigma)
996
997 call spectrum_signal_damp(spectrum%damp, spectrum%damp_factor, istart + 1, iend + 1, kick_time, dt, dipoleb)
998 call spectrum_fourier_transform(spectrum%method, spectrum_transform_sin, spectrum%noise, &
999 istart + 1, iend + 1, kick_time, dt, dipoleb, spectrum%min_energy, spectrum%max_energy, spectrum%energy_step, sigmab)
1000
1001 call dipoleb%end()
1002 call sigmab%end()
1003
1004 ! real part of the polarizability
1005
1006 safe_allocate(sigmap(1:no_e, 1:3, 1:nspin))
1007
1008 call batch_init(dipoleb, 3, 1, nspin, dipole)
1009 call batch_init(sigmab, 3, 1, nspin, sigmap)
1010
1011 call spectrum_signal_damp(spectrum%damp, spectrum%damp_factor, istart + 1, iend + 1, kick_time, dt, dipoleb)
1012 call spectrum_fourier_transform(spectrum%method, spectrum_transform_cos, spectrum%noise, &
1013 istart + 1, iend + 1, kick_time, dt, dipoleb, spectrum%min_energy, spectrum%max_energy, spectrum%energy_step, sigmab)
1014
1015 call dipoleb%end()
1016 call sigmab%end()
1017
1018 safe_allocate(eps(1:no_e))
1019
1020 ! multiplying by the dielectric function and taking the imaginary part of the product
1021
1022 do ie = 1, no_e
1023 call pcm_eps(pcm, eps(ie), (ie-1)*spectrum%energy_step + spectrum%min_energy)
1024 sigma(ie, 1:3, 1:nspin) = sigma(ie, 1:3, 1:nspin) * real(eps(ie), real64) + sigmap(ie, 1:3, 1:nspin) *aimag(eps(ie))
1025 end do
1026
1027 safe_deallocate_a(sigmap)
1028 safe_deallocate_a(eps)
1029
1031
1032 end subroutine spectrum_times_pcm_epsilon
1033
1034 ! ---------------------------------------------------------
1035
1036 subroutine spectrum_over_pcm_refraction_index(spectrum, pcm, sigma, nspin, no_e)
1037 type(spectrum_t), intent(in) :: spectrum
1038 type(pcm_min_t) , intent(in) :: pcm
1039 real(real64), allocatable, intent(inout) :: sigma(:, :, :)
1040 integer, intent(in) :: nspin
1041 integer, intent(in) :: no_e
1042
1043 integer :: ie
1044
1045 complex(real64), allocatable :: eps(:)
1046
1048
1049 safe_allocate(eps(1:no_e))
1050
1051 ! dividing by the refraction index - n(\omega)=\sqrt{\frac{|\epsilon(\omega)|+\Re[\epsilon(\omega)]}{2}}
1052
1053 do ie = 1, no_e
1054 call pcm_eps(pcm, eps(ie), (ie-1)*spectrum%energy_step + spectrum%min_energy)
1055 sigma(ie, 1:3, 1:nspin) = sigma(ie, 1:3, 1:nspin) / sqrt(0.5_real64 * (abs(eps(ie)) + real(eps(ie), real64)))
1056 end do
1057
1058 safe_deallocate_a(eps)
1059
1061
1063
1064 ! ---------------------------------------------------------
1065 subroutine spectrum_dipole_power(spectrum, namespace, in_file, out_file)
1066 type(spectrum_t), intent(inout) :: spectrum
1067 type(namespace_t), intent(in) :: namespace
1068 integer, intent(in) :: in_file
1069 integer, intent(in) :: out_file
1070
1071 character(len=20) :: header_string
1072 integer :: nspin, lmax, time_steps, istart, iend, ntiter, it, ii, isp, no_e, ie, idir
1073 real(real64) :: dt
1074 real(real64), allocatable :: dipole(:, :, :), transform_cos(:, :, :), transform_sin(:, :, :), power(:, :, :)
1075 type(unit_system_t) :: file_units
1076 type(batch_t) :: dipoleb, transformb_cos, transformb_sin
1077 type(kick_t) :: kick
1078
1079 push_sub(spectrum_dipole_power)
1080
1081 ! This function gives us back the unit connected to the "multipoles" file, the header information,
1082 ! the number of time steps, and the time step.
1083 call spectrum_mult_info(namespace, in_file, nspin, kick, time_steps, dt, file_units, lmax=lmax)
1084
1085 ! Now we cannot process files that do not contain the dipole, or that contain more than the dipole.
1086 if (lmax /= 1) then
1087 message(1) = 'Multipoles file should contain the dipole -- and only the dipole.'
1088 call messages_fatal(1, namespace=namespace)
1089 end if
1090
1091 ! Find out the iteration numbers corresponding to the time limits.
1092 call spectrum_fix_time_limits(spectrum, time_steps, dt, istart, iend, ntiter)
1093
1094 safe_allocate(dipole(0:time_steps, 1:3, 1:nspin))
1095 call spectrum_read_dipole(namespace, in_file, dipole)
1096
1097 ! Now subtract the initial dipole.
1098 do it = 1, time_steps
1099 dipole(it, :, :) = dipole(it, :, :) - dipole(0, :, :)
1100 end do
1101 dipole(0, :, :) = m_zero
1102
1103 if (spectrum%energy_step <= m_zero) spectrum%energy_step = m_two * m_pi / (dt*time_steps)
1104
1105 ! Get the number of energy steps.
1106 no_e = spectrum_nenergy_steps(spectrum)
1107 safe_allocate(transform_cos(1:no_e, 1:3, 1:nspin))
1108 safe_allocate(transform_sin(1:no_e, 1:3, 1:nspin))
1109 safe_allocate(power(1:no_e, 1:3, 1:nspin))
1110
1111
1112 call batch_init(dipoleb, 3, 1, nspin, dipole)
1113 call batch_init(transformb_cos, 3, 1, nspin, transform_cos)
1114 call batch_init(transformb_sin, 3, 1, nspin, transform_sin)
1115
1116 call spectrum_signal_damp(spectrum%damp, spectrum%damp_factor, istart + 1, iend + 1, spectrum%start_time, dt, dipoleb)
1117
1118 call spectrum_fourier_transform(spectrum%method, spectrum_transform_cos, spectrum%noise, &
1119 istart + 1, iend + 1, spectrum%start_time, dt, dipoleb, spectrum%min_energy, &
1120 spectrum%max_energy, spectrum%energy_step, transformb_cos)
1121 call spectrum_fourier_transform(spectrum%method, spectrum_transform_sin, spectrum%noise, &
1122 istart + 1, iend + 1, spectrum%start_time, dt, dipoleb, spectrum%min_energy, &
1123 spectrum%max_energy, spectrum%energy_step, transformb_sin)
1124
1125 do ie = 1, no_e
1126 power(ie, :, :) = (transform_sin(ie, :, :)**2 + transform_cos(ie, :, :)**2)
1127 end do
1128
1129 call dipoleb%end()
1130 call transformb_cos%end()
1131 call transformb_sin%end()
1132
1133 safe_deallocate_a(dipole)
1134 safe_deallocate_a(transform_sin)
1135 safe_deallocate_a(transform_cos)
1136
1137 write(out_file, '(a15,i2)') '# nspin ', nspin
1138 write(out_file, '(a)') '#%'
1139 write(out_file, '(a,i8)') '# Number of time steps = ', time_steps
1140 call spectrum_write_info(spectrum, out_file)
1141 write(out_file, '(a)') '#%'
1142
1143 write(out_file, '(a1,a20,1x)', advance = 'no') '#', str_center("Energy", 20)
1144 do isp = 1, nspin
1145 do idir = 1, 3
1146 write(header_string,'(a6,i1,a8,i1,a1)') 'power(', idir, ', nspin=', isp, ')'
1147 write(out_file, '(a20)', advance = 'no') str_center(trim(header_string), 20)
1148 end do
1149 end do
1150 write(out_file, '(1x)')
1151 write(out_file, '(a1,a20)', advance = 'no') '#', str_center('['//trim(units_abbrev(units_out%energy)) // ']', 20)
1152 do ii = 1, nspin * 3
1153 write(out_file, '(a20)', advance = 'no') str_center('[' // trim(units_abbrev(units_out%length**2)) // ']', 20)
1154 end do
1155 write(out_file, '(1x)')
1156
1157 do ie = 1, no_e
1158 write(out_file,'(e20.8)', advance = 'no') units_from_atomic(units_out%energy, &
1159 (ie-1) * spectrum%energy_step + spectrum%min_energy)
1160 do isp = 1, nspin
1161 write(out_file,'(3e20.8)', advance = 'no') (units_from_atomic(units_out%length**2, power(ie, idir, isp)), &
1162 idir = 1, 3)
1163 end do
1164 write(out_file, '(1x)')
1165 end do
1166
1167 safe_deallocate_a(power)
1168
1169 pop_sub(spectrum_dipole_power)
1170 end subroutine spectrum_dipole_power
1171
1172 ! ---------------------------------------------------------
1173 subroutine spectrum_dyn_structure_factor(spectrum, namespace, in_file_sin, in_file_cos, out_file)
1174 type(spectrum_t), intent(inout) :: spectrum
1175 type(namespace_t), intent(in) :: namespace
1176 integer, intent(in) :: in_file_sin, in_file_cos
1177 integer, intent(in) :: out_file
1178
1179 character(len=20) :: header_string
1180 integer :: time_steps, time_steps_sin, time_steps_cos
1181 integer :: istart, iend, ntiter, it, jj, ii, no_e, ie, trash
1182 real(real64) :: dt, dt_sin, dt_cos
1183 real(real64) :: dump, dummy1, dummy2, dummy3, dummy4, energy, fsum
1184 type(kick_t) :: kick
1185 complex(real64) :: xx
1186 complex(real64), allocatable :: ftchd(:), chi(:), damp(:)
1187 type(unit_system_t) :: file_units
1188 character(len=100) :: line
1189
1191
1192 safe_allocate(kick%qvector(1:3, 1:1))
1193
1194 ! Read information from ftchds.sin file
1195
1196 rewind(in_file_sin)
1197
1198 ! skip two lines
1199 read(in_file_sin, *)
1200 read(in_file_sin, *)
1201 read(in_file_sin, '(15x,i2)') kick%qkick_mode
1202 read(in_file_sin, '(10x,3f9.5)') kick%qvector
1203 read(in_file_sin, '(15x,f18.12)') kick%delta_strength
1204
1205 ! skip two lines
1206 read(in_file_sin, *)
1207 read(in_file_sin, '(a)') line
1208 call io_skip_header(in_file_sin)
1209 call io_skip_header(in_file_cos)
1210
1211 ! Figure out the units of the file
1212 ii = index(line, 'eV')
1213 if (ii /= 0) then
1214 call unit_system_get(file_units, units_eva)
1215 else
1216 call unit_system_get(file_units, units_atomic)
1217 end if
1218
1219 ! get time_steps and dt, and make sure that dt is the same in the two files
1220 call spectrum_count_time_steps(namespace, in_file_sin, time_steps_sin, dt_sin)
1221 call spectrum_count_time_steps(namespace, in_file_cos, time_steps_cos, dt_cos)
1222
1223 if (.not. is_close(dt_sin, dt_cos)) then
1224 message(1) = "dt is different in ftchds.cos and ftchds.sin!"
1225 call messages_fatal(1, namespace=namespace)
1226 end if
1227
1228 time_steps = min(time_steps_sin, time_steps_cos)
1229 dt = units_to_atomic(file_units%time, dt_cos) ! units_out is OK
1230
1231 ! Find out the iteration numbers corresponding to the time limits.
1232 call spectrum_fix_time_limits(spectrum, time_steps, dt, istart, iend, ntiter)
1233
1234 ! Read the f-transformed charge density.
1235 call io_skip_header(in_file_sin)
1236 call io_skip_header(in_file_cos)
1237
1238 safe_allocate(ftchd(0:time_steps))
1239 do it = 0, time_steps
1240 read(in_file_sin, *) trash, dump, dummy1, dummy2
1241 read(in_file_cos, *) trash, dump, dummy3, dummy4
1242 ftchd(it) = cmplx(dummy3-dummy2, dummy4+dummy1, real64)
1243 end do
1244
1245 ! Now subtract the initial value.
1246 do it = 1, time_steps
1247 ftchd(it) = ftchd(it) - ftchd(0)
1248 end do
1249 ftchd(0) = m_zero
1250
1251 if (spectrum%energy_step <= m_zero) spectrum%energy_step = m_two * m_pi / (dt*time_steps)
1252
1253 ! Get the number of energy steps.
1254 no_e = spectrum_nenergy_steps(spectrum)
1255
1256 safe_allocate(chi(1:no_e))
1257 chi = m_zero
1258
1259 ! Gets the damp function
1260 safe_allocate(damp(istart:iend))
1261 do it = istart, iend
1262 jj = it - istart
1263 select case (spectrum%damp)
1264 case (spectrum_damp_none)
1265 damp(it) = m_one
1267 damp(it)= exp(-jj * dt * spectrum%damp_factor)
1269 damp(it) = m_one - m_three * (real(jj, real64) / ntiter)**2 &
1270 + m_two * (real(jj, real64) / ntiter)**3
1272 damp(it)= exp(-(jj * dt)**2 * spectrum%damp_factor**2)
1273 end select
1274 end do
1275
1276 ! Fourier transformation from time to frequency
1277 if (abs(kick%delta_strength) < 1.d-12) kick%delta_strength = m_one
1278 do ie = 1, no_e
1279 energy = (ie-1) * spectrum%energy_step + spectrum%min_energy
1280 do it = istart, iend
1281 jj = it - istart
1282
1283 xx = exp(m_zi * energy * jj * dt)
1284 chi(ie) = chi(ie) + xx * damp(it) * ftchd(it)
1285
1286 end do
1287 chi(ie) = chi(ie) * dt / kick%delta_strength / m_pi
1288 end do
1289
1290 ! Test f-sum rule
1291 fsum = m_zero
1292 do ie = 1, no_e
1293 energy = (ie-1) * spectrum%energy_step + spectrum%min_energy
1294 fsum = fsum + energy * aimag(chi(ie))
1295 end do
1296 fsum = spectrum%energy_step * fsum * 2/sum(kick%qvector(:,1)**2)
1297
1298 write(out_file, '(a)') '#%'
1299 write(out_file, '(a,i8)') '# Number of time steps = ', time_steps
1300 call spectrum_write_info(spectrum, out_file)
1301 write(out_file, '(a,3f9.5)') '# qvector : ', kick%qvector
1302 write(out_file, '(a,f10.4)') '# F-sum rule : ', fsum
1303 write(out_file, '(a)') '#%'
1304
1305 write(out_file, '(a1,a20)', advance = 'no') '#', str_center("Energy", 20)
1306 write(header_string,'(a3)') 'chi'
1307 write(out_file, '(a20)', advance = 'no') str_center(trim(header_string), 20)
1308 write(out_file, '(1x)')
1309 write(out_file, '(a1,a20)', advance = 'no') '#', str_center('['//trim(units_abbrev(units_out%energy)) // ']', 20)
1310 write(out_file, '(a20)', advance = 'no') str_center('[' // trim(units_abbrev(units_out%energy)) // '**(-1)]', 20)
1311 write(out_file, '(1x)')
1312
1313 do ie = 1, no_e
1314 write(out_file,'(e20.8)', advance = 'no') units_from_atomic(units_out%energy, &
1315 (ie-1) * spectrum%energy_step + spectrum%min_energy)
1316 write(out_file,'(e20.8)', advance = 'no') units_from_atomic(units_out%energy**(-1), aimag(chi(ie)))
1317 write(out_file, '(1x)')
1318 end do
1319
1320 safe_deallocate_a(ftchd)
1321 safe_deallocate_a(chi)
1323
1324 end subroutine spectrum_dyn_structure_factor
1325
1326
1327 ! ---------------------------------------------------------
1328 subroutine spectrum_rotatory_strength(spectrum, namespace, in_file, out_file)
1329 type(spectrum_t), intent(inout) :: spectrum
1330 type(namespace_t), intent(in) :: namespace
1331 integer, intent(in) :: in_file
1332 integer, intent(in) :: out_file
1333
1334 integer :: istart, iend, ntiter, ie, idir, time_steps, no_e, nspin, trash, it
1335 real(real64) :: dump, dt, energy
1336 type(kick_t) :: kick
1337 complex(real64) :: sum1, sum2, sp
1338 real(real64), allocatable :: angular(:, :), resp(:), imsp(:)
1339 type(batch_t) :: angularb, respb, imspb
1340 type(unit_system_t) :: file_units
1341
1343
1344 call spectrum_mult_info(namespace, in_file, nspin, kick, time_steps, dt, file_units)
1345 call spectrum_fix_time_limits(spectrum, time_steps, dt, istart, iend, ntiter)
1346
1347 if (kick%dim /= 3) then
1348 message(1) = "Rotatory strength can only be computed for 3D systems."
1349 call messages_fatal(1, namespace=namespace)
1350 end if
1351
1352 ! load angular momentum from file
1353 safe_allocate(angular(0:time_steps, 1:3))
1354 call io_skip_header(in_file)
1355 do ie = 0, time_steps
1356 read(in_file, *) trash, dump, (angular(ie, idir), idir = 1, 3)
1357 end do
1358
1359 ! subtract static dipole
1360 do idir = 1, 3
1361 angular(:, idir) = angular(:, idir) - angular(0, idir)
1362 end do
1363
1364 if (spectrum%energy_step <= m_zero) spectrum%energy_step = m_two * m_pi / (dt*time_steps)
1365
1366 no_e = spectrum_nenergy_steps(spectrum)
1367
1368 do it = istart, iend
1369 angular(it, 1) = sum(angular(it, 1:3)*kick%pol(1:3, kick%pol_dir))
1370 end do
1371
1372 safe_allocate(resp(1:no_e))
1373 safe_allocate(imsp(1:no_e))
1374
1375 call batch_init(angularb, angular(:, 1))
1376 call batch_init(respb, resp)
1377 call batch_init(imspb, imsp)
1378
1379 call spectrum_signal_damp(spectrum%damp, spectrum%damp_factor, istart + 1, iend + 1, kick%time, dt, angularb)
1380
1381 call spectrum_fourier_transform(spectrum%method, spectrum_transform_cos, spectrum%noise, &
1382 istart + 1, iend + 1, kick%time, dt, angularb, spectrum%min_energy, spectrum%max_energy, spectrum%energy_step, respb)
1383 call spectrum_fourier_transform(spectrum%method, spectrum_transform_sin, spectrum%noise, &
1384 istart + 1, iend + 1, kick%time, dt, angularb, spectrum%min_energy, spectrum%max_energy, spectrum%energy_step, imspb)
1385
1386 call angularb%end()
1387 call respb%end()
1388 call imspb%end()
1389
1390 sum1 = m_z0
1391 sum2 = m_z0
1392 if (abs(kick%delta_strength) < 1.d-12) kick%delta_strength = m_one
1393 do ie = 1, no_e
1394 energy = (ie-1) * spectrum%energy_step + spectrum%min_energy
1395
1396 sp = cmplx(resp(ie), imsp(ie), real64)
1397
1398 sp = sp*m_zi/(m_two*p_c*kick%delta_strength)
1399
1400 sum1 = sum1 + spectrum%energy_step*sp
1401 sum2 = sum2 + spectrum%energy_step*sp*energy**2
1402
1403 resp(ie) = real(sp, real64)
1404 imsp(ie) = aimag(sp)
1405 end do
1406
1407 safe_deallocate_a(angular)
1408
1409 ! print some info
1410 write(message(1), '(a,i8)') 'Number of time steps = ', ntiter
1411 write(message(2), '(a,i4)') 'PropagationSpectrumDampMode = ', spectrum%damp
1412 write(message(3), '(a,f10.4)') 'PropagationSpectrumDampFactor = ', units_from_atomic(units_out%time**(-1), spectrum%damp_factor)
1413 write(message(4), '(a,f10.4)') 'PropagationSpectrumStartTime = ', units_from_atomic(units_out%time, spectrum%start_time)
1414 write(message(5), '(a,f10.4)') 'PropagationSpectrumEndTime = ', units_from_atomic(units_out%time, spectrum%end_time)
1415 write(message(6), '(a,f10.4)') 'PropagationSpectrumMaxEnergy = ', units_from_atomic(units_inp%energy, spectrum%max_energy)
1416 write(message(7),'(a,f10.4)') 'PropagationSpectrumEnergyStep = ', units_from_atomic(units_inp%energy, spectrum%energy_step)
1417 message(8) = ""
1418 write(message(9), '(a,5e15.6,5e15.6)') 'R(0) sum rule = ', sum1
1419 write(message(10),'(a,5e15.6,5e15.6)') 'R(2) sum rule = ', sum2
1420 call messages_info(10, namespace=namespace)
1421
1422
1423 ! Output to file
1424 write(out_file, '(a15,i2)') '# nspin ', nspin
1425 call kick_write(kick, out_file)
1426 write(out_file, '(a1,a20,a20,a20)') '#', str_center("Energy", 20), str_center("R", 20), str_center("Re[beta]", 20)
1427 write(out_file, '(a1,a20,a20,a20)') '#', str_center('[' // trim(units_abbrev(units_out%energy)) // ']', 20), &
1428 str_center('[' // trim(units_abbrev(units_out%length**3)) // ']', 20), &
1429 str_center('[' // trim(units_abbrev(units_out%length**4)) // ']', 20)
1430 write(out_file, '(a,5e15.6,5e15.6)') '# R(0) sum rule = ', sum1
1431 write(out_file, '(a,5e15.6,5e15.6)') '# R(2) sum rule = ', sum2
1432 do ie = 1, no_e
1433 write(out_file,'(e20.8,e20.8,e20.8)') units_from_atomic(units_out%energy, (ie-1)*spectrum%energy_step+spectrum%min_energy), &
1434 units_from_atomic(units_out%length**3, imsp(ie)/m_pi), &
1435 units_from_atomic(units_out%length**4, resp(ie)*p_c/(m_three*max((ie-1),1)*spectrum%energy_step))
1436 end do
1437
1438 safe_deallocate_a(resp)
1439 safe_deallocate_a(imsp)
1440
1442 end subroutine spectrum_rotatory_strength
1443 ! ---------------------------------------------------------
1444
1445
1446 ! ---------------------------------------------------------
1447 subroutine spectrum_hsfunction_init(dt, is, ie, niter, acc)
1448 real(real64), intent(in) :: dt
1449 integer, intent(in) :: is, ie, niter
1450 complex(real64), intent(in) :: acc(:)
1451
1452 integer :: nn(3), j, optimize_parity(3)
1453 logical :: optimize(3)
1454
1455 push_sub(spectrum_hsfunction_init)
1456
1457 is_ = is
1458 ie_ = ie
1459 time_step_ = dt
1460 niter_ = niter
1461 energy_step_ = (m_two * m_pi) / (niter * time_step_)
1462 safe_allocate(func_(0:niter))
1463 safe_allocate(funcw_(0:niter))
1464 func_ = m_z0
1465 func_ = acc
1466 nn(1:3) = (/ niter, 1, 1 /)
1467 optimize(1:3) = .false.
1468 optimize_parity(1:3) = -1
1469
1470 call fft_init(fft_handler, nn(1:3), 1, fft_complex, fftlib_fftw, optimize, optimize_parity)
1471 call zfft_forward(fft_handler, func_(0:niter-1), funcw_(0:niter-1))
1472 do j = 0, niter - 1
1473 funcw_(j) = -abs(funcw_(j))**2 * dt**2
1474 end do
1475
1477 end subroutine spectrum_hsfunction_init
1478 ! ---------------------------------------------------------
1479
1480
1481 ! ---------------------------------------------------------
1482 subroutine spectrum_hsfunction_end
1483
1484 push_sub(spectrum_hsfunction_end)
1485
1486 call fft_end(fft_handler)
1487
1488 safe_deallocate_a(func_)
1489 safe_deallocate_a(funcw_)
1491 end subroutine spectrum_hsfunction_end
1492 ! ---------------------------------------------------------
1493
1494
1495 ! ---------------------------------------------------------
1496 subroutine spectrum_hsfunction_min(namespace, aa, bb, omega_min, func_min)
1497 type(namespace_t), intent(in) :: namespace
1498 real(real64), intent(in) :: aa, bb
1499 real(real64), intent(out) :: omega_min, func_min
1500
1501 integer :: ierr, ie
1502 real(real64) :: xx, hsval, minhsval, ww, xa, xb, hxa, hxb
1503
1504 push_sub(spectrum_hsfunction_min)
1505
1506 ! xx should be an initial guess for the minimum. So we do a quick search
1507 ! that we refine later calling 1dminimize.
1508 !xx = omega
1509 !call hsfunction(xx, minhsval)
1510
1511 ierr = 0
1512
1513 ie = int(aa/energy_step_)
1514 ww = ie * energy_step_
1515 if (ww < aa) then
1516 ie = ie + 1
1517 ww = ie * energy_step_
1518 end if
1519 xx = ie * energy_step_
1520 minhsval = real(funcw_(ie), real64)
1521 do while(ww <= bb)
1522 hsval = real(funcw_(ie), real64)
1523 if (hsval < minhsval) then
1524 minhsval = hsval
1525 xx = ww
1526 end if
1527 ie = ie + 1
1528 ww = ie * energy_step_
1529 end do
1530
1531 ! Around xx, we call some GSL sophisticated search algorithm to find the minimum.
1532 ! First, we get the value of the function at the extremes of the interval
1533 xa = max(xx-energy_step_, aa)
1534 xb = min(xx+energy_step_, bb)
1535 call hsfunction(xa, hxa)
1536 call hsfunction(xb, hxb)
1537
1538 if (hxa <= minhsval) then
1539 xx = xa
1540 minhsval = hxa
1541 elseif (hxb <= minhsval) then
1542 xx = xb
1543 minhsval = hxb
1544 else
1545 call loct_1dminimize(xa, xb, xx, hsfunction, ierr)
1546 end if
1547
1548 if (ierr /= 0) then
1549 write(message(1),'(a,f14.6,a)') 'spectrum_hsfunction_min: The maximum at', xx,' was not properly converged.'
1550 write(message(2),'(a,i12)') 'Error code: ', ierr
1551 call messages_warning(2, namespace=namespace)
1552 end if
1553 call hsfunction(xx, hsval)
1554 omega_min = xx
1555 func_min = hsval
1556
1558 end subroutine spectrum_hsfunction_min
1559 ! ---------------------------------------------------------
1560
1561
1562 ! ---------------------------------------------------------
1563 subroutine hsfunction(omega, power)
1564 real(real64), intent(in) :: omega
1565 real(real64), intent(out) :: power
1566
1567 complex(real64) :: cc, ez1, ez, zz
1568 integer :: jj,dir
1569
1570 push_sub(hsfunction)
1571
1572 zz = m_zi * omega * time_step_
1573 ez = exp(zz)
1574
1575 if (allocated(func_ar_)) then
1576 power = m_zero
1577 do dir = 1, 3
1578 ez1 = exp((is_ - 1) * zz)
1579 cc = m_z0
1580 do jj = is_, ie_
1581 ! This would be easier, but slower.
1582 !cc = cc + exp(M_zI * omega * jj * time_step_) * func_(jj)
1583 ez1 = ez1 * ez
1584 cc = cc + ez1 * func_ar_(dir,jj) &
1585 *exp(-m_zi * omega * tret_(jj)) !integrate over the retarded time
1586 end do
1587 power = power - abs(cc)**2 * time_step_**2
1588! pp(dir) = cc * time_step_
1589 end do
1590! power = - sum(abs(zcross_product(vv_, pp))**2)
1591 else
1592 cc = m_z0
1593 ez1 = exp((is_ - 1) * zz)
1594 do jj = is_, ie_
1595 ez1 = ez1 * ez
1596 cc = cc + ez1 * func_(jj)
1597 end do
1598 power = -abs(cc)**2 * time_step_**2
1599 end if
1600
1601
1602 pop_sub(hsfunction)
1603 end subroutine hsfunction
1604 ! ---------------------------------------------------------
1605
1606 ! ---------------------------------------------------------
1607 subroutine spectrum_hsfunction_ar_init(dt, is, ie, niter, acc, pos,tret)
1608 real(real64), intent(in) :: dt
1609 integer, intent(in) :: is, ie, niter
1610 complex(real64), intent(in) :: acc(:,:),pos(:,:),tret(:)
1611
1613
1614 is_ = is
1615 ie_ = ie
1616 time_step_ = dt
1617 safe_allocate(func_ar_(1:3, 0:niter))
1618 safe_allocate(pos_(1:3, 0:niter))
1619 safe_allocate(tret_(0:niter))
1620 func_ar_ = acc
1621 pos_= pos
1622 tret_=tret
1623
1624
1626 end subroutine spectrum_hsfunction_ar_init
1627 ! ---------------------------------------------------------
1628
1629
1630 ! ---------------------------------------------------------
1631 ! FIXME: why is this never called?
1633
1635
1636 safe_deallocate_a(func_ar_)
1637 safe_deallocate_a(pos_)
1638 safe_deallocate_a(tret_)
1639
1641 end subroutine spectrum_hsfunction_ar_end
1642 ! ---------------------------------------------------------
1643
1644 ! ---------------------------------------------------------
1645 subroutine spectrum_hs_ar_from_acc(spectrum, namespace, out_file, vec, w0)
1646 type(spectrum_t), intent(inout) :: spectrum
1647 type(namespace_t), intent(in) :: namespace
1648 character(len=*), intent(in) :: out_file
1649 real(real64), intent(in) :: vec(:)
1650 real(real64), optional, intent(in) :: w0
1651
1652 integer :: istep, trash, iunit, nspin, time_steps, istart, iend, ntiter, lmax, ierr, jj, idir, ispin
1653 real(real64) :: dt, dump, aa(3)
1654 complex(real64) :: nn(3)
1655 type(kick_t) :: kick
1656 real(real64), allocatable :: dd(:,:)
1657 complex(real64), allocatable :: acc(:,:),pp(:,:),pos(:,:),tret(:)
1658 real(real64) :: vv(3)
1659 type(unit_system_t) :: file_units
1660
1661 push_sub(spectrum_hs_ar_from_acc)
1662
1663 call spectrum_tdfile_info(namespace, 'acceleration', iunit, time_steps, dt)
1664 call spectrum_fix_time_limits(spectrum, time_steps, dt, istart, iend, ntiter)
1665
1666 ! load dipole from file
1667 safe_allocate(acc(1:3, 0:time_steps))
1668 safe_allocate(pp(1:3, 0:time_steps))
1669 safe_allocate(pos(1:3, 0:time_steps))
1670 safe_allocate(tret(0:time_steps))
1671
1672 acc = m_zero
1673 pos = m_zero
1674 pp = m_zero
1675 nn = m_zero
1676 tret = m_zero
1677
1678 call io_skip_header(iunit)
1679 do istep = 0, time_steps-1
1680 aa = m_zero
1681 read(iunit, '(28x,e20.12)', advance = 'no', iostat = ierr) aa(1)
1682 jj = 2
1683 do while((ierr == 0) .and. (jj <= 3))
1684 read(iunit, '(e20.12)', advance = 'no', iostat = ierr) aa(jj)
1685 jj = jj+1
1686 end do
1687
1688! read(iunit, *) trash, dump, aa
1689
1690 acc(:,istep) = units_to_atomic(units_out%acceleration, aa(:))
1691! write (*,*) istep, Real(acc(:,istep))
1692 end do
1693 close(iunit)
1694
1695
1696 ! Try to get the trajectory from multipole file
1697
1698 iunit = io_open('multipoles', namespace, action='read', status='old', die=.false.)
1699 if (iunit == -1) then
1700 iunit = io_open('td.general/multipoles', namespace, action='read', status='old')
1701 end if
1702 call spectrum_mult_info(namespace, iunit, nspin, kick, time_steps, dt, file_units, lmax=lmax)
1703 call spectrum_fix_time_limits(spectrum, time_steps, dt, istart, iend, ntiter)
1704
1705 call io_skip_header(iunit)
1706! write (*,*)
1707
1708 safe_allocate(dd(1:3, 1:nspin))
1709 do istep = 0, time_steps-1
1710 dd = m_zero
1711 read(iunit, *) trash, dump, (dump, (dd(idir, ispin), idir = 1, kick%dim), ispin = 1, nspin)
1712 pos(1:3, istep) = -sum(dd(1:3, :),2)
1713 pos(:,istep) = units_to_atomic(units_out%length, pos(:,istep))
1714 end do
1715 safe_deallocate_a(dd)
1716 pos(:,0) = pos(:,1)
1717 call io_close(iunit)
1718
1719! write (*,*)
1720
1721 ! normalize vector and set to global var
1722! vv = vec / sqrt(sum(vec(:)**2))
1723 vv(1:3) = vec(1:3)
1724
1725 pp(:,0) = m_zero
1726 do istep = 0, time_steps - 1
1727 nn(:) = vv(:)-pos(:,istep)
1728 nn(:) = nn(:)/norm2(abs(nn(:)))
1729 tret(istep) = dot_product(vv(:), real(pos(:,istep), real64))/p_c
1730 pp(:,istep) = zcross_product(nn, zcross_product(nn, acc(:,istep)))
1731! write (*,*) istep, Real(PP(:,istep)),"acc", Real(acc(:,istep))
1732 end do
1733
1734 call spectrum_hsfunction_ar_init(dt, istart, iend, time_steps, pp, pos, tret)
1735 call spectrum_hs(spectrum, namespace, out_file, 'a', w0)
1737
1738 safe_deallocate_a(acc)
1739 safe_deallocate_a(pp)
1740 safe_deallocate_a(pos)
1741 safe_deallocate_a(tret)
1742
1744 end subroutine spectrum_hs_ar_from_acc
1745 ! ---------------------------------------------------------
1746
1747
1748 ! ---------------------------------------------------------
1749 subroutine spectrum_hs_ar_from_mult(spectrum, namespace, out_file, vec, w0)
1750 type(spectrum_t), intent(inout) :: spectrum
1751 type(namespace_t), intent(in) :: namespace
1752 character(len=*), intent(in) :: out_file
1753 real(real64), intent(in) :: vec(:)
1754 real(real64), optional, intent(in) :: w0
1755
1756 integer :: istep, trash, iunit, nspin, time_steps, istart, iend, ntiter, lmax, idir, ispin
1757 real(real64) :: dt, dump
1758 type(kick_t) :: kick
1759 real(real64), allocatable :: dd(:,:)
1760 complex(real64), allocatable :: dipole(:,:), ddipole(:,:), pp(:,:), tret(:)
1761 complex(real64) :: vv(3)
1762 type(unit_system_t) :: file_units
1763
1764 push_sub(spectrum_hs_ar_from_mult)
1765
1766
1767 iunit = io_open('multipoles', namespace, action='read', status='old', die=.false.)
1768 if (iunit == -1) then
1769 iunit = io_open('td.general/multipoles', namespace, action='read', status='old')
1770 end if
1771 call spectrum_mult_info(namespace, iunit, nspin, kick, time_steps, dt, file_units, lmax=lmax)
1772 call spectrum_fix_time_limits(spectrum, time_steps, dt, istart, iend, ntiter)
1773
1774 call io_skip_header(iunit)
1775
1776 ! load dipole from file
1777 safe_allocate(dipole(1:3, 0:time_steps))
1778 safe_allocate(ddipole(1:3, 0:time_steps))
1779 safe_allocate(pp(1:3, 0:time_steps))
1780 safe_allocate(tret(0:time_steps))
1781 safe_allocate(dd(1:3, 1:nspin))
1782
1783 dipole = m_z0
1784 ddipole = m_z0
1785 pp = m_z0
1786 tret= m_zero
1787
1788 do istep = 1, time_steps
1789 dd = m_zero
1790 read(iunit, *) trash, dump, (dump, (dd(idir, ispin), idir = 1, kick%dim), ispin = 1, nspin)
1791 dipole(1:3, istep) = -sum(dd(1:3, :),2)
1792 dipole(:,istep) = units_to_atomic(units_out%length, dipole(:,istep))
1793 end do
1794 safe_deallocate_a(dd)
1795 dipole(:,0) = dipole(:,1)
1796 call io_close(iunit)
1797
1798 ! we now calculate the acceleration.
1799 ddipole(:,0) = m_zero
1800 do istep = 1, time_steps - 1
1801 ddipole(:,istep) = (dipole(:,istep - 1) + dipole(:,istep + 1) - m_two * dipole(:,istep)) / dt**2
1802 end do
1803 call interpolate(dt*(/ -3, -2, -1 /), &
1804 ddipole(1,time_steps - 3:time_steps - 1), &
1805 m_zero, &
1806 ddipole(1,time_steps))
1807 call interpolate(dt*(/ -3, -2, -1 /), &
1808 ddipole(2,time_steps - 3:time_steps - 1), &
1809 m_zero, &
1810 ddipole(2,time_steps))
1811 call interpolate(dt*(/ -3, -2, -1 /), &
1812 ddipole(3,time_steps - 3:time_steps - 1), &
1813 m_zero, &
1814 ddipole(3,time_steps))
1815
1816 ! normalize vector and set to global var
1817 vv(1:3) = vec(1:3) / norm2(vec(1:3))
1818
1819 pp(:,0) = m_zero
1820 do istep = 1, time_steps - 1
1821! write (*,*) istep, istep*dt, Real(ddipole(1,istep)), Real(ddipole(2,istep))
1822 tret(istep) = dot_product(vv(:), dipole(:,istep))/p_c
1823 pp(:,istep) = zcross_product(vv, zcross_product(vv, ddipole(:,istep - 1)))
1824! PP(istep) = sum(abs(dipole(:,istep))**2)
1825! write(*,*) istep, PP(istep)
1826
1827 end do
1828
1829
1830 call spectrum_hsfunction_ar_init(dt, istart, iend, time_steps, pp, dipole,tret)
1831 call spectrum_hs(spectrum, namespace, out_file, 'a', w0)
1833
1834 safe_deallocate_a(dipole)
1835 safe_deallocate_a(ddipole)
1836 safe_deallocate_a(pp)
1837 safe_deallocate_a(tret)
1838
1839
1841 end subroutine spectrum_hs_ar_from_mult
1842 ! ---------------------------------------------------------
1843
1845 ! ---------------------------------------------------------
1846 subroutine spectrum_hs_from_mult(spectrum, namespace, out_file, pol, vec, w0)
1847 type(spectrum_t), intent(inout) :: spectrum
1848 type(namespace_t), intent(in) :: namespace
1849 character(len=*), intent(in) :: out_file
1850 character, intent(in) :: pol
1851 real(real64), intent(in) :: vec(:)
1852 real(real64), optional, intent(in) :: w0
1853
1854 integer :: istep, trash, iunit, nspin, time_steps, istart, iend, ntiter, lmax, no_e, ie, idir, ispin
1855 real(real64) :: dt, dump, vv(3)
1856 type(kick_t) :: kick
1857 real(real64), allocatable :: dd(:,:)
1858 real(real64), allocatable :: sps(:), spc(:), racc(:)
1859 complex(real64), allocatable :: dipole(:), ddipole(:)
1860 type(batch_t) :: acc_batch, sps_batch, spc_batch
1861 type(unit_system_t) :: file_units
1862
1863 push_sub(spectrum_hs_from_mult)
1864
1865 iunit = io_open('multipoles', namespace, action='read', status='old', die=.false.)
1866 if (iunit == -1) then
1867 iunit = io_open('td.general/multipoles', namespace, action='read', status='old')
1868 end if
1869 call spectrum_mult_info(namespace, iunit, nspin, kick, time_steps, dt, file_units, lmax=lmax)
1870 call spectrum_fix_time_limits(spectrum, time_steps, dt, istart, iend, ntiter)
1871
1872 if (spectrum%energy_step <= m_zero) spectrum%energy_step = m_two * m_pi / (dt*time_steps)
1873
1874 call io_skip_header(iunit)
1875
1876 ! load dipole from file
1877 safe_allocate(dipole(0:time_steps))
1878 safe_allocate(ddipole(0:time_steps))
1879 safe_allocate(dd(1:3, 1:nspin))
1880
1881 vv(1:3) = vec(1:3) / norm2(vec(1:3))
1882
1883 do istep = 1, time_steps
1884 dd = m_zero
1885 read(iunit, *) trash, dump, (dump, (dd(idir, ispin), idir = 1, kick%dim), ispin = 1, nspin)
1886 select case (pol)
1887 case ('x')
1888 dipole(istep) = -sum(dd(1, :))
1889 case ('y')
1890 dipole(istep) = -sum(dd(2, :))
1891 case ('z')
1892 dipole(istep) = sum(dd(3, :))
1893 case ('+')
1894 dipole(istep) = -sum(cmplx(dd(1, :), dd(2, :), real64)) / sqrt(m_two)
1895 case ('-')
1896 dipole(istep) = -sum(cmplx(dd(1, :), -dd(2, :), real64)) / sqrt(m_two)
1897 case ('v')
1898 dipole(istep) = -sum(vv(1)*dd(1, :) + vv(2)*dd(2, :) + vv(3)*dd(3, :))
1899 end select
1900 dipole(istep) = units_to_atomic(units_out%length, dipole(istep))
1901 end do
1902 safe_deallocate_a(dd)
1903 dipole(0) = dipole(1)
1904 call io_close(iunit)
1905
1906 ! we now calculate the acceleration.
1907 ddipole(0) = m_zero
1908 do istep = 1, time_steps - 1
1909 ddipole(istep) = (dipole(istep - 1) + dipole(istep + 1) - m_two * dipole(istep)) / dt**2
1910 end do
1911 call interpolate( dt*(/ -3, -2, -1 /), &
1912 ddipole(time_steps - 3:time_steps - 1), &
1913 m_zero, &
1914 ddipole(time_steps))
1915
1916 if (present(w0)) then
1917
1918 call spectrum_hsfunction_init(dt, istart, iend, time_steps, ddipole)
1919 call spectrum_hs(spectrum, namespace, out_file, pol, w0)
1921
1922 else
1923
1924 safe_allocate(racc(0:time_steps))
1925 racc = real(ddipole, real64)
1926
1927 no_e = spectrum_nenergy_steps(spectrum)
1928 safe_allocate(sps(1:no_e))
1929 safe_allocate(spc(1:no_e))
1930 sps = m_zero
1931 spc = m_zero
1932
1933 call batch_init(acc_batch, racc)
1934 call batch_init(sps_batch, sps)
1935 call batch_init(spc_batch, spc)
1936
1937 call spectrum_fourier_transform(spectrum%method, spectrum_transform_cos, spectrum%noise, &
1938 istart + 1, iend + 1, m_zero, dt, acc_batch, spectrum%min_energy, spectrum%max_energy, spectrum%energy_step, spc_batch)
1939 call spectrum_fourier_transform(spectrum%method, spectrum_transform_sin, spectrum%noise, &
1940 istart + 1, iend + 1, m_zero, dt, acc_batch, spectrum%min_energy, spectrum%max_energy, spectrum%energy_step, sps_batch)
1942 do ie = 1, no_e
1943 sps(ie) = (sps(ie)**2 + spc(ie)**2)
1944 end do
1945
1946 call spectrum_hs_output(spectrum, namespace, out_file, pol, no_e, sps)
1947
1948 call acc_batch%end()
1949 call sps_batch%end()
1950 call spc_batch%end()
1951
1952 safe_deallocate_a(racc)
1953
1954 end if
1955
1956 safe_deallocate_a(dipole)
1957 safe_deallocate_a(ddipole)
1958
1959 pop_sub(spectrum_hs_from_mult)
1960 end subroutine spectrum_hs_from_mult
1961 ! ---------------------------------------------------------
1962
1963
1964 ! ---------------------------------------------------------
1965 subroutine spectrum_hs_from_acc(spectrum, namespace, out_file, pol, vec, w0)
1966 type(spectrum_t), intent(inout) :: spectrum
1967 type(namespace_t), intent(in) :: namespace
1968 character(len=*), intent(in) :: out_file
1969 character, intent(in) :: pol
1970 real(real64), intent(in) :: vec(:)
1971 real(real64), optional, intent(in) :: w0
1972
1973 integer :: istep, jj, iunit, time_steps, istart, iend, ntiter, ierr, no_e, ie
1974 real(real64) :: dt, aa(3), vv(3)
1975 complex(real64), allocatable :: acc(:)
1976 real(real64), allocatable :: racc(:), sps(:), spc(:)
1977 type(batch_t) :: acc_batch, sps_batch, spc_batch
1978
1979 push_sub(spectrum_hs_from_acc)
1980
1981 call spectrum_tdfile_info(namespace, 'acceleration', iunit, time_steps, dt)
1982 call spectrum_fix_time_limits(spectrum, time_steps, dt, istart, iend, ntiter)
1983
1984 if (spectrum%energy_step <= m_zero) spectrum%energy_step = m_two * m_pi / (dt*time_steps)
1985
1986 ! load dipole from file
1987 safe_allocate(acc(0:time_steps))
1988 acc = m_zero
1989 vv = vec / norm2(vec(:))
1990 call io_skip_header(iunit)
1991
1992 do istep = 1, time_steps
1993 aa = m_zero
1994 read(iunit, '(28x,e20.12)', advance = 'no', iostat = ierr) aa(1)
1995 jj = 2
1996 do while((ierr == 0) .and. (jj <= 3))
1997 read(iunit, '(e20.12)', advance = 'no', iostat = ierr) aa(jj)
1998 jj = jj + 1
1999 end do
2000 select case (pol)
2001 case ('x')
2002 acc(istep) = aa(1)
2003 case ('y')
2004 acc(istep) = aa(2)
2005 case ('z')
2006 acc(istep) = aa(3)
2007 case ('+')
2008 acc(istep) = cmplx(aa(1), aa(2), real64) / sqrt(m_two)
2009 case ('-')
2010 acc(istep) = cmplx(aa(1), -aa(2), real64) / sqrt(m_two)
2011 case ('v')
2012 acc(istep) = vv(1)*aa(1) + vv(2)*aa(2) + vv(3)*aa(3)
2013 end select
2014 acc(istep) = units_to_atomic(units_out%acceleration, acc(istep))
2015 end do
2016 close(iunit)
2017
2018 if (present(w0)) then
2019
2020 call spectrum_hsfunction_init(dt, istart, iend, time_steps, acc)
2021 call spectrum_hs(spectrum, namespace, out_file, pol, w0)
2023
2024 else
2025
2026 safe_allocate(racc(0:time_steps))
2027 racc = real(acc, real64)
2028
2029 no_e = spectrum_nenergy_steps(spectrum)
2030 safe_allocate(sps(1:no_e))
2031 safe_allocate(spc(1:no_e))
2032 sps = m_zero
2033 spc = m_zero
2034
2035 call batch_init(acc_batch, racc)
2036 call batch_init(sps_batch, sps)
2037 call batch_init(spc_batch, spc)
2038
2039 call spectrum_fourier_transform(spectrum%method, spectrum_transform_cos, spectrum%noise, &
2040 istart + 1, iend + 1, m_zero, dt, acc_batch, spectrum%min_energy, &
2041 spectrum%max_energy, spectrum%energy_step, spc_batch)
2042 call spectrum_fourier_transform(spectrum%method, spectrum_transform_sin, spectrum%noise, &
2043 istart + 1, iend + 1, m_zero, dt, acc_batch, spectrum%min_energy, &
2044 spectrum%max_energy, spectrum%energy_step, sps_batch)
2045
2046 do ie = 1, no_e
2047 sps(ie) = (sps(ie)**2 + spc(ie)**2)
2048 end do
2049
2050 call spectrum_hs_output(spectrum, namespace, out_file, pol, no_e, sps)
2051
2052 call acc_batch%end()
2053 call sps_batch%end()
2054 call spc_batch%end()
2055
2056 safe_deallocate_a(racc)
2057
2058 end if
2059
2060 safe_deallocate_a(acc)
2061 pop_sub(spectrum_hs_from_acc)
2062 end subroutine spectrum_hs_from_acc
2063 ! ---------------------------------------------------------
2064
2065 ! ---------------------------------------------------------
2066 subroutine spectrum_hs_from_current(spectrum, namespace, out_file, pol, vec, w0)
2067 type(spectrum_t), intent(inout) :: spectrum
2068 type(namespace_t), intent(in) :: namespace
2069 character(len=*), intent(in) :: out_file
2070 character, intent(in) :: pol
2071 real(real64), intent(in) :: vec(:)
2072 real(real64), optional, intent(in) :: w0
2073
2074 integer :: istep, jj, iunit, time_steps, istart, iend, ntiter, ierr, no_e, ie
2075 real(real64) :: dt, cc(3), vv(3)
2076 complex(real64), allocatable :: cur(:)
2077 real(real64), allocatable :: rcur(:), sps(:), spc(:)
2078 type(batch_t) :: cur_batch, sps_batch, spc_batch
2079
2080 push_sub(spectrum_hs_from_current)
2081
2082 call spectrum_tdfile_info(namespace, 'total_current', iunit, time_steps, dt)
2083 call spectrum_fix_time_limits(spectrum, time_steps, dt, istart, iend, ntiter)
2084
2085 if (spectrum%energy_step <= m_zero) spectrum%energy_step = m_two * m_pi / (dt * time_steps)
2086
2087 ! load dipole from file
2088 safe_allocate(cur(0:time_steps))
2089 cur = m_zero
2090 vv = vec / norm2(vec(:))
2091 call io_skip_header(iunit)
2092
2093 do istep = 1, time_steps
2094 cc = m_zero
2095 read(iunit, '(28x,e20.12)', advance = 'no', iostat = ierr) cc(1)
2096 jj = 2
2097 do while((ierr == 0) .and. (jj <= 3))
2098 read(iunit, '(e20.12)', advance = 'no', iostat = ierr) cc(jj)
2099 jj = jj + 1
2100 end do
2101 select case (pol)
2102 case ('x')
2103 cur(istep) = cc(1)
2104 case ('y')
2105 cur(istep) = cc(2)
2106 case ('z')
2107 cur(istep) = cc(3)
2108 case ('+')
2109 cur(istep) = cmplx(cc(1), cc(2), real64) / sqrt(m_two)
2110 case ('-')
2111 cur(istep) = cmplx(cc(1), -cc(2), real64) / sqrt(m_two)
2112 case ('v')
2113 cur(istep) = vv(1)*cc(1) + vv(2)*cc(2) + vv(3)*cc(3)
2114 end select
2115 cur(istep) = units_to_atomic(units_out%velocity, cur(istep))
2116 end do
2117 close(iunit)
2118
2119 if (present(w0)) then
2120
2121 call spectrum_hsfunction_init(dt, istart, iend, time_steps, cur)
2122 call spectrum_hs(spectrum, namespace, out_file, pol, w0)
2124
2125 else
2126
2127 safe_allocate(rcur(0:time_steps))
2128 rcur = real(cur, real64)
2129
2130 no_e = spectrum_nenergy_steps(spectrum)
2131 safe_allocate(sps(1:no_e))
2132 safe_allocate(spc(1:no_e))
2133 sps = m_zero
2134 spc = m_zero
2135
2136 call batch_init(cur_batch, rcur)
2137 call batch_init(sps_batch, sps)
2138 call batch_init(spc_batch, spc)
2139
2140 call spectrum_fourier_transform(spectrum%method, spectrum_transform_cos, spectrum%noise, &
2141 istart + 1, iend + 1, m_zero, dt, cur_batch, spectrum%min_energy, spectrum%max_energy, spectrum%energy_step, spc_batch)
2142 call spectrum_fourier_transform(spectrum%method, spectrum_transform_sin, spectrum%noise, &
2143 istart + 1, iend + 1, m_zero, dt, cur_batch, spectrum%min_energy, spectrum%max_energy, spectrum%energy_step, sps_batch)
2144
2145 do ie = 1, no_e
2146 sps(ie) = (sps(ie)**2 + spc(ie)**2) * ((ie-1) * spectrum%energy_step + spectrum%min_energy)**2
2147 end do
2148
2149 call spectrum_hs_output(spectrum, namespace, out_file, pol, no_e, sps)
2150
2151 call cur_batch%end()
2152 call sps_batch%end()
2153 call spc_batch%end()
2154
2155 safe_deallocate_a(rcur)
2156
2157 end if
2158
2159 safe_deallocate_a(cur)
2162
2163
2164 ! ---------------------------------------------------------
2165 subroutine spectrum_hs(spectrum, namespace, out_file, pol, w0)
2166 type(spectrum_t), intent(inout) :: spectrum
2167 type(namespace_t), intent(in) :: namespace
2168 character(len=*), intent(in) :: out_file
2169 character, intent(in) :: pol
2170 real(real64), optional, intent(in) :: w0
2171
2172 integer :: iunit, no_e, ie
2173 real(real64) :: omega, hsval, xx
2174 real(real64), allocatable :: sp(:)
2175
2176 push_sub(spectrum_hs)
2177
2178 if (present(w0)) then
2179
2180 iunit = io_open(trim(out_file) // "." // trim(pol), namespace, action='write')
2181 write(iunit, '(a1,a20,a20)') '#', str_center("w", 20), str_center("H(w)", 20)
2182 write(iunit, '(a1,a20,a20)') '#', &
2183 str_center('['//trim(units_abbrev(units_out%energy)) // ']', 20), &
2184 str_center('[('//trim(units_abbrev(units_out%length))//'/' &
2185 //trim(units_abbrev(units_out%time**2)), 20)
2186
2187 ! output
2188 omega = w0
2189 do while(omega <= spectrum%max_energy)
2190 call spectrum_hsfunction_min(namespace, omega - w0, omega + w0, xx, hsval)
2191
2192 write(iunit, '(1x,2e20.8)') units_from_atomic(units_out%energy, xx), &
2193 units_from_atomic((units_out%length / units_out%time)**2, -hsval)
2194
2195 ! 2 * w0 because we assume that there are only odd peaks.
2196 omega = omega + 2 * w0
2197 end do
2198 call io_close(iunit)
2199
2200 else
2201 no_e = spectrum_nenergy_steps(spectrum)
2202 safe_allocate(sp(1:no_e))
2203 sp = m_zero
2204
2205 do ie = 1, no_e
2206 call hsfunction((ie-1) * spectrum%energy_step + spectrum%min_energy, sp(ie))
2207 sp(ie) = -sp(ie)
2208 end do
2209
2210 call spectrum_hs_output(spectrum, namespace, out_file, pol, no_e, sp)
2211
2212 safe_deallocate_a(sp)
2213
2214 end if
2215
2216 pop_sub(spectrum_hs)
2217 end subroutine spectrum_hs
2218 ! ---------------------------------------------------------
2219
2220
2221 subroutine spectrum_hs_output(spectrum, namespace, out_file, pol, no_e, sp)
2222 type(spectrum_t), intent(inout) :: spectrum
2223 type(namespace_t), intent(in) :: namespace
2224 character(len=*), intent(in) :: out_file
2225 character, intent(in) :: pol
2226 integer, intent(in) :: no_e
2227 real(real64), intent(in) :: sp(:)
2228
2229 integer :: iunit, ie
2230
2231 push_sub(spectrum_hs_output)
2232
2233 ! output
2234 if (trim(out_file) /= '-') then
2235 iunit = io_open(trim(out_file) // "." // trim(pol), namespace, action='write')
2236 write(iunit, '(a1,a20,a20)') '#', str_center("w", 20), str_center("H(w)", 20)
2237
2238 write(iunit, '(a1,a20,a20)') &
2239 '#', str_center('['//trim(units_abbrev(units_out%energy)) // ']', 20), &
2240 str_center('[('//trim(units_abbrev(units_out%length))//'/' &
2241 //trim(units_abbrev(units_out%time**2)), 20)
2242
2243 do ie = 1, no_e
2244 write(iunit, '(2e15.6)') units_from_atomic(units_out%energy, (ie-1) * spectrum%energy_step + spectrum%min_energy), &
2245 units_from_atomic((units_out%length / units_out%time)**2, sp(ie))
2246 end do
2247
2248 call io_close(iunit)
2249 end if
2250
2251 pop_sub(spectrum_hs_output)
2252 end subroutine spectrum_hs_output
2253
2254
2255 ! ---------------------------------------------------------
2256 subroutine spectrum_mult_info(namespace, iunit, nspin, kick, time_steps, dt, file_units, lmax)
2257 type(namespace_t), intent(in) :: namespace
2258 integer, intent(in) :: iunit
2259 integer, intent(out) :: nspin
2260 type(kick_t), intent(out) :: kick
2261 integer, intent(out) :: time_steps
2262 real(real64), intent(out) :: dt
2263 type(unit_system_t), intent(out) :: file_units
2264 integer, optional, intent(out) :: lmax
2265
2266 integer :: ii
2267 character(len=100) :: line
2268
2269 push_sub(spectrum_mult_info)
2270
2271 assert(iunit /= -1)
2272
2273 rewind(iunit)
2274 call spectrum_read_header(namespace, iunit, line, "header separator")
2275 call spectrum_read_header(namespace, iunit, line, "header title")
2276 call spectrum_read_header(namespace, iunit, nspin, "number of spin components")
2277 if (present(lmax)) then
2278 call spectrum_read_header(namespace, iunit, lmax, "maximum multipole order")
2279 end if
2280 call kick_read(kick, iunit, namespace)
2281 call spectrum_read_header(namespace, iunit, line, "multipoles column labels")
2282 call spectrum_read_header(namespace, iunit, line, "multipoles units")
2283 call io_skip_header(iunit)
2284
2285 ! Figure out the units of the file
2286 ii = index(line,'eV')
2287 if (ii /= 0) then
2288 call unit_system_get(file_units, units_eva)
2289 else
2290 call unit_system_get(file_units, units_atomic)
2291 end if
2292
2293 call spectrum_count_time_steps(namespace, iunit, time_steps, dt)
2294 dt = units_to_atomic(file_units%time, dt) ! units_out is OK
2295
2296 pop_sub(spectrum_mult_info)
2297 end subroutine spectrum_mult_info
2298 ! ---------------------------------------------------------
2299
2300
2301 ! ---------------------------------------------------------
2302 subroutine spectrum_count_time_steps(namespace, iunit, time_steps, dt)
2303 type(namespace_t), intent(in) :: namespace
2304 integer, intent(in) :: iunit
2305 integer, intent(out) :: time_steps
2306 real(real64), intent(out) :: dt
2307
2308 real(real64) :: t1, t2, dummy
2309 integer :: trash, ierr
2310 character(len=200) :: line
2311
2312 push_sub(count_time_steps)
2313
2314 ! count number of time_steps
2315 time_steps = 0
2317 t2 = m_zero
2318 do
2319 read(iunit, '(a)', iostat = ierr) line
2320 if (ierr == iostat_end) exit
2321 if (ierr /= 0) then
2322 message(1) = "Could not read multipoles time-step data."
2323 call messages_fatal(1, namespace=namespace)
2324 end if
2325 if (len_trim(line) == 0 .or. line(1:1) == "#") cycle
2326 read(line, *, iostat = ierr) trash, dummy
2327 if (ierr /= 0) then
2328 message(1) = "Could not parse multipoles time-step line:"
2329 write(message(2), '(a)') trim(line)
2330 call messages_fatal(2, namespace=namespace)
2331 end if
2332 time_steps = time_steps + 1
2333 if (time_steps == 1) t1 = dummy
2334 if (time_steps == 2) t2 = dummy
2335 end do
2336 dt = (t2 - t1)
2337 time_steps = time_steps - 1
2338
2339 if (time_steps < 3) then
2340 message(1) = "Empty file?"
2341 call messages_fatal(1, namespace=namespace)
2342 end if
2343
2344 pop_sub(count_time_steps)
2345 end subroutine spectrum_count_time_steps
2346 ! ---------------------------------------------------------
2347
2348
2349 ! ---------------------------------------------------------
2350 subroutine spectrum_cross_section_info(namespace, iunit, nspin, kick, energy_steps, dw)
2351 type(namespace_t), intent(in) :: namespace
2352 integer, intent(in) :: iunit
2353 integer, intent(out) :: nspin
2354 type(kick_t), intent(out) :: kick
2355 integer, intent(out) :: energy_steps
2356 real(real64), intent(out) :: dw
2357
2358 real(real64) :: dummy, e1, e2
2359
2361
2362 ! read in number of spin components
2363 call spectrum_find_cross_section_header(namespace, iunit)
2364 call spectrum_read_header(namespace, iunit, nspin, "number of spin components")
2365 call kick_read(kick, iunit, namespace)
2366 call io_skip_header(iunit)
2367
2368 ! count number of time_steps
2369 energy_steps = 0
2370 do
2371 read(iunit, *, end=100) dummy
2372 energy_steps = energy_steps + 1
2373 if (energy_steps == 1) e1 = dummy
2374 if (energy_steps == 2) e2 = dummy
2375 end do
2376100 continue
2377 dw = units_to_atomic(units_out%energy, e2 - e1)
2378
2379 if (energy_steps < 3) then
2380 message(1) = "Empty multipole file?"
2381 call messages_fatal(1, namespace=namespace)
2382 end if
2383
2385 end subroutine spectrum_cross_section_info
2386
2387
2388 ! ---------------------------------------------------------
2389 subroutine spectrum_tdfile_info(namespace, fname, iunit, time_steps, dt)
2390 type(namespace_t), intent(in) :: namespace
2391 character(len=*), intent(in) :: fname
2392 integer, intent(out) :: iunit, time_steps
2393 real(real64), intent(out) :: dt
2394
2395 integer :: trash
2396 real(real64) :: t1, t2, dummy
2397 character(len=256) :: filename
2398
2399 push_sub(spectrum_tdfile_info)
2400
2401 ! open files
2402 filename = trim('td.general/')//trim(fname)
2403 iunit = io_open(filename, namespace, action='read', status='old', die=.false.)
2404
2405 if (iunit == -1) then
2406 filename = trim('./')//trim(fname)
2407 iunit = io_open(filename, namespace, action='read', status='old')
2408 end if
2409
2410
2411 ! read in dipole
2412 call io_skip_header(iunit)
2413
2414 ! count number of time_steps
2415 time_steps = 0
2416 do
2417 read(iunit, *, end=100) trash, dummy
2418 time_steps = time_steps + 1
2419 if (time_steps == 1) t1 = dummy
2420 if (time_steps == 2) t2 = dummy
2421 end do
2422100 continue
2423 dt = units_to_atomic(units_out%time, t2 - t1) ! units_out is OK
2424 time_steps = time_steps - 1
2425
2426 if (time_steps < 3) then
2427 message(1) = "Empty file?"
2428 call messages_fatal(1, namespace=namespace)
2429 end if
2430
2431 rewind(iunit)
2432 pop_sub(spectrum_tdfile_info)
2433 end subroutine spectrum_tdfile_info
2434
2435
2436 ! ---------------------------------------------------------
2437 subroutine spectrum_fix_time_limits(spectrum, time_steps, dt, istart, iend, ntiter)
2438 type(spectrum_t), intent(inout) :: spectrum
2439 integer, intent(in) :: time_steps
2440 real(real64), intent(in) :: dt
2441 integer, intent(out) :: istart, iend, ntiter
2442
2443 real(real64) :: ts, te, dummy
2444
2446
2447 ts = m_zero
2448 te = time_steps * dt
2449
2450 if (spectrum%start_time < ts) spectrum%start_time = ts
2451 if (spectrum%start_time > te) spectrum%start_time = te
2452 if (spectrum%end_time > te .or. spectrum%end_time <= m_zero) spectrum%end_time = te
2453 if (spectrum%end_time < ts) spectrum%end_time = ts
2454
2455 if (spectrum%end_time < spectrum%start_time) then
2456 dummy = spectrum%end_time ! swap
2457 spectrum%end_time = spectrum%start_time
2458 spectrum%start_time = dummy
2459 end if
2460 istart = nint(spectrum%start_time / dt)
2461 iend = nint(spectrum%end_time / dt)
2462 ntiter = iend - istart + 1
2463
2464 ! Get default damp factor
2465 if (spectrum%damp /= spectrum_damp_none .and. spectrum%damp /= spectrum_damp_polynomial &
2466 .and. is_close(spectrum%damp_factor, -m_one)) then
2467 select case (spectrum%damp)
2469 spectrum%damp_factor = -log(0.0001_real64)/(spectrum%end_time-spectrum%start_time)
2471 spectrum%damp_factor = sqrt(-log(0.0001_real64)/(spectrum%end_time-spectrum%start_time)**2)
2472 end select
2473 end if
2474
2475
2477 end subroutine spectrum_fix_time_limits
2478
2479 ! -------------------------------------------------------
2480
2481 subroutine spectrum_signal_damp(damp_type, damp_factor, time_start, time_end, t0, time_step, time_function)
2482 integer, intent(in) :: damp_type
2483 real(real64), intent(in) :: damp_factor
2484 integer, intent(in) :: time_start
2485 integer, intent(in) :: time_end
2486 real(real64), intent(in) :: t0
2487 real(real64), intent(in) :: time_step
2488 type(batch_t), intent(inout) :: time_function
2489
2490 integer :: itime, ii
2491 real(real64) :: time
2492 real(real64), allocatable :: weight(:)
2493
2494 push_sub(signal_damp)
2495
2496 assert(time_function%status() == batch_not_packed)
2497
2498 safe_allocate(weight(time_start:time_end))
2499
2500 do itime = time_start, time_end
2501 time = time_step*(itime-1)
2502
2503 ! Gets the damp function
2504 select case (damp_type)
2505 case (spectrum_damp_none)
2506 weight(itime) = m_one
2508 if (time < t0) then
2509 weight(itime) = m_one
2510 else
2511 weight(itime) = exp(-(time - t0)*damp_factor)
2512 end if
2514 if (time < t0) then
2515 weight(itime) = m_one
2516 else
2517 weight(itime) = m_one - m_three*((time - t0) / (time_step * (time_end - 1) - t0))**2 + &
2518 m_two * ((time - t0) / (time_step * (time_end - 1) - t0))**3
2519 end if
2521 if (time < t0) then
2522 weight(itime) = m_one
2523 else
2524 weight(itime) = exp(-(time - t0)**2*damp_factor**2)
2525 end if
2526 case (spectrum_damp_sin)
2527 if (time < t0) then
2528 weight(itime) = m_one
2529 else
2530 weight(itime) = sin(-(time - t0)*m_pi/(time_end+t0))
2531 end if
2532 end select
2533 end do
2534
2535 if (time_function%type() == type_cmplx) then
2536 do ii = 1, time_function%nst_linear
2537 do itime = time_start, time_end
2538 time_function%zff_linear(itime, ii) = weight(itime)*time_function%zff_linear(itime, ii)
2539 end do
2540 end do
2541 else
2542 do ii = 1, time_function%nst_linear
2543 do itime = time_start, time_end
2544 time_function%dff_linear(itime, ii) = weight(itime)*time_function%dff_linear(itime, ii)
2545 end do
2546 end do
2547 end if
2548
2549 safe_deallocate_a(weight)
2550
2551 pop_sub(signal_damp)
2552
2553 end subroutine spectrum_signal_damp
2554 ! -------------------------------------------------------
2555
2556 ! -------------------------------------------------------
2566 subroutine spectrum_fourier_transform(method, transform, noise, time_start, time_end, t0, time_step, time_function, &
2567 energy_start, energy_end, energy_step, energy_function)
2568 integer, intent(in) :: method
2569 integer, intent(in) :: transform
2570 real(real64), intent(in) :: noise
2571 integer, intent(in) :: time_start
2572 integer, intent(in) :: time_end
2573 real(real64), intent(in) :: t0
2574 real(real64), intent(in) :: time_step
2575 type(batch_t), intent(in) :: time_function
2576 real(real64), intent(in) :: energy_start
2577 real(real64), intent(in) :: energy_end
2578 real(real64), intent(in) :: energy_step
2579 type(batch_t), intent(inout) :: energy_function
2580
2581 integer :: itime, ienergy, ii, energy_steps
2582 real(real64) :: energy, sinz, cosz
2583 complex(real64) :: ez, eidt
2584 type(compressed_sensing_t) :: cs
2585
2586 push_sub(fourier_transform)
2587
2588 assert(time_function%nst_linear == energy_function%nst_linear)
2589 assert(time_function%status() == energy_function%status())
2590 assert(time_function%status() == batch_not_packed)
2591 assert(time_function%type() == type_float)
2592 assert(energy_function%type() == type_float)
2593
2594 energy_steps = nint((energy_end-energy_start) / energy_step) + 1
2595
2596 select case (method)
2597
2598 case (spectrum_fourier)
2599
2600 do ienergy = 1, energy_steps
2601
2602 energy = energy_step*(ienergy - 1) + energy_start
2603
2604 do ii = 1, energy_function%nst_linear
2605 energy_function%dff_linear(ienergy, ii) = m_zero
2606 end do
2607
2608 select case (transform)
2609
2610 ! The sine and cosine transforms are computed as the real and imaginary part of the exponential.
2611 ! One can compute the exponential by successive multiplications, instead of calling the sine or
2612 ! cosine function at each time step.
2614
2615 eidt = exp(m_zi * energy * time_step)
2616 ez = exp(m_zi * energy * ((time_start-1)*time_step - t0))
2617 sinz = aimag(ez)
2618 do itime = time_start, time_end
2619 do ii = 1, time_function%nst_linear
2620 energy_function%dff_linear(ienergy, ii) = &
2621 energy_function%dff_linear(ienergy, ii) + &
2622 time_function%dff_linear(itime, ii) * sinz
2623 end do
2624 ez = ez * eidt
2625 sinz = aimag(ez)
2626 end do
2627
2629
2630 eidt = exp(m_zi * energy * time_step)
2631 ez = exp(m_zi * energy * ( (time_start-1)*time_step - t0))
2632 cosz = real(ez, real64)
2633 do itime = time_start, time_end
2634 do ii = 1, time_function%nst_linear
2635 energy_function%dff_linear(ienergy, ii) = &
2636 energy_function%dff_linear(ienergy, ii) + &
2637 time_function%dff_linear(itime, ii) * cosz
2638 end do
2639 ez = ez * eidt
2640 cosz = real(ez, real64)
2641 end do
2642
2644
2645 eidt = exp(-energy * time_step)
2646 ez = exp(-energy * ((time_start - 1) * time_step - t0))
2647 do itime = time_start, time_end
2648 do ii = 1, time_function%nst_linear
2649 energy_function%dff_linear(ienergy, ii) = &
2650 energy_function%dff_linear(ienergy, ii) + &
2651 real( time_function%dff_linear(itime, ii) * ez, real64)
2652 end do
2653 ez = ez * eidt
2654 end do
2655 end select
2656
2657 ! The total sum must be multiplied by time_step in order to get the integral.
2658 do ii = 1, time_function%nst_linear
2659 energy_function%dff_linear(ienergy, ii) = &
2660 energy_function%dff_linear(ienergy, ii) * time_step
2661 end do
2662
2663
2664 end do
2665
2667
2668 call compressed_sensing_init(cs, transform, &
2669 time_end - time_start + 1, time_step, time_step*(time_start - 1) - t0, &
2670 energy_steps, energy_step, energy_start, noise)
2671
2672 do ii = 1, time_function%nst_linear
2673 call compressed_sensing_spectral_analysis(cs, time_function%dff_linear(:, ii), &
2674 energy_function%dff_linear(:, ii))
2675 end do
2676
2677 call compressed_sensing_end(cs)
2678
2679 end select
2680
2681 pop_sub(fourier_transform)
2682
2683 end subroutine spectrum_fourier_transform
2684
2685 ! ---------------------------------------------------------
2686 subroutine spectrum_sigma_diagonalize(namespace, sigma, nspin, energy_step, min_energy, energy_steps, kick)
2687 type(namespace_t), intent(in) :: namespace
2688 real(real64), intent(in) :: sigma(:, :, :, :)
2689 integer, intent(in) :: nspin
2690 real(real64), intent(in) :: energy_step, min_energy
2691 integer, intent(in) :: energy_steps
2692 type(kick_t), optional, intent(in) :: kick
2693
2694 integer :: is, idir, jdir, ie, info, out_file, out_file_t
2695 real(real64), allocatable :: work(:,:)
2696 complex(real64), allocatable :: w(:)
2697 character(len=20) :: header_string
2698 logical :: spins_singlet, spins_triplet, symmetrize
2699 real(real64), allocatable :: pp(:,:), pp2(:,:)
2700
2702
2703
2704 !%Variable PropagationSpectrumSymmetrizeSigma
2705 !%Type logical
2706 !%Default .false.
2707 !%Section Utilities::oct-propagation_spectrum
2708 !%Description
2709 !% The polarizablity tensor has to be real and symmetric. Due to numerical accuracy,
2710 !% that is not extricly conserved when computing it from different time-propations.
2711 !% If <tt>PropagationSpectrumSymmetrizeSigma = yes</tt>, the polarizability tensor is
2712 !% symmetrized before its diagonalizied.
2713 !% This variable is only used if the cross_section_tensor is computed.
2714 !%End
2715 call parse_variable(namespace, 'PropagationSpectrumSymmetrizeSigma', .false., symmetrize)
2716 call messages_print_var_value('PropagationSpectrumSymmetrizeSigma', symmetrize, namespace=namespace)
2717
2718 spins_singlet = .true.
2719 spins_triplet = .false.
2720 if (present(kick)) then
2721 select case (kick_get_type(kick))
2722 case (kick_spin_mode)
2723 spins_triplet = .true.
2724 spins_singlet = .false.
2725 case (kick_spin_density_mode)
2726 spins_triplet = .true.
2727 end select
2728 end if
2729
2730 if (spins_singlet .and. spins_triplet) then
2731 out_file = io_open('cross_section_diagonal-sigma_s', namespace, action='write')
2732 out_file_t = io_open('cross_section_diagonal-sigma_t', namespace, action='write')
2733 else
2734 out_file = io_open('cross_section_diagonal-sigma', namespace, action='write')
2735 end if
2736
2737 is = 1
2738 write(out_file, '(a1, a20)', advance = 'no') '#', str_center("Energy", 20)
2739 do idir = 1, 3
2740 write(out_file, '(a20)', advance = 'no') str_center("Real part", 20)
2741 if (.not. symmetrize) write(out_file, '(a20)', advance = 'no') str_center("Imaginary part", 20)
2742 do jdir = 1, 3
2743 write(header_string,'(a7,i1,a1,i1,a1,i1,a1)') 'vector(', idir, ',', jdir, ',', is, ')'
2744 write(out_file, '(a20)', advance = 'no') str_center(trim(header_string), 20)
2745 end do
2746 end do
2747 write(out_file, '(1x)')
2748 write(out_file, '(a1,a20)', advance = 'no') '#', str_center('[' // trim(units_abbrev(units_out%energy)) // ']', 20)
2749
2750 do idir = 1, 3
2751 write(out_file, '(a20)', advance = 'no') str_center('[' // trim(units_abbrev(units_out%length**2)) // ']', 20)
2752 if (.not. symmetrize) then
2753 write(out_file, '(a20)', advance = 'no') str_center('[' // trim(units_abbrev(units_out%length**2)) // ']', 20)
2754 end if
2755 do jdir = 1, 3
2756 write(out_file, '(a20)', advance = 'no') str_center('[ - ]', 20)
2757 end do
2758 end do
2759 write(out_file, '(1x)')
2760
2761 if (spins_singlet .and. spins_triplet) then
2762 is = 2
2763 write(out_file_t, '(a1, a20)', advance = 'no') '#', str_center("Energy", 20)
2764 do idir = 1, 3
2765 write(out_file_t, '(a20)', advance = 'no') str_center("Real part", 20)
2766 if (.not. symmetrize) write(out_file_t, '(a20)', advance = 'no') str_center("Imaginary part", 20)
2767 do jdir = 1, 3
2768 write(header_string,'(a7,i1,a1,i1,a1,i1,a1)') 'vector(', idir, ',', jdir, ',', is, ')'
2769 write(out_file_t, '(a20)', advance = 'no') str_center(trim(header_string), 20)
2770 end do
2771 end do
2772 write(out_file_t, '(1x)')
2773 write(out_file_t, '(a1,a20)', advance = 'no') '#', str_center('[' // trim(units_abbrev(units_out%energy)) // ']', 20)
2774
2775 do idir = 1, 3
2776 write(out_file_t, '(a20)', advance = 'no') str_center('[' // trim(units_abbrev(units_out%length**2)) // ']', 20)
2777 if (.not. symmetrize) then
2778 write(out_file_t, '(a20)', advance = 'no') str_center('[' // trim(units_abbrev(units_out%length**2)) // ']', 20)
2779 end if
2780 do jdir = 1, 3
2781 write(out_file_t, '(a20)', advance = 'no') str_center('[ - ]', 20)
2782 end do
2783 end do
2784 write(out_file_t, '(1x)')
2785 end if
2786
2787 safe_allocate(pp(1:3, 1:3))
2788 if (spins_triplet .and. spins_singlet) then
2789 safe_allocate(pp2(1:3, 1:3))
2790 end if
2791 safe_allocate(w(1:3))
2792 safe_allocate(work(1:3, 1:3))
2793 do ie = 1, energy_steps
2794
2795 pp(:, :) = sigma(:, :, ie, 1)
2796 if (nspin >= 2) then
2797 if (spins_singlet .and. spins_triplet) then
2798 pp2(:, :) = pp(:, :) - sigma(:, :, ie, 2)
2799 pp(:, :) = pp(:, :) + sigma(:, :, ie, 2)
2800 elseif (spins_triplet .and. .not. spins_singlet) then
2801 pp(:, :) = pp(:, :) - sigma(:, :, ie, 2)
2802 elseif (spins_singlet .and. .not. spins_triplet) then
2803 pp(:, :) = pp(:, :) + sigma(:, :, ie, 2)
2804 end if
2805 end if
2806
2807 if (symmetrize) then
2808 do idir = 1, 3
2809 do jdir = idir + 1, 3
2810 pp(idir, jdir) = (pp(idir, jdir) + pp(jdir, idir)) / m_two
2811 pp(jdir, idir) = pp(idir, jdir)
2812 end do
2813 end do
2814 end if
2815
2816 work(1:3, 1:3) = pp(1:3, 1:3)
2817 call lalg_eigensolve_nonh(3, work, w, err_code = info, sort_eigenvectors = .true.)
2818 ! Note that the cross-section elements do not have to be transformed to the proper units, since
2819 ! they have been read from the "cross_section_vector.x", where they are already in the proper units.
2820
2821 write(out_file,'(e20.8)', advance = 'no') units_from_atomic(units_out%energy, ((ie-1) * energy_step + min_energy))
2822 do idir = 3, 1, -1
2823 if (symmetrize) then
2824 write(out_file,'(2e20.8)', advance = 'no') real(w(idir), real64)
2825 else
2826 write(out_file,'(2e20.8)', advance = 'no') w(idir)
2827 end if
2828
2829 do jdir = 1, 3
2830 write(out_file,'(e20.8)', advance = 'no') work(jdir, idir)
2831 end do
2832 end do
2833 write(out_file, '(1x)')
2834
2835 if (spins_singlet .and. spins_triplet) then
2836 if (symmetrize) then
2837 do idir = 1, 3
2838 do jdir = idir + 1, 3
2839 pp2(idir, jdir) = (pp2(idir, jdir) + pp2(jdir, idir)) / m_two
2840 pp2(jdir, idir) = pp2(idir, jdir)
2841 end do
2842 end do
2843 end if
2844 work(1:3, 1:3) = -pp2(1:3, 1:3)
2845 call lalg_eigensolve_nonh(3, work, w, err_code = info, sort_eigenvectors = .true.)
2846 ! Note that the cross-section elements do not have to be transformed to the proper units, since
2847 ! they have been read from the "cross_section_vector.x", where they are already in the proper units.
2848
2849 write(out_file_t,'(e20.8)', advance = 'no') units_from_atomic(units_out%energy, (ie * energy_step + min_energy))
2850 do idir = 3, 1, -1
2851 if (symmetrize) then
2852 write(out_file_t,'(2e20.8)', advance = 'no') real(w(idir), real64)
2853 else
2854 write(out_file_t,'(2e20.8)', advance = 'no') w(idir)
2855 end if
2856
2857 do jdir = 1, 3
2858 write(out_file_t,'(e20.8)', advance = 'no') work(jdir, idir)
2859 end do
2860 end do
2861 write(out_file_t, '(1x)')
2862 end if
2863 end do
2864
2865 call io_close(out_file)
2866
2867 safe_deallocate_a(pp)
2868 if (spins_triplet .and. spins_singlet) then
2869 safe_deallocate_a(pp2)
2870 call io_close(out_file_t)
2871 end if
2872 safe_deallocate_a(w)
2873 safe_deallocate_a(work)
2874
2876 end subroutine spectrum_sigma_diagonalize
2877
2878 pure integer function spectrum_nenergy_steps(spectrum) result(no_e)
2879 type(spectrum_t), intent(in) :: spectrum
2880
2881 no_e = nint((spectrum%max_energy-spectrum%min_energy) / spectrum%energy_step) + 1
2882 end function spectrum_nenergy_steps
2883
2884 subroutine spectrum_write_info(spectrum, out_file)
2885 type(spectrum_t), intent(in) :: spectrum
2886 integer, intent(in) :: out_file
2887
2888 push_sub(spectrum_write_info)
2889
2890 write(out_file, '(a,i4)') '# PropagationSpectrumDampMode = ', spectrum%damp
2891 write(out_file, '(a,f10.4)') '# PropagationSpectrumDampFactor = ', units_from_atomic(units_out%time**(-1), &
2892 spectrum%damp_factor)
2893 write(out_file, '(a,f10.4)') '# PropagationSpectrumStartTime = ', units_from_atomic(units_out%time, spectrum%start_time)
2894 write(out_file, '(a,f10.4)') '# PropagationSpectrumEndTime = ', units_from_atomic(units_out%time, spectrum%end_time)
2895 write(out_file, '(a,f10.4)') '# PropagationSpectrumMinEnergy = ', units_from_atomic(units_out%energy, spectrum%min_energy)
2896 write(out_file, '(a,f10.4)') '# PropagationSpectrumMaxEnergy = ', units_from_atomic(units_out%energy, spectrum%max_energy)
2897 write(out_file, '(a,f10.4)') '# PropagationSpectrumEnergyStep = ', units_from_atomic(units_out%energy, spectrum%energy_step)
2898
2899 pop_sub(spectrum_write_info)
2900 end subroutine spectrum_write_info
2901
2902 ! ---------------------------------------------------------
2904 subroutine spectrum_read_header_line(namespace, iunit, line, context)
2905 type(namespace_t), intent(in) :: namespace
2906 integer, intent(in) :: iunit
2907 character(len=*), intent(out) :: line
2908 character(len=*), intent(in) :: context
2909
2910 integer :: ierr
2911
2913
2914 read(iunit, '(a)', iostat = ierr) line
2915 if (ierr /= 0) then
2916 write(message(1), '(3a)') "Could not read ", trim(context), " from multipoles header."
2917 call messages_fatal(1, namespace=namespace)
2918 end if
2919
2921 end subroutine spectrum_read_header_line
2922
2923
2924 ! ---------------------------------------------------------
2926 subroutine spectrum_read_header_integer(namespace, iunit, value, context)
2927 type(namespace_t), intent(in) :: namespace
2928 integer, intent(in) :: iunit
2929 integer, intent(out) :: value
2930 character(len=*), intent(in) :: context
2931
2932 integer :: ierr
2933 character(len=200) :: line
2934
2936
2937 call spectrum_read_header(namespace, iunit, line, context)
2938 ! see the routine td_write_multipole_r to understand this
2939 if (len_trim(line) < 16) then
2940 ierr = 1
2941 else
2942 read(line(16:), *, iostat = ierr) value
2943 end if
2944 if (ierr /= 0) then
2945 write(message(1), '(3a)') "Could not read ", trim(context), " from multipoles header line:"
2946 write(message(2), '(a)') trim(line)
2947 call messages_fatal(2, namespace=namespace)
2948 end if
2949
2951 end subroutine spectrum_read_header_integer
2952
2953
2954 ! ---------------------------------------------------------
2956 subroutine spectrum_find_cross_section_header(namespace, iunit)
2957 type(namespace_t), intent(in) :: namespace
2958 integer, intent(in) :: iunit
2959
2960 integer :: ierr
2961 character(len=200) :: line
2962
2964
2965 rewind(iunit)
2966 do
2967 read(iunit, '(a)', iostat = ierr) line
2968 if (ierr == iostat_end) then
2969 message(1) = "Could not find cross-section header in spectrum file."
2970 call messages_fatal(1, namespace=namespace)
2971 end if
2972 if (ierr /= 0) then
2973 message(1) = "Could not read spectrum file while looking for cross-section header."
2974 call messages_fatal(1, namespace=namespace)
2975 end if
2976 if (index(adjustl(line), "# nspin") == 1) then
2977 backspace(iunit)
2978 exit
2979 end if
2980 end do
2981
2984
2985
2986end module spectrum_oct_m
2987
2988!! Local Variables:
2989!! mode: f90
2990!! coding: utf-8
2991!! End:
subroutine optimize()
subroutine info()
Definition: em_resp.F90:1093
initialize a batch with existing memory
Definition: batch.F90:278
This is the common interface to a simple-minded polynomical interpolation procedure (simple use of th...
Definition: math.F90:189
Prints out to iunit a message in the form: ["InputVariable" = value] where "InputVariable" is given b...
Definition: messages.F90:182
double log(double __x) __attribute__((__nothrow__
double exp(double __x) __attribute__((__nothrow__
double sin(double __x) __attribute__((__nothrow__
double sqrt(double __x) __attribute__((__nothrow__
This module implements batches of mesh functions.
Definition: batch.F90:135
integer, parameter spectrum_transform_cos
integer, parameter spectrum_transform_sin
Fast Fourier Transform module. This module provides a single interface that works with different FFT ...
Definition: fft.F90:120
subroutine, public fft_init(this, nn, dim, type, library, optimize, optimize_parity, comm, mpi_grp, use_aligned, howmany)
Definition: fft.F90:412
subroutine, public fft_end(this)
Definition: fft.F90:800
integer, parameter, public fft_complex
Definition: fft.F90:174
integer, parameter, public fftlib_fftw
Definition: fft.F90:179
real(real64), parameter, public m_two
Definition: global.F90:202
real(real64), parameter, public m_zero
Definition: global.F90:200
real(real64), parameter, public m_four
Definition: global.F90:204
real(real64), parameter, public p_ry
Definition: global.F90:239
real(real64), parameter, public m_third
Definition: global.F90:207
real(real64), parameter, public m_pi
some mathematical constants
Definition: global.F90:198
character(len= *), parameter, public pcm_dir
Definition: global.F90:288
complex(real64), parameter, public m_z0
Definition: global.F90:210
complex(real64), parameter, public m_zi
Definition: global.F90:214
real(real64), parameter, public p_c
Electron gyromagnetic ratio, see Phys. Rev. Lett. 130, 071801 (2023)
Definition: global.F90:242
real(real64), parameter, public m_one
Definition: global.F90:201
real(real64), parameter, public m_three
Definition: global.F90:203
Definition: io.F90:116
subroutine, public io_close(iunit, grp)
Definition: io.F90:467
subroutine, public io_skip_header(iunit)
Definition: io.F90:646
integer function, public io_open(file, namespace, action, status, form, position, die, recl, grp)
Definition: io.F90:402
subroutine, public kick_read(kick, iunit, namespace)
Definition: kick.F90:820
integer, parameter, public kick_spin_mode
Definition: kick.F90:165
pure integer function, public kick_get_type(kick)
Definition: kick.F90:1365
subroutine, public kick_write(kick, iunit, out)
Definition: kick.F90:892
integer, parameter, public kick_density_mode
Definition: kick.F90:165
integer, parameter, public kick_function_dipole
Definition: kick.F90:160
integer, parameter, public kick_spin_density_mode
Definition: kick.F90:165
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:117
pure complex(real64) function, dimension(1:3), public zcross_product(a, b)
Definition: math.F90:1718
subroutine, public messages_print_with_emphasis(msg, iunit, namespace)
Definition: messages.F90:898
character(len=512), private msg
Definition: messages.F90:167
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:525
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:162
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:410
subroutine, public messages_input_error(namespace, var, details, row, column)
Definition: messages.F90:691
subroutine, public messages_experimental(name, namespace)
Definition: messages.F90:1040
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:594
real(real64), dimension(:,:), allocatable sigma
S_E matrix.
Definition: pcm.F90:269
subroutine, public pcm_dipole(mu_pcm, q_pcm, tess, n_tess)
Computes the dipole moment mu_pcm due to a distribution of charges q_pcm.
Definition: pcm.F90:3235
subroutine, public pcm_eps(pcm, eps, omega)
Definition: pcm.F90:3255
subroutine, public pcm_min_input_parsing_for_spectrum(pcm, namespace)
Definition: pcm.F90:3279
subroutine, public spectrum_hsfunction_min(namespace, aa, bb, omega_min, func_min)
Definition: spectrum.F90:1592
subroutine spectrum_tdfile_info(namespace, fname, iunit, time_steps, dt)
Definition: spectrum.F90:2485
subroutine spectrum_hsfunction_ar_end
Definition: spectrum.F90:1728
subroutine spectrum_hsfunction_ar_init(dt, is, ie, niter, acc, pos, tret)
Definition: spectrum.F90:1703
subroutine spectrum_read_header_line(namespace, iunit, line, context)
Read a required multipoles or spectrum header line.
Definition: spectrum.F90:3000
subroutine, public spectrum_cross_section(spectrum, namespace, in_file, out_file, ref_file)
Definition: spectrum.F90:717
subroutine spectrum_times_pcm_epsilon(spectrum, pcm, dipole, sigma, nspin, istart, iend, kick_time, dt, no_e)
Definition: spectrum.F90:1068
subroutine, public spectrum_hs_ar_from_acc(spectrum, namespace, out_file, vec, w0)
Definition: spectrum.F90:1741
integer, parameter, public spectrum_damp_lorentzian
Definition: spectrum.F90:166
subroutine, public spectrum_fix_time_limits(spectrum, time_steps, dt, istart, iend, ntiter)
Definition: spectrum.F90:2533
subroutine, public spectrum_fourier_transform(method, transform, noise, time_start, time_end, t0, time_step, time_function, energy_start, energy_end, energy_step, energy_function)
Computes the sine, cosine, (or "exponential") Fourier transform of the real function given in the tim...
Definition: spectrum.F90:2663
integer, parameter, public spectrum_transform_laplace
Definition: spectrum.F90:173
subroutine spectrum_find_cross_section_header(namespace, iunit)
Position a spectrum file at the cross-section metadata header.
Definition: spectrum.F90:3052
subroutine, public spectrum_cross_section_tensor(spectrum, namespace, out_file, in_file)
Definition: spectrum.F90:437
subroutine, public spectrum_hsfunction_init(dt, is, ie, niter, acc)
Definition: spectrum.F90:1543
subroutine, public spectrum_hsfunction_end
Definition: spectrum.F90:1578
subroutine spectrum_read_dipole(namespace, in_file, dipole)
Definition: spectrum.F90:918
integer, parameter, public spectrum_damp_sin
Definition: spectrum.F90:166
subroutine spectrum_sigma_diagonalize(namespace, sigma, nspin, energy_step, min_energy, energy_steps, kick)
Definition: spectrum.F90:2782
subroutine spectrum_cross_section_info(namespace, iunit, nspin, kick, energy_steps, dw)
Definition: spectrum.F90:2446
subroutine, public spectrum_dyn_structure_factor(spectrum, namespace, in_file_sin, in_file_cos, out_file)
Definition: spectrum.F90:1269
integer, parameter, public spectrum_damp_gaussian
Definition: spectrum.F90:166
subroutine, public spectrum_init(spectrum, namespace, default_energy_step, default_max_energy)
Definition: spectrum.F90:220
subroutine, public spectrum_mult_info(namespace, iunit, nspin, kick, time_steps, dt, file_units, lmax)
Definition: spectrum.F90:2352
integer, parameter, public spectrum_fourier
Definition: spectrum.F90:184
subroutine, public spectrum_dipole_power(spectrum, namespace, in_file, out_file)
Definition: spectrum.F90:1161
subroutine spectrum_add_pcm_dipole(namespace, dipole, time_steps, nspin)
Definition: spectrum.F90:949
subroutine, public spectrum_signal_damp(damp_type, damp_factor, time_start, time_end, t0, time_step, time_function)
Definition: spectrum.F90:2577
subroutine, public spectrum_hs_from_acc(spectrum, namespace, out_file, pol, vec, w0)
Definition: spectrum.F90:2061
integer, parameter, public spectrum_energyloss
Definition: spectrum.F90:178
subroutine spectrum_over_pcm_refraction_index(spectrum, pcm, sigma, nspin, no_e)
Definition: spectrum.F90:1132
subroutine, public spectrum_hs_from_current(spectrum, namespace, out_file, pol, vec, w0)
Definition: spectrum.F90:2162
subroutine spectrum_cross_section_tensor_write(out_file, sigma, nspin, energy_step, min_energy, energy_steps, kick)
Definition: spectrum.F90:600
subroutine spectrum_read_header_integer(namespace, iunit, value, context)
Read a required integer value from a fixed-label header line.
Definition: spectrum.F90:3022
subroutine spectrum_write_info(spectrum, out_file)
Definition: spectrum.F90:2980
subroutine spectrum_hs_output(spectrum, namespace, out_file, pol, no_e, sp)
Definition: spectrum.F90:2317
integer, parameter, public spectrum_damp_polynomial
Definition: spectrum.F90:166
subroutine hsfunction(omega, power)
Definition: spectrum.F90:1659
integer, parameter, public spectrum_rotatory
Definition: spectrum.F90:178
subroutine spectrum_hs(spectrum, namespace, out_file, pol, w0)
Definition: spectrum.F90:2261
integer, parameter, public spectrum_damp_none
Definition: spectrum.F90:166
integer, parameter, public spectrum_transform_cos
Definition: spectrum.F90:173
integer, parameter, public spectrum_transform_sin
Definition: spectrum.F90:173
subroutine, public spectrum_count_time_steps(namespace, iunit, time_steps, dt)
Definition: spectrum.F90:2398
subroutine, public spectrum_rotatory_strength(spectrum, namespace, in_file, out_file)
Definition: spectrum.F90:1424
pure integer function, public spectrum_nenergy_steps(spectrum)
Definition: spectrum.F90:2974
subroutine, public spectrum_hs_from_mult(spectrum, namespace, out_file, pol, vec, w0)
Definition: spectrum.F90:1942
integer, parameter, public spectrum_compressed_sensing
Definition: spectrum.F90:184
subroutine, public spectrum_hs_ar_from_mult(spectrum, namespace, out_file, vec, w0)
Definition: spectrum.F90:1845
integer, parameter, public spectrum_p_power
Definition: spectrum.F90:178
character(len=80) function, public str_center(s_in, l_in)
puts space around string, so that it is centered
Definition: string.F90:176
brief This module defines the class unit_t which is used by the unit_systems_oct_m module.
Definition: unit.F90:134
character(len=20) pure function, public units_abbrev(this)
Definition: unit.F90:225
This module defines the unit system, used for input and output.
integer, parameter, public units_atomic
type(unit_system_t), public units_out
subroutine, public unit_system_get(uu, cc)
integer, parameter, public units_eva
type(unit_system_t), public units_inp
the units systems for reading and writing
type(unit_t), public unit_one
some special units required for particular quantities
Class defining batches of mesh functions.
Definition: batch.F90:162
int true(void)