<jupyter_start><jupyter_text>HII Region Thermal EquilibriumFigure 4.4 from Chapter 4 of *Interstellar and Intergalactic Medium* by <NAME>, 2021, Cambridge University Press.Plot of the heating and cooling functions for an HII region ionized by a T$_{eff}$=40,000 K blackbody with $n_e=10^2$cm$^{-3}$ and a typical ISM mix of metals.Computed using the 2017 release of Cloudy (C17.02)[Ferland et al. 2017, RMxAA, 53, 385](https://ui.adsabs.harvard.edu/abs/2017RMxAA..53..385F/abstract).  Weprovide the data from the calculation for this notebook.<jupyter_code>%matplotlib inline

import math
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, LogLocator, NullFormatter

# Filter nuisance warnings

import warnings
warnings.filterwarnings('ignore',category=UserWarning, append=True)
warnings.filterwarnings('ignore',category=RuntimeWarning, append=True)<jupyter_output><empty_output><jupyter_text>Standard Plot FormatSetup the standard plotting format and make the plot. Fonts and resolution adopted follow CUP style.<jupyter_code>figName = 'Fig4_4' 

# graphic aspect ratio = width/height

aspect = 4.0/3.0 # 4:3

# Text width in inches - don't change, this is defined by the print layout

textWidth = 6.0 # inches

# output format and resolution

figFmt = 'png'
dpi = 600

# Graphic dimensions 

plotWidth = dpi*textWidth
plotHeight = plotWidth/aspect
axisFontSize = 10
labelFontSize = 6
lwidth = 0.5
axisPad = 5
wInches = textWidth 
hInches = wInches/aspect

# Plot filename

plotFile = f'{figName}.{figFmt}'

# LaTeX is used throughout for markup of symbols, Times-Roman serif font

plt.rc('text', usetex=True)
plt.rc('font', **{'family':'serif','serif':['Times-Roman'],'weight':'bold','size':'16'})

# Font and line weight defaults for axes

matplotlib.rc('axes',linewidth=lwidth)
matplotlib.rcParams.update({'font.size':axisFontSize})

# axis and label padding

plt.rcParams['xtick.major.pad'] = f'{axisPad}'
plt.rcParams['ytick.major.pad'] = f'{axisPad}'
plt.rcParams['axes.labelpad'] = f'{axisPad}'<jupyter_output><empty_output><jupyter_text>Cooling and Heating functionsThe cooling and heating functions are computed for a T=40000K blackbody, density $n_e$=100-cm$^{-3}$, and the CLOUDY "ism" abundances.  The parameters of the blackbody are those used by the Paris HII region contributions.  Data are stored in 3-column format: * Te = electron temperature in K * Heating = heating rate in erg cm^-3 s^-1 * Cooling = cooling rate in erg cm^-3 s^-1 Need to divide by $n_e^2$ to get into the volumetric rate units of erg cm$^3$ s$^{-1}$<jupyter_code>dataFile = 'hII_40K_cool.txt'
n_e = 100.0

data = pd.read_csv(dataFile,sep=r'\s+',comment='#')
Te = np.array(data['Te'])
heat = np.array(data['Heating'])/(n_e**2) # get into units of erg cm^3 s^-1
cool = np.array(data['Cooling'])/(n_e**2)  

xMin = 60.0   # Kelvin
xMax = 60000.0

yMin = 1.0e-25 # erg cm^3 s^-1
yMax = 3.0e-22<jupyter_output><empty_output><jupyter_text>Make the PlotHeating and cooling functions - cooling as a solid line, heating as a dashed line.  Log-log axes<jupyter_code>fig,ax = plt.subplots()

fig.set_dpi(dpi)
fig.set_size_inches(wInches,hInches,forward=True)

ax.tick_params('both',length=6,width=lwidth,which='major',direction='in',top='on',right='on')
ax.tick_params('both',length=3,width=lwidth,which='minor',direction='in',top='on',right='on')

plt.xlim(xMin,xMax)
ax.set_xscale('log')
ax.set_xticks([100.,300,1000.,3000,1.0e4,3.0e4])
ax.set_xticklabels([r'100',r'300',r'1000',r'3000',r'10000',r'30000'])
plt.xlabel(r'Temperature [K]')

plt.ylim(yMin,yMax)
ax.set_yscale('log')
plt.ylabel(r'Heating/Cooling rate [erg cm$^3$ s$^{-1}$]',fontsize=axisFontSize)

# Heating

plt.plot(Te,heat,'--',lw=1.0,color='black',zorder=10)

# Cooling

plt.plot(Te,cool,'-',lw=1.0,color='black',zorder=10)

# Annotate

xlab = 2000.
plt.text(xlab,9.5e-24,r'Heating',rotation=-18,color='black',va='bottom',ha='center',fontsize=axisFontSize)
plt.text(xlab,4.2e-24,r'Cooling',rotation=4,color='black',va='bottom',ha='center',fontsize=axisFontSize)

# plot and file

plt.plot()
plt.savefig(plotFile,bbox_inches='tight',facecolor='white')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14953714692618086, 'fraction_numerical': 0.04272489912176596, 'mean_word_length': 6.315972222222222, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '598862', 'license': 'CC0-1.0', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1525, 'n_words': 357, 'path': 'Chapter4/Fig4_4_ThermalEq.ipynb', 'repo_name': 'CambridgeUniversityPress/Interstellar-and-Intergalactic-Medium', 'size': '4220'}