def plot_sfr(context, ax=None, label=None, **kwargs): ''' Plots the star formation rate ''' from seren3.analysis import stars from seren3.array import units if ax is None: ax = plt.gca() sfr_unit = None if "sfr_unit" in kwargs: sfr_unit = kwargs.pop("sfr_unit") sfr, lbtime, bsize = stars.sfr(context, **kwargs) if (sfr_unit is not None): sfr.convert_units(sfr_unit) ax.step(lbtime, sfr, linewidth=2., label=label) ax.set_yscale("log") unit = sfr.units dims = unit.dimensional_project([units.Unit("kg"), units.Unit("s")]) field_name = None if dims[0].numerator == 1: # SFR [Msol / Gyr] field_name = "SFR" elif dims[0].numerator == 0: # sSFR [Gyr^-1] field_name = "sSFR" else: raise Exception("Cannot understand SFR dims: %s" % dims) ax.set_ylabel(r"log$_{10}$ %s [$%s$]" % (field_name, unit.latex())) ax.set_xlabel(r"Lookback-time [$%s$]" % lbtime.units.latex())
def get_tracked_field_unit(family, field): unit = None if field[-1].isdigit(): unit = family.info[_tracked_field_unit_registry[field[:-1]]] else: unit = family.info[_tracked_field_unit_registry[field]] return units.Unit(str(unit))
def in_unit_system(unit, dims): from seren3.array import units if not hasattr(dims, "__iter__"): dims = [dims] if type(dims[0]) == str: dims = [units.Unit(x) for x in dims] new_unit = unit.dimensional_project(dims) new_unit = reduce(lambda x, y: x * y, [a**b for a, b in zip(dims, new_unit[:3])]) return new_unit
def physical_units(self, length="kpc", velocity="km s**-1", mass="Msol"): ''' Convert all units to desired physical system ''' from seren3.utils import units as unit_utils self.dims = [units.Unit(x) for x in length, velocity, mass, "h", "a"] cosmo = self.family.cosmo conversion_context = {"a" : cosmo["aexp"], "h" : cosmo["h"]} for k in self.keys(): v = self[k].units try: new_unit = unit_utils.in_unit_system(v, self.dims) self.indexed_fields[k].convert_units(new_unit) except UnitsException: continue
def __init__(self, family, dset, **kwargs): self._dset = dset self.dims = [units.Unit(x) for x in self.BASE_UNITS["length"],\ self.BASE_UNITS["velocity"], self.BASE_UNITS["mass"], "h", "a"] # default length, velocity, mass units self.family = family self.kwargs = kwargs # Index RAMSES fields with unit information self.indexed_fields = {} # keys = indexed_fields.fields if hasattr(indexed_fields, "fields") else indexed_fields.keys() fields = None if hasattr(dset, "fields"): fields = dset.fields elif isinstance(dset, dict): fields = dset.keys() else: raise Exception("Cannot extract fields from dset of type: %s" % type(dset)) for field in fields: if is_tracked(field): unit = get_tracked_field_unit(self.family, field) self.indexed_fields[field] = self.family.array(dset[field], unit) else: if (isinstance(dset[field], SimArray)): self.indexed_fields[field] = dset[field] else: self.indexed_fields[field] = self.family.array(dset[field]) if hasattr(dset, "points"): self["pos"] = self.family.array(dset.points, get_tracked_field_unit(self.family, "pos")) if hasattr(dset, "get_sizes"): self["dx"] = self.family.array(dset.get_sizes(), get_tracked_field_unit(self.family, "dx")) # Set units self.original_units()
def schmidtlaw(subsnap, filename=None, center=True, pretime='50 Myr', diskheight='3 kpc', rmax='20 kpc', compare=True, \ radial=True, clear=True, legend=True, bins=10, **kwargs): ''' Plots the schmidt law setting units correctly (i.e age). Follows pynbodys own routine. ''' import pynbody from pynbody.analysis import profile s = subsnap.pynbody_snapshot() # sets age property s.physical_units() if not radial: raise NotImplementedError( "Sorry, only radial Schmidt law currently supported") if center: pynbody.analysis.angmom.faceon(s.s) # faceon to stars if isinstance(pretime, str): from seren3.array import units pretime = units.Unit(pretime) # select stuff diskgas = s.gas[pynbody.filt.Disc(rmax, diskheight)] diskstars = s.star[pynbody.filt.Disc(rmax, diskheight)] tform = diskstars.s["age"] - diskstars.properties["time"] youngstars = np.where( diskstars["age"].in_units("Myr") <= pretime.in_units("Myr"))[0] # calculate surface densities if radial: ps = profile.Profile(diskstars[youngstars], nbins=bins) pg = profile.Profile(diskgas, nbins=bins) else: # make bins 2 kpc nbins = rmax * 2 / binsize pg, x, y = np.histogram2d(diskgas['x'], diskgas['y'], bins=nbins, weights=diskgas['mass'], range=[(-rmax, rmax), (-rmax, rmax)]) ps, x, y = np.histogram2d(diskstars[youngstars]['x'], diskstars[youngstars]['y'], weights=diskstars['mass'], bins=nbins, range=[(-rmax, rmax), (-rmax, rmax)]) if clear: plt.clf() print ps["density"] plt.loglog(pg['density'].in_units('Msol pc^-2'), ps['density'].in_units('Msol kpc^-2') / pretime / 1e6, "+", **kwargs) if compare: # Prevent 0 densitiy min_den = max(pg['density'].in_units('Msol pc^-2').min(), 1e-6) xsigma = np.logspace( min_den, np.log10(pg['density'].in_units('Msol pc^-2')).max(), 100) ysigma = 2.5e-4 * xsigma**1.5 # Kennicutt (1998) xbigiel = np.logspace(1, 2, 10) ybigiel = 10.**(-2.1) * xbigiel**1.0 # Bigiel et al (2007) plt.loglog(xsigma, ysigma, label='Kennicutt (1998)') plt.loglog(xbigiel, ybigiel, linestyle="dashed", label='Bigiel et al (2007)') plt.xlabel('$\Sigma_{gas}$ [M$_\odot$ pc$^{-2}$]') plt.ylabel('$\Sigma_{SFR}$ [M$_\odot$ yr$^{-1}$ kpc$^{-2}$]') if legend: plt.legend(loc=2) if (filename): plt.savefig(filename)