Skip to content

Commit

Permalink
Added utility function for generating smoothing lengths (#822)
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherlovell authored Feb 7, 2025
2 parents 8ab0106 + 545c24b commit 80b50e1
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 144 deletions.
22 changes: 3 additions & 19 deletions docs/source/galaxy/line_of_sight.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from scipy.spatial import cKDTree\n",
"from unyt import Mpc, Msun, Myr, yr\n",
"\n",
"from synthesizer.kernel_functions import Kernel\n",
Expand All @@ -29,22 +28,7 @@
"from synthesizer.particle.gas import Gas\n",
"from synthesizer.particle.particles import CoordinateGenerator\n",
"from synthesizer.particle.stars import sample_sfzh\n",
"\n",
"\n",
"def calculate_smoothing_lengths(positions, num_neighbors=56):\n",
" \"\"\"Calculate the SPH smoothing lengths for a set of coordinates.\"\"\"\n",
" tree = cKDTree(positions)\n",
" distances, _ = tree.query(positions, k=num_neighbors + 1)\n",
"\n",
" # The k-th nearest neighbor distance (k = num_neighbors)\n",
" kth_distances = distances[:, num_neighbors]\n",
"\n",
" # Set the smoothing length to the k-th nearest neighbor\n",
" # distance divided by 2.0\n",
" smoothing_lengths = kth_distances / 2.0\n",
"\n",
" return smoothing_lengths\n",
"\n",
"from synthesizer.particle.utils import calculate_smoothing_lengths\n",
"\n",
"# Define the grid (normally this would be defined by an SPS grid)\n",
"log10ages = np.arange(6.0, 10.5, 0.1)\n",
Expand Down Expand Up @@ -73,7 +57,7 @@
"coords = CoordinateGenerator.generate_3D_gaussian(nstars) * Mpc\n",
"\n",
"# Calculate the smoothing lengths\n",
"smls = calculate_smoothing_lengths(coords) * Mpc\n",
"smls = calculate_smoothing_lengths(coords)\n",
"\n",
"# Sample the parametric SFZH, producing a particle Stars object\n",
"# we will also pass some keyword arguments for some example attributes\n",
Expand Down Expand Up @@ -107,7 +91,7 @@
"coords = CoordinateGenerator.generate_3D_gaussian(ngas) * Mpc\n",
"\n",
"# Calculate the smoothing lengths\n",
"smls = calculate_smoothing_lengths(coords) * Mpc\n",
"smls = calculate_smoothing_lengths(coords)\n",
"\n",
"gas = Gas(\n",
" masses=np.random.uniform(10**6, 10**6.5, ngas) * Msun,\n",
Expand Down
22 changes: 3 additions & 19 deletions examples/particle/plot_get_los_attenutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import matplotlib.pyplot as plt
import numpy as np
from scipy.spatial import cKDTree
from unyt import Mpc, Msun, Myr

from synthesizer.grid import Grid
Expand All @@ -21,22 +20,7 @@
from synthesizer.particle.gas import Gas
from synthesizer.particle.particles import CoordinateGenerator
from synthesizer.particle.stars import sample_sfzh


def calculate_smoothing_lengths(positions, num_neighbors=56):
"""Calculate the SPH smoothing lengths for a set of coordinates."""
tree = cKDTree(positions)
distances, _ = tree.query(positions, k=num_neighbors + 1)

# The k-th nearest neighbor distance (k = num_neighbors)
kth_distances = distances[:, num_neighbors]

# Set the smoothing length to the k-th nearest neighbor
# distance divided by 2.0
smoothing_lengths = kth_distances / 2.0

return smoothing_lengths

from synthesizer.particle.utils import calculate_smoothing_lengths

plt.rcParams["font.family"] = "DeJavu Serif"
plt.rcParams["font.serif"] = ["Times New Roman"]
Expand Down Expand Up @@ -92,7 +76,7 @@ def calculate_smoothing_lengths(positions, num_neighbors=56):
)

# Calculate the smoothing lengths
smls = calculate_smoothing_lengths(coords, num_neighbors=56) * Mpc
smls = calculate_smoothing_lengths(coords, num_neighbours=56)

# Sample the SFZH, producing a Stars object
# we will also pass some keyword arguments for attributes
Expand Down Expand Up @@ -122,7 +106,7 @@ def calculate_smoothing_lengths(positions, num_neighbors=56):
)

# Calculate the smoothing lengths
smls = calculate_smoothing_lengths(coords, num_neighbors=56) * Mpc
smls = calculate_smoothing_lengths(coords, num_neighbours=56)

gas = Gas(
masses=np.random.uniform(10**6, 10**6.5, ngas) * Msun,
Expand Down
29 changes: 7 additions & 22 deletions examples/particle/plot_los_spectra.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import matplotlib.pyplot as plt
import numpy as np
from scipy.spatial import cKDTree
from unyt import Mpc, Msun, Myr

from synthesizer.emission_models import TotalEmission
Expand All @@ -23,26 +22,12 @@
from synthesizer.particle.gas import Gas
from synthesizer.particle.particles import CoordinateGenerator
from synthesizer.particle.stars import sample_sfzh
from synthesizer.particle.utils import calculate_smoothing_lengths

plt.rcParams["font.family"] = "DeJavu Serif"
plt.rcParams["font.serif"] = ["Times New Roman"]


def calculate_smoothing_lengths(positions, num_neighbors=56):
"""Calculate the SPH smoothing lengths for a set of coordinates."""
tree = cKDTree(positions)
distances, _ = tree.query(positions, k=num_neighbors + 1)

# The k-th nearest neighbor distance (k = num_neighbors)
kth_distances = distances[:, num_neighbors]

# Set the smoothing length to the k-th nearest neighbor
# distance divided by 2.0
smoothing_lengths = kth_distances / 2.0

return smoothing_lengths


# Set the seed
np.random.seed(42)

Expand Down Expand Up @@ -89,7 +74,7 @@ def calculate_smoothing_lengths(positions, num_neighbors=56):
ngas = 1000

# Generate some random coordinates
coords = CoordinateGenerator.generate_3D_gaussian(nstars)
coords = CoordinateGenerator.generate_3D_gaussian(nstars) * Mpc

# Calculate smoothing lengths
smls = calculate_smoothing_lengths(coords)
Expand All @@ -102,25 +87,25 @@ def calculate_smoothing_lengths(positions, num_neighbors=56):
param_stars.log10ages,
param_stars.log10metallicities,
nstars,
coordinates=coords * Mpc,
coordinates=coords,
current_masses=np.full(nstars, 10**8.7 / nstars) * Msun,
smoothing_lengths=smls * Mpc,
smoothing_lengths=smls,
redshift=1,
)

# Now make the gas

# Generate some random coordinates
coords = CoordinateGenerator.generate_3D_gaussian(ngas)
coords = CoordinateGenerator.generate_3D_gaussian(ngas) * Mpc

# Calculate the smoothing lengths
smls = calculate_smoothing_lengths(coords)

gas = Gas(
masses=np.random.uniform(10**6, 10**6.5, ngas) * Msun,
metallicities=np.random.uniform(0.01, 0.05, ngas),
coordinates=coords * Mpc,
smoothing_lengths=smls * Mpc,
coordinates=coords,
smoothing_lengths=smls,
dust_to_metal_ratio=0.2,
)

Expand Down
22 changes: 3 additions & 19 deletions examples/particle/plot_rotated_galaxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,11 @@

import matplotlib.pyplot as plt
import numpy as np
from scipy.spatial import cKDTree
from unyt import Msun, Myr, degree, km, kpc, s

from synthesizer.kernel_functions import Kernel
from synthesizer.particle import CoordinateGenerator, Galaxy, Gas, Stars


def calculate_smoothing_lengths(positions, num_neighbors=56):
"""Calculate the SPH smoothing lengths for a set of coordinates."""
tree = cKDTree(positions)
distances, _ = tree.query(positions, k=num_neighbors + 1)

# The k-th nearest neighbor distance (k = num_neighbors)
kth_distances = distances[:, num_neighbors]

# Set the smoothing length to the k-th nearest neighbor
# distance divided by 2.0
smoothing_lengths = kth_distances / 2.0

return smoothing_lengths

from synthesizer.particle.utils import calculate_smoothing_lengths

# Set the seed
np.random.seed(42)
Expand Down Expand Up @@ -79,7 +63,7 @@ def calculate_smoothing_lengths(positions, num_neighbors=56):
initial_masses = masses.copy()
redshift = 0.0
centre = np.array([0.0, 0.0, 0.0]) * kpc
smoothing_lengths = calculate_smoothing_lengths(coords) * kpc
smoothing_lengths = calculate_smoothing_lengths(coords)

# We'll start by simply using some stars
stars = Stars(
Expand Down Expand Up @@ -151,7 +135,7 @@ def calculate_smoothing_lengths(positions, num_neighbors=56):
redshift=redshift,
centre=centre,
dust_to_metal_ratio=0.3,
smoothing_lengths=calculate_smoothing_lengths(gas_coords) * kpc,
smoothing_lengths=calculate_smoothing_lengths(gas_coords),
)

# Make the galaxy
Expand Down
18 changes: 1 addition & 17 deletions profiling/compare_los_loop_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.lines import Line2D
from scipy.spatial import cKDTree
from unyt import Myr

from synthesizer.grid import Grid
Expand All @@ -24,22 +23,7 @@
from synthesizer.particle.gas import Gas
from synthesizer.particle.particles import CoordinateGenerator
from synthesizer.particle.stars import sample_sfzh


def calculate_smoothing_lengths(positions, num_neighbors=56):
"""Calculate the SPH smoothing lengths for a set of coordinates."""
tree = cKDTree(positions)
distances, _ = tree.query(positions, k=num_neighbors + 1)

# The k-th nearest neighbor distance (k = num_neighbors)
kth_distances = distances[:, num_neighbors]

# Set the smoothing length to the k-th nearest neighbor
# distance divided by 2.0
smoothing_lengths = kth_distances / 2.0

return smoothing_lengths

from synthesizer.particle.utils import calculate_smoothing_lengths

plt.rcParams["font.family"] = "DeJavu Serif"
plt.rcParams["font.serif"] = ["Times New Roman"]
Expand Down
17 changes: 1 addition & 16 deletions profiling/image_strong_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import matplotlib.pyplot as plt
import numpy as np
from astropy.cosmology import Planck15 as cosmo
from scipy.spatial import cKDTree
from unyt import Myr, kpc

from synthesizer.emission_models import IncidentEmission
Expand All @@ -27,6 +26,7 @@
from synthesizer.particle.galaxy import Galaxy
from synthesizer.particle.particles import CoordinateGenerator
from synthesizer.particle.stars import sample_sfzh
from synthesizer.particle.utils import calculate_smoothing_lengths

plt.rcParams["font.family"] = "DeJavu Serif"
plt.rcParams["font.serif"] = ["Times New Roman"]
Expand All @@ -35,21 +35,6 @@
np.random.seed(42)


def calculate_smoothing_lengths(positions, num_neighbors=56):
"""Calculate the SPH smoothing lengths for a set of coordinates."""
tree = cKDTree(positions)
distances, _ = tree.query(positions, k=num_neighbors + 1)

# The k-th nearest neighbor distance (k = num_neighbors)
kth_distances = distances[:, num_neighbors]

# Set the smoothing length to the k-th nearest neighbor
# distance divided by 2.0
smoothing_lengths = kth_distances / 2.0

return smoothing_lengths


def image_strong_scaling(
basename,
max_threads=8,
Expand Down
17 changes: 1 addition & 16 deletions profiling/los_surf_strong_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np
from scipy.spatial import cKDTree
from unyt import Myr

from synthesizer.kernel_functions import Kernel
Expand All @@ -24,6 +23,7 @@
from synthesizer.particle.gas import Gas
from synthesizer.particle.particles import CoordinateGenerator
from synthesizer.particle.stars import sample_sfzh
from synthesizer.particle.utils import calculate_smoothing_lengths

plt.rcParams["font.family"] = "DeJavu Serif"
plt.rcParams["font.serif"] = ["Times New Roman"]
Expand All @@ -32,21 +32,6 @@
np.random.seed(42)


def calculate_smoothing_lengths(positions, num_neighbors=56):
"""Calculate the SPH smoothing lengths for a set of coordinates."""
tree = cKDTree(positions)
distances, _ = tree.query(positions, k=num_neighbors + 1)

# The k-th nearest neighbor distance (k = num_neighbors)
kth_distances = distances[:, num_neighbors]

# Set the smoothing length to the k-th nearest neighbor
# distance divided by 2.0
smoothing_lengths = kth_distances / 2.0

return smoothing_lengths


def los_surface_density_strong_scaling(
basename,
max_threads=8,
Expand Down
17 changes: 1 addition & 16 deletions profiling/spectral_cube_strong_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import matplotlib.pyplot as plt
import numpy as np
from astropy.cosmology import Planck15 as cosmo
from scipy.spatial import cKDTree
from unyt import Myr, kpc

from synthesizer.emission_models import IncidentEmission
Expand All @@ -27,6 +26,7 @@
from synthesizer.particle.galaxy import Galaxy
from synthesizer.particle.particles import CoordinateGenerator
from synthesizer.particle.stars import sample_sfzh
from synthesizer.particle.utils import calculate_smoothing_lengths

plt.rcParams["font.family"] = "DeJavu Serif"
plt.rcParams["font.serif"] = ["Times New Roman"]
Expand All @@ -35,21 +35,6 @@
np.random.seed(42)


def calculate_smoothing_lengths(positions, num_neighbors=56):
"""Calculate the SPH smoothing lengths for a set of coordinates."""
tree = cKDTree(positions)
distances, _ = tree.query(positions, k=num_neighbors + 1)

# The k-th nearest neighbor distance (k = num_neighbors)
kth_distances = distances[:, num_neighbors]

# Set the smoothing length to the k-th nearest neighbor
# distance divided by 2.0
smoothing_lengths = kth_distances / 2.0

return smoothing_lengths


def cube_strong_scaling(
basename,
max_threads=8,
Expand Down
Loading

0 comments on commit 80b50e1

Please sign in to comment.