Octopus
casida.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!! Copyright (C) 2017-2018 J. Flick, S. Ohlmann
4!!
5!! This program is free software; you can redistribute it and/or modify
6!! it under the terms of the GNU General Public License as published by
7!! the Free Software Foundation; either version 2, or (at your option)
8!! any later version.
9!!
10!! This program is distributed in the hope that it will be useful,
11!! but WITHOUT ANY WARRANTY; without even the implied warranty of
12!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13!! GNU General Public License for more details.
14!!
15!! You should have received a copy of the GNU General Public License
16!! along with this program; if not, write to the Free Software
17!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18!! 02110-1301, USA.
19!!
20
21#include "global.h"
22
44
45module casida_oct_m
46 use batch_oct_m
49 use comm_oct_m
50 use debug_oct_m
55#ifdef HAVE_ELPA
56 use elpa
57#endif
60 use forces_oct_m
62 use global_oct_m
63 use grid_oct_m
65 use io_oct_m
67 use iso_c_binding
68 use, intrinsic :: iso_fortran_env
73 use lda_u_oct_m
74 use loct_oct_m
76 use mesh_oct_m
79 use mpi_oct_m
84 use parser_oct_m
85 use pblas_oct_m
86 use pcm_oct_m
95 use sort_oct_m
96 use space_oct_m
102 use unit_oct_m
104 use utils_oct_m
105 use varinfo_oct_m
106 use v_ks_oct_m
107 use xc_oct_m
108 use xc_sic_oct_m
109 use xc_f03_lib_m
111
112 implicit none
113
114 private
115 public :: casida_run
116
117 integer, parameter :: &
118 CASIDA_EPS_DIFF = 1, &
119 casida_petersilka = 2, &
121 casida_variational = 8, &
122 casida_casida = 16
123
124 integer, parameter :: &
125 SOLVER_ELPA = 1, &
127
129 type casida_t
130 private
131 integer :: type
132 ! !< CASIDA_VARIATIONAL | CASIDA_CASIDA
133
134 logical :: states_are_real
135 integer, allocatable :: n_occ(:)
136 integer, allocatable :: n_unocc(:)
137 integer :: nst
138 integer :: nik
139 integer :: space_dim
140 integer :: el_per_state
141 character(len=80) :: trandens
142 character(len=80) :: print_exst
143 real(real64) :: weight_thresh
144 logical :: triplet
145 logical :: calc_forces
146 logical :: calc_forces_kernel
147 logical :: calc_forces_scf
148 logical :: herm_conj
149 type(restart_t) :: restart_load
150 type(restart_t) :: restart_dump
151
152 logical, allocatable :: is_included(:,:,:)
153 integer :: n_pairs
154 type(states_pair_t), allocatable :: pair(:)
155
156 integer, allocatable :: index(:,:,:)
157 integer, allocatable :: ind(:)
158
159 real(real64), allocatable :: dmat(:,:)
160 real(real64), allocatable :: dmat_save(:,:)
161 complex(real64), allocatable :: zmat(:,:)
162 complex(real64), allocatable :: zmat_save(:,:)
163
165 real(real64), allocatable :: dmatB(:,:)
166 complex(real64), allocatable :: zmatB(:,:)
167 real(real64), allocatable :: w(:)
168 real(real64), allocatable :: dtm(:, :)
169 complex(real64), allocatable :: ztm(:, :)
170 real(real64), allocatable :: f(:)
171 real(real64), allocatable :: s(:)
172
173 real(real64), allocatable :: rho(:,:)
174 real(real64), allocatable :: fxc(:,:,:)
175 real(real64) :: kernel_lrc_alpha
176 logical :: gga
177 real(real64), allocatable :: fxc_grad(:,:,:,:,:)
178 real(real64), allocatable :: fxc_grad_spin(:,:,:,:)
179
180 real(real64), allocatable :: dmat2(:,:)
181 complex(real64), allocatable :: zmat2(:,:)
182 real(real64), allocatable :: dlr_hmat2(:,:)
183 complex(real64), allocatable :: zlr_hmat2(:,:)
184 real(real64), allocatable :: forces(:,:,:)
185 real(real64), allocatable :: dw2(:)
186 real(real64), allocatable :: zw2(:)
187
188 ! variables for momentum-transfer-dependent calculation
189 logical :: qcalc
190 real(real64), allocatable :: qvector(:)
191 real(real64), allocatable :: qf(:)
192 real(real64), allocatable :: qf_avg(:)
193 integer :: avg_order
194
195 logical :: parallel_in_eh_pairs
196 logical :: parallel_in_domains
197 logical :: distributed_matrix
198 logical :: write_matrix
199 integer :: parallel_solver
200 type(mpi_grp_t) :: mpi_grp
201 logical :: fromScratch
202 logical :: has_photons
203 integer :: pt_nmodes
204 type(photon_mode_t), pointer :: photon_modes => null()
205
206 integer :: n, nb_rows, nb_cols, block_size
207 type(blacs_proc_grid_t) :: proc_grid
208 integer :: desc(blacs_dlen)
209 type(mpi_datatype) :: darray
210 end type casida_t
213 private
214 integer :: qi
215 integer :: qa
216 integer :: qk
217 real(real64), allocatable :: dpot(:)
218 complex(real64), allocatable :: zpot(:)
221contains
223 subroutine casida_run(system, from_scratch)
224 class(*), intent(inout) :: system
225 logical, intent(in) :: from_scratch
227 push_sub(casida_run)
229 select type (system)
230 class is (multisystem_basic_t)
231 message(1) = "CalculationMode = casida not implemented for multi-system calculations"
232 call messages_fatal(1, namespace=system%namespace)
233 type is (electrons_t)
234 call casida_run_legacy(system, from_scratch)
235 end select
236
237 pop_sub(casida_run)
238 end subroutine casida_run
240 ! ---------------------------------------------------------
241 subroutine casida_run_legacy(sys, fromScratch)
242 type(electrons_t), intent(inout) :: sys
243 logical, intent(in) :: fromscratch
244
245 type(casida_t) :: cas
246 type(block_t) :: blk
247 integer :: idir, theorylevel, iatom, ierr, default_int
248 character(len=100) :: restart_filename
249 logical :: is_frac_occ
250 type(restart_t) :: gs_restart
251
253 call profiling_in('CASIDA')
255 if (sys%hm%pcm%run_pcm) then
256 call messages_not_implemented("PCM for CalculationMode /= gs or td", namespace=sys%namespace)
257 end if
259 if (sys%space%is_periodic()) then
260 message(1) = "Casida oscillator strengths will be incorrect in periodic systems."
261 call messages_warning(1, namespace=sys%namespace)
262 end if
264 if (kpoints_number(sys%kpoints) > 1) then
265 ! Hartree matrix elements may not be correct, not tested anyway. --DAS
266 call messages_not_implemented("Casida with k-points", namespace=sys%namespace)
267 end if
268 if (family_is_mgga_with_exc(sys%hm%xc)) then
269 call messages_not_implemented("Casida with MGGA and non-local terms", namespace=sys%namespace)
270 end if
271 if (sys%hm%lda_u_level /= dft_u_none) then
272 call messages_not_implemented("Casida with DFT+U", namespace=sys%namespace)
273 end if
274 if (sys%hm%theory_level == hartree_fock) then
275 ! Only pure Hartree-Fock (full Fock exchange, no semilocal xc) is supported.
276 if (family_is_hybrid(sys%ks%xc)) then
277 call messages_not_implemented("Casida for hybrid functionals", namespace=sys%namespace)
278 end if
279 call messages_experimental("Casida for Hartree-Fock", namespace=sys%namespace)
280 end if
281 if (sys%hm%theory_level == generalized_kohn_sham_dft) then
282 call messages_not_implemented("Casida for generalized Kohn-Sham", namespace=sys%namespace)
283 end if
285 if (sys%hm%ep%requires_generalized_overlap) then
286 if (sys%st%d%ispin == spinors) then
287 call messages_not_implemented("Casida with ultrasoft/PAW and spinors", namespace=sys%namespace)
288 end if
289 call messages_experimental("Casida with ultrasoft/PAW pseudopotentials", namespace=sys%namespace)
290 ! eps_diff excitation energies are exact (the eigenvalues come from the generalized
291 ! eigenproblem H psi = eps S psi). The oscillator strengths include the ultrasoft/PAW
292 ! augmentation of the dipole matrix elements (see X(oscillator_strengths)). The coupled
293 ! matrix levels (petersilka/tamm_dancoff/variational/full Casida) still build the kernel
294 ! from the SMOOTH transition densities psitilde_i* psitilde_a (the augmentation of the
295 ! transition density is not yet included), so those excitations are approximate.
296 message(1) = "Casida with ultrasoft/PAW: eps_diff energies are exact and the oscillator"
297 message(2) = "strengths include the augmentation term, but for theory levels beyond eps_diff"
298 message(3) = "the kernel neglects the augmentation of the transition density (approximate)."
299 call messages_warning(3, namespace=sys%namespace)
300 end if
301
302 message(1) = 'Info: Starting Casida linear-response calculation.'
303 call messages_info(1, namespace=sys%namespace)
304
305 call messages_print_with_emphasis(msg="XC Kernel level", namespace=sys%namespace)
306 call xc_write_fxc_info(sys%ks%xc, namespace=sys%namespace)
307 call xc_sic_write_info(sys%ks%sic, namespace=sys%namespace)
308 call messages_print_with_emphasis(namespace=sys%namespace)
309
310
311 call gs_restart%init(sys%namespace, restart_gs, restart_type_load, sys%mc, ierr, mesh=sys%gr, exact=.true.)
312 if (ierr == 0) then
313 call states_elec_look_and_load(gs_restart, sys%namespace, sys%space, sys%st, sys%gr, sys%kpoints, fixed_occ=.true.)
314 call gs_restart%end()
315 else
316 message(1) = "Previous gs calculation is required."
317 call messages_fatal(1, namespace=sys%namespace)
318 end if
319
320 cas%el_per_state = sys%st%smear%el_per_state
321 cas%nst = sys%st%nst
322 cas%nik = sys%st%nik
323 cas%space_dim = sys%space%dim
324 safe_allocate(cas%n_occ(1:sys%st%nik))
325 safe_allocate(cas%n_unocc(1:sys%st%nik))
326
327 call casida_count_pairs(sys%st, sys%namespace, cas%n_pairs, cas%n_occ, cas%n_unocc, cas%is_included, is_frac_occ)
328 select case (sys%st%d%ispin)
329 case (unpolarized, spinors)
330 write(message(1),'(a,i4,a)') "Info: Found", cas%n_occ(1), " occupied states."
331 write(message(2),'(a,i4,a)') "Info: Found", cas%n_unocc(1), " unoccupied states."
332 call messages_info(2, namespace=sys%namespace)
333 case (spin_polarized)
334 write(message(1),'(a,i4,a)') "Info: Found", cas%n_occ(1), " occupied states with spin up."
335 write(message(2),'(a,i4,a)') "Info: Found", cas%n_unocc(1), " unoccupied states with spin up."
336 write(message(3),'(a,i4,a)') "Info: Found", cas%n_occ(2), " occupied states with spin down."
337 write(message(4),'(a,i4,a)') "Info: Found", cas%n_unocc(2), " unoccupied states with spin down."
338 call messages_info(4, namespace=sys%namespace)
339 end select
340
341
342 ! setup Hamiltonian, without recalculating eigenvalues (use the ones from the restart information)
343 message(1) = 'Info: Setting up Hamiltonian.'
344 call messages_info(1, namespace=sys%namespace)
345 call v_ks_h_setup(sys%namespace, sys%space, sys%gr, sys%ions, sys%ext_partners, sys%st, sys%ks, &
346 sys%hm, calc_eigenval=.false.)
347
348 !%Variable CasidaTheoryLevel
349 !%Type flag
350 !%Section Linear Response::Casida
351 !%Default <tt>eps_diff + petersilka + lrtddft_casida</tt>
352 !%Description
353 !% Choose which electron-hole matrix-based theory levels to use in calculating excitation energies.
354 !% More than one may be used to take advantage of the significant commonality between the calculations.
355 !% <tt>variational</tt> and <tt>lrttdft_casida</tt> are not usable with complex wavefunctions.
356 !% Note the restart data saved by each theory level is compatible with all the others.
357 !%Option eps_diff 1
358 !% Difference of eigenvalues, <i>i.e.</i> independent-particle approximation.
359 !%Option petersilka 2
360 !% The Petersilka approximation uses only elements of the Tamm-Dancoff matrix between degenerate
361 !% transitions (if no degeneracy, this is just the diagonal elements). Also called the "single-pole" approximation.
362 !% This is acceptable if there is little mixing between single-particle transitions.
363 !% Ref: M Petersilka, UJ Gossmann, and EKU Gross, <i>Phys. Rev. Lett.</i> <b>76</b>, 1212 (1996);
364 !% T Grabo, M Petersilka,and EKU Gross, <i>Theochem</i> <b>501-502</b> 353 (2000).
365 !%Option tamm_dancoff 4
366 !% The Tamm-Dancoff approximation uses only occupied-unoccupied transitions and not
367 !% unoccupied-occupied transitions.
368 !% Ref: S Hirata and M Head-Gordon, <i>Chem. Phys. Lett.</i> <b>314</b>, 291 (1999).
369 !%Option variational 8
370 !% Second-order constrained variational theory CV(2)-DFT. Only applies to real wavefunctions.
371 !% Ref: T Ziegler, M Seth, M Krykunov, J Autschbach, and F Wang,
372 !% <i>J. Chem. Phys.</i> <b>130</b>, 154102 (2009).
373 !%Option lrtddft_casida 16
374 !% The full Casida method. Only applies to real wavefunctions.
375 !% Ref: C Jamorski, ME Casida, and DR Salahub, <i>J. Chem. Phys.</i> <b>104</b>, 5134 (1996)
376 !% and ME Casida, "Time-dependent density functional response theory for molecules,"
377 !% in <i>Recent Advances in Density Functional Methods</i>, edited by DE Chong, vol. 1
378 !% of <i>Recent Advances in Computational Chemistry</i>, pp. 155-192 (World Scientific,
379 !% Singapore, 1995).
380 !%End
381
382 call parse_variable(sys%namespace, 'CasidaTheoryLevel', casida_eps_diff + casida_petersilka + casida_casida, theorylevel)
383
384 if (states_are_complex(sys%st)) then
385 if ((bitand(theorylevel, casida_variational) /= 0 &
386 .or. bitand(theorylevel, casida_casida) /= 0)) then
387 message(1) = "Variational and full Casida theory levels do not apply to complex wavefunctions."
388 call messages_fatal(1, only_root_writes = .true., namespace=sys%namespace)
389 ! see section II.D of CV(2) paper regarding this assumption. Would be Eq. 30 with complex wfns.
390 end if
391 end if
392
393 ! This variable is documented in xc_oep_init.
394 call parse_variable(sys%namespace, 'EnablePhotons', .false., cas%has_photons)
395 cas%pt_nmodes = 0
396 if (cas%has_photons) then
397 call messages_experimental('EnablePhotons = yes', namespace=sys%namespace)
398 cas%photon_modes => sys%photons%modes
399 call photon_mode_set_n_electrons(cas%photon_modes, sys%st%qtot)
400 write(message(1), '(a,i7,a)') 'INFO: Solving Casida equation with ', &
401 cas%photon_modes%nmodes, ' photon modes.'
402 write(message(2), '(a)') 'as described in ACS Photonics 2019, 6, 11, 2757-2778.'
403 call messages_info(2, namespace=sys%namespace)
404 cas%pt_nmodes = cas%photon_modes%nmodes
405 end if
406
407 !%Variable CasidaTransitionDensities
408 !%Type string
409 !%Section Linear Response::Casida
410 !%Default write none
411 !%Description
412 !% Specifies which transition densities are to be calculated and written down. The
413 !% transition density for the many-body state <i>n</i> will be written to a file called
414 !% <tt>rho_0n</tt> prefixed by the theory level. Format is set by <tt>OutputFormat</tt>.
415 !%
416 !% This variable is a string in list form, <i>i.e.</i> expressions such as "1,2-5,8-15" are
417 !% valid.
418 !%End
419 call parse_variable(sys%namespace, 'CasidaTransitionDensities', "0", cas%trandens)
420
421 if (cas%trandens /= "0") then
422 call io_function_read_what_how_when(sys%namespace, sys%space, sys%outp%what,&
423 sys%outp%how, sys%outp%output_interval)
424 end if
425
426 !%Variable CasidaMomentumTransfer
427 !%Type block
428 !%Section Linear Response::Casida
429 !%Default 0
430 !%Description
431 !% Momentum-transfer vector for the calculation of the dynamic structure
432 !% factor. When this variable is set, the transition rates are determined
433 !% using an exponential operator instead of the normal dipole one.
434 !%End
435
436 safe_allocate(cas%qvector(1:cas%space_dim))
437 if (parse_block(sys%namespace, 'CasidaMomentumTransfer', blk) == 0) then
438 do idir = 1, cas%space_dim
439 call parse_block_float(blk, 0, idir - 1, cas%qvector(idir))
440 end do
441 call parse_block_end(blk)
442 if (sys%hm%ep%requires_generalized_overlap) then
443 ! The exp(i q.r) transition rates would need the complex augmentation moment
444 ! int exp(i q.r) Q_ij(r) dr, which is not implemented yet (only the real dipole
445 ! moment int x_alpha Q_ij is). Block the momentum-transfer path for ultrasoft/PAW.
447 "Casida momentum transfer with ultrasoft/PAW", namespace=sys%namespace)
448 end if
449 call messages_experimental("IXS/EELS transition rate calculation", namespace=sys%namespace)
450 message(1) = "Info: Calculating IXS/EELS transition rates."
451 call messages_info(1, namespace=sys%namespace)
452 cas%qcalc = .true.
453
454 !%Variable CasidaQuadratureOrder
455 !%Type integer
456 !%Section Linear Response::Casida
457 !%Default 5
458 !%Description
459 !% Only applies if <tt>CasidaMomentumTransfer</tt> is nonzero.
460 !% Directionally averaged dynamic structure factor is calculated by
461 !% averaging over the results from a set of <math>\vec{q}</math>-vectors. The vectors
462 !% are generated using Gauss-Legendre quadrature scheme [see <i>e.g.</i>
463 !% K. Atkinson, <i>J. Austral. Math. Soc.</i> <b>23</b>, 332 (1982)], and this
464 !% variable determines the order of the scheme.
465 !%End
466 call parse_variable(sys%namespace, 'CasidaQuadratureOrder', 5, cas%avg_order)
467 else
468 cas%qvector(:) = m_zero
469 cas%qcalc = .false.
470 end if
471
472 !%Variable CasidaCalcTriplet
473 !%Type logical
474 !%Section Linear Response::Casida
475 !%Default false
476 !%Description
477 !% For a non-spin-polarized ground state, singlet or triplet excitations can be calculated
478 !% using different matrix elements. Default is to calculate singlets. This variable has no
479 !% effect for a spin-polarized calculation.
480 !%End
481 if (sys%st%d%ispin == unpolarized) then
482 call parse_variable(sys%namespace, 'CasidaCalcTriplet', .false., cas%triplet)
483 else
484 cas%triplet = .false.
485 end if
486
487 if (cas%triplet) then
488 message(1) = "Info: Using triplet kernel. Oscillator strengths will be for spin magnetic-dipole field."
489 call messages_info(1, namespace=sys%namespace)
490 end if
491
492 !%Variable CasidaHermitianConjugate
493 !%Type logical
494 !%Section Linear Response::Casida
495 !%Default false
496 !%Description
497 !% The Casida matrix is Hermitian, so it should not matter whether we calculate the upper or
498 !% lower diagonal. Numerical issues may cause small differences however. Use this variable to
499 !% calculate the Hermitian conjugate of the usual matrix, for testing.
500 !%End
501 call parse_variable(sys%namespace, 'CasidaHermitianConjugate', .false., cas%herm_conj)
502
503 !%Variable CasidaDistributedMatrix
504 !%Type logical
505 !%Section Linear Response::Casida
506 !%Default false
507 !%Description
508 !% Large matrices with more than a few thousand rows and columns usually do
509 !% not fit into the memory of one processor anymore. With this option, the
510 !% Casida matrix is distributed in block-cyclic fashion over all cores in the
511 !% ParOther group. The diagonalization is done in parallel using ScaLAPACK
512 !% or ELPA, if available. For very large matrices (>100000), only the
513 !% ParOther strategy should be used because the diagonalization dominates
514 !% the run time of the computation.
515 !%End
516 call parse_variable(sys%namespace, 'CasidaDistributedMatrix', .false., cas%distributed_matrix)
517#ifndef HAVE_SCALAPACK
518 if (cas%distributed_matrix) then
519 message(1) = "ScaLAPACK layout requested, but code not compiled with ScaLAPACK"
520 call messages_fatal(1, namespace=sys%namespace)
521 end if
522#endif
523 call messages_obsolete_variable(sys%namespace, 'CasidaUseScalapackLayout', 'CasidaDistributedMatrix')
524
525 !%Variable CasidaWriteDistributedMatrix
526 !%Type logical
527 !%Section Linear Response::Casida
528 !%Default false
529 !%Description
530 !% Set to true to write out the full distributed Casida matrix to a file
531 !% using MPI-IO.
532 !%End
533 call parse_variable(sys%namespace, 'CasidaWriteDistributedMatrix', .false., cas%write_matrix)
534 if (.not. cas%distributed_matrix .and. cas%write_matrix) then
535 message(1) = "CasidaWriteDistributedMatrix con only be used with CasidaDistributedMatrix"
536 call messages_fatal(1, namespace=sys%namespace)
537 end if
538
539 !%Variable CasidaParallelEigensolver
540 !%Type integer
541 !%Section Linear Response::Casida
542 !%Description
543 !% Choose library to use for solving the parallel eigenproblem
544 !% of the Casida problem. This options is only relevant if a
545 !% distributed matrix is used (CasidaDistributedMatrix=true).
546 !% By default, elpa is chosen if available.
547 !%Option casida_elpa 1
548 !% Use ELPA library as parallel eigensolver
549 !%Option casida_scalapack 2
550 !% Use Scalapack as parallel eigensolver
551 !%End
552#ifdef HAVE_ELPA
553 default_int = solver_elpa
554#else
555 default_int = solver_scalapack
556#endif
557 call parse_variable(sys%namespace, 'CasidaParallelEigensolver', default_int, cas%parallel_solver)
558 if (.not. varinfo_valid_option('CasidaParallelEigensolver', cas%parallel_solver)) then
559 call messages_input_error(sys%namespace, 'CasidaParallelEigensolver')
560 end if
561#ifndef HAVE_ELPA
562 if (cas%distributed_matrix .and. cas%parallel_solver == solver_elpa) then
563 message(1) = "ELPA solver requested, but code not compiled with ELPA"
564 call messages_fatal(1, namespace=sys%namespace)
565 end if
566#endif
567
568 !%Variable CasidaPrintExcitations
569 !%Type string
570 !%Section Linear Response::Casida
571 !%Default write all
572 !%Description
573 !% Specifies which excitations are written at the end of the calculation.
574 !%
575 !% This variable is a string in list form, <i>i.e.</i> expressions such as "1,2-5,8-15" are
576 !% valid.
577 !%End
578 call parse_variable(sys%namespace, 'CasidaPrintExcitations', "all", cas%print_exst)
579 if (cas%distributed_matrix) then
580 ! do not print excited states -> too many files generated!
581 cas%print_exst = "none"
582 message(1) = "Using ScaLAPACK layout, thus disabling output of excited states."
583 message(2) = "This options creates too many files for large Casida matrices."
584 call messages_info(2, namespace=sys%namespace)
585 end if
586
587 !%Variable CasidaWeightThreshold
588 !%Type float
589 !%Section Linear Response::Casida
590 !%Default -1.
591 !%Description
592 !% Specifies the threshold value for which the individual excitations are printed.
593 !% i.e. juste-h pairs with weight larger than this threshold will be printed.
594 !%
595 !% If a negative value (default) is set, all coefficients will be printed.
596 !% For many case, a 0.01 value is a valid option.
597 !%End
598 call parse_variable(sys%namespace, 'CasidaWeightThreshold', -m_one, cas%weight_thresh)
599 if (cas%weight_thresh > m_one) then
600 message(1) = 'Casida coefficients have values between 0 and 1'
601 message(2) = 'Threshold values reset to default value'
602 call messages_warning(2, namespace=sys%namespace)
603 cas%weight_thresh = -m_one
604 end if
605
606 !%Variable CasidaCalcForces
607 !%Type logical
608 !%Section Linear Response::Casida
609 !%Default false
610 !%Description
611 !% (Experimental) Enable calculation of excited-state forces. Requires previous <tt>vib_modes</tt> calculation.
612 !%End
613 call parse_variable(sys%namespace, 'CasidaCalcForces', .false., cas%calc_forces)
614 if (cas%calc_forces .and. sys%hm%ep%requires_generalized_overlap) then
616 "Casida excited-state forces with ultrasoft/PAW", namespace=sys%namespace)
617 end if
618 if (cas%calc_forces) then
619 call messages_experimental("Excited-state forces calculation", namespace=sys%namespace)
620 if (sys%hm%theory_level == hartree_fock) then
621 call messages_not_implemented("Excited-state forces with Hartree-Fock", namespace=sys%namespace)
622 end if
623
624 !%Variable CasidaCalcForcesKernel
625 !%Type logical
626 !%Section Linear Response::Casida
627 !%Default true
628 !%Description
629 !% If false, the derivative of the kernel will not be included in the excited-state force calculation.
630 !%End
631 call parse_variable(sys%namespace, 'CasidaCalcForcesKernel', .true., cas%calc_forces_kernel)
632
633 !%Variable CasidaCalcForcesSCF
634 !%Type logical
635 !%Section Linear Response::Casida
636 !%Default false
637 !%Description
638 !% If true, the ground-state forces will be included in the excited-state forces, so they are total forces.
639 !% If false, the excited-state forces that are produced are only the gradients of the excitation energy.
640 !%End
641 call parse_variable(sys%namespace, 'CasidaCalcForcesSCF', .false., cas%calc_forces_scf)
642
643 if (cas%distributed_matrix) then
644 message(1) = "Info: Forces calculation not compatible with ScaLAPACK layout."
645 message(2) = "Using normal layout."
646 call messages_info(2, namespace=sys%namespace)
647 cas%distributed_matrix = .false.
648 end if
649 end if
650
651
652 ! Initialize structure
653 call casida_type_init(cas, sys)
654
655 cas%fromScratch = fromscratch
656
657 if (cas%fromScratch) then ! remove old restart files
658 if (cas%triplet) then
659 call cas%restart_dump%rm('kernel_triplet')
660 else
661 call cas%restart_dump%rm('kernel')
662 end if
663
664 if (cas%calc_forces) then
665 do iatom = 1, sys%ions%natoms
666 do idir = 1, cas%space_dim
667 write(restart_filename,'(a,i6.6,a,i1)') 'lr_kernel_', iatom, '_', idir
668 if (cas%triplet) restart_filename = trim(restart_filename)//'_triplet'
669 call cas%restart_dump%rm(restart_filename)
670
671 write(restart_filename,'(a,i6.6,a,i1)') 'lr_hmat1_', iatom, '_', idir
672 call cas%restart_dump%rm(restart_filename)
673 end do
674 end do
675 end if
676 end if
677
678 ! First, print the differences between KS eigenvalues (first approximation to the excitation energies).
679 if (bitand(theorylevel, casida_eps_diff) /= 0) then
680 message(1) = "Info: Approximating resonance energies through KS eigenvalue differences"
681 call messages_info(1, namespace=sys%namespace)
682 cas%type = casida_eps_diff
683 call casida_work(sys, cas)
684 end if
685
686 if (sys%st%d%ispin /= spinors) then
687
688 if (sys%hm%theory_level == hartree_fock .and. cas%distributed_matrix) then
689 call messages_not_implemented("Hartree-Fock Casida with distributed matrix (ScaLAPACK)", &
690 namespace=sys%namespace)
691 end if
692
693 if (bitand(theorylevel, casida_tamm_dancoff) /= 0) then
694 call messages_experimental("Tamm-Dancoff calculation", namespace=sys%namespace)
695 message(1) = "Info: Calculating matrix elements in the Tamm-Dancoff approximation"
696 call messages_info(1, namespace=sys%namespace)
697 cas%type = casida_tamm_dancoff
698 call casida_work(sys, cas)
699 end if
700
701 if (bitand(theorylevel, casida_variational) /= 0) then
702 if (sys%hm%theory_level == hartree_fock) then
703 call messages_not_implemented("CV(2)-DFT level with Hartree-Fock", namespace=sys%namespace)
704 else
705 call messages_experimental("CV(2)-DFT calculation", namespace=sys%namespace)
706 message(1) = "Info: Calculating matrix elements with the CV(2)-DFT theory"
707 call messages_info(1, namespace=sys%namespace)
708 cas%type = casida_variational
709 call casida_work(sys, cas)
710 end if
711 end if
712
713 if (bitand(theorylevel, casida_casida) /= 0) then
714 message(1) = "Info: Calculating matrix elements with the full Casida method"
715 call messages_info(1, namespace=sys%namespace)
716 cas%type = casida_casida
717 call casida_work(sys, cas)
718 end if
719
720 ! Doing this first, if doing the others later, takes longer, because we would use
721 ! each Poisson solution for only one matrix element instead of a whole column.
722 if (bitand(theorylevel, casida_petersilka) /= 0) then
723 if (sys%hm%theory_level == hartree_fock) then
724 call messages_not_implemented("Petersilka level with Hartree-Fock", namespace=sys%namespace)
725 else
726 message(1) = "Info: Calculating resonance energies via the Petersilka approximation"
727 call messages_info(1, namespace=sys%namespace)
728 cas%type = casida_petersilka
729 call casida_work(sys, cas)
730 end if
731 end if
732
733 end if
734
735 call casida_type_end(cas)
736
737 call profiling_out('CASIDA')
738 pop_sub(casida_run_legacy)
739 end subroutine casida_run_legacy
740
741 ! ---------------------------------------------------------
743 !
744 subroutine casida_count_pairs(st, namespace, n_pairs, n_occ, n_unocc, is_included, is_frac_occ)
745 type(states_elec_t), intent(in) :: st
746 type(namespace_t), intent(in) :: namespace
747 integer, intent(out) :: n_pairs
748 integer, contiguous, intent(out) :: n_occ(:)
749 integer, contiguous, intent(out) :: n_unocc(:)
750 logical, allocatable, intent(out) :: is_included(:,:,:)
752 logical, intent(out) :: is_frac_occ
753
754 integer :: ik, ist, ast, n_filled, n_partially_filled, n_half_filled
755 character(len=80) :: nst_string, default, wfn_list
756 real(real64) :: energy_window
757 real(real64) :: fdiff, delta_e
758 real(real64) :: fdiff_tol
759 real(real64) :: delta_e_tol
760
761 push_sub(casida_count_pairs)
762
763 is_frac_occ = .false.
764 do ik = 1, st%nik
765 call occupied_states(st, namespace, ik, n_filled, n_partially_filled, n_half_filled)
766 if (n_partially_filled > 0 .or. n_half_filled > 0) is_frac_occ = .true.
767 n_occ(ik) = n_filled + n_partially_filled + n_half_filled
768 n_unocc(ik) = st%nst - n_filled
769 ! We implemented fractional occupations, partially occupied levels need to be counted as both occ and unocc.
770 end do
771
772 !%Variable CasidaKSEnergyWindow
773 !%Type float
774 !%Section Linear Response::Casida
775 !%Description
776 !% An alternative to <tt>CasidaKohnShamStates</tt> for specifying which occupied-unoccupied
777 !% transitions will be used: all those whose eigenvalue differences are less than this
778 !% number will be included. If a value less than 0 is supplied, this criterion will not be used.
779 !%End
780
781 call parse_variable(namespace, 'CasidaKSEnergyWindow', -m_one, energy_window, units_inp%energy)
782
783 !%Variable CasidaThresholdOccupation
784 !%Type float
785 !%Default 1e-12
786 !%Section Linear Response::Casida
787 !%Description
788 !%Filters occupation numbers difference depending on user input.
789 !%If undefined, jump into the default value.
790 !%End
791 call parse_variable(namespace, 'CasidaThresholdOccupation', 1.0e-12_real64, fdiff_tol)
792
793 !%Variable CasidaThresholdEnergy
794 !%Type float
795 !%Default 1e-12
796 !%Section Linear Response::Casida
797 !%Description
798 !%Filters energy difference between the transitions.
799 !%If undefined, jump into the default value.
800 !%End
801 call parse_variable(namespace, 'CasidaThresholdEnergy', 1.0e-12_real64, delta_e_tol, units_inp%energy)
802
803 !%Variable CasidaKohnShamStates
804 !%Type string
805 !%Section Linear Response::Casida
806 !%Default all states
807 !%Description
808 !% The calculation of the excitation spectrum of a system in the Casida frequency-domain
809 !% formulation of linear-response time-dependent density functional theory (TDDFT)
810 !% implies the use of a basis set of occupied/unoccupied Kohn-Sham orbitals. This
811 !% basis set should, in principle, include all pairs formed by all occupied states,
812 !% and an infinite number of unoccupied states. In practice, one has to truncate this
813 !% basis set, selecting a number of occupied and unoccupied states that will form the
814 !% pairs. These states are specified with this variable. If there are, say, 15 occupied
815 !% states, and one sets this variable to the value "10-18", this means that occupied
816 !% states from 10 to 15, and unoccupied states from 16 to 18 will be considered.
817 !%
818 !% This variable is a string in list form, <i>i.e.</i> expressions such as "1,2-5,8-15" are
819 !% valid. You should include a non-zero number of unoccupied states and a non-zero number
820 !% of occupied states.
821 !%End
822
823 n_pairs = 0
824 safe_allocate(is_included(st%nst, st%nst , st%nik))
825 is_included(:,:,:) = .false.
826
827 if (energy_window < m_zero) then
828 write(nst_string,'(i6)') st%nst
829 write(default,'(a,a)') "1-", trim(adjustl(nst_string))
830 call parse_variable(namespace, 'CasidaKohnShamStates', default, wfn_list)
831
832 write(message(1),'(a,a)') "Info: States that form the basis: ", trim(wfn_list)
833 call messages_info(1, namespace=namespace)
834
835 ! count pairs
836 n_pairs = 0
837 do ik = 1, st%nik
838 do ast = 1, st%nst
839 if (.not. loct_isinstringlist(ast, wfn_list)) cycle
840 do ist = 1, st%nst
841 if (.not. loct_isinstringlist(ist, wfn_list)) cycle
842 fdiff = st%occ(ist,ik) - st%occ(ast,ik)
843 if (fdiff <= fdiff_tol) cycle
844 n_pairs = n_pairs + 1
845 is_included(ist, ast, ik) = .true.
846 end do
847 end do
848 end do
849
850 else ! using CasidaKSEnergyWindow
851
852 write(message(1),'(a,f12.6,a)') "Info: including transitions with energy < ", &
853 units_from_atomic(units_out%energy, energy_window), trim(units_abbrev(units_out%energy))
854 call messages_info(1, namespace=namespace)
855
856 ! count pairs
857 n_pairs = 0
858 do ik = 1, st%nik
859 do ast = 1, st%nst
860 do ist = 1, st%nst
861 fdiff = st%occ(ist,ik) - st%occ(ast,ik)
862 if (fdiff <= fdiff_tol) cycle
863 delta_e = st%eigenval(ast, ik) - st%eigenval(ist, ik)
864 if (delta_e <= delta_e_tol) cycle
865 if (delta_e >= energy_window) cycle
866 n_pairs = n_pairs + 1
867 is_included(ist, ast, ik) = .true.
868 end do
869 end do
870 end do
871
872 end if
873
874 pop_sub(casida_count_pairs)
875 end subroutine casida_count_pairs
876
877 ! ---------------------------------------------------------
879 subroutine casida_type_init(cas, sys)
880 type(casida_t), intent(inout) :: cas
881 type(electrons_t), intent(in) :: sys
882
883 integer :: ist, ast, jpair, ik, ierr
884#ifdef HAVE_SCALAPACK
885 integer :: np, np_rows, np_cols, ii, info
886
887#endif
888
889 push_sub(casida_type_init)
890
891 cas%kernel_lrc_alpha = sys%ks%xc%lrc%alpha
892 cas%states_are_real = states_are_real(sys%st)
893 if (cas%distributed_matrix .and. .not. cas%states_are_real) then
894 call messages_not_implemented("Complex wavefunctions with ScaLAPACK layout", namespace=sys%namespace)
895 end if
896
897 write(message(1), '(a,i9)') "Number of occupied-unoccupied pairs: ", cas%n_pairs
898 call messages_info(1, namespace=sys%namespace)
899
900 if (cas%n_pairs < 1) then
901 message(1) = "No Casida pairs -- maybe there are no unoccupied states?"
902 call messages_fatal(1, only_root_writes = .true., namespace=sys%namespace)
903 end if
904
905 if (mpi_world%is_root()) write(*, "(1x)")
906
907 ! now let us take care of initializing the parallel stuff
908 cas%parallel_in_eh_pairs = multicomm_strategy_is_parallel(sys%mc, p_strategy_other)
909 if (cas%parallel_in_eh_pairs) then
910 call mpi_grp_init(cas%mpi_grp, sys%mc%group_comm(p_strategy_other))
911 else
912 call mpi_grp_init(cas%mpi_grp, mpi_comm_undefined)
913 end if
914 cas%parallel_in_domains = multicomm_strategy_is_parallel(sys%mc, p_strategy_domains)
915
916 if (cas%distributed_matrix .and. .not. cas%parallel_in_eh_pairs) then
917 message(1) = "ScaLAPACK layout requested, but 'Other' parallelization strategy not available."
918 message(2) = "Please set ParOther to use the ScaLAPACK layout."
919 message(3) = "Continuing without ScaLAPACK layout."
920 call messages_info(3, namespace=sys%namespace)
921 cas%distributed_matrix = .false.
922 end if
923
924 ! dimension of matrix
925 cas%n = cas%n_pairs + cas%pt_nmodes
926
927 ! initialize block-cyclic matrix
928 if (cas%distributed_matrix) then
929#ifdef HAVE_SCALAPACK
930 ! processor layout: always use more processors for rows, this leads to
931 ! better load balancing when computing the matrix elements
932 np = cas%mpi_grp%size
933 np_cols = 1
934 if (np > 3) then
935 do ii = floor(sqrt(real(np))), 2, -1
936 if (mod(np, ii) == 0) then
937 np_cols = ii
938 exit
939 end if
940 end do
941 end if
942 np_rows = np / np_cols
943
944 ! recommended block size: 64, take smaller value for smaller matrices for
945 ! better load balancing
946 cas%block_size = min(64, cas%n / np_rows)
947 ! limit to a minimum block size of 5 for diagonalization efficiency
948 cas%block_size = max(5, cas%block_size)
949 write(message(1), '(A,I5,A,I5,A,I5,A)') 'Parallel layout: using block size of ',&
950 cas%block_size, ' and a processor grid with ', np_rows, 'x', np_cols, &
951 ' processors (rows x cols)'
952 call messages_info(1, namespace=sys%namespace)
953
954 call blacs_proc_grid_init(cas%proc_grid, cas%mpi_grp, procdim = (/np_rows, np_cols/))
955
956 ! get size of local matrices
957 cas%nb_rows = numroc(cas%n, cas%block_size, cas%proc_grid%myrow, 0, cas%proc_grid%nprow)
958 cas%nb_cols = numroc(cas%n, cas%block_size, cas%proc_grid%mycol, 0, cas%proc_grid%npcol)
959
960 ! get ScaLAPACK descriptor
961 call descinit(cas%desc(1), cas%n, cas%n, cas%block_size, cas%block_size, 0, 0, &
962 cas%proc_grid%context, cas%nb_rows, info)
963#endif
964 else
965 ! set to full size
966 cas%nb_rows = cas%n
967 cas%nb_cols = cas%n
968 end if
969
970 ! allocate stuff
971 safe_allocate(cas%pair(1:cas%n))
972 if (cas%states_are_real) then
973 safe_allocate( cas%dmat(1:cas%nb_rows, 1:cas%nb_cols))
974 safe_allocate( cas%dtm(1:cas%n, 1:cas%space_dim))
975 else
976 ! caution: ScaLAPACK layout not yet tested for complex wavefunctions!
977 safe_allocate( cas%zmat(1:cas%nb_rows, 1:cas%nb_cols))
978 safe_allocate( cas%ztm(1:cas%n, 1:cas%space_dim))
979 end if
980 safe_allocate( cas%f(1:cas%n))
981 safe_allocate( cas%s(1:cas%n_pairs))
982 safe_allocate( cas%w(1:cas%n))
983 safe_allocate( cas%index(1:cas%nst, 1:cas%nst, 1:cas%nik))
984 safe_allocate( cas%ind(1:cas%n))
985
986 if (cas%calc_forces) then
987 if (cas%states_are_real) then
988 safe_allocate(cas%dmat_save(1:cas%n_pairs, 1:cas%n_pairs))
989 else
990 safe_allocate(cas%zmat_save(1:cas%n_pairs, 1:cas%n_pairs))
991 end if
992 safe_allocate(cas%forces(1:cas%space_dim, 1:sys%ions%natoms, 1:cas%n_pairs))
993 end if
994
995 if (cas%qcalc) then
996 safe_allocate( cas%qf (1:cas%n_pairs))
997 safe_allocate( cas%qf_avg(1:cas%n_pairs))
998 end if
999
1000 cas%index(:,:,:) = 0
1001
1002 ! create pairs
1003 jpair = 1
1004 do ik = 1, cas%nik
1005 do ast = 1, cas%nst
1006 do ist = 1, cas%nst
1007 if (.not. cas%is_included(ist, ast, ik)) cycle
1008 cas%index(ist, ast, ik) = jpair
1009 cas%pair(jpair)%i = ist
1010 cas%pair(jpair)%a = ast
1011 cas%pair(jpair)%kk = ik
1012 jpair = jpair + 1
1013 end do
1014 end do
1015 end do
1016 if (jpair - 1 /= cas%n_pairs) then
1017 message(1) = "Mismatch in Casida pair counting"
1018 call messages_fatal(1, namespace=sys%namespace)
1019 end if
1020
1021 if (cas%has_photons) then
1022 ! create pairs for photon modes (negative number refers to photonic excitation)
1023 do ik = 1, cas%pt_nmodes
1024 cas%pair(cas%n_pairs + ik)%i = 1
1025 cas%pair(cas%n_pairs + ik)%a = -ik
1026 cas%pair(cas%n_pairs + ik)%kk = -ik
1027 end do
1028 end if
1029
1030 safe_deallocate_a(cas%is_included)
1031
1032 call cas%restart_dump%init(sys%namespace, restart_casida, restart_type_dump, sys%mc, ierr)
1033 call cas%restart_load%init(sys%namespace, restart_casida, restart_type_load, sys%mc, ierr)
1034
1035 pop_sub(casida_type_init)
1036 end subroutine casida_type_init
1037
1038
1039 ! ---------------------------------------------------------
1040 subroutine casida_type_end(cas)
1041 type(casida_t), intent(inout) :: cas
1042
1043 push_sub(casida_type_end)
1044
1045 assert(allocated(cas%pair))
1046 safe_deallocate_a(cas%pair)
1047 safe_deallocate_a(cas%index)
1048 if (cas%states_are_real) then
1049 safe_deallocate_a(cas%dmat)
1050 safe_deallocate_a(cas%dtm)
1051 safe_deallocate_a(cas%dmatB)
1052 else
1053 safe_deallocate_a(cas%zmat)
1054 safe_deallocate_a(cas%ztm)
1055 safe_deallocate_a(cas%zmatB)
1056 end if
1057 safe_deallocate_a(cas%s)
1058 safe_deallocate_a(cas%f)
1059 safe_deallocate_a(cas%w)
1060 safe_deallocate_a(cas%ind)
1061
1062 if (cas%qcalc) then
1063 safe_deallocate_a(cas%qf)
1064 safe_deallocate_a(cas%qf_avg)
1065 end if
1066
1067 safe_deallocate_a(cas%n_occ)
1068 safe_deallocate_a(cas%n_unocc)
1069
1070 if (cas%calc_forces) then
1071 if (cas%states_are_real) then
1072 safe_deallocate_a(cas%dmat_save)
1073 else
1074 safe_deallocate_a(cas%zmat_save)
1075 end if
1076 safe_deallocate_a(cas%forces)
1077 end if
1079 call cas%restart_dump%end()
1080 call cas%restart_load%end()
1081
1082 if (cas%distributed_matrix) then
1083#ifdef HAVE_SCALAPACK
1084 call blacs_proc_grid_end(cas%proc_grid)
1085#endif
1086 end if
1087
1088 safe_deallocate_a(cas%qvector)
1089
1090 pop_sub(casida_type_end)
1091 end subroutine casida_type_end
1092
1093
1094 ! ---------------------------------------------------------
1097 subroutine casida_work(sys, cas)
1098 type(electrons_t), target, intent(inout) :: sys
1099 type(casida_t), intent(inout) :: cas
1100
1101 type(states_elec_t), pointer :: st
1102 type(grid_t), pointer :: gr
1103
1104 real(real64), allocatable :: rho_spin(:, :)
1105 real(real64), allocatable :: fxc_spin(:,:,:), fxc_grad(:,:,:,:,:), fxc_grad_spin(:,:,:,:)
1106 character(len=100) :: restart_filename
1107
1108 push_sub(casida_work)
1109
1110 ! sanity checks
1111 assert(cas%type >= casida_eps_diff .and. cas%type <= casida_casida)
1112
1113 ! some shortcuts
1114 st => sys%st
1115 gr => sys%gr
1116
1117 ! initialize stuff
1118 if (cas%states_are_real) then
1119 cas%dmat = m_zero
1120 cas%dtm = m_zero
1121 if (allocated(cas%dmatB)) cas%dmatB = m_zero
1122 else
1123 cas%zmat = m_zero
1124 cas%ztm = m_zero
1125 if (allocated(cas%zmatB)) cas%zmatB = m_zero
1126 end if
1127 cas%f = m_zero
1128 cas%w = m_zero
1129 cas%s = m_zero
1130 if (cas%qcalc) then
1131 cas%qf = m_zero
1132 cas%qf_avg = m_zero
1133 end if
1134
1135 if (cas%type /= casida_eps_diff .or. cas%calc_forces) then
1136 ! We calculate here the kernel, since it will be needed later.
1137 safe_allocate(cas%rho(1:gr%np_part, 1:st%d%nspin))
1138 safe_allocate(cas%fxc(1:gr%np, 1:st%d%nspin, 1:st%d%nspin))
1139 cas%gga = in_family(sys%ks%xc%kernel_family, [xc_family_gga])
1140 ! Pure Hartree-Fock has no semilocal xc kernel: the coupling is Hartree
1141 ! plus the Fock exchange, both assembled directly in casida_get_matrix.
1142 if (sys%hm%theory_level == hartree_fock) cas%gga = .false.
1143 if (cas%gga) then
1144 safe_allocate(cas%fxc_grad(1:gr%np, 1:gr%der%dim, 1:gr%der%dim, 1:st%d%nspin, 1:st%d%nspin))
1145 if (st%d%ispin == spin_polarized) then
1146 safe_allocate(cas%fxc_grad_spin(1:gr%np, 1:gr%der%dim, 1:st%d%nspin, 1:st%d%nspin))
1147 else
1148 safe_allocate(cas%fxc_grad_spin(0, 0, 0, 0))
1149 end if
1150 else
1151 safe_allocate(cas%fxc_grad(0, 0, 0, 0, 0))
1152 safe_allocate(cas%fxc_grad_spin(0, 0, 0, 0))
1153 end if
1154
1155 call states_elec_total_density(st, gr, cas%rho)
1156 if (sys%hm%theory_level == hartree_fock) then
1157 ! Pure Hartree-Fock: the semilocal xc kernel vanishes.
1158 cas%fxc = m_zero
1159 else if (cas%triplet) then
1160 safe_allocate(rho_spin(1:gr%np_part, 1:2))
1161 safe_allocate(fxc_spin(1:gr%np, 1:2, 1:2))
1162
1163 rho_spin(:, 1) = m_half * cas%rho(:, 1)
1164 rho_spin(:, 2) = m_half * cas%rho(:, 1)
1165
1166 if (cas%gga) then
1167 safe_allocate(fxc_grad(1:gr%np, 1:gr%der%dim, 1:gr%der%dim, 1:2, 1:2))
1168 safe_allocate(fxc_grad_spin(1:gr%np, 1:gr%der%dim, 1:2, 1:2))
1169 call xc_get_fxc(sys%ks%xc, gr, sys%namespace, rho_spin, spin_polarized, fxc_spin, fxc_grad, fxc_grad_spin)
1170 cas%fxc_grad(:, :, :, 1, 1) = m_half * (fxc_grad(:, :, :, 1, 1) - fxc_grad(:, :, :, 1, 2))
1171 ! For the triplet case, fxc_grad_spin is zero as n_up = n_down
1172 safe_deallocate_a(fxc_grad)
1173 safe_deallocate_a(fxc_grad_spin)
1174 else
1175 call xc_get_fxc(sys%ks%xc, gr, sys%namespace, rho_spin, spin_polarized, fxc_spin)
1176 end if
1177 cas%fxc(:, 1, 1) = m_half * (fxc_spin(:, 1, 1) - fxc_spin(:, 1, 2))
1178
1179 safe_deallocate_a(rho_spin)
1180 safe_deallocate_a(fxc_spin)
1181 else
1182 if (cas%gga) then
1183 call xc_get_fxc(sys%ks%xc, gr, sys%namespace, cas%rho, st%d%ispin, cas%fxc, cas%fxc_grad, &
1184 cas%fxc_grad_spin)
1185 else
1186 call xc_get_fxc(sys%ks%xc, gr, sys%namespace, cas%rho, st%d%ispin, cas%fxc)
1187 end if
1188 end if
1189
1190 if (sys%ks%sic%level == sic_adsic) then
1191 call xc_sic_add_fxc_adsic(sys%namespace, sys%ks%xc, st, gr, cas%rho, cas%fxc, cas%fxc_grad, cas%fxc_grad_spin, cas%triplet)
1192 end if
1193
1194 end if
1195
1196 restart_filename = 'kernel'
1197 if (cas%triplet) restart_filename = trim(restart_filename)//'_triplet'
1198
1199 if (sys%hm%theory_level == hartree_fock .and. cas%type == casida_casida) then
1200 if (cas%pt_nmodes > 0) then
1201 call messages_not_implemented("Full TDHF Casida with photons", namespace=sys%namespace)
1202 end if
1203 if (cas%states_are_real) then
1204 safe_allocate(cas%dmatB(1:cas%n_pairs, 1:cas%n_pairs))
1205 else
1206 safe_allocate(cas%zmatB(1:cas%n_pairs, 1:cas%n_pairs))
1207 end if
1208 end if
1209
1210 select case (cas%type)
1211 case (casida_eps_diff)
1212 call solve_eps_diff()
1214 if (cas%states_are_real) then
1215 if (allocated(cas%dmatB)) then
1216 call dcasida_get_matrix(cas, sys%namespace, sys%hm, st, sys%ks, gr, cas%dmat, cas%fxc, &
1217 cas%fxc_grad, cas%fxc_grad_spin, restart_filename, matrixb=cas%dmatB)
1218 else
1219 call dcasida_get_matrix(cas, sys%namespace, sys%hm, st, sys%ks, gr, cas%dmat, cas%fxc, &
1220 cas%fxc_grad, cas%fxc_grad_spin, restart_filename)
1221 end if
1222 call dcasida_solve(cas, sys)
1223 else
1224 if (allocated(cas%zmatB)) then
1225 call zcasida_get_matrix(cas, sys%namespace, sys%hm, st, sys%ks, gr, cas%zmat, cas%fxc, &
1226 cas%fxc_grad, cas%fxc_grad_spin, restart_filename, matrixb=cas%zmatB)
1227 else
1228 call zcasida_get_matrix(cas, sys%namespace, sys%hm, st, sys%ks, gr, cas%zmat, cas%fxc, &
1229 cas%fxc_grad, cas%fxc_grad_spin, restart_filename)
1230 end if
1231 call zcasida_solve(cas, sys)
1232 end if
1233 end select
1234
1235 ! compute oscillator strengths on all processes for the ScaLAPACK layout
1236 if (cas%mpi_grp%is_root() .or. cas%distributed_matrix) then
1237 if (cas%states_are_real) then
1238 call doscillator_strengths(cas, gr, st, sys%hm)
1239 else
1240 call zoscillator_strengths(cas, gr, st, sys%hm)
1241 end if
1242 end if
1243
1244 if (cas%calc_forces) then
1245 if (cas%states_are_real) then
1246 call dcasida_forces(cas, sys, gr, st)
1247 else
1248 call zcasida_forces(cas, sys, gr, st)
1249 end if
1250 end if
1251
1252 if (cas%states_are_real) then
1253 call dcasida_write(cas, sys)
1254 else
1255 call zcasida_write(cas, sys)
1256 end if
1257
1258 safe_deallocate_a(cas%dmatB)
1259 safe_deallocate_a(cas%zmatB)
1260
1261 ! clean up
1262 if (cas%type /= casida_eps_diff .or. cas%calc_forces) then
1263 safe_deallocate_a(cas%fxc)
1264 safe_deallocate_a(cas%fxc_grad)
1265 safe_deallocate_a(cas%fxc_grad_spin)
1266 safe_deallocate_a(cas%rho)
1267 end if
1268
1269 pop_sub(casida_work)
1270
1271 contains
1272
1273 ! ---------------------------------------------------------
1274 subroutine solve_eps_diff
1275
1276 integer :: ia
1277 real(real64), allocatable :: w(:)
1278
1279 push_sub(casida_work.solve_eps_diff)
1280
1281 ! initialize progress bar
1282 if (mpi_world%is_root()) call loct_progress_bar(-1, cas%n_pairs)
1283
1284 do ia = 1, cas%n_pairs
1285 cas%w(ia) = st%eigenval(cas%pair(ia)%a, cas%pair(ia)%kk) - &
1286 st%eigenval(cas%pair(ia)%i, cas%pair(ia)%kk)
1287 if (cas%w(ia) < -m_epsilon) then
1288 message(1) = "There is a negative unocc-occ KS eigenvalue difference for"
1289 write(message(2),'("states ",I5," and ",I5," of k-point ",I5,".")') cas%pair(ia)%i, cas%pair(ia)%a, cas%pair(ia)%kk
1290 message(3) = "This indicates an inconsistency between gs, unocc, and/or casida calculations."
1291 call messages_fatal(3, only_root_writes = .true., namespace=sys%namespace)
1292 end if
1293 if (mpi_world%is_root()) call loct_progress_bar(ia, cas%n_pairs)
1294 end do
1295
1296 safe_allocate(w(1:size(cas%w)))
1297 w = cas%w
1298 call sort(w, cas%ind)
1299 safe_deallocate_a(w)
1300
1301 if (mpi_world%is_root()) write(*, "(1x)")
1302
1304 end subroutine solve_eps_diff
1305
1306 end subroutine casida_work
1307
1308 ! ---------------------------------------------------------
1309 real(real64) function casida_matrix_factor(cas, sys)
1310 type(casida_t), intent(in) :: cas
1311 type(electrons_t), intent(in) :: sys
1313 push_sub(casida_matrix_factor)
1314
1315 casida_matrix_factor = m_one
1316
1317 if (cas%type == casida_variational) then
1318 casida_matrix_factor = m_two * casida_matrix_factor
1319 end if
1320
1321 if (sys%st%d%ispin == unpolarized) then
1322 casida_matrix_factor = m_two * casida_matrix_factor
1323 end if
1324
1325 pop_sub(casida_matrix_factor)
1326
1327 end function casida_matrix_factor
1328
1329 ! ---------------------------------------------------------
1330 subroutine qcasida_write(cas, namespace)
1331 type(casida_t), intent(in) :: cas
1332 type(namespace_t), intent(in) :: namespace
1333
1334 integer :: iunit, ia
1335
1336 if (.not. mpi_world%is_root()) return
1337
1338 push_sub(qcasida_write)
1339
1340 call io_mkdir(casida_dir, namespace)
1341 iunit = io_open(casida_dir//'q'//trim(theory_name(cas)), namespace, action='write')
1342 write(iunit, '(a1,a14,1x,a24,1x,a24,1x,a10,3es15.8,a2)') '#','E' , '|<f|exp(iq.r)|i>|^2', &
1343 '<|<f|exp(iq.r)|i>|^2>','; q = (',cas%qvector(1:cas%space_dim),')'
1344 write(iunit, '(a1,a14,1x,a24,1x,a24,1x,10x,a15)') '#', trim(units_abbrev(units_out%energy)), &
1345 trim('-'), &
1346 trim('-'), &
1347 trim('a.u.')
1348
1349 if (cas%avg_order == 0) then
1350 do ia = 1, cas%n_pairs
1351 write(iunit, '(es15.8,es15.8)') units_from_atomic(units_out%energy, cas%w(cas%ind(ia))), cas%qf(cas%ind(ia))
1352 end do
1353 else
1354 do ia = 1, cas%n_pairs
1355 write(iunit, '(3es15.8)') units_from_atomic(units_out%energy, cas%w(cas%ind(ia))), &
1356 cas%qf (cas%ind(ia)), &
1357 cas%qf_avg(cas%ind(ia))
1358 end do
1359 end if
1360
1361 call io_close(iunit)
1362
1363 pop_sub(qcasida_write)
1364
1365 end subroutine qcasida_write
1366
1367 ! ---------------------------------------------------------
1368 character(len=80) pure function theory_name(cas)
1369 type(casida_t), intent(in) :: cas
1370
1371 select case (cas%type)
1372 case (casida_eps_diff)
1373 theory_name = "eps_diff"
1374 case (casida_petersilka)
1375 theory_name = "petersilka"
1376 case (casida_tamm_dancoff)
1377 theory_name = "tamm_dancoff"
1378 case (casida_variational)
1379 theory_name = "variational"
1380 case (casida_casida)
1381 theory_name = "casida"
1382 case default
1383 theory_name = "unknown"
1384 end select
1385
1386 end function theory_name
1387
1388 logical function isnt_degenerate(cas, st, ia, jb)
1389 type(casida_t), intent(in) :: cas
1390 type(states_elec_t), intent(in) :: st
1391 integer, intent(in) :: ia
1392 integer, intent(in) :: jb
1393
1394 push_sub(isnt_degenerate)
1395
1396 isnt_degenerate = (abs((st%eigenval(cas%pair(ia)%a, cas%pair(ia)%kk) - st%eigenval(cas%pair(ia)%i, cas%pair(ia)%kk)) &
1397 - (st%eigenval(cas%pair(jb)%a, cas%pair(jb)%kk) - st%eigenval(cas%pair(jb)%i, cas%pair(jb)%kk))) > 1e-8_real64)
1398
1399 pop_sub(isnt_degenerate)
1400 end function isnt_degenerate
1401
1402 integer function get_global_row(cas, jb_local) result(jb)
1403 implicit none
1404 type(casida_t), intent(inout) :: cas
1405 integer, intent(in) :: jb_local
1407 if (.not. cas%distributed_matrix) then
1408 jb = jb_local
1409 else
1410#ifdef HAVE_SCALAPACK
1411 jb = indxl2g(jb_local, cas%block_size, cas%proc_grid%myrow, 0, cas%proc_grid%nprow)
1412#endif
1413 end if
1414 end function get_global_row
1415
1416 integer function get_global_col(cas, ia_local) result(ia)
1417 implicit none
1418 type(casida_t), intent(inout) :: cas
1419 integer, intent(in) :: ia_local
1420
1421 if (.not. cas%distributed_matrix) then
1422 ia = ia_local
1423 else
1424#ifdef HAVE_SCALAPACK
1425 ia = indxl2g(ia_local, cas%block_size, cas%proc_grid%mycol, 0, cas%proc_grid%npcol)
1426#endif
1427 end if
1428 end function get_global_col
1429
1430 subroutine local_indices(cas, ia, jb, on_this_processor, ia_local, jb_local)
1431 implicit none
1432 type(casida_t), intent(in) :: cas
1433 integer, intent(in) :: ia, jb
1434 logical, intent(out) :: on_this_processor
1435 integer, intent(out) :: ia_local, jb_local
1436#ifdef HAVE_SCALAPACK
1437 integer :: ia_proc, jb_proc
1438#endif
1439
1440 if (.not. cas%distributed_matrix) then
1441 on_this_processor = .true.
1442 ia_local = ia
1443 jb_local = jb
1444 else
1445#ifdef HAVE_SCALAPACK
1446 ia_proc = indxg2p(ia, cas%block_size, cas%proc_grid%mycol, 0, cas%proc_grid%npcol)
1447 jb_proc = indxg2p(jb, cas%block_size, cas%proc_grid%myrow, 0, cas%proc_grid%nprow)
1448 if (cas%proc_grid%mycol == ia_proc .and. cas%proc_grid%myrow == jb_proc) then
1449 on_this_processor = .true.
1450 ia_local = indxg2l(ia, cas%block_size, cas%proc_grid%mycol, 0, cas%proc_grid%npcol)
1451 jb_local = indxg2l(jb, cas%block_size, cas%proc_grid%myrow, 0, cas%proc_grid%nprow)
1452 else
1453 on_this_processor = .false.
1454 ia_local = -1
1455 jb_local = -1
1456 end if
1457#endif
1458 end if
1459 end subroutine local_indices
1460
1461#include "undef.F90"
1462#include "real.F90"
1463#include "casida_inc.F90"
1464#include "undef.F90"
1465#include "complex.F90"
1466#include "casida_inc.F90"
1467
1469
1470!! Local Variables:
1471!! mode: f90
1472!! coding: utf-8
1473!! End:
subroutine solve_eps_diff
Definition: casida.F90:1313
This is the common interface to a sorting routine. It performs the shell algorithm,...
Definition: sort.F90:133
double floor(double __x) __attribute__((__nothrow__
This module implements batches of mesh functions.
Definition: batch.F90:112
This module provides the BLACS processor grid.
subroutine, public blacs_proc_grid_init(this, mpi_grp, procdim)
Initializes a blacs context from an MPI communicator with topological information.
subroutine, public blacs_proc_grid_end(this)
This module handles the calculation mode.
integer, parameter, public p_strategy_other
something else like e-h pairs
integer, parameter, public p_strategy_domains
parallelization in domains
This module implements the Casida equations for excited states.
Definition: casida.F90:117
integer function get_global_col(cas, ia_local)
Definition: casida.F90:1455
subroutine zcasida_get_matrix(cas, namespace, hm, st, ks, gr, matrix, fxc, fxc_grad, fxc_grad_spin, restart_file, is_forces, matrixB)
Definition: casida.F90:4246
integer, parameter solver_scalapack
Definition: casida.F90:196
subroutine casida_count_pairs(st, namespace, n_pairs, n_occ, n_unocc, is_included, is_frac_occ)
number of occupied-unoccupied pairs for Casida
Definition: casida.F90:817
integer, parameter casida_petersilka
Definition: casida.F90:189
integer, parameter casida_casida
Definition: casida.F90:189
subroutine doscillator_strengths(cas, mesh, st, hm)
Definition: casida.F90:1557
subroutine casida_type_init(cas, sys)
allocates stuff, and constructs the arrays pair_i and pair_j
Definition: casida.F90:952
integer function get_global_row(cas, jb_local)
Definition: casida.F90:1441
integer, parameter casida_eps_diff
Definition: casida.F90:189
character(len=80) pure function theory_name(cas)
Definition: casida.F90:1407
subroutine dcasida_forces(cas, sys, gr, st)
Definition: casida.F90:2951
subroutine local_indices(cas, ia, jb, on_this_processor, ia_local, jb_local)
Definition: casida.F90:1469
subroutine zcasida_solve(cas, sys)
Definition: casida.F90:5413
subroutine casida_work(sys, cas)
this subroutine calculates electronic excitation energies using the matrix formulation of M....
Definition: casida.F90:1136
integer, parameter casida_variational
Definition: casida.F90:189
subroutine dcasida_get_matrix(cas, namespace, hm, st, ks, gr, matrix, fxc, fxc_grad, fxc_grad_spin, restart_file, is_forces, matrixB)
Definition: casida.F90:2047
subroutine qcasida_write(cas, namespace)
Definition: casida.F90:1369
subroutine zcasida_write(cas, sys)
Definition: casida.F90:5639
logical function isnt_degenerate(cas, st, ia, jb)
Definition: casida.F90:1427
subroutine, public casida_run(system, from_scratch)
Definition: casida.F90:296
subroutine dcasida_write(cas, sys)
Definition: casida.F90:3440
real(real64) function casida_matrix_factor(cas, sys)
Definition: casida.F90:1348
subroutine casida_run_legacy(sys, fromScratch)
Definition: casida.F90:314
subroutine zoscillator_strengths(cas, mesh, st, hm)
Definition: casida.F90:3756
integer, parameter casida_tamm_dancoff
Definition: casida.F90:189
subroutine zcasida_forces(cas, sys, gr, st)
Definition: casida.F90:5150
subroutine casida_type_end(cas)
Definition: casida.F90:1079
subroutine dcasida_solve(cas, sys)
Definition: casida.F90:3214
This module implements a calculator for the density and defines related functions.
Definition: density.F90:99
subroutine, public states_elec_total_density(st, mesh, total_rho)
This routine calculates the total electronic density.
Definition: density.F90:1082
This module calculates the derivatives (gradients, Laplacians, etc.) of a function.
integer, parameter, public unpolarized
Parameters...
integer, parameter, public spinors
integer, parameter, public spin_polarized
real(real64), parameter, public m_two
Definition: global.F90:175
real(real64), parameter, public m_zero
Definition: global.F90:173
integer, parameter, public hartree_fock
Definition: global.F90:223
integer, parameter, public generalized_kohn_sham_dft
Definition: global.F90:223
real(real64), parameter, public m_epsilon
Definition: global.F90:189
real(real64), parameter, public m_half
Definition: global.F90:179
real(real64), parameter, public m_one
Definition: global.F90:174
This module implements the underlying real-space grid.
Definition: grid.F90:96
subroutine, public io_function_read_what_how_when(namespace, space, what, how, output_interval, what_tag_in, how_tag_in, output_interval_tag_in, ignore_error)
Definition: io.F90:93
integer pure function, public kpoints_number(this)
Definition: kpoints.F90:1205
A module to handle KS potential, without the external potential.
integer, parameter, public dft_u_none
Definition: lda_u.F90:184
System information (time, memory, sysname)
Definition: loct.F90:94
logical function, public loct_isinstringlist(a, s)
Definition: loct.F90:265
subroutine, public loct_progress_bar(a, maxcount)
A wrapper around the progress bar, such that it can be silenced without needing to dress the call wit...
Definition: loct.F90:253
This module defines various routines, operating on mesh functions.
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:97
subroutine, public messages_print_with_emphasis(msg, iunit, namespace)
Definition: messages.F90:878
subroutine, public messages_not_implemented(feature, namespace)
Definition: messages.F90:1048
character(len=512), private msg
Definition: messages.F90:143
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:501
subroutine, public messages_obsolete_variable(namespace, name, rep)
Definition: messages.F90:980
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_experimental(name, namespace)
Definition: messages.F90:1020
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:570
type(mpi_comm), parameter, public mpi_comm_undefined
used to indicate a communicator has not been initialized
Definition: mpi.F90:115
type(mpi_grp_t), public mpi_world
Definition: mpi.F90:317
subroutine mpi_grp_init(grp, comm)
Initialize MPI group instance.
Definition: mpi.F90:386
This module handles the communicators for the various parallelization strategies.
Definition: multicomm.F90:124
logical pure function, public multicomm_strategy_is_parallel(mc, level)
Definition: multicomm.F90:705
This module implements the basic mulsisystem class, a container system for other systems.
integer function, public parse_block(namespace, name, blk, check_varinfo_)
Definition: parser.F90:600
subroutine, public photon_mode_set_n_electrons(this, qtot)
/* 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_out(label)
Increment out counter and sum up difference between entry and exit time.
Definition: profiling.F90:672
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
Definition: profiling.F90:604
integer, parameter, public restart_casida
Definition: restart.F90:133
integer, parameter, public restart_gs
Definition: restart.F90:133
integer, parameter, public restart_type_dump
Definition: restart.F90:161
integer, parameter, public restart_type_load
Definition: restart.F90:161
This module contains interfaces for ScaLAPACK routines Interfaces are from http://www....
Definition: scalapack.F90:110
This module is intended to contain "only mathematical" functions and procedures.
Definition: sort.F90:96
pure logical function, public states_are_complex(st)
pure logical function, public states_are_real(st)
This module handles spin dimensions of the states and the k-point distribution.
subroutine, public occupied_states(st, namespace, ik, n_filled, n_partially_filled, n_half_filled, filled, partially_filled, half_filled)
return information about occupied orbitals in many-body state
This module handles reading and writing restart information for the states_elec_t.
subroutine, public states_elec_look_and_load(restart, namespace, space, st, mesh, kpoints, fixed_occ, is_complex, packed)
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
type(unit_system_t), public units_inp
the units systems for reading and writing
This module is intended to contain simple general-purpose utility functions and procedures.
Definition: utils.F90:99
subroutine, public v_ks_h_setup(namespace, space, gr, ions, ext_partners, st, ks, hm, calc_eigenval, calc_current)
Definition: v_ks.F90:711
subroutine, public xc_get_fxc(xcs, gr, namespace, rho, ispin, fxc, fxc_grad, fxc_grad_spin)
Returns the exchange-correlation kernel.
Definition: xc_kernel.F90:149
Definition: xc.F90:97
logical pure function, public family_is_mgga_with_exc(xcs)
Is the xc function part of the mGGA family with an energy functional.
Definition: xc.F90:706
logical pure function, public family_is_hybrid(xcs)
Returns true if the functional is an hybrid functional.
Definition: xc.F90:721
pure logical function, public in_family(family, xc_families)
Definition: xc.F90:734
subroutine, public xc_write_fxc_info(xcs, iunit, namespace)
Definition: xc.F90:264
subroutine, public xc_sic_write_info(sic, iunit, namespace)
Definition: xc_sic.F90:236
integer, parameter, public sic_adsic
Averaged density SIC.
Definition: xc_sic.F90:130
subroutine, public xc_sic_add_fxc_adsic(namespace, xc, st, gr, rho, fxc, fxc_grad, fxc_grad_spin, triplet)
Adds to fxc the ADSIC contribution.
Definition: xc_sic.F90:454
This class contains all parameters, needed for Casida calculations.
Definition: casida.F90:201
Class describing the electron system.
Definition: electrons.F90:199
Description of the grid, containing information on derivatives, stencil, and symmetries.
Definition: grid.F90:148
This is defined even when running serial.
Definition: mpi.F90:121
Container class for lists of system_oct_m::system_t.
The states_elec_t class contains all electronic wave functions.
int true(void)