Debian Codegraph @

python/demo/undocumented/multimesh-poisson/demo_multimesh-poisson.py

raw
1 # Copyright (C) 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 # First added: 2015-11-05
19 # Last changed: 2015-11-17
20 #
21 # This demo program solves Poisson's equation on a domain defined by
22 # three overlapping and non-matching meshes. The solution is computed
23 # on a sequence of rotating meshes to test the multimesh
24 # functionality.
25
26 from dolfin import *
27
28 class DirichletBoundary(SubDomain):
29 def inside(self, x, on_boundary):
30 return on_boundary
31
32 def solve_poisson(t, x1, y1, x2, y2):
33 "Compute solution for given mesh configuration"
34
35 # Create meshes
36 r = 0.5
37 mesh_0 = RectangleMesh(Point(-r, -r), Point(r, r), 16, 16)
38 mesh_1 = RectangleMesh(Point(x1 - r, y1 - r), Point(x1 + r, y1 + r), 8, 8)
39 mesh_2 = RectangleMesh(Point(x2 - r, y2 - r), Point(x2 + r, y2 + r), 8, 8)
40 mesh_1.rotate(70*t)
41 mesh_2.rotate(-70*t)
42
43 # Build multimesh
44 multimesh = MultiMesh()
45 multimesh.add(mesh_0)
46 multimesh.add(mesh_1)
47 multimesh.add(mesh_2)
48 multimesh.build()
49
50 # Create function space
51 V = MultiMeshFunctionSpace(multimesh, "Lagrange", 1)
52
53 # Define trial and test functions and right-hand side
54 u = TrialFunction(V)
55 v = TestFunction(V)
56 f = Constant(1)
57
58 # Define facet normal and mesh size
59 n = FacetNormal(multimesh)
60 h = 2.0*Circumradius(multimesh)
61 h = (h('+') + h('-')) / 2
62
63 # Set parameters
64 alpha = 10.0
65 beta = 1.0
66
67 # Define bilinear form
68 a = dot(grad(u), grad(v))*dX \
69 - dot(avg(grad(u)), jump(v, n))*dI \
70 - dot(avg(grad(v)), jump(u, n))*dI \
71 + alpha/h * jump(u)*jump(v)*dI \
72 + beta/h**2 * dot(jump(u), jump(v))*dO
73
74 # Define linear form
75 L = f*v*dX
76
77 # Assemble linear system
78 A = assemble_multimesh(a)
79 b = assemble_multimesh(L)
80
81 # Apply boundary condition
82 zero = Constant(0)
83 boundary = DirichletBoundary()
84 bc = MultiMeshDirichletBC(V, zero, boundary)
85 bc.apply(A, b)
86
87 # Lock inactive dofs
88 V.lock_inactive_dofs(A, b)
89
90 # Compute solution
91 u = MultiMeshFunction(V)
92 solve(A, u.vector(), b)
93
94 return u
95
96 if MPI.size(MPI.comm_world) > 1:
97 info("Sorry, this demo does not (yet) run in parallel.")
98 exit(0)
99
100 # Parameters
101 T = 10.0
102 N = 100
103 dt = T / N
104
105 # Create files for output
106 f0 = XDMFFile("output/u0.xdmf")
107 f1 = XDMFFile("output/u1.xdmf")
108 f2 = XDMFFile("output/u2.xdmf")
109
110 # Iterate over configurations
111 for n in range(N):
112 info("Computing solution, step %d / %d." % (n + 1, N))
113
114 # Compute coordinates for meshes
115 t = dt*n
116 x1 = sin(t)*cos(2*t)
117 y1 = cos(t)*cos(2*t)
118 x2 = cos(t)*cos(2*t)
119 y2 = sin(t)*cos(2*t)
120
121 # Compute solution
122 u = solve_poisson(t, x1, y1, x2, y2)
123
124 # Save to file
125 f0.write(u.part(0, deepcopy=True), t)
126 f1.write(u.part(1, deepcopy=True), t)
127 f2.write(u.part(2, deepcopy=True), t)
128
129 # Close files
130 f0.close()
131 f1.close()
132 f2.close()