Skip to content

Convert PC Python objects into strings - #46

Draft
connorjward wants to merge 3 commits into
connorjward/appctx-autofrom
connorjward/pcpython-detect
Draft

Convert PC Python objects into strings#46
connorjward wants to merge 3 commits into
connorjward/appctx-autofrom
connorjward/pcpython-detect

Conversation

@connorjward

Copy link
Copy Markdown
Collaborator

Makes it much more pleasant to construct preconditioners.

It really is that easy.

(Tests and Firedrake PR to follow)

Makes it much more pleasant to construct preconditioners.
Comment thread petsctools/options.py
for key, value in parameters.items():
if not isinstance(value, _native_petsc_option_types):
# Convert Python PC objects into their string representation
if key.endswith("python_type") and isinstance(value, type):

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.

Can we let the user do a single options entry:

"pc_type": module.FancyPC

and under the hood we split this into the pc_type and pc_python_type options?

Suggested change
if key.endswith("python_type") and isinstance(value, type):
if key.endswith("pc_type") and isinstance(value, type):

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good idea. We have to be careful to not accidentally break anything and we also want to work with snes_type=python too. I will have a think.

Comment thread petsctools/options.py
if not isinstance(value, _native_petsc_option_types):
# Convert Python PC objects into their string representation
if key.endswith("python_type") and isinstance(value, type):
parameters[key] = f"{value.__module__}.{value.__name__}"

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.

Does this work with multiply nested names? i.e. module.submodule.MyPC

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think so. For example:

(venv-firedrake-main-py313) [connor@levi petsctools]$ python
Python 3.13.11 (main, Jan 28 2026, 10:53:04) [GCC 15.2.1 20260103] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy
>>> scipy.minimize
...
AttributeError: Module 'scipy' has no attribute 'minimize'. Did you mean: 'optimize'?
>>> scipy.optimize.minimize
<function minimize at 0x7f5ce5afe020>
>>> scipy.optimize.minimize.__module__
'scipy.optimize._minimize'

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.

2 participants