Debian Codegraph @

python/test/unit/fem/test_coordinate_derivative.py

raw
1 """Unit tests for coordinate derivative"""
2
3 # Copyright (C) 2018 Florian Wechsung
4 #
5 # This file is part of DOLFIN.
6 #
7 # DOLFIN is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # DOLFIN is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
19 #
20 # Modified by Florian Wechsung 2018 and Jørgen S. Dokken 2020
21
22 import pytest
23 import numpy as np
24 from dolfin import *
25 from ufl_legacy import replace
26 from ufl_legacy.log import UFLException
27 from dolfin_utils.test import skip_in_parallel
28
29 def test_first_shape_derivative():
30 mesh = UnitSquareMesh(6, 6)
31 n = FacetNormal(mesh)
32 X = SpatialCoordinate(mesh)
33 x, y = X
34 V = FunctionSpace(mesh, "Lagrange", 1)
35 u = project(x*x+y*x+y*y+sin(x)+cos(x), V)
36 dX = TestFunction(VectorFunctionSpace(mesh, "Lagrange", 1))
37
38 def test_first(J, dJ):
39 computed = assemble(derivative(J, X)).get_local()
40 actual = assemble(dJ).get_local()
41 assert np.allclose(computed, actual, rtol=1e-14)
42
43 Ja = u * u * dx
44 dJa = u * u * div(dX) * dx
45 test_first(Ja, dJa)
46
47 Jb = inner(grad(u), grad(u)) * dx
48 dJb = -2*inner(dot(grad(dX), grad(u)), grad(u)) * dx + inner(grad(u), grad(u)) * div(dX) * dx
49 test_first(Jb, dJb)
50
51 f = x * y * sin(x) * cos(y)
52 Jc = f * dx
53 dJc = div(f * dX) * dx
54 test_first(Jc, dJc)
55
56 Jd = f * ds
57 dJd = inner(grad(f), dX) * ds \
58 + f * (div(dX) - inner(dot(grad(dX), n), n)) * ds
59 test_first(Jd, dJd)
60
61 J = Ja + Jb + Jc + Jd
62 dJ = dJa + dJb + dJc + dJd
63 test_first(J, dJ)
64
65
66 def test_mixed_derivatives():
67 mesh = UnitSquareMesh(6, 6)
68 X = SpatialCoordinate(mesh)
69 x, y = X
70 V = FunctionSpace(mesh, "CG", 1)
71 u = project(x * x + y * x + y * y + sin(x) + cos(x), V)
72 v = TrialFunction(V)
73 dX = TestFunction(VectorFunctionSpace(mesh, "Lagrange", 1))
74 dX_ = TrialFunction(VectorFunctionSpace(mesh, "Lagrange", 1))
75
76
77 def test_mixed(J, dJ_manual):
78 computed1 = assemble(derivative(derivative(J, X, dX), u)).norm("frobenius")
79 computed2 = assemble(derivative(derivative(J, u), X, dX_)).norm("frobenius")
80 computed3 = assemble(derivative(derivative(J, X), u)).norm("frobenius")
81 computed4 = assemble(derivative(derivative(J, u), X)).norm("frobenius")
82 actuala = assemble(dJ_manual).norm("frobenius")
83 assert np.isclose(computed1, actuala, rtol=1e-14)
84 assert np.isclose(computed2, actuala, rtol=1e-14)
85 assert np.isclose(computed3, actuala, rtol=1e-14)
86 assert np.isclose(computed4, actuala, rtol=1e-14)
87
88 Ja = u * u * dx
89 dJa = 2 * u * v * div(dX) * dx
90 test_mixed(Ja, dJa)
91
92 Jb = inner(grad(u), grad(u)) * dx
93 dJb = 2*inner(grad(u), grad(v)) * div(dX) * dx \
94 - 2*inner(dot(nabla_grad(dX), grad(u)), grad(v)) * dx \
95 - 2*inner(grad(u), dot(nabla_grad(dX), grad(v))) * dx
96 test_mixed(Jb, dJb)
97
98 J = Ja + Jb
99 dJ = dJa + dJb
100 test_mixed(J, dJ)
101
102
103
104 def test_second_shape_derivative():
105 mesh = UnitSquareMesh(6, 6)
106 V = FunctionSpace(mesh, "CG", 1)
107 X = SpatialCoordinate(mesh)
108 x, y = X
109 u = project(x * x + y * x + y * y + sin(x) + cos(x), V)
110 Z = VectorFunctionSpace(mesh, "Lagrange", 1)
111 dX1 = TestFunction(Z)
112 dX2 = TrialFunction(Z)
113
114 def test_second(J, ddJ):
115 computed = assemble(derivative(derivative(J, X, dX1), X, dX2)).norm("frobenius")
116 actual = assemble(ddJ).norm("frobenius")
117 assert np.isclose(computed, actual, rtol=1e-14)
118
119 Ja = u * u * dx
120 ddJa = u * u * div(dX1) * div(dX2) * dx - u * u * tr(grad(dX1)*grad(dX2)) * dx
121 test_second(Ja, ddJa)
122
123 Jb = inner(grad(u), grad(u)) * dx
124 ddJb = 2*inner(dot(dot(nabla_grad(dX2), nabla_grad(dX1)), grad(u)), grad(u)) * dx \
125 + 2*inner(dot(nabla_grad(dX1), dot(nabla_grad(dX2), grad(u))), grad(u)) * dx \
126 + 2*inner(dot(nabla_grad(dX1), grad(u)), dot(nabla_grad(dX2), grad(u))) * dx \
127 - 2*inner(dot(nabla_grad(dX2), grad(u)), grad(u)) * div(dX1) * dx \
128 - inner(grad(u), grad(u)) * tr(nabla_grad(dX1)*nabla_grad(dX2)) * dx \
129 - 2*inner(dot(nabla_grad(dX1), grad(u)), grad(u)) * div(dX2) * dx \
130 + inner(grad(u), grad(u)) * div(dX1) * div(dX2) * dx
131 test_second(Jb, ddJb)
132
133 test_second(Ja+Jb, ddJa + ddJb)
134
135
136 # In parallel a RuntimeError is thrown instead of the specific
137 # UFLException, so we skip that case.
138 @skip_in_parallel
139 def test_integral_scaling_edge_case():
140 mesh = UnitSquareMesh(6, 6)
141 X = SpatialCoordinate(mesh)
142 V = FunctionSpace(mesh, "CG", 1)
143 u = Function(V)
144
145 J = u * u * dx
146
147 with pytest.raises(UFLException):
148 assemble(Constant(2.0) * derivative(J, X))
149 with pytest.raises(UFLException):
150 assemble(derivative(Constant(2.0) * derivative(J, X), X))
151 with pytest.raises(UFLException):
152 assemble(Constant(2.0) * derivative(derivative(J, X), X))
153
154
155 def test_different_quadratures():
156 """
157 Checking that expressions with different quadrature rules
158 can be assembled as a sum, using the higher order quadrature for all
159 terms
160 """
161 mesh = UnitIntervalMesh(10)
162 x = SpatialCoordinate(mesh)
163 f = x[0]**2
164 V = FunctionSpace(mesh, "CG", 1)
165 u, v = TrialFunction(V), TestFunction(V)
166 a = inner(grad(u), grad(v)) * dx + inner(u, v) * dx
167 l = f * v * dx
168 bc = DirichletBC(V, Constant(1), "on_boundary")
169 uh = Function(V)
170 solve(a==l, uh, bcs=bc)
171 F = a - l
172 J = uh**2 * dx
173
174 # Adjoint equation disregarding Dirichlet BC adjoint
175 dJdu = derivative(J, uh)
176 dFdu = derivative(action(F, uh), uh)
177 bc_hom = DirichletBC(V, Constant(0), "on_boundary")
178 lmbd = Function(V)
179 solve(dFdu==-dJdu, lmbd, bc_hom)
180 F = replace(F, {u: uh})
181 dJdu = replace(dJdu, {u: uh})
182
183 # Parts of second order adjoint equation:
184 S = VectorFunctionSpace(mesh, "CG", 1)
185 s = Function(S)
186 F = replace(F, {v: lmbd})
187 ds = TestFunction(S)
188 dFdm = derivative(F, x, ds)
189 d2Fdm2 = derivative(dFdm, x, s)
190 soas_separated = assemble(d2Fdm2) + assemble(dFdm)
191
192 soa = d2Fdm2 + dFdm
193 soas = assemble(soa)
194 assert np.isclose(soas_separated.norm("l2"), soas.norm("l2"))
195
196 # 2D test for triangles with quadrature degree 3 and 4
197 mesh = UnitSquareMesh(5, 5)
198 x = SpatialCoordinate(mesh)
199 V = FunctionSpace(mesh, "CG", 1)
200 u0 = cos(pi*x[0])*sin(pi*x[1])
201
202 u, v = TrialFunction(V), TestFunction(V)
203 F = inner(u - u0, v)*dx
204 uh = Function(V)
205 solve(lhs(F) == rhs(F), uh)
206 J = uh**2*dx
207
208 # adjoint eq disregarding Dirichlet BC
209 dJdu = derivative(J, uh)
210 dFdu = derivative(action(F, uh), uh)
211 bc_hom = DirichletBC(V, Constant(0), "on_boundary")
212 lmbd = Function(V)
213 solve(dFdu==-dJdu, lmbd, bc_hom)
214 F = replace(F, {u:uh})
215 dJdu = replace(dJdu, {u: uh})
216 # Parts of second order adjoint eq:
217 S = VectorFunctionSpace(mesh, "CG", 1)
218 s = Function(S)
219 F = replace(F, {v: lmbd})
220 ds = TestFunction(S)
221 dFdm = derivative(F, x, ds)
222 d2Fdm2 = derivative(dFdm, x, s)
223 d2Fdudm = derivative(dFdm, uh, lmbd)
224 soas_separated = assemble(d2Fdm2) + assemble(dFdm) + assemble(d2Fdudm)
225 soa = d2Fdm2 + d2Fdudm + dFdm
226 soas = assemble(soa)
227 assert np.isclose(soas_separated.norm("l2"), soas.norm("l2"))
228
229
230 def test_unique_tables():
231 """
232 Checking that the unique table names in FFC are cached correctly
233 """
234 mesh = UnitIntervalMesh(10)
235 x = SpatialCoordinate(mesh)
236 f = x[0]**2
237 V = FunctionSpace(mesh, "CG", 1)
238 f = project(f, V)
239 u, v = TrialFunction(V), TestFunction(V)
240 a = inner(grad(u), grad(v)) * dx + inner(u, v) * dx
241 l = f * v * dx
242 bc = DirichletBC(V, Constant(1), "on_boundary")
243 uh = Function(V)
244 solve(a == l, uh, bcs=bc)
245 F = a - l
246 J = uh**2 * dx
247
248 # Adjoint eq disregarding Dirichlet BC
249 dJdu = derivative(J, uh)
250 dFdu = derivative(action(F, uh), uh)
251 bc_hom = DirichletBC(V, Constant(0), "on_boundary")
252 lmbd = Function(V)
253 solve(dFdu == -dJdu, lmbd, bc_hom)
254 F = replace(F, {u: uh})
255 dJdu = replace(dJdu, {u: uh})
256
257 # Parts of second order adjoint eq:
258 S = VectorFunctionSpace(mesh, "CG", 1)
259 s = Function(S)
260 F = replace(F, {v: lmbd})
261 ds = TestFunction(S)
262 dFdm = derivative(F, x, ds)
263 d2Fdm2 = derivative(dFdm, x, s)
264 soas_separated = assemble(d2Fdm2) + assemble(dFdm)
265
266 soa = d2Fdm2 + dFdm
267 soas = assemble(soa)
268 assert np.isclose(soas_separated.norm("l2"), soas.norm("l2"))
269
270
271 if __name__ == "__main__":
272 import os
273 pytest.main(os.path.abspath(__file__))