| 1 |
from dolfin import * |
| 2 |
set_log_level(LogLevel.ERROR) |
| 3 |
|
| 4 |
# Create mesh and define function space |
| 5 |
mesh = UnitSquareMesh(32, 32) |
| 6 |
|
| 7 |
V = FunctionSpace(mesh, "Lagrange", 1) |
| 8 |
|
| 9 |
marker = MeshFunction("size_t", mesh, mesh.topology().dim(), 0) |
| 10 |
for c in cells(mesh): |
| 11 |
marker[c] = c.midpoint().y() < 0.5 |
| 12 |
#marker[c] = c.midpoint().x() < 0.5 |
| 13 |
|
| 14 |
submesh1 = MeshView.create(marker, 1) |
| 15 |
submesh2 = MeshView.create(marker, 0) |
| 16 |
|
| 17 |
V1 = FunctionSpace(submesh1, "Lagrange", 1) |
| 18 |
V2 = FunctionSpace(submesh2, "Lagrange", 1) |
| 19 |
|
| 20 |
# Define Dirichlet boundary (x = 0 or x = 1) |
| 21 |
def boundary(x): |
| 22 |
return x[1] < DOLFIN_EPS or x[1] > 1.0 - DOLFIN_EPS |
| 23 |
#return x[0] < DOLFIN_EPS or x[0] > 1.0 - DOLFIN_EPS |
| 24 |
|
| 25 |
def boundarySub1(x): |
| 26 |
return x[1] < DOLFIN_EPS |
| 27 |
#return x[0] < DOLFIN_EPS |
| 28 |
|
| 29 |
def boundarySub2(x): |
| 30 |
return x[1] > 1.0 - DOLFIN_EPS |
| 31 |
#return x[0] > 1.0 - DOLFIN_EPS |
| 32 |
|
| 33 |
# Define boundary condition |
| 34 |
u0 = Constant(0.0) |
| 35 |
# Whole domain |
| 36 |
bc = DirichletBC(V, u0, boundary) |
| 37 |
# Subdomain 1 |
| 38 |
bc1 = DirichletBC(V1, u0, boundarySub1) |
| 39 |
# Subdomain 2 |
| 40 |
bc2 = DirichletBC(V2, u0, boundarySub2) |
| 41 |
|
| 42 |
# Define variational problem |
| 43 |
# Whole domain |
| 44 |
u = TrialFunction(V) |
| 45 |
v = TestFunction(V) |
| 46 |
# Subdomain 1 |
| 47 |
u1 = TrialFunction(V1) |
| 48 |
v1 = TestFunction(V1) |
| 49 |
# Subdomain 2 |
| 50 |
u2 = TrialFunction(V2) |
| 51 |
v2 = TestFunction(V2) |
| 52 |
|
| 53 |
f = Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)", degree=2) |
| 54 |
|
| 55 |
# Whole domain |
| 56 |
a = inner(grad(u), grad(v))*dx |
| 57 |
L = f*v*dx |
| 58 |
u = Function(V) |
| 59 |
solve(a == L, u, bc) |
| 60 |
# Subdomain 1 |
| 61 |
a1 = inner(grad(u1), grad(v1))*dx |
| 62 |
L1 = f*v1*dx |
| 63 |
u1 = Function(V1) |
| 64 |
solve(a1 == L1, u1, bc1) |
| 65 |
# Subdomain 2 |
| 66 |
a2 = inner(grad(u2), grad(v2))*dx |
| 67 |
L2 = f*v2*dx |
| 68 |
u2 = Function(V2) |
| 69 |
solve(a2 == L2, u2, bc2) |
| 70 |
|
| 71 |
## Export result |
| 72 |
encoding = XDMFFile.Encoding.HDF5 if has_hdf5() else XDMFFile.Encoding.ASCII |
| 73 |
|
| 74 |
out_global = XDMFFile(MPI.comm_world, "meshview-mapping-2D2D-global.xdmf") |
| 75 |
out_sub1 = XDMFFile(MPI.comm_world, "meshview-mapping-2D2D-subdomain1.xdmf") |
| 76 |
out_sub2 = XDMFFile(MPI.comm_world, "meshview-mapping-2D2D-subdomain2.xdmf") |
| 77 |
|
| 78 |
if MPI.size(MPI.comm_world) > 1 and encoding == XDMFFile.Encoding.ASCII: |
| 79 |
print("XDMF file output not supported in parallel without HDF5") |
| 80 |
else: |
| 81 |
out_global.write(u, encoding) |
| 82 |
out_sub1.write(u1, encoding) |
| 83 |
out_sub2.write(u2, encoding) |
| 84 |
|