| 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 |
# Subdomain 1 |
| 41 |
a1 = inner(grad(u1), grad(v1))*dx |
| 42 |
L1 = f*v1*dx |
| 43 |
sol1 = Function(V.sub_space(0)) |
| 44 |
solve(a1 == L1, sol1, bc1) |
| 45 |
# Subdomain 2 |
| 46 |
a2 = inner(grad(u2), grad(v2))*dx |
| 47 |
L2 = f*v2*dx |
| 48 |
sol2 = Function(V.sub_space(1)) |
| 49 |
solve(a2 == L2, sol2, bc2) |
| 50 |
|
| 51 |
# Save solution in vtk format |
| 52 |
out_sub1 = File("functionspace-product-subdomain1.pvd") |
| 53 |
out_sub1 << sol1 |
| 54 |
out_sub2 = File("functionspace-product-subdomain2.pvd") |
| 55 |
out_sub2 << sol2 |