def test_voxels(self): #return # load a mesh, make a grid, and voxelize print() ngrid = 3 * (64, ) snap = 'data/snapshot_010' mesh = psi.Mesh(filename=snap, loader='gadget2') grid = psi.Grid(type='cart', n=ngrid, window=(mesh.boxmin, mesh.boxmax)) psi.voxels(grid=grid, mesh=mesh, mode='density') #psi.voxels(grid=grid, mesh=mesh, mode='annihilation') # check the total mass # show a picture elemmass = np.sum(mesh.mass) voxmass = np.sum(grid.fields["m"]) err = np.abs(1.0 - voxmass / elemmass) print(' - total mass =', voxmass) print(" - mass error = ", err) #plt.imshow(np.log10(grid.fields["m"][:,:,32])) #plt.show() self.assertAlmostEqual(err, 0.0, delta=errtol)
def test_powerspec(self): # loads, voxelizes, and computes the potential field print() dim = 3 box = 128 win = 13.0 grid = psi.Grid(type='cart', n=dim * (box, ), window=(dim * (0., ), dim * (win, ))) # set phi min, max such that phi min/max = -+1 #rho0 = -(np.pi*nk**2)/(G*win**2) grid.fields['m'][:, :, :] = np.sin(2 * np.pi * 13 * np.arange(box) / box) grid.fields['m'][:, :, :] += 0.7 * np.sin( 2 * np.pi * 31 * np.arange(box) / box) grid.fields['m'][:, :, :] += 1.2 * np.sin( 2 * np.pi * 45 * np.arange(box) / box) P, k = psi.powerSpectrum(grid) #plt.plot(k, P) #plt.show() # test that the max error is small ptot_x = np.sum(grid.fields['m']**2) ptot_k = np.sum(P) maxerr = np.abs((ptot_x - ptot_k) / ptot_x) print(' - max relative err (checking Parseval) =', maxerr) self.assertAlmostEqual(maxerr, 0.0, delta=errtol)
def test_annihilation(self): return print() mesh = psi.Mesh(filename='data/snapshot_010', loader='gadget2') #mesh = psi.Mesh(filename='', loader='hacky_test') #mesh = psi.Mesh(filename='data/box128_010', loader='gadget2') #print mesh.pos[mesh.connectivity][12475] grid = psi.Grid(type='hpring', n=128) psi.skymap(grid=grid, mesh=mesh, bstep=1, mode=1) err = 1.0 - np.sum(grid.fields["m"]) print("mass = ", np.sum(grid.fields["m"]), 'err =', err) print(" - min = ", np.min(grid.fields["m"])) print(" - max = ", np.max(grid.fields["m"])) grid.fields["m"].tofile('data/annihilation128_shell.np') # show the pixel area plot #hp.mollview(np.log10(grid.fields['m']), title='Mass map, err = %.5e'%err) hp.mollview(np.log10(grid.fields['m']) ) #, title='Mass map, err = %.5e'%err)#, min = -4) plt.show()
def test_vdf(self): #return # load a mesh, make a grid, and voxelize print() snap = 'data/snapshot_010' mesh = psi.Mesh(filename=snap, loader='gadget2') rst = psi.RStarTree(mesh) for sample_pos in [(10., 10., 13.), (19., 23., 29.), (10., 12., 13.)]: rho, vel = psi.VDF(mesh, sample_pos, tree=rst) print('Sampled %d streams' % len(rho)) print('rho[0] =', rho[0]) print('vel[0] =', vel[0])
def test_mesh_init(self): print() mesh = psi.Mesh(filename='data/snapshot_010', loader='gadget2') print(' - pos =', mesh.pos.dtype, mesh.pos.shape) print(' - vel =', mesh.vel.dtype, mesh.vel.shape) print(' - conn =', mesh.connectivity.dtype, mesh.connectivity.shape) print(' - box =', mesh.boxmin, mesh.boxmax)
def test_grid_hpring(self): print() grid = psi.Grid(type='hpring') print(' - type =', grid.type) print(' - fields =', grid.fields['m'].dtype, grid.fields['m'].shape) print(' - window =', grid.winmin, grid.winmax) print(' - n =', grid.n) print(' - d =', grid.d) # get a pixel boundary center, boundary, vol = grid.getCellGeometry(cell=None, bstep=2)
def test_grid_cart(self): print() grid = psi.Grid(type='cart', n=(10, 10, 10)) # default is 64^3 unit box print(' - type =', grid.type) print(' - fields =', grid.fields['m'].dtype, grid.fields['m'].shape) print(' - window =', grid.winmin, grid.winmax) print(' - n =', grid.n) print(' - d =', grid.d) center, boundary, vol = grid.getCellGeometry(cell=[123])
def test_skymap(self): return print() snap = 'data/snapshot_010' nside = 64 # 16 # 32 mesh = psi.Mesh(filename=snap, loader='gadget2') #mesh = psi.Mesh(filename='', loader='hacky_test') grid = psi.Grid(type='hpring', n=nside) psi.skymap(grid=grid, mesh=mesh, bstep=2, mode=0) # show the mass map and error elemmass = np.sum(mesh.mass) voxmass = np.sum(grid.fields["m"]) err = np.abs(1.0 - voxmass / elemmass) print(' - total mass =', voxmass) print(" - mass error = ", err) hp.mollview(np.log10(grid.fields['m']), title='Mass map') plt.show() self.assertAlmostEqual(err, 0.0, delta=errtol)
def test_rtree(self): #return # load a mesh, make a grid, and voxelize print() snap = 'data/snapshot_010' mesh = psi.Mesh(filename=snap, loader='gadget2') rst = psi.RStarTree(mesh, initial_capacity=12) qinds = rst.query(box=(( 10., 10., 10., ), (11., 11.2, 11.3))) print('Queried indices from the tree!') print('Indices =', qinds.shape) print('Connectivity =', mesh.connectivity[qinds].shape) print('Elems =', mesh.pos[mesh.connectivity[qinds]].shape) print(' = Indices =', qinds[0]) print(' = Connectivity =', mesh.connectivity[qinds][0]) print(' = Elems =', mesh.pos[mesh.connectivity[qinds]][0])
def test_phi_simple(self): # loads, voxelizes, and computes the potential field print() dim = 3 box = 128 win = 21.0 G = 21.12131 nk = 2 grid = psi.Grid(type='cart', n=dim * (box, ), window=(dim * (0., ), dim * (win, ))) # set phi min, max such that phi min/max = -+1 rho0 = -(np.pi * nk**2) / (G * win**2) grid.fields['m'][:, :, :] = rho0 * np.sin( 2 * np.pi * nk * np.arange(box) / box) phi = psi.phi(grid, Gn=G) analytic = np.sin(2 * np.pi * nk * np.arange(box) / box) maxerr = np.max(np.abs(phi - analytic)) print(' - max absolute err in phi =', maxerr) # test that the max error is small self.assertAlmostEqual(maxerr, 0.0, delta=errtol)
''' Example 5: VDF sampling Demonstrates how to use PSI.VDF() and PSI.crossStreams() ''' import PSI as psi import helpers as hlp import numpy as np # load the mesh snap = '../data/snapshot_010' mesh = psi.Mesh(filename=snap, loader='gadget2') # make an RStarTree out of the mesh rst = psi.RStarTree(mesh) # define the velocity-dependent rate coefficient def xsn(v): return 1.0 # loop over all sample locations for sample_pos in hlp.sampleSphere(r=0.2, center=[10.,10.,13.], nsamp=5): print('Next sample...') # sample the multi-streaming region rho, vel = psi.VDF(mesh, tuple(sample_pos), tree=rst)
''' Example 2: Voxelization of a cosmological density field ''' import PSI as psi import helpers as hlp import numpy as np # load the mesh from a Gadget snapshot snap = '../data/snapshot_010' mesh = psi.Mesh(filename=snap, loader='gadget2') # create the Grid, specifying the resolution and projection window ngrid = 3 * (128, ) win = (mesh.boxmin, mesh.boxmax) grid = psi.Grid(type='cart', n=ngrid, window=win) # call PSI.voxels() psi.voxels(grid=grid, mesh=mesh, mode='density') #psi.voxels(grid=grid, mesh=mesh, mode='annihilation') # check the total mass # show a picture elemmass = np.sum(mesh.mass) voxmass = np.sum(grid.fields["m"]) err = np.abs(1.0 - voxmass / elemmass) # print the error and show the figure print('Global error = %.10e' % err)
# perturb the position randomly, keeping it within the unit box pos -= 0.5 pos *= 0.5 + 0.4 * np.random.sample(pos.shape) pos += 0.5 # make a mesh, using the direct array input mode # NOTE: connectivity must be 32-bit ints conn = np.array([list(range(pos.shape[0]))], dtype=np.int32) # TODO: connectivity array must be 2D pbox = ( None, None ) # TODO: this is still necessary to pass, even if the mesh is not periodic print(' - Connectivity =', conn) mesh = psi.Mesh(loader='array', posar=pos, velar=vel, massar=mass, connar=conn, box=pbox) print('Connectivity done') # create the Grid, specifying the resolution and projection window ngrid = 3 * (128, ) win = ((0., 0., 0.), (1., 1., 1.)) grid = psi.Grid(type='cart', n=ngrid, window=win) print('Grid done') # call PSI.voxels() psi.voxels(grid=grid, mesh=mesh,
def test_beam_tracing(self): return #metric = psi.Metric(type='flrw', filepattern='/home/devon/HDD/PS-128-vm/geo/%04d') metric = psi.Metric(type='kerr') with open('/home/devon/HDD/PS-128-vm/geo/0137', 'rb') as f: # read potential f.seek(0) phi = np.fromfile(f, count=128**3, dtype=np.float32).reshape( (128, 128, 128)) print(phi.shape, np.min(phi), np.max(phi)) # read velocity f.seek(128**3) vel = np.fromfile(f, count=3 * 128**3, dtype=np.float32).reshape( (128, 128, 128, 3)) # read potential f.seek(4 * 128**3) gradphi = np.fromfile(f, count=3 * 128**3, dtype=np.float32).reshape((128, 128, 128, 3)) print(gradphi.shape, np.min(gradphi), np.max(gradphi)) f.close() gradax = 2 mygrad = np.roll(phi, 1, axis=gradax) - np.roll(phi, -1, axis=gradax) fig, ax = plt.subplots(1, 2, figsize=(20, 10)) #ax[0].imshow(phi[64,:,:]) ax[0].imshow(mygrad[64, :, :]) ax[1].imshow(gradphi[64, :, :, gradax]) #ax[1].contour(phi[64,:,:]) plt.show() #return #print grid = psi.Grid(type='hpring', n=16) rayinfo = psi.beamtrace(grid=grid, metric=metric, obspos=(10., 9., 8.), obsvel=(0., 0., 0.)) print(rayinfo.shape) print(rayinfo[0, :, :]) fig = plt.figure() ax = fig.gca(projection='3d') for tray in rayinfo: ray = tray[tray[:, 0] > 0.0] ax.plot(ray[:, 1], ray[:, 2], ray[:, 3], 'k-', lw=0.2, label='parametric curve') #ax.set_xlim(-30,30) #ax.set_ylim(-30,30) #ax.set_zlim(-30,30) plt.show() # show the pixel area plot hp.mollview((grid.fields['m']), title='minkowski') plt.show()
Example 4: RStarTree Demonstrates how to use the RStarTree class Still somewhat rudimentary RStarTree is used by PSI.VDF and PSI.voxels(mode='annihlation') so far. ''' import PSI as psi import helpers as hlp import numpy as np # load the mesh snap = '../data/snapshot_010' mesh = psi.Mesh(filename=snap, loader='gadget2') # make an RStarTree out of the mesh # Currently this is the only constructor; # a mesh must be provided rst = psi.RStarTree(mesh) # set up the query box query_box = ((10., 9., 8.), (10.3, 9.5, 8.2)) # query the tree # this returns an index for each queried element! qinds = rst.query(box=query_box) print(' - element indices =', qinds) # use fancy indexing to get the element connectivity back
import PSI as psi import helpers as hlp import numpy as np # load the mesh from a Gadget snapshot snap = '../data/snapshot_010' mesh = psi.Mesh(filename=snap, loader='gadget2') # create the Grid, specifying the resolution and projection window ngrid = 3*(128,) win = (mesh.boxmin, mesh.boxmax) grid = psi.Grid(type='cart', n=ngrid, window=win) # call PSI.voxels() psi.voxels(grid=grid, mesh=mesh, mode='density') #psi.voxels(grid=grid, mesh=mesh, mode='annihilation') # check the total mass # show a picture elemmass = np.sum(mesh.mass) voxmass = np.sum(grid.fields["m"]) err = np.abs(1.0-voxmass/elemmass) # print the error and show the figure print('Global error = %.10e' % err) hlp.makeFigs(grid.fields['m'], log=True, title='Example 2: Voxelization of a cosmological density field')
pos *= 0.5 + 0.4*np.random.sample(pos.shape) pos += 0.5 # make a mesh, using the direct array input mode # NOTE: connectivity must be 32-bit ints conn = np.array([list(range(pos.shape[0]))], dtype=np.int32)# TODO: connectivity array must be 2D pbox = (None, None) # TODO: this is still necessary to pass, even if the mesh is not periodic print(' - Connectivity =', conn) mesh = psi.Mesh(loader='array', posar=pos, velar=vel, massar=mass, connar=conn, box=pbox) print('Connectivity done') # create the Grid, specifying the resolution and projection window ngrid = 3*(128,) win = ((0.,0.,0.),(1.,1.,1.)) grid = psi.Grid(type='cart', n=ngrid, window=win) print('Grid done') # call PSI.voxels() psi.voxels(grid=grid, mesh=mesh, mode='density', refine_tolerance=reftol, refine_max_lvl=reflvlmax) # print the error and show the figure elemmass = np.sum(mesh.mass) voxmass = np.sum(grid.fields["m"]) err = np.abs(1.0-voxmass/elemmass) print('Global error = %.10e' % err) hlp.makeFigs(grid.fields['m'], title='Example 2: Voxelization of a cosmological density field')