Writing some complex function to file

I have a scalar field \phi, and I want to write the following vectorial function of it to a pvd file:

\nabla_j(\nabla_i\phi\nabla_j\phi - \frac{1}{2}\delta_{ij}(\nabla\phi)^2)

I was doing the following

fFo << (div(outer(grad(f_),grad(f_))) - 0.5 * grad(inner(grad(f_),grad(f_))), t)

fFo has been defined as

fFo = File("output.pvd")

and ‘f_’ is defined from a scalar space as follows

P = FiniteElement('P', mesh.ufl_cell(), 1)
MFS = FunctionSpace(mesh, MixedElement([P,P]))
fm_ = Function(MFS)
f_, m_ = split(fm_)

But this gives an error on the line that tries to write to the file saying

AttributeError: 'ComponentTensor' object has no attribute '_cpp_object'

What would be the correct way of doing such a thing?

Thank you very much

Project the “function» to a suitable function space, and then use XDMFFile.write_checkpoint to write it to file. See for instance: Loading xdmf data back in - #4 by dokken
Note that differentiating a Finite element function changes the space it belongs to, see: How to choose projection space for stress tensor (post-processing)? - #2 by nate

1 Like

Thanks for the answer. For some reason I cannot open XDMF files on paraview even though the simulation looks fine? It’s annoying because I’d like to use write_checkpoint to then restart simulations. I 'll look into it, but for now I write everything to .pvd files. In any case the solution was just to change the writing line

fFo << (function, t)

to

fFo << (project(function, V), t)

where V is the vector space I’m using.
Thanks again!

What kind of error message do you get using XDMFFile?
There are three xdmf readers in paraview, and
You should use the Xdmf3ReaderT reader.

For the difference between the readers see: XMDF Reader names + XDMF2 Reader - #2 by Dave_DeMarle - ParaView Support - ParaView

If I use the normal reader it seems to load the file and it finds the fielsd I’m outputting to it, but doesn’t show anything. If I use the T or S readers, Paraview just crashes and closes. If I output the same information to a pvd file, then I can open it with Paraview and it renders everything just fine.

There were problems with older versions of paraview. Check what version you’ve got.

1 Like

It’s 5.9, the last as far as I know. I will try reinstalling everything see if I can use xdmf…

If you cant cant get it to work, please supply a minimal example that produces an XDMFFile that you cannot open.

paraview 5.9 should work fine (I think the previous problems were fixed around 5.6).