def _make_sigma(self, r_bins, sigmaBinned): """ Generates the surface density as a function of r, a callable object sigma(r) and assigns it to self.sigma Generates a spline interpolation of sigma vs r from the file defined by settings.filenames.sigmaFileName. Returns sigma vs r as an cubic spline interpolation object (see scipy.interpolation.InterpolatedUnivariateSpline) sigma_input should be a pickled dictionary with the entries: 'sigma': <sigma evaluated at r> 'r': <r for the bins> If the input sigma has units, sigma vs r will be returned in units of Msol/au^2 """ # Convert to default units of Msol/au^2. If no units, assign default sigmaBinned = match_units(sigmaBinned, 'Msol au**-2')[0] # Convert r_bins to default units of 'au' r_bins = match_units(r_bins, 'au')[0] # Calculate spline interpolation sigspline = spline(r_bins, sigmaBinned, k=3, ext='zeros') # print 'Calculating spline interpolation (slow for many data points)' # sigspline = interp1d(r_bins,sigmaBinned,kind='cubic',fill_value=0.0,\ # bounds_error=False) def sigout(r): """ Linear spline interpolation of sigma(r). ARGUMENTS: r - can be scalar, numpy array, or sim array RETURNS: sigma (surface density) evaluated at r """ # Try to convert r to the units used to make sigspline ('au') r = match_units(r, 'au')[0] return SimArray(sigspline(r), 'Msol au**-2') self.sigma = sigout self.r_bins = r_bins
def _make_pdf(self): """ Generates the probability density as a function of r (from the surface density) and returns it as a callable function pdf(r) to self.pdf pdf(r) = 2*pi*r*sigma(r), up to a normalization Using sigma(r) calculated in _make_sigma and r_bins loaded from settings.filenames.sigmaFileName, creates an interpolation over r_bins, sigma(r_bins). The PDF will be approximately normalized """ # Calculate the binned PDF pdfBinned = 2 * np.pi * self.r_bins * self.sigma(self.r_bins) # Normalize integral = simps(pdfBinned, self.r_bins) pdfBinned /= integral # Calculate a spline interpolation pdfSpline = spline(self.r_bins, pdfBinned, k=3, ext='zeros') # print 'Calculating spline interpolation (slow for many data points)' # pdfSpline = interp1d(self.r_bins, pdfBinned, kind='cubic',\ # fill_value=0.0, bounds_error=False) def pdf_fcn(r_in): """ Normalized cubic spline interpolation of the PDF(r) from sigma(r). The PDF is just calculated as 2*pi*r*sigma(r). ARGUMENTS: r_in - radii at which to calculate the PDF RETURNS: probability density function from sigma(r) evaluated at r_in """ # Put r_in into the units used in generating the pdf r_in = match_units(r_in, self.r_bins)[0] # Evaluate the pdf at r_in pdf_vals = pdfSpline(r_in) # Put the pdf into units of r_in.units**-1 pdf_vals = match_units(pdf_vals, 1 / r_in)[0] return pdf_vals self.pdf = pdf_fcn
def _make_pdf(self): """ Generates the probability density as a function of r (from the surface density) and returns it as a callable function pdf(r) to self.pdf pdf(r) = 2*pi*r*sigma(r), up to a normalization Using sigma(r) calculated in _make_sigma and r_bins loaded from settings.filenames.sigmaFileName, creates an interpolation over r_bins, sigma(r_bins). The PDF will be approximately normalized """ # Calculate the binned PDF pdfBinned = 2*np.pi*self.r_bins * self.sigma(self.r_bins) # Normalize integral = simps(pdfBinned,self.r_bins) pdfBinned /= integral # Calculate a spline interpolation pdfSpline = spline(self.r_bins, pdfBinned, k=3, ext='zeros') # print 'Calculating spline interpolation (slow for many data points)' # pdfSpline = interp1d(self.r_bins, pdfBinned, kind='cubic',\ # fill_value=0.0, bounds_error=False) def pdf_fcn(r_in): """ Normalized cubic spline interpolation of the PDF(r) from sigma(r). The PDF is just calculated as 2*pi*r*sigma(r). ARGUMENTS: r_in - radii at which to calculate the PDF RETURNS: probability density function from sigma(r) evaluated at r_in """ # Put r_in into the units used in generating the pdf r_in = match_units(r_in, self.r_bins)[0] # Evaluate the pdf at r_in pdf_vals = pdfSpline(r_in) # Put the pdf into units of r_in.units**-1 pdf_vals = match_units(pdf_vals, 1/r_in)[0] return pdf_vals self.pdf = pdf_fcn
def _make_sigma(self, r_bins, sigmaBinned): """ Generates the surface density as a function of r, a callable object sigma(r) and assigns it to self.sigma Generates a spline interpolation of sigma vs r from the file defined by settings.filenames.sigmaFileName. Returns sigma vs r as an cubic spline interpolation object (see scipy.interpolation.InterpolatedUnivariateSpline) sigma_input should be a pickled dictionary with the entries: 'sigma': <sigma evaluated at r> 'r': <r for the bins> If the input sigma has units, sigma vs r will be returned in units of Msol/au^2 """ # Convert to default units of Msol/au^2. If no units, assign default sigmaBinned = match_units(sigmaBinned, 'Msol au**-2')[0] # Convert r_bins to default units of 'au' r_bins = match_units(r_bins, 'au')[0] # Calculate spline interpolation sigspline = spline(r_bins, sigmaBinned, k=3, ext='zeros') def sigout(r): """ Linear spline interpolation of sigma(r). ARGUMENTS: r - can be scalar, numpy array, or sim array RETURNS: sigma (surface density) evaluated at r """ # Try to convert r to the units used to make sigspline ('au') r = match_units(r, 'au')[0] return SimArray(sigspline(r), 'Msol au**-2') self.sigma = sigout self.r_bins = r_bins