Skip to content

Add support for objective function in SNES#5155

Open
stefanozampini wants to merge 3 commits into
mainfrom
stefanozampini/snes-objective
Open

Add support for objective function in SNES#5155
stefanozampini wants to merge 3 commits into
mainfrom
stefanozampini/snes-objective

Conversation

@stefanozampini
Copy link
Copy Markdown
Contributor

Description

Add support for objective functions in NonlinearVariationalProblem and NonlinearVariationalSolver. Needs https://gitlab.com/petsc/petsc/-/merge_requests/9331 to be merged in PETSc

@stefanozampini stefanozampini requested review from dham and pbrubeck June 7, 2026 12:59
Comment thread firedrake/solving_utils.py Outdated

# Use the user-provided Jacobian. If none is provided, derive
# the Jacobian from the residual.
self.E = objective
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

One may also inspect F and decide to call derivative twice, but requires the pde_args check to be done earlier.

Copy link
Copy Markdown
Contributor

@pbrubeck pbrubeck Jun 7, 2026

Choose a reason for hiding this comment

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

It might be worthwhile to introduce an optimiser class that just takes in E and computes F and J automatically, but that could go as a separate PR

Copy link
Copy Markdown
Contributor

@pbrubeck pbrubeck Jun 7, 2026

Choose a reason for hiding this comment

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

The new line should not be added below the comment

Suggested change
self.E = objective

if self._problem.E:
snes.setObjective(self.form_objective)
else:
snes.setObjective(None)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

See the PETSc MR for why this is needed

Comment thread firedrake/solving_utils.py Outdated
Comment thread firedrake/variational_solver.py Outdated
Co-authored-by: Pablo Brubeck <brubeck@protonmail.com>
Comment thread firedrake/solving_utils.py
@pbrubeck
Copy link
Copy Markdown
Contributor

pbrubeck commented Jun 7, 2026

In order to get the energy at the coarse levels in MG/FAS we just need to add a tiny amount of logic in mg/ufl_utils.py.

I can add it for you, if you don't mind.

@stefanozampini
Copy link
Copy Markdown
Contributor Author

In order to get the energy at the coarse levels in MG/FAS we just need to add a tiny amount of logic in mg/ufl_utils.py.

I can add it for you, if you don't mind.

Go ahead

Comment thread tests/firedrake/deflation/test_bratu.py Outdated
self.E = objective
self.J = J or ufl_expr.derivative(F, u)
self.F = F
self.Jp = Jp
Copy link
Copy Markdown
Contributor

@pbrubeck pbrubeck Jun 7, 2026

Choose a reason for hiding this comment

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

Suggested change
self.Jp = Jp
self.Jp = Jp
self.E = objective

@pbrubeck pbrubeck force-pushed the stefanozampini/snes-objective branch from 8465c3c to d41cc7e Compare June 7, 2026 22:11
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