How-to: plot samurai fields and meshes#

In this how-to guide, we will show you how to plot samurai fields and meshes. For simulations with \(dim>1\), Paraview is the recommended tool for visualization. samurai provides functions to save meshes and fields in HDF5 format along with XDMF files for easy visualization in Paraview. Since it’s not possible to directly visualize samurai meshes and fields in 1D using Paraview, we provide a Python script read_mesh.py in the python directory of the samurai repository to help with this.

If you don’t know how to save meshes and fields, please refer to the save how-to guide here.

Plotting in nD with \(n>1\)#

In this simple case, you just need to open the xdmf file generated when saving the mesh and fields in Paraview. You can use the File -> Open menu in Paraview to open the xdmf file. Once the file is opened, you can use the various visualization tools provided by Paraview to visualize the mesh and fields.

Plotting in 1D#

To visualize samurai meshes and fields in 1D, you can use the provided Python script read_mesh.py. This script reads the HDF5 file generated when saving the mesh and fields and plots them using Matplotlib.

Dependencies#

Make sure you have the following dependencies installed in your Python environment.

  • h5py

  • matplotlib

Plot a mesh#

Here is an example of how to use the read_mesh.py script:

python python/read_mesh.py path_to_your_file

In this example, replace path_to_your_file with the path to the HDF5 file containing the saved mesh. The script will read the mesh from the file and plot it using Matplotlib.

Caution

Don’t write the extension of the file. The script will automatically look for the .h5 file.

Plot a field#

To plot a field along with the mesh, you can use the --field option followed by the name of the field you want to plot. Here is an example:

python samurai_path/python/read_mesh.py path_to_your_file --field field_name

You can add as many fields as you want by putting them after the --field option:

python samurai_path/python/read_mesh.py path_to_your_file --field field_name1 --field field_name2

Plot a time series#

If you have saved multiple time steps in your HDF5 file, you can plot a time series using the --start and --end options. Here is an example:

python samurai_path/python/read_mesh.py path_to_your_file_ --field field_name1 --field field_name2 --start 0 --end 10 --wait 100

In this example, the script will plot the fields field_name1 and field_name2 from time step 0 to time step 10. The --wait option specifies the time in milliseconds to wait between each time step.

The script will read the files named path_to_your_file_0.h5, path_to_your_file_1.h5, …, path_to_your_file_10.h5.

Save the results#

You can also save the plots generated by the script using the --save option followed by the output filename. Here is an example:

python samurai_path/python/read_mesh.py path_to_your_file --field field_name --save output_plot

or for a time series:

python samurai_path/python/read_mesh.py path_to_your_file_ --field field_name --start 0 --end 10 --save output_plot

Once again, don’t write the extension of the output file. The script will automatically save the plot in PNG format or MP4 format.