📄 conv-diff.py
字号:
from PyTrilinos import Epetra, AztecOO, TriUtils, ML from dolfin import *from sys import path path.append("../poisson")from Krylov import *class MLPreconditioner: def __init__(self, A): # Sets up the parameters for ML using a python dictionary MLList = { "max levels" : 5, "output" : 10, "smoother: type" : "ML symmetric Gauss-Seidel", "aggregation: type" : "Uncoupled", "ML validate parameter list" : False } ml_prec = ML.MultiLevelPreconditioner(A.mat(), False) ml_prec.SetParameterList(MLList) ml_prec.ComputePreconditioner() self.ml_prec = ml_prec def __mul__(self, b): x = b.copy() x.zero() err = self.ml_prec.ApplyInverse(b.vec(),x.vec()) if not err == 0: print "err ", err return -1 return x# Create mesh and finite elementN = 10 mesh = UnitSquare(N,N)element = FiniteElement("Lagrange", "triangle", 1)DG = FiniteElement("DG", "triangle", 0)vector_element = VectorElement("Lagrange", "triangle", 1)epsilon = 1.0/100w_value = 1.0# Source termclass Source(Function): def __init__(self, element, mesh): Function.__init__(self, element, mesh) def eval(self, values, x): values[0] = 0 # Source termclass BC(Function): def __init__(self, element, mesh): Function.__init__(self, element, mesh) def eval(self, values, x): y = x[1] c = 1/epsilon values[0] = exp(c*y)/exp(c) # Velocity termclass W(Function): def __init__(self, element, mesh): Function.__init__(self, element, mesh) def eval(self, values, x): values[0] = 0 values[1] = w_value def rank(self): return 1 def dim(self, i): return 2# Sub domain for Dirichlet boundary conditionclass DirichletBoundary(SubDomain): def inside(self, x, on_boundary): return bool(on_boundary)# Define variational problemv = TestFunction(element)w = W(vector_element, mesh)u = TrialFunction(element)f = Source(element, mesh)h = 1.0/Ntau = Function(DG, mesh, 5.0*h**2) eps = Function(DG, mesh, epsilon) a = eps*dot(grad(v), grad(u))*dx - dot(w, grad(u))*v*dx + tau*dot(dot(w, grad(u)), dot(w, grad(v)))*dxL = v*f*dx # Assemble matrix and right hand sidebackend = EpetraFactory.instance()A = assemble(a, mesh, backend=backend)b = assemble(L, mesh, backend=backend)# Define boundary conditionboundary = DirichletBoundary()bc_func = BC(element, mesh)bc = DirichletBC(bc_func, mesh, boundary)bc.apply(A, b, a)# create solution vector (also used as start vector) x = b.copy()x.zero()# create preconditionerB = MLPreconditioner(A)# solve the systemregularization_parameter = 1.0/2x = precRichardson(B, A, x, b, regularization_parameter, 10e-8, True, 20)#x = precondBiCGStab(B, A, x, b, 10e-6, True, 20)print x.norm(linf)# plot the solutionU = Function(element, mesh, x)plot(U)interactive()# Save solution to file#file = File("conv-diff.pvd")#file << U
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -