Skip to content

Commit daaa758

Browse files
authored
Merge pull request #460 from toxa81/develop
debug floating-point exceptions
2 parents 599421c + 553b62c commit daaa758

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

apps/dft_loop/sirius.scf.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "utils/profiler.hpp"
22
#include <sirius.h>
33
#include <utils/json.hpp>
4+
#include <cfenv>
5+
#include <fenv.h>
46

57
using namespace sirius;
68
using json = nlohmann::json;
@@ -334,13 +336,15 @@ void run_tasks(cmd_args const& args)
334336

335337
int main(int argn, char** argv)
336338
{
339+
std::feclearexcept(FE_ALL_EXCEPT);
337340
cmd_args args;
338341
args.register_key("--input=", "{string} input file name");
339342
args.register_key("--output=", "{string} output file name");
340343
args.register_key("--task=", "{int} task id");
341344
args.register_key("--aiida_output", "write output for AiiDA");
342345
args.register_key("--test_against=", "{string} json file with reference values");
343346
args.register_key("--repeat_update=", "{int} number of times to repeat update()");
347+
args.register_key("--fpe", "enable check of floating-point exceptions using GNUC library");
344348
args.register_key("--control.processing_unit=", "");
345349
args.register_key("--control.verbosity=", "");
346350
args.register_key("--control.verification=", "");
@@ -361,6 +365,12 @@ int main(int argn, char** argv)
361365
return 0;
362366
}
363367

368+
#if defined(_GNU_SOURCE)
369+
if (args.exist("fpe")) {
370+
feenableexcept(FE_DIVBYZERO|FE_INVALID|FE_OVERFLOW);
371+
}
372+
#endif
373+
364374
sirius::initialize(1);
365375

366376
run_tasks(args);
@@ -375,6 +385,18 @@ int main(int argn, char** argv)
375385
std::ofstream ofs("timers.json", std::ofstream::out | std::ofstream::trunc);
376386
ofs << timing_result.json();
377387
}
388+
if (std::fetestexcept(FE_DIVBYZERO)) {
389+
std::cout << "FE_DIVBYZERO exception\n";
390+
}
391+
if (std::fetestexcept(FE_INVALID)) {
392+
std::cout << "FE_INVALID exception\n";
393+
}
394+
if (std::fetestexcept(FE_UNDERFLOW)) {
395+
std::cout << "FE_UNDERFLOW exception\n";
396+
}
397+
if (std::fetestexcept(FE_OVERFLOW)) {
398+
std::cout << "FE_OVERFLOW exception\n";
399+
}
378400

379401
return 0;
380402
}

src/Potential/poisson.cpp

+19-15
Original file line numberDiff line numberDiff line change
@@ -103,34 +103,38 @@ void Potential::poisson_add_pseudo_pw(mdarray<double_complex, 2>& qmt__,
103103
}
104104
}
105105

106+
double fourpi_omega = fourpi / unit_cell_.omega();
107+
106108
/* add pseudo_density to interstitial charge density so that rho(G) has the correct
107109
* multipole moments in the muffin-tins */
108110
#pragma omp parallel for schedule(static)
109-
for (int igloc = 0; igloc < ctx_.gvec().count(); igloc++) {
111+
for (int igloc = ctx_.gvec().skip_g0(); igloc < ctx_.gvec().count(); igloc++) {
110112
int ig = ctx_.gvec().offset() + igloc;
111113

112114
double gR = ctx_.gvec().gvec_len(ig) * R;
113115
double gRn = std::pow(2.0 / gR, pseudo_density_order_ + 1);
114116

115117
double_complex rho_G(0, 0);
116-
if (ig) { // G!=0
117-
/* loop over atoms of the same type */
118-
for (int l = 0, lm = 0; l <= ctx_.lmax_rho(); l++) {
119-
double_complex zt1(0, 0);
120-
for (int m = -l; m <= l; m++, lm++) {
121-
zt1 += gvec_ylm_(lm, igloc) * qapf(lm, igloc);
122-
}
123-
rho_G += (fourpi / unit_cell_.omega()) * std::conj(zil_[l]) * zt1 * gamma_factors_R_(l, iat) *
124-
sbessel_mt_(l + pseudo_density_order_ + 1, igloc, iat) * gRn;
125-
} // l
126-
} else { // G=0
127-
for (int i = 0; i < unit_cell_.atom_type(iat).num_atoms(); i++) {
128-
int ia = unit_cell_.atom_type(iat).atom_id(i);
129-
rho_G += (fourpi / unit_cell_.omega()) * y00 * (qmt__(0, ia) - qit__(0, ia));
118+
/* loop over atoms of the same type */
119+
for (int l = 0, lm = 0; l <= ctx_.lmax_rho(); l++) {
120+
double_complex zt1(0, 0);
121+
for (int m = -l; m <= l; m++, lm++) {
122+
zt1 += gvec_ylm_(lm, igloc) * qapf(lm, igloc);
130123
}
124+
rho_G += fourpi_omega * std::conj(zil_[l]) * zt1 * gamma_factors_R_(l, iat) *
125+
sbessel_mt_(l + pseudo_density_order_ + 1, igloc, iat) * gRn;
131126
}
132127
rho_pw__[igloc] += rho_G;
133128
}
129+
/* for G=0 case */
130+
if (ctx_.comm().rank() == 0) {
131+
double_complex rho_G(0, 0);
132+
for (int i = 0; i < unit_cell_.atom_type(iat).num_atoms(); i++) {
133+
int ia = unit_cell_.atom_type(iat).atom_id(i);
134+
rho_G += fourpi_omega * y00 * (qmt__(0, ia) - qit__(0, ia));
135+
}
136+
rho_pw__[0] += rho_G;
137+
}
134138
}
135139
}
136140

0 commit comments

Comments
 (0)