diff --git a/bilby/gw/conversion.py b/bilby/gw/conversion.py index 9ee2ec66d39b8fce56cf8b1e2be4ac5c2b996067..bec64ef878cd5b7f0a9563159713d57ae57953f3 100644 --- a/bilby/gw/conversion.py +++ b/bilby/gw/conversion.py @@ -244,19 +244,28 @@ def convert_to_lal_binary_black_hole_parameters(parameters): converted_parameters[key]) converted_parameters['cos_tilt_{}'.format(idx)] = \ np.sign(converted_parameters[key]) - converted_parameters['phi_jl'] = 0.0 - converted_parameters['phi_12'] = 0.0 else: - converted_parameters[f"cos_tilt_{idx}"] = ( - converted_parameters[key] / converted_parameters[f"a_{idx}"] - ) + with np.errstate(invalid="raise"): + try: + converted_parameters[f"cos_tilt_{idx}"] = ( + converted_parameters[key] / converted_parameters[f"a_{idx}"] + ) + except (FloatingPointError, ZeroDivisionError): + logger.debug( + "Error in conversion to spherical spin tilt. " + "This is often due to the spin parameters being zero. " + f"Setting cos_tilt_{idx} = 1." + ) + converted_parameters[f"cos_tilt_{idx}"] = 1.0 + + for key in ["phi_jl", "phi_12"]: + if key not in converted_parameters: + converted_parameters[key] = 0.0 for angle in ['tilt_1', 'tilt_2', 'theta_jn']: cos_angle = str('cos_' + angle) if cos_angle in converted_parameters.keys(): - with np.errstate(invalid="ignore"): - converted_parameters[angle] =\ - np.arccos(converted_parameters[cos_angle]) + converted_parameters[angle] = np.arccos(converted_parameters[cos_angle]) if "delta_phase" in original_keys: with np.errstate(invalid="ignore"): @@ -782,6 +791,7 @@ def _generate_all_cbc_parameters(sample, defaults, base_conversion, logger.debug('Assuming {} = {}'.format(key, default)) output_sample = fill_from_fixed_priors(output_sample, priors) + output_sample, _ = base_conversion(output_sample) if likelihood is not None: if ( hasattr(likelihood, 'phase_marginalization') or @@ -820,7 +830,6 @@ def _generate_all_cbc_parameters(sample, defaults, base_conversion, ) if likelihood is not None: compute_snrs(output_sample, likelihood) - output_sample, _ = base_conversion(output_sample) for key, func in zip(["mass", "spin", "source frame"], [ generate_mass_parameters, generate_spin_parameters, generate_source_frame_parameters]): diff --git a/test/gw/conversion_test.py b/test/gw/conversion_test.py index 3914f1e0735b570162d24dee78cc00c7a6e733ca..5df2a74997cab417ecf3cf5754d8de5e48793ad1 100644 --- a/test/gw/conversion_test.py +++ b/test/gw/conversion_test.py @@ -296,6 +296,55 @@ class TestConvertToLALParams(unittest.TestCase): dict(a_1=a_1, tilt_1=tilt_1, phi_jl=phi_jl, phi_12=phi_12), ) + def test_bbh_zero_aligned_spin_to_spherical_with_magnitude(self): + """ + Test the the conversion returns the correct tilt angles when zero + aligned spin is passed if the magnitude is also pass. + + If the magnitude is zero this returns tilt = 0. + If the magnitude is non-zero this returns tilt = pi. + """ + self.parameters["chi_1"] = 0 + self.parameters["chi_2"] = 0 + a_1 = 0 + self.parameters["a_1"] = a_1 + a_2 = 1 + self.parameters["a_2"] = a_2 + tilt_1 = 0 + tilt_2 = np.pi / 2 + phi_jl = 0 + phi_12 = 0 + self.bbh_convert() + self.assertDictEqual( + { + key: self.parameters[key] + for key in ["a_1", "a_2", "tilt_1", "tilt_2", "phi_12", "phi_jl"] + }, + dict(a_1=a_1, a_2=a_2, tilt_1=tilt_1, tilt_2=tilt_2, phi_jl=phi_jl, phi_12=phi_12), + ) + + def test_bbh_zero_aligned_spin_to_spherical_without_magnitude(self): + """ + Test the the conversion returns the correct tilt angles when zero + aligned spin is passed if the magnitude is also pass. + + If the magnitude is zero this returns tilt = 0. + If the magnitude is non-zero this returns tilt = pi. + """ + self.parameters["chi_1"] = 0 + a_1 = 0 + tilt_1 = np.pi / 2 + phi_jl = 0 + phi_12 = 0 + self.bbh_convert() + self.assertDictEqual( + { + key: self.parameters[key] + for key in ["a_1", "tilt_1", "phi_12", "phi_jl"] + }, + dict(a_1=a_1, tilt_1=tilt_1, phi_jl=phi_jl, phi_12=phi_12), + ) + def test_bbh_cos_angle_to_angle_conversion(self): self.parameters["cos_tilt_1"] = 1 t1 = np.arccos(self.parameters["cos_tilt_1"])