Debian Codegraph @

python/demo/undocumented/block-assembly-3D1D/demo_block-assembly-3D1D.py

raw
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 W1 = FunctionSpace(mesh, "Lagrange", 1) ## 3D
13 W2 = FunctionSpace(submesh, "Lagrange", 1) ## 1D
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 # 1D submesh
27 bc_1D = DirichletBC(V.sub_space(1), u0, boundary)
28 bcs = [bc_3D,bc_1D]
29
30 # Define variational problem
31 # Use directly TrialFunction and TestFunction on the product space
32 (u_3D,u_1D) = TrialFunctions(V)
33 (v_3D,v_1D) = TestFunctions(V)
34
35 f = Expression("10*exp(-(pow(x[0] - 0.5, 2) ) / 0.02)", degree=2)
36
37 # Global weak form
38 a = inner(grad(u_3D), grad(v_3D))*dx + inner(grad(u_1D), grad(v_1D))*dx
39 L = f*v_3D*dx + f*v_1D*dx
40
41 # Solve the problem
42 sol = Function(V)
43 rtol=1e-4
44 print("******************************************************************")
45 print("NOTE : Relative tolerance for Krylov solver has been set to", rtol)
46 print("With default solver/preconditioner, convergence is very slow.")
47 print("We need to find a better configuration.")
48 print("******************************************************************")
49 solve(a == L, sol, bcs=bcs, solver_parameters={"krylov_solver":{"relative_tolerance":rtol, "maximum_iterations":10000}})
50
51 # extract components of the solution
52 sol_3D = sol.sub(0)
53 sol_1D = sol.sub(1)
54
55 ## Export result
56 encoding = XDMFFile.Encoding.HDF5 if has_hdf5() else XDMFFile.Encoding.ASCII
57
58 out_3D = XDMFFile(MPI.comm_world, "block-assembly-3D1D-3Dsol.xdmf")
59 out_1D = XDMFFile(MPI.comm_world, "block-assembly-3D1D-1Dsol.xdmf")
60
61 if MPI.size(MPI.comm_world) > 1 and encoding == XDMFFile.Encoding.ASCII:
62 print("XDMF file output not supported in parallel without HDF5")
63 else:
64 out_3D.write(sol_3D, encoding)
65 out_1D.write(sol_1D, encoding)
66