def test_scatter_parallel(save=False): """ Asserts that we create the same image with the parallel version of the code as with the serial version. """ number_of_parts = 1000 h_max = np.float32(0.05) resolution = 512 coordinates = (np.random.rand(2 * number_of_parts).reshape( (2, number_of_parts)).astype(np.float64)) hsml = np.random.rand(number_of_parts).astype(np.float32) * h_max masses = np.ones(number_of_parts, dtype=np.float32) image = scatter(coordinates[0], coordinates[1], masses, hsml, resolution) image_par = scatter_parallel(coordinates[0], coordinates[1], masses, hsml, resolution) if save: imsave("test_image_creation.png", image) assert np.isclose(image, image_par).all() return
def get_image(n): """ Gets the image for snapshot n, and also returns the associated SWIFT metadata object. """ filename = f"{snapshot_name}_{n:04d}.hdf5" data = load(filename) boxsize = data.metadata.boxsize[0].value output = np.zeros((resolution, resolution * 4), dtype=float) x, y, _ = data.gas.coordinates.value.T # This is an oblong box but we can only make squares! for box, box_edges in enumerate([[0.0, 1.1], [0.9, 2.1], [1.9, 3.1], [2.9, 4.0]]): mask = np.logical_and(x >= box_edges[0], x <= box_edges[1]) masked_x = x[mask] - np.float64(box) masked_y = y[mask] try: hsml = data.gas.smoothing_length.value[mask] except: hsml = data.gas.smoothing_lengths.value[mask] if plot == "density": mass = data.gas.masses.value[mask] image = scatter(x=masked_y, y=masked_x, m=mass, h=hsml, res=resolution) else: quantity = getattr(data.gas, plot).value[mask] # Need to divide out the particle density for non-projected density quantities image = scatter( x=masked_y, y=masked_x, m=quantity, h=hsml, res=resolution) / scatter(x=masked_y, y=masked_x, m=np.ones_like(quantity), h=hsml, res=resolution) output[:, box * resolution:(box + 1) * resolution] = image return output, data.metadata
def test_scatter(save=False): image = scatter( np.array([0.0, 1.0, 1.0]), np.array([0.0, 0.0, 1.0]), np.array([1.0, 1.0, 1.0]), np.array([0.2, 0.2, 0.2]), 256, ) if save: imsave("test_image_creation.png", image) return
def test_scatter_mass_conservation(): np.random.seed(971263) # Width of 0.8 centered on 0.5, 0.5. x = 0.8 * np.random.rand(100) + 0.1 y = 0.8 * np.random.rand(100) + 0.1 m = np.ones_like(x) h = 0.05 * np.ones_like(x) resolutions = [8, 16, 32, 64, 128, 256, 512] total_mass = np.sum(m) for resolution in resolutions: image = scatter(x, y, m, h, resolution) mass_in_image = image.sum() / (resolution**2) # Check mass conservation to 5% assert np.isclose(mass_in_image, total_mass, 0.05) return
def project(data, m_res, property, ylim): x, y, _ = data.gas.coordinates.value.T mask = np.logical_and(y >= ylim[0], y <= ylim[1]) x = x[mask] y = y[mask] - np.float64(ylim[0]) h = data.gas.smoothing_length[mask] if property == "density": property = "masses" if property is not None: quant = getattr(data.gas, property).value[mask] else: quant = np.ones_like(x) image = scatter(x=x, y=y, m=quant, h=h, res=m_res) return image.T
""" output = [] for file in tqdm(range(n_files), desc=f"Reading {to_read}"): current_filename = f"{filename}.{file}.hdf5" with h5py.File(current_filename, "r") as handle: output.append(handle[to_read][...]) return np.concatenate(output) with h5py.File(f"{snapshot}.0.hdf5", "r") as handle: boxsize = handle["Header"].attrs["BoxSize"] x, y, _ = load_data(snapshot, n_files, "PartType0/Coordinates").T / boxsize hsml = load_data(snapshot, n_files, "PartType0/SmoothingLength") / ( kernel_gamma * boxsize ) temp = load_data(snapshot, n_files, "PartType0/Temperature") # We need to construct sum(T_j W_ij) / sum(W_ij) norm_grid = scatter(x, y, np.ones_like(temp), hsml, resolution) temp_grid = scatter(x, y, temp, hsml, resolution) # Imsave does not take a norm normalized_grid = LogNorm(vmin=1e2, vmax=1e8)(temp_grid / norm_grid) imsave(output_filename, normalized_grid, cmap="twilight")
import matplotlib.pyplot as plt from matplotlib.colors import Normalize data = load("kelvinHelmholtz_0175.hdf5") resolution = 1024 n_streamline = 512 grid = project_gas_pixel_grid(data, resolution) sub_mask_velocity = 1024 * 2 x, y, _ = data.gas.coordinates[:].value.T u, v, _ = data.gas.velocities[:].value.T h = data.gas.smoothing_lengths.value u_grid = scatter(x, y, u, h, resolution * 2).T v_grid = scatter(x, y, v, h, resolution * 2).T x = np.linspace(0, 1, resolution * 2) y = np.linspace(0, 1, resolution * 2) speed = np.sqrt(u_grid * u_grid + v_grid * v_grid) fig, ax = plt.subplots(figsize=(8, 8), dpi=300) fig.subplots_adjust(0, 0, 1, 1) ax.axis("off") # ax.quiver( # x[::sub_mask_velocity], # y[::sub_mask_velocity], # u[::sub_mask_velocity], # v[::sub_mask_velocity],
seed(1234) print("Generating particles") x = rand(number_of_particles).astype(float64) y = rand(number_of_particles).astype(float64) h = rand(number_of_particles).astype(float32) * 0.2 m = ones_like(h) print("Finished generating particles") print("Compiling") t = time() scatter( array([0.0], dtype=float64), array([0.0], dtype=float64), array([1.0], dtype=float32), array([0.01], dtype=float32), 128, ) print(f"Took {time() - t} to compile.") print("Scattering") t = time() image = scatter(x, y, m, h, res) dt_us = time() - t print(f"Took {dt_us} to scatter.") try: from sphviewer.tools import QuickView import os
# We need to construct sum(T_j W_ij) / sum(W_ij) norm_grid = project_gas_pixel_grid(data, resolution, None) temp_grid = project_gas_pixel_grid(data, resolution, "temperature") # Imsave does not take a norm normalized_grid = temp_grid / norm_grid # Now we have to load the positions boxsize = data.metadata.boxsize[0] x, y, _ = data.gas.coordinates[:].T / boxsize u, v, w = data.gas.velocities[:].T / data.gas.velocities.max() h = data.gas.smoothing_length / boxsize # Generate the smoothed velocity maps u_grid = scatter(x, y, u, h, resolution).T / norm_grid.T v_grid = scatter(x, y, v, h, resolution).T / norm_grid.T w_grid = scatter(x, y, w, h, resolution).T / norm_grid.T x = np.linspace(0, 1, resolution) y = np.linspace(0, 1, resolution) # Calculate the actual speed for the line width speed = np.sqrt(u_grid * u_grid + v_grid * v_grid + w_grid * w_grid) lw = 0.5 * speed / speed.max() # Can't do imsave here, have to actually set up a figure. fig, ax = plt.subplots(figsize=(8, 8), dpi=resolution // 8) fig.subplots_adjust(0, 0, 1, 1) ax.axis("off") ax.streamplot(x,