Skip to content
Draft
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
1 change: 1 addition & 0 deletions source/Makefile.Objects
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,7 @@ OBJS_SRCPW=h_ewald_pw.o\
elecond.o\
sto_tool.o\
sto_elecond.o\
sto_dos_quality.o\
sto_dos.o\
onsite_proj.o\
onsite_proj_init.o\
Expand Down
6 changes: 5 additions & 1 deletion source/source_io/module_parameter/read_inp_postproc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ void ReadInput::item_postprocess()
item.annotation = "orders of Chebyshev expansions for dos";
item.category = "Density of states";
item.type = "Integer";
item.description = "The order of Chebyshev expansions when using Stochastic Density Functional Theory (SDFT) to calculate DOS.";
item.description = "The order of Chebyshev expansions when using Stochastic Density Functional Theory (SDFT) to calculate DOS."
"\n\nIt must be at least 2. The order has to grow with the ratio between the spectral range and "
"`dos_sigma`: resolving a Gaussian of width `dos_sigma` over a range of a few thousand eV can "
"require an order of 1e5 or more, so a value that is too small gives an under-resolved curve. "
"The code warns when the requested order cannot resolve `range/dos_sigma`.";
item.default_value = "100";
item.unit = "";
read_sync_int(input.dos_nche);
Expand Down
2 changes: 2 additions & 0 deletions source/source_pw/module_stodft/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ list(
sto_hsolver_pw.cpp
sto_iter.cpp
sto_che.cpp
sto_dos_quality.cpp
sto_wf.cpp
sto_func.cpp
sto_forces.cpp
Expand All @@ -23,4 +24,5 @@ if(BUILD_TESTING)
if(ENABLE_MPI)
add_subdirectory(test)
endif()
add_subdirectory(unittests)
endif()
175 changes: 171 additions & 4 deletions source/source_pw/module_stodft/sto_dos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,29 @@
#include "source_base/timer.h"
#include "source_base/tool_title.h"
#include "source_io/module_parameter/parameter.h"
#include "sto_dos_quality.h"
#include "sto_tool.h"

#include <algorithm>
#include <cmath>
#include <limits>
#include <sstream>

namespace
{
/// Single entry point for the DOS diagnostics of this file. Keeping the
/// running-log reference here (instead of at every call site) keeps the global
/// dependency of this module to one place; none of these messages aborts a run.
void dos_log(const std::string& message, const bool also_stdout)
{
GlobalV::ofs_running << message << std::endl;
if (also_stdout)
{
std::cout << message << std::endl;
}
}
} // namespace

template <typename FPTYPE, typename Device>
Sto_DOS<FPTYPE, Device>::~Sto_DOS()
{
Expand All @@ -28,7 +50,10 @@ Sto_DOS<FPTYPE, Device>::Sto_DOS(ModulePW::PW_Basis_K* p_wfcpw_in,
this->p_hamilt_sto = static_cast<StoHamiltPW<std::complex<double>>*>(p_hamilt_in);
this->p_stowf = p_stowf_in;
this->nbands_ks = p_psi_in->get_nbands();
this->nbands_sto = p_stowf_in->nchi;
// Stochastic_WF::nchi is now initialized by allocate_chi0() (MDFT) and
// init_com_orbitals() (complete basis). Keep a safe fallback for old
// restart paths where only nchip is available.
this->nbands_sto = (p_stowf_in->nchi > 0) ? p_stowf_in->nchi : p_stowf_in->nchip_max;
this->method_sto = stoche.method_sto;
this->stofunc.set_E_range((double*)&stoche.emin_sto, (double*)&stoche.emax_sto);
}
Expand All @@ -44,14 +69,71 @@ void Sto_DOS<FPTYPE, Device>::decide_param(const int& dos_nche,
const double& dos_scale)
{
this->dos_nche = dos_nche;
if (this->dos_nche < 2)
{
ModuleBase::WARNING_QUIT("Sto_DOS", "dos_nche should be at least 2.");
}

// If emin_sto/emax_sto are left at zero, keep the bounds already found by
// the SCF range check instead of resetting them to the bare input defaults.
// The repeated reset discarded a valid converged range and made the DOS
// range check depend on the random test vector alone.
double try_emin = emin_sto;
double try_emax = emax_sto;
if (try_emin == 0.0)
{
try_emin = *p_hamilt_sto->emin;
}
if (try_emax == 0.0)
{
try_emax = *p_hamilt_sto->emax;
}

check_che_op<FPTYPE, Device>()(
this->dos_nche,
emin_sto,
emax_sto,
try_emin,
try_emax,
this->nbands_sto,
this->p_kv,
reinterpret_cast<Stochastic_WF<std::complex<FPTYPE>, Device>*>(this->p_stowf),
reinterpret_cast<StoHamiltPW<std::complex<FPTYPE>, Device>*>(this->p_hamilt_sto));

// The range determined by the SCF check only learns the high-energy end
// reliably. Run the pure, unit-tested sanitiser (module
// sto_dos_quality): it covers the known deterministic eigenvalues and the
// plane-wave spectrum top, so an occupied state outside the interval (T_n
// grows exponentially there) and an underestimated upper bound are both
// removed. Healthy ranges are returned bit for bit unchanged.
{
std::vector<double> det_eigs;
if (this->p_elec != nullptr)
{
const auto& ekb = this->p_elec->ekb;
for (int ik = 0; ik < ekb.nr; ++ik)
{
for (int ib = 0; ib < ekb.nc; ++ib)
{
const double e = ekb(ik, ib);
if (std::isfinite(e))
{
det_eigs.push_back(e);
}
}
}
}
const ModuleIO::CheRange fixed
= ModuleIO::sanitize_che_range(*p_hamilt_sto->emin, *p_hamilt_sto->emax, PARAM.inp.ecutwfc, det_eigs);
if (fixed.emin_repaired || fixed.emax_repaired || fixed.degenerate)
{
std::ostringstream range_msg;
range_msg << " DOS: Chebyshev range repaired from [" << *p_hamilt_sto->emin << ", " << *p_hamilt_sto->emax
<< "] to [" << fixed.emin << ", " << fixed.emax << "] Ry";
dos_log(range_msg.str(), false);
}
*p_hamilt_sto->emin = fixed.emin;
*p_hamilt_sto->emax = fixed.emax;
}

if (dos_setemax)
{
this->emax = dos_emax_ev;
Expand All @@ -75,6 +157,54 @@ void Sto_DOS<FPTYPE, Device>::decide_param(const int& dos_nche,
this->emax = emax + delta / 2.0;
this->emin = emin - delta / 2.0;
}

if (!std::isfinite(this->emin) || !std::isfinite(this->emax) || this->emax <= this->emin)
{
ModuleBase::WARNING_QUIT("Sto_DOS", "Invalid DOS energy range: dos_emax_ev must be larger than dos_emin_ev.");
}

// The Chebyshev interval is a property of the spectrum and is kept as it is:
// widening it to fit the requested output window would reduce the resolution
// of a fixed-order expansion and could hide a peak that a narrower interval
// resolves. A requested window that sticks out of the interval is therefore
// clipped to it (the `dos_scale` widening above makes that routine) and the
// change is reported, because energies outside the interval cannot be
// represented by the expansion at all.
const double interval_emin_ev = *p_hamilt_sto->emin * ModuleBase::Ry_to_eV;
const double interval_emax_ev = *p_hamilt_sto->emax * ModuleBase::Ry_to_eV;
const double requested_emin_ev = this->emin;
const double requested_emax_ev = this->emax;
const double clipped_emin_ev = std::max(requested_emin_ev, interval_emin_ev);
const double clipped_emax_ev = std::min(requested_emax_ev, interval_emax_ev);
// The `dos_scale` widening above intentionally makes the window a little wider
// than the spectrum, so a sub-percent clip is expected and is not reported;
// only a window that really asks for energies outside the interval is.
const double interval_span_ev = interval_emax_ev - interval_emin_ev;
const double dropped_ev
= std::max(requested_emin_ev - clipped_emin_ev, clipped_emax_ev - requested_emax_ev);
const bool report_clip = dropped_ev > 0.01 * interval_span_ev;
if (clipped_emax_ev > clipped_emin_ev)
{
// Always clip, so that |t| <= 1 holds by construction for every output
// energy. Only a clip that removes a visible part of the window is
// reported, because `dos_scale` intentionally widens the window by a
// fraction of a percent.
if (report_clip)
{
std::ostringstream clip_msg;
clip_msg << " DOS: output window clipped from [" << requested_emin_ev << ", " << requested_emax_ev << "] to ["
<< clipped_emin_ev << ", " << clipped_emax_ev << "] eV to stay inside the Chebyshev interval ["
<< interval_emin_ev << ", " << interval_emax_ev << "] eV";
dos_log(clip_msg.str(), false);
}
this->emin = clipped_emin_ev;
this->emax = clipped_emax_ev;
}

std::ostringstream range_msg;
range_msg << " DOS: Chebyshev range = [" << *p_hamilt_sto->emin << ", " << *p_hamilt_sto->emax
<< "] Ry; output window = [" << this->emin << ", " << this->emax << "] eV";
dos_log(range_msg.str(), false);
}

template <typename FPTYPE, typename Device>
Expand Down Expand Up @@ -168,13 +298,36 @@ void Sto_DOS<FPTYPE, Device>::caldos(const double sigmain, const double de, cons
allorderchi.shrink_to_fit();

std::ofstream ofsdos;
if (de <= 0.0)
{
ModuleBase::WARNING_QUIT("Sto_DOS", "dos_edelta_ev must be positive.");
}
int ndos = int((emax - emin) / de) + 1;
if (ndos < 1)
{
ModuleBase::WARNING_QUIT("Sto_DOS", "The DOS output window is empty; check dos_emin_ev/dos_emax_ev.");
}
this->stofunc.sigma = sigmain / ModuleBase::Ry_to_eV;

// Report DOS settings that cannot resolve the curve: grid spacing versus
// Gaussian broadening, and Chebyshev order versus range/sigma. The checks
// are pure functions (unittests/test_sto_dos_quality.cpp) and only report;
// a long run is never aborted by a quality suggestion.
{
const double range_ev = (*this->stofunc.Emax - *this->stofunc.Emin) * ModuleBase::Ry_to_eV;
const double sigma_ev = std::abs(sigmain);
for (const std::string& msg: ModuleIO::check_dos_parameters(range_ev, sigma_ev, de, this->dos_nche))
{
dos_log(" WARNING: DOS: " + msg, true);
}
}
ModuleBase::timer::end("Sto_DOS", "Tracepoly");

std::cout << "2. Dos:" << std::endl;
ModuleBase::timer::start("Sto_DOS", "DOS Loop");
int n10 = ndos / 10;
// For very small output windows ndos/10 is zero; the old modulus then
// divided by zero and terminated the run with SIGFPE.
int n10 = std::max(1, ndos / 10);
int percent = 10;
std::vector<double> sto_dos(ndos);
std::vector<double> ks_dos(ndos);
Expand Down Expand Up @@ -246,6 +399,7 @@ void Sto_DOS<FPTYPE, Device>::caldos(const double sigmain, const double de, cons
ofsdos.open(dosfile.c_str());
double maxerror = 0;
double sum = 0;
double min_dos = std::numeric_limits<double>::max();

ofsdos << ndos << " # number of points" << std::endl;
ofsdos << "#" << std::setw(19) << "energy(eV)"
Expand All @@ -259,6 +413,7 @@ void Sto_DOS<FPTYPE, Device>::caldos(const double sigmain, const double de, cons
maxerror = tmperror;
}
double dos = 2.0 * (ks_dos[ie] + sto_dos[ie]) / ModuleBase::Ry_to_eV;
min_dos = std::min(min_dos, dos);
sum += dos;
ofsdos << std::setw(20) << emin + ie * de << std::setw(20) << dos << std::setw(20) << sum * de
<< std::setw(20) << tmperror << std::endl;
Expand All @@ -267,6 +422,18 @@ void Sto_DOS<FPTYPE, Device>::caldos(const double sigmain, const double de, cons
std::cout << "Finish DOS" << std::endl;
std::cout << std::scientific << "DOS max absolute Chebyshev Error: " << maxerror << std::endl;
ofsdos.close();

// Result-level checks: a curve that goes visibly negative, or an output
// energy outside the Chebyshev interval, means the printed numbers must
// not be used without changing dos_sigma / dos_nche / the window.
const double dE = *this->stofunc.Emax - *this->stofunc.Emin;
const double t_lo = (2.0 * this->emin / ModuleBase::Ry_to_eV - *this->stofunc.Emax - *this->stofunc.Emin) / dE;
const double t_hi = (2.0 * this->emax / ModuleBase::Ry_to_eV - *this->stofunc.Emax - *this->stofunc.Emin) / dE;
const double max_abs_t = std::max(std::fabs(t_lo), std::fabs(t_hi));
for (const std::string& msg: ModuleIO::check_dos_result(min_dos, max_abs_t))
{
dos_log(" WARNING: DOS: " + msg, true);
}
}
ModuleBase::timer::end("Sto_DOS", "DOS Loop");
ModuleBase::timer::end("Sto_DOS", "caldos");
Expand Down
Loading
Loading