Fenics示例ft06_elasticity.py中如何解决未定义名称“ nabla_div”的错误?

时间:2019-08-07 08:41:05

标签: python linux python-3.x conda fenics

我在Ubuntu 18.04上使用Conda安装了Fenics,并在运行其ft06_elasticity.py示例时收到以下错误。

我试图在文档中找到解决方案或解决方法,但是我什至在任何地方都找不到nabla_div()函数描述。

疯子documentation指出以下内容:

  

nabla_grad

     

渐变和散度运算符现在具有前缀nabla_。这个   在当前问题中完全没有必要,但建议在   由连续介质力学产生的矢量PDE的一般信息,如果您   在PDE表示法中将∇解释为向量;见盒子   nabla_grad在“变分公式”部分。

"""
FEniCS tutorial demo program: Linear elastic problem.
  -div(sigma(u)) = f
The model is used to simulate an elastic beam clamped at
its left end and deformed under its own weight.
"""

from __future__ import print_function
from fenics import *

# Scaled variables
L = 1; W = 0.2
mu = 1
rho = 1
delta = W/L
gamma = 0.4*delta**2
beta = 1.25
lambda_ = beta
g = gamma

# Create mesh and define function space
mesh = BoxMesh(Point(0, 0, 0), Point(L, W, W), 10, 3, 3)
V = VectorFunctionSpace(mesh, 'P', 1)

# Define boundary condition
tol = 1E-14

def clamped_boundary(x, on_boundary):
    return on_boundary and x[0] < tol

bc = DirichletBC(V, Constant((0, 0, 0)), clamped_boundary)

# Define strain and stress

def epsilon(u):
    return 0.5*(nabla_grad(u) + nabla_grad(u).T)
    #return sym(nabla_grad(u))

def sigma(u):
    return lambda_*nabla_div(u)*Identity(d) + 2*mu*epsilon(u)

# Define variational problem
u = TrialFunction(V)
d = u.geometric_dimension()  # space dimension
v = TestFunction(V)
f = Constant((0, 0, -rho*g))
T = Constant((0, 0, 0))
a = inner(sigma(u), epsilon(v))*dx
L = dot(f, v)*dx + dot(T, v)*ds

# Compute solution
u = Function(V)
solve(a == L, u, bc)

# Plot solution
plot(u, title='Displacement', mode='displacement')

# Plot stress
s = sigma(u) - (1./3)*tr(sigma(u))*Identity(d)  # deviatoric stress
von_Mises = sqrt(3./2*inner(s, s))
V = FunctionSpace(mesh, 'P', 1)
von_Mises = project(von_Mises, V)
plot(von_Mises, title='Stress intensity')

# Compute magnitude of displacement
u_magnitude = sqrt(dot(u, u))
u_magnitude = project(u_magnitude, V)
plot(u_magnitude, 'Displacement magnitude')
print('min/max u:',
      u_magnitude.vector().array().min(),
      u_magnitude.vector().array().max())

# Save solution to file in VTK format
File('elasticity/displacement.pvd') << u
File('elasticity/von_mises.pvd') << von_Mises
File('elasticity/magnitude.pvd') << u_magnitude

# Hold plot
interactive()
Traceback (most recent call last):
  File "fenics_ft06_elasticity.py", line 48, in <module>
    a = inner(sigma(u), epsilon(v))*dx
  File "fenics_ft06_elasticity.py", line 40, in sigma
    return lambda_*nabla_div(u)*Identity(d) + 2*mu*epsilon(u)
NameError: name 'nabla_div' is not defined

2 个答案:

答案 0 :(得分:0)

我发现仅用“ div(u)”替换“ nabla_div(u)”就解决了该错误。但是,它确实直接导致了下一个错误:

formula

答案 1 :(得分:0)

只需将这两行添加到代码的开头即可使用nabla_grad和nabla_div:

from ufl import nabla_grad
from ufl import nabla_div