Skip to content

dolfinx.fem.petsc.LinearProblem for nest and block systems - #3684

Merged
jorgensd merged 143 commits into
mainfrom
dokken/blocked_linear_solver
Jun 16, 2025
Merged

dolfinx.fem.petsc.LinearProblem for nest and block systems#3684
jorgensd merged 143 commits into
mainfrom
dokken/blocked_linear_solver

Conversation

@jorgensd

@jorgensd jorgensd commented Apr 3, 2025

Copy link
Copy Markdown
Member

With the unification of create_*, assemble_* and assign for various PETSc operators, I suggest we let LinearProblem support non-blocked and blocked problems, i.e.

problem = LinearProblem(a, L, [bc0, bc1], petsc_options={"ksp_type": "preonly",
                                                         "pc_type": "lu",
                                                         "pc_factor_mat_solver_type":
                                                           "mumps"})
problem = LinearProblem([[a00,a01],[None, a11]], [L0, L1], bcs=[bc0, bc1],
                        u=[uh0, uh1])

For now, I've chosen to force the user to specify u for blocked problems, as it is hard extract the correct function spaces (dolfinx.fem.FunctionSpace, python class) from the nested list of forms with the current functions (extract_function_spaces work on the C++ function-spaces).

This also introduces an immediate solution vector _x, in the same fashion as within the proposed SNES solver.

It also adds the option of sending in a preconditioner to LinearProblem, as a ufl.Form or a nested sequence of Forms, which is then set to the krylov-solver.

jorgensd and others added 30 commits February 20, 2025 10:14
Co-authored-by: Michal Habera <michal.habera@gmail.com>
…of `solve`. Let the user do this with the Problem-class function replace solution.

Also expose the petsc objects in the solver through properties
@jorgensd
jorgensd enabled auto-merge June 13, 2025 10:24
@jhale
jhale disabled auto-merge June 14, 2025 19:56
@jorgensd
jorgensd added this pull request to the merge queue Jun 16, 2025
Merged via the queue into main with commit 2485983 Jun 16, 2025
@jorgensd
jorgensd deleted the dokken/blocked_linear_solver branch June 16, 2025 07:10
ordinary-slim pushed a commit to ordinary-slim/dolfinx that referenced this pull request Jul 6, 2025
…3684)

* Add snes solver

* Replace monolitic snes from test

* Add nate to copyright

* Add snes solvers for blocked problems

* Add nest snes solver

* Ruff formatting

* Shorten description

* Add preconditioner property

* Apply suggestions from code review

* Improve documentation to address reviewer comments

* Fix documentation

* Typo

* Apply suggestions from code review

* Unify docs

* Ruff

* Improve documentation of blocked problem.

* Remove communicator from snesproblem

* Move petsc imports into tests to avoid failure when petsc is not installed

* Update naming scheme

* Apply Michals suggestions

Co-authored-by: Michal Habera <michal.habera@gmail.com>

* Use typing protocol to remove inheritance as well as adding a helper function for extracting Jacobians

* Add francesco's comments

* Unify snes solver into one

* Ruff formatting

* Ruff + mypy

* Remove error if not converged

* Do not update the variable `u` used in the variational forms as part of `solve`. Let the user do this with the Problem-class function replace solution.
Also expose the petsc objects in the solver through properties

* Start prototyping functional interface

* Make solvers work

* Remove nonexistent classes

* Ruff formatting

* mypy + ruff

* Fix import

* Use public members

* Remove snesproblem def

* Add note

* Fully functional

* Remove protocol

* Move import

* Add docstrings

* Use functools partial

* Remove update solution

* Move snes options to constructor

* Rename data structures

* Rename again

* Rename

* Move dfx.fem.Function<->PETSc.Vec methods to dolfinx.fem.petsc

* Add example of direct usage with snes

* Stricter tolerance

* Set tolerance depending on eps

* Ruff format

* Insert initial guess for hand written solver

* Move assembly type into dolfinx.fem.petsc

* Rename "default"->"standard". Switch order in Vec->Function

* Add prefix for snes options

* Apply suggestions from code review

* Add full type hints

* default->standard for residual and jacobian helper function

* Make _create_snes_matrices_and_vectors private

* Update python/dolfinx/fem/petsc.py

Co-authored-by: Garth N. Wells <gnw20@cam.ac.uk>

* Apply suggestions from code review

* More minor updates to adhere to reviewer comments

* Merge main into snes solver. Add required ghost updates within F and J computations

* Fix typehint

* Update typesetting of singleton assemble_jacobian

* Use try-except instead of if-else

* Proposal for supporting blocked problems (not nest) in `LinearProblem`.

* Add asserts for typehinting to demos

* Add kind as input argument and allow for nest

* Use same logic for both blocked and non-blocked problems

* Test all use-cases

* Centralize ghostupdate function

* Simplify vector creation

* add element dtypes

* Ruff formatting

* Use realtype

* Use inner

* Add kind which is nested list of types

* Make ghostupdate private

* Ruff format comment and add Preconditioner input to `LinearProblem`

* Ruff format

* Add entity maps as optional input to snessolver

* Fix check

* Various import cleanups

* Remove unused code.

* Unify assemble_residual (FEniCS#3682)

* Unify assemble_residual

* Ruff format

* Fix imports

* Add back comma

* Move assignment of block data out of assembler

* Ignore type-hints

* Switch order of operations

* Ruff format

* Add entity maps

* Introduce major API changes and deprecations.

* Improve documentation messages on deprecation.

* Refactor in progress, tests not passing yet.

* Update python/dolfinx/nls/petsc.py

Co-authored-by: Jørgen Schartum Dokken <dokken92@gmail.com>

* Update python/dolfinx/fem/petsc.py

Co-authored-by: Jørgen Schartum Dokken <dokken92@gmail.com>

* Fix tests.

* Fix test

* Fix Cahn-Hilliard - move to new NonlinearProblem class?

* Fix ruff.

* Refactor compute_jacobian

* Ruff format.

* Update petsc.py

* Illustrate usage of LinearProblem with blocked matrices

* Fix import

* Consistency with LinearProblem class.

* A few extra comments in test.

* Consistency petsc_options with LinearProblem

* Disable test

* Use sequence consistently.

* More specific.

* Tidy up.

* Loosen typing.

* Leave that unknown

* Fix typing.

* More typing fixes.

* Fixup comments and typing.

* Remove duplicate classes

---------

Co-authored-by: Michal Habera <michal.habera@gmail.com>
Co-authored-by: Jack S. Hale <mail@jackhale.co.uk>
Co-authored-by: Garth N. Wells <gnw20@cam.ac.uk>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

proposal Suggested change or addition

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants