Debian Codegraph @

python/demo/undocumented/multimesh-marking/demo_multimesh_marking.py

raw
1 from dolfin import *
2
3 if MPI.size(MPI.comm_world) > 1:
4 info("Sorry, this demo does not (yet) run in parallel.")
5 exit(0)
6
7 # Generate meshes
8 background_mesh = UnitSquareMesh(16, 16)
9 annulus_mesh = Mesh("../donut.xml.gz")
10
11 center = Point(0.5, 0.5)
12 r = 0.2
13
14 # Build the multimesh
15 multimesh = MultiMesh()
16 multimesh.add(background_mesh)
17 multimesh.add(annulus_mesh)
18 multimesh.build()
19
20 # Identify background cells within the hole and mark them as covered
21 multimesh.auto_cover(0, center)
22
23 # Variational formulation
24 V = MultiMeshFunctionSpace(multimesh, "P", 1)
25 u, v = TrialFunction(V), TestFunction(V)
26 n = FacetNormal(multimesh)
27 h = 2*Circumradius(multimesh)
28
29 a = (inner(grad(u), grad(v)) * dX
30 - inner(avg(grad(u)), jump(v, n)) * dI
31 - inner(avg(grad(v)), jump(u, n)) * dI
32 + Constant(10) / avg(h) * jump(u) * jump(v) * dI
33 + inner(jump(grad(u)), jump(grad(v))) * dO)
34
35 L = Constant(1) * v * dX
36
37 # Assemble system
38 A = assemble_multimesh(a)
39 b = assemble_multimesh(L)
40
41 # Will mark boundary of outer mesh
42 class OuterBoundary(SubDomain):
43 def inside(self, x, on_boundary):
44 return on_boundary
45 outer = OuterBoundary()
46 mf0 = MeshFunction("size_t", multimesh.part(0), multimesh.part(0).topology().dim() - 1)
47 outer.mark(mf0, 2)
48 bc0 = MultiMeshDirichletBC(V, Constant(0), mf0, 2, 0)
49
50 # Will mark inner part of top mesh
51 class InnerBoundary(SubDomain):
52 def inside(self, x, on_boundary):
53 return on_boundary and ((x[0] - 0.5)**2 + (x[1] - 0.5)**2 < 0.5*r)
54 inner = InnerBoundary()
55 mf1 = MeshFunction("size_t", multimesh.part(1), multimesh.part(1).topology().dim()-1)
56 inner.mark(mf1 , 3)
57 bc1 = MultiMeshDirichletBC(V, Constant(1), mf1, 3, 1)
58 bcs = [bc0, bc1]
59
60 [bc.apply(A, b) for bc in bcs]
61 V.lock_inactive_dofs(A, b)
62
63 # Solve and plot
64 uh = MultiMeshFunction(V)
65 x = uh.vector()
66
67 solve(A, x, b)
68
69 outfile0 = XDMFFile("output/u0.xdmf")
70 outfile1 = XDMFFile("output/u1.xdmf")
71
72 outfile0.write(uh.part(0, deepcopy=True), 0.0)
73 outfile1.write(uh.part(1, deepcopy=True), 0.0)
74
75 outfile0.close()
76 outfile1.close()
77