[BUG]: AttributeError: module 'dolfinx.mesh' has no attribute 'Triangle'
Summarize the issue
AttributeError: module 'dolfinx.mesh' has no attribute 'Triangle'
How to reproduce the bug
AttributeError: module 'dolfinx.mesh' has no attribute 'Triangle'
Minimal Example (Python)
from dolfinx import mesh, fem
import ufl
import numpy as np
from mpi4py import MPI
import matplotlib.pyplot as plt
# Create the mesh and function space
nel = 32 # Number of elements along each axis
xmin, xmax = 0, 1 # Domain range
ymin, ymax = 0, 1
p = 2 # Polynomial degree
# Step 1: Create a 2D mesh and function space
domain = mesh.create_rectangle(MPI.COMM_WORLD, [np.array([xmin, ymin]), np.array([xmax, ymax])], [nel, nel], cell_type=mesh.Triangle)
V = fem.functionspace(domain, ("CG", p))
# Step 2: Define the expressions for f and g (right-hand side and Neumann boundary conditions)
x, y = ufl.SpatialCoordinate(domain)
f_expr = 10 * ufl.exp(-(ufl.pow(x - 0.5, 2) + ufl.pow(y - 0.5, 2)) / 0.02)
g_expr = ufl.sin(5 * x)
f = fem.Function(V)
f.interpolate(f_expr) # Interpolating the function
g = fem.Function(V)
g.interpolate(g_expr) # Interpolating the function
# Step 3: Define the Dirichlet boundary condition (u = 0 on the boundary)
u0 = fem.Function(V)
u0.x[:] = 0.0 # Initialize solution as zero
def dirichlet_boundary(x):
return np.full(x.shape, x[0] < 1E-14 or x[0] > 1 - 1E-14)
bc = fem.dirichletbc(u0, fem.locate_dofs_geometrical(V, dirichlet_boundary))
# Step 4: Define the variational form (a and L)
u = fem.Function(V)
v = ufl.TestFunction(V)
a = ufl.inner(ufl.grad(u), ufl.grad(v)) * ufl.dx
L = f * v * ufl.dx + g * v * ufl.ds
# Step 5: Solve the variational problem
problem = fem.petsc.LinearProblem(a, L, bcs=[bc])
u = problem.solve()
# Step 6: Plot the result
import matplotlib.pyplot as plt
from petsc4py import PETSc
# Extract the solution for plotting
u_ = u.x.array
# Plotting
plt.figure(figsize=(6, 5))
plt.tricontourf(domain.geometry[:, 0], domain.geometry[:, 1], domain.cells, u_)
plt.colorbar()
plt.title("Solution of Poisson equation")
plt.show()
# Optionally, save to VTK
from petsc4py import PETSc
file = fem.XDMFFile(MPI.COMM_WORLD, "poisson_solution.xdmf", "w")
file.write_mesh(domain)
file.write_function(u)
file.close()
Output (Python)
Traceback (most recent call last):
File "/home/ovo/myapp2024/myFEniCS/入门.py", line 25, in <module>
bc = fem.dirichletbc(u0, fem.locate_dofs_geometrical(V, dirichlet_boundary))
~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ovo/.conda/envs/fenicsx-env/lib/python3.13/site-packages/dolfinx/fem/bcs.py", line 51, in locate_dofs_geometrical
return _cpp.fem.locate_dofs_geometrical(V._cpp_object, marker) # type: ignore
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
TypeError: dirichlet_boundary() missing 1 required positional argument: 'on_boundary'
(fenicsx-env) ovo@OVO:~/myapp2024/myFEniCS$ python 入门.py
Traceback (most recent call last):
File "/home/ovo/myapp2024/myFEniCS/入门.py", line 19, in <module>
f = fem.Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)", degree=2)
TypeError: Expression.__init__() got an unexpected keyword argument 'degree'
(fenicsx-env) ovo@OVO:~/myapp2024/myFEniCS$ python 入门.py
Traceback (most recent call last):
File "/home/ovo/myapp2024/myFEniCS/入门.py", line 14, in <module>
domain = mesh.create_rectangle(MPI.COMM_WORLD, [np.array([xmin, ymin]), np.array([xmax, ymax])], [nel, nel], cell_type=mesh.Triangle)
^^^^^^^^^^^^^
AttributeError: module 'dolfinx.mesh' has no attribute 'Triangle'
(fenicsx-env) ovo@OVO:~/myapp2024/myFEniCS$
Version
main branch
DOLFINx git commit
No response
Installation
No response
Additional information
No response
Traceback (most recent call last):
File "/home/ovo/myapp2024/myFEniCS/入门.py", line 48, in
This is not a bug, it's just your programming error. mesh.Triangle (which never existed in dolfinx) should have been mesh.CellType.triangle.
Please refrain from opening future reports of this type here. Post instead a message on the discourse fourm.