Skip to content

Commit 003bbbb

Browse files
authored
Merge pull request #149 from dolfin-adjoint/dolci/fix_tests_for_L2_riesz
Fix tests for L2 riesz maps
2 parents a97e4d6 + d9ca94e commit 003bbbb

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

pyadjoint/control.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .overloaded_type import OverloadedType, create_overloaded_object
2-
import warnings
2+
import logging
33

44

55
class Control(object):
@@ -47,13 +47,13 @@ def tape_value(self):
4747

4848
def get_derivative(self, options={}):
4949
if self.block_variable.adj_value is None:
50-
warnings.warn("Adjoint value is None, is the functional independent of the control variable?")
50+
logging.warning("Adjoint value is None, is the functional independent of the control variable?")
5151
return self.control._ad_convert_type(0., options=options)
5252
return self.control._ad_convert_type(self.block_variable.adj_value, options=options)
5353

5454
def get_hessian(self, options={}):
5555
if self.block_variable.adj_value is None:
56-
warnings.warn("Hessian value is None, is the functional independent of the control variable?")
56+
logging.warning("Hessian value is None, is the functional independent of the control variable?")
5757
return self.control._ad_convert_type(0., options=options)
5858
return self.control._ad_convert_type(self.block_variable.hessian_value, options=options)
5959

tests/firedrake_adjoint/test_assemble.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
pytest.importorskip("firedrake")
44

55
from numpy.random import rand
6-
from numpy.testing import assert_approx_equal
6+
from numpy.testing import assert_allclose
77

88
from firedrake import *
99
from firedrake.adjoint import *
@@ -22,8 +22,8 @@ def test_assemble_0_forms():
2222
# where stored as a Function instead of Vector()
2323
s = a1 + a2 + 2.0 * a3
2424
rf = ReducedFunctional(s, Control(u))
25-
# derivative is: (1+2*u+6*u**2)*dx - summing is equivalent to testing with 1
26-
assert_approx_equal(rf.derivative().vector().sum(), 1. + 2. * 4 + 6 * 16.)
25+
dJdm = rf.derivative()
26+
assert_allclose(dJdm.dat.data_ro, 1. + 2. * 4. + 6. * 16.)
2727

2828

2929
def test_assemble_0_forms_mixed():
@@ -41,7 +41,9 @@ def test_assemble_0_forms_mixed():
4141
s -= a3 # this is done deliberately to end up with an adj_input of 0.0 for the a3 AssembleBlock
4242
rf = ReducedFunctional(s, Control(u))
4343
# derivative is: (1+4*u)*dx - summing is equivalent to testing with 1
44-
assert_approx_equal(rf.derivative().vector().sum(), 1. + 4. * 7)
44+
dJdm = rf.derivative()
45+
assert_allclose(dJdm.dat.data_ro[0], 1. + 4. * 7)
46+
assert_allclose(dJdm.dat.data_ro[1], 0.0)
4547

4648

4749
def test_assemble_1_forms_adjoint():

tests/firedrake_adjoint/test_hessian.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def test_simple_solve():
4343
tape.evaluate_adj()
4444

4545
m = f.copy(deepcopy=True)
46-
dJdm = Jhat.derivative().vector().inner(h.vector())
47-
Hm = Jhat.hessian(h).vector().inner(h.vector())
46+
dJdm = assemble(inner(Jhat.derivative(), h)*dx)
47+
Hm = assemble(inner(Jhat.hessian(h), h)*dx)
4848
assert taylor_test(Jhat, m, h, dJdm=dJdm, Hm=Hm) > 2.9
4949

5050

@@ -133,15 +133,13 @@ def test_function():
133133

134134
# Total derivative
135135
dJdc, dJdf = compute_gradient(J, [control_c, control_f])
136-
dJdm = dJdc.vector().inner(h_c) + dJdf.vector().inner(h_f)
136+
dJdm = assemble(dJdc * h_c * dx + dJdf * h_f * dx)
137137

138138
# Hessian
139139
Hcc, Hff = compute_hessian(J, [control_c, control_f], [h_c, h_f])
140-
Hm = Hff.vector().inner(h_f.vector()) + Hcc.vector().inner(h_c.vector())
141-
140+
Hm = assemble(Hcc * h_c * dx + Hff * h_f * dx)
142141
assert taylor_test(Jhat, [c, f], [h_c, h_f], dJdm=dJdm, Hm=Hm) > 2.9
143142

144-
145143
def test_nonlinear():
146144
tape = Tape()
147145
set_working_tape(tape)

tests/firedrake_adjoint/test_shape_derivatives.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ def test_sin_weak_spatial():
2121
computed = Jhat.derivative().vector().get_local()
2222

2323
V = TestFunction(S)
24-
dJV = div(V)*sin(x[0])*dx + V[0]*cos(x[0])*dx
25-
actual = assemble(dJV).vector().get_local()
24+
# Derivative (Cofunction)
25+
dJV = assemble(div(V)*sin(x[0])*dx + V[0]*cos(x[0])*dx)
26+
# Apply L2 riesz representation to obtain the gradient.
27+
actual = dJV.riesz_representation().vector().get_local()
2628
assert np.allclose(computed, actual, rtol=1e-14)
2729

2830

@@ -71,7 +73,7 @@ def test_shape_hessian():
7173
s = Function(S,name="deform")
7274

7375
mesh.coordinates.assign(mesh.coordinates + s)
74-
J = assemble(sin(x[1])* dx(domain=mesh))
76+
J = assemble(cos(x[1])* dx(domain=mesh))
7577
c = Control(s)
7678
Jhat = ReducedFunctional(J, c)
7779

@@ -80,12 +82,13 @@ def test_shape_hessian():
8082
h.interpolate(as_vector((cos(x[2]), A*cos(x[1]), A*x[1])))
8183

8284
# Second order taylor
83-
dJdm = Jhat.derivative().vector().inner(h.vector())
84-
Hm = compute_hessian(J, c, h).vector().inner(h.vector())
85+
dJdm = assemble(inner(Jhat.derivative(), h)*dx)
86+
Hm = assemble(inner(compute_hessian(J, c, h), h)*dx)
8587
r2 = taylor_test(Jhat, s, h, dJdm=dJdm, Hm=Hm)
88+
print(r2)
8689
assert(r2 > 2.9)
8790
Jhat(s)
88-
dJdmm_exact = derivative(derivative(sin(x[1])* dx(domain=mesh),x,h), x, h)
91+
dJdmm_exact = derivative(derivative(cos(x[1]) * dx(domain=mesh), x, h), x, h)
8992
assert(np.isclose(assemble(dJdmm_exact), Hm))
9093

9194

@@ -108,8 +111,8 @@ def test_PDE_hessian_neumann():
108111
l = f*v*dx
109112
u = Function(V)
110113
solve(a==l, u, solver_parameters={'ksp_type':'preonly', 'pc_type':'lu',
111-
"mat_type": "aij",
112-
"pc_factor_mat_solver_type": "mumps"})
114+
"mat_type": "aij",
115+
"pc_factor_mat_solver_type": "mumps"})
113116
J = assemble(u*dx(domain=mesh))
114117
c = Control(s)
115118
Jhat = ReducedFunctional(J, c)
@@ -136,8 +139,8 @@ def test_PDE_hessian_neumann():
136139
Jhat(s)
137140

138141
# # Second order taylor
139-
dJdm = Jhat.derivative().vector().inner(h.vector())
140-
Hm = compute_hessian(J, c, h).vector().inner(h.vector())
142+
dJdm = assemble(inner(Jhat.derivative(), h)*dx)
143+
Hm = assemble(inner(compute_hessian(J, c, h), h)*dx)
141144
r2 = taylor_test(Jhat, s, h, dJdm=dJdm, Hm=Hm)
142145
assert(r2>2.95)
143146

@@ -190,8 +193,8 @@ def test_PDE_hessian_dirichlet():
190193
Jhat(s)
191194

192195
# # Second order taylor
193-
dJdm = Jhat.derivative().vector().inner(h.vector())
194-
Hm = compute_hessian(J, c, h).vector().inner(h.vector())
196+
dJdm = assemble(inner(Jhat.derivative(), h)*dx)
197+
Hm = assemble(inner(compute_hessian(J, c, h), h)*dx)
195198
r2 = taylor_test(Jhat, s, h, dJdm=dJdm, Hm=Hm)
196199
assert(r2>2.95)
197200

0 commit comments

Comments
 (0)