Skip to content
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

Make LinearSolver a subclass of LinearVariationalSolver #4012

Open
wants to merge 18 commits into
base: pbrubeck/feature/fenics-bcs
Choose a base branch
from

Conversation

pbrubeck
Copy link
Contributor

@pbrubeck pbrubeck commented Feb 5, 2025

Description

Provides enhacements for LinearVariationalSolver to support more generic MatrixBase operators, and makes LinearSolver a subclass of LinearVariationalSolver.

Fixes #4016

Copy link

github-actions bot commented Feb 5, 2025

TestsPassed ✅Skipped ⏭️Failed ❌
Firedrake real7686 ran7051 passed635 skipped0 failed

Copy link

github-actions bot commented Feb 5, 2025

TestsPassed ✅Skipped ⏭️Failed ❌
Firedrake complex8112 ran6556 passed1555 skipped1 failed

@pbrubeck pbrubeck force-pushed the pbrubeck/linear-solver branch from 529d226 to 59fc6ed Compare February 5, 2025 20:05
@pbrubeck pbrubeck force-pushed the pbrubeck/linear-solver branch from da40554 to 400ae30 Compare February 5, 2025 22:20
Copy link
Contributor

@connorjward connorjward left a comment

Choose a reason for hiding this comment

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

Rather than doing this would it not be possible to deprecate LinearSolver in favour of LinearVariationalSolver? I don't think wrapping one in the other makes sense in terms of code design.

@pbrubeck
Copy link
Contributor Author

pbrubeck commented Feb 6, 2025

There's still good use for this class, especially because it enables the user to provide multiple right-hand side and solution pairs. Another good reason is that this gives the user another way of preventing the Jacobian of being re-assembled.

@pbrubeck
Copy link
Contributor Author

pbrubeck commented Feb 6, 2025

This PR is also paving the way for composing Fieldsplit and other solvers to solve systems with pre-assembled matrices and also more general BaseForms.

@connorjward
Copy link
Contributor

This PR is also paving the way for composing Fieldsplit and other solvers to solve systems with pre-assembled matrices and also more general BaseForms.

But why is LinearVariationalSolver not suitable for those cases? We can now represent assembled matrices in UFL so can't the code paths be unified? I think @JHopeCollins has had some success with this sort of thing using external operators (when we were discussing taping for LinearSolver).

gives the user another way of preventing the Jacobian of being re-assembled.

Surely multiple methods to achieve the same result suggests that things should be combined?

it enables the user to provide multiple right-hand side and solution pairs

Could we extend LinearVariationalSolver to support this?

@pbrubeck
Copy link
Contributor Author

pbrubeck commented Feb 6, 2025

But why is LinearVariationalSolver not suitable for those cases? We can now represent assembled matrices in UFL so can't the code paths be unified? I think @JHopeCollins has had some success with this sort of thing using external operators (when we were discussing taping for LinearSolver).

There are a couple of cases where LinearVariationalSolver needs to catch up twith LinearSolver to support the more generic operators that LinearSolver is currently capable of handling. This is mostly to do with the fact that the action of the operator apears in the linear form RHS, and fieldsplit will struggle with this as the second argument in the action would no longer be a Function, but a vector of split Functions. This PR is to fix this.

Surely multiple methods to achieve the same result suggests that things should be combined?

We could allow LinearVariationalSolver.solve(x, b) to take in the solution-RHS pair as optional arguments. But having a class that also sets up both the LinearVariationalProblem and the solver is another argument to keep LinearSolver.

Could we extend LinearVariationalSolver to support this?

Basically the PR consists mostly of enhacements to LinearVariationalSolver.

@pbrubeck pbrubeck force-pushed the pbrubeck/linear-solver branch from 29ace44 to e695cd2 Compare February 6, 2025 15:59
@connorjward
Copy link
Contributor

Basically the PR consists mostly of enhancements to LinearVariationalSolver.

Ah I see. Could we therefore think of this PR as a step along the way to fully removing LinearSolver (at a later date)? If so then I am completely on board.

Copy link
Contributor

@connorjward connorjward left a comment

Choose a reason for hiding this comment

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

I think I have only noted small things.

Also, does this PR add any new functionality? If so it would be great to test it.

firedrake/linear_solver.py Outdated Show resolved Hide resolved
firedrake/linear_solver.py Outdated Show resolved Hide resolved
firedrake/linear_solver.py Outdated Show resolved Hide resolved
firedrake/linear_solver.py Outdated Show resolved Hide resolved
firedrake/projection.py Outdated Show resolved Hide resolved
firedrake/solving_utils.py Outdated Show resolved Hide resolved
firedrake/variational_solver.py Outdated Show resolved Hide resolved
@Ig-dolci
Copy link
Contributor

Ig-dolci commented Feb 6, 2025

Description

Provides enhacements for LinearVariationalSolver to support more generic MatrixBase operators, and makes LinearSolver a wrapper for LinearVariationalSolver,

Just a note to pay attention for adjoints: I am more concerned about how the adjoint-based gradients will work with LinearVariationalSolver supporting MatrixBase. Currently, we have LinearVariationalSolver that works perfectly to have the adjoint equations and to obtain and solve an adjoint-based gradient form. Considering this additional feature, I do not see how adjoint-based gradient will work for general cases.

@pbrubeck
Copy link
Contributor Author

pbrubeck commented Feb 7, 2025

Just a note to pay attention for adjoints: I am more concerned about how the adjoint-based gradients will work with LinearVariationalSolver supporting MatrixBase. Currently, we have LinearVariationalSolver that works perfectly to have the adjoint equations and to obtain and solve an adjoint-based gradient form. Considering this additional feature, I do not see how adjoint-based gradient will work for general cases.

@Ig-dolci We support variational problems defined purely in UFL, thus we get differentiability and adjoints. The enhacements are to do with Fieldsplit.

firedrake/linear_solver.py Outdated Show resolved Hide resolved
@pbrubeck pbrubeck changed the base branch from pbrubeck/feature/fenics-bcs to master February 7, 2025 08:49
@pbrubeck pbrubeck changed the base branch from master to pbrubeck/feature/fenics-bcs February 7, 2025 08:49
@pbrubeck pbrubeck changed the title Make LinearSolver a wrapper of LinearVariationalSolver LinearSolver a subclass of LinearVariationalSolver Feb 7, 2025
@pbrubeck pbrubeck changed the title LinearSolver a subclass of LinearVariationalSolver Make LinearSolver a subclass of LinearVariationalSolver Feb 7, 2025
@pbrubeck pbrubeck force-pushed the pbrubeck/linear-solver branch from 41c12a0 to 292b083 Compare February 7, 2025 12:31
@pbrubeck pbrubeck force-pushed the pbrubeck/linear-solver branch from 292b083 to ba4a055 Compare February 7, 2025 12:36
@pbrubeck pbrubeck force-pushed the pbrubeck/linear-solver branch from 824198c to 7958933 Compare February 7, 2025 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants