plotting scalar field and deformed mesh

267
views
0
3 months ago by
Hello,

I'm solving a nonlinear elasticity problem.  I would like to plot the deformed shape (with mesh) but with coloring given by the elastic energy density.  I'm able to do these individually, that is, I can plot the deformed configuration with mesh easily (I save the deformations as a .pvd file, and then use paraview to "warp by vector"), and I can easily save the elastic energy density field and plot that but in the *undeformed* configuration.

I don't know how to do this and would appreciate any help.
Community: FEniCS Project

4
3 months ago by
You can either
1. write both field into the same .xdmf file (i.e.  do not ues .pvd files)
2. write two different .pvd files and "join" them inside Paraview using AppendAttributes
I'd suggest option 1)
3
3 months ago by

You can combine fields from different .pvd files in Paraview with the "Append Attributes" filter.  If you open all of the relevant .pvd files, select them all in the pipeline browser, then apply Append Attributes, which can be found through the Filters menu.

2
3 months ago by
"Append attribute" is the ParaView filter for you. https://www.allanswered.com/post/loqve/two-different-fields-into-the-same-xdmf-file/
0
3 months ago by
Thanks everyone!  This worked perfectly.  A follow-up: I need to do this lots of times.  Is there some way to automate this (either in the FENICS output or in Paraview) so that I don't need to click a bunch of buttons in Paraview each time to do this.
1
If you go to Tools > Start Trace, you can write out a Python script that records all of your actions in the GUI.  The result is usually readable enough to modify, to put actions in a loop.  (I do remember I had to spend some time fussing with Paraview versions and environment variables to get the scripts to run correctly, but, unfortunately, I forgot the details; if you're lucky, everything will just work on the first try...)
written 3 months ago by David Kamensky
Thank you!  This worked *almost* perfectly.  I did the trace, and then ran it in the Python shell the next time around.  Paraview seems to use arbitrary names for the data ("f319").  It goes well as far as append attributes, but then I have to do the rest by hand.

Maybe the arbitrary names are because I'm not saving them well in FENICS?  Here is my script snippet:
fileYpvd=File("deformation.pvd")
fileYpvd << y

(my Paraview version is 5.0.1 running on Ubuntu through Windows, and for some reason it crashes every time I try to open the XDMF version)
written 3 months ago by K D
1
When you define your function y use a name for it so that it will always be the same e.g.
y = Function(V, name="my_displacement")​
written 3 months ago by bleyerj
Thanks!  I tried this, but Paraview still insists on calling my data "f_5670" and similar meaningless names...
written 3 months ago by K D
1
Do not use .pvd files. Switch to .xdmf, and explicitly add a time argument to work around a bug, i.e.
filex = XDMFFile('Displacement_TYMX_GE.xdmf')
filex.parameters['functions_share_mesh'] = True
filex.parameters['rewrite_function_mesh'] = False
filex.parameters["flush_output"] = True

filex.write(FU3_6.sub(0), t=0.)
filex.write(FU3_6.sub(1), t=0.)
filex.write(t1_project, t=0.)
filex.write(t2_project, t=0.)
filex.write(tn_project, t=0.)
​
written 3 months ago by Marco Morandini
Thanks Marco.  I'd prefer to use XDMF files but Paraview crashes when I try to open them (please see above for details).

I tried 3 things below.  First, save in 2 separate XDMF files.  Second, your suggestion.  Third, your suggestion with a tweak.  None work out.

When I do this, I am able to save the output (I think, but I have no way to visualize it because Paraview crashes).  And even if it works, it is in 2 separate files which I would also prefer to avoid for eventual plotting.
fileY = XDMFFile("deformation.xdmf")
fileY.write(y)
filePsi = XDMFFile("Energy_Density.xdmf")
filePsi.write(psi_field)

When I try essentially your suggestion, FENICS gives me the error
TypeError: XDMFFile_write() takes no keyword argumentsTypeError: XDMFFile_write() takes no keyword arguments
Here is the exact code that I used:
fileY=XDMFFile('All_Stuff.xdmf')
fileY.parameters['functions_share_mesh'] = True
fileY.parameters['rewrite_function_mesh'] = False
fileY.parameters["flush_output"] = True
fileY.write(y,t=0.)
fileY.write(psi_field,t=0.)

When I get rid of the $t=0.$ argument, FENICS doesn't complain but Paraview crashes.
fileY=XDMFFile('All_Stuff.xdmf')
fileY.parameters['functions_share_mesh'] = True
fileY.parameters['rewrite_function_mesh'] = False
fileY.parameters["flush_output"] = True
fileY.write(y)
fileY.write(psi_field)

written 3 months ago by K D
Are you using the last Paraview, i.e. 5.5? Beside this, and the suggestion to use dolfin master, I have no suggestions.
written 3 months ago by Marco Morandini
Unfortunately my Paraview is 5.0.1, and my Ubuntu believes that it is the latest version available for my OS.
written 3 months ago by K D
1
Ubuntu can believe whatever it likes. But you should not believe Ubuntu ;) . Simply dowload a new Paraview binary from https://www.paraview.org/download/ , unpack it locally and run it!
written 3 months ago by Marco Morandini
OK, I did that (actually, I just installed it on Windows since I am running Ubuntu on Windows...I should have thought of that earlier). Now I can finally open XDMF files, which is great!  So I can stop using PVD files.

But the issue that Paraview gives my datasets weird arbitrary names is not solved by this.  Also I think only $y$ is saved.  My code snippet is below.

fileY=XDMFFile('All_Stuff.xdmf')
fileY.parameters['functions_share_mesh'] = True
fileY.parameters['rewrite_function_mesh'] = False
fileY.parameters["flush_output"] = True

fileY.write(y)
fileY.write(psi_field)
written 3 months ago by K D
"name=" while defining the Functions is your friend. And remember the "t=" while writing. If it does not work perhaps you need a more recent dolfin.
written 3 months ago by Marco Morandini
I do have the "name=".  I get an error with "t=".  So it looks like I need a more recent version of dolfin.  I will fight with Ubuntu /Spyder to see how I do that.
written 3 months ago by K D
"t=" is for a dolfin bug. So, if you are not bitten by it you can neglect it.
Also: if you have a time loop remember that the Functions must be declared outside. And if you project, solve, or anything else you must be sure that you are assigning the resulting vector to the Function with the name, and not simply using the same name (variable name, that is) for a different, temporary object created by dolfin (this is likely the root of your problems)
written 3 months ago by Marco Morandini
Thanks again!  Partial progress to report.

Indeed, I had a number of operations that likely broke the name assignment.  Now I do this just before saving:
y.rename("y","deformation")
psi_field.rename("psi_field","energy_density")

and my files in Paraview have decent names.

But I still have the (somewhat minor) issue that I have to save in 2 files.  If I save both variables in a single file following your script above, I can only see one of them in Paraview.
written 3 months ago by K D
Do not rename, assemble (or interpolate) directly into the right function. ("tensor=" or something like that, check the documentation).
written 3 months ago by Marco Morandini
Can you give me a link or more specific keywords?  When I search the documentation for "tensor", there's tons of stuff that appears unrelated.
written 3 months ago by K D
write a code snippet after which you need rename and I can point you in the right direction.
written 3 months ago by Marco Morandini
I think the project statement below breaks it, and I'm not sure but I think the solve statement also does that.

psi_field=Function(W,name="energy_density")
psi_field = project(psi,W)
solve(F == 0, y, bcs=bc, J=J,tol=tol,M=M)​

written 3 months ago by K D
project(psi,W, function=psi_field)​

should do the trick (untested).
written 3 months ago by Marco Morandini
Thanks, I'll try it out.  Though it doesn't fix the issue of not getting two fields into the same XDMF file...?
written 3 months ago by K D
No, for that I have no clues (apart from the suggestion of using dolfin master): I have no problems with many fields into the same XDMF
(see however this discussion and this commit ). Perhaps this is what is biting you. If this is the case then you need to use dolfin master.
written 3 months ago by Marco Morandini
Thanks for your help with all the other issues (particularly getting XDMF going)
written 3 months ago by K D