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