Skip to content

Refactor options manager#39

Draft
connorjward wants to merge 4 commits into
mainfrom
connorjward/auto-options
Draft

Refactor options manager#39
connorjward wants to merge 4 commits into
mainfrom
connorjward/auto-options

Conversation

@connorjward

Copy link
Copy Markdown
Collaborator

Key changes:

  • Allow users to set command line options for autogenerated prefixes, this will throw a warning eventually.
  • Handle most options work at the point that the inserted_options context manager is used. I don't think we should be handling things in the constructor because we then can't adapt to modifications to the Options() object.
  • Don't overwrite options set on the command line, and don't drop them after first use.

Closes #37

The MFE in the issue is fixed by this:

from firedrake import *
import pytest


@pytest.mark.parametrize("options_prefix", (None, "", "pippo_"))
def test_change_command_line(options_prefix):
    mesh = UnitSquareMesh(2, 2)
    V = FunctionSpace(mesh, "CG", 1)

    u = Function(V)
    v = TestFunction(V)

    F = inner(u, v) * dx - conj(v) * dx

    problem = NonlinearVariationalProblem(F, u)
    
    # suppose users pass options by command line or via environment variable
    # here we mimic this by inserting the option directly in PETSc database
    # PETSc behavior: -some_option_whatever equivalent to -some_option_<n>_whatever for all n integers
    opts = PETSc.Options(options_prefix if options_prefix is not None else "firedrake_")
    opts["ksp_type"] = "minres"
    solver = NonlinearVariationalSolver(problem, solver_parameters={"ksp_type": "cg"}, options_prefix=options_prefix)
    opts_not_removed = "ksp_type" in opts and opts["ksp_type"] == "minres"
    # remove our option so that it won't conflict with all other tests
    del opts["ksp_type"]
    assert solver.snes.ksp.getType() == "minres" and opts_not_removed

@connorjward

Copy link
Copy Markdown
Collaborator Author

@JHopeCollins this is only a rough first attempt but I want to check with you on the approach. The main thing I'm doing is moving the options logic into the inserted_options context manager/method instead of __init__. I think that this makes sense because users should be allowed to noodle with PETSc.Options() themselves. Currently the options are baked in when we construct the OptionsManager.

@connorjward connorjward force-pushed the connorjward/auto-options branch from 0b9e714 to a0fd39a Compare June 19, 2026 14:46
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.

Raise a warning when trying to pass options to an autogenerated prefix.

1 participant