Added parallelization to _project.pyx code

This commit is contained in:
Guilhem Lavaux 2014-06-11 11:14:33 +02:00
parent 5b133a9ac8
commit 401ddc8a8b
9 changed files with 173 additions and 122 deletions

View file

@ -1,57 +0,0 @@
import healpy as hp
import numpy as np
import cosmotool as ct
import h5py as h5
from matplotlib import pyplot as plt
L=600.
Nside=128
INDATA="/nethome/lavaux/Copy/PlusSimulation/BORG/Input_Data/2m++.npy"
tmpp = np.load(INDATA)
def build_sky_proj(density, dmax=60.,dmin=0):
N = density.shape[0]
ix = (np.arange(N)-0.5)*L/N - 0.5 * L
dist2 = (ix[:,None,None]**2 + ix[None,:,None]**2 + ix[None,None,:]**2)
flux = density.transpose().astype(ct.DTYPE) # / dist2
dmax=N*dmax/L
dmin=N*dmin/L
projsky1 = ct.spherical_projection(Nside, flux, dmin, dmax, integrator_id=1)
# projsky0 = ct.spherical_projection(Nside, flux, 0, 52, integrator_id=0)
return projsky1*L/N#,projsky0
l,b = tmpp['gal_long'],tmpp['gal_lat']
l = np.radians(l)
b = np.pi/2 - np.radians(b)
dcmb = tmpp['velcmb']/100.
idx = np.where((dcmb>10)*(dcmb<60))
plt.figure(1)
plt.clf()
if False:
with h5.File("fields.h5", mode="r") as f:
d = f["density"][:]
d /= np.average(np.average(np.average(d,axis=0),axis=0),axis=0)
proj = build_sky_proj(f["density"][:], dmin=10,dmax=60.)
else:
d = np.load("icgen/dcic0.npy")
proj0 = build_sky_proj(1+d, dmin=10,dmax=60.)
d = np.load("icgen/dcic1.npy")
proj1 = build_sky_proj(1+d, dmin=10,dmax=60.)
hp.mollview(proj0, fig=1, coord='CG', max=60, cmap=plt.cm.coolwarm)
hp.projscatter(b[idx], l[idx], lw=0, color='g', s=5.0, alpha=0.8)
plt.figure(2)
plt.clf()
hp.mollview(proj1, fig=2, coord='CG', max=60, cmap=plt.cm.coolwarm)
hp.projscatter(b[idx], l[idx], lw=0, color='g', s=5.0, alpha=0.8)

View file

@ -0,0 +1 @@
/nethome/lavaux/wuala/WualaDrive/g_lavaux/PythonCode/BORG/build_nbody_skymap.py

View file

@ -93,17 +93,67 @@ def run_generation(input_borg, a_borg, a_ic, cosmo, supersample=1, do_lpt2=True,
density = np.fft.irfftn(lpt.dhat*D1_0)*(supersample*N/L)**3
return posx,vel,density,N*supersample,L,a_ic
return posx,vel,density,N*supersample,L,a_ic,cosmo
def write_icfiles(*generated_ic, **cosmo):
"""Write the initial conditions from the tuple returned by run_generation"""
posx,vel,density,N,L,a_ic = generated_ic
def whitify(density, L, cosmo, supergenerate=1, func='HU_WIGGLES'):
N = density.shape[0]
ik = np.fft.fftfreq(N, d=L/N)*2*np.pi
ct.simpleWriteGadget("borg.gad", posx, velocities=vel, boxsize=L, Hubble=cosmo['h'], Omega_M=cosmo['omega_M_0'], time=a_ic)
for i,c in enumerate(["x","y","z"]):
ct.writeGrafic("ic_velc%s" % c, vel[i].reshape((N,N,N)), L, a_ic, **cosmo)
# This used to be necessary. However this has been fixed in writeGrafic now
# ct.writeGrafic("ic_velc%s" % c, vel[i].reshape((N,N,N)).transpose(), L, a_ic, **cosmo)
k = np.sqrt(ik[:,None,None]**2 + ik[None,:,None]**2 + ik[None,None,:(N/2+1)]**2)
p = ct.CosmologyPower(**cosmo)
p.setFunction(func)
p.normalize(cosmo['SIGMA8'])
Pk = p.compute(k)*cosmo['h']**3*L**3
Pk[0,0,0]=1
density_hat = np.fft.rfftn(density)/N**3/np.sqrt(Pk)
Ns = N*supergenerate
density_hat_super = np.zeros((Ns,Ns,Ns/2+1), dtype=np.complex128)
density_hat_super[:] = np.nan
# Copy density hat in place
hN = N/2
density_hat_super[:hN, :hN, :hN+1] = density_hat[:hN, :hN, :]
density_hat_super[:hN, (Ns-hN):Ns, :hN+1] = density_hat[:hN, hN:, :]
density_hat_super[(Ns-hN):Ns, (Ns-hN):Ns, :hN+1] = density_hat[hN:, hN:, :]
density_hat_super[(Ns-hN):Ns, :hN, :hN+1] = density_hat[hN:, :hN, :]
# The moved nyquist place is untouched (so loss of "noise") to keep the structure
# now we just add some noise term
cond=np.isnan(density_hat_super)
x = np.random.randn(np.count_nonzero(cond),2)/np.sqrt(2.0)
density_hat_super[cond] = x[:,0] + 1j * x[:,1]
# Now we have to fix the Nyquist plane
hNs = Ns/2
nyquist = density_hat_super[:, :, :hNs]
Nplane = nyquist.size
nyquist.flat[:Nplane/2] = nyquist.flat[Nplane:Nplane/2:-1].conj()
return np.fft.irfftn(density_hat_super)
def write_icfiles(*generated_ic, **kwargs):
"""Write the initial conditions from the tuple returned by run_generation"""
supergenerate=1
if 'supergenerate' in kwargs:
supergenerate=kwargs['supergenerate']
posx,vel,density,N,L,a_ic,cosmo = generated_ic
ct.simpleWriteGadget("borg.gad", posx, velocities=vel, boxsize=L, Hubble=cosmo['h'], Omega_M=cosmo['omega_M_0'], time=a_ic)
for i,c in enumerate(["x","y","z"]):
ct.writeGrafic("ic_velc%s" % c, vel[i].reshape((N,N,N)), L, a_ic, **cosmo)
ct.writeGrafic("ic_deltab", density, L, a_ic, **cosmo)
ct.writeWhitePhase("white.dat", whitify(density, L, cosmo, supergenerate=supergenerate))
with file("white_params", mode="w") as f:
f.write("4\n%lg, %lg, %lg\n" % (cosmo['omega_M_0'], cosmo['omega_lambda_0'], 100*cosmo['h']))
f.write("%lg\n%lg\n-%lg\n0,0\n" % (cosmo['omega_B_0'],cosmo['ns'],cosmo['SIGMA8']))
f.write("-%lg\n1\n0\n\n\n\n\n" % L)
f.write("2\n\n0\nwhite.dat\n0\npadding_white.dat\n")
ct.writeGrafic("ic_deltab", density, L, a_ic, **cosmo)
ct.writeWhitePhase("white.dat", density)

View file

@ -14,4 +14,4 @@ astart=1/(1.+zstart)
halfPixelShift=True
if __name__=="__main__":
bic.write_icfiles(*bic.run_generation("initial_condition_borg.dat", 0.001, astart, cosmo, supersample=2, shiftPixel=halfPixelShift, do_lpt2=False), **cosmo)
bic.write_icfiles(*bic.run_generation("initial_condition_borg.dat", 0.001, astart, cosmo, supersample=2, shiftPixel=halfPixelShift, do_lpt2=False))

View file

@ -10,7 +10,7 @@ cosmo['omega_k_0'] = 0
cosmo['omega_B_0']=0.049
cosmo['SIGMA8']=0.8344
cosmo['ns']=0.9624
N0=256
N0=128
doSimulation=True
@ -29,7 +29,7 @@ if doSimulation:
dsim_hat = np.fft.rfftn(dsim)*(L/N0)**3
Psim, bsim = bic.bin_power(np.abs(dsim_hat)**2/L**3, L, range=(0,1.), bins=150)
pos,_,density,N,L,_ = bic.run_generation("initial_condition_borg.dat", 0.001, astart, cosmo, supersample=2, do_lpt2=True)
pos,_,density,N,L,_,_ = bic.run_generation("initial_density_1380.dat", 0.001, astart, cosmo, supersample=2, do_lpt2=True)
dcic = ct.cicParticles(pos, L, N0)
dcic /= np.average(np.average(np.average(dcic, axis=0), axis=0), axis=0)
@ -50,5 +50,5 @@ Pref, bref = bic.compute_ref_power(L, N0, cosmo, range=(0,1.), bins=150)
Pcic /= D1_0**2
Pdens /= D1_0**2
borg_evolved = ct.read_borg_vol("final_density_380.dat")
borg_evolved = ct.read_borg_vol("final_density_1380.dat")

View file

@ -1,20 +0,0 @@
import cosmotool as ct
import numpy as np
import healpy as hp
d = np.zeros((64,64,64), ct.DTYPE)
d[32,32,32] = 1
ii=np.arange(256)*64/256.-32
xx = ii[:,None,None].repeat(256,axis=1).repeat(256,axis=2).reshape(256**3)
yy = ii[None,:,None].repeat(256,axis=0).repeat(256,axis=2).reshape(256**3)
zz = ii[None,None,:].repeat(256,axis=0).repeat(256,axis=1).reshape(256**3)
d_high = ct.interp3d(xx, yy, zz, d, 64, periodic=True)
d_high = d_high.reshape((256,256,256))
proj0 = ct.spherical_projection(64, d, 0, 20, integrator_id=0, shifter=np.array([0.5,0.5,0.5]))
proj1 = ct.spherical_projection(64, d, 0, 20, integrator_id=1)
proj0_high = ct.spherical_projection(256, d_high, 0, 30, integrator_id=0, shifter=np.array([0.5,0.5,0.5]))

View file

@ -0,0 +1 @@
/nethome/lavaux/wuala/WualaDrive/g_lavaux/PythonCode/BORG/test_spheric_proj.py