| 1 |
from dolfin import * |
| 2 |
|
| 3 |
#Create mesh and define function space |
| 4 |
mesh = UnitSquareMesh(30, 30) |
| 5 |
|
| 6 |
marker = MeshFunction("size_t", mesh, mesh.topology().dim(), 0) |
| 7 |
for c in cells(mesh): |
| 8 |
marker[c] = c.midpoint().y() < 0.5 |
| 9 |
#marker[c] = c.midpoint().x() < 0.5 |
| 10 |
|
| 11 |
submesh1 = MeshView.create(marker, 1) |
| 12 |
submesh2 = MeshView.create(marker, 0) |
| 13 |
|
| 14 |
# Define Dirichlet boundary |
| 15 |
def boundarySub1(x): |
| 16 |
return x[1] < DOLFIN_EPS |
| 17 |
#return x[0] < DOLFIN_EPS |
| 18 |
|
| 19 |
def boundarySub2(x): |
| 20 |
return x[1] > 1.0 - DOLFIN_EPS |
| 21 |
#return x[0] > 1.0 - DOLFIN_EPS |
| 22 |
|
| 23 |
#element2D = FiniteElement("Lagrange", triangle, 1) |
| 24 |
W1 = FunctionSpace(submesh1, "Lagrange", 1) |
| 25 |
W2 = FunctionSpace(submesh2, "Lagrange", 1) |
| 26 |
# Define the mixed function space |
| 27 |
V = MixedFunctionSpace( W1, W2 ) |
| 28 |
|
| 29 |
# Define boundary conditions |
| 30 |
u0 = Constant(0.0) |
| 31 |
# Subdomain 1 |
| 32 |
bc1 = DirichletBC(V.sub_space(0), u0, boundarySub1) |
| 33 |
# Subdomain 2 |
| 34 |
bc2 = DirichletBC(V.sub_space(1), u0, boundarySub2) |
| 35 |
bcs = [bc1,bc2] |
| 36 |
|
| 37 |
# Define variational problem |
| 38 |
# Use directly TrialFunction and TestFunction on the product space |
| 39 |
# Subdomain 1 |
| 40 |
u1 = TrialFunction(W1) |
| 41 |
v1 = TestFunction(W1) |
| 42 |
# Subdomain 2 |
| 43 |
u2 = TrialFunction(W2) |
| 44 |
v2 = TestFunction(W2) |
| 45 |
# Mixed |
| 46 |
(u1_m,u2_m) = TrialFunctions(V) |
| 47 |
(v1_m,v2_m) = TestFunctions(V) |
| 48 |
|
| 49 |
f = Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)", degree=2) |
| 50 |
|
| 51 |
# Weak form - 1 |
| 52 |
a1 = inner(grad(u1), grad(v1))*dx |
| 53 |
L1 = f*v1*dx |
| 54 |
u1 = Function(W1) |
| 55 |
solve(a1 == L1, u1, bc1) |
| 56 |
# Weak form - 1 |
| 57 |
a2 = inner(grad(u2), grad(v2))*dx |
| 58 |
L2 = f*v2*dx |
| 59 |
u2 = Function(W2) |
| 60 |
solve(a2 == L2, u2, bc2) |
| 61 |
|
| 62 |
# Mixed weak form |
| 63 |
a = inner(grad(u1_m), grad(v1_m))*dx + inner(grad(u2_m), grad(v2_m))*dx |
| 64 |
L = f*v1_m*dx + f*v2_m*dx |
| 65 |
# Solve the problem |
| 66 |
sol = Function(V) |
| 67 |
solve(a == L, sol, bcs, solver_parameters={"linear_solver":"direct"}) |
| 68 |
|
| 69 |
sol1 = sol.sub(0) |
| 70 |
sol2 = sol.sub(1) |
| 71 |
|
| 72 |
assert len(u1.vector()) == len(sol1.vector()) |
| 73 |
for i in range(len(u1.vector())): |
| 74 |
assert abs(sol1.vector()[i] - u1.vector()[i]) < 1e-10 |
| 75 |
|
| 76 |
assert len(u2.vector()) == len(sol2.vector()) |
| 77 |
for i in range(len(u2.vector())): |
| 78 |
assert abs(sol2.vector()[i] - u2.vector()[i]) < 1e-10 |
| 79 |
|
| 80 |
## Export result |
| 81 |
encoding = XDMFFile.Encoding.HDF5 if has_hdf5() else XDMFFile.Encoding.ASCII |
| 82 |
|
| 83 |
out_u1 = XDMFFile(MPI.comm_world, "block-assembly-2D2D-subdomain1-ref.xdmf") |
| 84 |
out_u2 = XDMFFile(MPI.comm_world, "block-assembly-2D2D-subdomain2-ref.xdmf") |
| 85 |
out_sub1 = XDMFFile(MPI.comm_world, "block-assembly-2D2D-subdomain1.xdmf") |
| 86 |
out_sub2 = XDMFFile(MPI.comm_world, "block-assembly-2D2D-subdomain2.xdmf") |
| 87 |
|
| 88 |
if MPI.size(MPI.comm_world) > 1 and encoding == XDMFFile.Encoding.ASCII: |
| 89 |
print("XDMF file output not supported in parallel without HDF5") |
| 90 |
else: |
| 91 |
out_u1.write(u1, encoding) |
| 92 |
out_u2.write(u2, encoding) |
| 93 |
out_sub1.write(sol1, encoding) |
| 94 |
out_sub2.write(sol2, encoding) |