📄 demo.py
字号:
# This program illustrates the use of the DOLFIN for solving a nonlinear PDE# by solving the nonlinear variant of Poisson's equation## - div (1+u^2) grad u(x, y) = f(x, y)## on the unit square with source f given by## f(x, y) = t * x * sin(y)## and boundary conditions given by## u(x, y) = t for x = 0# du/dn(x, y) = 0 otherwise## where t is pseudo time.## This is equivalent to solving: # F(u) = (grad(v), (1-u^2)*grad(u)) - f(x,y) = 0## Original implementation: ../cpp/main.cpp by Garth N. Wells#__author__ = "Kristian B. Oelgaard (k.b.oelgaard@tudelft.nl)"__date__ = "2007-11-14 -- 2007-11-28"__copyright__ = "Copyright (C) 2007 Kristian B. Oelgaard"__license__ = "GNU LGPL Version 2.1"from dolfin import *# FIXME: Not working, see notice belowimport sysprint "This demo is not working, please fix me"sys.exit(1)# Create mesh and finite elementmesh = UnitSquare(16, 16)element = FiniteElement("Lagrange", "triangle", 1)# Source termclass Source(Function, TimeDependent): def __init__(self, element, mesh, t): Function.__init__(self, element, mesh) TimeDependent.__init__(self, t) def eval(self, values, x): values[0] = time()*x[0]*sin(x[1])# Dirichlet boundary conditionclass DirichletBoundaryCondition(Function, TimeDependent): def __init__(self, element, mesh, t): Function.__init__(self, element, mesh) TimeDependent.__init__(self, t) def eval(self, values, x): values[0] = 1.0*time()# Sub domain for Dirichlet boundary conditionclass DirichletBoundary(SubDomain): def inside(self, x, on_boundary): return bool(abs(x[0] - 1.0) < DOLFIN_EPS and on_boundary)# Pseudo timet = 0.0# Create source functionf = Source(element, mesh, t)# Dirichlet boundary conditionsdirichlet_boundary = DirichletBoundary()g = DirichletBoundaryCondition(element, mesh, t)bc = DirichletBC(g, mesh, dirichlet_boundary)# Solution functionu = Function(element, mesh)v = TestFunction(element)U = TrialFunction(element)U0= Function(element, mesh)a = v.dx(i)*(1.0 + U0*U0)*U.dx(i)*dx + v.dx(i)*2.0*U0*U*U0.dx(i)*dxL = v*f*dx - v.dx(i)*(1.0 + U0*U0)*U0.dx(i)*dxpde = NonlinearPDE(a, L, mesh, bc)# ERROR:# Traceback (most recent call last):# File "demo.py", line 80, in <module># pde = NonlinearPDE(a, L, mesh, bc)# NameError: name 'NonlinearPDE' is not defined# We need to define NonlinearPDE in assemble.py.# However, we need to solve the issues with NonlinearProblem first.# # Solve nonlinear problem in a series of steps# dt = 1.0# T = 3.0# # pde.set("Newton relative tolerance", 1e-6); # # pde.set("Newton convergence criterion", "incremental"); # # Solve# u = pde.solve(U0, t, T, dt);# # Plot solution# plot(u)# # Save function to file# file = File("nonlinear_poisson.pvd")# file << u
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -