| 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()-2, 0) |
| 7 |
for e in edges(mesh): |
| 8 |
marker[e] = 0.5 - DOLFIN_EPS < e.midpoint().z() < 0.5 + DOLFIN_EPS and 0.5 - DOLFIN_EPS < e.midpoint().y() < 0.5 + DOLFIN_EPS |
| 9 |
|
| 10 |
submesh = MeshView.create(marker, 1) |
| 11 |
|
| 12 |
V1 = FunctionSpace(mesh, "Lagrange", 1) ## 3D |
| 13 |
V2 = FunctionSpace(submesh, "Lagrange", 1) ## 1D |
| 14 |
|
| 15 |
# Define Dirichlet boundary (x = 0 or x = 1) |
| 16 |
def boundary(x): |
| 17 |
return x[0] < DOLFIN_EPS or x[0] > 1.0 - DOLFIN_EPS |
| 18 |
|
| 19 |
# Define boundary condition |
| 20 |
u0 = Constant(0.0) |
| 21 |
# 3D mesh |
| 22 |
bc_3D = DirichletBC(V1, u0, boundary) |
| 23 |
# 1D submesh |
| 24 |
bc_1D = DirichletBC(V2, u0, boundary) |
| 25 |
|
| 26 |
# Define variational problem |
| 27 |
# 3D |
| 28 |
u_3D = TrialFunction(V1) |
| 29 |
v_3D = TestFunction(V1) |
| 30 |
# 1D |
| 31 |
u_1D = TrialFunction(V2) |
| 32 |
v_1D = TestFunction(V2) |
| 33 |
|
| 34 |
f = Expression("10*exp(-(pow(x[0] - 0.5, 2) ) / 0.02)", degree=2) |
| 35 |
|
| 36 |
a_3D = inner(grad(u_3D), grad(v_3D))*dx |
| 37 |
L_3D = f*v_3D*dx |
| 38 |
u_3D = Function(V1) |
| 39 |
solve(a_3D == L_3D, u_3D, bc_3D) |
| 40 |
|
| 41 |
a_1D = inner(grad(u_1D), grad(v_1D))*dx |
| 42 |
L_1D = f*v_1D*dx |
| 43 |
u_1D = Function(V2) |
| 44 |
solve(a_1D == L_1D, u_1D, bc_1D) |
| 45 |
|
| 46 |
# Save solution in vtk format |
| 47 |
out_3D = File("meshview-mapping-3D1D-3Dsol.pvd") |
| 48 |
out_3D << u_3D |
| 49 |
out_1D = File("meshview-mapping-3D1D-1Dsol.pvd") |
| 50 |
out_1D << u_1D |