Hello,
I am trying to compute the Von Mises stresses from the displacement field. This is my script, mainly taking inspiration from
import dolfinx, ufl, json
from petsc4py import PETSc
from mpi4py import MPI
import numpy as np
E = 1.0
nu = 0.3
mu = E / (2.0 * (1.0 + nu))
lmbda = E * nu / ((1.0 + nu) * (1.0 - 2.0 * nu))
def epsilon(v):
return ufl.sym(ufl.nabla_grad(v))
def sigma(v):
return 2.0 * mu * epsilon(v) + lmbda * ufl.tr(epsilon(v)) * ufl.Identity(len(v))
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10, dolfinx.mesh.CellType.quadrilateral)
V = dolfinx.fem.VectorFunctionSpace(mesh, ('CG', 1))
u, v = ufl.TrialFunction(V), ufl.TestFunction(V)
a = dolfinx.fem.form(ufl.inner(sigma(u), ufl.nabla_grad(v)) * ufl.dx)
left_edge = dolfinx.mesh.locate_entities_boundary(mesh, 0, marker=lambda x: np.isclose(x[0], 0))
right_edge = dolfinx.mesh.locate_entities_boundary(mesh, 1, marker=lambda x: np.isclose(x[0], 1))
bc = dolfinx.fem.dirichletbc(np.array((0, ) * mesh.topology.dim, dtype=np.double), left_edge, V)
A = dolfinx.fem.petsc.assemble_matrix(a, bcs=[bc])
A.assemble()
facet_indices, facet_markers = [], []
fdim = mesh.topology.dim - 1
facet_indices.append(right_edge)
facet_markers.append(np.full(len(right_edge), 1))
facet_indices, pos = np.unique(np.array(np.hstack(facet_indices), dtype=np.int32), return_index=True)
facet_markers = np.array(np.hstack(facet_markers)[pos], dtype=np.int32)
facet_tag = dolfinx.mesh.meshtags(mesh, fdim, facet_indices, facet_markers)
ds = ufl.ds(domain=mesh, subdomain_data=facet_tag)
t = ufl.as_vector([1.0, 0.0])
L = dolfinx.fem.form(ufl.inner(t, v) * ds(1))
b = dolfinx.fem.petsc.assemble_vector(L)
uh = dolfinx.fem.Function(V)
ksp = PETSc.KSP().create(A.comm)
ksp.setOperators(A)
ksp.setType("preonly")
ksp.getPC().setType("lu")
ksp.getPC().setFactorSolverType("mumps")
vec = b.copy()
ksp.solve(b, vec)
uh.vector.setArray(vec.array)
uh.name = "Displacement"
sigma_dev = sigma(uh) - (1/3) * ufl.tr(sigma(uh)) * ufl.Identity(len(uh))
sigma_vm = ufl.sqrt((3/2) * ufl.inner(sigma_dev, sigma_dev))
mises_space = dolfinx.fem.FunctionSpace(mesh, ('DG', 0))
stress_expr = dolfinx.fem.Expression(sigma_vm, mises_space.element.interpolation_points())
stress = dolfinx.fem.Function(mises_space)
stress.interpolate(stress_expr)
I am getting the following weird error:
AttributeError: 'ExpressionIR' object has no attribute 'has_runtime_qr'
when using dolfinx.fem.Expression
. Any help? Thanks in advance.