Hello all,
I am trying to compute the reaction force on a boundary in a linear elasticity problem, but have run into some trouble. Here is the MWE.
# Scaled variable
L = 1
W = 0.2
mu = 1
rho = 1
delta = W/L
gamma = 0.4*delta**2
beta = 1.25
lambda_ = beta
g = gamma
import numpy as np
import ufl
from mpi4py import MPI
from petsc4py.PETSc import ScalarType
from dolfinx import mesh, fem, plot, io
domain = mesh.create_box(MPI.COMM_WORLD, [np.array([0,0,0]), np.array([L, W, W])],
[20,6,6], cell_type=mesh.CellType.hexahedron)
V = fem.VectorFunctionSpace(domain, ("CG", 1))
def clamped_boundary(x):
return np.isclose(x[0], 0)
fdim = domain.topology.dim - 1
boundary_facets = mesh.locate_entities_boundary(domain, fdim, clamped_boundary)
u_D = np.array([0,0,0], dtype=ScalarType)
bc = fem.dirichletbc(u_D, fem.locate_dofs_topological(V, fdim, boundary_facets), V)
T = fem.Constant(domain, ScalarType((0, 0, 0)))
ds = ufl.Measure("ds", domain=domain)
subdomain_clamp = mesh.meshtags(domain, fdim, np.sort(boundary_facets), np.full(boundary_facets.shape[0],1))
ds_clamp = ufl.Measure("ds", domain=domain, subdomain_data=subdomain_clamp)
def epsilon(u):
return ufl.sym(ufl.grad(u)) # Equivalent to 0.5*(ufl.nabla_grad(u) + ufl.nabla_grad(u).T)
def sigma(u):
return lambda_ * ufl.nabla_div(u) * ufl.Identity(u.geometric_dimension()) + 2*mu*epsilon(u)
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
f = fem.Constant(domain, ScalarType((0, 0, -rho*g)))
a = ufl.inner(sigma(u), epsilon(v)) * ufl.dx
L = ufl.dot(f, v) * ufl.dx + ufl.dot(T, v) * ds
problem = fem.petsc.LinearProblem(a, L, bcs=[bc], petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
uh = problem.solve()
with io.XDMFFile(domain.comm, "deformation.xdmf", "w") as xdmf:
xdmf.write_mesh(domain)
uh.name = "Deformation"
xdmf.write_function(uh)
nn = ufl.FacetNormal(domain)
flux_1 = fem.form(ufl.dot(ufl.dot(sigma,nn),nn))
TT_1 = fem.assemble_scalar(flux_1 * ds_clamp(1)) #Force resultant clamped boundary
print(TT_1)
When I run this, I get the following error.
flux_1 = fem.form(ufl.dot(ufl.dot(sigma,nn),nn))
File "/usr/local/lib/python3.9/dist-packages/ufl/operators.py", line 172, in dot
a = as_ufl(a)
File "/usr/local/lib/python3.9/dist-packages/ufl/constantvalue.py", line 437, in as_ufl
raise UFLValueError("Invalid type conversion: %s can not be converted"
ufl.log.UFLValueError: Invalid type conversion: <function sigma at 0x7f0e4d6005e0> can not be converted to any UFL type.
Can anyone please help me figure out what is the correct way to do this?
Thank you