AttributeError: 'Cell' object has no attribute 'name'

Hello everyone, I have an issue on my code.

Here is my input:

import numpy as np
from dolfinx import mesh, fem, io, plot
from mpi4py import MPI
import matplotlib.pyplot as plt
from basix import ufl
from ufl import TrialFunction, TestFunction, dx, inner, grad, div

# Load the mesh
mesh_path = 'TTpath26x.xdmf'
with io.XDMFFile(MPI.COMM_WORLD, mesh_path, "r") as xdmf:
    domain_mesh = xdmf.read_mesh(name="Grid")

# Material properties definition
E = 2200  # Young's modulus for ABS in MPa
nu = 0.35  # Poisson's ratio for ABS
rho = 1040  # Density of ABS (kg/m^3)
g = 9.81  # Acceleration due to gravity (m/s^2)
mu = E / (2 * (1 + nu))
lambda_ = E * nu / ((1 + nu) * (1 - 2 * nu))

# Function spaces and test functions
element = ufl.element("Lagrange", domain_mesh.ufl_cell(), 1, shape=3)
V = fem.FunctionSpace(domain_mesh, element)
u = TrialFunction(V)
v = TestFunction(V)

# Define boundary conditions
def boundary_bottom(x):
    return np.isclose(x[2], 0.01, atol=0.21)  # z = 0.01 to -0.20

def boundary_top(x):
    return np.isclose(x[2], 2.02, atol=0.01)  # z = 2.02

bc_bottom = fem.dirichletbc(value=np.array([0, 0, 0]), dofs=V.locate_dofs_geometrical(boundary_bottom), V=V)
bc_top = fem.dirichletbc(value=np.array([0, 0, -1000]), dofs=V.locate_dofs_geometrical(boundary_top), V=V)

# Force definition
f = fem.Constant(domain_mesh, (0, 0, -rho * g * 0.01**2))  # Gravitational force

# Weak formulation
a = (inner(grad(u), grad(v)) + lambda_ * div(u) * div(v)) * dx
L = inner(f, v) * dx

# Solve the problem
problem = fem.petsc.LinearProblem(a, L, bcs=[bc_bottom, bc_top])
u_solution = problem.solve()

# Visualize the solution and save to a file
fig = plt.figure()
plotting_mesh = plot.create_vtk_mesh(V)
p = plot.plot(u_solution, mesh=plotting_mesh)
plt.colorbar(p)
plt.savefig('simulation_result.png')  # File path
plt.close()

output of the code:
Traceback (most recent call last): File "/home/mtm/FEA_yeni2.py", line 22, in <module> element = ufl.element("Lagrange", domain_mesh.ufl_cell(), 1, shape=3) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/mtm/miniconda3/envs/fenicsx-env/lib/python3.12/site-packages/basix/ufl.py", line 1988, in element family = _basix.finite_element.string_to_family(family, cell.name) AttributeError: 'Cell' object has no attribute 'name'

I am new at FEA and Fenics. I couldn’t solve this error. What should I do to fix it?

This should be
element = ufl.element(“Lagrange”, domain_mesh.topology.cell_name(), 1, shape=3)

For further questions please encapsulate code with 3x` encapsulation

1 Like

Thank you very much for your advice, it fixed the error. But now I am trying to fix this:
input: element = ufl.element("Lagrange", domain_mesh.topology.cell_name(), 1, shape=(3,)) V = fem.FunctionSpace(domain_mesh, element)

output:
‘’‘Traceback (most recent call last):
File “/home/mtm/FEA_yeni2.py”, line 23, in
V = fem.FunctionSpace(domain_mesh, element)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: FunctionSpace.init() missing 1 required positional argument: ‘cppV’
‘’’

FunctionSpace has been replaced by functionspace

1 Like

Thaks to you and documentation I fixed all the errors. But I think there is a logic problem on my code. I want to fix my mesh in the z-axis between approximately 0.2 and -0.2 under gravity, and measure its deformation under a downward force of 1000 N from above (approximately from z = 3.02). However, when I look at the results in Paraview, I get the following outcome. No matter how much I increase the force, it does not deform. Did I make a mistake in the boundary conditions?
My final code:

import numpy as np
from dolfinx import mesh, fem, io
import dolfinx
from mpi4py import MPI
import matplotlib.pyplot as plt
from ufl import TrialFunction, TestFunction, dx, inner, grad, div
from basix import ufl
import dolfinx.fem.petsc
# Load the mesh
mesh_path = 'Mesh_path26x.xdmf'
with io.XDMFFile(MPI.COMM_WORLD, mesh_path, "r") as xdmf:
    domain_mesh = xdmf.read_mesh(name="Grid")

# Material properties definition
E = 2200  # Young's modulus for ABS in MPa
nu = 0.35  # Poisson's ratio for ABS
rho = 1040  # Density of ABS (kg/m^3)
g = 9.81  # Acceleration due to gravity (m/s^2)
mu = E / (2 * (1 + nu))
lambda_ = E * nu / ((1 + nu) * (1 - 2 * nu))

# Function spaces and test functions
element = ufl.element('Lagrange', domain_mesh.topology.cell_name(), 1, shape=(3,))
V = fem.functionspace(domain_mesh, element)
u = TrialFunction(V)
v = TestFunction(V)

# Define boundary conditions
def boundary_bottom(x):
    return np.isclose(x[2], 0.01, atol=0.21)  # z = 0.01 to -0.20

def boundary_top(x):
    return np.isclose(x[2], 2.01, atol=0.01)  # z = 2.02

bc_bottom = fem.dirichletbc(np.array([0.0, 0.0, 0.0]), fem.locate_dofs_geometrical(V, boundary_bottom), V)
                
bc_top = fem.dirichletbc(np.array([0.0, 0.0, -10000000000.0]), fem.locate_dofs_geometrical(V, boundary_top), V)

# Force definition
f = fem.Constant(domain_mesh, (0, 0, -rho * g * 0.01**2))  # Gravitational force

# Weak formulation
a = (inner(grad(u), grad(v)) + lambda_ * div(u) * div(v)) * dx
L = inner(f, v) * dx

# Solve the problem
problem = fem.petsc.LinearProblem(a, L, bcs=[bc_bottom, bc_top])

u_solution = problem.solve()

# Save the solution to an XDMF file for visualization in ParaView
solution_path = 'solution.xdmf'
with io.XDMFFile(MPI.COMM_WORLD, solution_path, "w") as xdmf:
    xdmf.write_mesh(domain_mesh)
    xdmf.write_function(u_solution)

Paraview output: