freegsnke.nk_solver_H module
Implements the core Newton Krylov nonlinear solver used by both static GS solver and evolutive solver.
Copyright 2025 UKAEA, UKRI-STFC, and The Authors, as per the COPYRIGHT and README files.
This file is part of FreeGSNKE.
FreeGSNKE is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
FreeGSNKE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with FreeGSNKE. If not, see <http://www.gnu.org/licenses/>.
- class freegsnke.nk_solver_H.nksolver(problem_dimension: int, l2_reg: float = 1e-06, collinearity_reg: float = 1e-06, verbose: bool = False)[source]
Bases:
objectImplementation of Newton Krylow algorithm for solving a generic root problem of the type F(x, other args) = 0 in the variable x – F(x) should have the same dimensions as x. Problem must be formulated so that x is a 1d np.array.
In practice, given a guess x_0 and F(x_0) = R_0 it aims to find the best step dx such that F(x_0 + dx) is minimum.
- Arnoldi_iteration(x0: ndarray, dx: ndarray, R0: ndarray, F_function: Callable[[...], ndarray], args: list, step_size: float, scaling_with_n: float, target_relative_unexplained_residual: float, max_n_directions: int, clip: float) None[source]
Perform the iteration of the NK solution method.
explores direction dx
checks what fraction of the residual can be (linearly) canceled
restarts if not satisfied
The best candidate step combining all explored directions is stored at
self.dx, with coefficients inself.coeffs.- Parameters:
x0 (np.ndarray) – The expansion point x_0, 1d of length problem_dimension.
dx (np.ndarray) – The first direction to be explored (normalised in place, then sized appropriately), 1d of length problem_dimension.
R0 (np.ndarray) – Residual of the root problem F_function at expansion point x_0, 1d of length problem_dimension.
F_function (Callable[..., np.ndarray]) – Function representing the root problem at hand.
args (list) – Additional arguments for using function F, F = F(x, *args).
step_size (float) – l2 norm of proposed step in units of the residual norm.
scaling_with_n (float) – Further scales dx candidate steps as a function of the iteration number n_it, by a factor (1 + self.n_it)**scaling_with_n.
target_relative_unexplained_residual (float) – Between 0 and 1. Terminates iteration when the unexplained fraction of the initial residual R0 (after linear cancellation) drops below this.
max_n_directions (int) – Terminates iteration even though the condition on the explained residual is not met.
clip (float) – Maximum step size for each explored direction, in units of the exploratory step dx_i.
- Arnoldi_unit(x0: ndarray, dx: ndarray, R0: ndarray, F_function: Callable[[...], ndarray], args: list, build_next: bool = True) ndarray | None[source]
Explore direction dx and propose a new direction for next exploration.
If evaluating F_function at
x0 + dxraises, the step is repeatedly shrunk by a factor 0.75 (together with the stored direction Q) until it succeeds. Updates the residual-space bases and the Hessenberg matrix.- Parameters:
x0 (np.ndarray) – The expansion point x_0, 1d of length problem_dimension.
dx (np.ndarray) – The direction to be explored (will be shrunk if needed), 1d of length problem_dimension.
R0 (np.ndarray) – Residual of the root problem F_function at expansion point x_0, 1d of length problem_dimension.
F_function (Callable[..., np.ndarray]) – Function representing the root problem at hand.
args (list) – Additional arguments for using function F, F = F(x, *args).
build_next (bool) – If True, build and return the next (orthonormal) direction to explore.
- Returns:
The normalised direction to be explored next (1d of length problem_dimension), or None if build_next is False.
- Return type:
np.ndarray or None
- __init__(problem_dimension: int, l2_reg: float = 1e-06, collinearity_reg: float = 1e-06, verbose: bool = False) None[source]
Instantiate the class.
- Parameters:
problem_dimension (int) – Dimension of independent variable. np.shape(x) = problem_dimension x is a 1d vector.
l2_reg (float) – Tychonoff regularization coeff
collinearity_reg (float) – Tychonoff regularization coeff which further penalizes collinear terms
verbose (bool) – Verbosity flag (stored but currently unused).