|
| 1 | +#include "source_lcao/module_extrap/wf_extrap_lcao.h" |
| 2 | + |
| 3 | +#include "source_base/global_function.h" |
| 4 | +#include "source_base/global_variable.h" |
| 5 | +#include "source_base/timer.h" |
| 6 | +#include "source_base/tool_quit.h" |
| 7 | +#include "source_estate/module_charge/charge.h" |
| 8 | +#include "source_estate/module_dm/cal_dm_psi.h" |
| 9 | +#include "source_estate/module_dm/density_matrix.h" |
| 10 | +#include "source_lcao/hamilt_lcao.h" |
| 11 | +#include "source_lcao/module_operator_lcao/operator_lcao.h" |
| 12 | +#include "source_lcao/rho_tau_lcao.h" |
| 13 | + |
| 14 | +#include <complex> |
| 15 | +#include <iomanip> |
| 16 | +#include <sstream> |
| 17 | + |
| 18 | +namespace |
| 19 | +{ |
| 20 | + |
| 21 | +std::string wfc_extrapolation_failure_message(const ModuleExtrap::WfExtrapApplyResult& result) |
| 22 | +{ |
| 23 | + std::ostringstream oss; |
| 24 | + oss << std::scientific << std::setprecision(16); |
| 25 | + oss << "WFN extrapolation failed with status: " << ModuleExtrap::to_string(result.status) << "\n\n" |
| 26 | + << " snapshot_istep=" << result.snapshot_istep << "\n" |
| 27 | + << " failed_state=" << result.failed_state << "\n" |
| 28 | + << " failed_pivot_index=" << result.failed_pivot_index << "\n" |
| 29 | + << " failed_pivot=" << result.failed_pivot << "\n" |
| 30 | + << " nstate=" << result.nstate << "\n" |
| 31 | + << " nbands=" << result.nbands << "\n" |
| 32 | + << " nbasis=" << result.nbasis << "\n" |
| 33 | + << " min_metric_diag=" << result.min_metric_diag << "\n" |
| 34 | + << " max_metric_diag=" << result.max_metric_diag << "\n" |
| 35 | + << " max_metric_abs=" << result.max_metric_abs << "\n" |
| 36 | + << " max_metric_asymmetry=" << result.max_metric_asymmetry << "\n" |
| 37 | + << " max_orthonormality_deviation=" << result.max_orthonormality_deviation << "\n"; |
| 38 | + return oss.str(); |
| 39 | +} |
| 40 | + |
| 41 | +} // namespace |
| 42 | + |
| 43 | +namespace ModuleExtrap |
| 44 | +{ |
| 45 | + |
| 46 | +template <typename TK> |
| 47 | +std::unique_ptr<WfHistoryLCAO<TK>> make_wf_history_lcao(const std::string& method) |
| 48 | +{ |
| 49 | + const WfcExtrapMethod wfc_extrap_method = wfc_extrap_method_from_string(method); |
| 50 | + if (wfc_extrap_method == WfcExtrapMethod::None) |
| 51 | + { |
| 52 | + return std::unique_ptr<WfHistoryLCAO<TK>>(); |
| 53 | + } |
| 54 | + |
| 55 | + GlobalV::ofs_running << " WFN extrapolation method: " << to_string(wfc_extrap_method) << std::endl; |
| 56 | + return std::unique_ptr<WfHistoryLCAO<TK>>(new WfHistoryLCAO<TK>(wfc_extrap_method)); |
| 57 | +} |
| 58 | + |
| 59 | +bool initialize_gamma_density_from_history(WfHistoryLCAO<double>* const history, |
| 60 | + hamilt::HamiltLCAO<double, double>& hamilt_lcao, |
| 61 | + const Parallel_Orbitals& pv, |
| 62 | + psi::Psi<double>& psi, |
| 63 | + const ModuleBase::matrix& wg, |
| 64 | + elecstate::DensityMatrix<double, double>& density_matrix, |
| 65 | + Charge& charge, |
| 66 | + const int nspin, |
| 67 | + const std::string& ks_solver) |
| 68 | +{ |
| 69 | + if (history == nullptr) |
| 70 | + { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + // S(R) is allocated with HamiltLCAO, but its values are normally filled later |
| 75 | + // by updateHk(). Build and fold the current overlap before restoring the WFN. |
| 76 | + auto* const lcao_op = dynamic_cast<hamilt::OperatorLCAO<double, double>*>(hamilt_lcao.getOperator()); |
| 77 | + if (lcao_op == nullptr) |
| 78 | + { |
| 79 | + ModuleBase::WARNING_QUIT("ModuleExtrap::initialize_gamma_density_from_history", |
| 80 | + "Failed to access the LCAO operator chain for WFN extrapolation."); |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + ModuleBase::timer::start("WFN_Extrap", "prepare_overlap"); |
| 85 | + lcao_op->contributeHR(); |
| 86 | + const int sk_layout = ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(ks_solver) ? 1 : 0; |
| 87 | + hamilt_lcao.updateSk(0, sk_layout); |
| 88 | + ModuleBase::timer::end("WFN_Extrap", "prepare_overlap"); |
| 89 | + |
| 90 | + ModuleBase::timer::start("WFN_Extrap", "apply"); |
| 91 | + const WfExtrapApplyResult result |
| 92 | + = history->try_use_prev_wf_gamma(hamilt_lcao.getSk(), pv, psi, wg); |
| 93 | + ModuleBase::timer::end("WFN_Extrap", "apply"); |
| 94 | + if (!result.ok()) |
| 95 | + { |
| 96 | + ModuleBase::WARNING_QUIT("ModuleExtrap::initialize_gamma_density_from_history", |
| 97 | + wfc_extrapolation_failure_message(result)); |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + ModuleBase::timer::start("WFN_Extrap", "rebuild_density"); |
| 102 | + elecstate::cal_dm_psi(density_matrix.get_paraV_pointer(), wg, psi, density_matrix); |
| 103 | + density_matrix.cal_DMR(); |
| 104 | + LCAO_domain::dm2rho(density_matrix.get_DMR_vector(), nspin, &charge); |
| 105 | + ModuleBase::timer::end("WFN_Extrap", "rebuild_density"); |
| 106 | + |
| 107 | + GlobalV::ofs_running << " WFN extrapolation: use_prev_wf from ionic step " << result.snapshot_istep |
| 108 | + << ", active bands = " << result.nactive_bands |
| 109 | + << ", max |C^T S C - I| = " << result.max_orthonormality_deviation << std::endl; |
| 110 | + return true; |
| 111 | +} |
| 112 | + |
| 113 | +template <typename TK, typename TR> |
| 114 | +bool initialize_gamma_density_from_history(WfHistoryLCAO<TK>* const history, |
| 115 | + hamilt::HamiltLCAO<TK, TR>&, |
| 116 | + const Parallel_Orbitals&, |
| 117 | + psi::Psi<TK>&, |
| 118 | + const ModuleBase::matrix&, |
| 119 | + elecstate::DensityMatrix<TK, TR>&, |
| 120 | + Charge&, |
| 121 | + const int, |
| 122 | + const std::string&) |
| 123 | +{ |
| 124 | + if (history == nullptr) |
| 125 | + { |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + ModuleBase::WARNING_QUIT("ModuleExtrap::initialize_gamma_density_from_history", |
| 130 | + "WFN extrapolation is currently supported only for the real Gamma-only NAO path."); |
| 131 | + return false; |
| 132 | +} |
| 133 | + |
| 134 | +template <typename TK> |
| 135 | +void update_wf_history_after_scf(WfHistoryLCAO<TK>* const history, |
| 136 | + const bool converged, |
| 137 | + const int istep, |
| 138 | + const psi::Psi<TK>* const psi, |
| 139 | + const ModuleBase::matrix& wg) |
| 140 | +{ |
| 141 | + if (converged && history != nullptr && psi != nullptr) |
| 142 | + { |
| 143 | + history->update_after_scf(istep, *psi, wg); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +template std::unique_ptr<WfHistoryLCAO<double>> make_wf_history_lcao<double>(const std::string& method); |
| 148 | +template std::unique_ptr<WfHistoryLCAO<std::complex<double>>> |
| 149 | +make_wf_history_lcao<std::complex<double>>(const std::string& method); |
| 150 | + |
| 151 | +template bool initialize_gamma_density_from_history<std::complex<double>, double>( |
| 152 | + WfHistoryLCAO<std::complex<double>>* history, |
| 153 | + hamilt::HamiltLCAO<std::complex<double>, double>& hamilt_lcao, |
| 154 | + const Parallel_Orbitals& pv, |
| 155 | + psi::Psi<std::complex<double>>& psi, |
| 156 | + const ModuleBase::matrix& wg, |
| 157 | + elecstate::DensityMatrix<std::complex<double>, double>& density_matrix, |
| 158 | + Charge& charge, |
| 159 | + int nspin, |
| 160 | + const std::string& ks_solver); |
| 161 | + |
| 162 | +template bool initialize_gamma_density_from_history<std::complex<double>, std::complex<double>>( |
| 163 | + WfHistoryLCAO<std::complex<double>>* history, |
| 164 | + hamilt::HamiltLCAO<std::complex<double>, std::complex<double>>& hamilt_lcao, |
| 165 | + const Parallel_Orbitals& pv, |
| 166 | + psi::Psi<std::complex<double>>& psi, |
| 167 | + const ModuleBase::matrix& wg, |
| 168 | + elecstate::DensityMatrix<std::complex<double>, std::complex<double>>& density_matrix, |
| 169 | + Charge& charge, |
| 170 | + int nspin, |
| 171 | + const std::string& ks_solver); |
| 172 | + |
| 173 | +template void update_wf_history_after_scf<double>(WfHistoryLCAO<double>* history, |
| 174 | + bool converged, |
| 175 | + int istep, |
| 176 | + const psi::Psi<double>* psi, |
| 177 | + const ModuleBase::matrix& wg); |
| 178 | +template void update_wf_history_after_scf<std::complex<double>>( |
| 179 | + WfHistoryLCAO<std::complex<double>>* history, |
| 180 | + bool converged, |
| 181 | + int istep, |
| 182 | + const psi::Psi<std::complex<double>>* psi, |
| 183 | + const ModuleBase::matrix& wg); |
| 184 | + |
| 185 | +} // namespace ModuleExtrap |
0 commit comments