1
from PyTrilinos import Epetra, AztecOO, TriUtils, ML
3
path.append("../poisson")
8
from BlockLinearAlgebra import *
12
class MLPreconditioner:
13
def __init__(self, A):
14
# Sets up the parameters for ML using a python dictionary
17
"output" : 0, # as little output as possible
18
"smoother: type" : "ML symmetric Gauss-Seidel",
19
"aggregation: type" : "Uncoupled",
20
"ML validate parameter list" : False
22
ml_prec = ML.MultiLevelPreconditioner(A.mat(), False)
23
ml_prec.SetParameterList(MLList)
24
ml_prec.ComputePreconditioner()
25
self.ml_prec = ml_prec
29
err = self.ml_prec.ApplyInverse(b.vec(),x.vec())
36
def __init__(self, M, N):
40
self.M_prec = MLPreconditioner(M)
41
self.N_prec = MLPreconditioner(N)
44
if (not isinstance(b, BlockVector)):
45
raise TypeError, "Can not multiply DiagBlockMatrix with %s" % (str(type(b)))
46
if (not b.n == self.n):
47
raise ValueError, "dim(BlockVector) != dim(Prec) "
55
xx = BlockVector(x0, x1)
59
# Function for no-slip boundary condition for velocity
60
class Noslip(Function):
61
def __init__(self, mesh):
62
Function.__init__(self, mesh)
64
def eval(self, values, x):
74
# Function for inflow boundary condition for velocity
75
class Inflow(Function):
76
def __init__(self, mesh):
77
Function.__init__(self, mesh)
79
def eval(self, values, x):
89
mesh = Mesh("../../../data/meshes/dolfin-2.xml.gz")
90
sub_domains = MeshFunction("uint", mesh, "../../../demo/pde/stokes/taylor-hood/subdomains.xml.gz")
92
# create Taylor-Hood elements
94
V = VectorElement("CG", shape, 2)
95
Q = FiniteElement("CG", shape, 1)
97
# Test and trial functions
102
tau = Function(Q, mesh, 0.0)
103
f = Function(V, mesh, 0.0)
107
a00 = dot(grad(v), grad(u))*dx
109
# Divergence constraint
116
a11 = tau*dot(grad(p), grad(q))*dx
121
#FIXME: does not remember stabilization terms on rhs right now
125
# Create functions for boundary conditions
126
noslip = Noslip(mesh)
127
inflow = Inflow(mesh)
129
# No-slip boundary condition for velocity
130
bc0 = DirichletBC(noslip, sub_domains, 0)
132
# Inflow boundary condition for velocity
133
bc1 = DirichletBC(inflow, sub_domains, 1)
135
# Collect boundary conditions
138
# fetch the EpetraFactory backend
139
backend = EpetraFactory.instance()
142
A00 = assemble(a00, mesh, backend=backend)
143
A10 = assemble(a10, mesh, backend=backend)
144
A01 = assemble(a01, mesh, backend=backend)
145
A11 = assemble(a11, mesh, backend=backend)
147
# create right hand sides
148
b0 = assemble(L0, mesh, backend=backend)
149
b1 = assemble(L1, mesh, backend=backend)
153
bc.apply(A00, b0, a00)
157
# ensure that bc are in start vector
162
U = Function(V, mesh, x0)
163
P = Function(Q, mesh, x1)
166
# create block matrix
167
AA = BlockMatrix(2,2)
173
#file = File("A00.m"); file << A00
174
#file = File("A10.m"); file << A10
175
#file = File("A01.m"); file << A01
176
#file = File("A11.m"); file << A11
178
# create block vector
184
# create solution vector
190
# create matrices needed in the preconditioner
192
C11 = assemble(c11, mesh, backend=backend)
193
#file = File("C11.m"); file << C11
196
# create block preconditioner
197
BB = SaddlePrec(AA[0,0], C11)
199
xx, i, rho = precondBiCGStab(BB, AA, xx, bb, 10e-8, True, 200)