Skip to content

Commit

Permalink
Merge pull request #1229 from pmgbergen:maint-omit-redundant-residual…
Browse files Browse the repository at this point in the history
…-computations

Turn off redundant residual computations if tolerance np.inf
  • Loading branch information
IvarStefansson authored Jan 31, 2025
2 parents 1345c48 + 97048ce commit 99f7e65
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
20 changes: 14 additions & 6 deletions src/porepy/models/solution_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def after_simulation(self) -> None:
def check_convergence(
self,
nonlinear_increment: np.ndarray,
residual: np.ndarray,
residual: Optional[np.ndarray],
reference_residual: np.ndarray,
nl_params: dict[str, Any],
) -> tuple[bool, bool]:
Expand All @@ -487,7 +487,7 @@ def check_convergence(
Parameters:
nonlinear_increment: Newly obtained solution increment vector
residual: Residual vector of non-linear system, evaluated at the newly
obtained solution vector.
obtained solution vector. Potentially None, if not needed.
reference_residual: Reference residual vector of non-linear system,
evaluated for the initial guess at current time step.
nl_params: Dictionary of parameters used for the convergence check.
Expand Down Expand Up @@ -530,8 +530,14 @@ def check_convergence(
f"Nonlinear residual norm: {residual_norm:.2e}"
)
# Check convergence requiring both the increment and residual to be small.
converged_inc = nonlinear_increment_norm < nl_params["nl_convergence_tol"]
converged_res = residual_norm < nl_params["nl_convergence_tol_res"]
converged_inc = (
nl_params["nl_convergence_tol"] is np.inf
or nonlinear_increment_norm < nl_params["nl_convergence_tol"]
)
converged_res = (
nl_params["nl_convergence_tol_res"] is np.inf
or residual_norm < nl_params["nl_convergence_tol_res"]
)
converged = converged_inc and converged_res
diverged = False

Expand All @@ -543,7 +549,7 @@ def check_convergence(
return converged, diverged

def compute_residual_norm(
self, residual: np.ndarray, reference_residual: np.ndarray
self, residual: Optional[np.ndarray], reference_residual: np.ndarray
) -> float:
"""Compute the residual norm for a nonlinear iteration.
Expand All @@ -553,9 +559,11 @@ def compute_residual_norm(
allowing for definiting relative criteria.
Returns:
float: Residual norm.
float: Residual norm; np.nan if the residual is None.
"""
if residual is None:
return np.nan
residual_norm = np.linalg.norm(residual) / np.sqrt(residual.size)
return residual_norm

Expand Down
11 changes: 8 additions & 3 deletions src/porepy/numerics/nonlinear/nonlinear_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,14 @@ def newton_step() -> None:
nonlinear_increment = self.iteration(model)

model.after_nonlinear_iteration(nonlinear_increment)
# Note: The residual is extracted after the solution has been updated by the
# after_nonlinear_iteration() method.
residual = model.equation_system.assemble(evaluate_jacobian=False)
if self.params["nl_convergence_tol_res"] is not np.inf:
# Note: The residual is extracted after the solution has been updated by
# the after_nonlinear_iteration() method. This is only required if the
# residual is used to check convergence, i.e., the tolerance is not
# np.inf.
residual = model.equation_system.assemble(evaluate_jacobian=False)
else:
residual = None

is_converged, is_diverged = model.check_convergence(
nonlinear_increment, residual, reference_residual, self.params
Expand Down

0 comments on commit 99f7e65

Please sign in to comment.