Skip to content

Commit

Permalink
Cosmetic
Browse files Browse the repository at this point in the history
Slightly simplified LevelSetMesher logic
  • Loading branch information
regislebrun committed Dec 10, 2023
1 parent 586ced0 commit f2c086c
Showing 1 changed file with 10 additions and 27 deletions.
37 changes: 10 additions & 27 deletions lib/src/Base/Geom/LevelSetMesher.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -242,47 +242,30 @@ Mesh LevelSetMesher::build(const LevelSet & levelSet,
OptimizationAlgorithm solver(solver_);
solver.setStartingPoint(delta);
solver.setProblem(problem);
OptimizationResult result;
// Here we have to catch exceptions raised by the gradient
// Here we have to catch exceptions raised by the algorithm (may be due to e.g the gradient)
try
{
solver.run();
result = solver.getResult();
movedVertices.add(currentVertex + solver.getResult().getOptimalPoint());
}
catch(...)
{
LOGDEBUG(OSS() << "Problem to project point=" << currentVertex << " with solver=" << solver_ << ", using finite differences for gradient");
// Here we may have to fix the gradient eg in the case of analytical functions, when Ev3 does not handle the expression.
const Scalar epsilon = ResourceMap::GetAsScalar("CenteredFiniteDifferenceGradient-DefaultEpsilon");
levelFunction.setGradient(CenteredFiniteDifferenceGradient((localVertices.getMin() - localVertices.getMax()) * epsilon + Point(dimension, epsilon), levelFunction.getEvaluation()).clone());
problem.setLevelFunction(levelFunction);
solver.setProblem(problem);
// Try with the new gradients
// There is a problem with this vertex. Try a gradient-free solver
Cobyla cobyla(solver.getProblem());
cobyla.setStartingPoint(delta);
LOGDEBUG(OSS() << "Problem to project point=" << currentVertex << " with solver=" << solver << " and finite differences for gradient, switching to solver=" << cobyla);
try
{
solver.run();
result = solver.getResult();
movedVertices.add(currentVertex + result.getOptimalPoint());
}
catch(...)
{
// There is definitely a problem with this vertex. Try a gradient-free solver
Cobyla cobyla(solver.getProblem());
cobyla.setStartingPoint(solver.getStartingPoint());
LOGDEBUG(OSS() << "Problem to project point=" << currentVertex << " with solver=" << solver << " and finite differences for gradient, switching to solver=" << cobyla);
try
{
cobyla.run();
result = cobyla.getResult();
movedVertices.add(currentVertex + result.getOptimalPoint());
movedVertices.add(currentVertex + cobyla.getResult().getOptimalPoint());
}
catch(...)
catch(...)
{
LOGDEBUG(OSS() << "Problem to project point=" << currentVertex << " with solver=" << cobyla << ", use basic linear interpolation");
movedVertices.add(currentVertex + delta);
}
} // Even finite differences gradient failed?
} // Gradient failed ?
} // User-defined solver failed ?
// Restore the use of solve-the-equation if we used minimization due to a difficult vertex
minimizeDistance = !solveEquation;
} // minimizeDistance
} // project
Expand Down

0 comments on commit f2c086c

Please sign in to comment.