Debian Codegraph @

python/test/unit/fem/test_form.py

raw
1 """Unit tests for the fem interface"""
2
3 # Copyright (C) 2011-2014 Johan Hake
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 Marie E. Rognes ([email protected]), 2012
21 # Modified by Martin S. Alnaes ([email protected]), 2014
22
23 import pytest
24 import numpy
25 import ufl_legacy as ufl
26 from dolfin import *
27 from dolfin_utils.test import skip_in_parallel, fixture
28
29
30 @fixture
31 def square():
32 return UnitSquareMesh(8, 8)
33
34
35 @fixture
36 def square_boundary(square):
37 return BoundaryMesh(square, "exterior")
38
39
40 @fixture
41 def cube_boundary(cube):
42 return BoundaryMesh(cube, "exterior")
43
44
45 @fixture
46 def cube():
47 return UnitCubeMesh(2, 2, 2)
48
49
50 @fixture
51 def V1(square_boundary):
52 return FunctionSpace(square_boundary, "CG", 1)
53
54
55 @fixture
56 def VV1(square_boundary):
57 return VectorFunctionSpace(square_boundary, "CG", 1)
58
59
60 @fixture
61 def Q1(square_boundary):
62 return FunctionSpace(square_boundary, "DG", 0)
63
64
65 @fixture
66 def V2(cube_boundary):
67 return FunctionSpace(cube_boundary, "CG", 1)
68
69
70 @fixture
71 def VV2(cube_boundary):
72 return VectorFunctionSpace(cube_boundary, "CG", 1)
73
74
75 @fixture
76 def Q2(cube_boundary):
77 return FunctionSpace(cube_boundary, "DG", 0)
78
79
80 def test_assemble_functional(V1, V2):
81
82 mesh = V1.mesh()
83 surfacearea = assemble(1*dx(mesh))
84 assert round(surfacearea - 4.0, 7) == 0
85
86 u = Function(V1)
87 u.vector()[:] = 1.0
88 surfacearea = assemble(u*dx)
89 assert round(surfacearea - 4.0, 7) == 0
90
91 u = Function(V2)
92 u.vector()[:] = 1.0
93 surfacearea = assemble(u*dx)
94 assert round(surfacearea - 6.0, 7) == 0
95
96 f = Expression("1.0", degree=0)
97 u = interpolate(f, V1)
98 surfacearea = assemble(u*dx)
99 assert round(surfacearea - 4.0, 7) == 0
100
101 f = Expression("1.0", degree=0)
102 u = interpolate(f, V2)
103 surfacearea = assemble(u*dx)
104 assert round(surfacearea - 6.0, 7) == 0
105
106
107 def test_assemble_linear(V1, Q1, square_boundary, V2, Q2, cube_boundary):
108
109 u = Function(V1)
110 w = TestFunction(Q1)
111 u.vector()[:] = 0.5
112 facetareas = MPI.sum(square_boundary.mpi_comm(),
113 assemble(u*w*dx).get_local().sum())
114 assert round(facetareas - 2.0, 7) == 0
115
116 u = Function(V2)
117 w = TestFunction(Q2)
118 u.vector()[:] = 0.5
119 a = u*w*dx
120 b = assemble(a)
121 facetareas = MPI.sum(cube_boundary.mpi_comm(),
122 assemble(u*w*dx).get_local().sum())
123 assert round(facetareas - 3.0, 7) == 0
124
125 mesh = UnitSquareMesh(8, 8)
126 bdry = BoundaryMesh(mesh, "exterior")
127 V = FunctionSpace(mesh, "CG", 1)
128 u = TrialFunction(V)
129 v = TestFunction(V)
130
131 BV = FunctionSpace(bdry, "CG", 1)
132 bu = TrialFunction(BV)
133 bv = TestFunction(BV)
134
135 a = MPI.sum(mesh.mpi_comm(),
136 assemble(inner(u, v)*ds).array().sum())
137 b = MPI.sum(bdry.mpi_comm(),
138 assemble(inner(bu, bv)*dx).array().sum())
139 assert round(a - b, 7) == 0
140
141
142 @skip_in_parallel
143 def test_assemble_bilinear_1D_2D(square, V1, square_boundary):
144
145 V = FunctionSpace(square, 'CG', 1)
146 u = TrialFunction(V)
147 v = TestFunction(V)
148
149 bu = TrialFunction(V1)
150 bv = TestFunction(V1)
151
152 a = MPI.sum(square.mpi_comm(),
153 assemble(inner(u, v)*ds).array().sum())
154 b = MPI.sum(square_boundary.mpi_comm(),
155 assemble(inner(bu, bv)*dx).array().sum())
156 assert round(a - b, 7) == 0
157
158 # Assemble over subset of mesh facets
159 subdomain = CompiledSubDomain("near(x[1], 0.0)")
160 bottom = MeshFunction("size_t", square, square.topology().dim()-1)
161 bottom.set_all(0)
162 subdomain.mark(bottom, 1)
163 dss = ds(subdomain_data=bottom)
164 foo = MPI.sum(square.mpi_comm(),
165 abs(assemble(inner(grad(u)[0],
166 grad(v)[0])*dss(1)).array()).sum())
167
168 # Assemble over all cells of submesh created from subset of
169 # boundary mesh
170 bottom2 = MeshFunction("size_t", square_boundary, square_boundary.topology().dim())
171 bottom2.set_all(0)
172 subdomain.mark(bottom2, 1)
173 BV = FunctionSpace(SubMesh(square_boundary, bottom2, 1), "CG", 1)
174 bu = TrialFunction(BV)
175 bv = TestFunction(BV)
176 bar = MPI.sum(square_boundary.mpi_comm(),
177 abs(assemble(inner(grad(bu)[0],
178 grad(bv)[0])*dx).array()).sum())
179 # Should give same result
180 assert round(bar - foo, 7) == 0
181
182
183 @skip_in_parallel
184 def test_assemble_bilinear_2D_3D(cube, V2, cube_boundary):
185
186 V = FunctionSpace(cube, 'CG', 1)
187 u = TrialFunction(V)
188 v = TestFunction(V)
189
190 # V2 is a FunctionSpace over cube_boundary
191 bu = TrialFunction(V2)
192 bv = TestFunction(V2)
193
194 a = MPI.sum(cube.mpi_comm(),
195 assemble(inner(u, v)*ds).array().sum())
196 b = MPI.sum(cube_boundary.mpi_comm(),
197 assemble(inner(bu, bv)*dx).array().sum())
198 assert round(a - b, 7) == 0
199
200 # Assemble over subset of mesh facets
201 subdomain = CompiledSubDomain("near(x[1], 0.0)")
202 bottom = MeshFunction("size_t", cube, cube.topology().dim()-1)
203 bottom.set_all(0)
204 subdomain.mark(bottom, 1)
205 dss = ds(subdomain_data=bottom)
206 foo = MPI.sum(cube.mpi_comm(),
207 abs(assemble(inner(grad(u)[0],
208 grad(v)[0])*dss(1)).array()).sum())
209
210 # Assemble over all cells of submesh created from subset of
211 # boundary mesh
212 bottom2 = MeshFunction("size_t", cube_boundary, cube_boundary.topology().dim())
213 bottom2.set_all(0)
214 subdomain.mark(bottom2, 1)
215 BV = FunctionSpace(SubMesh(cube_boundary, bottom2, 1), "CG", 1)
216 bu = TrialFunction(BV)
217 bv = TestFunction(BV)
218 bar = MPI.sum(cube_boundary.mpi_comm(),
219 abs(assemble(inner(grad(bu)[0],
220 grad(bv)[0])*dx).array()).sum())
221
222 # Should give same result
223 assert round(bar - foo, 7) == 0
224
225
226 @fixture
227 def base():
228 n = 16
229 plane = CompiledSubDomain("near(x[1], 1.0)")
230 square = UnitSquareMesh(n, n)
231
232 square3d = SubMesh(BoundaryMesh(UnitCubeMesh(n, n, n), "exterior"), plane)
233 global_normal = Expression(("0.0", "1.0", "0.0"), degree=0)
234 square3d.init_cell_orientations(global_normal)
235
236 RT2 = FiniteElement("RT", square.ufl_cell(), 1)
237 RT3 = FiniteElement("RT", square3d.ufl_cell(), 1)
238 DG2 = FiniteElement("DG", square.ufl_cell(), 0)
239 DG3 = FiniteElement("DG", square3d.ufl_cell(), 0)
240
241 return [(RT2, RT3), (DG2, DG3), (square, square3d)]
242
243
244 @fixture
245 def RT2(base):
246 return FunctionSpace(base[2][0], base[0][0])
247
248
249 @fixture
250 def RT3(base):
251 return FunctionSpace(base[2][1], base[0][1])
252
253
254 @fixture
255 def W2(base):
256 """ RT2 * DG2 """
257 return FunctionSpace(base[2][0], base[0][0] * base[1][0])
258
259
260 @fixture
261 def W3(base):
262 """ RT3 * DG3 """
263 return FunctionSpace(base[2][1], base[0][1] * base[1][1])
264
265
266 @fixture
267 def QQ2(base):
268 """ DG2 * DG2 """
269 return FunctionSpace(base[2][0], base[1][0] * base[1][0])
270
271
272 @fixture
273 def QQ3(base):
274 """ DG3 * DG3 """
275 return FunctionSpace(base[2][1], base[1][1] * base[1][1])
276
277
278 @skip_in_parallel
279 def test_basic_rt(RT2, RT3):
280
281 f2 = Expression(("2.0", "1.0"), degree=0)
282 f3 = Expression(("1.0", "0.0", "2.0"), degree=0)
283
284 u2 = TrialFunction(RT2)
285 u3 = TrialFunction(RT3)
286 v2 = TestFunction(RT2)
287 v3 = TestFunction(RT3)
288
289 # Project
290 pw2 = project(f2, RT2)
291 pw3 = project(f3, RT3)
292 pa2 = assemble(pw2**2*dx)
293 pa3 = assemble(pw3**2*dx)
294
295 # Project explicitly
296 a2 = inner(u2, v2)*dx
297 a3 = inner(u3, v3)*dx
298 L2 = inner(f2, v2)*dx
299 L3 = inner(f3, v3)*dx
300 w2 = Function(RT2)
301 w3 = Function(RT3)
302 A2 = assemble(a2)
303 b2 = assemble(L2)
304 A3 = assemble(a3)
305 b3 = assemble(L3)
306 solve(A2, w2.vector(), b2)
307 solve(A3, w3.vector(), b3)
308 a2 = assemble(w2**2*dx)
309 a3 = assemble(w3**2*dx)
310
311 # Compare various results
312 assert round((w2.vector() - pw2.vector()).norm("l2"), 4) == 0
313 assert round((w3.vector() - pw3.vector()).norm("l2"), 4) == 0
314 # 2d
315 assert round(a2 - 5.0, 7) == 0
316 assert round(pa2 - 5.0, 7) == 0
317 # 3d
318 assert round(a3 - 5.0, 7) == 0
319 assert round(pa3 - 5.0, 6) == 0
320
321
322 @skip_in_parallel
323 def test_mixed_poisson_solve(W2, W3):
324
325 f = Constant(1.0)
326
327 # Solve mixed Poisson on standard unit square
328 (sigma2, u2) = TrialFunctions(W2)
329 (tau2, v2) = TestFunctions(W2)
330 a = (inner(sigma2, tau2) + div(tau2)*u2 + div(sigma2)*v2)*dx
331 L = f*v2*dx
332 w2 = Function(W2)
333 solve(a == L, w2)
334
335 # Solve mixed Poisson on unit square in 3D
336 (sigma3, u3) = TrialFunctions(W3)
337 (tau3, v3) = TestFunctions(W3)
338 a = (inner(sigma3, tau3) + div(tau3)*u3 + div(sigma3)*v3)*dx
339 L = f*v3*dx
340 w3 = Function(W3)
341 solve(a == L, w3)
342
343 # Check that results are about the same
344 assert round(assemble(inner(w2, w2)*dx) - assemble(inner(w3, w3)*dx)) == 0
345
346
347 @fixture
348 def m():
349 return 3
350
351
352 @fixture
353 def cube(m):
354 return UnitCubeMesh(m, m, m)
355
356
357 @fixture
358 def cube_boundary(cube):
359 return BoundaryMesh(cube, "exterior")
360
361
362 @fixture
363 def plane():
364 return CompiledSubDomain("near(x[1], 0.0)")
365
366
367 @fixture
368 def square_boundary_(m):
369 square = UnitSquareMesh(m, m)
370 return BoundaryMesh(square, "exterior")
371
372
373 @fixture
374 def line():
375 return CompiledSubDomain("near(x[0], 0.0)")
376
377
378 @fixture
379 def mesh3(cube_boundary, plane):
380 return BoundaryMesh(SubMesh(cube_boundary, plane), "exterior")
381
382
383 @fixture
384 def bottom1(square_boundary_, plane):
385 return SubMesh(square_boundary_, plane)
386
387
388 @fixture
389 def bottom2(cube_boundary, plane):
390 return SubMesh(cube_boundary, plane)
391
392
393 @fixture
394 def bottom3(mesh3, line):
395 return SubMesh(mesh3, line)
396
397
398 @skip_in_parallel
399 def test_normals_2D_1D(bottom1, m):
400 "Testing assembly of normals for 1D meshes embedded in 2D"
401
402 n = FacetNormal(bottom1)
403 a = inner(n, n)*ds
404 value_bottom1 = assemble(a)
405 assert round(value_bottom1 - 2.0, 7) == 0
406 b = inner(n('+'), n('+'))*dS
407 b1 = assemble(b)
408 c = inner(n('+'), n('-'))*dS
409 c1 = assemble(c)
410 assert round(b1 - m + 1, 7) == 0
411 assert round(c1 + b1, 7) == 0
412
413
414 @skip_in_parallel
415 def test_normals_3D_1D(bottom3, m):
416 "Testing assembly of normals for 1D meshes embedded in 3D"
417
418 n = FacetNormal(bottom3)
419 a = inner(n, n)*ds
420 v1 = assemble(a)
421 assert round(v1 - 2.0, 7) == 0
422 b = inner(n('+'), n('+'))*dS
423 b1 = assemble(b)
424 c = inner(n('+'), n('-'))*dS
425 c1 = assemble(c)
426 assert round(b1 - m + 1, 7) == 0
427 assert round(c1 + b1, 7) == 0
428
429
430 @skip_in_parallel
431 def test_normals_3D_2D(bottom2):
432 "Testing assembly of normals for 2D meshes embedded in 3D"
433
434 n = FacetNormal(bottom2)
435 a = inner(n, n)*ds
436 v1 = assemble(a)
437 assert round(v1 - 4.0, 7) == 0
438
439 b = inner(n('+'), n('+'))*dS
440 b1 = assemble(b)
441 c = inner(n('+'), n('-'))*dS
442 c1 = assemble(c)
443 assert round(c1 + b1, 7) == 0
444
445
446 @skip_in_parallel
447 def test_cell_volume(m, bottom1, bottom2, bottom3):
448 "Testing assembly of volume for embedded meshes"
449
450 volume = CellVolume(bottom1)
451 a = volume*dx
452 b = assemble(a)
453 assert round(b - 1.0/m, 7) == 0
454
455 volume = CellVolume(bottom3)
456 a = volume*dx
457 b = assemble(a)
458 assert round(b - 1.0/m, 7) == 0
459
460 volume = CellVolume(bottom2)
461 a = volume*dx
462 b = assemble(a)
463 assert round(b - 1.0/(2*m*m), 7) == 0
464
465
466 @skip_in_parallel
467 def test_circumradius(m, bottom1, bottom2, bottom3):
468 "Testing assembly of circumradius for embedded meshes"
469
470 r = Circumradius(bottom1)
471 a = r*dx
472 b = assemble(a)
473 assert round(b - 0.5*(1.0/m), 7) == 0
474
475 r = Circumradius(bottom3)
476 a = r*dx
477 b = assemble(a)
478 assert round(b - 0.5*(1.0/m), 7) == 0
479
480 square = UnitSquareMesh(m, m)
481 r = Circumradius(square)
482 a = r*dx
483 b0 = assemble(a)
484
485 r = Circumradius(bottom2)
486 a = r*dx
487 b1 = assemble(a)
488 assert round(b0 - b1, 7) == 0
489
490
491 @skip_in_parallel
492 def test_facetarea(bottom1, bottom2, bottom3, m):
493 "Testing assembly of facet area for embedded meshes"
494
495 area = FacetArea(bottom1)
496 a = area*ds
497 b = assemble(a)
498 assert round(b - 2.0, 7) == 0
499
500 area = FacetArea(bottom3)
501 a = area*ds
502 b = assemble(a)
503 assert round(b - 2.0, 7) == 0
504
505 square = UnitSquareMesh(m, m)
506 area = FacetArea(square)
507 a = area*ds
508 b0 = assemble(a)
509
510 area = FacetArea(bottom2)
511 a = area*ds
512 b1 = assemble(a)
513 assert round(b0 - b1, 7) == 0
514
515
516 @skip_in_parallel
517 def test_derivative(QQ2, QQ3):
518 for W in [QQ2, QQ3]:
519 w = Function(W)
520 dim = w.value_dimension(0)
521 w.interpolate(Constant([42.0*(i+1) for i in range(dim)]))
522
523 # Derivative w.r.t. mixed space
524 u, v = split(w)
525 F = u*v*dx
526 dF = derivative(F, w)
527 b1 = assemble(dF)
528
529
530 def test_coefficient_derivatives(V1, V2):
531 for V in [V1, V2]:
532 f = Function(V)
533 g = Function(V)
534 v = TestFunction(V)
535 u = TrialFunction(V)
536
537 f.interpolate(Expression("1.0 + x[0] + x[1]", degree=1))
538 g.interpolate(Expression("2.0 + x[0] + x[1]", degree=1))
539
540 # Since g = f + 1, define dg/df = 1
541 cd = {g: 1}
542
543 # Handle relation between g and f in derivative
544 M = g**2*dx
545 L = derivative(M, f, v, coefficient_derivatives=cd)
546 a = derivative(L, f, u, coefficient_derivatives=cd)
547 A0 = assemble(a).norm('frobenius')
548 b0 = assemble(L).norm('l2')
549
550 # Manually construct the above case
551 M = g**2*dx
552 L = 2*g*v*dx
553 a = 2*u*v*dx
554 A1 = assemble(a).norm('frobenius')
555 b1 = assemble(L).norm('l2')
556
557 # Compare
558 assert round(A0 - A1, 7) == 0.0
559 assert round(b0 - b1, 7) == 0.0