<jupyter_start><jupyter_text>A Tutorial for the Bell Energy Balance Model (Bell_EBM)  Author: <NAME> (McGill University) Last Modified: 2019-02-15<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import astropy.constants as const

import Bell_EBM as ebm
import Bell_EBM.pretty_plotting<jupyter_output><empty_output><jupyter_text>First, let's setup a planet, star, and system    We'll use the WASP-12b system and include the effects of H2 dissociation (Bell & Cowan 2018)<jupyter_code># WASP-12b
# Periods are in units of days
# All other units are in SI (m, kg, s, K, J, Pa, etc.)
# All angles are in degrees
planet = ebm.Planet('bell2018', rad=1.900*const.R_jup.value, mass=1.470*const.M_jup.value,
                    Porb=1.09142030, a=0.02340*const.au.value, inc=83.37, vWind=5e3)

# WASP-12
# Radius and mass are in solar units
# Temperatures are always in K
star = ebm.Star(teff=6300., rad=1.59, mass=1.20)

# Load the star and planet into a system
system = ebm.System(star, planet)<jupyter_output><empty_output><jupyter_text>Let's show the star-planet system<jupyter_code>fig = planet.orbit.plot_orbit()
plt.show()<jupyter_output><empty_output><jupyter_text>Now, let's model the planet's atmoshere<jupyter_code># Run initial burn-in - try guessing the equilibrium temperature to begin with
Teq = system.get_teq()
T0 = np.ones_like(system.planet.map.values)*Teq
t0 = 0.
t1 = t0+system.planet.Porb*2
dt = system.planet.Porb/1000.
times, maps = system.run_model(T0, t0, t1, dt, verbose=False)<jupyter_output><empty_output><jupyter_text>Then we can visualize the planet's temperature and H2 dissociation map Even with only moderate winds, there is a strong hotspot offset due to the effects of H2 dissociation/recombination<jupyter_code>fig = system.planet.plot_map()
plt.show()

fig = system.planet.plot_H2_dissociation()
plt.show()<jupyter_output><empty_output><jupyter_text>Finally, we can look at the orbital phase curve<jupyter_code># Plot the lightcurves - by default it will use one planetary orbit
fig = system.plot_lightcurve()
plt.show()

# Plot the temperature curves
fig = system.plot_tempcurve()
plt.show()<jupyter_output><empty_output><jupyter_text>Now let's look at an eccentric rocky planet in a 3:2 spin-orbit resonance<jupyter_code># We can also specify a rotation rate for the planet: we'll say it somehow isn't
# tidally locked yet. We also don't have to specify the planet's orbital period:
# it will be calculated using Kepler's equations
planet = ebm.Planet('rock', rad=const.R_earth.value, mass=const.M_earth.value,
                    a=0.05*const.au.value, e=0.4, argp=45.)

# The default values are the sun's values already
star = ebm.Star()

# Load everything into the system: this is when the orbital period is calculated
system = ebm.System(star, planet)

# Now that we know the orbital period, we can set the planet's rotation period
system.planet.Prot = system.planet.Porb*(2./3.)
# The plotted dots have equal spacing in time, so the get scruntched up near apastron
fig = planet.orbit.plot_orbit()
plt.show()
# Run initial burn-in - try guessing the median irradiation temperature to begin with
Teq = np.median(system.get_teq(np.linspace(0.,system.planet.Porb,1000, endpoint=False)))
T0 = Teq*np.ones_like(system.planet.map.values)
t0 = 0.
t1 = t0+system.planet.Porb*10
dt = system.planet.Porb/1000.
times, maps = system.run_model(T0, t0, t1, dt, verbose=False)<jupyter_output><empty_output><jupyter_text>Because this is an eccentric planet and it isn't synchronously rotating, it's map isn't constant with time.  As a result, we need to grab the map and time stamps for one orbit to do our plotting<jupyter_code># Run a production run with intermediates=True to get the times and maps at each time step
T0 = maps[-1]
t0 = times[-1]
t1 = t0+system.planet.Porb
dt = system.planet.Porb/1000.
times, maps = system.run_model(T0, t0, t1, dt, verbose=False, intermediates=True)
testLightcurve = system.lightcurve(times, maps)
plt.plot((testLightcurve))<jupyter_output><empty_output><jupyter_text>Let's show the map during periastron For planet's which are not synchronously rotating, the longitude of zero in all plots refers to the sub-stellar point. This requires that we pass in the time of the map though, so it can rotate it as necessary<jupyter_code>phases = system.get_phase(times)
phasePeri = system.get_phase_periastron()
indexPeri = np.argmin(np.abs(phases-phasePeri))
subStellLon = system.planet.orbit.get_ssp(system.planet.orbit.t_peri)[0]

fig = system.planet.plot_map(maps[indexPeri], subStellLon)
plt.show()<jupyter_output><empty_output><jupyter_text>This time, let's look at the lightcurve at the Spitzer/IRAC 4.5 micron band<jupyter_code># Plot the lightcurves
fig = system.plot_lightcurve(times, maps, bolo=False, wav=4.5e-6)
plt.show()

# Plot the temperature curves
fig = system.plot_tempcurve(times, maps, bolo=False, wav=4.5e-6)
plt.show()<jupyter_output><empty_output><jupyter_text>It might seem odd that the coldest temperature on the plot above is near periastron when the planet is receiving the most flux, but this is because we are looking at the planet's cold nightside at that point in time  Now let's try to model the Earth (a pretty challenging task...) to demonstrate the flexibility, strengths, and weaknesses of this EBM.  We'll need to make some simplifying assumptions. We'll pretend that we only probe the atmosphere (pretending the atmosphere is opaque) and assume the atmosphere is made of pure N2. Feel free to instead model the Earth as pure rock or water, and see how the results change!<jupyter_code># The Earth's atmosphere is mostly N2, so we'll change the heat capacity
# of the gas from H2 (the default) to N2
cp_N2 = 1.039e3 # J/(kg K)

# We'll pretend the whole atmosphere absorbs and radiates
P0 = const.atm.value

a = const.au.value # AU

Rp = const.R_earth.value # m

Mp = const.M_earth.value # kg

Prot = 1. # days

albedo = 0.30

e = 0.0167

obliquity = 23.5 # degrees

# We'll assume that the Earth transits during the northern hemisphere's summer solstice
argp = -90.
t0 = 0.

planet = ebm.Planet('gas', rad=Rp, mass=Mp, Prot=Prot, a=a, e=e, obliq=obliquity,
                    argp=argp, t0=t0, cp=cp_N2, mlDepth=P0, albedo=albedo)

# The default values are the sun's values already
star = ebm.Star()

# Load everything into the system
system = ebm.System(star, planet)<jupyter_output><empty_output><jupyter_text>Given the extreme difference between the spin and orbital periods, burning in this planet is a little trickier and takes longer<jupyter_code># Run initial burn-in - try guessing the median irradiation temperature to begin with
# Run for two full-orbits to work out seasonal variations
T0 = system.get_teq()*np.ones_like(system.planet.map.values)
t0 = planet.t0
t1 = t0+system.planet.Porb*2.-system.planet.Prot
dt = system.planet.Prot/10.
times, maps = system.run_model(T0, t0, t1, dt, verbose=False)

# Run one day at high-rez to get good temperatures around the planet
T0 = maps[-1]
t0 = times[-1]
t1 = t0+system.planet.Prot
dt = system.planet.Prot/100.
times, maps = system.run_model(T0, t0, t1, dt, verbose=False)<jupyter_output><empty_output><jupyter_text>Run a production run<jupyter_code>T0 = maps[-1]
t0 = times[-1]
t1 = t0+system.planet.Porb
# We'll use a small time step to make sure we're doing a good job
dt = system.planet.Prot/10.
times, maps = system.run_model(T0, t0, t1, dt, verbose=False, intermediates=True)

# Then we'll cut down on the number of points, since our full-orbit phasecurves
# don't need that high of a time resolution. Just daily outputs are fine
times = times[::10]
maps = maps[::10]<jupyter_output><empty_output><jupyter_text>Let's get the lightcurves ignoring and accounting for reflected light to demonstrate the difference at blue wavelengths<jupyter_code>lc_reflected = system.lightcurve(times, maps, bolo=False, wav=0.5e-6,
                                 allowThermal=False, allowReflect=True)
lc_thermal = system.lightcurve(times, maps, bolo=False, wav=0.5e-6,
                               allowThermal=True, allowReflect=False)
x = times.flatten()
x = (x-planet.Porb*int(np.median(x/planet.Porb)))

fig = plt.figure(figsize=(12,4))
plt.plot(x, lc_reflected*1e9, c='b', label=r'$\rm Reflected~Flux$')
plt.plot(x, lc_thermal*1e9, c='r', label=r'$\rm Thermal~Flux$')
plt.ylabel(r'$F_p/F_* \rm ~(ppb)$')
plt.xlabel(r'$\rm Time~from~Transit~(days)$')
plt.xlim(0, 365)
plt.legend(loc='best')
plt.show()
plt.close(fig)<jupyter_output><empty_output><jupyter_text>We're seeing here that at short wavelengths nearly all of the flux comes from reflected light, and the amount of light we see varies due to the Earth going through different phases (like the Moon does from our perspective). The blue curve just ends up being the Lambertian phase function  This time, let's look at the peak in Earth's thermal emission (~10 microns) where reflected light shouldn't matter near as much<jupyter_code>lc_reflected = system.lightcurve(times, maps, bolo=False, wav=10e-6,
                                 allowThermal=False, allowReflect=True)
lc_thermal = system.lightcurve(times, maps, bolo=False, wav=10e-6,
                               allowThermal=True, allowReflect=False)
x = times.flatten()
x = (x-planet.Porb*int(np.median(x/planet.Porb)))

fig = plt.figure(figsize=(12,4))
plt.plot(x, lc_reflected*1e9, c='b', label=r'$\rm Reflected~Flux$')
plt.plot(x, lc_thermal*1e9, c='r', label=r'$\rm Thermal~Flux$')
plt.ylabel(r'$F_p/F_* \rm ~(ppb)$')
plt.xlabel(r'$\rm Time~from~Transit~(days)$')
plt.xlim(0, 365)
plt.legend(loc='best')
plt.show()
plt.close(fig)<jupyter_output><empty_output><jupyter_text>We see that there is a significant offset in the peak of the phasecurve due to the rapid rotation of the planet, and we also see that thermal emission indeed dominates  While this EBM is quite flexible, it is by no means perfect, and you should evaluate your findings with caution and scepticism (as with all things in science).  Let's show the planet's map at the time of the last timestep in the production run (which is during the Northern Hemisphere's Summer Solstice)<jupyter_code>fig = system.planet.plot_map()
plt.show()<jupyter_output><empty_output><jupyter_text>As you can see, the map doesn't appear to make a lot of sense - the south pole is far too cold (-100°C), the tropics are too cold, and the maximum temperature is only ~6°C. This is because we haven't taken into account many important features like any north-south heat transport (e.g. Hadley circulation), ocean currents, spatially varying albedo, the contribution of flux from the surface (the Earth's atmosphere is not opaque), latent heat, greenhouse gasses...: the list goes on. This EBM is pretty flexible, but it is definitely idealized!  We do get the year-averaged equator-to-pole temperature difference (ETPD) in roughly the correct ballpark though. For reference, on earth the ETPD is ~40°C for the north pole, and ~70°C for the south pole.<jupyter_code>meanMap = np.mean(maps, axis=0)
EPTD = np.max(meanMap)-np.min(meanMap) # Average Equator-to-pole temperature difference
print('Average equator-to-pole temperature difference is ~'+str(int(np.rint(EPTD)))+'°C.')<jupyter_output>Average equator-to-pole temperature difference is ~66°C.
{'fraction_non_alphanumeric': 0.10408163265306122, 'fraction_numerical': 0.021827861579414375, 'mean_word_length': 4.954041204437401, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '406152', 'license': 'MIT', 'n_tokens_mistral': 4041, 'n_tokens_neox': 3730, 'n_words': 1258, 'path': 'MiscNotebookFiles/Bell_EBM_Tutorial.ipynb', 'repo_name': 'ZYVE255/ebm-optimizer', 'size': '11281'}