Debian Codegraph @

python/demo/undocumented/collision-detection/demo_collision-detection.py

raw
1 # Copyright (C) 2013, 2015 Anders Logg
2 #
3 # This file is part of DOLFIN.
4 #
5 # DOLFIN is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # DOLFIN is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17
18 from dolfin import *
19
20 # There is an issue with the Matplotlib backend that needs to be investigated
21
22 # Some parameters
23 L = 10.0
24 h = 0.1
25 M = 64
26 N = 32
27 num_steps = 1000
28 (x_B, y_B) = (0.75*L, 0.5*L)
29 (x_C, y_C) = (0.33*L, 0.75*L)
30 (dx_B, dy_B) = (2*h, h)
31 (dx_C, dy_C) = (h, 2*h)
32
33 # Create meshes: a box and two circles
34 mesh_A = RectangleMesh(Point(0, 0), Point(L, L), M, M)
35 mesh_B = Mesh("../circle.xml.gz")
36 mesh_C = Mesh("../circle.xml.gz")
37
38 # Displace circles to initial positions
39 mesh_B.translate(Point(x_B, y_B))
40 mesh_C.translate(Point(x_C, y_C))
41
42 # Create mesh function for plotting
43 f = MeshFunction("size_t", mesh_A, mesh_A.topology().dim())
44
45 # Build bounding box trees for background mesh
46 tree_A = BoundingBoxTree()
47 tree_A.build(mesh_A)
48
49 # Create bounding box trees for circles
50 tree_B = BoundingBoxTree()
51 tree_C = BoundingBoxTree()
52
53 # Loop over time steps
54 for n in range(num_steps):
55
56 # Make it bounce
57 x_B += dx_B; y_B += dy_B
58 x_C += dx_C; y_C += dy_C
59 if x_B > L - 1.0 or x_B < 1.0: dx_B = -dx_B
60 if y_B > L - 1.0 or y_B < 1.0: dy_B = -dy_B
61 if x_C > L - 1.0 or x_C < 1.0: dx_C = -dx_C
62 if y_C > L - 1.0 or y_C < 1.0: dy_C = -dy_C
63
64 # Translate circles
65 mesh_B.translate(Point(dx_B, dy_B))
66 mesh_C.translate(Point(dx_C, dy_C))
67
68 # Rebuild trees
69 tree_B.build(mesh_B)
70 tree_C.build(mesh_C)
71
72 # Compute collisions
73 entities_AB, entities_B = tree_A.compute_collisions(tree_B)
74 entities_AC, entities_C = tree_A.compute_collisions(tree_C)
75 entities_BC = set(entities_AB).intersection(set(entities_AC))
76
77 # Mark mesh function
78 f.set_all(0)
79 for i in entities_AB:
80 f[int(i)] = 1
81 for i in entities_AC:
82 f[int(i)] = 2
83 for i in entities_BC:
84 f[int(i)] = 3