Equivalent of ufl.TensorElement in v0.9?

I am translating my v0.6 to v0.9 fenicsx code and most of the things are easy enough to figure out, but I got stuck on TensorElement from ufl, which do not exists anymore.

So in my code I had

from ufl import TensorElement

# Dokken's solution below
tensor_el = TensorElement("DG", the_mesh.mesh.ufl_cell(), 0, shape=(2, 2))

And now I don’t know how to perform the very same thing with dolfinx 0.9 (as installed through conda).

I have searched with google, here with the search option, in the source code of basix.ufl and in some other documentation. But I haven’t found out how to deal with this case. Any help is greatly appreciated.

Edit: Hmm it looks like I can probably use “element” from basix.ufl, the tensor element is considered through the shape argument.

My line of code:

tensor_el = element(family="DG", cell=the_mesh.mesh.ufl_cell(), degree=0, shape=(2, 2))

yields the error:

    tensor_el = element(family="DG", cell=the_mesh.mesh.ufl_cell(), degree=0, shape=(2, 2))
  File ".conda/envs/fenicsx-env/lib/python3.13/site-packages/basix/ufl.py", line 2038, in element
    family = _basix.finite_element.string_to_family(family, cell.name)
                                                            ^^^^^^^^^
AttributeError: 'Cell' object has no attribute 'name'

Apparently “element” expects either a str or _basix.CellType object. In my case I am giving it the object returned by the first argument of

mesh, cell_markers, facet_markers = gmshio.read_from_msh(mesh_file, MPI.COMM_WORLD, gdim=2) which is a “Mesh” object, coming from dolfinx’s mesh.py. I see that this Mesh object has an attribute called “name”.

Not sure how to fix my problem.

Edit:

tensor_el = element(family="DG", cell=the_mesh.mesh.topology.cell_name(), degree=0, shape=(2, 2))

T = functionspace(the_mesh.mesh, tensor_el)

yields:

Traceback (most recent call last):
  File "/home/user/Documents/fenicsx/v0dot9.py", line 2, in <module>
    from dolfinx import log
  File "/home/user/.conda/envs/fenicsx-env/lib/python3.13/site-packages/dolfinx/__init__.py", line 32, in <module>
    from dolfinx import fem, geometry, graph, io, jit, la, log, mesh, nls, plot, utils
  File "/home/user/.conda/envs/fenicsx-env/lib/python3.13/site-packages/dolfinx/fem/__init__.py", line 18, in <module>
    from dolfinx.fem.assemble import (
    ...<7 lines>...
    )
  File "/home/user/.conda/envs/fenicsx-env/lib/python3.13/site-packages/dolfinx/fem/assemble.py", line 23, in <module>
    from dolfinx.fem.forms import Form
  File "/home/user/.conda/envs/fenicsx-env/lib/python3.13/site-packages/dolfinx/fem/forms.py", line 23, in <module>
    from dolfinx.fem.function import FunctionSpace
ImportError: cannot import name 'FunctionSpace' from 'dolfinx.fem.function' (/home/user/.conda/envs/fenicsx-env/lib/python3.13/site-packages/dolfinx/fem/function.py)

Uh? It looks like forms.py tries to import FunctionSpace, which does not exist anymore (has been supplanted by functionspace). Is this a FEniCSx bug? How do people use it, then? I certainly am not the first one to fall over this situation, right?

For vector, tensor and other elements, I would suggest having a look at:

which explains how to create various element types.

For your error, please note that dolfinx.fem.function.FunctionSpace still exists:

but it is not the natural way to construct a finite element (as stated in: dolfinx/python/dolfinx/fem/function.py at v0.9.0.post1 · FEniCS/dolfinx · GitHub)

Could you make a minimal reproducible example that creates your error, as this seems rather strange to me?
Also please yield the ouput of conda env export.

1 Like

I am also surprised, the code used to get stuck further down, but now I get this error basically on line 2. So a MWE would simply be:

import numpy as np
from dolfinx import fem, log
from dolfinx.fem import (Constant, Function, functionspace, assemble_scalar,
                         dirichletbc, form, locate_dofs_geometrical,
                         locate_dofs_topological, Expression, create_matrix, assemble_matrix)

Here’s the output of the command you asked for:

(fenicsx-env) desktop% conda env export
Error while loading conda entry point: conda-content-trust (OpenSSL 3.0's legacy provider failed to load. This is a fatal error by default, but cryptography supports running without legacy algorithms by setting the environment variable CRYPTOGRAPHY_OPENSSL_NO_LEGACY. If you did not expect this error, you have likely made a mistake with your OpenSSL configuration.)
name: fenicsx-env
channels:
  - conda-forge
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
dependencies:
  - _openmp_mutex=4.5=3_kmp_llvm
  - _x86_64-microarch-level=3=2_skylake
  - aiohappyeyeballs=2.6.1=pyhd8ed1ab_0
  - aiohttp=3.11.18=py313h8060acc_0
  - aiosignal=1.3.2=pyhd8ed1ab_0
  - alsa-lib=1.2.14=hb9d3cd8_0
  - attr=2.5.1=h166bdaf_1
  - attrs=25.3.0=pyh71513ae_0
  - binutils_impl_linux-64=2.43=h4bf12b8_4
  - binutils_linux-64=2.43=h4852527_4
  - blosc=1.21.6=he440d0b_1
  - brotli=1.1.0=hb9d3cd8_2
  - brotli-bin=1.1.0=hb9d3cd8_2
  - brotli-python=1.1.0=py313h46c70d0_2
  - bzip2=1.0.8=h4bc722e_7
  - c-ares=1.34.5=hb9d3cd8_0
  - c-blosc2=2.17.1=h3122c55_0
  - ca-certificates=2025.4.26=hbd8a1cb_0
  - cairo=1.18.4=h3394656_0
  - certifi=2025.4.26=pyhd8ed1ab_0
  - cffi=1.17.1=py313hfab6e84_0
  - charset-normalizer=3.4.2=pyhd8ed1ab_0
  - contourpy=1.3.2=py313h33d0bda_0
  - cycler=0.12.1=pyhd8ed1ab_1
  - cyrus-sasl=2.1.27=h54b06d7_7
  - dbus=1.13.6=h5008d03_3
  - double-conversion=3.3.1=h5888daf_0
  - expat=2.7.0=h5888daf_0
  - fenics-basix=0.9.0=py313h867a91d_2
  - fenics-basix-nanobind-abi=0.2.1.13=h6c05e69_2
  - fenics-dolfinx=0.9.0=py313hd3aa609_114
  - fenics-ffcx=0.9.0=pyh2e48890_0
  - fenics-libbasix=0.9.0=h7cb7ce6_2
  - fenics-libdolfinx=0.9.0=py313he4cdb1c_114
  - fenics-ufcx=0.9.0=hb7f7608_0
  - fenics-ufl=2024.2.0=pyhd8ed1ab_1
  - fftw=3.3.10=mpi_mpich_hbcf76dd_10
  - fmt=11.1.4=h07f6e7f_1
  - font-ttf-dejavu-sans-mono=2.37=hab24e00_0
  - font-ttf-inconsolata=3.000=h77eed37_0
  - font-ttf-source-code-pro=2.038=h77eed37_0
  - font-ttf-ubuntu=0.83=h77eed37_3
  - fontconfig=2.15.0=h7e30c49_1
  - fonts-conda-ecosystem=1=0
  - fonts-conda-forge=1=0
  - fonttools=4.57.0=py313h8060acc_0
  - freetype=2.13.3=ha770c72_1
  - frozenlist=1.6.0=py313h61b7b33_0
  - gcc_impl_linux-64=13.3.0=h1e990d8_2
  - gcc_linux-64=13.3.0=hc28eda2_10
  - gl2ps=1.4.2=hae5d5c5_1
  - graphite2=1.3.13=h59595ed_1003
  - h2=4.2.0=pyhd8ed1ab_0
  - harfbuzz=11.1.0=h3beb420_0
  - hdf4=4.2.15=h2a13503_7
  - hdf5=1.14.6=mpi_mpich_h7f58efa_1
  - hpack=4.1.0=pyhd8ed1ab_0
  - hyperframe=6.1.0=pyhd8ed1ab_0
  - hypre=2.32.0=mpi_mpich_h2e71eac_1
  - icu=75.1=he02047a_0
  - idna=3.10=pyhd8ed1ab_1
  - jsoncpp=1.9.6=hf42df4d_1
  - kahip=3.19=h7d9e1f9_0
  - kernel-headers_linux-64=3.10.0=he073ed8_18
  - keyutils=1.6.1=h166bdaf_0
  - kiwisolver=1.4.7=py313h33d0bda_0
  - krb5=1.21.3=h659f571_0
  - lcms2=2.17=h717163a_0
  - ld_impl_linux-64=2.43=h712a8e2_4
  - lerc=4.0.0=h0aef613_1
  - libadios2=2.10.2=mpi_mpich_h50647c6_6
  - libaec=1.1.3=h59595ed_0
  - libamd=3.3.3=haaf9dc3_7100102
  - libblas=3.9.0=31_h59b9bed_openblas
  - libboost=1.86.0=h6c02f8c_3
  - libboost-devel=1.86.0=h1a2810e_3
  - libboost-headers=1.86.0=ha770c72_3
  - libbrotlicommon=1.1.0=hb9d3cd8_2
  - libbrotlidec=1.1.0=hb9d3cd8_2
  - libbrotlienc=1.1.0=hb9d3cd8_2
  - libbtf=2.3.2=h32481e8_7100102
  - libcamd=3.3.3=h32481e8_7100102
  - libcap=2.75=h39aace5_0
  - libcblas=3.9.0=31_he106b2a_openblas
  - libccolamd=3.3.4=h32481e8_7100102
  - libcholmod=5.3.1=h59ddab4_7100102
  - libclang-cpp20.1=20.1.4=default_h1df26ce_0
  - libclang13=20.1.4=default_he06ed0a_0
  - libcolamd=3.3.4=h32481e8_7100102
  - libcups=2.3.3=h4637d8d_4
  - libcurl=8.13.0=h332b0f4_0
  - libdeflate=1.23=h86f0d12_0
  - libdrm=2.4.124=hb9d3cd8_0
  - libedit=3.1.20250104=pl5321h7949ede_0
  - libegl=1.7.0=ha4b6fd6_2
  - libev=4.33=hd590300_2
  - libexpat=2.7.0=h5888daf_0
  - libfabric=2.1.0=ha770c72_1
  - libfabric1=2.1.0=hf45584d_1
  - libffi=3.4.6=h2dba641_1
  - libfreetype=2.13.3=ha770c72_1
  - libfreetype6=2.13.3=h48d6fc4_1
  - libgcc=15.1.0=h767d61c_2
  - libgcc-devel_linux-64=13.3.0=hc03c837_102
  - libgcc-ng=15.1.0=h69a702a_2
  - libgcrypt-lib=1.11.0=hb9d3cd8_2
  - libgfortran=15.1.0=h69a702a_2
  - libgfortran-ng=15.1.0=h69a702a_2
  - libgfortran5=15.1.0=hcea5267_2
  - libgl=1.7.0=ha4b6fd6_2
  - libglib=2.84.1=h2ff4ddf_0
  - libglvnd=1.7.0=ha4b6fd6_2
  - libglx=1.7.0=ha4b6fd6_2
  - libgomp=15.1.0=h767d61c_2
  - libgpg-error=1.55=h3f2d84a_0
  - libhwloc=2.11.2=default_h0d58e46_1001
  - libiconv=1.18=h4ce23a2_1
  - libjpeg-turbo=3.1.0=hb9d3cd8_0
  - libklu=2.3.5=hf24d653_7100102
  - liblapack=3.9.0=31_h7ac8fdf_openblas
  - libllvm20=20.1.4=he9d0ab4_0
  - liblzma=5.8.1=hb9d3cd8_1
  - libmpdec=4.0.0=h4bc722e_0
  - libnetcdf=4.9.2=nompi_h0134ee8_117
  - libnghttp2=1.64.0=h161d5f1_0
  - libnl=3.11.0=hb9d3cd8_0
  - libntlm=1.8=hb9d3cd8_0
  - libogg=1.3.5=hd0c01bc_1
  - libopenblas=0.3.29=openmp_hd680484_0
  - libpciaccess=0.18=hd590300_0
  - libpng=1.6.47=h943b412_0
  - libpq=17.5=h27ae623_0
  - libptscotch=7.0.6=h4c3caac_1
  - libsanitizer=13.3.0=he8ea267_2
  - libscotch=7.0.6=hea33c07_1
  - libsodium=1.0.20=h4ab18f5_0
  - libspqr=4.3.4=h852d39f_7100102
  - libsqlite=3.49.2=hee588c1_0
  - libssh2=1.11.1=hcf80075_0
  - libstdcxx=15.1.0=h8f9b012_2
  - libstdcxx-ng=15.1.0=h4852527_2
  - libsuitesparseconfig=7.10.1=h92d6892_7100102
  - libsystemd0=257.4=h4e0b6ca_1
  - libtheora=1.1.1=h4ab18f5_1006
  - libtiff=4.7.0=hd9ff511_4
  - libudev1=257.4=hbe16f8c_1
  - libumfpack=6.3.5=heb53515_7100102
  - libuuid=2.38.1=h0b41bf4_0
  - libvorbis=1.3.7=h9c3ff4c_0
  - libwebp-base=1.5.0=h851e524_0
  - libxcb=1.17.0=h8a09558_0
  - libxkbcommon=1.9.2=h65c71a3_0
  - libxml2=2.13.8=h4bc477f_0
  - libzip=1.11.2=h6991a6a_0
  - libzlib=1.3.1=hb9d3cd8_2
  - llvm-openmp=20.1.4=h024ca30_0
  - loguru=0.7.3=pyh707e725_0
  - lz4-c=1.10.0=h5888daf_1
  - matplotlib-base=3.10.3=py313h129903b_0
  - metis=5.1.0=hd0bcaf9_1007
  - mpi=1.0.1=mpich
  - mpi4py=4.0.3=py313h42d17bb_101
  - mpich=4.3.0=h1a8bee6_100
  - msgpack-python=1.1.0=py313h33d0bda_0
  - multidict=6.4.3=py313h8060acc_0
  - mumps-include=5.7.3=h23d43cc_10
  - mumps-mpi=5.7.3=h8c07e11_10
  - munkres=1.1.4=pyh9f0ad1d_0
  - ncurses=6.5=h2d0b736_3
  - nlohmann_json=3.12.0=h3f2d84a_0
  - numpy=2.2.5=py313h17eae1a_0
  - openjpeg=2.5.3=h5fbd93e_0
  - openldap=2.6.9=he970967_0
  - openssl=3.5.0=h7b32b05_1
  - packaging=25.0=pyh29332c3_1
  - parmetis=4.0.3=hc7bef4e_1007
  - pcre2=10.44=hc749103_2
  - petsc=3.23.2=real_hf9cfe27_0
  - petsc4py=3.23.2=np2py313hde6fa77_0
  - pillow=11.2.1=py313h8db990d_0
  - pip=25.1.1=pyh145f28c_0
  - pixman=0.46.0=h29eaf8c_0
  - pkg-config=0.29.2=h4bc722e_1009
  - platformdirs=4.3.8=pyhe01879c_0
  - pooch=1.8.2=pyhd8ed1ab_1
  - proj=9.6.0=h0054346_1
  - propcache=0.3.1=py313h8060acc_0
  - pthread-stubs=0.4=hb9d3cd8_1002
  - pugixml=1.15=h3f63f65_0
  - pycparser=2.22=pyh29332c3_1
  - pyparsing=3.2.3=pyhd8ed1ab_1
  - pysocks=1.7.1=pyha55dd90_7
  - python=3.13.3=hf636f53_101_cp313
  - python-dateutil=2.9.0.post0=pyhff2d567_1
  - python_abi=3.13=7_cp313
  - pyvista=0.45.1=pyhd8ed1ab_0
  - qhull=2020.2=h434a139_5
  - qt6-main=6.9.0=h8d00660_2
  - rdma-core=57.0=h5888daf_0
  - readline=8.2=h8c095d6_2
  - requests=2.32.3=pyhd8ed1ab_1
  - scalapack=2.2.0=h7e29ba8_4
  - scooby=0.10.1=pyhd8ed1ab_0
  - setuptools=80.1.0=pyhff2d567_0
  - six=1.17.0=pyhd8ed1ab_0
  - slepc=3.23.1=real_h7f1eba4_0
  - slepc4py=3.23.1=np2py313h7deae8f_0
  - snappy=1.2.1=h8bd8927_1
  - spdlog=1.15.3=h10b92b3_0
  - sqlite=3.49.2=h9eae976_0
  - superlu=7.0.1=h8f6e6c4_0
  - superlu_dist=9.1.0=h0804ebd_0
  - sysroot_linux-64=2.17=h0157908_18
  - tbb=2022.1.0=h4ce085d_0
  - tk=8.6.13=noxft_h4845f30_101
  - typing-extensions=4.13.2=h0e9735f_0
  - typing_extensions=4.13.2=pyh29332c3_0
  - tzdata=2025b=h78e105d_0
  - ucx=1.18.1=h1369271_0
  - urllib3=2.4.0=pyhd8ed1ab_0
  - utfcpp=4.0.6=h005c6e1_0
  - vtk-base=9.4.2=py313h6b6eb50_0
  - wayland=1.23.1=h3e06ad9_1
  - wslink=2.3.3=pyhd8ed1ab_0
  - xcb-util=0.4.1=hb711507_2
  - xcb-util-cursor=0.1.5=hb9d3cd8_0
  - xcb-util-image=0.4.0=hb711507_2
  - xcb-util-keysyms=0.4.1=hb711507_0
  - xcb-util-renderutil=0.3.10=hb711507_0
  - xcb-util-wm=0.4.2=hb711507_0
  - xkeyboard-config=2.44=hb9d3cd8_0
  - xorg-libice=1.1.2=hb9d3cd8_0
  - xorg-libsm=1.2.6=he73a12e_0
  - xorg-libx11=1.8.12=h4f16b4b_0
  - xorg-libxau=1.0.12=hb9d3cd8_0
  - xorg-libxcomposite=0.4.6=hb9d3cd8_2
  - xorg-libxcursor=1.2.3=hb9d3cd8_0
  - xorg-libxdamage=1.1.6=hb9d3cd8_0
  - xorg-libxdmcp=1.1.5=hb9d3cd8_0
  - xorg-libxext=1.3.6=hb9d3cd8_0
  - xorg-libxfixes=6.0.1=hb9d3cd8_0
  - xorg-libxi=1.8.2=hb9d3cd8_0
  - xorg-libxrandr=1.5.4=hb9d3cd8_0
  - xorg-libxrender=0.9.12=hb9d3cd8_0
  - xorg-libxtst=1.2.5=hb9d3cd8_3
  - xorg-libxxf86vm=1.1.6=hb9d3cd8_0
  - yaml=0.2.5=h7f98852_2
  - yarl=1.20.0=py313h8060acc_0
  - zeromq=4.3.5=h3b0a872_7
  - zfp=1.0.1=h5888daf_2
  - zlib=1.3.1=hb9d3cd8_2
  - zlib-ng=2.2.4=h7955e40_0
  - zstandard=0.23.0=py313h536fd9c_2
  - zstd=1.5.7=hb8e6e7a_2
prefix: /home/isaac/.conda/envs/fenicsx-env

Edit: My line 649 in function.py has indeed lower cases, unlike the code you posted… Maybe I accidentally renamed it? Very strange, anyway, I put back the capital letters, but now I get another error, for the same line:


Traceback (most recent call last):
  File "/home/user/Documents/fenicsx/v0dot9.py", line 2, in <module>
    from dolfinx import fem, log
  File "/home/user/.conda/envs/fenicsx-env/lib/python3.13/site-packages/dolfinx/__init__.py", line 32, in <module>
    from dolfinx import fem, geometry, graph, io, jit, la, log, mesh, nls, plot, utils
  File "/home/user/.conda/envs/fenicsx-env/lib/python3.13/site-packages/dolfinx/fem/__init__.py", line 73, in <module>
    V_to: FunctionSpace,
          ^^^^^^^^^^^^^
NameError: name 'FunctionSpace' is not defined. Did you mean: 'functionspace'?

And I just checked, that this time the code is as is.

Could you try with a new conda environment? As there has definitely been some changes within your source files if Line 649 is lowercase.

1 Like

I have removed the environment, recreated it with the same name, reinstall dolfinx in it. I get exactly the same error/traceback as described in my previous post.

Wow. I actually not only had to remove the conda env, but also all the conda packages (which are apparently not removed by default) and all the tarballs (whole thing was over 2.5 GB). Only then a new redownload of Fenicsx has the correct, untouched code. I do not get the above error/traceback anymore.

Edit: Thanks a lot @dokken. I had some more errors throughout my code, but they’re now all fixed, my code is now fully updated to v0.9 and I can finally use Fenicsx using VScodium!