def main(): if MPI.COMM_WORLD.rank == 0: print 'importing done' pm = ParticleMesh(ns.BoxSize, ns.Nmesh, dtype='f4') Ntot = paint_darkmatter(pm, ns.filename, TPMSnapshotFile) if ns.remove_shotnoise: shotnoise = pm.BoxSize ** 3 / Ntot else: shotnoise = 0 pm.r2c() if MPI.COMM_WORLD.rank == 0: print 'r2c done' k, p = measurepower(pm, pm.complex, ns.binshift, ns.remove_cic, shotnoise) if MPI.COMM_WORLD.rank == 0: print 'measure power done' 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()
def main(): if MPI.COMM_WORLD.rank == 0: print 'importing done' pm = ParticleMesh(ns.BoxSize, ns.Nmesh, dtype='f4') Ntot = paint_darkmatter(pm, ns.filename, TPMSnapshotFile) if ns.remove_shotnoise: shotnoise = pm.BoxSize**3 / Ntot else: shotnoise = 0 pm.r2c() if MPI.COMM_WORLD.rank == 0: print 'r2c done' k, p = measurepower(pm, pm.complex, ns.binshift, ns.remove_cic, shotnoise) if MPI.COMM_WORLD.rank == 0: print 'measure power done' 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()
def do1d(pm, complex, ns, shotnoise): result = measurepower(pm, complex, ns.binshift, ns.remove_cic, shotnoise) if MPI.COMM_WORLD.rank == 0: print 'measure' if pm.comm.rank == 0: storage = plugins.PowerSpectrumStorage.get(ns.mode, ns.output) storage.write(result)
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 main(): if MPI.COMM_WORLD.rank == 0: print 'importing done' pm = ParticleMesh(ns.BoxSize, ns.Nmesh, dtype='f4') Ntot = paint_darkmatter(pm, ns.filename1, TPMSnapshotFile) if MPI.COMM_WORLD.rank == 0: print 'painting done' pm.r2c() if MPI.COMM_WORLD.rank == 0: print 'r2c done' complex = pm.complex.copy() numpy.conjugate(complex, out=complex) Ntot = paint_darkmatter(pm, ns.filename2, TPMSnapshotFile) if MPI.COMM_WORLD.rank == 0: print 'painting 2 done' pm.r2c() if MPI.COMM_WORLD.rank == 0: print 'r2c 2 done' complex *= pm.complex complex **= 0.5 if MPI.COMM_WORLD.rank == 0: print 'cross done' k, p = measurepower(pm, complex, ns.binshift, ns.remove_cic, 0) if MPI.COMM_WORLD.rank == 0: print 'measure' 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()
def main(): if MPI.COMM_WORLD.rank == 0: print "importing done" pm = ParticleMesh(ns.BoxSize, ns.Nmesh, dtype="f4") Ntot = paint_darkmatter(pm, ns.filename1, TPMSnapshotFile) if MPI.COMM_WORLD.rank == 0: print "painting done" pm.r2c() if MPI.COMM_WORLD.rank == 0: print "r2c done" complex = pm.complex.copy() numpy.conjugate(complex, out=complex) Ntot = paint_darkmatter(pm, ns.filename2, TPMSnapshotFile) if MPI.COMM_WORLD.rank == 0: print "painting 2 done" pm.r2c() if MPI.COMM_WORLD.rank == 0: print "r2c 2 done" complex *= pm.complex complex **= 0.5 if MPI.COMM_WORLD.rank == 0: print "cross done" k, p = measurepower(pm, complex, ns.binshift, ns.remove_cic, 0) if MPI.COMM_WORLD.rank == 0: print "measure" 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()
def main(): pm = ParticleMesh(ns.BoxSize, ns.Nmesh) Ntot = paint_halos(pm, ns.halocatalogue, ns.BoxSize, ns.m0, ns.massmin, ns.massmax) 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()
def main(): pm = ParticleMesh(ns.BoxSize, ns.Nmesh) Ntot = paint_halos(pm, ns.halocatalogue, ns.BoxSize, ns.m0, ns.massmin, ns.massmax) 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()
def main(): if MPI.COMM_WORLD.rank == 0: print 'importing done' chain = [ TransferFunction.NormalizeDC, TransferFunction.RemoveDC, TransferFunction.Gaussian(1), ] if ns.remove_cic == 'anisotropic': chain.append(AnisotropicCIC) if ns.remove_cic == 'isotropic': chain.append(IsotropicCIC) # setup the particle mesh object pm = ParticleMesh(ns.BoxSize, ns.Nmesh, dtype='f4') # paint first input Ntot1 = ns.inputs[0].paint(ns, pm) r1 = pm.real.copy() # painting if MPI.COMM_WORLD.rank == 0: print 'painting done' pm.r2c() if MPI.COMM_WORLD.rank == 0: print 'r2c done' # filter the field pm.transfer(chain) # do the cross power do_cross = len(ns.inputs) > 1 if do_cross: c1 = pm.complex.copy() Ntot2 = ns.inputs[1].paint(ns, pm) if MPI.COMM_WORLD.rank == 0: print 'painting 2 done' pm.r2c() if MPI.COMM_WORLD.rank == 0: print 'r2c 2 done' # filter the field pm.transfer(chain) c2 = pm.complex.copy() if MPI.COMM_WORLD.rank == 0: print 'convolution done' dot(pm, c2, c2) c2 = pm.complex # do the auto power else: c1 = pm.complex c2 = pm.complex Ntot2 = Ntot1 if ns.remove_shotnoise and not do_cross: shotnoise = pm.BoxSize ** 3 / (1.0*Ntot1) else: shotnoise = 0 # only need one mu bin if 1d case is requested if ns.mode == "1d": ns.Nmu = 1 # do the calculation meta = {'box_size':pm.BoxSize, 'N1':Ntot1, 'N2':Ntot2, 'shot_noise': shotnoise} result = measurepower(pm, c1, c2, ns.Nmu, binshift=ns.binshift, shotnoise=shotnoise, los=ns.los) # format the output appropriately if ns.mode == "1d": # this writes out 0 -> mean k, 2 -> mean power, 3 -> number of modes meta['edges'] = result[-1][0] # write out kedges as metadata result = map(numpy.ravel, (result[i] for i in [0, 2, 3])) elif ns.mode == "2d": result = dict(zip(['k','mu','power','modes','edges'], result)) if MPI.COMM_WORLD.rank == 0: print 'measure' storage = plugins.PowerSpectrumStorage.get(ns.mode, ns.output) storage.write(result, **meta)
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 compute_power(ns, comm=None): """ Compute the power spectrum. Given a `Namespace`, this is the function, that computes and saves the power spectrum. It does all the work. Parameters ---------- ns : argparse.Namespace the parser namespace corresponding to the ``initialize_power_parser`` functions comm : MPI.Communicator the communicator to pass to the ``ParticleMesh`` object """ rank = comm.rank if comm is not None else MPI.COMM_WORLD.rank # set logging level logger.setLevel(ns.log_level) if rank == 0: logger.info('importing done') chain = [TransferFunction.NormalizeDC, TransferFunction.RemoveDC] if ns.remove_cic == 'anisotropic': chain.append(AnisotropicCIC) if ns.remove_cic == 'isotropic': chain.append(IsotropicCIC) # setup the particle mesh object, taking BoxSize from the painters pm = ParticleMesh(ns.inputs[0].BoxSize, ns.Nmesh, dtype='f4', comm=comm) # paint first input Ntot1 = paint(ns.inputs[0], pm, ns) # painting if rank == 0: logger.info('painting done') pm.r2c() if rank == 0: logger.info('r2c done') # filter the field pm.transfer(chain) # do the cross power do_cross = len(ns.inputs) > 1 and ns.inputs[0] != ns.inputs[1] if do_cross: # crash if box size isn't the same if not numpy.all(ns.inputs[0].BoxSize == ns.inputs[1].BoxSize): raise ValueError("mismatch in box sizes for cross power measurement") c1 = pm.complex.copy() Ntot2 = paint(ns.inputs[1], pm, ns) if rank == 0: logger.info('painting 2 done') pm.r2c() if rank == 0: logger.info('r2c 2 done') # filter the field pm.transfer(chain) c2 = pm.complex # do the auto power else: c1 = pm.complex c2 = pm.complex Ntot2 = Ntot1 if ns.remove_shotnoise and not do_cross: shotnoise = pm.BoxSize.prod() / (1.0*Ntot1) else: shotnoise = 0 # only need one mu bin if 1d case is requested if ns.mode == "1d": ns.Nmu = 1 # do the calculation Lx, Ly, Lz = pm.BoxSize meta = {'Lx':Lx, 'Ly':Ly, 'Lz':Lz, 'volume':Lx*Ly*Lz, 'N1':Ntot1, 'N2':Ntot2, 'shot_noise': shotnoise} result = measurepower(pm, c1, c2, ns.Nmu, binshift=ns.binshift, shotnoise=shotnoise, los=ns.los, dk=ns.dk, kmin=ns.kmin, poles=ns.poles) # format the output appropriately if len(ns.poles): pole_result, pkmu_result, edges = result result = dict(zip(['k','mu','power','modes','edges'], pkmu_result+(edges,))) elif ns.mode == "1d": # this writes out 0 -> mean k, 2 -> mean power, 3 -> number of modes meta['edges'] = result[-1][0] # write out kedges as metadata result = map(numpy.ravel, (result[i] for i in [0, 2, 3])) elif ns.mode == "2d": result = dict(zip(['k','mu','power','modes','edges'], result)) if rank == 0: # save the power logger.info('measurement done; saving power to %s' %ns.output) storage = plugins.PowerSpectrumStorage.new(ns.mode, ns.output) storage.write(result, **meta) # save the multipoles if len(ns.poles): if ns.pole_output is None: raise RuntimeError("you specified multipoles to compute, but did not provide an output file name") meta['edges'] = edges[0] # format is k pole_0, pole_1, ...., modes_1d logger.info('saving ell = %s multipoles to %s' %(",".join(map(str,ns.poles)), ns.pole_output)) result = [x for x in numpy.vstack(pole_result)] storage = plugins.PowerSpectrumStorage.new('1d', ns.pole_output) storage.write(result, **meta)