Debian Codegraph @

python/demo/undocumented/formsplitter/demo_formsplitter.py

raw
1 from dolfin import *
2
3 #Create mesh and define function space
4 mesh = UnitSquareMesh(32, 32)
5
6 marker = MeshFunction("size_t", mesh, mesh.topology().dim(), 0)
7 for c in cells(mesh):
8 marker[c] = c.midpoint().x() < 0.5
9
10 submesh1 = MeshView.create(marker, 1)
11 submesh2 = MeshView.create(marker, 0)
12
13 # Define Dirichlet boundary
14 def boundarySub1(x):
15 return x[0] < DOLFIN_EPS
16
17 def boundarySub2(x):
18 return x[0] > 1.0 - DOLFIN_EPS
19
20 W1 = FunctionSpace(submesh1, "Lagrange", 1)
21 W2 = FunctionSpace(submesh2, "Lagrange", 1)
22
23 # Define the mixed function space
24 V = MixedFunctionSpace( W1, W2 )
25
26 # Define boundary conditions
27 u0 = Constant(0.0)
28 # Subdomain 1
29 bc1 = DirichletBC(V.sub_space(0), u0, boundarySub1)
30 # Subdomain 2
31 bc2 = DirichletBC(V.sub_space(1), u0, boundarySub2)
32
33 # Define variational problem
34 # Use directly TrialFunction and TestFunction on the product space
35 (u1,u2) = TrialFunctions(V)
36 (v1,v2) = TestFunctions(V)
37
38 f = Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)", degree=2)
39
40 # Define new measures so that we can sort the integrals
41 a_prod = inner(grad(u1), grad(v1))*dx + inner(grad(u2), grad(v2))*dx
42 L_prod = f*v1*dx + f*v2*dx
43 # Subdomain 1
44 a1 = extract_blocks(a_prod,0,0)
45 L1 = extract_blocks(L_prod,0)
46 sol1 = Function(V.sub_space(0))
47 solve(a1 == L1, sol1, bc1)
48 # Subdomain 2
49 a2 = extract_blocks(a_prod,1,1)
50 L2 = extract_blocks(L_prod,1)
51 sol2 = Function(V.sub_space(1))
52 solve(a2 == L2, sol2, bc2)
53
54 # Save solution in vtk format
55 out_sub1 = File("formsplitter-subdomain1.pvd")
56 out_sub1 << sol1
57 out_sub2 = File("formsplitter-subdomain2.pvd")
58 out_sub2 << sol2