def denormalize(self): """ Undo the normalization. """ R0 = self._normR0 Vc0 = self._normVc0 Phi0 = self._normPhi0 # rescale the simulation if not self._posunit is None: self._s['pos'].convert_units(self._posunit) if not self._velunit is None: self._s['vel'].convert_units(self._velunit) # rescale the grid self._rgrid *= R0 if self._logR: self._logrgrid += np.log(R0) rs = self._logrgrid else: rs = self._rgrid self._zgrid *= R0 # rescale the potential self._amp *= Phi0 # restore the splines if not self._enable_c and self._interpPot: self._potInterp = self._savedsplines['pot'] self._rforceInterp = self._savedsplines['rforce'] self._zforceInterp = self._savedsplines['zforce'] elif self._enable_c and self._interpPot: self._potGrid_splinecoeffs = interpRZPotential.calc_2dsplinecoeffs_c( self._potGrid) if self._interpPot: self._vcircInterp = self._savedsplines['vcirc'] if self._interpepifreq: self._R2interp = self._savedsplines['R2deriv'] self._epifreqInterp = self._savedsplines['epifreq'] if self._interpverticalfreq: self._z2interp = self._savedsplines['z2deriv'] self._verticalfreqInterp = self._savedsplines['verticalfreq']
def denormalize(self) : """ Undo the normalization. """ R0 = self._normR0 Vc0 = self._normVc0 Phi0 = self._normPhi0 # rescale the simulation if not self._posunit is None: self._s['pos'].convert_units(self._posunit) if not self._velunit is None: self._s['vel'].convert_units(self._velunit) # rescale the grid self._rgrid *= R0 if self._logR: self._logrgrid += np.log(R0) rs = self._logrgrid else : rs = self._rgrid self._zgrid *= R0 # rescale the potential self._amp *= Phi0 # restore the splines if not self._enable_c and self._interpPot : self._potInterp= self._savedsplines['pot'] self._rforceInterp= self._savedsplines['rforce'] self._zforceInterp= self._savedsplines['zforce'] elif self._enable_c and self._interpPot: self._potGrid_splinecoeffs= interpRZPotential.calc_2dsplinecoeffs_c(self._potGrid) if self._interpPot : self._vcircInterp = self._savedsplines['vcirc'] if self._interpepifreq : self._R2interp = self._savedsplines['R2deriv'] self._epifreqInterp = self._savedsplines['epifreq'] if self._interpverticalfreq: self._z2interp = self._savedsplines['z2deriv'] self._verticalfreqInterp = self._savedsplines['verticalfreq']
def normalize(self, R0=8.0) : """ Normalize all positions by R0 and velocities by Vc(R0). If :class:`~scipy.interpolate.RectBivariateSpline` or :class:`~scipy.interpolate.InterpolatedUnivariateSpline` are used, redefine them for use with the rescaled coordinates. To undo the normalization, call :func:`~galpy.potential.SnapshotPotential.InterpSnapshotPotential.denormalize`. """ Vc0 = self.vcirc(R0) Phi0 = np.abs(self.Rforce(R0,0.0)) self._normR0 = R0 self._normVc0 = Vc0 self._normPhi0 = Phi0 # rescale the simulation if not isinstance(self._s['pos'].units,NoUnit): self._posunit = self._s['pos'].units self._s['pos'].convert_units('%s kpc'%R0) else: self._posunit = None if not isinstance(self._s['vel'].units,NoUnit): self._velunit = self._s['vel'].units self._s['vel'].convert_units('%s km s**-1'%Vc0) else: self._velunit = None # rescale the grid self._rgrid /= R0 if self._logR: self._logrgrid -= np.log(R0) rs = self._logrgrid else : rs = self._rgrid self._zgrid /= R0 # rescale the potential self._amp /= Phi0 self._savedsplines = {} # rescale anything using splines if not self._enable_c and self._interpPot : for spline,name in zip([self._potInterp, self._rforceInterp, self._zforceInterp], ["pot", "rforce", "zforce"]): self._savedsplines[name] = spline self._potInterp= interpolate.RectBivariateSpline(rs, self._zgrid, self._potGrid/R0, kx=3,ky=3,s=0.) self._rforceInterp= interpolate.RectBivariateSpline(rs, self._zgrid, self._rforceGrid, kx=3,ky=3,s=0.) self._zforceInterp= interpolate.RectBivariateSpline(rs, self._zgrid, self._zforceGrid, kx=3,ky=3,s=0.) elif self._enable_c and self._interpPot: self._potGrid_splinecoeffs= interpRZPotential.calc_2dsplinecoeffs_c(self._potGrid/R0) if self._interpPot : self._savedsplines['vcirc'] = self._vcircInterp self._vcircInterp = interpolate.InterpolatedUnivariateSpline(rs, self._vcircGrid/Vc0, k=3) if self._interpepifreq: self._savedsplines['R2deriv'] = self._R2interp self._savedsplines['epifreq'] = self._epifreqInterp self._R2interp = interpolate.RectBivariateSpline(rs, self._zgrid, self._R2derivGrid, kx=3,ky=3,s=0.) self._epifreqInterp = interpolate.InterpolatedUnivariateSpline(rs[self._epigoodindx], self._epifreqGrid[self._epigoodindx]/np.sqrt(Phi0/R0), k=3) if self._interpverticalfreq: self._savedsplines['z2deriv'] = self._z2interp self._savedsplines['verticalfreq'] = self._verticalfreqInterp self._z2interp = interpolate.RectBivariateSpline(rs, self._zgrid, self._z2derivGrid, kx=3,ky=3,s=0.) self._verticalfreqInterp = interpolate.InterpolatedUnivariateSpline(rs[self._verticalgoodindx], self._verticalfreqGrid[self._verticalgoodindx]/np.sqrt(Phi0/R0), k=3)
def __init__(self, s, ro=None,vo=None, rgrid=(np.log(0.01),np.log(20.),101), zgrid=(0.,1.,101), interpepifreq = False, interpverticalfreq = False, interpPot = True, enable_c = True, logR = True, zsym = True, numcores=None,nazimuths=4,use_pkdgrav = False) : """ NAME: __init__ PURPOSE: Initialize an InterpSnapshotRZPotential instance INPUT: s - a simulation snapshot loaded with pynbody rgrid - R grid to be given to linspace as in rs= linspace(*rgrid) zgrid - z grid to be given to linspace as in zs= linspace(*zgrid) logR - if True, rgrid is in the log of R so logrs= linspace(*rgrid) interpPot, interpepifreq, interpverticalfreq= if True, interpolate these functions (interpPot=True also interpolates the R and zforce) enable_c= enable use of C for interpolations zsym= if True (default), the potential is assumed to be symmetric around z=0 (so you can use, e.g., zgrid=(0.,1.,101)). numcores= if set to an integer, use this many cores nazimuths= (4) number of azimuths to average over use_pkdgrav= (False) use PKDGRAV to calculate the snapshot's potential and forces (CURRENTLY NOT IMPLEMENTED) ro=, vo= distance and velocity scales for translation into internal units (default from configuration file) OUTPUT: instance HISTORY: 2013 - Written - Rok Roskar (ETH) 2014-11-24 - Edited for merging into main galpy - Bovy (IAS) """ if not _PYNBODY_LOADED: raise ImportError("The InterpSnapRZShotPotential class is designed to work with pynbody snapshots, which cannot be loaded (probably because it is not installed) -- obtain from pynbody.github.io") # inititalize using the base class Potential.__init__(self,amp=1.0,ro=ro,vo=vo) # other properties if numcores is None: self._numcores= pynbody.config['number_of_threads'] else: self._numcores = numcores self._s = s # Set up azimuthal averaging self._naz= nazimuths self._cosaz= np.cos(np.arange(self._naz,dtype='float')\ /self._naz*2.*np.pi) self._sinaz= np.sin(np.arange(self._naz,dtype='float')\ /self._naz*2.*np.pi) self._zones= np.ones(self._naz) self._zzeros= np.zeros(self._naz) # the interpRZPotential class sets these flags self._enable_c = enable_c self.hasC = True # set up the flags for interpolated quantities # since the potential and force are always calculated together, # set the force interpolations to true if potential is true and # vice versa self._interpPot = interpPot self._interpRforce = self._interpPot self._interpzforce = self._interpPot self._interpvcirc = self._interpPot # these require additional calculations so set them seperately self._interpepifreq = interpepifreq self._interpverticalfreq = interpverticalfreq # make the potential accessible at points beyond the grid self._origPot = SnapshotRZPotential(s, numcores) # setup the grid self._zsym = zsym self._logR = logR self._rgrid = np.linspace(*rgrid) if logR : self._rgrid = np.exp(self._rgrid) self._logrgrid = np.log(self._rgrid) rs = self._logrgrid else : rs = self._rgrid self._zgrid = np.linspace(*zgrid) # calculate the grids self._setup_potential(self._rgrid,self._zgrid,use_pkdgrav=use_pkdgrav) if enable_c and interpPot: self._potGrid_splinecoeffs = interpRZPotential.calc_2dsplinecoeffs_c(self._potGrid) self._rforceGrid_splinecoeffs = interpRZPotential.calc_2dsplinecoeffs_c(self._rforceGrid) self._zforceGrid_splinecoeffs = interpRZPotential.calc_2dsplinecoeffs_c(self._zforceGrid) else : self._potInterp= interpolate.RectBivariateSpline(rs, self._zgrid, self._potGrid, kx=3,ky=3,s=0.) self._rforceInterp= interpolate.RectBivariateSpline(rs, self._zgrid, self._rforceGrid, kx=3,ky=3,s=0.) self._zforceInterp= interpolate.RectBivariateSpline(rs, self._zgrid, self._zforceGrid, kx=3,ky=3,s=0.) if interpepifreq: self._R2interp = interpolate.RectBivariateSpline(rs, self._zgrid, self._R2derivGrid, kx=3,ky=3,s=0.) if interpverticalfreq: self._z2interp = interpolate.RectBivariateSpline(rs, self._zgrid, self._z2derivGrid, kx=3,ky=3,s=0.) if interpepifreq and interpverticalfreq: self._Rzinterp = interpolate.RectBivariateSpline(rs, self._zgrid, self._RzderivGrid, kx=3,ky=3,s=0.) # setup the derived quantities if interpPot: self._vcircGrid = np.sqrt(self._rgrid*(-self._rforceGrid[:,0])) self._vcircInterp = interpolate.InterpolatedUnivariateSpline(rs, self._vcircGrid, k=3) if interpepifreq: self._epifreqGrid = np.sqrt(self._R2derivGrid[:,0] - 3./self._rgrid*self._rforceGrid[:,0]) goodindx= True^np.isnan(self._epifreqGrid) self._epifreqInterp=\ interpolate.InterpolatedUnivariateSpline(rs[goodindx], self._epifreqGrid[goodindx], k=3) self._epigoodindx= goodindx if interpverticalfreq: self._verticalfreqGrid = np.sqrt(np.abs(self._z2derivGrid[:,0])) goodindx= True^np.isnan(self._verticalfreqGrid) self._verticalfreqInterp=\ interpolate.InterpolatedUnivariateSpline(rs[goodindx], self._verticalfreqGrid[goodindx],k=3) self._verticalgoodindx= goodindx
def normalize(self, R0=8.0): """ Normalize all positions by R0 and velocities by Vc(R0). If :class:`~scipy.interpolate.RectBivariateSpline` or :class:`~scipy.interpolate.InterpolatedUnivariateSpline` are used, redefine them for use with the rescaled coordinates. To undo the normalization, call :func:`~galpy.potential.SnapshotPotential.InterpSnapshotPotential.denormalize`. """ Vc0 = self.vcirc(R0) Phi0 = np.abs(self.Rforce(R0, 0.0)) self._normR0 = R0 self._normVc0 = Vc0 self._normPhi0 = Phi0 # rescale the simulation if not isinstance(self._s['pos'].units, NoUnit): self._posunit = self._s['pos'].units self._s['pos'].convert_units('%s kpc' % R0) else: self._posunit = None if not isinstance(self._s['vel'].units, NoUnit): self._velunit = self._s['vel'].units self._s['vel'].convert_units('%s km s**-1' % Vc0) else: self._velunit = None # rescale the grid self._rgrid /= R0 if self._logR: self._logrgrid -= np.log(R0) rs = self._logrgrid else: rs = self._rgrid self._zgrid /= R0 # rescale the potential self._amp /= Phi0 self._savedsplines = {} # rescale anything using splines if not self._enable_c and self._interpPot: for spline, name in zip( [self._potInterp, self._rforceInterp, self._zforceInterp], ["pot", "rforce", "zforce"]): self._savedsplines[name] = spline self._potInterp = interpolate.RectBivariateSpline(rs, self._zgrid, self._potGrid / R0, kx=3, ky=3, s=0.) self._rforceInterp = interpolate.RectBivariateSpline( rs, self._zgrid, self._rforceGrid, kx=3, ky=3, s=0.) self._zforceInterp = interpolate.RectBivariateSpline( rs, self._zgrid, self._zforceGrid, kx=3, ky=3, s=0.) elif self._enable_c and self._interpPot: self._potGrid_splinecoeffs = interpRZPotential.calc_2dsplinecoeffs_c( self._potGrid / R0) if self._interpPot: self._savedsplines['vcirc'] = self._vcircInterp self._vcircInterp = interpolate.InterpolatedUnivariateSpline( rs, self._vcircGrid / Vc0, k=3) if self._interpepifreq: self._savedsplines['R2deriv'] = self._R2interp self._savedsplines['epifreq'] = self._epifreqInterp self._R2interp = interpolate.RectBivariateSpline(rs, self._zgrid, self._R2derivGrid, kx=3, ky=3, s=0.) self._epifreqInterp = interpolate.InterpolatedUnivariateSpline( rs[self._epigoodindx], self._epifreqGrid[self._epigoodindx] / np.sqrt(Phi0 / R0), k=3) if self._interpverticalfreq: self._savedsplines['z2deriv'] = self._z2interp self._savedsplines['verticalfreq'] = self._verticalfreqInterp self._z2interp = interpolate.RectBivariateSpline(rs, self._zgrid, self._z2derivGrid, kx=3, ky=3, s=0.) self._verticalfreqInterp = interpolate.InterpolatedUnivariateSpline( rs[self._verticalgoodindx], self._verticalfreqGrid[self._verticalgoodindx] / np.sqrt(Phi0 / R0), k=3)
def __init__(self, s, rgrid=(np.log(0.01), np.log(20.), 101), zgrid=(0., 1., 101), interpepifreq=False, interpverticalfreq=False, interpPot=True, enable_c=True, logR=True, zsym=True, numcores=None, nazimuths=4, use_pkdgrav=False): """ NAME: __init__ PURPOSE: Initialize an InterpSnapshotRZPotential instance INPUT: s - a simulation snapshot loaded with pynbody rgrid - R grid to be given to linspace as in rs= linspace(*rgrid) zgrid - z grid to be given to linspace as in zs= linspace(*zgrid) logR - if True, rgrid is in the log of R so logrs= linspace(*rgrid) interpPot, interpepifreq, interpverticalfreq= if True, interpolate these functions (interpPot=True also interpolates the R and zforce) enable_c= enable use of C for interpolations zsym= if True (default), the potential is assumed to be symmetric around z=0 (so you can use, e.g., zgrid=(0.,1.,101)). numcores= if set to an integer, use this many cores nazimuths= (4) number of azimuths to average over use_pkdgrav= (False) use PKDGRAV to calculate the snapshot's potential and forces (CURRENTLY NOT IMPLEMENTED) OUTPUT: instance HISTORY: 2013 - Written - Rok Roskar (ETH) 2014-11-24 - Edited for merging into main galpy - Bovy (IAS) """ if not _PYNBODY_LOADED: raise ImportError( "The InterpSnapRZShotPotential class is designed to work with pynbody snapshots, which cannot be loaded (probably because it is not installed) -- obtain from pynbody.github.io" ) # inititalize using the base class Potential.__init__(self, amp=1.0) # other properties if numcores is None: self._numcores = pynbody.config['number_of_threads'] else: self._numcores = numcores self._s = s # Set up azimuthal averaging self._naz = nazimuths self._cosaz= np.cos(np.arange(self._naz,dtype='float')\ /self._naz*2.*np.pi) self._sinaz= np.sin(np.arange(self._naz,dtype='float')\ /self._naz*2.*np.pi) self._zones = np.ones(self._naz) self._zzeros = np.zeros(self._naz) # the interpRZPotential class sets these flags self._enable_c = enable_c self.hasC = True # set up the flags for interpolated quantities # since the potential and force are always calculated together, # set the force interpolations to true if potential is true and # vice versa self._interpPot = interpPot self._interpRforce = self._interpPot self._interpzforce = self._interpPot self._interpvcirc = self._interpPot # these require additional calculations so set them seperately self._interpepifreq = interpepifreq self._interpverticalfreq = interpverticalfreq # make the potential accessible at points beyond the grid self._origPot = SnapshotRZPotential(s, numcores) # setup the grid self._zsym = zsym self._logR = logR self._rgrid = np.linspace(*rgrid) if logR: self._rgrid = np.exp(self._rgrid) self._logrgrid = np.log(self._rgrid) rs = self._logrgrid else: rs = self._rgrid self._zgrid = np.linspace(*zgrid) # calculate the grids self._setup_potential(self._rgrid, self._zgrid, use_pkdgrav=use_pkdgrav) if enable_c and interpPot: self._potGrid_splinecoeffs = interpRZPotential.calc_2dsplinecoeffs_c( self._potGrid) self._rforceGrid_splinecoeffs = interpRZPotential.calc_2dsplinecoeffs_c( self._rforceGrid) self._zforceGrid_splinecoeffs = interpRZPotential.calc_2dsplinecoeffs_c( self._zforceGrid) else: self._potInterp = interpolate.RectBivariateSpline(rs, self._zgrid, self._potGrid, kx=3, ky=3, s=0.) self._rforceInterp = interpolate.RectBivariateSpline( rs, self._zgrid, self._rforceGrid, kx=3, ky=3, s=0.) self._zforceInterp = interpolate.RectBivariateSpline( rs, self._zgrid, self._zforceGrid, kx=3, ky=3, s=0.) if interpepifreq: self._R2interp = interpolate.RectBivariateSpline(rs, self._zgrid, self._R2derivGrid, kx=3, ky=3, s=0.) if interpverticalfreq: self._z2interp = interpolate.RectBivariateSpline(rs, self._zgrid, self._z2derivGrid, kx=3, ky=3, s=0.) if interpepifreq and interpverticalfreq: self._Rzinterp = interpolate.RectBivariateSpline(rs, self._zgrid, self._RzderivGrid, kx=3, ky=3, s=0.) # setup the derived quantities if interpPot: self._vcircGrid = np.sqrt(self._rgrid * (-self._rforceGrid[:, 0])) self._vcircInterp = interpolate.InterpolatedUnivariateSpline( rs, self._vcircGrid, k=3) if interpepifreq: self._epifreqGrid = np.sqrt(self._R2derivGrid[:, 0] - 3. / self._rgrid * self._rforceGrid[:, 0]) goodindx = True - np.isnan(self._epifreqGrid) self._epifreqInterp=\ interpolate.InterpolatedUnivariateSpline(rs[goodindx], self._epifreqGrid[goodindx], k=3) self._epigoodindx = goodindx if interpverticalfreq: self._verticalfreqGrid = np.sqrt(np.abs(self._z2derivGrid[:, 0])) goodindx = True - np.isnan(self._verticalfreqGrid) self._verticalfreqInterp=\ interpolate.InterpolatedUnivariateSpline(rs[goodindx], self._verticalfreqGrid[goodindx],k=3) self._verticalgoodindx = goodindx