Skip to content

Commit be0a98b

Browse files
authored
Better check for exceptional floating point values (#128)
- also trap positive infinity when determining CN weights
1 parent 438dbee commit be0a98b

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/dftd4/model.f90

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ subroutine weight_references(self, mol, cn, q, gwvec, gwdcn, gwdq)
323323
expd = expd + 2*wf * (self%cn(iref, izp) - cn(iat)) * gw
324324
end do
325325
gwk = expw * norm
326-
if (ieee_is_nan(gwk)) then
326+
if (is_exceptional(gwk)) then
327327
if (maxval(self%cn(:self%ref(izp), izp)) == self%cn(iref, izp)) then
328328
gwk = 1.0_wp
329329
else
@@ -334,7 +334,7 @@ subroutine weight_references(self, mol, cn, q, gwvec, gwdcn, gwdq)
334334
gwdq(iref, iat) = gwk * dzeta(self%ga, gi, self%q(iref, izp)+zi, q(iat)+zi)
335335

336336
dgwk = norm * (expd - expw * dnorm * norm)
337-
if (ieee_is_nan(dgwk)) then
337+
if (is_exceptional(dgwk)) then
338338
dgwk = 0.0_wp
339339
end if
340340
gwdcn(iref, iat) = dgwk * zeta(self%ga, gi, self%q(iref, izp)+zi, q(iat)+zi)
@@ -368,7 +368,7 @@ subroutine weight_references(self, mol, cn, q, gwvec, gwdcn, gwdq)
368368
expw = expw + weight_cn(wf, cn(iat), self%cn(iref, izp))
369369
end do
370370
gwk = expw * norm
371-
if (ieee_is_nan(gwk)) then
371+
if (is_exceptional(gwk)) then
372372
if (maxval(self%cn(:self%ref(izp), izp)) == self%cn(iref, izp)) then
373373
gwk = 1.0_wp
374374
else
@@ -383,6 +383,14 @@ subroutine weight_references(self, mol, cn, q, gwvec, gwdcn, gwdq)
383383
end subroutine weight_references
384384

385385

386+
!> Check whether we are dealing with an exceptional value, NaN or Inf
387+
elemental function is_exceptional(val)
388+
real(wp), intent(in) :: val
389+
logical :: is_exceptional
390+
is_exceptional = ieee_is_nan(val) .or. abs(val) > huge(val)
391+
end function is_exceptional
392+
393+
386394
!> Calculate atomic dispersion coefficients and their derivatives w.r.t.
387395
!> the coordination numbers and atomic partial charges.
388396
subroutine get_atomic_c6(self, mol, gwvec, gwdcn, gwdq, c6, dc6dcn, dc6dq)

0 commit comments

Comments
 (0)