Skip to content

Commit df36259

Browse files
authored
Merge pull request #133 from dolfin-adjoint/dolci/fix_warnings
Fix the warnings with Constant and subfunctions.
2 parents 9bfe70f + 5763641 commit df36259

9 files changed

+69
-57
lines changed

tests/firedrake_adjoint/test_assignment.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,23 @@ def test_assign_tlm():
7777
def test_assign_tlm_with_constant():
7878
mesh = IntervalMesh(10, 0, 1)
7979
V = FunctionSpace(mesh, "CG", 1)
80+
R = FunctionSpace(mesh, "R", 0)
8081

8182
x = SpatialCoordinate(mesh)
8283
f = assemble(interpolate(x[0], V))
8384
g = assemble(interpolate(sin(x[0]), V))
84-
c = Constant(5.0, domain=mesh)
85+
c = Function(R, val=5.0)
8586

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

89-
c.block_variable.tlm_value = Constant(0.3, domain=mesh)
90+
c.block_variable.tlm_value = Function(R, val=0.3)
9091
tape = get_working_tape()
9192
tape.evaluate_tlm()
9293
assert_allclose(u.block_variable.tlm_value.dat.data, 0.3 * f.dat.data ** 2)
9394

9495
tape.reset_tlm_values()
95-
c.block_variable.tlm_value = Constant(0.4, domain=mesh)
96+
c.block_variable.tlm_value = Function(R, val=0.4)
9697
f.block_variable.tlm_value = g
9798
tape.evaluate_tlm()
9899
assert_allclose(u.block_variable.tlm_value.dat.data, 0.4 * f.dat.data ** 2 + 10. * f.dat.data * g.dat.data)
@@ -143,11 +144,11 @@ def test_assign_nonlincom():
143144
def test_assign_with_constant():
144145
mesh = IntervalMesh(10, 0, 1)
145146
V = FunctionSpace(mesh, "CG", 1)
146-
147+
R = FunctionSpace(mesh, "R", 0)
147148
x = SpatialCoordinate(mesh)
148149
f = assemble(interpolate(x[0], V))
149-
c = Constant(3.0, domain=mesh)
150-
d = Constant(2.0, domain=mesh)
150+
c = Function(R, val=3.0)
151+
d = Function(R, val=2.0)
151152
u = Function(V)
152153

153154
u.assign(c*f+d**3)
@@ -198,9 +199,9 @@ def test_assign_nonlin_changing():
198199
def test_assign_constant_scale():
199200
mesh = UnitSquareMesh(10, 10)
200201
V = VectorFunctionSpace(mesh, "CG", 1)
201-
202+
R = FunctionSpace(mesh, "R", 0)
202203
f = Function(V)
203-
c = Constant(2.0, domain=mesh)
204+
c = Function(R, val=2.0)
204205
x, y = SpatialCoordinate(mesh)
205206
g = assemble(interpolate(as_vector([sin(y)+x, cos(x)*y]), V))
206207

@@ -209,8 +210,7 @@ def test_assign_constant_scale():
209210
J = assemble(inner(f, f) ** 2 * dx)
210211

211212
rf = ReducedFunctional(J, Control(c))
212-
h = Constant(0.1)
213-
r = taylor_to_dict(rf, c, h)
213+
r = taylor_to_dict(rf, c, Constant(0.1))
214214

215215
assert min(r["R0"]["Rate"]) > 0.9
216216
assert min(r["R1"]["Rate"]) > 1.9

tests/firedrake_adjoint/test_disk_checkpointing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def adjoint_example(fine, coarse):
1919
# AssembleBlock
2020
m = assemble(interpolate(sin(4*pi*x)*cos(4*pi*y), cg_space))
2121

22-
u, v = w.split()
22+
u, v = w.subfunctions
2323
# FunctionAssignBlock, FunctionMergeBlock
2424
v.assign(m)
2525
# FunctionSplitBlock, GenericSolveBlock

tests/firedrake_adjoint/test_hessian.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,16 @@ def test_function():
107107

108108
mesh = IntervalMesh(10, 0, 1)
109109
V = FunctionSpace(mesh, "Lagrange", 2)
110-
111-
c = Constant(4, domain=mesh)
110+
R = FunctionSpace(mesh, "R", 0)
111+
c = Function(R, val=4)
112112
control_c = Control(c)
113113
f = Function(V)
114114
f.vector()[:] = 3
115115
control_f = Control(f)
116116

117117
u = Function(V)
118118
v = TestFunction(V)
119-
bc = DirichletBC(V, Constant(1, domain=mesh), "on_boundary")
119+
bc = DirichletBC(V, Function(R, val=1), "on_boundary")
120120

121121
F = inner(grad(u), grad(v)) * dx + u**2*v*dx - f ** 2 * v * dx - c**2*v*dx
122122
solve(F == 0, u, bc)
@@ -127,7 +127,7 @@ def test_function():
127127
dJdc, dJdf = compute_gradient(J, [control_c, control_f])
128128

129129
# Step direction for derivatives and convergence test
130-
h_c = Constant(1.0, domain=mesh)
130+
h_c = Function(R, val=1.0)
131131
h_f = Function(V)
132132
h_f.vector()[:] = 10*rng.random(V.dim())
133133

@@ -148,13 +148,13 @@ def test_nonlinear():
148148

149149
mesh = UnitSquareMesh(10, 10)
150150
V = FunctionSpace(mesh, "Lagrange", 1)
151-
151+
R = FunctionSpace(mesh, "R", 0)
152152
f = Function(V)
153153
f.vector()[:] = 5
154154

155155
u = Function(V)
156156
v = TestFunction(V)
157-
bc = DirichletBC(V, Constant(1, domain=mesh), "on_boundary")
157+
bc = DirichletBC(V, Function(R, val=1), "on_boundary")
158158

159159
F = inner(grad(u), grad(v)) * dx - u**2*v*dx - f * v * dx
160160
solve(F == 0, u, bc)

tests/firedrake_adjoint/test_optimisation.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
def test_optimisation_constant_control():
1010
"""This tests a list of controls in a minimisation (through scipy L-BFGS-B)"""
1111
mesh = UnitSquareMesh(1, 1)
12+
R = FunctionSpace(mesh, "R", 0)
1213

1314
n = 3
14-
x = [Constant(0., domain=mesh) for i in range(n)]
15+
x = [Function(R) for i in range(n)]
1516
c = [Control(xi) for xi in x]
1617

1718
# Rosenbrock function https://en.wikipedia.org/wiki/Rosenbrock_function

tests/firedrake_adjoint/test_projection.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -135,36 +135,36 @@ def test_project_nonlin_changing():
135135
def test_self_project():
136136
mesh = UnitSquareMesh(1,1)
137137
V = FunctionSpace(mesh, "CG", 1)
138+
R = FunctionSpace(mesh, "R", 0)
138139
u = Function(V)
139-
c = Constant(1., domain=mesh)
140+
c = Function(R, val=1.)
140141
u.project(u+c)
141142
J = assemble(u**2*dx)
142143
rf = ReducedFunctional(J, Control(c))
143-
h = Constant(0.1)
144-
assert taylor_test(rf, Constant(2., domain=mesh), h)
144+
assert taylor_test(rf, Function(R, val=2.), Constant(0.1))
145145

146146
def test_self_project_function():
147147
mesh = UnitSquareMesh(1,1)
148148
V = FunctionSpace(mesh, "CG", 1)
149+
R = FunctionSpace(mesh, "R", 0)
149150
u = Function(V)
150-
c = Constant(1., domain=mesh)
151+
c = Function(R, val=1.)
151152
project(u+c, u)
152153
project(u+c*u**2, u)
153154
J = assemble(u**2*dx)
154155
rf = ReducedFunctional(J, Control(c))
155-
h = Constant(0.1)
156-
assert taylor_test(rf, Constant(3., domain=mesh), h)
156+
assert taylor_test(rf, Function(R, val=3.), Constant(0.1))
157157

158158
def test_project_to_function_space():
159159
mesh = UnitSquareMesh(1,1)
160160
V = FunctionSpace(mesh, "CG", 1)
161161
W = FunctionSpace(mesh, "DG", 1)
162+
R = FunctionSpace(mesh, "R", 0)
162163
u = Function(V)
163164
x = SpatialCoordinate(mesh)
164165
u.interpolate(x[0])
165-
c = Constant(1., domain=mesh)
166+
c = Function(R, val=1.)
166167
w = project((u+c)*u, W)
167168
J = assemble(w**2*dx)
168169
rf = ReducedFunctional(J, Control(c))
169-
h = Constant(0.1)
170-
assert taylor_test(rf, Constant(1., domain=mesh), h)
170+
assert taylor_test(rf, Function(R, val=1.), Constant(0.1)) > 1.9

tests/firedrake_adjoint/test_reduced_functional.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@
99
def test_constant():
1010
mesh = IntervalMesh(10, 0, 1)
1111
V = FunctionSpace(mesh, "Lagrange", 1)
12+
R = FunctionSpace(mesh, "R", 0)
1213

13-
c = Constant(1, domain=mesh)
14+
c = Function(R, val=1)
1415
f = Function(V)
1516
f.vector()[:] = 1
1617

1718
u = Function(V)
1819
v = TestFunction(V)
19-
bc = DirichletBC(V, Constant(1, domain=mesh), "on_boundary")
20+
bc = DirichletBC(V, Function(R, val=1), "on_boundary")
2021

2122
F = inner(grad(u), grad(v))*dx - f**2*v*dx
2223
solve(F == 0, u, bc)
2324

2425
J = assemble(c**2*u*dx)
2526
Jhat = ReducedFunctional(J, Control(c))
26-
assert taylor_test(Jhat, c, Constant(1, domain=mesh)) > 1.9
27+
assert taylor_test(Jhat, c, Function(R, val=1)) > 1.9
2728

2829

2930
def test_function():
@@ -54,6 +55,7 @@ def test_wrt_function_dirichlet_boundary(control):
5455
mesh = UnitSquareMesh(10,10)
5556

5657
V = FunctionSpace(mesh,"CG",1)
58+
R = FunctionSpace(mesh,"R",0)
5759
u = TrialFunction(V)
5860
u_ = Function(V)
5961
v = TestFunction(V)
@@ -64,8 +66,8 @@ def test_wrt_function_dirichlet_boundary(control):
6466
bc2 = DirichletBC(V, 2, 2)
6567
bc = [bc1,bc2]
6668

67-
g1 = Constant(2, domain=mesh)
68-
g2 = Constant(1, domain=mesh)
69+
g1 = Function(R, val=2)
70+
g2 = Function(R, val=1)
6971
f = Function(V)
7072
f.vector()[:] = 10
7173

@@ -166,10 +168,11 @@ def test_mixed_boundary():
166168
def test_assemble_recompute():
167169
mesh = UnitSquareMesh(10, 10)
168170
V = FunctionSpace(mesh, "CG", 1)
171+
R = FunctionSpace(mesh, "R", 0)
169172

170173
f = Function(V)
171174
f.vector()[:] = 2
172-
expr = Constant(assemble(f**2*dx), domain=mesh)
175+
expr = Function(R).assign(assemble(f**2*dx))
173176
J = assemble(expr**2*dx(domain=mesh))
174177
Jhat = ReducedFunctional(J, Control(f))
175178

tests/firedrake_adjoint/test_solving.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
def test_linear_problem():
1010
mesh = IntervalMesh(10, 0, 1)
1111
V = FunctionSpace(mesh, "Lagrange", 1)
12-
12+
R = FunctionSpace(mesh, "R", 0)
1313
f = Function(V)
1414
f.vector()[:] = 1
1515

1616
u = TrialFunction(V)
1717
u_ = Function(V)
1818
v = TestFunction(V)
19-
bc = DirichletBC(V, Constant(1, domain=mesh), "on_boundary")
19+
bc = DirichletBC(V, Function(R, val=1), "on_boundary")
2020

2121
def J(f):
2222
a = inner(grad(u), grad(v))*dx
@@ -60,13 +60,13 @@ def test_nonlinear_problem():
6060
"""This tests whether nullspace and solver_parameters are passed on in adjoint solves"""
6161
mesh = IntervalMesh(10, 0, 1)
6262
V = FunctionSpace(mesh, "Lagrange", 1)
63-
63+
R = FunctionSpace(mesh, "R", 0)
6464
f = Function(V)
6565
f.vector()[:] = 1
6666

6767
u = Function(V)
6868
v = TestFunction(V)
69-
bc = DirichletBC(V, Constant(1, domain=mesh), "on_boundary")
69+
bc = DirichletBC(V, Function(R, val=1), "on_boundary")
7070

7171
def J(f):
7272
a = f*inner(grad(u), grad(v))*dx + u**2*v*dx - f*v*dx
@@ -160,6 +160,7 @@ def test_wrt_function_neumann_boundary():
160160
mesh = UnitSquareMesh(10,10)
161161

162162
V = FunctionSpace(mesh,"CG",1)
163+
R = FunctionSpace(mesh,"R",0)
163164
u = TrialFunction(V)
164165
u_ = Function(V)
165166
v = TestFunction(V)
@@ -169,8 +170,8 @@ def test_wrt_function_neumann_boundary():
169170
bc2 = DirichletBC(V, 2, 2)
170171
bc = [bc1,bc2]
171172

172-
g1 = Constant(2, domain=mesh)
173-
g2 = Constant(1, domain=mesh)
173+
g1 = Function(R, val=2)
174+
g2 = Function(R, val=1)
174175
f = Function(V)
175176
f.vector()[:] = 10
176177

@@ -188,13 +189,14 @@ def J(g1):
188189
def test_wrt_constant():
189190
mesh = IntervalMesh(10, 0, 1)
190191
V = FunctionSpace(mesh, "Lagrange", 1)
192+
R = FunctionSpace(mesh, "R", 0)
191193

192-
c = Constant(1, domain=mesh)
194+
c = Function(R, val=1)
193195

194196
u = TrialFunction(V)
195197
u_ = Function(V)
196198
v = TestFunction(V)
197-
bc = DirichletBC(V, Constant(1, domain=mesh), "on_boundary")
199+
bc = DirichletBC(V, Function(R, val=1), "on_boundary")
198200

199201
def J(c):
200202
a = inner(grad(u), grad(v))*dx
@@ -209,6 +211,7 @@ def test_wrt_constant_neumann_boundary():
209211
mesh = UnitSquareMesh(10,10)
210212

211213
V = FunctionSpace(mesh,"CG",1)
214+
R = FunctionSpace(mesh,"R",0)
212215
u = TrialFunction(V)
213216
u_ = Function(V)
214217
v = TestFunction(V)
@@ -218,8 +221,8 @@ def test_wrt_constant_neumann_boundary():
218221
bc2 = DirichletBC(V, 2, 2)
219222
bc = [bc1,bc2]
220223

221-
g1 = Constant(2, domain=mesh)
222-
g2 = Constant(1, domain=mesh)
224+
g1 = Function(R, val=2)
225+
g2 = Function(R, val=1)
223226
f = Function(V)
224227
f.vector()[:] = 10
225228

@@ -240,6 +243,7 @@ def test_time_dependent():
240243

241244
# Defining function space, test and trial functions
242245
V = FunctionSpace(mesh,"CG",1)
246+
R = FunctionSpace(mesh,"R",0)
243247
u = TrialFunction(V)
244248
u_ = Function(V)
245249
v = TestFunction(V)
@@ -252,7 +256,7 @@ def test_time_dependent():
252256
# Some variables
253257
T = 0.2
254258
dt = 0.1
255-
f = Constant(1, domain=mesh)
259+
f = Function(R, val=1)
256260

257261
def J(f):
258262
u_1 = Function(V)
@@ -277,11 +281,12 @@ def test_two_nonlinear_solves():
277281
# regression test for firedrake issue #1841
278282
mesh = UnitSquareMesh(1,1)
279283
V = FunctionSpace(mesh, "CG", 1)
284+
R = FunctionSpace(mesh, "R", 0)
280285
v = TestFunction(V)
281286
u0 = Function(V)
282287
u1 = Function(V)
283288

284-
ui = Constant(2.0, domain=mesh)
289+
ui = Function(R, val=2.0)
285290
c = Control(ui)
286291
u0.assign(ui)
287292
F = dot(v, (u1-u0))*dx - dot(v, u0*u1)*dx

0 commit comments

Comments
 (0)