MADNESS 0.10.1
DFConvergence.h
Go to the documentation of this file.
1#ifndef MADNESS_APPS_DIRAC_DFCONVERGENCE_H_INCLUDED
2#define MADNESS_APPS_DIRAC_DFCONVERGENCE_H_INCLUDED
3
5#include <cmath>
6#include <string>
7#include <string_view>
8
9namespace madness {
10
12
13/// Map an input keyword onto the criterion. Returns false for an unknown keyword
14/// instead of throwing. MADNESS_EXCEPTION stores the \c const \c char* it is given
15/// and does not copy it. A message built here from the keyword dangles after the
16/// throw. The caller prints the keyword, then throws.
17inline bool df_convergence_criterion_from_string(const std::string& keyword,
18 DFConvergenceCriterion& criterion) {
19 if (keyword == "bsh_residual") {
21 return true;
22 }
23 if (keyword == "energy_density_residual") {
25 return true;
26 }
27 return false;
28}
29
30inline const char* df_convergence_criterion_name(const DFConvergenceCriterion criterion) {
31 switch (criterion) {
33 return "bsh_residual";
35 return "energy_density_residual";
36 }
37 MADNESS_EXCEPTION("invalid Dirac-Fock convergence criterion", 0);
38}
39
49
51 const DFConvergenceMetrics& m) {
52 switch (criterion) {
54 return m.max_bsh_residual <= m.bsh_tolerance;
56 const double relative_energy_change =
57 m.current_total_energy == 0.0
58 ? std::abs(m.current_total_energy - m.previous_total_energy)
59 : std::abs((m.current_total_energy - m.previous_total_energy) /
60 m.current_total_energy);
61 return relative_energy_change <= m.energy_tolerance &&
62 m.density_residual <= m.density_tolerance &&
63 m.max_bsh_residual <= 100.0 * m.bsh_tolerance;
64 }
65 }
66 MADNESS_EXCEPTION("invalid Dirac-Fock convergence criterion", 0);
67}
68
69/// Why the SCF loop stopped. The message text is separate from the print, so a
70/// unit test can assert the exact wording.
72
73inline std::string_view df_stop_message(const DFStopReason reason) {
74 switch (reason) {
76 return "Converged due to residuals";
78 return "Converged due to energy, density, and residuals";
80 return "WARNING: maximum iterations reached without convergence";
81 }
82 MADNESS_EXCEPTION("invalid Dirac-Fock stop reason", 0);
83}
84
85} // namespace madness
86
87#endif
Defines madness::MadnessException for exception handling.
#define MADNESS_EXCEPTION(msg, value)
Macro for throwing a MADNESS exception.
Definition madness_exception.h:119
Namespace for all elements and tools of MADNESS.
Definition DFConvergence.h:9
DFStopReason
Definition DFConvergence.h:71
const char * df_convergence_criterion_name(const DFConvergenceCriterion criterion)
Definition DFConvergence.h:30
std::string_view df_stop_message(const DFStopReason reason)
Definition DFConvergence.h:73
DFConvergenceCriterion
Definition DFConvergence.h:11
bool df_iteration_converged(const DFConvergenceCriterion criterion, const DFConvergenceMetrics &m)
Definition DFConvergence.h:50
bool df_convergence_criterion_from_string(const std::string &keyword, DFConvergenceCriterion &criterion)
Definition DFConvergence.h:17
static long abs(long a)
Definition tensor.h:219
static const double m
Definition relops.cc:9
Definition DFConvergence.h:40
double density_residual
Definition DFConvergence.h:44
double previous_total_energy
Definition DFConvergence.h:42
double energy_tolerance
Definition DFConvergence.h:43
double max_bsh_residual
Definition DFConvergence.h:46
double density_tolerance
Definition DFConvergence.h:45
double current_total_energy
Definition DFConvergence.h:41
double bsh_tolerance
Definition DFConvergence.h:47