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

📄 heat_complete.py

📁 利用有限元算法计算各类微分方程
💻 PY
字号:
from dolfin import *# Source termclass Source(Function):    def __init__(self, element, mesh):        Function.__init__(self, element, mesh)    def eval(self, values, x):	values[0] = 0.0# Source termclass InitialValue(Function):    def __init__(self, element, mesh):        Function.__init__(self, element, mesh)    def eval(self, values, x):	values[0] = 1.0# Neumann boundary conditionclass Flux(Function):    def __init__(self, element, mesh):        Function.__init__(self, element, mesh)    def eval(self, values, x):        values[0] = 0.0# Sub domain for Dirichlet boundary conditionclass DirichletBoundary(SubDomain):    def inside(self, x, on_boundary):        return bool(on_boundary)# Load mesh and create finite elementsmesh = Mesh("Square.xml")mesh.refine()element = FiniteElement("Lagrange", "triangle", 1)element2 = FiniteElement("Lagrange", "triangle", 2)# Define variational problemv = TestFunction(element)U = TrialFunction(element)f = Source(element2, mesh)g = Flux(element, mesh)c = 0.01# Sub domain for Dirichlet boundary conditionclass DirichletBoundary(SubDomain):    def inside(self, x, on_boundary):        return bool(on_boundary)# Initialise source function and previous solution functionf = Source(element2, mesh)u0 = InitialValue(element, mesh)# ParametersT = 0.5k = 0.01t = k# Test and trial functionsv = TestFunction(element)u = TrialFunction(element)# Functionsx0 = Vector()x1 = Vector()u1 = Function(element, mesh, x1)h = MeshSize("triangle", mesh)c = 1.0# Variational problema = (u*v+k*dot(grad(u),grad(v)))*dxL = (u0*v+f*v)*dx# Set up boundary conditionboundary = DirichletBoundary()zero = Function(mesh, 0.0)bc = DirichletBC(zero, mesh, boundary)# Assemble matrix (since heat coefficient c is constant in time, the matrix# A is also constant in time)A = assemble(a, mesh)# Output fileout_file = File("temperature.pvd")# Time-steppingwhile t < T:    # Assemble vector and apply boundary conditions    b = assemble(L, mesh)    bc.apply(A, b, a)        # Solve the linear system    solve(A, x1, b)        # Save the solution to file    out_file << u1    # Move to next interval    t += k    # Copy solution from previous interval    u0.assign(u1)

⌨️ 快捷键说明

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