FEniCS / dolfinx

Next generation FEniCS problem solving environment
https://fenicsproject.org
GNU Lesser General Public License v3.0
696 stars 172 forks source link

[BUG]: Implicit use of previously created function space #3288

Closed IgorBaratta closed 1 week ago

IgorBaratta commented 1 week ago

Summarize the issue

When creating a function space using a tensor product element and then attempting to create a function space with a tuple ("Lagrange", degree), the previously created space is used implicitly, leading to unexpected behaviour.

Minimal Example (Python)

from mpi4py import MPI
import numpy as np
import basix
import dolfinx

shape = "quadrilateral"
dtype = dolfinx.default_scalar_type
real_type = np.real(dtype(0.0)).dtype
comm = MPI.COMM_WORLD

# Create rectangle mesh
mesh = dolfinx.mesh.create_rectangle(
    comm, [[0.0, 0.0], [1.0, 1.0]], [10, 10], cell_type=dolfinx.mesh.CellType[shape], dtype=real_type
)

# Create tensor product element
degree = 2
basix_element = basix.create_tp_element(basix.ElementFamily.P, basix.CellType[shape], degree)
element = basix.ufl._BasixElement(basix_element)  # Wrap basix element in UFL element

# Create function space
V = dolfinx.fem.functionspace(mesh, element)
uD = dolfinx.fem.Function(V, dtype=dtype)
uD.interpolate(lambda x: 1 + x[0] ** 2 + 2 * x[1] ** 2)

# WRONG Visualization
V_out = dolfinx.fem.functionspace(mesh, ("Lagrange", degree))
u_out = dolfinx.fem.Function(V_out, dtype=dtype)
u_out.interpolate(uD)

with dolfinx.io.VTXWriter(mesh.comm, "U_wrong.bp", u_out) as file:
    file.write(0.0)

# Correct Visualization
basix_element = basix.create_element(basix.ElementFamily.P, basix.CellType[shape], degree)
element = basix.ufl._BasixElement(basix_element)  # Wrap basix element in UFL element
V_out = dolfinx.fem.functionspace(mesh, element)

u_out = dolfinx.fem.Function(V_out, dtype=dtype)
u_out.interpolate(uD)

with dolfinx.io.VTXWriter(mesh.comm, "U_right.bp", u_out) as file:
    file.write(0.0)

Version

main branch

IgorBaratta commented 1 week ago

Never mind, expected behavior with main.