Rev. | 5f3b4941f4485411daafa5c3a42017647da24277 |
---|---|
大小 | 1,337 字节 |
时间 | 2007-11-06 18:03:33 |
作者 | iselllo |
Log Message | I added the code dolfin_demo.py which solves Poisson equation on a 2D
|
#! /usr/bin/env python
from dolfin import *
# Create mesh and finite element
mesh = UnitSquare(32, 32)
element = FiniteElement("Lagrange", "triangle", 1)
# Source term
class Source(Function):
def __init__(self, element, mesh):
Function.__init__(self, element, mesh)
def eval(self, values, x):
dx = x[0] - 0.5
dy = x[1] - 0.5
values[0] = 500.0*exp(-(dx*dx + dy*dy)/0.02)
# Neumann boundary condition
class Flux(Function):
def __init__(self, element, mesh):
Function.__init__(self, element, mesh)
def eval(self, values, x):
if x[0] > DOLFIN_EPS:
values[0] = 25.0*sin(5.0*DOLFIN_PI*x[1])
else:
values[0] = 0.0
# Sub domain for Dirichlet boundary condition
class DirichletBoundary(SubDomain):
def inside(self, x, on_boundary):
return bool(on_boundary and x[0] < DOLFIN_EPS)
# Define variational problem
v = TestFunction(element)
u = TrialFunction(element)
f = Source(element, mesh)
g = Flux(element, mesh)
a = dot(grad(v), grad(u))*dx
L = v*f*dx + v*g*ds
# Define boundary condition
u0 = Function(mesh, 0.0)
boundary = DirichletBoundary()
bc = DirichletBC(u0, mesh, boundary)
# Solve PDE and plot solution
pde = LinearPDE(a, L, mesh, bc)
u = pde.solve()
plot(u)
# Save solution to file
file = File("poisson.pvd")
file << u