Rounding error in acos function
Posted: Thu Dec 12, 2019 1:05 pm
Hi,
I was working with the CustomHbond force using an acos function to calculate the angle between two vectors. In order to compute the angle between two vectors I used the function:
t3 = acos(cost3lim);
cost3 = sin(t1)*sin(t2)*cos(phi)-cos(t1)*cos(t2);
This function shouldn't be more than 1 or less than -1 but I noticed that I was getting the following error frequently during my simulation:
Particle coordinate is nan
Which may have happened because of a rounding error. I tried to add a limit to this function using -1 and 1 but it didn't fix the problem.
t3 = acos(cost3lim);
cost3lim = min(max(cost3,-1),1);
cost3 = sin(t1)*sin(t2)*cos(phi)-cos(t1)*cos(t2);
Nevertheless restricting the limit further to 0.9999 fixes it:
t3 = acos(cost3lim);
cost3lim = min(max(cost3,-0.9999),0.9999);
cost3 = sin(t1)*sin(t2)*cos(phi)-cos(t1)*cos(t2);
This problem seems to be depend on the force used (CustomNonBonded vs CustomHbond) and the platform (CPU vs OpenCL). I include a small example that reproduces the problem using 3 types of forces:
"CustomNonbondedForce2": Only contains the acos function
nonbondedForce=simtk.openmm.CustomNonbondedForce(f"acos({lim})")
"CustomNonbondedForce": Contains the acos function and the min/max
nonbondedForce=simtk.openmm.CustomNonbondedForce(f"""acos(cost3lim);
cost3lim = min(max(cost3,-{lim}),{lim});
cost3=4*(r-1)^2-2;""")
"CustomHbondForce": Contains the acos function and the min/max and uses CustomHbond instead of CustomNonbondedForce
nonbondedForce=simtk.openmm.CustomHbondForce(f"""acos(cost3lim);
cost3lim = min(max(cost3,-{lim}),{lim});
cost3=4*(r-1)^2-2;
r=distance(a1,d1)""")
I include the output with annotations here:
Running: Simulation in platform CPU using force CustomNonbondedForce2 with limit 0.9999
(Completed)
Running: Simulation in platform CPU using force CustomNonbondedForce with limit 0.9999
(Completed)
Running: Simulation in platform CPU using force CustomHbondForce with limit 0.9999
(Completed)
Running: Simulation in platform OpenCL using force CustomNonbondedForce2 with limit 0.9999
(Completed)
Running: Simulation in platform OpenCL using force CustomNonbondedForce with limit 0.9999
(Completed)
Running: Simulation in platform OpenCL using force CustomHbondForce with limit 0.9999
(Completed)
Running: Simulation in platform Reference using force CustomNonbondedForce2 with limit 0.9999
(Completed)
Running: Simulation in platform Reference using force CustomNonbondedForce with limit 0.9999
(Completed)
Running: Simulation in platform Reference using force CustomHbondForce with limit 0.9999
(Completed)
Running: Simulation in platform CPU using force CustomNonbondedForce2 with limit 0.99999999
(Completed)
Running: Simulation in platform CPU using force CustomNonbondedForce with limit 0.99999999
(Completed)
Running: Simulation in platform CPU using force CustomHbondForce with limit 0.99999999
(Completed)
Running: Simulation in platform OpenCL using force CustomNonbondedForce2 with limit 0.99999999
(Completed)
Running: Simulation in platform OpenCL using force CustomNonbondedForce with limit 0.99999999
(Completed)
Running: Simulation in platform OpenCL using force CustomHbondForce with limit 0.99999999
=====Failed=====
Particle position is NaN
[*]CustomHBond force fails with a different limit
Running: Simulation in platform Reference using force CustomNonbondedForce2 with limit 0.99999999
(Completed)
Running: Simulation in platform Reference using force CustomNonbondedForce with limit 0.99999999
(Completed)
Running: Simulation in platform Reference using force CustomHbondForce with limit 0.99999999
(Completed)
Running: Simulation in platform CPU using force CustomNonbondedForce2 with limit 1
(Completed)
Running: Simulation in platform CPU using force CustomNonbondedForce with limit 1
=====Failed=====
Particle coordinate is nan
[*]The problem seems to occur after including the max/min functions
Running: Simulation in platform CPU using force CustomHbondForce with limit 1
=====Failed=====
Particle coordinate is nan
Running: Simulation in platform OpenCL using force CustomNonbondedForce2 with limit 1
(Completed)
Running: Simulation in platform OpenCL using force CustomNonbondedForce with limit 1
(Completed)
[*]The problem on CustomNonbondedForce doesn't happen using OpenCL
Running: Simulation in platform OpenCL using force CustomHbondForce with limit 1
=====Failed=====
Particle coordinate is nan
Running: Simulation in platform Reference using force CustomNonbondedForce2 with limit 1
(Completed)
Running: Simulation in platform Reference using force CustomNonbondedForce with limit 1
[*] Simulation hangs when using Reference
I was working with the CustomHbond force using an acos function to calculate the angle between two vectors. In order to compute the angle between two vectors I used the function:
t3 = acos(cost3lim);
cost3 = sin(t1)*sin(t2)*cos(phi)-cos(t1)*cos(t2);
This function shouldn't be more than 1 or less than -1 but I noticed that I was getting the following error frequently during my simulation:
Particle coordinate is nan
Which may have happened because of a rounding error. I tried to add a limit to this function using -1 and 1 but it didn't fix the problem.
t3 = acos(cost3lim);
cost3lim = min(max(cost3,-1),1);
cost3 = sin(t1)*sin(t2)*cos(phi)-cos(t1)*cos(t2);
Nevertheless restricting the limit further to 0.9999 fixes it:
t3 = acos(cost3lim);
cost3lim = min(max(cost3,-0.9999),0.9999);
cost3 = sin(t1)*sin(t2)*cos(phi)-cos(t1)*cos(t2);
This problem seems to be depend on the force used (CustomNonBonded vs CustomHbond) and the platform (CPU vs OpenCL). I include a small example that reproduces the problem using 3 types of forces:
"CustomNonbondedForce2": Only contains the acos function
nonbondedForce=simtk.openmm.CustomNonbondedForce(f"acos({lim})")
"CustomNonbondedForce": Contains the acos function and the min/max
nonbondedForce=simtk.openmm.CustomNonbondedForce(f"""acos(cost3lim);
cost3lim = min(max(cost3,-{lim}),{lim});
cost3=4*(r-1)^2-2;""")
"CustomHbondForce": Contains the acos function and the min/max and uses CustomHbond instead of CustomNonbondedForce
nonbondedForce=simtk.openmm.CustomHbondForce(f"""acos(cost3lim);
cost3lim = min(max(cost3,-{lim}),{lim});
cost3=4*(r-1)^2-2;
r=distance(a1,d1)""")
I include the output with annotations here:
Running: Simulation in platform CPU using force CustomNonbondedForce2 with limit 0.9999
(Completed)
Running: Simulation in platform CPU using force CustomNonbondedForce with limit 0.9999
(Completed)
Running: Simulation in platform CPU using force CustomHbondForce with limit 0.9999
(Completed)
Running: Simulation in platform OpenCL using force CustomNonbondedForce2 with limit 0.9999
(Completed)
Running: Simulation in platform OpenCL using force CustomNonbondedForce with limit 0.9999
(Completed)
Running: Simulation in platform OpenCL using force CustomHbondForce with limit 0.9999
(Completed)
Running: Simulation in platform Reference using force CustomNonbondedForce2 with limit 0.9999
(Completed)
Running: Simulation in platform Reference using force CustomNonbondedForce with limit 0.9999
(Completed)
Running: Simulation in platform Reference using force CustomHbondForce with limit 0.9999
(Completed)
Running: Simulation in platform CPU using force CustomNonbondedForce2 with limit 0.99999999
(Completed)
Running: Simulation in platform CPU using force CustomNonbondedForce with limit 0.99999999
(Completed)
Running: Simulation in platform CPU using force CustomHbondForce with limit 0.99999999
(Completed)
Running: Simulation in platform OpenCL using force CustomNonbondedForce2 with limit 0.99999999
(Completed)
Running: Simulation in platform OpenCL using force CustomNonbondedForce with limit 0.99999999
(Completed)
Running: Simulation in platform OpenCL using force CustomHbondForce with limit 0.99999999
=====Failed=====
Particle position is NaN
[*]CustomHBond force fails with a different limit
Running: Simulation in platform Reference using force CustomNonbondedForce2 with limit 0.99999999
(Completed)
Running: Simulation in platform Reference using force CustomNonbondedForce with limit 0.99999999
(Completed)
Running: Simulation in platform Reference using force CustomHbondForce with limit 0.99999999
(Completed)
Running: Simulation in platform CPU using force CustomNonbondedForce2 with limit 1
(Completed)
Running: Simulation in platform CPU using force CustomNonbondedForce with limit 1
=====Failed=====
Particle coordinate is nan
[*]The problem seems to occur after including the max/min functions
Running: Simulation in platform CPU using force CustomHbondForce with limit 1
=====Failed=====
Particle coordinate is nan
Running: Simulation in platform OpenCL using force CustomNonbondedForce2 with limit 1
(Completed)
Running: Simulation in platform OpenCL using force CustomNonbondedForce with limit 1
(Completed)
[*]The problem on CustomNonbondedForce doesn't happen using OpenCL
Running: Simulation in platform OpenCL using force CustomHbondForce with limit 1
=====Failed=====
Particle coordinate is nan
Running: Simulation in platform Reference using force CustomNonbondedForce2 with limit 1
(Completed)
Running: Simulation in platform Reference using force CustomNonbondedForce with limit 1
[*] Simulation hangs when using Reference