Skip to content

SNES solver interface for standard, block and nest systems #3648

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

Open
wants to merge 87 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
f763917
Add snes solver
jorgensd Feb 20, 2025
f5e592f
Replace monolitic snes from test
jorgensd Feb 20, 2025
b36e4f3
Add nate to copyright
jorgensd Feb 20, 2025
f5dd86b
Add snes solvers for blocked problems
jorgensd Feb 20, 2025
cf79b33
Add nest snes solver
jorgensd Feb 20, 2025
f1e01c9
Merge branch 'main' into dokken/snes-solver
jorgensd Feb 20, 2025
45a0919
Ruff formatting
jorgensd Feb 20, 2025
59c10ea
Shorten description
jorgensd Feb 20, 2025
47fbb9d
Add preconditioner property
jorgensd Feb 20, 2025
3ff4199
Apply suggestions from code review
jorgensd Feb 20, 2025
eec9ede
Improve documentation to address reviewer comments
jorgensd Feb 20, 2025
fbe2c35
Fix documentation
jorgensd Feb 20, 2025
98bd069
Typo
jorgensd Feb 20, 2025
cf39c4a
Apply suggestions from code review
jorgensd Feb 20, 2025
8abe6d2
Unify docs
jorgensd Feb 20, 2025
3a0d02f
Ruff
jorgensd Feb 20, 2025
99d3e71
Improve documentation of blocked problem.
jorgensd Feb 20, 2025
fddb215
Remove communicator from snesproblem
jorgensd Feb 20, 2025
df6593a
Move petsc imports into tests to avoid failure when petsc is not inst…
jorgensd Feb 20, 2025
b7e0c1c
Update naming scheme
jorgensd Feb 20, 2025
00f2e00
Apply Michals suggestions
jorgensd Feb 21, 2025
b13f1ca
Use typing protocol to remove inheritance as well as adding a helper …
jorgensd Feb 21, 2025
d9707d3
Add francesco's comments
jorgensd Feb 21, 2025
81415bf
Unify snes solver into one
jorgensd Feb 21, 2025
eb22e5b
Ruff formatting
jorgensd Feb 21, 2025
df0a51e
Ruff + mypy
jorgensd Feb 21, 2025
3056808
Remove error if not converged
jorgensd Feb 21, 2025
94c2401
Do not update the variable `u` used in the variational forms as part …
jorgensd Feb 21, 2025
3f2d626
Merge branch 'main' into dokken/snes-solver
jorgensd Feb 24, 2025
9d8fb9d
Start prototyping functional interface
jorgensd Feb 24, 2025
15f4489
Make solvers work
jorgensd Feb 24, 2025
d1c60ac
Remove nonexistent classes
jorgensd Feb 24, 2025
5c0becb
Ruff formatting
jorgensd Feb 24, 2025
9e29d28
mypy + ruff
jorgensd Feb 24, 2025
20df001
Fix import
jorgensd Feb 24, 2025
1cee6ce
Use public members
jorgensd Feb 24, 2025
69ffbee
Remove snesproblem def
jorgensd Feb 24, 2025
0137711
Add note
jorgensd Feb 24, 2025
3d8fc76
Fully functional
jorgensd Feb 25, 2025
e20b884
Merge branch 'main' into snes-solver-functional
jorgensd Feb 25, 2025
96a57d7
Remove protocol
jorgensd Feb 25, 2025
4d80957
Move import
jorgensd Feb 25, 2025
2894967
Add docstrings
jorgensd Feb 25, 2025
873779b
Use functools partial
jorgensd Feb 25, 2025
7b76244
Remove update solution
jorgensd Feb 25, 2025
c662958
Move snes options to constructor
jorgensd Feb 25, 2025
6f54e63
Rename data structures
jorgensd Feb 25, 2025
0493de4
Rename again
jorgensd Feb 25, 2025
3471ae1
Rename
jorgensd Feb 25, 2025
924cea0
Move dfx.fem.Function<->PETSc.Vec methods to dolfinx.fem.petsc
jorgensd Feb 25, 2025
fa8f64b
Add example of direct usage with snes
jorgensd Feb 25, 2025
ff2c2e3
Stricter tolerance
jorgensd Feb 25, 2025
824d7b5
Set tolerance depending on eps
jorgensd Feb 25, 2025
dd6f271
Ruff format
jorgensd Feb 25, 2025
fdeb620
Insert initial guess for hand written solver
jorgensd Feb 25, 2025
662f57a
Move assembly type into dolfinx.fem.petsc
jorgensd Feb 25, 2025
b500008
Rename "default"->"standard". Switch order in Vec->Function
jorgensd Feb 25, 2025
b82e45e
Add prefix for snes options
jorgensd Feb 25, 2025
d3e0f7c
Apply suggestions from code review
jorgensd Feb 25, 2025
4c0cd34
Add full type hints
jorgensd Feb 25, 2025
fd204fc
default->standard for residual and jacobian helper function
jorgensd Feb 26, 2025
8249375
Merge branch 'main' into snes-solver-functional
jhale Feb 26, 2025
e767c0a
Make _create_snes_matrices_and_vectors private
jorgensd Feb 26, 2025
d359d6b
Update python/dolfinx/fem/petsc.py
jhale Feb 27, 2025
5f0230d
Apply suggestions from code review
jorgensd Feb 27, 2025
f12cf71
More minor updates to adhere to reviewer comments
jorgensd Feb 27, 2025
04546cb
Merge branch 'main' into snes-solver-functional
jhale Mar 6, 2025
68f6395
Merge main into snes solver. Add required ghost updates within F and …
jorgensd Mar 7, 2025
d3dc332
Merge branch 'main' into snes-solver-functional
jorgensd Mar 7, 2025
d35a5d6
Merge branch 'main' into snes-solver-functional
jorgensd Mar 17, 2025
bc4be9b
Merge branch 'main' into snes-solver-functional
jorgensd Mar 20, 2025
87b2269
Merge branch 'main' into snes-solver-functional
jorgensd Mar 24, 2025
236a378
Fix typehint
jorgensd Mar 24, 2025
438d4fa
Merge branch 'main' into snes-solver-functional
jorgensd Apr 2, 2025
0a4f5bf
Update typesetting of singleton assemble_jacobian
jorgensd Apr 2, 2025
f066c37
Use try-except instead of if-else
jorgensd Apr 2, 2025
52a3323
Merge branch 'main' into snes-solver-functional
jorgensd May 2, 2025
bb5c431
Add entity maps as optional input to snessolver
jorgensd May 6, 2025
3789bcf
Fix check
jorgensd May 6, 2025
d6b3839
Various import cleanups
jorgensd May 6, 2025
e0f10f6
Merge branch 'main' into snes-solver-functional
jorgensd May 12, 2025
d908f83
Remove unused code.
jorgensd May 19, 2025
60ef729
Unify assemble_residual (#3682)
jorgensd May 19, 2025
526697d
Merge branch 'main' into snes-solver-functional
jorgensd May 19, 2025
ee9c022
Switch order of operations
jorgensd May 19, 2025
0dcb348
Ruff format
jorgensd May 19, 2025
c56ab4f
Merge branch 'main' into snes-solver-functional
jhale May 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion python/dolfinx/fem/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from dolfinx import cpp as _cpp
from dolfinx import default_scalar_type, jit
from dolfinx.fem import IntegralType
from dolfinx.fem.function import FunctionSpace
from dolfinx.fem.function import Function, FunctionSpace

if typing.TYPE_CHECKING:
from mpi4py import MPI
Expand Down Expand Up @@ -641,3 +641,27 @@ def create_form(
msh._cpp_object,
)
return Form(f, form.ufcx_form, form.code)


def compute_jacobian(
F: typing.Union[ufl.Form | list[ufl.Form]],
u: typing.Union[Function | list[Function]],
du: typing.Optional[typing.Union[ufl.Argument, list[ufl.Argument]]] = None,
) -> typing.Union[ufl.Form, list[list[ufl.Form]]]:
"""Compute the Jacobian :math:`J = \\frac{\\partial F}{\\partial u}[\\delta u]`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear from the docs what is 'computed'. It should say something like it computes the derivative of a UFL form to generate a UFL form for the Jacobian.

Need to document the arguments.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to review and edit all of the documentation in due course. Many issues, also with consistency with respect to LinearProblem.


If `F` is a list of forms, the Jacobian is computed as
:math:`J_{ij} = \\frac{\\partial F_i}{u_j}[\\delta u_j]`.
"""
if isinstance(F, ufl.Form):
if du is None:
du = ufl.TrialFunction(u.function_space) # type: ignore
return ufl.derivative(F, u, du)
else:
assert [isinstance(Fi, ufl.Form) for Fi in F], "F must be a list of UFL forms"
assert len(F) == len(u), "Number of forms and functions must be equal"
if du is not None:
assert len(F) == len(du), "Number of forms and du must be equal"
else:
du = [ufl.TrialFunction(u_i.function_space) for u_i in u]
return [[ufl.derivative(Fi, u_j, du_j) for u_j, du_j in zip(u, du)] for Fi in F]
2 changes: 1 addition & 1 deletion python/dolfinx/fem/petsc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2018-2025 Garth N. Wells and Jørgen S. Dokken
# Copyright (C) 2018-2025 Garth N. Wells, Nathan Sime and Jørgen S. Dokken
#
# This file is part of DOLFINx (https://www.fenicsproject.org)
#
Expand Down
Loading
Loading