Commit 2855d12e authored by Peter Eastman's avatar Peter Eastman
Browse files

More robust against numerical error

parent f6a8d1ea
...@@ -7207,7 +7207,11 @@ double OpenCLCalcRMSDForceKernel::executeImpl(ContextImpl& context) { ...@@ -7207,7 +7207,11 @@ double OpenCLCalcRMSDForceKernel::executeImpl(ContextImpl& context) {
// Compute the RMSD. // Compute the RMSD.
double msd = (sumNormRef+b[9]-2*values[3])/numParticles; double msd = (sumNormRef+b[9]-2*values[3])/numParticles;
msd = max(0.0, msd); // Protect against numerical error when the particles are perfectly aligned. if (msd < 1e-20) {
// The particles are perfectly aligned, so all the forces should be zero.
// Numerical error can lead to NaNs, so just return 0 now.
return 0.0;
}
double rmsd = sqrt(msd); double rmsd = sqrt(msd);
b[9] = rmsd; b[9] = rmsd;
......
...@@ -97,7 +97,11 @@ double ReferenceRMSDForce::calculateIxn(vector<Vec3>& atomCoordinates, vector<Ve ...@@ -97,7 +97,11 @@ double ReferenceRMSDForce::calculateIxn(vector<Vec3>& atomCoordinates, vector<Ve
sum += positions[i].dot(positions[i]) + referencePos[index].dot(referencePos[index]); sum += positions[i].dot(positions[i]) + referencePos[index].dot(referencePos[index]);
} }
double msd = (sum-2*values[3])/numParticles; double msd = (sum-2*values[3])/numParticles;
msd = max(0.0, msd); // Protect against numerical error when the particles are perfectly aligned. if (msd < 1e-20) {
// The particles are perfectly aligned, so all the forces should be zero.
// Numerical error can lead to NaNs, so just return 0 now.
return 0.0;
}
double rmsd = sqrt(msd); double rmsd = sqrt(msd);
// Compute the rotation matrix. // Compute the rotation matrix.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment