Plotting
This page documents Ulula’s plotting capabilities.
Overview
Ulula comes with two plotting routines for 1D and 2D data, plot1d()
and
plot2d()
. Both plot multiple panels for multiple fluid quantities that are
given as a list of strings. Valid identifiers are listed in the fields
dictionary. A simulation object must be passed to the plotting functions.
Plottable quantities
All plotting functions take a q_plot
parameter that contains a list of the fluid variables to
be plotted. The fields and their properties are defined in the following dictionary:
- ulula.core.plots.fields
List of fields that can be plotted. Most fields occur in the primitive or conserved variable arrays, but some fields are derived (e.g., total velocity).
Possible values include the primitive and conserved variables that the simulation keeps track of (see Simulation framework), as well as some derived quantities:
Abbreviation |
Quantity |
---|---|
|
Density |
|
X-velocity |
|
Y-velocity |
|
Total velocity |
|
X-momentum |
|
Y-momentum |
|
Pressure |
|
Total energy (internal + kinetic + potentials) |
|
Internal energy |
|
Temperature in Kelvin |
|
Gravitational potential |
|
Gravitational potential gradient in x |
|
Gravitational potential gradient in y |
Units
The user can plot in code units or choose a unit system for plotting, which may or may not correspond to the system of code units set in the simulation itself. For example, if the unit system of the underlying simulation is cgs (the default), then plotting with unit length, time, and mass of 1 centimeter, 1 second, and 1 gram leads to exactly the same plot as plotting in code units (albeit with slightly different labels). However, instead choosing 1 meter as the plotted unit will change the spatial axes as well as fluid properties that depend on length, such as density. In the plotting functions documented below, the plotted units are chosen via the following string codes:
Abbreviation |
Symbol |
CGS value |
---|---|---|
Length (in cm) |
||
|
\({\rm nm}\) |
\(10^{-7}\) |
|
\({\rm \mu m}\) |
\(10^{-4}\) |
|
\({\rm mm}\) |
\(10^{-1}\) |
|
\({\rm cm}\) |
\(1\) |
|
\({\rm m}\) |
\(10^{2}\) |
|
\({\rm km}\) |
\(10^{5}\) |
|
\({\rm AU}\) |
\(1.496\times10^{13}\) |
|
\({\rm pc}\) |
\(3.086\times10^{18}\) |
|
\({\rm kpc}\) |
\(3.086\times10^{21}\) |
|
\({\rm Mpc}\) |
\(3.086\times10^{24}\) |
|
\({\rm Gpc}\) |
\(3.086\times10^{27}\) |
Time (in s) |
||
|
\({\rm ns}\) |
\(10^{-9}\) |
|
\({\rm \mu s}\) |
\(10^{-6}\) |
|
\({\rm ms}\) |
\(10^{-3}\) |
|
\({\rm s}\) |
\(1\) |
|
\({\rm min}\) |
\(6.000\times10^{1}\) |
|
\({\rm hr}\) |
\(3.600\times10^{3}\) |
|
\({\rm yr}\) |
\(3.156\times10^{7}\) |
|
\({\rm kyr}\) |
\(3.156\times10^{10}\) |
|
\({\rm Myr}\) |
\(3.156\times10^{13}\) |
|
\({\rm Gyr}\) |
\(3.156\times10^{16}\) |
Mass (in g) |
||
|
\({\rm u}\) |
\(1.661\times10^{-24}\) |
|
\(m_{\rm p}\) |
\(1.673\times10^{-24}\) |
|
\({\rm ng}\) |
\(10^{-9}\) |
|
\({\rm \mu g}\) |
\(10^{-6}\) |
|
\({\rm mg}\) |
\(10^{-3}\) |
|
\({\rm g}\) |
\(1\) |
|
\({\rm kg}\) |
\(10^{3}\) |
|
\({\rm t}\) |
\(10^{6}\) |
|
\(M_{\oplus}\) |
\(5.972\times10^{27}\) |
|
\(M_{\odot}\) |
\(1.988\times10^{33}\) |
Plotting functions
- ulula.core.plots.plot1d(sim, q_plot=['DN', 'VX', 'VY', 'PR'], plot_unit_l='code', plot_unit_t='code', plot_unit_m='code', plot_ghost_cells=False, range_func=None, true_solution_func=None, plot_geometry='line', invert_direction=False, radial_bins_per_cell=4.0)
Plot fluid state along a 1D line
Create a multi-panel plot of the fluid variables along a line through the domain. This plotting routine is intended for pseudo-1D simulations, where the fluid state is uniform in the second dimension. The line is taken at the center of the domain in that dimension. The plot is created but not shown or saved to a file; these operations can be completed using the current matplotlib figure.
- Parameters:
- q_plot: array_like
List of quantities to plot. Quantities are identified via the short strings given in the
fields
dictionary.- plot_unit_l: str
Length unit to be plotted (see
units
for valid units). If other thancode
,plot_unit_t
andplot_unit_m
must also be changed from code units.- plot_unit_t: str
Time unit to be plotted (see
units
for valid units). If other thancode
,plot_unit_l
andplot_unit_m
must also be changed from code units.- plot_unit_m: str
Mass unit to be plotted (see
units
for valid units). If other thancode
,plot_unit_l
andplot_unit_t
must also be changed from code units.- plot_ghost_cells: bool
If
True
, ghost cells are plotted and separated from the physical domain by a gray vertical line. This option is useful for debugging. Ignored ifplot_geometry == 'radius'
.- range_func: function
If
None
, the plotting ranges are chosen automatically. Otherwise this parameter must be a function that can be called with the signaturerange_func(sim, q_plot, plot_geometry)
and that returns three lists with the same lengths as the number of plot quantities. The lists give the minimum and maximum plot extents for each fluid variable, as well as whether to use a log scale (True
). Elements in the lists can beNone
in which case ranges are chosen automatically.- true_solution_func: function
If
None
, no true solution is plotted. Otherwise must be a function that can be called with the signaturetrue_solution_func(sim, x_plot, q_plot, plot_geometry)
, wherex_plot
is an array of x-bins. The function must return a list with one element for each of the quantities inq_plot
. If an element isNone
, no solution is plotted. Otherwise the element must be an array with the same dimensions asx_plot
. The true solution must be in code units, which are automatically converted if the user has chosen different units for plotting. The true solution function is typically implemented within a problem setup (see Problem setups).- plot_geometry: str
The type of cut through the domain that is plotted. Can be
line
(in which case theidir
parameter specifies the dimension along which the plot is made), orradius
(which creates a radially averaged plot from the center).- invert_direction: bool
By default, the plotted line is along the dimension (x or y) that has more cells, and along x if they have the same number of cells. If
True
, this parameter inverts the direction.- radial_bins_per_cell: float
If
plot_geometry == radius
, this parameter chooses how many radial bins per cell are plotted. The bins are averaged onto the radial annuli, so this number can be greater than one.
- Returns:
- fig: matplotlib figure
The figure object.
- panels: array_like
List of axes objects.
- ulula.core.plots.plot2d(sim, q_plot=['DN', 'VX', 'VY', 'PR'], plot_unit_l='code', plot_unit_t='code', plot_unit_m='code', plot_ghost_cells=False, range_func=None, cmap_func=None, panel_size=3.0)
Plot fluid state in 2D
Create a multi-panel plot of the fluid variables in 2D. The plot is created but not shown or saved to a file; these operations can be completed using the current matplotlib figure.
- Parameters:
- q_plot: array_like
List of quantities to plot. Quantities are identified via the short strings given in the
fields
dictionary.- plot_unit_l: str
Length unit to be plotted (see
units
for valid units). If other thancode
,plot_unit_t
andplot_unit_m
must also be changed from code units.- plot_unit_t: str
Time unit to be plotted (see
units
for valid units). If other thancode
,plot_unit_l
andplot_unit_m
must also be changed from code units.- plot_unit_m: str
Mass unit to be plotted (see
units
for valid units). If other thancode
,plot_unit_l
andplot_unit_t
must also be changed from code units.- plot_ghost_cells: bool
If
True
, ghost cells are plotted and separated from the physical domain by a gray frame. This option is useful for debugging.- range_func: function
If
None
, the plotting ranges are chosen automatically. Otherwise this parameter must be a function that can be called with the signaturerange_func(sim, q_plot, plot_geometry)
and that returns three lists with the same lengths as the number of plot quantities. The lists give the minimum and maximum plot extents for each fluid variable, as well as whether to use a log scale (True
). Elements in the lists can beNone
in which case ranges are chosen automatically.- cmap_func: function
A function that returns a list of size nq with colormap objects to be used when plotting the fluid variables. If
None
, the default colormap is used for all fluid variables.- panel_size: float
Size of each plotted panel in inches
- Returns:
- fig: matplotlib figure
The figure object.
- panels: array_like
List of axes objects.
Helper functions
If you wish to create an entirely different plot without using the functions above, the following helper function might be helpful.
- ulula.core.plots.getPlotQuantities(sim, q_plot, unit_l='code', unit_t='code', unit_m='code')
Compile an array of fluid properties
Fluid properties are stored in separate arrays as primitive and conserved variables, or even in other arrays. Some quantities, such as total velocity, need to be calculated after the simulation has finished. This function takes care of all related operations and returns a single array that has the same dimensions as the domain.
Moreover, the function computes unit conversion factors if necessary and creates the corresponding labels. One quantity that demands special treatment is temperature, since it cannot sensibly be plotted in code units, or any other units than Kelvin. So we always convert to Kelvin, even if code units are used.
- Parameters:
- sim: Simulation
Object of type
Simulation
- q_plot: array_like
List of quantities to plot. Quantities are identified via the short strings given in the
fields
dictionary.- unit_l: str
Length unit to be plotted (see
units
for valid units). If other thancode
,unit_t
andunit_m
must also be changed from code units.- unit_t: str
Time unit to be plotted (see
units
for valid units). If other thancode
,unit_l
andunit_m
must also be changed from code units.- unit_m: str
Mass unit to be plotted (see
units
for valid units). If other thancode
,unit_l
andunit_t
must also be changed from code units.
- Returns:
- q_array: array_like
Array of fluid properties
- conv_factors: array_like
Unit conversion factors. The return
q_array
has already been multiplied by these factors in order to bring it into the desired unit system, but some other parts of the plot routines (e.g., color map limits) may also depend on these factors. If the plotting happens in code units, all factors are unity.- q_labels: array_like
List of labels for the fluid quantities
- conv_l: float
Unit conversion factor for length, which must be applied to the dimensions of any plot.
- label_l: float
Unit label for lengths.