cashocs.nonlinear_solvers.newton_solve#
- cashocs.nonlinear_solvers.newton_solve(nonlinear_form: ufl.Form, u: fenics.Function, bcs: fenics.DirichletBC | list[fenics.DirichletBC], derivative: ufl.Form | None = None, shift: ufl.Form | None = None, rtol: float = 1e-10, atol: float = 1e-10, max_iter: int = 50, convergence_type: Literal['combined', 'rel', 'abs'] = 'combined', norm_type: Literal['l2', 'linf'] = 'l2', damped: bool = True, inexact: bool = True, verbose: bool = True, ksp_options: _typing.KspOption | None = None, A_tensor: fenics.PETScMatrix | None = None, b_tensor: fenics.PETScVector | None = None, is_linear: bool = False, preconditioner_form: ufl.Form | None = None, linear_solver: _utils.linalg.LinearSolver | None = None) fenics.Function [source]#
Solves a nonlinear problem with Newton's method.
- Parameters:
nonlinear_form (ufl.Form) – The variational form of the nonlinear problem to be solved by Newton’s method.
u (fenics.Function) – The sought solution / initial guess. It is not assumed that the initial guess satisfies the Dirichlet boundary conditions, they are applied automatically. The method overwrites / updates this Function.
bcs (fenics.DirichletBC | list[fenics.DirichletBC]) – A list of DirichletBCs for the nonlinear variational problem.
derivative (ufl.Form | None) – The Jacobian of nonlinear_form, used for the Newton method. Default is None, and in this case the Jacobian is computed automatically with AD.
shift (ufl.Form | None) – A shift term, if the right-hand side of the nonlinear problem is not zero, but shift.
rtol (float) – Relative tolerance of the solver if convergence_type is either
'combined'
or'rel'
(default isrtol = 1e-10
).atol (float) – Absolute tolerance of the solver if convergence_type is either
'combined'
or'abs'
(default isatol = 1e-10
).max_iter (int) – Maximum number of iterations carried out by the method (default is
max_iter = 50
).convergence_type (Literal['combined', 'rel', 'abs']) – Determines the type of stopping criterion that is used.
norm_type (Literal['l2', 'linf']) – Determines which norm is used in the stopping criterion.
damped (bool) – If
True
, then a damping strategy is used. IfFalse
, the classical Newton-Raphson iteration (without damping) is used (default isTrue
).inexact (bool) – If
True
, an inexact Newton's method is used. Default isTrue
.verbose (bool) – If
True
, prints status of the iteration to the console (default isTrue
).ksp_options (_typing.KspOption | None) – The list of options for the linear solver.
A_tensor (fenics.PETScMatrix | None) – A fenics.PETScMatrix for storing the left-hand side of the linear sub-problem.
b_tensor (fenics.PETScVector | None) – A fenics.PETScVector for storing the right-hand side of the linear sub-problem.
is_linear (bool) – A boolean flag, which indicates whether the problem is actually linear.
preconditioner_form (ufl.Form | None) – A UFL form which defines the preconditioner matrix.
linear_solver (_utils.linalg.LinearSolver | None) – The linear solver (KSP) which is used to solve the linear systems arising from the discretized PDE.
- Returns:
The solution of the nonlinear variational problem, if converged. This overwrites the input function u.
- Return type:
fenics.Function
Examples
Consider the problem
\[\begin{split}\begin{alignedat}{2} - \Delta u + u^3 &= 1 \quad &&\text{ in } \Omega=(0,1)^2 \\ u &= 0 \quad &&\text{ on } \Gamma. \end{alignedat}\end{split}\]This is solved with the code
from fenics import * import cashocs mesh, _, boundaries, dx, _, _ = cashocs.regular_mesh(25) V = FunctionSpace(mesh, 'CG', 1) u = Function(function_space) v = TestFunction(function_space) F = inner(grad(u), grad(v))*dx + pow(u,3)*v*dx - Constant(1)*v*dx bcs = cashocs.create_dirichlet_bcs(V, Constant(0.0), boundaries, [1,2,3,4]) cashocs.newton_solve(F, u, bcs)