Voxelization (volume sampling) is controlled with 'grid', the integer dimensions of the target grid, and 'window', which sets the extent of the grid in coordinate-space. Change 'order' to 0, 1, or 2 to play with tetrahedral, trilinear, and triquadratic finite elements. 'tol' controls the accuracy of the voxelization in units of the voxel size. Try varying it between 0 and 1. Fields to voxelize are requested via 'fields'. Setting 'm' to 'None' tells PSI to voxelize the mass field to new numpy array. ''' # initialize an element of a given order order = 1 pos = psi.elementBase(order) vel = np.zeros_like(pos) mass = np.ones(1) # perturb the control points, keeping them within the unit box pos -= 0.5 pos *= 0.5 + 0.4 * np.random.sample(pos.shape) pos += 0.5 # PyPSI voxelization options fields = {'m': None} grid = (64, 64, 64) window = ((0.0, 0.0, 0.0), (1.0, 1.0, 1.0)) tol = 0.01 psi.elementMesh(fields, pos, vel, mass, grid=grid, window=window, tol=tol)
boxsize = 1. box = ((0.0, 0.0, 0.0), (boxsize, boxsize, boxsize)) nx = ds.domain_dimensions[0] ## set voxelization options order = 1 tol = 1000 subgrid = (subgridsize, subgridsize, subgridsize) window = box fields = {'m': None} ## chunk through Lagrangian patches mtot = 0.0 tstart = time.time() for pos, vel, mass, block, nblocks in psi.elementBlocksFromGrid(pos, vel, order=order, periodic=True): ## make periodic and voxelize it psi.voxelize(fields, pos, vel, mass, tol=tol, window=window, \ grid=subgrid, periodic=True, box=box) ## bookkeeping mtot += np.sum(mass) tend = time.time() sys.stdout.write("\rVoxelized block %d of %d. Time = %.1f s" % (block, nblocks, tend-tstart)) sys.stdout.flush() print '\nGlobal error = %.10e' % np.abs(np.sum(fields['m'])/mtot-1.0) # save density data to file f = tables.open_file("density%04d_64.h5" % i, mode = 'w') f.create_array(f.root, "density", fields['m'])
Voxelization (volume sampling) is controlled with 'grid', the integer dimensions of the target grid, and 'window', which sets the extent of the grid in coordinate-space. Change 'order' to 0, 1, or 2 to play with tetrahedral, trilinear, and triquadratic finite elements. 'tol' controls the accuracy of the voxelization in units of the voxel size. Try varying it between 0 and 1. Fields to voxelize are requested via 'fields'. Setting 'm' to 'None' tells PSI to voxelize the mass field to new numpy array. ''' # initialize an element of a given order order = 1 pos = psi.elementBase(order) vel = np.zeros_like(pos) mass = np.ones(1) # perturb the control points, keeping them within the unit box pos -= 0.5 pos *= 0.5 + 0.4*np.random.sample(pos.shape) pos += 0.5 # PyPSI voxelization options fields = {'m': None} grid = (64, 64, 64) window = ((0.0, 0.0, 0.0), (1.0, 1.0, 1.0)) tol = 0.01 psi.elementMesh(fields, pos, vel, mass, grid=grid, window=window, tol=tol)
import os import sys sys.path.insert(0, "/home/phsun/PyPSI") import PyPSI as psi import matplotlib.pyplot as plt import numpy as np import tables fig, ax = plt.subplots() f = tables.open_file("density0000.h5", mode = 'r') density = np.array(f.get_node("/density")) f.close() ## calculate power spectrum power, k = psi.powerSpectrum(density, \ dims=(300., 300., 300.), bins = 256) l = 2*np.pi/k ## plot spectrum ax.loglog(l, power, label = "0000") ## save to file np.savez_compressed("spectrum0000.npz", power=power, l=l) ax.set_xlabel("l (Mpc/h)") ax.set_ylabel("power") legend = plt.legend(loc = 'upper right', shadow = True) fig.savefig("spectrum_PSI_0000.png")
sys.path.insert(0, "/home/phsun/PyPSI") import PyPSI as psi import matplotlib.pyplot as plt import numpy as np import tables fig, ax = plt.subplots() i = 0 while os.path.isfile("density%04d_64.h5" % i): f = tables.open_file("density%04d_64.h5" % i, mode="r") density = np.array(f.get_node("/density")) f.close() ## calculate power spectrum power, k = psi.powerSpectrum(density, dims=(300.0 / 4, 300.0 / 4, 300.0 / 4), bins=100) l = 2 * np.pi / k ## plot spectrum ax.loglog(l, power, label="%04d" % i) ## save to file np.savez_compressed("spectrum%04d_64.npz" % i, power=power, l=l) i += 1 ax.set_xlabel("l (Mpc/h)") ax.set_ylabel("power") legend = plt.legend(loc="upper right", shadow=True) fig.savefig("spectrum_PSI_64.png")