Skip to content
Snippets Groups Projects
Commit 382ff682 authored by Oliver Sander's avatar Oliver Sander Committed by sander@FU-BERLIN.DE
Browse files

Recompute Hessian and gradient only after successful iterations.

Another easy patch which makes the code a lot faster.

Also, this patch removes the quasi-Newton code.  So far that code
hasn't helped at all.  Quasi-Newton methods will need more work
in the future.

[[Imported from SVN: r8588]]
parent 2cface63
No related branches found
No related tags found
No related merge requests found
...@@ -213,8 +213,8 @@ void RiemannianTrustRegionSolver<GridType,TargetSpace>::solve() ...@@ -213,8 +213,8 @@ void RiemannianTrustRegionSolver<GridType,TargetSpace>::solve()
// ///////////////////////////////////////////////////// // /////////////////////////////////////////////////////
double oldEnergy = assembler_->computeEnergy(x_); double oldEnergy = assembler_->computeEnergy(x_);
bool quasiNewtonMethod = false; bool recomputeGradientHessian = true;
bool recomputeHessian = true; CorrectionType rhs;
for (int i=0; i<maxTrustRegionSteps_; i++) { for (int i=0; i<maxTrustRegionSteps_; i++) {
...@@ -231,27 +231,25 @@ void RiemannianTrustRegionSolver<GridType,TargetSpace>::solve() ...@@ -231,27 +231,25 @@ void RiemannianTrustRegionSolver<GridType,TargetSpace>::solve()
std::cout << "----------------------------------------------------" << std::endl; std::cout << "----------------------------------------------------" << std::endl;
} }
CorrectionType rhs;
CorrectionType corr(x_.size()); CorrectionType corr(x_.size());
corr = 0; corr = 0;
Dune::Timer gradientTimer; Dune::Timer gradientTimer;
assembler_->assembleGradient(x_, rhs);
std::cout << "gradient assembly took " << gradientTimer.elapsed() << " sec." << std::endl;
gradientTimer.reset();
if (recomputeHessian) { if (recomputeGradientHessian) {
assembler_->assembleGradient(x_, rhs);
rhs *= -1; // The right hand side is the _negative_ gradient
std::cout << "gradient assembly took " << gradientTimer.elapsed() << " sec." << std::endl;
gradientTimer.reset();
assembler_->assembleMatrix(x_, assembler_->assembleMatrix(x_,
*hessianMatrix_, *hessianMatrix_,
i==0 // assemble occupation pattern only for the first call i==0 // assemble occupation pattern only for the first call
); );
std::cout << "hessian assembly took " << gradientTimer.elapsed() << " sec." << std::endl; std::cout << "hessian assembly took " << gradientTimer.elapsed() << " sec." << std::endl;
} else recomputeGradientHessian = false;
std::cout << "Reuse previous Hessian" << std::endl; }
// The right hand side is the _negative_ gradient
rhs *= -1;
/* std::cout << "rhs:\n" << rhs << std::endl; /* std::cout << "rhs:\n" << rhs << std::endl;
std::cout << "matrix[0][0]:\n" << (*hessianMatrix_)[0][0] << std::endl;*/ std::cout << "matrix[0][0]:\n" << (*hessianMatrix_)[0][0] << std::endl;*/
...@@ -455,8 +453,7 @@ void RiemannianTrustRegionSolver<GridType,TargetSpace>::solve() ...@@ -455,8 +453,7 @@ void RiemannianTrustRegionSolver<GridType,TargetSpace>::solve()
// current energy becomes 'oldEnergy' for the next iteration // current energy becomes 'oldEnergy' for the next iteration
oldEnergy = energy; oldEnergy = energy;
if (quasiNewtonMethod) recomputeGradientHessian = true;
recomputeHessian = false;
} else if ( (oldEnergy-energy) / modelDecrease > 0.01 } else if ( (oldEnergy-energy) / modelDecrease > 0.01
|| std::abs(oldEnergy-energy) < 1e-12) { || std::abs(oldEnergy-energy) < 1e-12) {
...@@ -466,21 +463,14 @@ void RiemannianTrustRegionSolver<GridType,TargetSpace>::solve() ...@@ -466,21 +463,14 @@ void RiemannianTrustRegionSolver<GridType,TargetSpace>::solve()
// current energy becomes 'oldEnergy' for the next iteration // current energy becomes 'oldEnergy' for the next iteration
oldEnergy = energy; oldEnergy = energy;
if (quasiNewtonMethod) recomputeGradientHessian = true;
recomputeHessian = false;
} else { } else {
// unsuccessful iteration // unsuccessful iteration
// If quasi Newton method and we have used an old matrix: // Decrease the trust-region radius
// Try again with the actual Newton matrix trustRegion.scale(0.5);
if (not recomputeHessian && quasiNewtonMethod) {
recomputeHessian = true;
} else {
// Decrease the trust-region radius
trustRegion.scale(0.5);
}
if (this->verbosity_ == NumProc::FULL) if (this->verbosity_ == NumProc::FULL)
std::cout << "Unsuccessful iteration!" << std::endl; std::cout << "Unsuccessful iteration!" << std::endl;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment