4. Using Python to plot simulation¶
PyPion is a Python library written to read in Silo data files from PION simulations and to plot the data. This library works for 1D, 2D, and 3D data files and for any amount of nested-grid levels. For the moment this library only works with Python 2, work is being done to update to Python 3.
SILO is both a scientific database format and a data I/O library, producing machine-independent data files that can be easily shared between different computing architectures. It is often built using HDF5 as the low-level I/O driver, and has excellent performance on HPC systems. The SILO library comes with a python interface, but these routines require an extra layer of customisation to read PION snapshots simply. This is what PyPion provides – a set of routines that call functions from the SILO python library to read PION snapshots into numpy arrays and plot them easily and efficiently.
4.1. Installing PyPion¶
We have provided two ways to set up PyPion, using a docker container and using your system python installation.
4.1.1. Using the PyPion docker image¶
A docker image has been created to make it easier to run the python library. Coming soon…
4.1.2. Using the system python installation¶
To be able to use all the features of this library you will need to have the following python modules installed on your system. At the time of writing, installing the python-silo module yourself means you can only use this library with python2.7.
Here are instructions for debian/Ubuntu installation:
$ sudo apt install python-silo
$ sudo apt install python-numpy
$ sudo apt install python-astropy
$ sudo apt install python-matplotlib
$ sudo apt install python-tk
All of these modules can also be installed through pip if you prefer:
$ pip install 'python-module'
And of course you need to have the lastest version of the PyPion repository pulled to your desktop! This is available here: https://git.dias.ie/massive-stars-software/pypion/
4.2. Using PyPion to look at simulation data¶
The main scripts in the library are:
- argparse_command.py - Saves the options entered into the command line when the python script is run.
- SiloHeader_data.py - Which opens the silo file and saves all of the important header variables (eg. sim_time, xmax, xmin, etc.).
- ReadData.py - Opens the directory in the silo (or vtk, or fits) file and saves the requested variable data (eg. density, temp, etc.).
- Plotting_Classes.py - Sets up the plotting function and the figure.
For the following example of how to plot PION data we are using the data created from the simulation in Running an example simulation. You can also download the python script for the following example here [Add link!].
- Import modules:
First you will want to import the PyPion library, all you need to import is ReadData.py since it inherits all information from SiloHeader_data.py.from ReadData import ReadData
Next import numpy and astropy to help with the data analysis.import numpy as np from astropy import units as u
And finally you will need to import matplotlib libraries to plot the data.import matplotlib from matplotlib.colorbar import Colorbar import matplotlib.pyplot as plt import matplotlib as mpl from mpl_toolkits.axes_grid1 import make_axes_locatable from matplotlib.ticker import MultipleLocator import matplotlib.gridspec as gridspec
- Bring in the data:
We’re going to plot all the levels from 1 timestep here, so create an array with the location and name of these 3 files. Then pass this array into the ReadData class.arr = ('Wind2D_HD_l3n0128_level00_0000.00009216.silo', 'Wind2D_HD_l3n0128_level01_0000.00009216.silo', 'Wind2D_HD_l3n0128_level02_0000.00009216.silo') read_data = ReadData(arr)
We’re also going to be plotting the density parameter here. So lets save the density data into an array called ‘data’ and also save the size of the grid and the simulation time into their own respective arrays.param = 'Density' data = read_data.get_2Darray(param)['data'] lim_max = (read_data.get_2Darray(param)['max_extents'] * u.cm) lim_min = (read_data.get_2Darray(param)['min_extents'] * u.cm) sim_time = read_data.get_2Darray(param)['sim_time'].to(u.Myr)
Now that we have all the data we need, we can start to plot it. First let’s create an empty figure instance.fig = plt.figure()
To plot the data from all the levels onto the same figure we need to loop over each level’s data to plot it to the figure.for i in range(len(data)): log_data = np.log10(data[i]) ax1.set_title('Time = %5.5f Myr' % sim_time.value) ax1.set_xlim(lim_min.value, lim_max.value) ax1.set_ylim(lim_min.value, lim_max.value) im1 = ax1.imshow(log_data, interpolation='nearest', cmap="viridis", extent=[lim_min[i].value, lim_max[i].value, lim_min[i].value, lim_max[i].value], origin='lower', vmax=-22, vmin=-27) plt.show()
This is the basics you’ll need to plot the simulation data, if you need to do other things like adding a colorbar or reflecting the data about the x-axis then see the Plotting_Classes.py script in the PyPion https://git.dias.ie/massive-stars-software/pypion/ repository.
4.3. Known issues with Python¶
There are a couple known issues with the python library.
If you want to run this library over ssh and don’t have an x-server then you will need to add this piece of code after you import matlotlib:
To stop python from printing deprecation warnings to the screen then add the following when importing modules:
import warnings warnings.filterwarnings("ignore", category=matplotlib.cbook.mplDeprecation)