📄 demo.py
字号:
# This demo solves the Stokes equations, using quadratic# elements for the velocity and first degree elements for# the pressure (Taylor-Hood elements). The sub domains# for the different boundary conditions used in this# simulation are computed by the demo program in# src/demo/mesh/subdomains.## Original implementation: ../cpp/main.cpp by Anders Logg#__author__ = "Kristian B. Oelgaard (k.b.oelgaard@tudelft.nl)"__date__ = "2007-11-16 -- 2007-12-03"__copyright__ = "Copyright (C) 2007 Kristian B. Oelgaard"__license__ = "GNU LGPL Version 2.1"from dolfin import *# Load mesh and create finite elementsmesh = Mesh("../../../../../data/meshes/dolfin-2.xml.gz")scalar = FiniteElement("Lagrange", "triangle", 1)vector = VectorElement("Lagrange", "triangle", 2)system = vector + scalar# Load subdomainssub_domains = MeshFunction("uint", mesh, "../subdomains.xml.gz")# Function for no-slip boundary condition for velocityclass Noslip(Function): def __init__(self, mesh): Function.__init__(self, mesh) def eval(self, values, x): values[0] = 0.0 values[1] = 0.0 def rank(self): return 1 def dim(self, i): return 2# Function for inflow boundary condition for velocityclass Inflow(Function): def __init__(self, mesh): Function.__init__(self, mesh) def eval(self, values, x): values[0] = -sin(x[1]*DOLFIN_PI) values[1] = 0.0 def rank(self): return 1 def dim(self, i): return 2# Create functions for boundary conditionsnoslip = Noslip(mesh)inflow = Inflow(mesh)zero = Function(mesh, 0.0)# Define sub systems for boundary conditionsvelocity = SubSystem(0)pressure = SubSystem(1)# No-slip boundary condition for velocitybc0 = DirichletBC(noslip, sub_domains, 0, velocity)# Inflow boundary condition for velocitybc1 = DirichletBC(inflow, sub_domains, 1, velocity)# Boundary condition for pressure at outflowbc2 = DirichletBC(zero, sub_domains, 2, pressure)# Collect boundary conditionsbcs = [bc0, bc1, bc2]# Define variational problem(v, q) = TestFunctions(system)(u, p) = TrialFunctions(system)f = Function(vector, mesh, 0.0)a = (dot(grad(v), grad(u)) - div(v)*p + q*div(u))*dxL = dot(v, f)*dx# Set up PDEpde = LinearPDE(a, L, mesh, bcs)# Solve PDE(U, P) = pde.solve().split()# Save solutionufile = File("velocity.xml")ufile << Upfile = File("pressure.xml")pfile << P# Save solution in VTK formatufile_pvd = File("velocity.pvd")ufile_pvd << Upfile_pvd = File("pressure.pvd")pfile_pvd << P# Plot solutionplot(U)plot(P)interactive()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -