| 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 |
V1 = FunctionSpace(mesh, "Lagrange", 1) ## 3D |
| 13 |
V2 = FunctionSpace(submesh, "Lagrange", 1) ## 2D |
| 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 |
# 2D submesh |
| 24 |
bc_2D = DirichletBC(V2, u0, boundary) |
| 25 |
|
| 26 |
# Define variational problem |
| 27 |
# 3D |
| 28 |
u_3D = TrialFunction(V1) |
| 29 |
v_3D = TestFunction(V1) |
| 30 |
# 2D |
| 31 |
u_2D = TrialFunction(V2) |
| 32 |
v_2D = TestFunction(V2) |
| 33 |
|
| 34 |
f = Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 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_2D = inner(grad(u_2D), grad(v_2D))*dx |
| 42 |
L_2D = f*v_2D*dx |
| 43 |
u_2D = Function(V2) |
| 44 |
solve(a_2D == L_2D, u_2D, bc_2D) |
| 45 |
|
| 46 |
# ## Export result |
| 47 |
# encoding = XDMFFile.Encoding.HDF5 if has_hdf5() else XDMFFile.Encoding.ASCII |
| 48 |
|
| 49 |
# out_3D = XDMFFile(MPI.comm_world, "meshview-mapping-3D2D-3Dsol.xdmf") |
| 50 |
# out_2D = XDMFFile(MPI.comm_world, "meshview-mapping-3D2D-2Dsol.xdmf") |
| 51 |
|
| 52 |
# if MPI.size(MPI.comm_world) > 1 and encoding == XDMFFile.Encoding.ASCII: |
| 53 |
# print("XDMF file output not supported in parallel without HDF5") |
| 54 |
# else: |
| 55 |
# out_3D.write(u_3D, encoding) |
| 56 |
# out_2D.write(u_2D, encoding) |