maxUncontaminatedDist = 0 for LGdict in LGdicts: if LGdict['contaminationDistance'] > maxUncontaminatedDist: maxUncontaminatedDist = LGdict['contaminationDistance'] bestLG = LGdict if bestLG['mass1'] < bestLG['mass2']: MWindex = bestLG['ind1'] centre = bestLG['cop1'] else: MWindex = bestLG['ind2'] centre = bestLG['cop2'] cop = bestLG['cops'] distances = np.array([physUtils.distance(centre, pos) for pos in cop]) distmask = np.array( [distance < maxdist and distance > mindist for distance in distances]) distances = distances[distmask] relvel = (bestLG['vels'] - bestLG['vels'][MWindex])[distmask] cop = cop[distmask] directions = np.array( [physUtils.sphericalCoordinates(pos - centre) for pos in cop]) radvel = np.array([ physUtils.velocityComponents(relvel[j], cop[j] - centre)[0] for j in range(len(cop)) ]) fitdata = clustering.precomputeDistances(directions) db = DBSCAN(
mass, cop, quiet=True, outputAll=True) LG = LGs[LGfinder.chooseClosestToCentre(LGs, contaminationMask, fullCOP)] if mass[LG[0]] > mass[LG[1]]: centreIndex = LG[1] else: centreIndex = LG[0] centre = cop[centreIndex] centreVel = staticVel[centreIndex] closestContDist = physUtils.findClosestDistance(centre, contaminatedPositions) distances = np.array([physUtils.distance(centre, c) for c in cop]) vel = physUtils.addExpansion(staticVel, cop, centre) # LG mass from MW and andromeda M_big2 = mass[LG[0]] + mass[LG[1]] originalDistances = list(distances) # contamination cut distmask = distances < closestContDist cop = cop[distmask] vel = vel[distmask] distances = distances[distmask] # radial velocities radvel = np.array([
def readAndSave(simulationfiles, datafile, mindist=1.5, maxdist=5.0, eps=1.6, ms=4, scale_eps=False): masses = [] H0s = [] inClusterH0s = [] outClusterH0s = [] zeropoints = [] inClusterZeros = [] outClusterZeros = [] allDispersions = [] clusterDispersions = [] unclusteredDispersions = [] radialVelocities = [] tangentialVelocities = [] LGdistances = [] timingArgumentMasses = [] f = open(simulationfiles, 'r') for simdir in f.readlines(): dirname = simdir.strip() staticVel = filereader.readAllFiles(dirname, "Subhalo/Velocity", 3) mass = filereader.readAllFiles(dirname, "Subhalo/Mass", 1) fullCOP = filereader.readAllFiles(dirname, "Subhalo/CentreOfPotential", 3) FoFcontamination = filereader.readAllFiles(dirname, "FOF/ContaminationCount", 1) groupNumbers = filereader.readAllFiles(dirname, "Subhalo/GroupNumber", 1) fullCOP = fullCOP/h0 # to Mpc # creation of mask with True if there is no contamination in Subhalo contaminationMask = np.asarray([FoFcontamination[int(group)-1]<1 for group in groupNumbers]) # save contaminated haloes for finding closest one contaminatedPositions = fullCOP[contaminationMask==False, :] # elimination of contaminated haloes staticVel = staticVel[contaminationMask,:] mass = mass[contaminationMask] cop = fullCOP[contaminationMask, :] groupNumbers = groupNumbers[contaminationMask] # to physical units mass = mass/h0*1e10 # to M_☉ LGs = LGfinder.findLocalGroup(staticVel, mass, cop, quiet=True, outputAll=True) unmaskedLGs = LGfinder.maskedToUnmasked(LGs, cop, fullCOP) bestLGindex = LGfinder.chooseClosestToCentre(unmaskedLGs, contaminationMask, fullCOP) LG = LGs[bestLGindex] if mass[LG[0]] > mass[LG[1]]: centreIndex = LG[1] else: centreIndex = LG[0] massCentre = physUtils.massCentre(cop[LG[0]], cop[LG[1]], mass[LG[0]], mass[LG[1]]) closestContDist = physUtils.findClosestDistance(massCentre, contaminatedPositions) if closestContDist < maxdist: print("Warning: closest contaminated halo closer than the edge of\ Hubble flow fitting range in simulation " + dirname + ".\nExcluding simulation from analysis.") continue centre = cop[centreIndex] centreVel = staticVel[centreIndex] closestContDist = physUtils.findClosestDistance(centre, contaminatedPositions) distances = np.array([physUtils.distance(centre, c) for c in cop]) vel = physUtils.addExpansion(staticVel, cop, centre) LGrelVel = vel[LG[0]] - vel[LG[1]] LGrelVelComponents = physUtils.velocityComponents(LGrelVel, cop[LG[1]]-cop[LG[0]]) LGdistance = physUtils.distance(cop[LG[0]], cop[LG[1]]) closestContMask = distances < closestContDist distRangeMask = np.array([d < maxdist and d > mindist for d in distances]) # contamination and distance range cut mask = np.logical_and(closestContMask, distRangeMask) cop = cop[mask] vel = vel[mask] distances = distances[mask] # radial velocities radvel = np.array([physUtils.velocityComponents(vel[j] - centreVel, cop[j] - centre)[0] for j in range(len(vel))]) ##### extracting clustering data ##### clusteringDB = clustering.runClustering(cop, centre, ms, eps, meansep=scale_eps) labels = clusteringDB.labels_ uniqueLabels = set(labels) clusterMemberMask = labels != -1 # True for haloes in cluster ## all haloes ## (H0, zero) = simpleFit(distances, radvel) radvelResiduals = np.empty(radvel.shape) for i in range(len(radvel)): radvelResiduals[i] = radvel[i] - (distances[i] - zero) * H0 zeropoints.append(zero) H0s.append(H0) allDispersions.append(np.std(radvelResiduals, ddof=1)) ## outside clusters ## (H0, zero, dispersion) = outClusterFit(clusteringDB, radvel, distances, minHaloes=10) outClusterH0s.append(H0) outClusterZeros.append(zero) unclusteredDispersions.append(dispersion) ## inside clusters ## (H0, zero, dispersion) = inClusterFit(clusteringDB, radvel, distances, minSize=10) inClusterH0s.append(H0) inClusterZeros.append(zero) clusterDispersions.append(dispersion) # LG mass from MW and andromeda M_big2 = mass[LG[0]] + mass[LG[1]] masses.append(M_big2) radialVelocities.append(LGrelVelComponents[0]) tangentialVelocities.append(LGrelVelComponents[1]) LGdistances.append(LGdistance) timingArgumentMass = timingargument.timingArgumentMass(-1 * LGrelVelComponents[0], LGdistance*1000.0, 13.815, G) timingArgumentMasses.append(timingArgumentMass) ##### finalizing data ##### masses = np.array(masses) H0s = np.array(H0s) inClusterH0s = np.array(inClusterH0s) outClusterH0s = np.array(outClusterH0s) zeropoints = np.array(zeropoints) inClusterZeros = np.array(inClusterZeros) outClusterZeros = np.array(outClusterZeros) allDispersions = np.array(allDispersions) clusterDispersions = np.array(clusterDispersions) unclusteredDispersions = np.array(unclusteredDispersions) radialVelocities = np.array(radialVelocities) tangentialVelocities = np.array(tangentialVelocities) LGdistances = np.array(LGdistances) timingArgumentMasses = np.array(timingArgumentMasses) data = np.array([masses, timingArgumentMasses, H0s, inClusterH0s, outClusterH0s, zeropoints, inClusterZeros, outClusterZeros, allDispersions, unclusteredDispersions, clusterDispersions, radialVelocities, tangentialVelocities, LGdistances]).T np.savetxt(datafile, data) return data
def readAndSave(simulationfiles, datafile, mindist=1.5, maxdist=5.0, eps=1.6, ms=4, scale_eps=False): masses = [] H0s = [] inClusterH0s = [] outClusterH0s = [] zeropoints = [] inClusterZeros = [] outClusterZeros = [] allDispersions = [] clusterDispersions = [] unclusteredDispersions = [] radialVelocities = [] tangentialVelocities = [] LGdistances = [] timingArgumentMasses = [] f = open(simulationfiles, 'r') for simdir in f.readlines(): dirname = simdir.strip() staticVel = filereader.readAllFiles(dirname, "Subhalo/Velocity", 3) mass = filereader.readAllFiles(dirname, "Subhalo/Mass", 1) fullCOP = filereader.readAllFiles(dirname, "Subhalo/CentreOfPotential", 3) FoFcontamination = filereader.readAllFiles(dirname, "FOF/ContaminationCount", 1) groupNumbers = filereader.readAllFiles(dirname, "Subhalo/GroupNumber", 1) fullCOP = fullCOP / h0 # to Mpc # creation of mask with True if there is no contamination in Subhalo contaminationMask = np.asarray( [FoFcontamination[int(group) - 1] < 1 for group in groupNumbers]) # save contaminated haloes for finding closest one contaminatedPositions = fullCOP[contaminationMask == False, :] # elimination of contaminated haloes staticVel = staticVel[contaminationMask, :] mass = mass[contaminationMask] cop = fullCOP[contaminationMask, :] groupNumbers = groupNumbers[contaminationMask] # to physical units mass = mass / h0 * 1e10 # to M_☉ LGs = LGfinder.findLocalGroup(staticVel, mass, cop, quiet=True, outputAll=True) unmaskedLGs = LGfinder.maskedToUnmasked(LGs, cop, fullCOP) bestLGindex = LGfinder.chooseClosestToCentre(unmaskedLGs, contaminationMask, fullCOP) LG = LGs[bestLGindex] if mass[LG[0]] > mass[LG[1]]: centreIndex = LG[1] else: centreIndex = LG[0] massCentre = physUtils.massCentre(cop[LG[0]], cop[LG[1]], mass[LG[0]], mass[LG[1]]) closestContDist = physUtils.findClosestDistance( massCentre, contaminatedPositions) if closestContDist < maxdist: print("Warning: closest contaminated halo closer than the edge of\ Hubble flow fitting range in simulation " + dirname + ".\nExcluding simulation from analysis.") continue centre = cop[centreIndex] centreVel = staticVel[centreIndex] closestContDist = physUtils.findClosestDistance( centre, contaminatedPositions) distances = np.array([physUtils.distance(centre, c) for c in cop]) vel = physUtils.addExpansion(staticVel, cop, centre) LGrelVel = vel[LG[0]] - vel[LG[1]] LGrelVelComponents = physUtils.velocityComponents( LGrelVel, cop[LG[1]] - cop[LG[0]]) LGdistance = physUtils.distance(cop[LG[0]], cop[LG[1]]) closestContMask = distances < closestContDist distRangeMask = np.array( [d < maxdist and d > mindist for d in distances]) # contamination and distance range cut mask = np.logical_and(closestContMask, distRangeMask) cop = cop[mask] vel = vel[mask] distances = distances[mask] # radial velocities radvel = np.array([ physUtils.velocityComponents(vel[j] - centreVel, cop[j] - centre)[0] for j in range(len(vel)) ]) ##### extracting clustering data ##### clusteringDB = clustering.runClustering(cop, centre, ms, eps, meansep=scale_eps) labels = clusteringDB.labels_ uniqueLabels = set(labels) clusterMemberMask = labels != -1 # True for haloes in cluster ## all haloes ## (H0, zero) = simpleFit(distances, radvel) radvelResiduals = np.empty(radvel.shape) for i in range(len(radvel)): radvelResiduals[i] = radvel[i] - (distances[i] - zero) * H0 zeropoints.append(zero) H0s.append(H0) allDispersions.append(np.std(radvelResiduals, ddof=1)) ## outside clusters ## (H0, zero, dispersion) = outClusterFit(clusteringDB, radvel, distances, minHaloes=10) outClusterH0s.append(H0) outClusterZeros.append(zero) unclusteredDispersions.append(dispersion) ## inside clusters ## (H0, zero, dispersion) = inClusterFit(clusteringDB, radvel, distances, minSize=10) inClusterH0s.append(H0) inClusterZeros.append(zero) clusterDispersions.append(dispersion) # LG mass from MW and andromeda M_big2 = mass[LG[0]] + mass[LG[1]] masses.append(M_big2) radialVelocities.append(LGrelVelComponents[0]) tangentialVelocities.append(LGrelVelComponents[1]) LGdistances.append(LGdistance) timingArgumentMass = timingargument.timingArgumentMass( -1 * LGrelVelComponents[0], LGdistance * 1000.0, 13.815, G) timingArgumentMasses.append(timingArgumentMass) ##### finalizing data ##### masses = np.array(masses) H0s = np.array(H0s) inClusterH0s = np.array(inClusterH0s) outClusterH0s = np.array(outClusterH0s) zeropoints = np.array(zeropoints) inClusterZeros = np.array(inClusterZeros) outClusterZeros = np.array(outClusterZeros) allDispersions = np.array(allDispersions) clusterDispersions = np.array(clusterDispersions) unclusteredDispersions = np.array(unclusteredDispersions) radialVelocities = np.array(radialVelocities) tangentialVelocities = np.array(tangentialVelocities) LGdistances = np.array(LGdistances) timingArgumentMasses = np.array(timingArgumentMasses) data = np.array([ masses, timingArgumentMasses, H0s, inClusterH0s, outClusterH0s, zeropoints, inClusterZeros, outClusterZeros, allDispersions, unclusteredDispersions, clusterDispersions, radialVelocities, tangentialVelocities, LGdistances ]).T np.savetxt(datafile, data) return data
# find LG analogue that is closest to the centre of the high res area maxUncontaminatedDist = 0 for LGdict in LGdicts: if LGdict['contaminationDistance'] > maxUncontaminatedDist: maxUncontaminatedDist = LGdict['contaminationDistance'] d = LGdict centre = d['centre'] if d['mass1'] < d['mass2']: centre = d['cop1'] else: centre = d['cop2'] cop = d['cops'] distances = np.array([physUtils.distance(centre, pos) for pos in cop]) if max(distances) < 5.0: print("distance criteria failed") continue distmask = np.array([distance < 5.0 and distance > 1.5 for distance in distances]) distances = distances[distmask] cop = cop[distmask] directions = np.array([physUtils.sphericalCoordinates(pos - centre) for pos in cop]) fitdata = clustering.precomputeDistances(directions) meansep = np.mean([min(x[x>0]) for x in fitdata])
if mass[LG[0]] > mass[LG[1]]: centreIndex = LG[1] else: centreIndex = LG[0] centre = cop[centreIndex] centreVel = staticVel[centreIndex] massCentre = physUtils.massCentre(cop[LG[0]], cop[LG[1]], mass[LG[0]], mass[LG[1]]) closestContDist = physUtils.findClosestDistance( massCentre, contaminatedPositions) if closestContDist < maxdist: continue distances = np.array([physUtils.distance(centre, c) for c in cop]) vel = physUtils.addExpansion(staticVel, cop, centre) LGrelVel = vel[LG[0]] - vel[LG[1]] LGrelVelComponents = physUtils.velocityComponents( LGrelVel, cop[LG[1]] - cop[LG[0]]) LGdistance = physUtils.distance(cop[LG[0]], cop[LG[1]]) mask = np.array([d < maxdist and d > mindist for d in distances]) cop = cop[mask] vel = vel[mask] distances = distances[mask] mass = mass[mask] # radial velocities radvel = np.array([
andromedaIndex = LG[1] MWcop = cop[MWindex] # massCentre = physUtils.massCentre(cop[MWindex], cop[andromedaIndex], # mass[MWindex], mass[andromedaIndex]) closestContDist = physUtils.findClosestDistance(MWcop, contaminatedPositions) if closestContDist < maxdist: excludedSims += 1 print("excluding " + simdir) continue LGvector = cop[andromedaIndex] - cop[MWindex] centreVel = staticVel[MWindex] distances = np.array([physUtils.distance(MWcop, c) for c in cop]) vel = physUtils.addExpansion(staticVel, cop, MWcop) LGrelVel = vel[LG[0]] - vel[LG[1]] LGrelVelComponents = physUtils.velocityComponents(LGrelVel, cop[LG[1]]-cop[LG[0]]) LGdistance = physUtils.distance(cop[LG[0]], cop[LG[1]]) mask = np.array([d < maxdist and d >= mindist for d in distances]) cop = cop[mask] vel = vel[mask] distances = distances[mask] radvel = np.array([physUtils.velocityComponents(vel[j] - centreVel, cop[j] - MWcop)[0]
groupNumbers = groupNumbers[contaminationMask] LGs = LGfinder.findLocalGroup(staticVel, mass, cop, quiet=True, outputAll=True) LG = LGs[LGfinder.chooseClosestToCentre(LGs, contaminationMask, fullCOP)] if mass[LG[0]] > mass[LG[1]]: centreIndex = LG[1] else: centreIndex = LG[0] centre = cop[centreIndex] centreVel = staticVel[centreIndex] closestContDist = physUtils.findClosestDistance(centre, contaminatedPositions) distances = np.array([physUtils.distance(centre, c) for c in cop]) vel = physUtils.addExpansion(staticVel, cop, centre) # LG mass from MW and andromeda M_big2 = mass[LG[0]] + mass[LG[1]] originalDistances = list(distances) # contamination cut distmask = distances < closestContDist cop = cop[distmask] vel = vel[distmask] distances = distances[distmask] # radial velocities radvel = np.array([physUtils.velocityComponents(vel[j] - centreVel,
massCentre = cop[main1ind] massCentreVel = vel[main1ind] # expansion vel = physUtils.addExpansion(vel, cop, massCentre) dist = np.zeros(len(vel)) speed = np.zeros(len(vel)) # find most distant contaminated closestContDistance = physUtils.findClosestDistance(massCentre, contaminatedPositions) # distances and velocities relative to centre for i in range(len(vel)): dist[i] = physUtils.distance(cop[i], massCentre) relVel = vel[i]-massCentreVel # velocity relative to movement of the mass CentreOfPotential diffInLocation = cop[i]-massCentre # vector defining difference in position speed[i] = physUtils.velocityComponents(relVel, diffInLocation)[0] # radial velocity colours = np.zeros((len(vel), 4)) colours[:, 3] = 1 ax.scatter(dist[dist<closestContDistance], speed[dist<closestContDistance], color=colours[dist<closestContDistance], s=1) #ax.set_xlim([0,closestContDistance]) ax.set_xlim([0, 6.1]) ax.set_ylim([-700, 900])
centreIndex = LG[1] else: centreIndex = LG[0] centre = cop[centreIndex] centreVel = staticVel[centreIndex] vel = physUtils.addExpansion(staticVel, cop, centre) LGrelVel = vel[LG[0]] - vel[LG[1]] (rad, tan) = physUtils.velocityComponents(LGrelVel, cop[LG[1]] - cop[LG[0]]) radvel[sim] = rad tanvel[sim] = tan distance[sim] = physUtils.distance(cop[LG[0]], cop[LG[1]]) totalmass[sim] = masses[LG[0]] + masses[LG[1]] massdifference[sim] = math.fabs(masses[LG[0]] - masses[LG[1]]) bighalomasses[sim] = max(masses[LG[0]], masses[LG[1]]) smallhalomasses[sim] = min(masses[LG[0]], masses[LG[1]]) distances = np.array([physUtils.distance(centre, pos) for pos in cop]) mass2mpc = sum(masses[np.where(distances <= 2)]) overdensity2mpc[sim] = (mass2mpc / (4.0 / 3.0 * math.pi * (2**3))) / critical_density #### plotting ##### rc('font', **{'family': 'serif', 'serif': ['Palatino']}) rc('text', usetex=True) rcParams['text.latex.preamble'] = [r'\usepackage{wasysym}'] # rcParams.update({'font.size': 13})