From b67d74e7bc02f6e6c2dd65d665f1d2f7dff1e1ad Mon Sep 17 00:00:00 2001 From: Salzman Alexis Date: Thu, 30 Jan 2025 16:57:29 +0100 Subject: [PATCH 1/2] New NewtonSolver._residual0 initialisation (residual) * Forces _residual0 to be set to the norm of the first residual vector when convergence_criterion=="residual". * Done after the first convergence test, so that this test can play a role in the case of multiple calls to the solve method. * Retain the same behaviour when convergence_criterion=="incremental". --- cpp/dolfinx/nls/NewtonSolver.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/cpp/dolfinx/nls/NewtonSolver.cpp b/cpp/dolfinx/nls/NewtonSolver.cpp index a631edab817..c3637b3c83b 100644 --- a/cpp/dolfinx/nls/NewtonSolver.cpp +++ b/cpp/dolfinx/nls/NewtonSolver.cpp @@ -169,7 +169,10 @@ std::pair nls::petsc::NewtonSolver::solve(Vec x) // Check convergence bool newton_converged = false; if (convergence_criterion == "residual") + { std::tie(_residual, newton_converged) = this->_converged(*this, _b); + _residual0 = _residual; + } else if (convergence_criterion == "incremental") { // We need to do at least one Newton step with the ||dx||-stopping @@ -215,13 +218,6 @@ std::pair nls::petsc::NewtonSolver::solve(Vec x) if (_system) _system(x); _fnF(x, _b); - // Initialize _residual0 - if (_iteration == 1) - { - PetscReal _r = 0.0; - VecNorm(_dx, NORM_2, &_r); - _residual0 = _r; - } // Test for convergence if (convergence_criterion == "residual") @@ -232,6 +228,9 @@ std::pair nls::petsc::NewtonSolver::solve(Vec x) // set. if (_iteration == 1) { + PetscReal _r = 0.0; + VecNorm(_dx, NORM_2, &_r); + _residual0 = _r; _residual = 1.0; newton_converged = false; } From c83d8918479e35831c1428dfab1b756287f7dcf9 Mon Sep 17 00:00:00 2001 From: Salzman Alexis Date: Sun, 16 Feb 2025 11:11:08 +0100 Subject: [PATCH 2/2] Fix indentation errors --- cpp/dolfinx/nls/NewtonSolver.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cpp/dolfinx/nls/NewtonSolver.cpp b/cpp/dolfinx/nls/NewtonSolver.cpp index c3637b3c83b..18434a2db47 100644 --- a/cpp/dolfinx/nls/NewtonSolver.cpp +++ b/cpp/dolfinx/nls/NewtonSolver.cpp @@ -228,9 +228,9 @@ std::pair nls::petsc::NewtonSolver::solve(Vec x) // set. if (_iteration == 1) { - PetscReal _r = 0.0; - VecNorm(_dx, NORM_2, &_r); - _residual0 = _r; + PetscReal _r = 0.0; + VecNorm(_dx, NORM_2, &_r); + _residual0 = _r; _residual = 1.0; newton_converged = false; }