Debian Codegraph @

python/test/unit/ale/test_harmonic_smoothing.py

raw
1 """Unit test for HarmonicSmoothing and ALE"""
2
3 # Copyright (C) 2013 Jan Blechta
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 import pytest
21 from dolfin import UnitSquareMesh, BoundaryMesh, Expression, \
22 MeshFunction, SubMesh, Constant, MPI, MeshQuality,\
23 ALE
24 from dolfin_utils.test import skip_in_parallel
25
26 def test_HarmonicSmoothing():
27
28 # Create some mesh and its boundary
29 mesh = UnitSquareMesh(10, 10)
30 boundary = BoundaryMesh(mesh, 'exterior')
31
32 # Move boundary
33 disp = Expression(("0.3*x[0]*x[1]", "0.5*(1.0-x[1])"), degree=2)
34 ALE.move(boundary, disp)
35
36 # Move mesh according to given boundary
37 ALE.move(mesh, boundary)
38
39 # Check that new boundary topology corresponds to given one
40 boundary_new = BoundaryMesh(mesh, 'exterior')
41 assert boundary.topology().hash() == boundary_new.topology().hash()
42
43 # Check that coordinates are almost equal
44 err = sum(sum(abs(boundary.coordinates() \
45 - boundary_new.coordinates()))) / mesh.num_vertices()
46 print("Current CG solver produced error in boundary coordinates", err)
47 assert round(err - 0.0, 5) == 0
48
49 # Check mesh quality
50 magic_number = 0.35
51 rmin = MeshQuality.radius_ratio_min_max(mesh)[0]
52 assert rmin > magic_number
53
54 @skip_in_parallel
55 def test_ale():
56
57 # Create some mesh
58 mesh = UnitSquareMesh(4, 5)
59
60 # Make some cell function
61 # FIXME: Initialization by array indexing is probably
62 # not a good way for parallel test
63 cellfunc = MeshFunction('size_t', mesh, mesh.topology().dim())
64 cellfunc.array()[0:4] = 0
65 cellfunc.array()[4:] = 1
66
67 # Create submeshes - this does not work in parallel
68 submesh0 = SubMesh(mesh, cellfunc, 0)
69 submesh1 = SubMesh(mesh, cellfunc, 1)
70
71 # Move submesh0
72 disp = Constant(("0.1", "-0.1"))
73 ALE.move(submesh0, disp)
74
75 # Move and smooth submesh1 accordignly
76 ALE.move(submesh1, submesh0)
77
78 # Move mesh accordingly
79 parent_vertex_indices_0 = \
80 submesh0.data().array('parent_vertex_indices', 0)
81 parent_vertex_indices_1 = \
82 submesh1.data().array('parent_vertex_indices', 0)
83 mesh.coordinates()[parent_vertex_indices_0[:]] = \
84 submesh0.coordinates()[:]
85 mesh.coordinates()[parent_vertex_indices_1[:]] = \
86 submesh1.coordinates()[:]
87
88 # If test passes here then it is probably working
89 # Check for cell quality for sure
90 magic_number = 0.28
91 rmin = MeshQuality.radius_ratio_min_max(mesh)[0]
92 assert rmin > magic_number