Commit ec05f1cd authored by Peter Eastman's avatar Peter Eastman
Browse files

Energy minimizer is more robust when constraints are present

parent 51ddfb26
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2010 Stanford University and the Authors. * * Portions copyright (c) 2010-2011 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -111,28 +111,28 @@ void LocalEnergyMinimizer::minimize(Context& context, double tolerance, int maxI ...@@ -111,28 +111,28 @@ void LocalEnergyMinimizer::minimize(Context& context, double tolerance, int maxI
param.max_iterations = maxIterations; param.max_iterations = maxIterations;
param.linesearch = LBFGS_LINESEARCH_BACKTRACKING_STRONG_WOLFE; param.linesearch = LBFGS_LINESEARCH_BACKTRACKING_STRONG_WOLFE;
// Repeatedly minimize, steadily increasing the strength of the springs until all constraints are satisfied. // Make sure the initial configuration satisfies all constraints.
double prevMaxError = 1e10; context.applyConstraints(constraintTol);
while (true) {
// Make sure the initial configuration satisfies all constraints.
context.applyConstraints(constraintTol); // Record the initial positions and determine a normalization constant for scaling the tolerance.
// Record the initial positions and determine a normalization constant for scaling the tolerance. vector<Vec3> initialPos = context.getState(State::Positions).getPositions();
double norm = 0.0;
for (int i = 0; i < numParticles; i++) {
x[3*i] = initialPos[i][0];
x[3*i+1] = initialPos[i][1];
x[3*i+2] = initialPos[i][2];
norm += initialPos[i].dot(initialPos[i]);
}
norm /= numParticles;
norm = (norm < 1 ? 1 : sqrt(norm));
param.epsilon = tolerance/norm;
vector<Vec3> positions = context.getState(State::Positions).getPositions(); // Repeatedly minimize, steadily increasing the strength of the springs until all constraints are satisfied.
double norm = 0.0;
for (int i = 0; i < numParticles; i++) {
x[3*i] = positions[i][0];
x[3*i+1] = positions[i][1];
x[3*i+2] = positions[i][2];
norm += positions[i].dot(positions[i]);
}
norm /= numParticles;
norm = (norm < 1 ? 1 : sqrt(norm));
param.epsilon = tolerance/norm;
double prevMaxError = 1e10;
while (true) {
// Perform the minimization. // Perform the minimization.
lbfgsfloatval_t fx; lbfgsfloatval_t fx;
...@@ -141,7 +141,7 @@ void LocalEnergyMinimizer::minimize(Context& context, double tolerance, int maxI ...@@ -141,7 +141,7 @@ void LocalEnergyMinimizer::minimize(Context& context, double tolerance, int maxI
// Check whether all constraints are satisfied. // Check whether all constraints are satisfied.
positions = context.getState(State::Positions).getPositions(); vector<Vec3> positions = context.getState(State::Positions).getPositions();
int numConstraints = system.getNumConstraints(); int numConstraints = system.getNumConstraints();
double maxError = 0.0; double maxError = 0.0;
for (int i = 0; i < numConstraints; i++) { for (int i = 0; i < numConstraints; i++) {
...@@ -156,13 +156,9 @@ void LocalEnergyMinimizer::minimize(Context& context, double tolerance, int maxI ...@@ -156,13 +156,9 @@ void LocalEnergyMinimizer::minimize(Context& context, double tolerance, int maxI
} }
if (maxError <= constraintTol) if (maxError <= constraintTol)
break; // All constraints are satisfied. break; // All constraints are satisfied.
if (maxError >= prevMaxError) { context.setPositions(initialPos);
// Further tightening the springs doesn't seem to be helping, so just to a final if (maxError >= prevMaxError)
// constraint application and return. break; // Further tightening the springs doesn't seem to be helping, so just give up.
context.applyConstraints(constraintTol);
break;
}
prevMaxError = maxError; prevMaxError = maxError;
k *= 10; k *= 10;
} }
......
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