-
Notifications
You must be signed in to change notification settings - Fork 176
Use petsctools.AppContext
#4526
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
base: main
Are you sure you want to change the base?
Changes from all commits
9ac5e2e
ad8f18f
a2ed0cd
0d15c5b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,11 +66,14 @@ def initialize(self, pc): | |
| :arg pc: PETSc preconditioner instance | ||
| """ | ||
| from firedrake.assemble import assemble, get_assembler | ||
| from dmhooks import get_appctx | ||
|
|
||
| Citations().register("Gopalakrishnan2009") | ||
| _, P = pc.getOperators() | ||
| appctx = self.get_appctx(pc) | ||
| fcp = appctx.get("form_compiler_parameters") | ||
| ctx = get_appctx(pc) | ||
| fcp = ctx.fcp | ||
|
|
||
| appctx = ctx.appctx | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is weird. This is saying
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, this is really annoying. There is the
I would be in favour of cleaning up this naming, but it'd touch a lot of code and I don't want to do it unilaterally.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. I'd be happy for now to just open an issue about it, plus perhaps an explanatory comment. |
||
|
|
||
| if pc.getType() != "python": | ||
| raise ValueError("Expecting PC type python") | ||
|
|
@@ -119,21 +122,21 @@ def initialize(self, pc): | |
| coarse_mat_type = opts.getString(coarse_options_prefix + "mat_type", | ||
| parameters["default_matrix_type"]) | ||
|
|
||
| get_coarse_space = appctx.get("get_coarse_space", None) | ||
| get_coarse_space = appctx.get(options_prefix+"get_coarse_space", None) | ||
| if not get_coarse_space: | ||
| raise ValueError("Need to provide a callback which provides the coarse space.") | ||
| coarse_space = get_coarse_space() | ||
|
|
||
| get_coarse_operator = appctx.get("get_coarse_operator", None) | ||
| get_coarse_operator = appctx.get(options_prefix+"get_coarse_operator", None) | ||
| if not get_coarse_operator: | ||
| raise ValueError("Need to provide a callback which provides the coarse operator.") | ||
| coarse_operator = get_coarse_operator() | ||
|
|
||
| coarse_space_bcs = appctx.get("coarse_space_bcs", None) | ||
| coarse_space_bcs = appctx.get(options_prefix+"coarse_space_bcs", None) | ||
|
|
||
| # These should be callbacks which return the relevant nullspaces | ||
| get_coarse_nullspace = appctx.get("get_coarse_op_nullspace", None) | ||
| get_coarse_transpose_nullspace = appctx.get("get_coarse_op_transpose_nullspace", None) | ||
| get_coarse_nullspace = appctx.get(options_prefix+"get_coarse_op_nullspace", None) | ||
| get_coarse_transpose_nullspace = appctx.get(options_prefix+"get_coarse_op_transpose_nullspace", None) | ||
|
|
||
| coarse_form_assembler = get_assembler(coarse_operator, bcs=coarse_space_bcs, form_compiler_parameters=fcp, mat_type=coarse_mat_type, options_prefix=coarse_options_prefix) | ||
| self.coarse_op = coarse_form_assembler.allocate() | ||
|
|
@@ -150,14 +153,14 @@ def initialize(self, pc): | |
| tnsp = get_coarse_transpose_nullspace() | ||
| coarse_opmat.setTransposeNullSpace(tnsp.nullspace()) | ||
|
|
||
| interp_petscmat = appctx.get("interpolation_matrix", None) | ||
| interp_petscmat = appctx.get(options_prefix+"interpolation_matrix", None) | ||
| if interp_petscmat is None: | ||
| # Create interpolation matrix from coarse space to fine space | ||
| fine_space = ctx.J.arguments()[0].function_space() | ||
| coarse_test, coarse_trial = coarse_operator.arguments() | ||
| interp = assemble(Interpolate(coarse_trial, fine_space)) | ||
| interp_petscmat = interp.petscmat | ||
| restr_petscmat = appctx.get("restriction_matrix", None) | ||
| restr_petscmat = appctx.get(options_prefix+"restriction_matrix", None) | ||
|
|
||
| # We set up a PCMG object that uses the constructed interpolation | ||
| # matrix to generate the restriction/prolongation operators. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,9 +141,8 @@ class _SNESContext(object): | |
| :arg pmat_type: Indicates whether the preconditioner (if present) is assembled | ||
| monolithically ('aij'), as a block sparse matrix ('nest') or | ||
| matrix-free (as :class:`~.ImplicitMatrix`, 'matfree'). | ||
| :arg appctx: Any extra information used in the assembler. For the | ||
| matrix-free case this will contain the Newton state in | ||
| ``"state"``. | ||
| :arg appctx: A petsctools.AppContext containing application | ||
| context that is passed to the preconditioner if matrix-free. | ||
| :arg pre_jacobian_callback: User-defined function called immediately | ||
| before Jacobian assembly | ||
| :arg post_jacobian_callback: User-defined function called immediately | ||
|
|
@@ -194,14 +193,13 @@ def __init__(self, problem, mat_type, pmat_type, appctx=None, | |
| self._x = problem.u_restrict | ||
|
|
||
| if appctx is None: | ||
| appctx = {} | ||
| from petsctools import AppContext | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do import at the top level |
||
| appctx = AppContext() | ||
| # A split context will already get the full state. | ||
| # TODO, a better way of doing this. | ||
| # Now we don't have a temporary state inside the snes | ||
| # context we could just require the user to pass in the | ||
| # full state on the outside. | ||
| appctx.setdefault("state", self._x) | ||
| appctx.setdefault("form_compiler_parameters", self.fcp) | ||
|
|
||
| self.appctx = appctx | ||
| self.matfree = matfree | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you set
fcp? It's not a clear name.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it is already attached to the
_SNESContext. I'm just switching to getting the compiler parameters from there instead of the appctx because it is a "global" rather than a namespaced (prefixed) thing.