| 1 |
from dolfin import * |
| 2 |
|
| 3 |
# Sub domains for Dirichlet boundary conditions |
| 4 |
class DirichletBoundary1(SubDomain): |
| 5 |
def inside(self, x, on_boundary): |
| 6 |
return abs(x[0] - 0.0) < DOLFIN_EPS and on_boundary |
| 7 |
class DirichletBoundary2(SubDomain): |
| 8 |
def inside(self, x, on_boundary): |
| 9 |
return abs(x[0] - 1.0) < DOLFIN_EPS and on_boundary |
| 10 |
|
| 11 |
def newton_solver_parameters(): |
| 12 |
return{"nonlinear_solver": "newton", |
| 13 |
"newton_solver": {"linear_solver": "gmres"}} |
| 14 |
|
| 15 |
# Create meshes |
| 16 |
n = 16 |
| 17 |
mesh = UnitSquareMesh(n, n) |
| 18 |
|
| 19 |
marker = MeshFunction("size_t", mesh, mesh.topology().dim(), 0) |
| 20 |
for c in cells(mesh): |
| 21 |
marker[c] = c.midpoint().x() < 0.5 |
| 22 |
|
| 23 |
submesh1 = MeshView.create(marker, 1) |
| 24 |
submesh2 = MeshView.create(marker, 0) |
| 25 |
|
| 26 |
# Create function spaces |
| 27 |
W1 = FunctionSpace(submesh1, "Lagrange", 1) |
| 28 |
W2 = FunctionSpace(submesh2, "Lagrange", 1) |
| 29 |
# Define the mixed function space W = W1 x W2 |
| 30 |
W = MixedFunctionSpace( W1, W2 ) |
| 31 |
|
| 32 |
# Define boundary conditions |
| 33 |
g = Constant(1.0) |
| 34 |
bc1 = DirichletBC(W1, g, DirichletBoundary1()) |
| 35 |
bc2 = DirichletBC(W2, g, DirichletBoundary2()) |
| 36 |
bcs = [bc1,bc2] |
| 37 |
|
| 38 |
f = Expression("x[0]*sin(x[1])", degree=2) |
| 39 |
|
| 40 |
# Define mixed-domains variational problem |
| 41 |
(v1,v2) = TestFunctions(W) |
| 42 |
u = Function(W) |
| 43 |
u1 = u.sub(0) |
| 44 |
u2 = u.sub(1) |
| 45 |
|
| 46 |
dx1 = Measure("dx", domain=W.sub_space(0).mesh()) |
| 47 |
dx2 = Measure("dx", domain=W.sub_space(1).mesh()) |
| 48 |
|
| 49 |
F1 = inner((1 + u1**2)*grad(u1), grad(v1))*dx1 - f*v1*dx1 |
| 50 |
F2 = inner((1 + u2**2)*grad(u2), grad(v2))*dx2 - f*v2*dx2 |
| 51 |
F = F1 + F2 |
| 52 |
|
| 53 |
# Compute solution - ref monodomain problems |
| 54 |
solve(F1 == 0, u1, bc1) |
| 55 |
u1_ref = u1.copy(deepcopy=True) |
| 56 |
solve(F2 == 0, u2, bc2) |
| 57 |
u2_ref = u2.copy(deepcopy=True) |
| 58 |
|
| 59 |
# Compute solution - mixed-domains problem |
| 60 |
solve(F == 0, u, bcs, solver_parameters=newton_solver_parameters()) |
| 61 |
# solve(F == 0, u, bcs, solver_parameters={"nonlinear_solver":"snes"}) # Not available yet |
| 62 |
|
| 63 |
for i in range(len(u1.vector())): |
| 64 |
assert abs(u1_ref.vector()[i] - u1.vector()[i]) < 1e-8 |
| 65 |
for i in range(len(u2.vector())): |
| 66 |
assert abs(u2_ref.vector()[i] - u2.vector()[i]) < 1e-8 |
| 67 |
|
| 68 |
## Export result |
| 69 |
encoding = XDMFFile.Encoding.HDF5 if has_hdf5() else XDMFFile.Encoding.ASCII |
| 70 |
out_ref1 = XDMFFile(MPI.comm_world, "block-assembly-2D2D-nonlinear-subdomain1-ref.xdmf") |
| 71 |
out_ref2 = XDMFFile(MPI.comm_world, "block-assembly-2D2D-nonlinear-subdomain2-ref.xdmf") |
| 72 |
out_sub1 = XDMFFile(MPI.comm_world, "block-assembly-2D2D-nonlinear-subdomain1.xdmf") |
| 73 |
out_sub2 = XDMFFile(MPI.comm_world, "block-assembly-2D2D-nonlinear-subdomain2.xdmf") |
| 74 |
|
| 75 |
if MPI.size(MPI.comm_world) > 1 and encoding == XDMFFile.Encoding.ASCII: |
| 76 |
print("XDMF file output not supported in parallel without HDF5") |
| 77 |
else: |
| 78 |
out_ref1.write(u1_ref, encoding) |
| 79 |
out_ref2.write(u2_ref, encoding) |
| 80 |
out_sub1.write(u1, encoding) |
| 81 |
out_sub2.write(u2, encoding) |