def _initUnits(self,cf,pf,opt): def loadOld(): values = pf.getValue(['UnitMass_in_g','UnitLength_in_cm','UnitVelocity_in_cm_per_s','UnitPhotons_per_s']) return { 'mass': values['UnitMass_in_g'], 'length': values['UnitLength_in_cm'], 'velocity': values['UnitVelocity_in_cm_per_s'], 'flux': values['UnitPhotons_per_s'] } if opt is True: self.units = apy.units(old=loadOld()) elif isinstance(opt,dict): old = opt['old'] if 'old' in opt else loadOld() new = opt['new'] if 'new' in opt else None self.units = apy.units(old=old,new=new) else: self.units = None
def getUnits(self, newUnits=None, comoving=False): names = [ 'UnitMass_in_g', 'UnitLength_in_cm', 'UnitVelocity_in_cm_per_s' ] if self.opt['comoving'] or comoving: names += ['HubbleParam', 'ExpansionFactor'] values = self.getHeader(names) units = {'mass': values[0], 'length': values[1], 'velocity': values[2]} if self.opt['comoving'] or comoving: units['h'] = values[3] units['a'] = values[4] return apy.units(oldUnits=units, newUnits=newUnits)
def __init__(self,sim,snap,cut,units): self.sim = sim self.snap = snap apy.shell.printc('Cutting simulation "%s"'%sim.name,'g') self.units = apy.units({ 'length': sim.units['length'], 'mass': sim.units['mass'], 'velocity':sim.units['velocity'], },units) print( 'Length: %-15s -> %s'%(self.units.guess('length',utype='old'), self.units.guess('length',utype='new')) ) print( 'Mass: %-15s -> %s'%(self.units.guess('mass',utype='old'), self.units.guess('mass',utype='new')) ) print( 'Velocity: %-15s -> %s'%(self.units.guess('time',utype='old'), self.units.guess('time',utype='new')) ) self.cut = cut # in old units print( 'Cutting time: %-15s = %s'%(self.cut['time'][0],self.units.guess('time',self.cut['time'][0],'old')) ) print( 'New TimeMax: %-15s = %s'%(self.cut['time'][1],self.units.guess('time',self.cut['time'][1],'old')) ) print( 'Coordinates: ', self.cut['coord'] )
def __init__(self,units,density=None,**header): """Initial Conditions :param dict/int units: Dictionary with units or parameter file name :param callable/[float]/float density: Density function, list or a scalar in code units :param dict header: Additional header settings """ apy.shell.printc('Creating initial conditions') # set density self.density = density # code units # default header settings self.header = { 'NumPart_ThisFile': [0]*6, 'NumPart_Total': [0]*6, 'NumPart_Total_HighWord': [0]*6, 'MassTable': [0.0]*6, 'Redshift': 0.0, 'BoxSize': 1.0, # in code units 'NumFilesPerSnapshot': 1, 'Omega0': 0.0, 'OmegaLambda': 0.0, 'OmegaBaryon': 0.0, 'HubbleParam': 1.0, 'Flag_Sfr': 0, 'Flag_Cooling': 0, 'Flag_StellarAge': 0, 'Flag_Metals': 0, 'Flag_Feedback': 0, 'Flag_DoublePrecision': 1, 'Composition_vector_length': 0, 'UnitLength_in_cm': 1, 'UnitMass_in_g': 1, 'UnitVelocity_in_cm_per_s': 1, 'Time': 0, } # set units and box size if isinstance(units,str): print('Parameters set from: '+units) with apy.files.param(units) as f: params = f.getValue([ 'BoxSize', 'UnitMass_in_g', 'UnitLength_in_cm', 'UnitVelocity_in_cm_per_s', 'ReferenceGasPartMass' ]) self.header['BoxSize'] = params['BoxSize'] self.header['UnitLength_in_cm'] = params['UnitLength_in_cm'] self.header['UnitMass_in_g'] = params['UnitMass_in_g'] self.header['UnitVelocity_in_cm_per_s'] = params['UnitVelocity_in_cm_per_s'] self.units = apy.units({ 'mass': self.header['UnitMass_in_g'], 'length': self.header['UnitLength_in_cm'], 'velocity': self.header['UnitVelocity_in_cm_per_s'], }) self.other = { 'ReferenceGasPartMass': params['ReferenceGasPartMass'] } else: print('Parameters set by hand') self.units = units self.header['UnitLength_in_cm'] = self.units['length'] self.header['UnitMass_in_g'] = self.units['mass'] self.header['UnitVelocity_in_cm_per_s'] = self.units['velocity'] self.other = {} # set additional headers before prints self.header.update(header) print('Units length ', self.units['length'], 'cm =', self.units.guess('length',1) ) print(' mass ', self.units['mass'], 'g =', self.units.guess('mass',1) ) print(' velocity', self.units['velocity'], 'cm/s ~', self.units.guess('time',1) ) boxSize = self.header['BoxSize'] boxVol = boxSize**3 print('Box size %.03e = %.03e cm = '%(boxSize, boxSize*self.units['length']), self.units.guess('length',boxSize)) print(' volume %.03e = %.03e cm^3 = '%(boxVol, boxVol*self.units['volume']), self.units.guess('volume',boxVol)) if 'ReferenceGasPartMass' in self.other: rmass = self.other['ReferenceGasPartMass'] print('Other:') print('ReferenceGasPartMass %.03e = %.03e g = '%(rmass,rmass*self.units['mass']), self.units.guess('mass',rmass) )
def show(self, leap, msg): step = time.time() - self.get(leap) timestr = apy.units('cgs').guess('time', step) apy.shell.printc('%s%s' % (msg, timestr))