"""Lab to Code — How an airbag inflates: stoichiometry + the ideal gas law.

Reaction in the classic inflator: 2 NaN3 to 2 Na + 3 N2 (2 mol of sodium azide give 3 mol of nitrogen).
Molar mass of NaN3: 65.01 g/mol (PubChem). Ideal gas law: P V = n R T.
"""
M_NAN3 = 65.01      # g/mol
R = 0.08206         # L*atm/(mol*K)


def liters_of_n2(grams, T_C=25, P_atm=1.0):
    mol_azide = grams / M_NAN3
    mol_n2 = mol_azide * 3 / 2          # the 3:2 ratio from the equation
    return mol_n2 * R * (T_C + 273.15) / P_atm


def grams_for(liters, T_C=25, P_atm=1.0):
    mol_n2 = P_atm * liters / (R * (T_C + 273.15))
    return mol_n2 * 2 / 3 * M_NAN3


print(f"65 g of NaN3 gives {liters_of_n2(65):.1f} L of N2 at 25 C")
print(f"A 60 L bag needs {grams_for(60):.0f} g of NaN3")
print(f"If the gas were at 300 C: {liters_of_n2(65, 300):.1f} L")
