| 1 |
from dolfin import * |
| 2 |
|
| 3 |
# Create mesh and define function space |
| 4 |
mesh = UnitCubeMesh(32, 32, 32) |
| 5 |
|
| 6 |
marker = MeshFunction("size_t", mesh, mesh.topology().dim()-1, 0) |
| 7 |
for f in facets(mesh): |
| 8 |
marker[f] = 0.5 - DOLFIN_EPS < f.midpoint().z() < 0.5 + DOLFIN_EPS |
| 9 |
|
| 10 |
submesh = MeshView.create(marker, 1) |
| 11 |
|
| 12 |
W1 = FunctionSpace(mesh, "Lagrange", 1) ## 3D |
| 13 |
W2 = FunctionSpace(submesh, "Lagrange", 1) ## 2D |
| 14 |
|
| 15 |
# Define the mixed function space |
| 16 |
V = MixedFunctionSpace( W1, W2 ) |
| 17 |
|
| 18 |
# Define Dirichlet boundary (x = 0 or x = 1) |
| 19 |
def boundary(x): |
| 20 |
return x[0] < DOLFIN_EPS or x[0] > 1.0 - DOLFIN_EPS |
| 21 |
|
| 22 |
# Define boundary condition |
| 23 |
u0 = Constant(0.0) |
| 24 |
# 3D mesh |
| 25 |
bc_3D = DirichletBC(V.sub_space(0), u0, boundary) |
| 26 |
# 2D submesh |
| 27 |
bc_2D = DirichletBC(V.sub_space(1), u0, boundary) |
| 28 |
bcs = [bc_3D,bc_2D] |
| 29 |
|
| 30 |
# Define variational problem |
| 31 |
# 3D |
| 32 |
u_3D = TrialFunction(W1) |
| 33 |
v_3D = TestFunction(W1) |
| 34 |
# 2D |
| 35 |
u_2D = TrialFunction(W2) |
| 36 |
v_2D = TestFunction(W2) |
| 37 |
# Use directly TrialFunction and TestFunction on the product space |
| 38 |
(u_3D_m,u_2D_m) = TrialFunctions(V) |
| 39 |
(v_3D_m,v_2D_m) = TestFunctions(V) |
| 40 |
|
| 41 |
f = Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2) ) / 0.02)", degree=2) |
| 42 |
|
| 43 |
# Weak form - 1 (3D problem) |
| 44 |
a_3D = inner(grad(u_3D), grad(v_3D))*dx |
| 45 |
L_3D = f*v_3D*dx |
| 46 |
u_3D = Function(W1) |
| 47 |
solve(a_3D == L_3D, u_3D, bc_3D) |
| 48 |
# Weak form - 2 (2D problem) |
| 49 |
a_2D = inner(grad(u_2D), grad(v_2D))*dx |
| 50 |
L_2D = f*v_2D*dx |
| 51 |
u_2D = Function(W2) |
| 52 |
solve(a_2D == L_2D, u_2D, bc_2D) |
| 53 |
|
| 54 |
# Global weak form (Mixed problem) |
| 55 |
a = inner(grad(u_3D_m), grad(v_3D_m))*dx + inner(grad(u_2D_m), grad(v_2D_m))*dx |
| 56 |
L = f*v_3D_m*dx + f*v_2D_m*dx |
| 57 |
|
| 58 |
# Solve the problem |
| 59 |
sol = Function(V) |
| 60 |
|
| 61 |
# rtol=1e-4 |
| 62 |
# print("******************************************************************") |
| 63 |
# print("NOTE : Relative tolerance for Krylov solver has been set to", rtol) |
| 64 |
# print("With default solver/preconditioner, convergence is very slow.") |
| 65 |
# print("We need to find a better configuration.") |
| 66 |
# print("******************************************************************") |
| 67 |
# solve(a == L, sol, bcs=bcs, solver_parameters={"krylov_solver":{"relative_tolerance":rtol, "maximum_iterations":10000}}) |
| 68 |
|
| 69 |
# Direct solve |
| 70 |
solve(a == L, sol, bcs, solver_parameters={"linear_solver":"direct"}) |
| 71 |
sol_3D = sol.sub(0) |
| 72 |
sol_2D = sol.sub(1) |
| 73 |
|
| 74 |
assert len(u_3D.vector()) == len(sol_3D.vector()) |
| 75 |
for i in range(len(u_3D.vector())): |
| •76 |
assert abs(u_3D.vector()[i] - sol_3D.vector()[i]) < 5e-9 |
| 77 |
|
| 78 |
assert len(u_2D.vector()) == len(sol_2D.vector()) |
| 79 |
for i in range(len(u_2D.vector())): |
| 80 |
assert abs(u_2D.vector()[i] - sol_2D.vector()[i]) < 1e-10 |
| 81 |
|
| 82 |
## Export result |
| 83 |
encoding = XDMFFile.Encoding.HDF5 if has_hdf5() else XDMFFile.Encoding.ASCII |
| 84 |
|
| 85 |
out_u3D = XDMFFile(MPI.comm_world, "block-assembly-3D2D-3Dsol-ref.xdmf") |
| 86 |
out_u2D = XDMFFile(MPI.comm_world, "block-assembly-3D2D-2Dsol-ref.xdmf") |
| 87 |
out_3D = XDMFFile(MPI.comm_world, "block-assembly-3D2D-3Dsol.xdmf") |
| 88 |
out_2D = XDMFFile(MPI.comm_world, "block-assembly-3D2D-2Dsol.xdmf") |
| 89 |
|
| 90 |
if MPI.size(MPI.comm_world) > 1 and encoding == XDMFFile.Encoding.ASCII: |
| 91 |
print("XDMF file output not supported in parallel without HDF5") |
| 92 |
else: |
| 93 |
out_u3D.write(u_3D, encoding) |
| 94 |
out_u2D.write(u_2D, encoding) |
| 95 |
out_3D.write(sol_3D, encoding) |
| 96 |
out_2D.write(sol_2D, encoding) |