#!pip install -I "phoebe>=2.4,<2.5"

"""
Explanation: Critical Radii: Semidetached Systems
Setup
Let's first make sure we have the latest version of PHOEBE 2.4 installed (uncomment this line if running in an online notebook session such as colab).
End of explanation
"""


import phoebe
from phoebe import u # units
import numpy as np
import matplotlib.pyplot as plt

logger = phoebe.logger()

b = phoebe.default_binary()

"""
Explanation: As always, let's do imports and initialize a logger and a new Bundle.
End of explanation
"""


b.add_constraint('semidetached', 'primary')

"""
Explanation: Semi-Detached Systems
Semi-detached systems are implemented by constraining the value of requiv to be the same as requiv_max by appyling the 'semidetached' constraint on the 'primary' component.
End of explanation
"""


b['requiv@constraint@primary']

"""
Explanation: We can view the constraint on requiv by accessing the constraint:
End of explanation
"""


b['requiv_max@constraint@primary']

"""
Explanation: Now whenever any of the relevant parameters (q, ecc, syncpar, sma) are changed, the value of requiv will change to match the critical value as defined by requiv_max.
End of explanation
"""


b.add_dataset('lc', compute_phases=phoebe.linspace(0,1,101))

try:
    b.run_compute()
except Exception as err:
    print(err)

"""
Explanation: Note that the point region in semi-detached systems often falls outside of the atmosphere tables.  Since this requires the full mesh, this cannot be checked in advance with b.run_checks but will raise an error during b.run_compute.
End of explanation
"""


b.set_value('atm', component='primary', value='blackbody')

b.set_value('ld_mode', component='primary', value='manual')

"""
Explanation: In this case, it is necessary to fallback on blackbody atmospheres and manually provide the limb-darkening function and coefficients:
End of explanation
"""


print(b.filter(qualifier=['ld_func', 'ld_coeffs']))

b.run_compute()

"""
Explanation: The default values of the ld_func and ld_coeffs should be changed to appropriate values for a given system, but should still compute without errors.
End of explanation
"""


