Automatically Derived Arrays¶
Pynbody includes a system which automatically derives one array from others. The idea is that this system
(1) permits analysis code to assume that certain arrays exist – whether or not they exist in the file;
(2) allows arrays to be kept up-to-date when they depend on other arrays
Built-in derived arrays¶
The quantities listed under derived
are calculated for
all simulation types. If you want, for example, to calculate the
specific kinetic energy, you can just access the ke
array and
pynbody will calculate it:
In [1]: import pynbody
In [2]: s = pynbody.load('testdata/g15784.lr.01024.gz')
In [3]: s['ke']
Out[3]:
SimArray([0.01443704, 0.01435218, 0.01545113, ..., 0.02690037, 0.02084463,
0.02266956], '2.98e+06 km**2 a**2 s**-2')
Note that you cannot directly alter a derived array:
In [4]: s['ke'][0] = 0
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-115c2ca7f0b6> in <module>
----> 1 s['ke'][0] = 0
~/Science/pynbody/pynbody/array.py in q(a, *y, **kw)
702 return w(a, *y, **kw)
703 else:
--> 704 return w(a, *y)
705
706 q.__name__ = w.__name__
~/Science/pynbody/pynbody/array.py in __setitem__(self, item, to)
490 np.ndarray.__setitem__(self, item, to.in_units(self.units))
491 else:
--> 492 np.ndarray.__setitem__(self, item, to)
493
494 def __setslice__(self, a, b, to):
ValueError: assignment destination is read-only
But the array updates itself when one of its dependencies changes:
In [5]: s['vel']*=2
In [6]: s['ke']
Out[6]:
SimArray([0.05774817, 0.05740871, 0.0618045 , ..., 0.10760149, 0.08337851,
0.09067823], '2.98e+06 km**2 a**2 s**-2')
The recalculation only takes place when required – in the example
above the ke
array is stored (just like a normal array) until the velocity array is updated,
at which point it is deleted. When you ask for the ke
array again,
it is recalculated.
This is why you’re not allowed to change values in a derived array – your changes could be overwritten, leading to confusing bugs. However, if you want to make a derived array back into a normal array you can do so.
In [7]: s['ke'].derived = False
In [8]: s['ke'][0] = 0
In [9]: s['ke']
Out[9]:
SimArray([0. , 0.05740871, 0.0618045 , ..., 0.10760149, 0.08337851,
0.09067823], '2.98e+06 km**2 a**2 s**-2')
At this point you’ve taken full responsibility for the array, so if you update its dependencies the framework won’t help you out any longer:
In [10]: s['vel']*=2
In [11]: s['ke']
Out[11]:
SimArray([0. , 0.05740871, 0.0618045 , ..., 0.10760149, 0.08337851,
0.09067823], '2.98e+06 km**2 a**2 s**-2')
To get the framework back on your side, you can delete the modified array:
In [12]: del s['ke']
In [13]: s['ke']
Out[13]:
SimArray([0.23099267, 0.22963485, 0.247218 , ..., 0.43040596, 0.33351406,
0.36271291], '2.98e+06 km**2 a**2 s**-2')
Defining your own deriving functions¶
You can easily define your own derived arrays. The easiest way to do
this is using the decorator pynbody.derived_array
. An example is
given in the data access tutorial.
When your function is called, the framework monitors any arrays it retrieves from the simulation. It automatically marks the accessed arrays as dependencies for your function.
Built-in derived arrays for all snapshot classes¶
derived¶
Holds procedures for creating new arrays from existing ones, e.g. for getting the radial position. For more information see Automatically Derived Arrays.
-
pynbody.derived.
age
(self)[source]¶ Stellar age determined from formation time and current snapshot time
-
pynbody.derived.
X
(s, b='K')¶ K magnitude from analysis.luminosity.calc_mags
-
pynbody.derived.
lum_den
(s)¶ Luminosity density in astronomy-friendly units: 10^(-0.4 K_mag) per unit volume. The magnitude is taken from analysis.luminosity.calc_mags.
-
pynbody.derived.
mu
(sim, t0=None)[source]¶ mean molecular mass, i.e. the mean atomic mass per particle
tipsy¶
Note
These take advantage of arrays present in Gasoline snapshots
-
pynbody.snapshot.tipsy.
hetot
(self)[source]¶ Helium mass fraction including correction based on metallicity
-
pynbody.snapshot.tipsy.
hydrogen
(self)[source]¶ Hydrogen mass fraction including correction based on metallicity
-
pynbody.snapshot.tipsy.
feh
(self)[source]¶ Iron abundance [Fe/H] derived from tipsy array FeMassFrac, with solar values from Asplund et al 09
-
pynbody.snapshot.tipsy.
oxh
(self)[source]¶ Oxygen abundance [O/H] derived from tipsy array FeMassFrac, with solar values from Asplund et al 09
-
pynbody.snapshot.tipsy.
ofe
(self)[source]¶ Oxygen-to-iron ratio [O/Fe] derived from tipsy arrays OxMassFrac and FeMassFrac with solar values from Asplund et al 09
-
pynbody.snapshot.tipsy.
mgfe
(sim)[source]¶ Magnesium-to-iron ratio [Mg/Fe] derived from tipsy arrays MgMassFrac and FeMassFrac with solar values from Asplund et al 09
-
pynbody.snapshot.tipsy.
nefe
(sim)[source]¶ Neon-to-iron ratio [Ne/Fe] derived from tipsy arrays MgMassFrac and FeMassFrac with solar values from Asplund et al 09
gadget¶
No special derived quantities at the moment.