Skip to content

Commit

Permalink
Merge pull request #127 from dolfin-adjoint/interp
Browse files Browse the repository at this point in the history
Interpolate: Update tests
  • Loading branch information
dham authored Jan 18, 2024
2 parents f194553 + 361e920 commit 2c6614d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 39 deletions.
37 changes: 19 additions & 18 deletions tests/firedrake_adjoint/test_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pytest.importorskip("firedrake")

from firedrake import *
from firedrake.__future__ import *
from firedrake.adjoint import *

from numpy.random import rand
Expand All @@ -13,8 +14,8 @@ def test_assign_linear_combination():
V = FunctionSpace(mesh, "CG", 1)

x, = SpatialCoordinate(mesh)
f = interpolate(x, V)
g = interpolate(sin(x), V)
f = assemble(interpolate(x, V))
g = assemble(interpolate(sin(x), V))
u = Function(V)

u.assign(3*f + g)
Expand All @@ -33,8 +34,8 @@ def test_assign_vector_valued():
V = FunctionSpace(mesh, element)

x = SpatialCoordinate(mesh)
f = interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)
g = interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)
f = assemble(interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V))
g = assemble(interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V))
u = Function(V)

u.assign(f - 0.5*g)
Expand All @@ -53,8 +54,8 @@ def test_assign_tlm():
V = FunctionSpace(mesh, element)

x = SpatialCoordinate(mesh)
f = interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)
g = interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)
f = assemble(interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V))
g = assemble(interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V))
u = Function(V)

u.assign(f - 0.5*g)
Expand All @@ -78,20 +79,20 @@ def test_assign_tlm_with_constant():
V = FunctionSpace(mesh, "CG", 1)

x = SpatialCoordinate(mesh)
f = interpolate(x[0], V)
g = interpolate(sin(x[0]), V)
f = assemble(interpolate(x[0], V))
g = assemble(interpolate(sin(x[0]), V))
c = Constant(5.0, domain=mesh)

u = Function(V)
u.interpolate(c * f**2)

c.block_variable.tlm_value = Constant(0.3)
c.block_variable.tlm_value = Constant(0.3, domain=mesh)
tape = get_working_tape()
tape.evaluate_tlm()
assert_allclose(u.block_variable.tlm_value.dat.data, 0.3 * f.dat.data ** 2)

tape.reset_tlm_values()
c.block_variable.tlm_value = Constant(0.4)
c.block_variable.tlm_value = Constant(0.4, domain=mesh)
f.block_variable.tlm_value = g
tape.evaluate_tlm()
assert_allclose(u.block_variable.tlm_value.dat.data, 0.4 * f.dat.data ** 2 + 10. * f.dat.data * g.dat.data)
Expand All @@ -103,8 +104,8 @@ def test_assign_hessian():
V = FunctionSpace(mesh, element)

x = SpatialCoordinate(mesh)
f = interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)
g = interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)
f = assemble(interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V))
g = assemble(interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V))
u = Function(V)

u.assign(f - 0.5*g)
Expand All @@ -125,8 +126,8 @@ def test_assign_nonlincom():
V = FunctionSpace(mesh, "CG", 1)

x = SpatialCoordinate(mesh)
f = interpolate(x[0], V)
g = interpolate(sin(x[0]), V)
f = assemble(interpolate(x[0], V))
g = assemble(interpolate(sin(x[0]), V))
u = Function(V)

u.interpolate(f*g)
Expand All @@ -144,7 +145,7 @@ def test_assign_with_constant():
V = FunctionSpace(mesh, "CG", 1)

x = SpatialCoordinate(mesh)
f = interpolate(x[0], V)
f = assemble(interpolate(x[0], V))
c = Constant(3.0, domain=mesh)
d = Constant(2.0, domain=mesh)
u = Function(V)
Expand All @@ -166,8 +167,8 @@ def test_assign_nonlin_changing():
V = FunctionSpace(mesh, "CG", 1)

x = SpatialCoordinate(mesh)
f = interpolate(x[0], V)
g = interpolate(sin(x[0]), V)
f = assemble(interpolate(x[0], V))
g = assemble(interpolate(sin(x[0]), V))
control = Control(g)

test = TestFunction(V)
Expand Down Expand Up @@ -201,7 +202,7 @@ def test_assign_constant_scale():
f = Function(V)
c = Constant(2.0, domain=mesh)
x, y = SpatialCoordinate(mesh)
g = interpolate(as_vector([sin(y)+x, cos(x)*y]), V)
g = assemble(interpolate(as_vector([sin(y)+x, cos(x)*y]), V))

f.assign(c * g)

Expand Down
7 changes: 4 additions & 3 deletions tests/firedrake_adjoint/test_disk_checkpointing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pytest.importorskip("firedrake")

from firedrake import *
from firedrake.__future__ import *
from firedrake.adjoint import *
import numpy as np
import os
Expand All @@ -15,8 +16,8 @@ def adjoint_example(fine, coarse):
w = Function(W)

x, y = SpatialCoordinate(fine)
# InterpolateBlock
m = interpolate(sin(4*pi*x)*cos(4*pi*y), cg_space)
# AssembleBlock
m = assemble(interpolate(sin(4*pi*x)*cos(4*pi*y), cg_space))

u, v = w.split()
# FunctionAssignBlock, FunctionMergeBlock
Expand All @@ -37,7 +38,7 @@ def adjoint_example(fine, coarse):
Jhat = ReducedFunctional(J, Control(m))

with stop_annotating():
m_new = interpolate(sin(4*pi*x)*cos(4*pi*y), cg_space)
m_new = assemble(interpolate(sin(4*pi*x)*cos(4*pi*y), cg_space))
checkpointer = get_working_tape()._package_data["firedrake"]
init_file_timestamp = os.stat(checkpointer.init_checkpoint_file.name).st_mtime
current_file_timestamp = os.stat(checkpointer.current_checkpoint_file.name).st_mtime
Expand Down
21 changes: 11 additions & 10 deletions tests/firedrake_adjoint/test_projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pytest.importorskip("firedrake")

from firedrake import *
from firedrake.__future__ import *
from firedrake.adjoint import *

from numpy.random import rand
Expand All @@ -13,8 +14,8 @@ def test_project_vector_valued():
V = FunctionSpace(mesh, element)

x = SpatialCoordinate(mesh)
f = interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)
g = interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)
f = assemble(interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V))
g = assemble(interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V))
u = Function(V)

u.project(f - 0.5*g)
Expand All @@ -33,8 +34,8 @@ def test_project_tlm():
V = FunctionSpace(mesh, element)

x = SpatialCoordinate(mesh)
f = interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)
g = interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)
f = assemble(interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V))
g = assemble(interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V))
u = Function(V)

u.project(f - 0.5*g)
Expand All @@ -58,8 +59,8 @@ def test_project_hessian():
V = FunctionSpace(mesh, element)

x = SpatialCoordinate(mesh)
f = interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)
g = interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)
f = assemble(interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V))
g = assemble(interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V))
u = Function(V)

u.project(f - 0.5*g)
Expand All @@ -85,8 +86,8 @@ def test_project_nonlincom():
V3 = FunctionSpace(mesh, element)

x = SpatialCoordinate(mesh)
f = interpolate(x[0], V1)
g = interpolate(sin(x[0]), V2)
f = assemble(interpolate(x[0], V1))
g = assemble(interpolate(sin(x[0]), V2))
u = Function(V3)

u.project(f*g)
Expand All @@ -104,8 +105,8 @@ def test_project_nonlin_changing():
V = FunctionSpace(mesh, "CG", 1)

x = SpatialCoordinate(mesh)
f = interpolate(x[0], V)
g = interpolate(sin(x[0]), V)
f = assemble(interpolate(x[0], V))
g = assemble(interpolate(sin(x[0]), V))
control = Control(g)

test = TestFunction(V)
Expand Down
5 changes: 3 additions & 2 deletions tests/firedrake_adjoint/test_shape_derivatives.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import numpy as np
from firedrake import *
from firedrake.__future__ import *
from firedrake.adjoint import *
from pyadjoint import taylor_to_dict

Expand Down Expand Up @@ -221,7 +222,7 @@ def test_multiple_assignments():
dJdm = Jhat.derivative()

pert = as_vector((x * y, sin(x)))
pert = interpolate(pert, S)
pert = assemble(interpolate(pert, S))
results = taylor_to_dict(Jhat, s, pert)

assert min(results["R0"]["Rate"]) > 0.9
Expand Down Expand Up @@ -252,7 +253,7 @@ def test_multiple_assignments():
dJdm.vector().get_local())

pert = as_vector((x * y, sin(x)))
pert = interpolate(pert, S)
pert = assemble(interpolate(pert, S))
results = taylor_to_dict(Jhat, s, pert)

assert min(results["R0"]["Rate"]) > 0.9
Expand Down
6 changes: 0 additions & 6 deletions tests/firedrake_adjoint/test_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
@pytest.fixture(params=[
"constant assign",
"function assign",
"interpolate",
"project",
"supermesh project",
"interpolate method",
"project method",
"supermesh project method",
])
Expand All @@ -36,10 +34,6 @@ def test_tags(tag):
f2.assign(1.0)
if tag == "function assign":
f1.assign(f2, ad_block_tag=tag)
elif tag == "interpolate":
f1 = interpolate(f2, V, ad_block_tag=tag)
elif tag == "interpolate method":
f1.interpolate(f2, ad_block_tag=tag)
elif "project method" in tag:
f1.project(f2, ad_block_tag=tag)
elif "project" in tag:
Expand Down

0 comments on commit 2c6614d

Please sign in to comment.