-
Notifications
You must be signed in to change notification settings - Fork 12
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
Adjoint #592
Closed
Adjoint #592
Changes from 24 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
eb360ac
make dt and t Functions on the Real space
jshipton 93ad1cf
sort out value of t in timestepper
jshipton b54f3bd
more Functions in R instead of Constants
jshipton 998deb6
adjoint diffusion example
jshipton 209d056
try adjoint with shallow water
jshipton 1682f88
Merge branch 'main' of https://github.com/firedrakeproject/gusto into…
jshipton e49b006
use SIQN for adjoint shallow water
jshipton 2b38eea
some small changes to enable moist thermal shallow water adjoint
jshipton b0055c8
Merge branch 'main' into adjoint
jshipton a7e6de1
Add adjoint tests
Ig-dolci 3bc9a15
flake8
Ig-dolci 4cfd4bb
wip
Ig-dolci 7b9d24d
flake8
Ig-dolci 31d5c59
Testing
Ig-dolci 96becfe
Minor changer
Ig-dolci 9f9b8eb
Test all controls
Ig-dolci 61ca2dc
Check the blocks are empty
Ig-dolci 6da7518
Add a notebook
Ig-dolci 14371fd
Small changes
Ig-dolci 9b8b0ed
dd
Ig-dolci b088504
Remove adjoint examples; enhance the notebook text; fix the tests
Ig-dolci 7680ebc
flake8
Ig-dolci af8908b
Merge branch 'main' into adjoint
Ig-dolci c6eb3b2
Match with the main branch
Ig-dolci 719a194
wip
Ig-dolci 542d33e
Add convert_parameters_to_real_space function
Ig-dolci efc7e99
wip
Ig-dolci f89173a
flake8
Ig-dolci 84814ce
replace deprecated decorator
Ig-dolci 06de329
fix error
Ig-dolci 145f111
solve conflict
Ig-dolci ba4fc95
more fixes
Ig-dolci 52ed722
wip
Ig-dolci 0601968
Revert "solve conflict"
Ig-dolci File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import pytest | ||
import numpy as np | ||
|
||
from firedrake import * | ||
from firedrake.adjoint import * | ||
from pyadjoint import get_working_tape | ||
from gusto import * | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def handle_taping(): | ||
yield | ||
tape = get_working_tape() | ||
tape.clear_tape() | ||
|
||
|
||
@pytest.fixture(autouse=True, scope="module") | ||
def handle_annotation(): | ||
from firedrake.adjoint import annotate_tape, continue_annotation | ||
if not annotate_tape(): | ||
continue_annotation() | ||
yield | ||
# Ensure annotation is paused when we finish. | ||
annotate = annotate_tape() | ||
if annotate: | ||
pause_annotation() | ||
|
||
|
||
@pytest.mark.parametrize("nu_is_control", [True, False]) | ||
def test_diffusion_sensitivity(nu_is_control, tmpdir): | ||
assert get_working_tape()._blocks == [] | ||
n = 30 | ||
mesh = PeriodicUnitSquareMesh(n, n) | ||
output = OutputParameters(dirname=str(tmpdir)) | ||
dt = 0.01 | ||
domain = Domain(mesh, 10*dt, family="BDM", degree=1) | ||
io = IO(domain, output) | ||
|
||
V = VectorFunctionSpace(mesh, "CG", 2) | ||
domain.spaces.add_space("vecCG", V) | ||
|
||
R = FunctionSpace(mesh, "R", 0) | ||
# We need to define nu as a function in order to have a control variable. | ||
nu = Function(R, val=0.0001) | ||
diffusion_params = DiffusionParameters(kappa=nu) | ||
eqn = DiffusionEquation(domain, V, "f", diffusion_parameters=diffusion_params) | ||
|
||
diffusion_scheme = BackwardEuler(domain) | ||
diffusion_methods = [CGDiffusion(eqn, "f", diffusion_params)] | ||
timestepper = Timestepper(eqn, diffusion_scheme, io, spatial_methods=diffusion_methods) | ||
|
||
x = SpatialCoordinate(mesh) | ||
fexpr = as_vector((sin(2*pi*x[0]), cos(2*pi*x[1]))) | ||
timestepper.fields("f").interpolate(fexpr) | ||
|
||
end = 0.1 | ||
timestepper.run(0., end) | ||
|
||
u = timestepper.fields("f") | ||
J = assemble(inner(u, u)*dx) | ||
|
||
if nu_is_control: | ||
control = Control(nu) | ||
h = Function(R, val=0.0001) # the direction of the perturbation | ||
else: | ||
control = Control(u) | ||
# the direction of the perturbation | ||
h = Function(V).interpolate(fexpr * np.random.rand()) | ||
|
||
# the functional as a pure function of nu | ||
Jhat = ReducedFunctional(J, control) | ||
|
||
if nu_is_control: | ||
assert np.allclose(J, Jhat(nu)) | ||
assert taylor_test(Jhat, nu, h) > 1.95 | ||
else: | ||
assert np.allclose(J, Jhat(u)) | ||
assert taylor_test(Jhat, u, h) > 1.95 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Here we should use Function in Real space instead using
Constant
.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.
However, we have to have access to the mesh at this point for using Function in Real Space.