Debian Codegraph @

python/demo/undocumented/matnest/demo_matnest.py

raw
1 # Copyright (C) 2016 Garth N. Wells and Chris N. Richardson
2 #
3 # This file is part of DOLFIN.
4 #
5 # DOLFIN is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # DOLFIN is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17
18 from dolfin import *
19
20 if not has_linear_algebra_backend("PETSc"):
21 print("DOLFIN has not been configured with PETSc. Exiting.")
22 exit()
23
24 PETScOptions.set("ksp_view");
25 PETScOptions.set("ksp_monitor_true_residual");
26 PETScOptions.set("pc_type", "fieldsplit");
27 PETScOptions.set("pc_fieldsplit_type", "additive");
28
29 PETScOptions.set("fieldsplit_0_ksp_type", "preonly");
30 PETScOptions.set("fieldsplit_0_pc_type", "gamg");
31 PETScOptions.set("fieldsplit_0_pc_gamg_coarse_eq_limit", 10000);
32 PETScOptions.set("fieldsplit_0_pc_gamg_threshold", 0.05);
33 PETScOptions.set("fieldsplit_0_mg_levels_ksp_type", "chebyshev");
34 PETScOptions.set("fieldsplit_0_mg_levels_pc_type", "sor");
35 PETScOptions.set("fieldsplit_0_mg_levels_ksp_max_it", 3);
36 PETScOptions.set("fieldsplit_0_mg_levels_esteig_ksp_type", "cg");
37 PETScOptions.set("fieldsplit_0_mg_levels_ksp_chebyshev_esteig_steps", 20);
38 PETScOptions.set("fieldsplit_0_mg_levels_ksp_chebyshev_esteig_random");
39 PETScOptions.set("fieldsplit_0_mg_coarse_ksp_type", "preonly");
40 PETScOptions.set("fieldsplit_0_mg_coarse_pc_type", "lu");
41 PETScOptions.set("fieldsplit_0_mg_coarse_pc_factor_mat_solver_package", "mumps");
42 PETScOptions.set("fieldsplit_1_pc_factor_mat_solver_package", "mumps");
43 PETScOptions.set("fieldsplit_1_ksp_type", "preonly");
44 PETScOptions.set("fieldsplit_1_pc_type", "lu");
45
46 # Sub domain for top and bottom
47 def TopBottom(x, on_boundary):
48 return abs(1.0 - x[1]) < DOLFIN_EPS or abs(x[1]) < DOLFIN_EPS
49
50 mesh = UnitCubeMesh(12, 12, 12);
51
52 # Create function spaces
53 P2 = VectorElement("Lagrange", mesh.ufl_cell(), 2)
54 P1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
55 V = FunctionSpace(mesh, P2)
56 Q = FunctionSpace(mesh, P1)
57
58 u = TrialFunction(V)
59 p = TrialFunction(Q)
60 v = TestFunction(V)
61 q = TestFunction(Q)
62
63 # Define forms for each block
64 a00 = inner(grad(u), grad(v))*dx
65 a01 = -div(v)*p*dx
66 a10 = -div(u)*q*dx
67 # Preconditiner uses pressure mass matrix
68 p11 = p*q*dx
69
70 f = Constant((0.0, 1.0, 0.0))
71 g = Constant(0.0)
72 L0 = dot(f, v)*dx
73 L1 = dot(g, q)*dx
74
75 # Velocity BC
76 flow_velocity = Constant((0.0, 0.0, 0.0));
77 bc0 = DirichletBC(V, flow_velocity, TopBottom)
78
79 # Assemble all blocks with BCs
80 bcsV = [bc0]
81 bcsQ = []
82
83 assemblerA = SystemAssembler([a00, a01, a10, None], [L0, L1], [bcsV, bcsQ])
84
85 A00 = PETScMatrix()
86 A01 = PETScMatrix()
87 A10 = PETScMatrix()
88
89 b0 = PETScVector()
90 b1 = PETScVector()
91
92 assemblerA.assemble([A00, A01, A10, None], [b0, b1])
93
94 P11 = PETScMatrix()
95 assemblerP = SystemAssembler(p11, L1, bcsQ)
96 assemblerP.assemble(P11)
97
98 print("A00 = ", A00.size(0), "x" , A00.size(1), A00.norm("frobenius"))
99 print("A01 = ", A01.size(0), "x" , A01.size(1), A01.norm("frobenius"))
100 print("A10 = ", A10.size(0), "x" , A10.size(1), A10.norm("frobenius"))
101 print("P11 = ", P11.size(0), "x" , P11.size(1), P11.norm("frobenius"))
102
103 # Combine matrices
104 AA = PETScNestMatrix([A00, A01, A10, None])
105 PP = PETScNestMatrix([A00, None, None, P11])
106
107 # Create solution vectors
108 u = Function(V)
109 u.rename('u','velocity')
110 p = Function(Q)
111 p.rename('p','pressure')
112 # Map RHS and solution into block space vectors
113 x = Vector()
114 b = Vector()
115 AA.init_vectors(x, [u.vector(), p.vector()])
116 AA.init_vectors(b, [b0, b1])
117
118 solver = PETScKrylovSolver("minres")
119 solver.set_from_options()
120 solver.set_operators(AA, PP)
121
122 fields = [PETScNestMatrix.get_block_dofs(AA, i) for i in [0,1]]
123 PETScPreconditioner.set_fieldsplit(solver, fields, ["0", "1"])
124
125 solver.solve(x, b)
126
127 print("u.norm = ", u.vector().norm("l2"))
128
129 if has_hdf5_parallel():
130 xdmf = XDMFFile("solution.xdmf")
131 xdmf.write(u)
132 xdmf.write(p)