Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redshift alignment for simple rays #196

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
62 changes: 47 additions & 15 deletions trident/light_ray.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,11 @@ def _calculate_light_ray_solution(self, seed=None,
def make_light_ray(self, seed=None, periodic=True,
left_edge=None, right_edge=None, min_level=None,
start_position=None, end_position=None,
trajectory=None,
fields=None, setup_function=None,
trajectory=None, fields=None, setup_function=None,
solution_filename=None, data_filename=None,
get_los_velocity=None, use_peculiar_velocity=True,
redshift=None, field_parameters=None, njobs=-1):
redshift=None, redshift_align=None,
field_parameters=None, njobs=-1):
"""
Actually generate the LightRay by traversing the desired dataset.

Expand Down Expand Up @@ -419,7 +419,19 @@ def make_light_ray(self, seed=None, periodic=True,
redshift will be 0 for a non-cosmological dataset and
the dataset redshift for a cosmological dataset.
Default: None.


:redshift_align: optional, str

Used with light rays with single datasets to specify where the
redshift is "equal" to the redshift of the dataset, or "redshift"
if that was specified. If "start" or None, the line start will be
at this redshift and the line end will be at a lower redshift.
Other options are "center", so the line start will be greater than
this redshift, the line end will be less, and they will align at the
midpoint, and "everywhere", so the line will be at this redshift
throughout.
Default: None.

:field_parameters: optional, dict
Used to set field parameters in light rays. For example,
if the 'bulk_velocity' field parameter is set, the relative
Expand Down Expand Up @@ -517,13 +529,14 @@ def make_light_ray(self, seed=None, periodic=True,
self._data = {}
# temperature field is automatically added to fields
if fields is None: fields = []
if ('gas', 'temperature') not in fields:
fields.append(('gas', 'temperature'))
if (('gas', 'temperature') not in fields) and \
('temperature' not in fields):
fields.append(('gas', 'temperature'))
data_fields = fields[:]
all_fields = fields[:]
all_fields.extend(['l', 'dl', 'redshift'])
all_fields.extend(['x', 'y', 'z'])
data_fields.extend(['x', 'y', 'z'])
all_fields.extend([('gas','x'),('gas','y'),('gas','z')])
data_fields.extend([('gas','x'),('gas','y'),('gas','z')])
if use_peculiar_velocity:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change wasn't actually meant to be part of the PR, was just a random bug fix. I don't remember exactly the problem here, but if there is a more elegant solution to this I'm happy to change it

all_fields.extend(['relative_velocity_x', 'relative_velocity_y',
'relative_velocity_z',
Expand Down Expand Up @@ -552,7 +565,10 @@ def make_light_ray(self, seed=None, periodic=True,
mylog.warning("Generating light ray with different redshift than " +
"the dataset itself.")
my_segment["redshift"] = redshift


if redshift_align is None:
redshift_align = 'start'

if setup_function is not None:
setup_function(ds)

Expand All @@ -567,8 +583,8 @@ def make_light_ray(self, seed=None, periodic=True,
segment_length = my_segment["traversal_box_fraction"] * \
ds.domain_width[0].in_units("Mpccm / h")
next_redshift = my_segment["redshift"] - \
self._deltaz_forward(my_segment["redshift"],
segment_length)
self._deltaz_forward(my_segment["redshift"],
segment_length)
elif my_segment.get("next", None) is None:
next_redshift = self.near_redshift
else:
Expand Down Expand Up @@ -675,10 +691,26 @@ def make_light_ray(self, seed=None, periodic=True,
sub_data[key] = ds.arr(sub_data[key]).in_cgs()

# Get redshift for each lixel. Assume linear relation between l
# and z. so z = z_start - z_range * (l / l_range)
sub_data[('gas', 'redshift')] = my_segment['redshift'] - \
(sub_data[('gas', 'l')] / ray_length) * \
(my_segment['redshift'] - next_redshift)
# and z, depending on where they should be "aligned".
# 'start': z = z_dataset - z_range * (l / l_range)
# 'center': z = z_dataset - z_range * ((l - l_range/2) / l_range)
# 'everywhere': z = z_dataset
if redshift_align == 'start':
sub_data[('gas','redshift')] = my_segment['redshift'] - \
(sub_data[('gas','l')] / ray_length) * \
(my_segment['redshift'] - next_redshift)
elif redshift_align == 'center':
sub_data[('gas','redshift')] = my_segment['redshift'] - \
((sub_data[('gas','l')]-ray_length/2) / ray_length) * \
(my_segment['redshift'] - next_redshift)
elif redshift_align == 'everywhere':
sub_data[('gas','redshift')] = np.ones(sub_data[('gas','l')].shape)*\
my_segment['redshift']
else:
mylog.warning(f'redshift_align {redshift_align} not recognized. '+\
'Using z = z_start for full length')
sub_data[('gas','redshift')] = np.ones(sub_data[('gas','l')].shape)*\
my_segment['redshift']

# When using the peculiar velocity, create effective redshift
# (redshift_eff) field combining cosmological redshift and
Expand Down
17 changes: 14 additions & 3 deletions trident/ray_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
def make_simple_ray(dataset_file, start_position, end_position,
lines=None, ftype="gas", fields=None,
solution_filename=None, data_filename=None,
trajectory=None, redshift=None, field_parameters=None,
trajectory=None, redshift=None,
redshift_align='start', field_parameters=None,
setup_function=None, load_kwargs=None,
line_database=None, ionization_table=None):
"""
Expand Down Expand Up @@ -124,10 +125,19 @@ def make_simple_ray(dataset_file, start_position, end_position,

:redshift: float, optional

Sets the highest cosmological redshift of the ray. By default, it will
Sets the base cosmological redshift of the ray. By default, it will
use the cosmological redshift of the dataset, if set, and if not set,
it will use a redshift of 0.
Default: None

:redshift_align: optional, str

Sets the location where the cosmological redshift "aligns" with the base
cosmological redshift. Default is the start of the ray, other options are
'center', so the start will be higher redshift and the end will be lower,
and 'everywhere', so the full length will be at the base cosmological
redshift
Default: 'start'

:field_parameters: optional, dict
Used to set field parameters in light rays. For example,
Expand Down Expand Up @@ -231,7 +241,8 @@ def make_simple_ray(dataset_file, start_position, end_position,
solution_filename=solution_filename,
data_filename=data_filename,
field_parameters=field_parameters,
redshift=redshift)
redshift=redshift,
redshift_align=redshift_align)

def make_compound_ray(parameter_filename, simulation_type,
near_redshift, far_redshift,
Expand Down