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

Fix division by zero at geoopt #1170

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
42 changes: 26 additions & 16 deletions src/constr.f90
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,11 @@ Subroutine dphidrPBC(mode,nat,xyz,i,j,k,l,vTrR,vTrB,vTrC,phi,&
dphidrj=0
dphidrk=0
dphidrl=0
onenner=1.0d0/(nan*nbn)
if (abs(nan*nbn).gt.eps) then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth clarifying what the purpose of this check is. It seems like we would only reach this condition if for some reason the magnitude of one or both of these surface normals was very small. The earlier check already determines if the normals are nearly parallel.

Is there a specific example you have found that the code change here fixes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The earlier check already determines if the normals are nearly parallel.

You can be here if nan or nbn equal zero and then you divide by zero :)

Is there a specific example you have found that the code change here fixes?

Yep, you can detect this with input provided in #500.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised the example from #500 goes through this function. I assumed from the name it only applied for PBC structures, but the example from the issue is molecular.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

¯_(ツ)_/¯

onenner=1.0d0/(nan*nbn)
else
onenner=0.0d0
endif
else
onenner=1.d0/nenner
endif
Expand All @@ -630,21 +634,27 @@ Subroutine dphidrPBC(mode,nat,xyz,i,j,k,l,vTrR,vTrB,vTrC,phi,&
call crossprod(rbpc,na,rbpca)
call crossprod(rbpc,nb,rbpcb)

! ... dphidri
do ic=1,3
dphidri(ic)=onenner*(cosphi*nbn/nan*rab(ic)-rbb(ic))

! ... dphidrj
dphidrj(ic)=onenner*(cosphi*(nbn/nan*rapba(ic)&
& +nan/nbn*rbc(ic))&
& -(rac(ic)+rapbb(ic)))
! ... dphidrk
dphidrk(ic)=onenner*(cosphi*(nbn/nan*raa(ic)&
& +nan/nbn*rbpcb(ic))&
& -(rba(ic)+rbpca(ic)))
! ... dphidrl
dphidrl(ic)=onenner*(cosphi*nan/nbn*rbb(ic)-rab(ic))
end do
if (abs(onenner).gt.eps) then
do ic=1,3
! ... dphidri
dphidri(ic)=onenner*(cosphi*nbn/nan*rab(ic)-rbb(ic))
! ... dphidrj
dphidrj(ic)=onenner*(cosphi*(nbn/nan*rapba(ic)&
& +nan/nbn*rbc(ic))&
& -(rac(ic)+rapbb(ic)))
! ... dphidrk
dphidrk(ic)=onenner*(cosphi*(nbn/nan*raa(ic)&
& +nan/nbn*rbpcb(ic))&
& -(rba(ic)+rbpca(ic)))
! ... dphidrl
dphidrl(ic)=onenner*(cosphi*nan/nbn*rbb(ic)-rab(ic))
end do
else
dphidri=0.0d0
dphidrj=0.0d0
dphidrk=0.0d0
dphidrl=0.0d0
endif

End subroutine

Expand Down
2 changes: 1 addition & 1 deletion src/optimizer.f90
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ subroutine relax(env,iter,mol,anc,restart,maxcycle,maxdispl,ethr,gthr, &
write(env%unit,'(5x,"change ",e18.7,1x,"Eh")') echng
write(env%unit,'(3x,"gradient norm :",f14.7,1x,"Eh/α")',advance='no') gnorm
write(env%unit,'(3x,"predicted",e18.7)',advance='no') depred
write(env%unit,'(1x,"("f7.2"%)")') (depred-echng)/echng*100
write(env%unit,'(1x,"("f7.2"%)")') (depred-echng)/(echng+1e-34_wp)*100
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you determine this fixed #500? At least in the example they attached, the gradient norm is also NaN.

I would probably say to just not even write the percentage difference if the actual echng is effectively 0, rather than write an enormous value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found since I am using the same technique as for other issues. :-)

endif

! check 0 energy case !
Expand Down
Loading