TypeError: FiniteElement.__init__() missing 3 required positional arguments: 'reference_value_shape', 'pullback', and 'sobolev_space'

DOLFINx version: 0.8.0
Traceback (most recent call last):
  File "/home/prusso/dolfinx/scratchpad13.py", line 33, in <module>
    element = FiniteElement("Lagrange", ufl.hexahedron, 1)
TypeError: FiniteElement.__init__() missing 3 required positional arguments: 'reference_value_shape', 'pullback', and 'sobolev_space'

import dolfinx
import dolfinx.io
import meshio
import ufl
from mpi4py import MPI
import numpy as np
import basix 
from petsc4py import PETSc
from ufl.finiteelement import FiniteElement
import dolfinx.cpp.mesh
print("DOLFINx version:", dolfinx.__version__)

# Convert the VTK mesh to XDMF format
meshio_mesh = meshio.read("hexahedron_mesh.vtk")
meshio.write("hexahedron_mesh.xdmf", meshio_mesh)

# Read the XDMF file using DOLFINx
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, "hexahedron_mesh.xdmf", "r") as xdmf:
    mesh = xdmf.read_mesh(name="Grid")

# Define the material properties
κ = 0.83  # Thermal conductivity
ρ = 7850  # Density of the material (kg/m^3)
c_p = 500  # Specific heat capacity (J/(kg*K))
f = 5000  # Heat source term

# Define the function space
element_family = basix.ElementFamily.P
cell_type = basix.CellType.hexahedron
element_degree = 1

# Define the element
element = FiniteElement("Lagrange", ufl.hexahedron, 1)
V = dolfinx.fem.FunctionSpace(mesh, element)

# Define the boundary condition function
u_D = dolfinx.Function(V)
u_D.interpolate(lambda x: np.zeros((x.shape[1], 3)))  # Adjust dimensions based on problem

# Define the degrees of freedom to which the condition applies
# You need to define boundary_facets based on your problem
boundary_facets = ...
dofs = dolfinx.fem.locate_dofs_topological(V, mesh.topology.dim - 1, boundary_facets)

# Define the Dirichlet boundary condition
dirichlet_bc = dolfinx.fem.DirichletBC(u_D, dofs)

# Define the test and trial functions
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)

# Define the time step and the initial condition
dt = 0.01  # Time step
u_n = dolfinx.Function(V)  # Initial condition (zero by default)

# Define the variational problem
F = (ρ * c_p * u / dt) * v * ufl.dx - (ρ * c_p * u_n / dt) * v * ufl.dx + κ * ufl.inner(ufl.grad(u), ufl.grad(v)) * ufl.dx - f * v * ufl.dx

# Define the bilinear and linear forms
a = ufl.lhs(F)
L = ufl.rhs(F)

# Assemble the system matrix
A = dolfinx.fem.assemble_matrix(a)

# Print the values of the system matrix

# Create an empty vector for the solution
u = dolfinx.Function(V)

# Time-stepping
t = 0
T = 1  # Final time
while t < T:
    # Assemble the right-hand side
    b = dolfinx.fem.assemble_vector(L)

    # Apply boundary conditions
    dolfinx.fem.apply_lifting(b, [a], [[dirichlet_bc]])
    b.ghostUpdate(addv=PETSc.InsertMode.ADD, mode=PETSc.ScatterMode.REVERSE)
    dolfinx.fem.set_bc(b, [dirichlet_bc])

    # Solve the system
    dolfinx.la.solve(A, u.vector, b)

    # Update the previous solution
    with u.vector.localForm() as loc, u_n.vector.localForm() as loc_n:

    # Advance the time step
    t += dt


Seems to be some difficulties here:

*element = FiniteElement("Lagrange", ufl.hexahedron, 1)*

Not quite sure yet how to get that resolved it.

Just for informational pruposes the VTK file is constructed so far like this:

import numpy as np
import meshio

# Define the dimensions
x_dim, y_dim, z_dim = 8, 8, 8

# Define the number of segments
segments = 15

# Create a grid of points within the dimensions
x = np.linspace(0, x_dim, segments+1)
y = np.linspace(0, y_dim, segments+1)
z = np.linspace(0, z_dim, segments+1)

# Create a 3D meshgrid
X, Y, Z = np.meshgrid(x, y, z)

# Stack the points into an array
points = np.stack((X.flatten(), Y.flatten(), Z.flatten()), axis=-1)

# Define the connectivity for each hexahedral cell
indices = np.arange((segments+1)**3).reshape((segments+1, segments+1, segments+1))
cells = []
for z in range(segments):
    for y in range(segments):
        for x in range(segments):
            # Indices of the eight corners of the hexahedron
            corners = [indices[x, y, z], indices[x+1, y, z], indices[x+1, y+1, z], indices[x, y+1, z],
                       indices[x, y, z+1], indices[x+1, y, z+1], indices[x+1, y+1, z+1], indices[x, y+1, z+1]]

# Create the mesh
mesh = meshio.Mesh(points, {'hexahedron': np.array(cells)})

# Save the mesh to a file
meshio.write("hexahedron_mesh.vtk", mesh)

boundary_facets = …, is a placeholder right now. I guess that I will need to collect those. My aim is to build in the mass matrix M or just to build M and have it accounted for by the system some way.

There was an API change in dolfinx 0.8.0: finite elements must be now created through basix. See for instance dolfinx/python/demo/demo_lagrange_variants.py at v0.8.0 · FEniCS/dolfinx · GitHub

1 Like

So far my attempt to create the functions space V with the new basix element looks like this:

# Define the function space
element_family = basix.ElementFamily.P
cell_type = basix.CellType.hexahedron
element_degree = 1
variant = basix.LagrangeVariant.equispaced

# Define the element using Basix
ufl_element = basix.ufl.element(element_family, cell_type, element_degree, variant)

V = dolfinx.fem.FunctionSpace(mesh, ufl_element) <--

howver this code leads to a trace back:

DOLFINx version: 0.8.0
Traceback (most recent call last):
  File "/home/prusso/dolfinx/scratchpad14.py", line 36, in <module>
    V = dolfinx.fem.FunctionSpace(mesh, ufl_element) <--
TypeError: FunctionSpace.__init__() missing 1 required positional argument: 'cppV'

I don’t really know enough about cppV to really have much to go off of to fix that yet. Could I get some idea of where I went wrong here?

Someplace I maybe recall a discussion about converting the basix element to ufl however the exact details escape me at the moment. It seems that dolfinx.fem.FunctionSpace expects a type ufl element still not Basix from reading the documentation. I made an attempt to do so but couldn’t yet.

basix_element = basix.create_element(element_family, cell_type, element_degree, variant)

# Create a DOLFINx element using the Basix element
ufl_element = FiniteElement("Lagrange", mesh.ufl_cell(), degree=element_degree) <--

DOLFINx version: 0.8.0
Traceback (most recent call last):
  File "/home/prusso/dolfinx/scratchpad15.py", line 40, in <module>
    ufl_element = FiniteElement("Lagrange", mesh.ufl_cell(), degree=element_degree)
TypeError: FiniteElement.__init__() missing 3 required positional arguments: 'reference_value_shape', 'pullback', and 'sobolev_space'

So must be some problem in conversion there that is all I know…

Consider the following MWE:

from mpi4py import MPI
import basix.ufl
import dolfinx

# Define the function space
element_family = basix.ElementFamily.P
element_degree = 1
variant = basix.LagrangeVariant.equispaced

mesh = dolfinx.mesh.create_unit_cube(MPI.COMM_WORLD, 
                                     4, 4, 4, cell_type=dolfinx.mesh.CellType.hexahedron)
ufl_element = basix.ufl.element(element_family, mesh.topology.cell_name(), element_degree, variant)

V = dolfinx.fem.functionspace(mesh, ufl_element)

Note that the most use cases of finite elements are covered in:

1 Like