diff --git a/bilby/gw/conversion.py b/bilby/gw/conversion.py index f128ad6292d607cbcbb9635dbbf73e0f299cf7cc..d33226ef511a732d4ec7441858c4481c02057ad3 100644 --- a/bilby/gw/conversion.py +++ b/bilby/gw/conversion.py @@ -4,6 +4,8 @@ from pandas import DataFrame from ..core.utils import logger, solar_mass from ..core.prior import DeltaFunction, Interped +from .utils import lalsim_SimInspiralTransformPrecessingNewInitialConditions + try: from astropy.cosmology import z_at_value, Planck15 @@ -12,12 +14,6 @@ except ImportError: logger.warning("You do not have astropy installed currently. You will" " not be able to use some of the prebuilt functions.") -try: - import lalsimulation as lalsim -except ImportError: - logger.warning("You do not have lalsuite installed currently. You will" - " not be able to use some of the prebuilt functions.") - def redshift_to_luminosity_distance(redshift): return Planck15.luminosity_distance(redshift).value @@ -87,10 +83,12 @@ def transform_precessing_spins(theta_jn, phi_jl, tilt_1, tilt_2, phi_12, a_1, spin_1x, spin_1y, spin_1z, spin_2x, spin_2y, spin_2z: float Cartesian spin components """ - iota, spin_1x, spin_1y, spin_1z, spin_2x, spin_2y, spin_2z = \ - lalsim.SimInspiralTransformPrecessingNewInitialConditions( - theta_jn, phi_jl, tilt_1, tilt_2, phi_12, a_1, a_2, mass_1, - mass_2, reference_frequency, phase) + + iota, spin_1x, spin_1y, spin_1z, spin_2x, spin_2y, spin_2z = ( + lalsim_SimInspiralTransformPrecessingNewInitialConditions( + theta_jn, phi_jl, tilt_1, tilt_2, phi_12, a_1, a_2, mass_1, mass_2, + reference_frequency, phase)) + return iota, spin_1x, spin_1y, spin_1z, spin_2x, spin_2y, spin_2z diff --git a/bilby/gw/source.py b/bilby/gw/source.py index 7d927b1c15123bfefad5a66dfa9c30f33ca97487..4f2fcd07393c87289b0f38e5f25efc33ae45d2b2 100644 --- a/bilby/gw/source.py +++ b/bilby/gw/source.py @@ -4,12 +4,16 @@ import numpy as np from ..core import utils from ..core.utils import logger +from .utils import (lalsim_SimInspiralTransformPrecessingNewInitialConditions, + lalsim_GetApproximantFromString, + lalsim_SimInspiralChooseFDWaveform, + lalsim_SimInspiralWaveformParamsInsertTidalLambda1, + lalsim_SimInspiralWaveformParamsInsertTidalLambda2) try: - import lalsimulation as lalsim import lal except ImportError: - logger.warning("You do not have lalsuite installed currently. You will " + logger.warning("You do not have lalsuite installed currently. You will" " not be able to use some of the prebuilt functions.") @@ -74,9 +78,10 @@ def lal_binary_black_hole( spin_2y = 0 spin_2z = a_2 else: - iota, spin_1x, spin_1y, spin_1z, spin_2x, spin_2y, spin_2z = \ - lalsim.SimInspiralTransformPrecessingNewInitialConditions( - iota, phi_jl, tilt_1, tilt_2, phi_12, a_1, a_2, mass_1, mass_2, reference_frequency, phase) + iota, spin_1x, spin_1y, spin_1z, spin_2x, spin_2y, spin_2z = ( + lalsim_SimInspiralTransformPrecessingNewInitialConditions( + iota, phi_jl, tilt_1, tilt_2, phi_12, a_1, a_2, mass_1, + mass_2, reference_frequency, phase)) longitude_ascending_nodes = 0.0 eccentricity = 0.0 @@ -84,12 +89,12 @@ def lal_binary_black_hole( waveform_dictionary = None - approximant = lalsim.GetApproximantFromString(waveform_approximant) + approximant = lalsim_GetApproximantFromString(waveform_approximant) maximum_frequency = frequency_array[-1] delta_frequency = frequency_array[1] - frequency_array[0] - hplus, hcross = lalsim.SimInspiralChooseFDWaveform( + hplus, hcross = lalsim_SimInspiralChooseFDWaveform( mass_1, mass_2, spin_1x, spin_1y, spin_1z, spin_2x, spin_2y, spin_2z, luminosity_distance, iota, phase, longitude_ascending_nodes, eccentricity, mean_per_ano, delta_frequency, @@ -159,12 +164,12 @@ def lal_eccentric_binary_black_hole_no_spins( waveform_dictionary = None - approximant = lalsim.GetApproximantFromString(waveform_approximant) + approximant = lalsim_GetApproximantFromString(waveform_approximant) maximum_frequency = frequency_array[-1] delta_frequency = frequency_array[1] - frequency_array[0] - hplus, hcross = lalsim.SimInspiralChooseFDWaveform( + hplus, hcross = lalsim_SimInspiralChooseFDWaveform( mass_1, mass_2, spin_1x, spin_1y, spin_1z, spin_2x, spin_2y, spin_2z, luminosity_distance, iota, phase, longitude_ascending_nodes, eccentricity, mean_per_ano, delta_frequency, @@ -306,15 +311,15 @@ def lal_binary_neutron_star( mean_per_ano = 0.0 waveform_dictionary = lal.CreateDict() - lalsim.SimInspiralWaveformParamsInsertTidalLambda1(waveform_dictionary, lambda_1) - lalsim.SimInspiralWaveformParamsInsertTidalLambda2(waveform_dictionary, lambda_2) + lalsim_SimInspiralWaveformParamsInsertTidalLambda1(waveform_dictionary, lambda_1) + lalsim_SimInspiralWaveformParamsInsertTidalLambda2(waveform_dictionary, lambda_2) - approximant = lalsim.GetApproximantFromString(waveform_approximant) + approximant = lalsim_GetApproximantFromString(waveform_approximant) maximum_frequency = frequency_array[-1] delta_frequency = frequency_array[1] - frequency_array[0] - hplus, hcross = lalsim.SimInspiralChooseFDWaveform( + hplus, hcross = lalsim_SimInspiralChooseFDWaveform( mass_1, mass_2, spin_1x, spin_1y, spin_1z, spin_2x, spin_2y, spin_2z, luminosity_distance, iota, phase, longitude_ascending_nodes, eccentricity, mean_per_ano, delta_frequency, diff --git a/bilby/gw/utils.py b/bilby/gw/utils.py index 668f42c0841019df3e70a7f109c6dfc3fa2c22f0..6c30a7f7e79448e8b13c418812a70b66ca5c3a26 100644 --- a/bilby/gw/utils.py +++ b/bilby/gw/utils.py @@ -14,6 +14,12 @@ except ImportError: logger.warning("You do not have gwpy installed currently. You will " " not be able to use some of the prebuilt functions.") +try: + import lalsimulation as lalsim +except ImportError: + logger.warning("You do not have lalsuite installed currently. You will" + " not be able to use some of the prebuilt functions.") + def asd_from_freq_series(freq_data, df): """ @@ -588,3 +594,77 @@ def plot_skymap(result, center='120d -40d', nside=512): lat.set_ticks_visible(False) fig.savefig('{}/{}_skymap.png'.format(result.outdir, result.label)) + + +def lalsim_SimInspiralTransformPrecessingNewInitialConditions( + theta_jn, phi_jl, tilt_1, tilt_2, phi_12, a_1, a_2, mass_1, mass_2, + reference_frequency, phase): + + for arg in (theta_jn, phi_jl, tilt_1, tilt_2, phi_12, a_1, a_2, mass_1, + mass_2, reference_frequency, phase): + try: + arg = float(arg) + except ValueError: + raise ValueError("Unable to convert inputs to floats") + + return lalsim.SimInspiralTransformPrecessingNewInitialConditions( + theta_jn, phi_jl, tilt_1, tilt_2, phi_12, a_1, a_2, mass_1, mass_2, + reference_frequency, phase) + + +def lalsim_GetApproximantFromString(waveform_approximant): + if isinstance(waveform_approximant, str): + return lalsim.GetApproximantFromString(waveform_approximant) + else: + raise ValueError("waveform_approximant must be of type str") + + +def lalsim_SimInspiralChooseFDWaveform( + mass_1, mass_2, spin_1x, spin_1y, spin_1z, spin_2x, spin_2y, + spin_2z, luminosity_distance, iota, phase, + longitude_ascending_nodes, eccentricity, mean_per_ano, delta_frequency, + minimum_frequency, maximum_frequency, reference_frequency, + waveform_dictionary, approximant): + + for arg in (mass_1, mass_2, spin_1x, spin_1y, spin_1z, spin_2x, spin_2y, + spin_2z, luminosity_distance, iota, phase, + longitude_ascending_nodes, eccentricity, mean_per_ano, + delta_frequency, minimum_frequency, maximum_frequency, + reference_frequency): + try: + arg = float(arg) + except ValueError: + raise ValueError("Unable to convert inputs to floats") + + # Note, this is the approximant number returns by GetApproximantFromString + if isinstance(approximant, int) is False: + raise ValueError("approximant not an int") + + return lalsim.SimInspiralChooseFDWaveform( + mass_1, mass_2, spin_1x, spin_1y, spin_1z, spin_2x, spin_2y, + spin_2z, luminosity_distance, iota, phase, + longitude_ascending_nodes, eccentricity, mean_per_ano, delta_frequency, + minimum_frequency, maximum_frequency, reference_frequency, + waveform_dictionary, approximant) + + +def lalsim_SimInspiralWaveformParamsInsertTidalLambda1( + waveform_dictionary, lambda_1): + try: + lambda_1 = float(lambda_1) + except ValueError: + raise ValueError("Unable to convert lambda_1 to float") + + return lalsim.SimInspiralWaveformParamsInsertTidalLambda1( + waveform_dictionary, lambda_1) + + +def lalsim_SimInspiralWaveformParamsInsertTidalLambda2( + waveform_dictionary, lambda_2): + try: + lambda_1 = float(lambda_2) + except ValueError: + raise ValueError("Unable to convert lambda_2 to float") + + return lalsim.SimInspiralWaveformParamsInsertTidalLambda1( + waveform_dictionary, lambda_1)