Octopus
casida_spectrum.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2006 M. Marques, A. Castro, A. Rubio, G. Bertsch
2!! Copyright (C) 2012-2013 D. Strubbe
3!!
4!! This program is free software; you can redistribute it and/or modify
5!! it under the terms of the GNU General Public License as published by
6!! the Free Software Foundation; either version 2, or (at your option)
7!! any later version.
8!!
9!! This program is distributed in the hope that it will be useful,
10!! but WITHOUT ANY WARRANTY; without even the implied warranty of
11!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12!! GNU General Public License for more details.
13!!
14!! You should have received a copy of the GNU General Public License
15!! along with this program; if not, write to the Free Software
16!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17!! 02110-1301, USA.
18!!
19
20#include "global.h"
21
24 use debug_oct_m
25 use global_oct_m
26 use io_oct_m
27 use ions_oct_m
28 use, intrinsic :: iso_fortran_env
30 use mpi_oct_m
32 use parser_oct_m
34 use space_oct_m
35 use unit_oct_m
37 use utils_oct_m
39
40 implicit none
41
42 private
43 public :: casida_spectrum_main
44
45contains
46
47 subroutine casida_spectrum_main() bind(c)
48 type casida_spectrum_t
49 real(real64) :: br, energy_step, min_energy, max_energy
50 integer :: ispin
51 end type casida_spectrum_t
52
53 integer :: ierr, idir, jdir, iatom
54 type(space_t) :: space
55 type(casida_spectrum_t) :: cs
56 real(real64), allocatable :: rotation(:,:), rot2(:,:), identity(:,:), coord(:)
57 type(block_t) :: blk
58 type(ions_t), pointer :: ions
59
60 ! Initialize stuff
62
63 call getopt_init(ierr)
64 if (ierr == 0) call getopt_casida_spectrum()
65 call getopt_end()
66
67 call parser_init()
68 call messages_init()
69 call io_init()
73
74 allocate(rotation(space%dim, space%dim))
75 allocate(rot2(space%dim, space%dim))
76 allocate(identity(space%dim, space%dim))
77 allocate(coord(space%dim))
78
79 ! Reads the spin components. This is read here, as well as in states_init.
80 call parse_variable(global_namespace, 'SpinComponents', 1, cs%ispin)
81 if (.not. varinfo_valid_option('SpinComponents', cs%ispin)) call messages_input_error(global_namespace, 'SpinComponents')
82 cs%ispin = min(2, cs%ispin)
83
84 !%Variable CasidaSpectrumBroadening
85 !%Type float
86 !%Default 0.005 Ha
87 !%Section Utilities::oct-casida_spectrum
88 !%Description
89 !% Width of the Lorentzian used to broaden the excitations.
90 !%End
91 call parse_variable(global_namespace, 'CasidaSpectrumBroadening', 0.005_real64, cs%br, units_inp%energy)
92
93 call messages_print_var_value('CasidaSpectrumBroadening', cs%br, unit = units_out%energy)
95 !%Variable CasidaSpectrumEnergyStep
96 !%Type float
97 !%Default 0.001 Ha
98 !%Section Utilities::oct-casida_spectrum
99 !%Description
100 !% Sampling rate for the spectrum.
101 !%End
102 call parse_variable(global_namespace, 'CasidaSpectrumEnergyStep', 0.001_real64, cs%energy_step, units_inp%energy)
103
104 call messages_print_var_value('CasidaSpectrumEnergyStep', cs%energy_step, unit = units_out%energy)
105
106 !%Variable CasidaSpectrumMinEnergy
107 !%Type float
108 !%Default 0.0
109 !%Section Utilities::oct-casida_spectrum
110 !%Description
111 !% The broadening is done for energies greater than <tt>CasidaSpectrumMinEnergy</tt>.
112 !%End
113 call parse_variable(global_namespace, 'CasidaSpectrumMinEnergy', m_zero, cs%min_energy, units_inp%energy)
114
115 call messages_print_var_value('CasidaSpectrumMinEnergy', cs%min_energy, unit = units_out%energy)
116
117 !%Variable CasidaSpectrumMaxEnergy
118 !%Type float
119 !%Default 1.0 Ha
120 !%Section Utilities::oct-casida_spectrum
121 !%Description
122 !% The broadening is done for energies smaller than <tt>CasidaSpectrumMaxEnergy</tt>.
123 !%End
124 call parse_variable(global_namespace, 'CasidaSpectrumMaxEnergy', m_one, cs%max_energy, units_inp%energy)
125
126 call messages_print_var_value('CasidaSpectrumMaxEnergy', cs%max_energy, unit = units_out%energy)
127
128 identity = m_zero
129 do idir = 1, space%dim
130 identity(idir, idir) = m_one
131 end do
132
133 !%Variable CasidaSpectrumRotationMatrix
134 !%Type block
135 !%Default identity
136 !%Section Utilities::oct-casida_spectrum
137 !%Description
138 !% Supply a rotation matrix to apply to the transition dipoles in generating the spectrum. The rotated atomic structure
139 !% will also be output. Size of matrix must be <tt>Dimensions</tt>.
140 !%End
141
142 if (parse_block(global_namespace, 'CasidaSpectrumRotationMatrix', blk) == 0) then
143 do idir = 1, space%dim
144 do jdir = 1, space%dim
145 call parse_block_float(blk, idir - 1, jdir - 1, rotation(idir, jdir))
146 end do
147 end do
148 call parse_block_end(blk)
149
150 message(1) = "Info: Applying rotation matrix"
151 call messages_info(1)
152 call output_tensor(rotation, space%dim, unit_one, write_average = .false., namespace=global_namespace)
153
154 ! allowing inversions is fine
155 rot2(:,:) = abs(matmul(transpose(rotation), rotation))
156 if (any(abs(rot2(:,:) - identity(:,:)) > 1e-6_real64)) then
157 write(message(1),'(a,es13.6)') "Rotation matrix is not orthogonal. max discrepancy in product = ", &
158 maxval(abs(rot2(:,:) - identity(:,:)))
159 call messages_warning(1)
160 end if
161
162 ! apply rotation to geometry
164 do iatom = 1, ions%natoms
165 coord = ions%pos(:, iatom)
166 ions%pos(:, iatom) = matmul(rotation, coord)
167 end do
168 call ions%write_xyz(trim(casida_dir)//'rotated')
169 safe_deallocate_p(ions)
170 else
171 rotation(:,:) = identity(:,:)
172 end if
173
174 call calc_broad(cs, casida_dir, 'eps_diff', .true.)
175 call calc_broad(cs, casida_dir, 'petersilka', .false.)
176 call calc_broad(cs, casida_dir, 'tamm_dancoff', .false.)
177 call calc_broad(cs, casida_dir, 'variational', .false.)
178 call calc_broad(cs, casida_dir, 'casida', .false.)
179
180 deallocate(rotation)
181 deallocate(rot2)
182 deallocate(identity)
183 deallocate(coord)
184
186 call io_end()
187 call messages_end()
188
189 call parser_end()
190 call global_end()
191
192 contains
193
194 ! ---------------------------------------------------------
195 subroutine calc_broad(cs, dir, fname, extracols)
196 type(casida_spectrum_t), intent(in) :: cs
197 character(len=*), intent(in) :: dir
198 character(len=*), intent(in) :: fname
199 logical, intent(in) :: extracols
200
201 real(real64), allocatable :: spectrum(:,:)
202 real(real64) :: omega, energy, re_tm(space%dim), im_tm(space%dim), ff(space%dim+1), tm_sq(space%dim)
203 integer :: istep, nsteps, iunit, trash(3), idir, ncols, ios
204 character(len=256) :: string
205 logical :: is_complex
206
207 push_sub(calc_broad)
208
209 nsteps = int((cs%max_energy - cs%min_energy) / cs%energy_step)
210 safe_allocate(spectrum(1:space%dim+1, 1:nsteps))
211 spectrum = m_zero
212
213 iunit = io_open(trim(dir)// fname, global_namespace, action='read', status='old', die = .false.)
214 if (iunit == -1) then
215 message(1) = 'Cannot open file "'//trim(dir)//trim(fname)//'".'
216 message(2) = 'The '//trim(fname)//' spectrum was not generated.'
217 call messages_warning(2)
218 pop_sub(calc_broad)
219 return
220 end if
221
222 ! For Casida, CV(2), Tamm-Dancoff, Petersilka: first column is the index of the excitation
223 ! For eps_diff: first two columns are occ and unocc states, then spin if spin-polarized
224 ncols = 1
225 if (extracols) then
226 ncols = ncols + cs%ispin
227 end if
228
229 read(iunit, *) ! skip header
230
231 read(iunit, '(a)') string
232 ! complex has this many columns; real lacks the imaginary columns (im_tm)
233 read(string, *, iostat = ios) trash(1:ncols), energy, (re_tm(idir), im_tm(idir), idir = 1, space%dim), ff(space%dim+1)
234 is_complex = (ios == 0)
235 ! put it back to read the data in the loop
236 backspace(iunit)
237
238 do
239 if (is_complex) then
240 read(iunit, *, iostat = ios) trash(1:ncols), energy, (re_tm(idir), im_tm(idir), idir = 1, space%dim), ff(space%dim+1)
241 im_tm = units_to_atomic(units_out%length, im_tm)
242 else
243 read(iunit, *, iostat = ios) trash(1:ncols), energy, (re_tm(idir), idir = 1, space%dim), ff(space%dim+1)
244 end if
245 re_tm = units_to_atomic(units_out%length, re_tm)
246 ! ff, the last column, is a dimensionless number
247
248 if (ios < 0) then
249 exit ! end of file
250 else if (ios > 0) then
251 message(1) = "Error parsing file " // trim(fname)
252 call messages_fatal(1)
253 end if
254
255 energy = units_to_atomic(units_out%energy, energy)
256 ! transition matrix elements by themselves are dependent on gauge in degenerate subspaces
257 ! make into oscillator strengths, as in casida_inc.F90 X(oscillator_strengths), and like the last column
258 tm_sq = (matmul(rotation, re_tm))**2
259 if (is_complex) then
260 tm_sq = tm_sq + (matmul(rotation, im_tm))**2
261 end if
262 ff(1:space%dim) = m_two * energy * tm_sq
263
264 do istep = 1, nsteps
265 omega = cs%min_energy + real(istep-1, real64) *cs%energy_step
266 spectrum(:, istep) = spectrum(:, istep) + ff*cs%br/((omega-energy)**2 + cs%br**2)/m_pi ! Lorentzian
267 end do
268 end do
269 call io_close(iunit)
270
271 ! print spectra
272 iunit = io_open(trim(dir)//"/spectrum."//fname, global_namespace, action='write')
273
274 write(iunit, '(a2,a12)', advance = 'no') '# ', 'E [' // trim(units_abbrev(units_out%energy)) // ']'
275 do idir = 1, space%dim
276 write(iunit, '(a14)', advance = 'no') '<' // index2axis(idir) // index2axis(idir) // '>'
277 end do
278 write(iunit, '(a14)') '<f>'
279
280 do istep = 1, nsteps
281 write(iunit, '(99es14.6)') units_from_atomic(units_out%energy, cs%min_energy + real(istep - 1, real64) &
282 *cs%energy_step), (units_from_atomic(unit_one/units_out%energy, spectrum(idir, istep)), idir = 1, space%dim+1)
283 end do
284
285 call io_close(iunit)
286
287 safe_deallocate_a(spectrum)
288 pop_sub(calc_broad)
289 end subroutine calc_broad
290 end subroutine casida_spectrum_main
291
293
294!! Local Variables:
295!! mode: f90
296!! coding: utf-8
297!! End:
subroutine calc_broad(cs, dir, fname, extracols)
Each program/utility that needs to use the getopt features should have an interface here – the defini...
Prints out to iunit a message in the form: ["InputVariable" = value] where "InputVariable" is given b...
Definition: messages.F90:158
subroutine, public casida_spectrum_main()
This module provides an interface to the getopt C library, so that the command line arguments can be ...
subroutine, public getopt_init(ierr)
Initializes the getopt machinery. Must be called before attempting to parse the options....
subroutine, public getopt_end
real(real64), parameter, public m_two
Definition: global.F90:175
subroutine, public global_end()
Finalise parser varinfo file, and MPI.
Definition: global.F90:500
real(real64), parameter, public m_zero
Definition: global.F90:173
character(len= *), parameter, public casida_dir
Definition: global.F90:259
real(real64), parameter, public m_pi
some mathematical constants
Definition: global.F90:171
type(mpi_comm), parameter, public serial_dummy_comm
Alias MPI_COMM_UNDEFINED for the specific use case of initialising Octopus utilities with no MPI supp...
Definition: global.F90:268
subroutine, public init_octopus_globals(comm)
Initialise Octopus-specific global constants and files. This routine performs no initialisation calls...
Definition: global.F90:438
real(real64), parameter, public m_one
Definition: global.F90:174
Definition: io.F90:93
subroutine, public io_init(defaults)
If the argument defaults is present and set to true, then the routine will not try to read anything f...
Definition: io.F90:142
subroutine, public io_close(iunit, grp)
Definition: io.F90:444
subroutine, public io_end()
Definition: io.F90:248
integer function, public io_open(file, namespace, action, status, form, position, die, recl, grp)
Definition: io.F90:379
subroutine, public messages_end()
Definition: messages.F90:249
subroutine, public messages_init(output_dir)
Definition: messages.F90:196
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:501
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:138
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:386
subroutine, public messages_input_error(namespace, var, details, row, column)
Definition: messages.F90:671
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:570
type(mpi_grp_t), public mpi_world
Definition: mpi.F90:317
type(namespace_t), public global_namespace
Definition: namespace.F90:112
subroutine, public parser_init()
Initialise the Octopus parser.
Definition: parser.F90:387
subroutine, public parser_end()
End the Octopus parser.
Definition: parser.F90:419
integer function, public parse_block(namespace, name, blk, check_varinfo_)
Definition: parser.F90:600
/* To use the profiling module, call profiling_in and profiling_out with the label of the region as a...
Definition: profiling.F90:122
subroutine, public profiling_end(namespace)
Definition: profiling.F90:455
subroutine, public profiling_init(namespace)
Create profiling subdirectory.
Definition: profiling.F90:264
brief This module defines the class unit_t which is used by the unit_systems_oct_m module.
Definition: unit.F90:111
character(len=20) pure function, public units_abbrev(this)
Definition: unit.F90:202
This module defines the unit system, used for input and output.
type(unit_system_t), public units_out
subroutine, public unit_system_init(namespace)
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
This module is intended to contain simple general-purpose utility functions and procedures.
Definition: utils.F90:99
subroutine, public output_tensor(tensor, ndim, unit, write_average, iunit, namespace)
Definition: utils.F90:231
character pure function, public index2axis(idir)
Definition: utils.F90:191
int true(void)