📄 demo.py
字号:
"""This demo program solves Poisson's equation - div grad u(x, y) = f(x, y)on the unit square with source f given by f(x, y) = 500*exp(-((x-0.5)^2 + (y-0.5)^2)/0.02)and boundary conditions given by u(x, y) = 0 for x = 0 du/dn(x, y) = 25 sin(5 pi y) for x = 1 du/dn(x, y) = 0 otherwise"""__author__ = "Anders Logg (logg@simula.no)"__date__ = "2007-08-16 -- 2008-04-03"__copyright__ = "Copyright (C) 2007-2008 Anders Logg"__license__ = "GNU LGPL Version 2.1"from dolfin import *# Create mesh and finite elementmesh = UnitSquare(32, 32)element = FiniteElement("Lagrange", "triangle", 1)# Source termclass 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 conditionclass 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 conditionclass DirichletBoundary(SubDomain): def inside(self, x, on_boundary): return bool(on_boundary and x[0] < DOLFIN_EPS)# Define variational problemv = TestFunction(element)u = TrialFunction(element)f = Source(element, mesh)g = Flux(element, mesh)a = dot(grad(v), grad(u))*dxL = v*f*dx + v*g*ds# Define boundary conditionu0 = Function(mesh, 0.0)boundary = DirichletBoundary()bc = DirichletBC(u0, mesh, boundary)# Solve PDE and plot solutionpde = LinearPDE(a, L, mesh, bc)u = pde.solve()plot(u)# Save solution to filefile = File("poisson.pvd")file << u# Hold plotinteractive()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -