Flux computed and interpolated into deg 0 space leads to an error

Hi,
I have a code very similar to the one I posted here.
At some point, I compute a heat flux as:

# Heat flux.
Q = functionspace(mesh, ("DG", 0, (mesh.geometry.dim,)))
q = Function(Q)
flux_calculator = Expression(-κ * grad(temp), Q.element.interpolation_points())
q.interpolate(flux_calculator)

Where temp comes from:

# Define ME function space
deg = 1
el = FiniteElement("CG", mesh.ufl_cell(), deg)
mel = MixedElement([el, el])
ME = FunctionSpace(mesh, mel)

u, v = TestFunction(ME)
TempVolt = Function(ME)
temp, volt = split(TempVolt)

As far as I understand, the above code should work. However, I get an error when I save the heat flux field with:

with VTXWriter(MPI.COMM_WORLD, "results/heat_flux.bp", [out2], engine="BP4") as vtx:
    vtx.write(0.0)

The traceback is:

Traceback (most recent call last):
  File "/usr/local/dolfinx-real/lib/python3.10/dist-packages/dolfinx/io/utils.py", line 91, in __init__
    comm, filename, output._cpp_object, engine)  # type: ignore[union-attr]
AttributeError: 'list' object has no attribute '_cpp_object'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/shared/TE_simulation/BN_example.py", line 261, in <module>
    with VTXWriter(MPI.COMM_WORLD, "results/heat_flux.bp", [out2], engine="BP4") as vtx:
  File "/usr/local/dolfinx-real/lib/python3.10/dist-packages/dolfinx/io/utils.py", line 94, in __init__
    self._cpp_object = _vtxwriter(comm, filename, _extract_cpp_functions(
RuntimeError: VTK does not support cell-wise fields. See https://gitlab.kitware.com/vtk/vtk/-/issues/18458.

One way to bypass this problem is to pick a higher degree for the heat flux (which I don’t think make sense). It doesn’t make sense because the temp field is CG 1st degree, so linear. The heat flux involves a spatial derivative of it, so it should be cell-wise constant, and DG (because I picked it this way). Therefore, picking a deg == 1 is not correct. But this fixes this error… What is going on?

I also got strange warnings when I played around with the degrees of both temp and the heat flux, such as:

libffcx_forms_6d805b63b192afc755cfe28f475c8e3fdb08abc5.c: In function ‘tabulate_tensor_integral_888714572bbf59b20e226d6f0e3ddb633201b4cc’:
libffcx_forms_6d805b63b192afc755cfe28f475c8e3fdb08abc5.c:855:21: warning: unused variable ‘weights_083’ [-Wunused-variable]
  855 | static const double weights_083[1] = {0.5};
      |                     ^~~~~~~~~~~
At top level:
libffcx_forms_6d805b63b192afc755cfe28f475c8e3fdb08abc5.c:855:21: warning: ‘weights_083’ defined but not used [-Wunused-const-variable=]
libffcx_forms_1bccbd7aa7954a36b415fefd115e419ef5f34d0f.c: In function ‘tabulate_tensor_integral_d8173cb13faee8cb1f81d5db877573e52f4a8903’:
libffcx_forms_1bccbd7aa7954a36b415fefd115e419ef5f34d0f.c:855:21: warning: unused variable ‘weights_083’ [-Wunused-variable]

although I don’t get those with the code above. I do not remember which combination of degrees yielded these warnings.

There is already a feature request on this Add support for cell-wise data in ADIOS2 output · Issue #2720 · FEniCS/dolfinx · GitHub

Note that the error is not in the interpolation, but in writing to file.

Therefore, picking a deg == 1 is not correct

If you wish to export to file you can try reinterpolating again the DG0 field into a DG1 function and export to file.

1 Like

PR added for DG-0 support in VTXWriter (which would work with Paraview >= 5.12) at: Support DG-0 functions in VTXWriter by jorgensd · Pull Request #3107 · FEniCS/dolfinx · GitHub

1 Like