Repository navigation
SDFT: fix the high-temperature DOS blow-up and the Ne/TS precision asymmetry #8382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,5 +22,6 @@ endif() | |
| if(BUILD_TESTING) | ||
| if(ENABLE_MPI) | ||
| add_subdirectory(test) | ||
| add_subdirectory(unittests) | ||
| endif() | ||
| endif() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,12 @@ | |
| #include "source_base/tool_title.h" | ||
| #include "source_io/module_parameter/parameter.h" | ||
| #include "sto_tool.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <cmath> | ||
| #include <limits> | ||
| #include <sstream> | ||
|
|
||
| template <typename FPTYPE, typename Device> | ||
| Sto_DOS<FPTYPE, Device>::~Sto_DOS() | ||
| { | ||
|
|
@@ -28,7 +34,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); | ||
| } | ||
|
|
@@ -44,14 +53,84 @@ 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 stochastic range check learns only the high-energy end reliably. | ||
| // The lowest deterministic KS eigenvalue is a much safer lower bound; if | ||
| // the automatic Emin lies above it, the Chebyshev interval would exclude | ||
| // occupied states and T_n(H) would grow exponentially outside [-1,1]. | ||
| // Extending the interval below the lowest eigenvalue is always harmless. | ||
| double det_emin = std::numeric_limits<double>::max(); | ||
| bool has_det_emin = false; | ||
| 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_emin = std::min(det_emin, e); | ||
| has_det_emin = true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| #ifdef __MPI | ||
| double global_det_emin = has_det_emin ? det_emin : std::numeric_limits<double>::max(); | ||
| Parallel_Reduce::reduce_min(global_det_emin); | ||
| if (global_det_emin < std::numeric_limits<double>::max()) | ||
| { | ||
| det_emin = global_det_emin; | ||
| has_det_emin = true; | ||
| } | ||
| #endif | ||
| if (has_det_emin) | ||
| { | ||
| const double margin_ry = 1.0e-3; | ||
| const double safe_emin = det_emin - margin_ry; | ||
| if (safe_emin < *p_hamilt_sto->emin) | ||
| { | ||
| GlobalV::ofs_running << " DOS: reset Chebyshev Emin from " << *p_hamilt_sto->emin << " Ry to " | ||
| << safe_emin << " Ry (lowest KS eigenvalue = " << det_emin << " Ry)" | ||
| << std::endl; | ||
| *p_hamilt_sto->emin = safe_emin; | ||
| } | ||
| } | ||
| if (!std::isfinite(*p_hamilt_sto->emax) || *p_hamilt_sto->emax <= *p_hamilt_sto->emin) | ||
| { | ||
| *p_hamilt_sto->emax = *p_hamilt_sto->emin + 1.0; | ||
| } | ||
|
|
||
| if (dos_setemax) | ||
| { | ||
| this->emax = dos_emax_ev; | ||
|
|
@@ -75,6 +154,33 @@ 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."); | ||
| } | ||
|
|
||
| // A Gaussian centred outside the Chebyshev interval requires T_n(x) with | ||
| // |x| > 1, which grows exponentially with n. The default dos_scale path | ||
| // deliberately makes the output window slightly wider than the spectral | ||
| // range, so evaluating the Gaussian at those edge points used to be the | ||
| // final trigger for the 1e36-scale DOS values. Expand the Chebyshev | ||
| // interval itself (never the other way round) so that every requested | ||
| // output energy satisfies |x| <= 1. | ||
| const double output_emin_ry = this->emin / ModuleBase::Ry_to_eV; | ||
| const double output_emax_ry = this->emax / ModuleBase::Ry_to_eV; | ||
| const double guard_ry = std::max(1.0e-6, 1.0e-6 * (*p_hamilt_sto->emax - *p_hamilt_sto->emin)); | ||
| if (output_emin_ry - guard_ry < *p_hamilt_sto->emin) | ||
| { | ||
| *p_hamilt_sto->emin = output_emin_ry - guard_ry; | ||
| } | ||
| if (output_emax_ry + guard_ry > *p_hamilt_sto->emax) | ||
| { | ||
| *p_hamilt_sto->emax = output_emax_ry + guard_ry; | ||
|
Comment on lines
+173
to
+179
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Keep output limits separate from the spectral interval The output energy is the Gaussian centre; |
||
| } | ||
|
|
||
| GlobalV::ofs_running << " DOS: Chebyshev range = [" << *p_hamilt_sto->emin << ", " << *p_hamilt_sto->emax | ||
| << "] Ry; output window = [" << this->emin << ", " << this->emax << "] eV" << std::endl; | ||
| } | ||
|
|
||
| template <typename FPTYPE, typename Device> | ||
|
|
@@ -168,13 +274,47 @@ 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; | ||
|
|
||
| // Estimate the Chebyshev order needed to resolve a Gaussian of width | ||
| // sigmain over the actual spectral range. Reporting this explicitly | ||
| // prevents a silently under-resolved DOS: with a large ecut and the | ||
| // default dos_sigma (0.07 eV) the required order can be thousands or more, | ||
| // while method 2 stores an nche x nche matrix and becomes prohibitively | ||
| // expensive. | ||
| const double range_ev = (*this->stofunc.Emax - *this->stofunc.Emin) * ModuleBase::Ry_to_eV; | ||
| const double sigma_ev = std::abs(sigmain); | ||
| if (sigma_ev > 0.0 && range_ev > 0.0) | ||
| { | ||
| const double prefactor = std::sqrt(2.0 * std::log(1.0e6)); | ||
| const double nche_needed = 0.5 * range_ev / sigma_ev * prefactor; | ||
| if (nche_needed > static_cast<double>(this->dos_nche)) | ||
| { | ||
| std::ostringstream msg; | ||
| msg << "dos_sigma = " << sigma_ev << " eV is narrower than the Chebyshev expansion can resolve: " | ||
| << "range = " << range_ev << " eV, dos_nche = " << this->dos_nche | ||
| << ", estimated order needed ~ " << static_cast<long long>(std::ceil(nche_needed)) | ||
| << ". Increase dos_nche (method 1 or method 2 with sufficient memory) or use a larger dos_sigma."; | ||
| GlobalV::ofs_running << " WARNING: " << msg.str() << std::endl; | ||
| std::cout << " WARNING: " << msg.str() << std::endl; | ||
| } | ||
| } | ||
| 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); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,9 @@ | |
| #include "source_hsolver/para_lin_tf.h" | ||
| #include "source_io/module_parameter/parameter.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <cmath> | ||
|
|
||
| template <typename T, typename Device> | ||
| Stochastic_Iter<T, Device>::Stochastic_Iter() | ||
| { | ||
|
|
@@ -211,6 +214,29 @@ void Stochastic_Iter<T, Device>::checkemm(const int& ik, | |
| Parallel_Reduce::reduce_min(*p_hamilt_sto->emin); | ||
| MPI_Allreduce(MPI_IN_PLACE, &change, 1, MPI_CHAR, MPI_LOR, MPI_COMM_WORLD); | ||
| #endif | ||
| // checkconverge() treats (0,0) as "unset" and can end with a | ||
| // positive Emin when the trial Rayleigh quotient is positive. This | ||
| // removes all bound states from the Chebyshev interval and makes the | ||
| // Fermi-level search fail at high temperature. Correct only that | ||
| // pathological final bound; negative bounds are left untouched so | ||
| // normal SDFT results do not change. | ||
| if (!std::isfinite(*p_hamilt_sto->emin) || *p_hamilt_sto->emin > 0) | ||
| { | ||
| const double safe_emin = -std::max(5.0, 0.01 * std::abs(*p_hamilt_sto->emax)); | ||
| GlobalV::ofs_running << " WARNING: positive automatic Chebyshev Emin " | ||
| << *p_hamilt_sto->emin << " Ry is reset to " << safe_emin << " Ry" << std::endl; | ||
| *p_hamilt_sto->emin = safe_emin; | ||
|
Comment on lines
+223
to
+228
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Preserve valid positive trial bounds This branch also replaces an explicitly supplied positive |
||
| // The same pathological Krylov path also underestimates the upper | ||
| // bound. The highest plane-wave eigenvalue cannot exceed roughly | ||
| // ecutwfc, so use the cutoff as a safe floor. | ||
| const double ecut_floor = 1.05 * PARAM.inp.ecutwfc; | ||
| if (PARAM.inp.ecutwfc > 0 && *p_hamilt_sto->emax < ecut_floor) | ||
|
Comment on lines
+232
to
+233
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking: the exception rationale also needs to cover the four new |
||
| { | ||
| GlobalV::ofs_running << " WARNING: Chebyshev Emax " << *p_hamilt_sto->emax | ||
| << " Ry is raised to the ecutwfc-based floor " << ecut_floor << " Ry" << std::endl; | ||
| *p_hamilt_sto->emax = ecut_floor; | ||
| } | ||
| } | ||
| if (change) | ||
| { | ||
| GlobalV::ofs_running << "New Emax Ry" << *p_hamilt_sto->emax << " ; new Emin " << *p_hamilt_sto->emin | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking: register the scalar entropy tests outside the
ENABLE_MPIguard. Their own CMake file disables MPI and they exercise no parallel code, but this placement prevents MPI-off builds from running the new regression.