Suppose I run the Navier Stokes equation given here: Implementation — FEniCSx tutorial (jsdokken.com)
Then I have the answer u_n
as :
Coefficient(FunctionSpace(Mesh(blocked element (Basix element (P, triangle, 1, gll_warped, unset, False), (2,)), 0), VectorElement(FiniteElement('Lagrange', triangle, 2), dim=2)), 0)
And its mesh is given by:
mesh = <dolfinx.mesh.Mesh at 0x7f56de19f5e0>
Now I was going to save the value of u_n
. I have tried multiple ways and the closest I could get was:
from dolfinx import io
with io.XDMFFile(mesh.comm, "u_n.xdmf", "w") as xdmf_file:
xdmf_file.write_mesh(mesh)
xdmf_file.write_function(u_n)
But this still gives me the following error:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Cell In[5], line 5
3 with io.XDMFFile(mesh.comm, "u_n.xdmf", "w") as xdmf:
4 xdmf.write_mesh(mesh)
----> 5 xdmf.write_function(u_n)
File /usr/local/dolfinx-real/lib/python3.10/dist-packages/dolfinx/io/utils.py:235, in XDMFFile.write_function(self, u, t, mesh_xpath)
219 def write_function(self, u: Function, t: float = 0.0, mesh_xpath="/Xdmf/Domain/Grid[@GridType='Uniform'][1]"):
220 """Write function to file for a given time.
221
222 Note:
(...)
233
234 """
--> 235 super().write_function(getattr(u, "_cpp_object", u), t, mesh_xpath)
RuntimeError: Degree of output Function must be same as mesh degree. Maybe the Function needs to be interpolated?
Could you please help me out with this. I don’t need to strictly use this method. I simply want to save the u_n
, p_n
and mesh
separately. And then load it to my notebook later.
I use dolfinx through a docker container and use jupyter notebook to run the codes.