Skip to content
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

Introduce a guard against overflow in the dense FOE #239

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions Source/Fortran/FermiOperatorModule.F90
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,12 @@ SUBROUTINE ComputeDenseFOE(H, ISQ, trace, K, inv_temp_in, &
chemical_potential = left + (right - left) / 2
DO II = 1, num_eigs
sval = eigs(II) - chemical_potential
! occ(II) = 0.5_NTREAL * (1.0_NTREAL - ERF(inv_temp * sval))
occ(II) = 1.0_NTREAL / (1.0_NTREAL + EXP(inv_temp * sval))
! Guard against overflow
IF (inv_temp * sval .GT. 30) THEN
occ(II) = 0.5_NTREAL * (1.0_NTREAL - ERF(inv_temp * sval))
ELSE
occ(II) = 1.0_NTREAL / (1.0_NTREAL + EXP(inv_temp * sval))
END IF
END DO
sv = SUM(occ)
IF (ABS(trace - sv) .LT. 1E-8_NTREAL) THEN
Expand Down Expand Up @@ -621,5 +625,24 @@ SUBROUTINE ComputeCStep(X, A, W, pool, threshold, Out)
CALL DestructMatrix(XA)

END SUBROUTINE ComputeCStep
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> ERF is a Fortran2008 feature, and we just need a crude approximation here
!! so we implement it with the Abramowitz and Stegun approximation
!! (credit ChatGPT).
FUNCTION ERF(x)
REAL(NTREAL) :: x, erf
REAL(NTREAL) :: t, z, tau
REAL(NTREAL), PARAMETER :: a1 = 0.254829592_NTREAL
REAL(NTREAL), PARAMETER :: a2 = -0.284496736_NTREAL
REAL(NTREAL), PARAMETER :: a3 = 1.421413741_NTREAL
REAL(NTREAL), PARAMETER :: a4 = -1.453152027_NTREAL
REAL(NTREAL), PARAMETER :: a5 = 1.061405429_NTREAL
REAL(NTREAL), PARAMETER :: p = 0.3275911_NTREAL

z = ABS(x)
t = 1.0_NTREAL / (1.0_NTREAL + p * z)
tau = t * (a1 + t * (a2 + t * (a3 + t * (a4 + t * a5))))
erf = SIGN(1.0_NTREAL, x) * (1.0_NTREAL - tau * EXP(-z * z))
END FUNCTION ERF
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
END MODULE FermiOperatorModule
Loading