Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/advanced/elec_properties/hs_matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ In gamma-only mode, every generated file reports one Bravais lattice vector and
# representation: gamma-only folded matrix; stored R-space contributions are summed into R = (0, 0, 0)
```

### Using the matrices for LR-TDDFT absorption

The full-Hamiltonian [velocity commutator](../input_files/input-main.md#abs_gauge) is available for MPI LCAO LR-TDDFT with `gamma_only 0`. If the ground-state functional uses exact exchange, LR-TDDFT selects it when `abs_gauge` is omitted. With `ks-lr`, the ground-state solver supplies the matrices in memory.

For standalone `lr`, add the following to the SCF or NSCF input that supplies the orbitals:

```text
gamma_only 0
out_hsr 1 12
out_app_flag 0
```

Place `hrs1_nao.csr` and `sr_nao.csr` in the LR `read_file_dir` alongside the matching wavefunctions. For `nspin 2`, also place `hrs2_nao.csr` there. ABACUS calculates the position integrals internally. The single `nspin 1` Hamiltonian block is used at its original scale, with spin degeneracy included in the singlet transition amplitude.

For local and semilocal functionals without exact exchange, use `abs_gauge velocity`. Its direct momentum and nonlocal pseudopotential integrals avoid the additional finite-AO projection used to form the full commutator.

LR-TDDFT reports excitation energies with 17 significant digits. Use
`abs_gauge none` to report energies without evaluating optical matrix
elements. The absorption spectrum is a text table in `absorption.dat`.

### Native Binary CSR Format

Set `out_hsr 2` to write the same H(R) and S(R) matrix sets with a `.dat` suffix. For example, an `nspin = 2` calculation writes `hrs1_nao.dat`, `hrs2_nao.dat`, and `sr_nao.dat`. When `out_app_flag` is 2, the one-based ionic step is included before `_nao`, for example `hrs1g1_nao.dat` and `srg1_nao.dat`.
Expand Down
2 changes: 1 addition & 1 deletion source/source_esolver/esolver_lr_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ void ModuleESolver::ESolver_LR<T, TR>::initialize_from_unitcell_(UnitCell &ucell
/// read orbitals and build the interpolation table
two_center_bundle_own_.build_orb(ucell.ntype, ucell.orbital_fn.data(), inp.orbital_dir);

LCAO_Orbitals orb;
LCAO_Orbitals& orb = this->orb_;
two_center_bundle_own_.to_LCAO_Orbitals(
orb, inp.lcao_ecut, inp.lcao_dk, inp.lcao_dr, inp.lcao_rmax, inp.out_element_info, inp.cal_force);
orb_cutoff_ = orb.cutoffs();
Expand Down
25 changes: 13 additions & 12 deletions source/source_esolver/esolver_lr_lcao_bse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ void ESolver_BSE<T, TR>::after_all_runners(BaseCell &basecell)
}
if (this->inp_->lr_solver == "spectrum" || this->inp_->lr_solver == "elpa")
{
const int legacy_energy_precision = 0;
const std::string gauge = LR_Util::tolower(this->inp_->abs_gauge);
if (gauge == "none")
{
Expand All @@ -412,7 +413,7 @@ void ESolver_BSE<T, TR>::after_all_runners(BaseCell &basecell)
{
const double *const energies = &this->tda_ene[is * this->nstates];
const std::string channel = this->inp_->bse_spin_types[is] + "_tda";
LR_Util::print_excitation_energies(this->ofs_running_, energies, this->nstates, channel);
LR_Util::print_excitation_energies(this->ofs_running_, energies, this->nstates, channel, legacy_energy_precision);
}
}
if (this->inp_->bse_tda == "both" || this->inp_->bse_tda == "full")
Expand All @@ -421,7 +422,7 @@ void ESolver_BSE<T, TR>::after_all_runners(BaseCell &basecell)
{
const double *const energies = &this->full_ene[is * this->nstates];
const std::string channel = this->inp_->bse_spin_types[is] + "_full";
LR_Util::print_excitation_energies(this->ofs_running_, energies, this->nstates, channel);
LR_Util::print_excitation_energies(this->ofs_running_, energies, this->nstates, channel, legacy_energy_precision);
}
}
}
Expand Down Expand Up @@ -450,12 +451,12 @@ void ESolver_BSE<T, TR>::after_all_runners(BaseCell &basecell)
spectrum.set_vmo(this->velocity_mo.data());
}
spectrum.cal_spectrum();
spectrum.transition_analysis(this->inp_->bse_spin_types[is] + "_tda");
if (this->inp_->bse_spin_types[is] !=
"triplet") // triplets has no transition dipole and no contribution to the spectrum
const std::string channel = this->inp_->bse_spin_types[is] + "_tda";
spectrum.transition_analysis(channel, legacy_energy_precision);
if (this->inp_->bse_spin_types[is] != "triplet") // triplets has no transition dipole and no contribution to the spectrum
{
spectrum.write_transition_dipole(
output_dir + "trans_dipole_" + this->inp_->bse_spin_types[is] + "_tda.dat");
const std::string dipole_filename = output_dir + "trans_dipole_" + channel + ".dat";
spectrum.write_transition_dipole(dipole_filename, legacy_energy_precision);
// ============================== for test ==============================
if (LR_Util::tolower(this->inp_->abs_gauge) == "velocity")
{ //// TEST the formula v/omega rather than v/(e_a-e_i)
Expand Down Expand Up @@ -484,12 +485,12 @@ void ESolver_BSE<T, TR>::after_all_runners(BaseCell &basecell)
spectrum.set_Y(this->full_Y[is].template data<T>());
spectrum.set_full(true);
spectrum.cal_spectrum();
spectrum.transition_analysis(this->inp_->bse_spin_types[is] + "_full");
if (this->inp_->bse_spin_types[is] !=
"triplet") // triplets has no transition dipole and no contribution to the spectrum
const std::string channel = this->inp_->bse_spin_types[is] + "_full";
spectrum.transition_analysis(channel, legacy_energy_precision);
if (this->inp_->bse_spin_types[is] != "triplet") // triplets has no transition dipole and no contribution to the spectrum
{
spectrum.write_transition_dipole(
output_dir + "trans_dipole_" + this->inp_->bse_spin_types[is] + "_full.dat");
const std::string dipole_filename = output_dir + "trans_dipole_" + channel + ".dat";
spectrum.write_transition_dipole(dipole_filename, legacy_energy_precision);
}
}
}
Expand Down
92 changes: 71 additions & 21 deletions source/source_esolver/esolver_lr_lcao_tddft.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// LR execution flow and final spectrum/density output.
#include "esolver_lr_lcao_tddft.h"
#include "source_hamilt/module_xc/xc_functional.h"
#include "source_lcao/hamilt_lcao.h"
#include "source_lcao/module_lr/hamilt_ulr.h"
#include "source_lcao/module_lr/hsolver_lrtd.h"
#include "source_lcao/module_lr/lr_density.h"
Expand Down Expand Up @@ -37,6 +38,7 @@ void ModuleESolver::ESolver_LR<T, TR>::runner(BaseCell &basecell, const int iste
basecell.require_kind(BaseCell::Kind::unitcell, __FUNCTION__);
UnitCell &ucell = static_cast<UnitCell &>(basecell);

this->last_ionic_step_ = istep + 1;
ModuleBase::TITLE("ESolver_LR", "runner");
ModuleBase::timer::start("ESolver_LR", "runner");

Expand Down Expand Up @@ -91,14 +93,24 @@ void ModuleESolver::ESolver_LR<T, TR>::runner(BaseCell &basecell, const int iste
else if (this->inp_->lr_solver != "spectrum")
{
auto write_states = [this, &efile_out, &vfile_out](
const std::string &label, const Real<T> *e, const T *v, const int &dim, const int &nst,
const int &prec = 8) -> void
const std::string& label, const Real<T>* e, const T* v, const int dim, const int nst)
{
if (this->my_rank_ == 0)
{
assert(nst == LR_Util::write_value(efile_out(label), prec, e, nst));
const std::string energy_file = efile_out(label);
if (!LR_Util::write_excitation_energy_file(energy_file, e, nst))
{
ModuleBase::WARNING_QUIT("ESolver_LR", "Failed to write " + energy_file);
}
this->ofs_running_ << " Write LR excitation energies to " << energy_file << '\n';
}
const std::string amplitude_file = vfile_out(label);
const int amplitude_digits = 8;
if (LR_Util::write_value(amplitude_file, amplitude_digits, v, nst, dim) != nst * dim)
{
ModuleBase::WARNING_QUIT("ESolver_LR", "Failed to write " + amplitude_file);
}
assert(nst * dim == LR_Util::write_value(vfile_out(label), prec, v, nst, dim));
this->ofs_running_ << " Write LR excitation amplitudes to " << amplitude_file << '\n';
};
std::vector<double> precondition(this->inp_->lr_solver == "lapack" ? 0 : nloc_per_state, 1.0);
// allocate and initialize A matrix and density matrix
Expand Down Expand Up @@ -282,14 +294,50 @@ void ModuleESolver::ESolver_LR<T, TR>::after_all_runners(BaseCell &basecell)
const std::string gauge = LR_Util::tolower(this->inp_->abs_gauge);
const bool calculate_spectrum = gauge != "none";
const std::vector<double> spatial_weights = LR_Util::spatial_k_weights(this->kv.wk, nspin);
// cal spectrum
if (LR_Util::tolower(this->inp_->abs_gauge) == "velocity")
const bool use_velocity = gauge == "velocity" || gauge == "velocity_commutator";
const int nspin_tmp = this->inp_->nspin == 2 ? 2 : 1;
if (gauge == "velocity")
{
const int nspin_tmp = this->inp_->nspin == 2 ? 2 : 1;
this->velocity_mo = LR_Util::cal_velocity_mo(
*this->ucell_, this->gd(), this->tcb(), this->paraMat_, this->paraC_, this->kv, *this->psi_ks, this->nk,
nspin_tmp, this->nbasis, this->nocc, this->nvirt);
}
if (gauge == "velocity_commutator")
{
std::vector<const hamilt::HContainer<double>*> hr;
hamilt::HContainer<double> overlap(&this->paraMat_);
hamilt::HContainer<double> hr_up(&this->paraMat_);
hamilt::HContainer<double> hr_down(&this->paraMat_);
const hamilt::HContainer<double>* sr = &overlap;
const LCAO_Orbitals& orbitals = this->ks_ ? this->ks_->orb_ : this->orb_;
if (this->ks_)
{
auto* hamilt = static_cast<hamilt::HamiltLCAO<T, TR>*>(this->ks_->p_hamilt);
const auto spin_blocks = hamilt->getHR_vector();
hr.assign(spin_blocks.begin(), spin_blocks.end());
sr = hamilt->getSR();
}
else
{
const std::string overlap_path = this->in_dir + "sr_nao.csr";
const std::string up_path = this->in_dir + "hrs1_nao.csr";
LR_Util::read_velocity_matrix_R(overlap, overlap_path, ucell, this->nbasis, this->my_rank_,
this->ofs_running_);
LR_Util::read_velocity_matrix_R(hr_up, up_path, ucell, this->nbasis, this->my_rank_,
this->ofs_running_);
hr.push_back(&hr_up);
if (nspin_tmp == 2)
{
const std::string down_path = this->in_dir + "hrs2_nao.csr";
LR_Util::read_velocity_matrix_R(hr_down, down_path, ucell, this->nbasis, this->my_rank_,
this->ofs_running_);
hr.push_back(&hr_down);
}
}
this->velocity_mo = LR_Util::cal_commutator_velocity_mo(
ucell, this->gd(), orbitals, this->paraMat_, this->paraC_, this->kv, *this->psi_ks,
this->nk, nspin_tmp, this->nocc, this->nvirt, this->inp_->cal_force, *sr, hr);
}
std::vector<double> freq;
if (calculate_spectrum)
{
Expand All @@ -307,36 +355,38 @@ void ModuleESolver::ESolver_LR<T, TR>::after_all_runners(BaseCell &basecell)
freq[i] = 91.126664 / (lambda_min + 0.01 * static_cast<double>(i + 1) * lambda_diff);
}
}
// auto spin_types = (nspin == 2 && !openshell) ? std::vector<std::string>({ "singlet", "triplet" }) :
// std::vector<std::string>({ "updown" }); for (int is = 0;is < this->X.size() - 1;++is)
const int energy_precision = LR_Util::lr_excitation_energy_digits;
for (int is = 0; is < this->X.size(); ++is)
{
if (!calculate_spectrum)
{
const double *const energies = &this->pelec->ekb.c[is * nstates];
LR_Util::print_excitation_energies(this->ofs_running_, energies, nstates, spin_types[is]);
const double* const energies = &this->pelec->ekb.c[is * nstates];
LR_Util::print_excitation_energies(this->ofs_running_, energies, nstates, spin_types[is], energy_precision);
}
else
{
LR_Spectrum<T> spectrum(
nspin, this->nbasis, this->nocc, this->nvirt, *this->pw_rho, *this->psi_ks, *this->ucell_, this->kv,
spatial_weights, this->gd(), this->orb_cutoff_, this->tcb(), this->paraX_, this->paraC_, this->paraMat_,
&this->pelec->ekb.c[is * nstates], this->eig_ks.c, this->X[is].template data<T>(), nstates, openshell,
LR_Util::tolower(this->inp_->abs_gauge), this->my_rank_, this->out_dir);
if (LR_Util::tolower(this->inp_->abs_gauge) == "velocity")
gauge, this->my_rank_, this->out_dir);
if (use_velocity)
{
spectrum.set_vmo(this->velocity_mo.data());
}
spectrum.cal_spectrum();
spectrum.transition_analysis(spin_types[is] + "_tda");
if (spin_types[is] != "triplet") // triplets has no transition dipole and no contribution to the spectrum
const std::string channel = spin_types[is] + "_tda";
spectrum.transition_analysis(channel, energy_precision);
if (spin_types[is] != "triplet") // triplets has no transition dipole and no contribution to the spectrum
{
spectrum.optical_absorption_method1(freq, this->inp_->abs_broadening);
spectrum.write_transition_dipole(this->out_dir + "trans_dipole_" + spin_types[is] + "_tda.dat");
// =============================================== for test
// ==================================================== spectrum.optical_absorption_method2(freq,
// this->inp_->abs_broadening); if (LR_Util::tolower(this->inp_->abs_gauge) == "velocity") { // TEST
// the formula v/omega rather than v/(e_a-e_i)
spectrum.optical_absorption_method1(
freq, this->inp_->abs_broadening, this->last_ionic_step_, this->ofs_running_);
const std::string dipole_filename = this->out_dir + "trans_dipole_" + channel + ".dat";
spectrum.write_transition_dipole(dipole_filename, energy_precision);
// =============================================== for test ====================================================
// spectrum.optical_absorption_method2(freq, this->inp_->abs_broadening);
// if (LR_Util::tolower(this->inp_->abs_gauge) == "velocity")
// { // TEST the formula v/omega rather than v/(e_a-e_i)
// spectrum.test_transition_dipoles_velocity_omega();
// spectrum.write_transition_dipole(this->out_dir +
// "trans_dipole_" + spin_types[is] + "_vomega_tda.dat");
Expand Down
1 change: 1 addition & 0 deletions source/source_esolver/esolver_lr_lcao_tddft.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class ESolver_LR : public ModuleESolver::ESolver_FP
void initialize_from_ks_(UnitCell &ucell, const Input_para &inp);
/// re-read everything that depends on the atomic positions, once per ionic step
void refresh_from_ks_(UnitCell &ucell);
int last_ionic_step_ = 1; ///< one-based step of the final LR spectrum
bool ks_initialized_ = false; ///< whether `initialize_from_ks_` has already run
bool exx_owned_ = false; ///< `exx_lri` was built here (so its Cs/Vs are ours to refresh)

Expand Down
12 changes: 9 additions & 3 deletions source/source_hamilt/module_hcontainer/read_hcontainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "hcontainer_funcs.h"
#include "source_base/module_out/csr_reader.h"
#include "source_base/module_out/sparse_matrix.h"
#include "source_base/tool_quit.h"
#include "transfer.h"

#include <fstream>
Expand Down Expand Up @@ -56,9 +57,14 @@ void Read_HContainer<T>::read()
{
#endif
ModuleIO::csrFileReader<T> csr(this->_filename);
int step = csr.getStep();
int matrix_dimension = csr.getMatrixDimension();
int r_number = csr.getNumberOfR();
const int matrix_dimension = csr.getMatrixDimension();
if (matrix_dimension != this->_nlocal)
{
const std::string message =
"Matrix dimension in " + this->_filename + " does not match the number of basis functions.";
ModuleBase::WARNING_QUIT("Read_HContainer::read", message);
}
const int r_number = csr.getNumberOfR();

// construct serial hcontainer firstly
// prepare atom index mapping from csr row/col to atom index
Expand Down
6 changes: 1 addition & 5 deletions source/source_hamilt/module_xc/exx_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ bool init_general_exx_info(General_Exx_Info &info, const Input_para &inp)
std::string dft_functional_lower = inp.dft_functional;
std::transform(inp.dft_functional.begin(), inp.dft_functional.end(), dft_functional_lower.begin(), tolower);
bool generate_opt_orb = false;
if (dft_functional_lower == "hf" || dft_functional_lower == "pbe0" || dft_functional_lower == "b3lyp" ||
dft_functional_lower == "hse" || dft_functional_lower == "scan0" || dft_functional_lower == "muller" ||
dft_functional_lower == "power" || dft_functional_lower == "cwp22" || dft_functional_lower == "wp22" ||
dft_functional_lower == "lc_pbe" || dft_functional_lower == "lc_wpbe" || dft_functional_lower == "lrc_wpbe" ||
dft_functional_lower == "lrc_wpbeh" || dft_functional_lower == "cam_pbeh")
if (xc_uses_exact_exchange(dft_functional_lower))
{
info.cal_exx = true;

Expand Down
11 changes: 11 additions & 0 deletions source/source_hamilt/module_xc/general_exx_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
#include "coulomb_config.h"

#include <cstddef>
#include <string>

/// Identify functionals that assemble a nonlocal Fock term. The name is lowercase.
inline bool xc_uses_exact_exchange(const std::string& functional)
{
return functional == "hf" || functional == "pbe0" || functional == "b3lyp"
|| functional == "hse" || functional == "scan0" || functional == "muller"
|| functional == "power" || functional == "cwp22" || functional == "wp22"
|| functional == "lc_pbe" || functional == "lc_wpbe" || functional == "lrc_wpbe"
|| functional == "lrc_wpbeh" || functional == "cam_pbeh";
}

/// General EXX configuration, independent of basis type.
/// Contains the fields needed by both PW and LCAO EXX calculations.
Expand Down
Loading
Loading