-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Add functionality to have user-specific convergence criteria for nonlinear solvers #341
Draft
tarun-mitruka
wants to merge
5
commits into
main
Choose a base branch
from
feature/convergenceCriteria
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// SPDX-FileCopyrightText: 2021-2024 The Ikarus Developers [email protected] | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
|
||
/** | ||
* \file convergencecriteria.hh | ||
* \brief A collection of convergence criteria used by the nonlinear solvers. | ||
*/ | ||
|
||
#include <dune/common/float_cmp.hh> | ||
|
||
#include <ikarus/controlroutines/pathfollowingfunctions.hh> | ||
#include <ikarus/utils/defaultfunctions.hh> | ||
#include <ikarus/utils/linearalgebrahelper.hh> | ||
#include <ikarus/utils/makeenum.hh> | ||
|
||
#pragma once | ||
|
||
namespace Ikarus { | ||
|
||
/** | ||
* \enum PreConditioner | ||
* \brief Enumeration of available preconditioners for the trust region solver. | ||
*/ | ||
enum class PreConditioner | ||
{ | ||
IncompleteCholesky, | ||
IdentityPreconditioner, | ||
DiagonalPreconditioner | ||
}; | ||
|
||
MAKE_ENUM(ConvergenceCriterion, ResiduumNorm, CorrectionNorm, MaximumOfCorrectionVector); | ||
|
||
template <typename NLO, ConvergenceCriterion CC = ConvergenceCriterion::ResiduumNorm, | ||
typename LS = utils::SolverDefault, typename UF = utils::UpdateDefault> | ||
class NewtonRaphson; | ||
|
||
template <typename NLO, ConvergenceCriterion CC = ConvergenceCriterion::ResiduumNorm, | ||
typename LS = utils::SolverDefault, typename UF = utils::UpdateDefault> | ||
class NewtonRaphsonWithSubsidiaryFunction; | ||
|
||
template <typename NLO, PreConditioner preConditioner = PreConditioner::IncompleteCholesky, | ||
typename UF = utils::UpdateDefault> | ||
class TrustRegion; | ||
|
||
template <ConvergenceCriterion CC> | ||
requires(CC != ConvergenceCriterion::BEGIN and CC != ConvergenceCriterion::END) | ||
struct NRConvergenceCriteria | ||
{ | ||
template <typename NLO, typename NSS, typename CV> | ||
bool operator()(const NLO& nonLinearOperator, const NSS& solverSettings, const CV& correctionVector) { | ||
if constexpr (CC == ConvergenceCriterion::ResiduumNorm) { | ||
const auto& rx = nonLinearOperator.value(); | ||
auto rNorm = norm(rx); | ||
return Dune::FloatCmp::le(static_cast<double>(rNorm), solverSettings.tol); | ||
} else if constexpr (CC == ConvergenceCriterion::CorrectionNorm) { | ||
auto dNorm = norm(correctionVector); | ||
return Dune::FloatCmp::le(static_cast<double>(dNorm), solverSettings.tol); | ||
} else if constexpr (CC == ConvergenceCriterion::MaximumOfCorrectionVector) { | ||
auto maxCorr = max(correctionVector); | ||
return Dune::FloatCmp::le(static_cast<double>(maxCorr), solverSettings.tol); | ||
} else { | ||
return false; // TODO Change to Dune::AlwaysFalse | ||
} | ||
} | ||
}; | ||
|
||
template <ConvergenceCriterion CC> | ||
requires(CC != ConvergenceCriterion::BEGIN and CC != ConvergenceCriterion::END) | ||
struct NRWSFConvergenceCriteria | ||
{ | ||
template <typename NLO, typename NSS, typename DD> | ||
bool operator()(const NLO& nonLinearOperator, const NSS& solverSettings, const DD& deltaD, double deltaLambda, | ||
const SubsidiaryArgs& subsidiaryArgs) { | ||
if constexpr (CC == ConvergenceCriterion::ResiduumNorm) { | ||
const auto& rx = nonLinearOperator.value(); | ||
Eigen::VectorXd totalResidual(rx.size() + 1); | ||
totalResidual << rx, subsidiaryArgs.f; | ||
auto rNorm = norm(totalResidual); | ||
return Dune::FloatCmp::le(static_cast<double>(rNorm), solverSettings.tol); | ||
} else if constexpr (CC == ConvergenceCriterion::CorrectionNorm) { | ||
Eigen::VectorXd correctionVector(deltaD.size() + 1); | ||
correctionVector << deltaD, deltaLambda; | ||
auto dNorm = norm(correctionVector); | ||
return Dune::FloatCmp::le(static_cast<double>(dNorm), solverSettings.tol); | ||
} else if constexpr (CC == ConvergenceCriterion::MaximumOfCorrectionVector) { | ||
Eigen::VectorXd correctionVector(deltaD.size() + 1); | ||
correctionVector << deltaD, deltaLambda; | ||
auto maxCorr = max(correctionVector); | ||
return Dune::FloatCmp::le(static_cast<double>(maxCorr), solverSettings.tol); | ||
} else { | ||
return false; // TODO Change to Dune::AlwaysFalse | ||
} | ||
} | ||
}; | ||
|
||
template <ConvergenceCriterion CC> | ||
requires(CC != ConvergenceCriterion::BEGIN and CC != ConvergenceCriterion::END) | ||
struct TRConvergenceCriteria | ||
{ | ||
template <typename NLO, typename NSS, typename CV> | ||
bool operator()(const NLO& nonLinearOperator, const NSS& solverSettings, const CV& correctionVector) { | ||
if constexpr (CC == ConvergenceCriterion::ResiduumNorm) { | ||
return false; | ||
} else if constexpr (CC == ConvergenceCriterion::CorrectionNorm) { | ||
return false; | ||
} else if constexpr (CC == ConvergenceCriterion::MaximumOfCorrectionVector) { | ||
return false; | ||
} else { | ||
return false; // TODO Change to Dune::AlwaysFalse | ||
} | ||
} | ||
}; | ||
|
||
template <typename NLS, ConvergenceCriterion CC> | ||
struct ConvergenceCriteria | ||
{ | ||
using NR = NewtonRaphson<typename NLS::NonLinearOperator, NLS::criteraType, typename NLS::LinearSolverType, | ||
typename NLS::UpdateFunctionType>; | ||
using NRWSF = NewtonRaphsonWithSubsidiaryFunction<typename NLS::NonLinearOperator, NLS::criteraType, | ||
typename NLS::LinearSolverType, typename NLS::UpdateFunctionType>; | ||
using TR = TrustRegion<typename NLS::NonLinearOperator>; | ||
|
||
using Criteria = std::conditional_t< | ||
std::is_same_v<NLS, NR>, NRConvergenceCriteria<CC>, | ||
std::conditional_t<std::is_same_v<NLS, NRWSF>, NRWSFConvergenceCriteria<CC>, TRConvergenceCriteria<CC>>>; | ||
|
||
template <typename... Args> | ||
bool operator()(Args&&... args) { | ||
return criteria(std::forward<Args>(args)...); | ||
} | ||
|
||
private: | ||
Criteria criteria{}; | ||
}; | ||
|
||
} // namespace Ikarus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.