FERRETGame engine over Babylon.js
Ferret

Cookbook: writing chemistry with formulas

The guide behind these: chemistry. Each recipe says what to build and what to expect; the runnable versions, with the numbers they must produce, are in tests/unit/examples/physics-cookbook-chemistry-with-formulas.test.ts.

1. A substance is a formula#

Write the formula; atoms, charge and molar mass follow from it. Hydrates, brackets and ions work.

const lib = new SpeciesLibrary(['core', 'metals']);
const p = parseFormula('CuSO4·5H2O');                 // atoms { Cu: 1, S: 1, O: 9, H: 10 }
const grams = formulaMass(p.atoms);                    // 249.68 g/mol
lib.find('Fe2O3(s)');                                  // the library's name for it

2. An equation balances itself#

Give the substances and let the library find the whole numbers. A game that writes an unbalanced equation is refused with the element named; balanceEquation is for tools.

const chem = new Chemistry(new SpeciesLibrary(['core', 'combustion']));
chem.balanceEquation('C3H8 + O2 -> CO2 + H2O').equation;   // 'C3H8 + 5 O2 → 3 CO2 + 4 H2O'

3. A bottle of acid#

Load the aqueous sets, add the reactions of solutions, put water and acid in a vessel and read the pH. A tenth of a mole of acetic acid is far weaker than the same of hydrochloric.

const chem = new Chemistry(new SpeciesLibrary(['core', 'aqueous', 'acids-bases']));
addAqueousReactions(chem);
const beaker = new Vessel(chem, { volume: 1e-3, liquidVolume: 1e-3, isothermal: true,
  contents: { 'H2O(l)': waterMoles(1e-3), 'CH3COOH(aq)': 0.1 } });
beaker.react(1);
beaker.pH;                                              // 2.89

4. A piece of lithium in water#

The metals and aqueous sets and the standard reactions: the metal gives hydrogen and hydroxide, the water warms and turns alkaline. Sodium does it faster. Add addPhaseChanges(chem) and the metal melts at its melting point, then reacts as a liquid (recipe 6).

const chem = new Chemistry(new SpeciesLibrary(['core', 'metals', 'aqueous']));
addAqueousReactions(chem);
addStandardReactions(chem);
const v = new Vessel(chem, { volume: 2e-3, liquidVolume: 1e-3, contents: { 'H2O(l)': waterMoles(1e-3), 'Li(s)': 0.01 } });
for (let i = 0; i < 40; i++) v.react(0.5);

5. Lime heating its water#

Quicklime in water is a reaction with a tabulated heat. The library has it; the temperature follows from the energy balance.

const chem = new Chemistry(new SpeciesLibrary(['core', 'metals']));
addStandardReactions(chem, { families: ['metals'] });
const v = new Vessel(chem, { volume: 1e-3, liquidVolume: 2e-4, contents: { 'H2O(l)': waterMoles(2e-4), 'CaO(s)': 1 } });
for (let i = 0; i < 24; i++) v.react(5);

Every reaction of the library has its equation, rate and a note on where the rate comes from; STANDARD_REACTIONS lists them with the tabulated heat of reaction at 298 K.

6. Sodium through its melting point#

Melting and boiling are reactions too: addPhaseChanges makes Na(l) and Na(g) from the melting and boiling data and adds melt-Na and boil-Na. Heated from outside, the vessel sits at 370.87 K until the latent heat is in, then warms on.

const chem = new Chemistry(new SpeciesLibrary(['core', 'metals']));
addPhaseChanges(chem);
const v = new Vessel(chem, { volume: 1e-3, hA: 1, Tenv: 500, contents: { 'Na(s)': 0.1 } });
for (let i = 0; i < 400; i++) v.react(0.05);          // a plateau of 2 s at 370.87 K: 0.1 mol × 2.6 kJ / 129 W