Skip to content

Make EigenSolver and NonlinearVariationalSolver have an OptionsManager, not be one#5251

Open
connorjward wants to merge 3 commits into
mainfrom
connorjward/solver-options-manager
Open

Make EigenSolver and NonlinearVariationalSolver have an OptionsManager, not be one#5251
connorjward wants to merge 3 commits into
mainfrom
connorjward/solver-options-manager

Conversation

@connorjward

Copy link
Copy Markdown
Contributor

Composition > inheritance.

This is a step along the way to cleaning up the appctxs in Firedrake.

I've also cleaned up the imports a little.

Composition > inheritance.

This is a step along the way to cleaning up the appctxs in Firedrake.
@connorjward
connorjward marked this pull request as ready for review July 15, 2026 14:14
@dham
dham requested a review from JHopeCollins July 21, 2026 16:20

@JHopeCollins JHopeCollins left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested updates to the EigenSolver to use the petsctools.options free functions. This mean that the OptionsManager is attached directly to the PETSc object and we don't have to carry it around separately.
Similar edits needed for the NLVS.

See demo for full example: https://www.firedrakeproject.org/petsctools/appctx.html#the-options-and-the-appcontext

Comment thread firedrake/eigensolver.py
from functools import cached_property

from petsc4py import PETSc
from petsctools import OptionsManager, flatten_parameters

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
from petsctools import OptionsManager, flatten_parameters
import petsctools

Comment thread firedrake/eigensolver.py
Comment on lines +169 to +170
self.options_manager = OptionsManager(solver_parameters, options_prefix)
self.options_manager.set_from_options(self.es)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
self.options_manager = OptionsManager(solver_parameters, options_prefix)
self.options_manager.set_from_options(self.es)
petsctools.set_from_options(self.es, solver_parameters, options_prefix)

Comment thread firedrake/eigensolver.py

@property
def parameters(self) -> dict:
return self.options_manager.parameters

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
return self.options_manager.parameters
return petsctools.get_options(self.es).parameters

Comment thread firedrake/eigensolver.py

@property
def options_prefix(self) -> str:
return self.options_manager.options_prefix

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
return self.options_manager.options_prefix
return petsctools.get_options(self.es).options_prefix

Comment thread firedrake/eigensolver.py
Comment on lines +180 to +186
def set_from_options(self, snes: PETSc.SNES) -> None:
warnings.warn(
"'LinearEigensolver.set_from_options' is deprecated, use "
"'LinearEigensolver.options_manager.set_from_options' instead",
FutureWarning
)
self.options_manager.set_from_options(snes)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
def set_from_options(self, snes: PETSc.SNES) -> None:
warnings.warn(
"'LinearEigensolver.set_from_options' is deprecated, use "
"'LinearEigensolver.options_manager.set_from_options' instead",
FutureWarning
)
self.options_manager.set_from_options(snes)
def set_from_options(self, snes: PETSc.SNES) -> None:
warnings.warn(
"'LinearEigensolver.set_from_options' is deprecated, "
"'the ES has already been set from options.",
FutureWarning
)

Comment thread firedrake/eigensolver.py
Comment on lines +188 to +194
def inserted_options(self):
warnings.warn(
"'LinearEigensolver.inserted_options' is deprecated, use "
"'LinearEigensolver.options_manager.inserted_options' instead",
FutureWarning
)
return self.options_manager.inserted_options()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
def inserted_options(self):
warnings.warn(
"'LinearEigensolver.inserted_options' is deprecated, use "
"'LinearEigensolver.options_manager.inserted_options' instead",
FutureWarning
)
return self.options_manager.inserted_options()
def inserted_options(self):
warnings.warn(
"'LinearEigensolver.inserted_options' is deprecated, use "
"'petsctools.inserted_options(LinearEigensolver.es)' instead",
FutureWarning
)
return petsctools.inserted_options(self.es)

Comment thread firedrake/eigensolver.py
self.es.setDimensions(nev=self.n_evals, ncv=self.ncv, mpd=self.mpd)
self.es.setOperators(self.A_mat, self.M_mat)
with self.inserted_options():
with self.options_manager.inserted_options():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
with self.options_manager.inserted_options():
with petsctools.inserted_options(self.es):

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