def amr_nHII(context, dset): ''' Neutral hydrogen number density ''' nHII = SimArray(dset["nH"] * dset["xHII"], dset["nH"].units) nHII.set_field_latex("n$_{\\mathrm{HII}}$") return nHII
def amr_xHI(context, dset): ''' Hydrogen neutral fraction ''' val = 1. - dset["xHII"] xHI = SimArray(val) xHI.set_field_latex("$\\mathrm{x}_{\\mathrm{HI}}$") return xHI
def part_age(context, dset, **kwargs): #return context.quantities.tform(dset['epoch'], **kwargs) ''' Return the formation (lookback) time in units of Gyr For formation time since the big bang (age), do age_simu - tform (also in units of Gyr) ''' nml = context.nml isCosmoSim = ('cosmo' in nml['RUN_PARAMS'] and nml['RUN_PARAMS']['cosmo'] == '.true.') verbose = kwargs.get("verbose", False) tform = dset['epoch'] output = None if verbose: print 'part_age isCosmoSim: ', isCosmoSim if not isCosmoSim: output = tform * context.info['unit_time'].express(context.C.Gyr) # age in Gyr else: cosmology = context.cosmo h0 = cosmology['h'] * 100 friedmann = context.friedmann axp_out = friedmann['axp_out'] # hexp_out = friedmann['hexp_out'] tau_out = friedmann['tau_out'] t_out = friedmann['t_out'] # age_tot = friedmann['age_tot'] # age_simu = friedmann['age_simu'] time_simu = friedmann['time_simu'] patch = kwargs.get('patch', context.patch) if verbose: print 'part_age: patch = ', patch if patch == 'rt': output = (time_simu - tform) / (h0 * 1e5 / 3.08e24) / (365. * 24. * 3600. * 1e9) else: ntable = len(axp_out) - 1 output = np.zeros(len(tform)) for j in range(0, len(tform) - 1): i = 1 while ((tau_out[i] > tform[j]) and (i < ntable)): i += 1 # Interpolate time time = t_out[i] * (tform[j] - tau_out[i - 1]) / (tau_out[i] - tau_out[i - 1]) + \ t_out[i - 1] * (tform[j] - tau_out[i]) / \ (tau_out[i - 1] - tau_out[i]) time = max( (time_simu - time) / (h0 * 1e5 / 3.08e24) / (365 * 24 * 3600 * 1e9), 0) output[j] = time # output[j] = (time_simu - time)*unit_t/(365 * 24 * 3600 * 1e9) result = SimArray(output, "Gyr") result.set_field_latex("$\\mathrm{Age}$") return result
def star_Nion_d(context, dset, dt=0., group=1): ''' Computes the number of ionisiing photons produced by a stellar population per solar mass per second ''' from seren3.array import SimArray from seren3.utils.sed import io from seren3.exceptions import NoParticlesException from seren3 import config # from seren3.analysis import interpolate from scipy.interpolate import interp2d verbose = config.get("general", "verbose") Z_sun = 0.02 # metallicity of the sun nGroups = context.info["nGroups"] if (verbose): print 'Computing Nion_d for photon group %i/%i' % (group, nGroups) nIons = context.info["nIons"] nPhotons_idx = 0 # index of photon number in SED # Load the SED table agebins, zbins, SEDs = io.read_seds_from_lists(context.path, nGroups, nIons) igroup = group - 1 fn = interp2d(zbins / Z_sun, agebins, SEDs[:, :, igroup, nPhotons_idx]) age = dset["age"].in_units("Gyr") Z = dset["metal"] / Z_sun # in units of solar metalicity # Which star particles should we keep if dt != 0.: age -= dt.in_units("Gyr") # keep = np.where( np.logical_and(age >= 0., age.in_units("Myr") <= 10.) ) keep = np.where(age >= 0.) age = age[keep] Z = Z[keep] if len(age) == 0: raise NoParticlesException("No particles with (age - dt) > 0", "star_Nion_d") # interpolate photon production rate from SED nStars = len(age) nPhotons = np.zeros(nStars) for i in xrange(nStars): nPhotons[i] = fn(Z[i], age[i]) # nPhotons = interpolate.interpolate2d(age, Z, agebins, zbins, SEDs[:,:,igroup,nPhotons_idx]) # Multiply by (SSP) escape fraction and return nml = context.nml NML_KEYS = nml.NML rt_esc_frac = float(nml[NML_KEYS.RT_PARAMS]['rt_esc_frac'].replace( 'd', 'e')) Nion_d = SimArray(rt_esc_frac * nPhotons, "s**-1 Msol**-1") Nion_d.set_field_latex("$\\dot{N_{\\mathrm{ion}}}$") return Nion_d
def amr_nHI(context, dset): ''' Neutral hydrogen number density ''' xHII = np.round( dset["xHII"], decimals=5 ) nHI = SimArray(dset["nH"] * (1. - xHII), dset["nH"].units) # nHI = SimArray(dset["nH"] * (1. - dset["xHII"]), dset["nH"].units) nHI.set_field_latex("n$_{\\mathrm{HI}}$") return nHI
def hist2d(xo, yo, weights=None, mass=None, gridsize=(100, 100), nbins=None, make_plot=True, density=True, **kwargs): ''' Plots a 2D histogram of fields[0] against fields[1] ''' import numpy as np from seren3.array import SimArray # process keywords x_range = kwargs.get('x_range', None) y_range = kwargs.get('y_range', None) xlogrange = kwargs.get('xlogrange', False) ylogrange = kwargs.get('ylogrange', False) ret_im = kwargs.get('ret_im', False) if y_range is not None: if len(y_range) != 2: raise RuntimeError("Range must be a length 2 list or array") else: if ylogrange: y_range = [np.log10(np.min(yo)), np.log10(np.max(yo))] else: y_range = [np.min(yo), np.max(yo)] kwargs['y_range'] = y_range if x_range is not None: if len(x_range) != 2: raise RuntimeError("Range must be a length 2 list or array") else: if xlogrange: x_range = [np.log10(np.min(xo)), np.log10(np.max(xo))] else: x_range = [np.min(xo), np.max(xo)] kwargs['x_range'] = x_range if (xlogrange): x = np.log10(xo) else: x = xo if (ylogrange): y = np.log10(yo) else: y = yo if nbins is not None: gridsize = (nbins, nbins) if nbins is not None: gridsize = (nbins, nbins) ind = np.where((x > x_range[0]) & (x < x_range[1]) & (y > y_range[0]) & (y < y_range[1])) x = x[ind[0]] y = y[ind[0]] draw_contours = False if weights is not None and mass is not None: draw_contours = True weights = weights[ind[0]] mass = mass[ind[0]] # produce a mass-weighted histogram of average weight values at each # bin hist, ys, xs = np.histogram2d(y, x, weights=weights * mass, bins=gridsize, range=[y_range, x_range]) hist_mass, ys, xs = np.histogram2d(y, x, weights=mass, bins=gridsize, range=[y_range, x_range]) good = np.where(hist_mass > 0) hist[good] = hist[good] / hist_mass[good] else: if weights is not None: # produce a weighted histogram weights = weights[ind[0]] elif mass is not None: # produce a mass histogram weights = mass[ind[0]] hist, ys, xs = np.histogram2d(y, x, weights=weights, bins=gridsize, range=[y_range, x_range]) xs = .5 * (xs[:-1] + xs[1:]) ys = .5 * (ys[:-1] + ys[1:]) # if ret_im: # return make_contour_plot(hist, xs, ys, **kwargs) # if make_plot: # make_contour_plot(hist, xs, ys, **kwargs) # if draw_contours: # make_contour_plot(SimArray(density_mass, mass.units), xs, ys, filled=False, # clear=False, colorbar=False, colors='black', scalemin=nmin, nlevels=10) if isinstance(xo, SimArray): xs = SimArray(xs, xo.units) xs.set_field_latex(xo.get_field_latex()) if isinstance(yo, SimArray): ys = SimArray(ys, yo.units) ys.set_field_latex(yo.get_field_latex()) hist = np.flipud(hist) if density: return hist / len(xo), xs, ys return hist, xs, ys