def main(): pm = ParticleMesh(ns.BoxSize, ns.Nmesh, dtype='f4') if pm.comm.rank == 0: hf = files.HaloFile(ns.halocatalogue) nhalo = hf.nhalo halopos = hf.read_pos() halopos = pm.comm.bcast(halopos) else: halopos = pm.comm.bcast(None) Ntot = 0 for round, (P, PL) in enumerate(izip( files.read(pm.comm, ns.filename, files.TPMSnapshotFile, columns=['Position'], bunchsize=ns.bunchsize), files.read(pm.comm, ns.halolabel, files.HaloLabelFile, columns=['Label'], bunchsize=ns.bunchsize), )): mask = PL['Label'] != 0 logging.info("Number of particles in halos is %d" % mask.sum()) P['Position'][mask] = halopos[PL['Label'][mask]] P['Position'] *= ns.BoxSize layout = pm.decompose(P['Position']) tpos = layout.exchange(P['Position']) #print tpos.shape pm.paint(tpos) npaint = pm.comm.allreduce(len(tpos), op=MPI.SUM) nread = pm.comm.allreduce(len(P['Position']), op=MPI.SUM) if pm.comm.rank == 0: logging.info('round %d, npaint %d, nread %d' % (round, npaint, nread)) Ntot = Ntot + nread if ns.remove_shotnoise: shotnoise = pm.BoxSize ** 3 / Ntot else: shotnoise = 0 pm.r2c() k, p = measurepower(pm, pm.complex, ns.binshift, ns.remove_cic, shotnoise) if pm.comm.rank == 0: if ns.output != '-': myout = open(ns.output, 'w') else: myout = stdout numpy.savetxt(myout, zip(k, p), '%0.7g') myout.flush() hf = files.HaloFile(ns.halocatalogue) mass = hf.read_mass() M1 = ((mass[1:] * 1.0)** 2).sum(dtype='f8') / (1.0 * Ntot) ** 2 * ns.BoxSize ** 3 logging.info("p[-inf] = %g, M1 = %g" % (p[-1], M1))
def parallel_read(self, columns, full=False): if comm.rank == 0: hf = files.HaloFile(self.pathhalo) nhalo = hf.nhalo halopos = numpy.float32(hf.read_pos()) halomass = numpy.float32(hf.read_mass() * self.m0) logmass = numpy.log10(halomass) halomask = logmass > self.logMmin halomask &= logmass < self.logMmax self.logger.info("total number of halos in mass range is %d" % halomask.sum()) else: halopos = None halomask = None halopos = comm.bcast(halopos) halomask = comm.bcast(halomask) for round, (P, PL) in enumerate( izip( files.read(comm, self.pathmatter, files.TPMSnapshotFile, columns=['Position', 'Velocity'], bunchsize=ns.bunchsize), files.read(comm, self.pathlabel, files.HaloLabelFile, columns=['Label'], bunchsize=ns.bunchsize), )): mask = PL['Label'] != 0 mask &= halomask[PL['Label']] self.logger.info("Number of particles in halos is %d" % mask.sum()) P['Position'][mask] = halopos[PL['Label'][mask]] if self.rsd is not None: dir = 'xyz'.index(self.rsd) P['Position'][:, dir] += P['Velocity'][:, dir] P['Position'][:, dir] %= 1.0 # enforce periodic boundary conditions P['Position'] *= self.BoxSize P['Velocity'] *= self.BoxSize if comm.rank == 0: self.logger.info('round %d, nread %d' % (round, nread)) yield [P.get(key, None) for key in columns]
def paint(self, ns, pm): if pm.comm.rank == 0: hf = files.HaloFile(self.pathhalo) nhalo = hf.nhalo halopos = numpy.float32(hf.read_pos()) halomass = numpy.float32(hf.read_mass() * self.m0) logmass = numpy.log10(halomass) halomask = logmass > self.logMmin halomask &= logmass < self.logMmax logging.info("total number of halos in mass range is %d" % halomask.sum()) else: halopos = None halomask = None halopos = pm.comm.bcast(halopos) halomask = pm.comm.bcast(halomask) Ntot = 0 for round, (P, PL) in enumerate(izip( files.read(pm.comm, self.pathmatter, files.TPMSnapshotFile, columns=['Position', 'Velocity'], bunchsize=ns.bunchsize), files.read(pm.comm, self.pathlabel, files.HaloLabelFile, columns=['Label'], bunchsize=ns.bunchsize), )): mask = PL['Label'] != 0 mask &= halomask[PL['Label']] logging.info("Number of particles in halos is %d" % mask.sum()) P['Position'][mask] = halopos[PL['Label'][mask]] if self.rsd is not None: dir = 'xyz'.index(self.rsd) P['Position'][:, dir] += P['Velocity'][:, dir] P['Position'][:, dir] %= 1.0 # enforce periodic boundary conditions P['Position'] *= ns.BoxSize layout = pm.decompose(P['Position']) tpos = layout.exchange(P['Position']) #print tpos.shape pm.paint(tpos) npaint = pm.comm.allreduce(len(tpos)) nread = pm.comm.allreduce(len(P['Position'])) if pm.comm.rank == 0: logging.info('round %d, npaint %d, nread %d' % (round, npaint, nread)) Ntot = Ntot + nread npaint = pm.comm.allreduce(len(tpos)) return Ntot
def read(self, columns, comm, bunchsize): Ntot = 0 # avoid reading Velocity if RSD is not requested. # this is only needed for large data like a TPMSnapshot # for small Pandas reader etc it doesn't take time to # read velocity if self.rsd is not None: newcolumns = set(columns + ['Velocity']) else: newcolumns = set(columns) if 'Mass' in newcolumns: newcolumns.remove('Mass') for round, P in enumerate( files.read(comm, self.path, files.TPMSnapshotFile, columns=newcolumns, bunchsize=bunchsize)): if 'Position' in P: P['Position'] *= self.BoxSize P['Mass'] = None if 'Velocity' in P: P['Velocity'] *= self.BoxSize if self.rsd is not None: dir = "xyz".index(self.rsd) P['Position'][:, dir] += P['Velocity'][:, dir] P['Position'][:, dir] %= self.BoxSize[dir] yield [P[key] for key in columns]
def parallel_read(self, columns, full=False): if comm.rank == 0: hf = files.HaloFile(self.pathhalo) nhalo = hf.nhalo halopos = numpy.float32(hf.read_pos()) halomass = numpy.float32(hf.read_mass() * self.m0) logmass = numpy.log10(halomass) halomask = logmass > self.logMmin halomask &= logmass < self.logMmax self.logger.info("total number of halos in mass range is %d" % halomask.sum()) else: halopos = None halomask = None halopos = comm.bcast(halopos) halomask = comm.bcast(halomask) for round, (P, PL) in enumerate( izip( files.read( comm, self.pathmatter, files.TPMSnapshotFile, columns=["Position", "Velocity"], bunchsize=ns.bunchsize, ), files.read(comm, self.pathlabel, files.HaloLabelFile, columns=["Label"], bunchsize=ns.bunchsize), ) ): mask = PL["Label"] != 0 mask &= halomask[PL["Label"]] self.logger.info("Number of particles in halos is %d" % mask.sum()) P["Position"][mask] = halopos[PL["Label"][mask]] if self.rsd is not None: dir = "xyz".index(self.rsd) P["Position"][:, dir] += P["Velocity"][:, dir] P["Position"][:, dir] %= 1.0 # enforce periodic boundary conditions P["Position"] *= self.BoxSize P["Velocity"] *= self.BoxSize if comm.rank == 0: self.logger.info("round %d, nread %d" % (round, nread)) yield [P.get(key, None) for key in columns]
def read(self, columns, comm, bunchsize): if comm.rank == 0: hf = files.HaloFile(self.pathhalo) nhalo = hf.nhalo halopos = numpy.float32(hf.read_pos()) halomass = numpy.float32(hf.read_mass() * self.m0) logmass = numpy.log10(halomass) halomask = logmass > self.logMmin halomask &= logmass < self.logMmax logger.info("total number of halos in mass range is %d" % halomask.sum()) else: halopos = None halomask = None halopos = comm.bcast(halopos) halomask = comm.bcast(halomask) for round, (P, PL) in enumerate(izip( files.read(comm, self.pathmatter, files.TPMSnapshotFile, columns=['Position', 'Velocity'], bunchsize=ns.bunchsize), files.read(comm, self.pathlabel, files.HaloLabelFile, columns=['Label'], bunchsize=ns.bunchsize), )): mask = PL['Label'] != 0 mask &= halomask[PL['Label']] logger.info("Number of particles in halos is %d" % mask.sum()) P['Position'][mask] = halopos[PL['Label'][mask]] if self.rsd is not None: dir = 'xyz'.index(self.rsd) P['Position'][:, dir] += P['Velocity'][:, dir] P['Position'][:, dir] %= 1.0 # enforce periodic boundary conditions P['Position'] *= self.BoxSize P['Velocity'] *= self.BoxSize P['Mass'] = None if comm.rank == 0: logger.info('round %d, nread %d' % (round, nread)) yield [P[key] for key in columns]
def paint_darkmatter(pm, filename, fileformat): pm.real[:] = 0 Ntot = 0 for round, P in enumerate(read(pm.comm, filename, TPMSnapshotFile, columns=["Position"], bunchsize=ns.bunchsize)): P["Position"] *= ns.BoxSize layout = pm.decompose(P["Position"]) tpos = layout.exchange(P["Position"]) # print tpos.shape pm.paint(tpos) npaint = pm.comm.allreduce(len(tpos), op=MPI.SUM) nread = pm.comm.allreduce(len(P["Position"]), op=MPI.SUM) if pm.comm.rank == 0: logging.info("round %d, npaint %d, nread %d" % (round, npaint, nread)) Ntot = Ntot + nread return Ntot
def paint(self, ns, pm): pm.real[:] = 0 Ntot = 0 columns = ["Position"] if self.rsd is not None or self.mom is not None: columns.append("Velocity") for round, P in enumerate( files.read(pm.comm, self.path, files.TPMSnapshotFile, columns=columns, bunchsize=ns.bunchsize) ): nread = pm.comm.allreduce(len(P["Position"])) if self.rsd is not None: dir = "xyz".index(self.rsd) P["Position"][:, dir] += P["Velocity"][:, dir] P["Position"][:, dir] %= 1.0 # enforce periodic boundary conditions P["Position"] *= ns.BoxSize layout = pm.decompose(P["Position"]) P["Position"] = layout.exchange(P["Position"]) npaint = pm.comm.allreduce(len(P["Position"])) if self.mom is not None: dir = "xyz".index(self.mom) weight = 1.0 + P["Velocity"][:, dir].copy() del P["Velocity"] weight = layout.exchange(weight) else: # uniform mass weight = 1 del layout pm.paint(P["Position"], weight) del P del weight if pm.comm.rank == 0: logging.info("round %d, npaint %d, nread %d" % (round, npaint, nread)) Ntot = Ntot + nread return Ntot
def paint_darkmatter(pm, filename, fileformat): pm.real[:] = 0 Ntot = 0 for round, P in enumerate( read(pm.comm, filename, TPMSnapshotFile, columns=['Position'], bunchsize=ns.bunchsize)): P['Position'] *= ns.BoxSize layout = pm.decompose(P['Position']) tpos = layout.exchange(P['Position']) #print tpos.shape pm.paint(tpos) npaint = pm.comm.allreduce(len(tpos), op=MPI.SUM) nread = pm.comm.allreduce(len(P['Position']), op=MPI.SUM) if pm.comm.rank == 0: logging.info('round %d, npaint %d, nread %d' % (round, npaint, nread)) Ntot = Ntot + nread return Ntot
def main(): comm = MPI.COMM_WORLD np = split_size_2d(comm.size) grid = [ numpy.linspace(0, 1.0, np[0] + 1, endpoint=True), numpy.linspace(0, 1.0, np[1] + 1, endpoint=True), ] domain = GridND(grid) if comm.rank == 0: logging.info('grid %s' % str(grid) ) [P] = read(comm, ns.filename, TPMSnapshotFile, columns=['Position', 'ID']) tpos = P['Position'] tid = P['ID'] del P Ntot = sum(comm.allgather(len(tpos))) if comm.rank == 0: logging.info('Total number of particles %d, ll %g' % (Ntot, ns.LinkingLength)) ll = ns.LinkingLength * Ntot ** -0.3333333 #print pos #print ((pos[0] - pos[1]) ** 2).sum()** 0.5, ll layout = domain.decompose(tpos, smoothing=ll * 1) tpos = layout.exchange(tpos) tid = layout.exchange(tid) logging.info('domain %d has %d particles' % (comm.rank, len(tid))) data = cluster.dataset(tpos, boxsize=1.0) fof = cluster.fof(data, linking_length=ll, np=0, verbose=True) # initialize global labels minid = equiv_class(fof.labels, tid, op=numpy.fmin)[fof.labels] del fof del data del tpos del tid while True: # merge, if a particle belongs to several ranks # use the global label of the minimal minid_new = layout.gather(minid, mode=numpy.fmin) minid_new = layout.exchange(minid_new) # on my rank, these particles have been merged merged = minid_new != minid # if no rank has merged any, we are done # gl is the global label (albeit with some holes) total = comm.allreduce(merged.sum()) if comm.rank == 0: print 'merged ', total, 'halos' if total == 0: del minid_new break old = minid[merged] new = minid_new[merged] arg = old.argsort() new = new[arg] old = old[arg] replacesorted(minid, old, new, out=minid) minid = layout.gather(minid, mode=numpy.fmin) label = assign_halo_label(minid, comm, thresh=ns.nmin) N = halos.count(label, comm=comm) if comm.rank == 0: print 'total halos is', len(N) [P] = read(comm, ns.filename, TPMSnapshotFile, columns=['Position']) hpos = halos.centerofmass(label, P['Position'], boxsize=1.0, comm=comm) [P] = read(comm, ns.filename, TPMSnapshotFile, columns=['Velocity']) hvel = halos.centerofmass(label, P['Velocity'], boxsize=None, comm=comm) if comm.rank == 0: print N print 'total groups', N.shape print 'total particles', N.sum() print 'above ', ns.nmin, (N >ns.nmin).sum() N[0] = -1 with open(ns.output + '.halo', 'w') as ff: numpy.int32(len(N)).tofile(ff) numpy.float32(ns.LinkingLength).tofile(ff) numpy.int32(N).tofile(ff) numpy.float32(hpos).tofile(ff) numpy.float32(hvel).tofile(ff) print hpos del N del hpos npart = None if comm.rank == 0: snapshot = Snapshot(ns.filename,TPMSnapshotFile) npart = snapshot.npart for i in range(len(snapshot.npart)): with open(ns.output + '.grp.%02d' % i, 'w') as ff: numpy.int32(npart[i]).tofile(ff) numpy.float32(ns.LinkingLength).tofile(ff) pass npart = comm.bcast(npart) start = sum(comm.allgather(len(label))[:comm.rank]) end = sum(comm.allgather(len(label))[:comm.rank+1]) label = numpy.int32(label) written = 0 for i in range(len(npart)): filestart = sum(npart[:i]) fileend = sum(npart[:i+1]) mystart = start - filestart myend = end - filestart if myend <= 0 : continue if mystart >= npart[i] : continue if myend > npart[i]: myend = npart[i] if mystart < 0: mystart = 0 with open(ns.output + '.grp.%02d' % i, 'r+') as ff: ff.seek(8, 0) ff.seek(mystart * 4, 1) label[written:written + myend - mystart].tofile(ff) written += myend - mystart return
def main(): comm = MPI.COMM_WORLD if comm.rank == 0: h = files.HaloFile(ns.halocatalogue) #print h.read_pos().shape() N = h.read('Mass') halo_pos = h.read('Position') else: N = None halo_pos = None N = comm.bcast(N) halo_pos = comm.bcast(halo_pos) halo_mass = N * ns.m0 halo_ind = numpy.digitize(numpy.log10(halo_mass), ns.logMedges) Nhalo_per_massbin = numpy.bincount(halo_ind, minlength=len(ns.logMedges) + 1) redges = numpy.linspace(0, ns.Rmax / ns.boxsize, ns.Rbins + 2, endpoint=True)[1:] den_prof = numpy.zeros((ns.Rbins, len(ns.logMedges) - 1)) count_prof = numpy.zeros((ns.Rbins + 2, len(ns.logMedges) + 1)) Ntot = 0 for round, (P, PL) in enumerate( zip( files.read(comm, ns.snapfilename, files.TPMSnapshotFile, columns=['Position', 'Velocity'], bunchsize=ns.bunchsize), files.read(comm, ns.halolabel, files.HaloLabelFile, columns=['Label'], bunchsize=ns.bunchsize), )): m_ind = halo_ind[PL['Label']] center = halo_pos[PL['Label']] dist = distp(center, P['Position'], 1.0) d_ind = numpy.digitize(dist, redges) ind = numpy.ravel_multi_index((d_ind, m_ind), count_prof.shape) count_prof.flat += numpy.bincount(ind, minlength=count_prof.size) Ntot += len(PL['Label']) Ntot = comm.allreduce(Ntot) count_prof = comm.allreduce(count_prof) count_prof = numpy.cumsum(count_prof, axis=0) vol = 4 * numpy.pi / 3. * redges**3 # this is over density averaged over all halos in this mass bin den_prof = count_prof[:-1, 1:-1] / vol[:, None] / \ Nhalo_per_massbin[None, 1:-1] / Ntot - 1 if comm.rank == 0: if ns.output != '-': ff = open(ns.output, 'w') else: ff = stdout with ff: ff.write('# nhalo_per_massbin: %s\n' % ' '.join([str(n) for n in Nhalo_per_massbin])) ff.write('# edges of the mass bins %s\n' % ' '.join([str(m) for m in ns.logMedges])) ff.write('# r cumulative_dens_prof\n') numpy.savetxt( ff, numpy.concatenate((redges[:, None] * ns.boxsize, den_prof), axis=-1))
def main(): comm = MPI.COMM_WORLD if comm.rank == 0: h = files.HaloFile(ns.halocatalogue) #print h.read_pos().shape() N = h.read('Mass') halo_pos = h.read('Position') else: N = None halo_pos = None N = comm.bcast(N) halo_pos = comm.bcast(halo_pos) halo_mass = N * ns.m0 halo_ind = numpy.digitize(numpy.log10(halo_mass), ns.logMedges) Nhalo_per_massbin = numpy.bincount(halo_ind, minlength=len(ns.logMedges) + 1) redges = numpy.linspace(0, ns.Rmax / ns.boxsize, ns.Rbins + 2, endpoint=True)[1:] den_prof = numpy.zeros((ns.Rbins, len(ns.logMedges) - 1)) count_prof = numpy.zeros((ns.Rbins + 2, len(ns.logMedges) + 1)) Ntot = 0 for round, (P, PL) in enumerate( zip(files.read(comm, ns.snapfilename, files.TPMSnapshotFile, columns=['Position', 'Velocity'], bunchsize=ns.bunchsize), files.read(comm, ns.halolabel, files.HaloLabelFile, columns=['Label'], bunchsize=ns.bunchsize), )): m_ind = halo_ind[PL['Label']] center = halo_pos[PL['Label']] dist = distp(center, P['Position'], 1.0) d_ind = numpy.digitize(dist, redges) ind = numpy.ravel_multi_index((d_ind, m_ind), count_prof.shape) count_prof.flat += numpy.bincount(ind, minlength=count_prof.size) Ntot += len(PL['Label']) Ntot = comm.allreduce(Ntot) count_prof = comm.allreduce(count_prof) count_prof = numpy.cumsum(count_prof, axis=0) vol = 4 * numpy.pi / 3. * redges ** 3 # this is over density averaged over all halos in this mass bin den_prof = count_prof[:-1, 1:-1] / vol[:, None] / \ Nhalo_per_massbin[None, 1:-1] / Ntot - 1 if comm.rank == 0: if ns.output != '-': ff = open(ns.output, 'w') else: ff = stdout with ff: ff.write('# nhalo_per_massbin: %s\n' % ' '.join([str(n) for n in Nhalo_per_massbin])) ff.write('# edges of the mass bins %s\n' % ' '.join([str(m) for m in ns.logMedges]) ) ff.write('# r cumulative_dens_prof\n') numpy.savetxt(ff, numpy.concatenate( (redges[:, None] * ns.boxsize, den_prof), axis=-1))
def main(): pm = ParticleMesh(ns.BoxSize, ns.Nmesh, dtype='f4') if pm.comm.rank == 0: hf = files.HaloFile(ns.halocatalogue) nhalo = hf.nhalo halopos = hf.read_pos() halopos = pm.comm.bcast(halopos) else: halopos = pm.comm.bcast(None) Ntot = 0 for round, (P, PL) in enumerate( izip( files.read(pm.comm, ns.filename, files.TPMSnapshotFile, columns=['Position'], bunchsize=ns.bunchsize), files.read(pm.comm, ns.halolabel, files.HaloLabelFile, columns=['Label'], bunchsize=ns.bunchsize), )): mask = PL['Label'] != 0 logging.info("Number of particles in halos is %d" % mask.sum()) P['Position'][mask] = halopos[PL['Label'][mask]] P['Position'] *= ns.BoxSize layout = pm.decompose(P['Position']) tpos = layout.exchange(P['Position']) #print tpos.shape pm.paint(tpos) npaint = pm.comm.allreduce(len(tpos), op=MPI.SUM) nread = pm.comm.allreduce(len(P['Position']), op=MPI.SUM) if pm.comm.rank == 0: logging.info('round %d, npaint %d, nread %d' % (round, npaint, nread)) Ntot = Ntot + nread if ns.remove_shotnoise: shotnoise = pm.BoxSize**3 / Ntot else: shotnoise = 0 pm.r2c() k, p = measurepower(pm, pm.complex, ns.binshift, ns.remove_cic, shotnoise) if pm.comm.rank == 0: if ns.output != '-': myout = open(ns.output, 'w') else: myout = stdout numpy.savetxt(myout, zip(k, p), '%0.7g') myout.flush() hf = files.HaloFile(ns.halocatalogue) mass = hf.read_mass() M1 = ((mass[1:] * 1.0)** 2).sum(dtype='f8') / (1.0 * Ntot)**2 * ns.BoxSize**3 logging.info("p[-inf] = %g, M1 = %g" % (p[-1], M1))