-
Notifications
You must be signed in to change notification settings - Fork 191
Add support for objective function in SNES #5155
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 1 commit
add29d3
d55a3bb
d41cc7e
bf6f6c7
5835420
a4c3dc3
6654120
0ea74a0
ef82c67
1cb9b75
c3d50f2
b899636
78d169f
dd0e73b
fe5dce6
e49c317
96b63ef
8105f56
f94cb29
66483db
64b9690
13895de
20adc3e
f0767f1
913190a
ba19351
384e096
b98076d
c03ec10
fc6c220
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 |
|---|---|---|
|
|
@@ -231,6 +231,7 @@ def __init__(self, problem, | |
| self.pmatfree = pmatfree | ||
| self.F = problem.F | ||
| self.J = problem.J | ||
| self.E = problem.E | ||
|
|
||
| # For Jp to equal J, bc.Jp must equal bc.J for all EquationBC objects. | ||
| Jp_eq_J = problem.Jp is None and all(bc.Jp_eq_J for bc in problem.bcs) | ||
|
|
@@ -261,6 +262,12 @@ def __init__(self, problem, | |
|
|
||
| self.F -= problem.compute_bc_lifting(self.J, self._bc_residual) | ||
|
|
||
| self._assemble_objective = lambda *args, **kwargs: args | ||
| if self.E: | ||
| self._assemble_objective = get_assembler(self.E, | ||
| form_compiler_parameters=self.fcp, | ||
| ).assemble | ||
|
|
||
| self._assemble_residual = get_assembler(self.F, bcs=self.bcs_F, | ||
| form_compiler_parameters=self.fcp, | ||
| zero_bc_nodes=pre_apply_bcs, | ||
|
|
@@ -342,6 +349,12 @@ def transfer_manager(self, manager): | |
| raise ValueError("Must set transfer manager before first use.") | ||
| self._transfer_manager = manager | ||
|
|
||
| def set_objective(self, snes): | ||
| if self._problem.E: | ||
| snes.setObjective(self.form_objective) | ||
| else: | ||
| snes.setObjective(None) | ||
|
|
||
| def set_function(self, snes): | ||
| r"""Set the residual evaluation function""" | ||
| with self._F.dat.vec_wo as v: | ||
|
|
@@ -449,6 +462,27 @@ def split(self, fields): | |
| splits.append(self.reconstruct(new_problem, options_prefix=options_prefix)) | ||
| return self._splits.setdefault(tuple(fields), splits) | ||
|
|
||
| @staticmethod | ||
| def form_objective(snes, X): | ||
| r"""Form the objective for this problem | ||
|
|
||
| :arg snes: a PETSc SNES object | ||
| :arg X: the current guess (a Vec) | ||
| """ | ||
| dm = snes.getDM() | ||
| ctx = dmhooks.get_appctx(dm) | ||
| # X may not be the same vector as the vec behind self._x, so | ||
| # copy guess in from X. | ||
| with ctx._x.dat.vec_wo as v: | ||
| X.copy(v) | ||
|
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. Is this safe?
Contributor
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 don't know, I copied that from the residual evaluation routine. At the end of
Member
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. Can I think you just need to save the state in
Contributor
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. In principle it should not happen, but I agree with you better be safe. Make a suggestion in the diff and I will merge it in |
||
|
|
||
| if not ctx.pre_apply_bcs: | ||
|
stefanozampini marked this conversation as resolved.
Outdated
|
||
| # Compute DirichletBC residual | ||
| for bc in ctx._problem.dirichlet_bcs(): | ||
| bc.apply(ctx._bc_residual, u=ctx._x) | ||
|
stefanozampini marked this conversation as resolved.
Outdated
|
||
|
|
||
| return ctx._assemble_objective(current_state=ctx._x) | ||
|
stefanozampini marked this conversation as resolved.
Outdated
|
||
|
|
||
| @staticmethod | ||
| def form_function(snes, X, F): | ||
| r"""Form the residual for this problem | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.