⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo.py

📁 Dolfin provide a high-performance linear algebra library
💻 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) = 1  for x = 1#     du/dn(x, y) = 0  otherwise__author__ = "Anders Logg (logg@simula.no)"__date__ = "2007-08-16 -- 2007-08-21"__copyright__ = "Copyright (C) 2007 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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -