Hi Jorgen,
Thank you for the prompt reply; the reason I am making external_mesh is to have a minimal running code. In the actual code the nodes and connectivity come from a mesh from another library.
I changed the code to not use mesh as a variable name (below). Now the error I get is:
Traceback (most recent call last):
File ~/.local/lib/python3.10/site-packages/spyder_kernels/customize/utils.py:209 in exec_encapsulate_locals
exec_fun(compile(code_ast, filename, “exec”), globals)
File ~/robotDepowdering/simple_fenics_example.py:41
part_mesh.topology.create_connectivity(mesh.topology.dim, 0)
AttributeError: module ‘dolfinx.mesh’ has no attribute ‘topology’
# Common libraries
import numpy as np
# Fenicsx FEA libraries
from dolfinx import mesh, fem, io, default_scalar_type
from dolfinx.fem.petsc import LinearProblem
from mpi4py import MPI
import ufl
import basix
mu = 1
rho = 1
gamma = 10
beta = 1.25
lambda_ = beta
g = gamma
# Make nodes and tetrahedral connectivity matrix
external_mesh = mesh.create_unit_cube(MPI.COMM_WORLD, nx=1, ny=1, nz=1, cell_type=mesh.CellType.tetrahedron)
points = external_mesh.geometry.x
cells = external_mesh.topology.connectivity(3,0).array.reshape((-1,4))
# Import nodes and connectivity matrix into domain and mesh objects
finiteElement = basix.ufl.element("Lagrange", "tetrahedron", 1, shape=(3, ))
domain = ufl.Mesh(finiteElement)
part_mesh = mesh.create_mesh(MPI.COMM_WORLD, cells, points, domain)
part_mesh.topology.create_connectivity(mesh.topology.dim, 0)
V = fem.functionspace(part_mesh, finiteElement)
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=default_scalar_type)
bc = fem.dirichletbc(u_D, fem.locate_dofs_topological(V, fdim, boundary_facets), V)
T = fem.Constant(domain, default_scalar_type((0, 0, 0)))
ds = ufl.Measure("ds", domain=domain)
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(len(u)) + 2 * mu * epsilon(u)
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
f = fem.Constant(domain, default_scalar_type((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 = 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)