Module:MolarMass
MyWikiBiz, Author Your Legacy — Saturday January 11, 2025
Jump to navigationJump to searchTemplate:Module rating This module computes the molar mass (in grams per mole) from a simple chemical formula.
Usage
Exported function :
mm(frame)
– returns the molar mass (in grams per mole)
Uses :
mw.frame
– args[1] = chemical formula
Examples
Example | Wiki code | Result |
---|---|---|
C0.5•1.5H2O | {{#invoke:MolarMass|mm|C0.5•1.5H2O}} | 33.02827 |
Mo | {{#invoke:MolarMass|mm|Mo}} | 95.94 |
H2SO4 | {{#invoke:MolarMass|mm|H2SO4}} | 98.07948 |
Whitespace allowed but cannot break atom-symbols or numbers | {{#invoke:MolarMass|mm|H 2 S O 4}} | 98.07948 |
{{#invoke:MolarMass|mm|C 4 H 1 0}} | 49.05074 | |
Wrong: {{#invoke:MolarMass|mm|C a O}} | Lua error at line 38: Invalid character in formula beginning at 'a O'. | |
Si(OH)4 | {{#invoke:MolarMass|mm|Si(OH)4}} | 96.11486 |
H(C(OH)2)2H | {{#invoke:MolarMass|mm|H(C(OH)2)2H}} | 94.06664 |
CuSO4•H2O[1] | {{#invoke:MolarMass|mm|CuSO4•H2O}} | 159.6096 |
CuSO4•5H2O[1][2] | {{#invoke:MolarMass|mm|CuSO4•5H2O}} | 249.686 |
{{#invoke:MolarMass|mm|CuSO4(H2O)5}} | 249.686 | |
HO0.5 | {{#invoke:MolarMass|mm|HO0.5}} | 9.00764 |
Error: Unknown element symbol | {{#invoke:MolarMass|mm|ZxPo4}} | Lua error at line 49: Unknown element : Zx. |
Error: Unbalanced parentheses | {{#invoke:MolarMass|mm|((Na)Cl}} | Lua error at line 64: Too many '(' in ((Na)Cl. |
{{#invoke:MolarMass|mm|Si(OH))4}} | Lua error at line 53: Too many ')' in Si(OH))4. | |
Error: Misc unrecognized character | {{#invoke:MolarMass|mm|Si(OH)4q}} | Lua error at line 38: Invalid character in formula beginning at 'q'. |
Leading numbers ignored[2] | {{#invoke:MolarMass|mm|H}} | 1.00794 |
{{#invoke:MolarMass|mm|2H}} | 2.01588 | |
Error: Unknown bracket type[3] | {{#invoke:MolarMass|mm|2[AlCl3]}} | Lua error at line 38: Invalid character in formula beginning at '[AlCl3]'. |
Error: Only accepts unformatted text | {{#invoke:MolarMass|mm|H<sub>2</sub>O}} | Lua error at line 38: Invalid character in formula beginning at '<sub>2</sub>O'. |
Notes and references
-- Computes the mollar mass from a simple chemical formula -- like H2O, NH3, CuSO4, Si(OH)4, 2H2O -- local c = {} -- module's table local am = { -- Atomic Mass table (extracted from .svg) H=1.00794; He=4.002602; Li=6.941; Be=9.012182; B=10.811; C=12.0107; N=14.00674; O=15.9994; F=18.9984032; Ne=20.1797; Na=22.98976928; Mg=24.3050; Al=26.9815386; Si=28.0855; P=30.973762; S=32.066; Cl=35.4527; Ar=39.948; K=39.0983; Ca=40.078; Sc=44.955912; Ti=47.867; V=50.9415; Cr=51.9961; Mn=54.938045; Fe=55.845; Co=58.933195; Ni=58.6934; Cu=63.546; Zn=65.39; Ga=69.723; Ge=72.61; As=74.92160; Se=78.96; Br=79.904; Kr=83.80; Rb=85.4678; Sr=87.62; Y=88.90585; Zr=91.224; Nb=92.90638; Mo=95.94; Tc=97.9072; Ru=101.07; Rh=102.90550; Pd=106.42; Ag=107.8682; Cd=112.411; In=114.818; Sn=118.710; Sb=121.760; Te=127.60; I=126.90447; Xe=131.29; Cs=132.9054519; Ba=137.327; Hf=178.49; Ta=180.94788; W=183.84; Re=186.207; Os=190.23; Ir=192.217; Pt=195.084; Au=196.966569; Hg=200.59; Tl=204.3833; Pb=207.2; Bi=208.98040; Po=208.9824; At=209.9871; Rn=222.0176; La=138.90547; Ce=140.116; Pr=140.90765; Nd=144.242; Pm=144.9127; Sm=150.36; Eu=151.964; Gd=157.25; Tb=158.92535; Dy=162.500; Ho=164.93032; Er=167.259; Tm=168.93421; Yb=173.04; Lu=174.967; Fr=223.0197; Ra=226.0254; Rf=263.1125; Db=262.1144; Sg=266.1219; Bh=264.1247; Hs=269.1341; Mt=268.1388; Ds=272.1463; Rg=272.1535; Cn=277.0; Nh=284.0; Fl=289.0; Mc=288.0; Lv=292.0; Ts=292.0; Og=294.0; Ac=227.0277; Th=232.03806; Pa=231.03588; U=238.02891; Np=237.0482; Pu=244.0642; Am=243.0614; Cm=247.0703; Bk=247.0703; Cf=251.0796; Es=252.0830; Fm=257.0951; Md=258.0984; No=259.1011; Lr=262.110; } local T_ELEM = 0 -- token types local T_NUM = 1 local T_O = 2 -- open '(' local T_C = 3 -- close ')' local T_MIDDOT = 4 -- hydration delimiter local T_SPACE = 5 -- whitespace local T_WATER = 6 -- crystallization water '•xH2O' function item(f) -- (iterator) returns one token (type, value) at a time from the formula 'f' local i = 1 return function () local t, x = nil, nil if i <= f:len() then x = f:match('^%u%l*', i); t = T_ELEM; -- matching elem (C, O, Ba, Na,...) if not x then x = f:match('^[%d.]+', i); t = T_NUM; end -- matching number if not x then x = f:match('^%(', i); t = T_O; end -- matching '(' if not x then x = f:match('^%)', i); t = T_C; end -- matching ')' if not x then x = f:match('^•[%d.]*H2O', i); t = T_WATER; end -- matching '•xH2O' x number, optional if not x then x = f:match('^•', i); t = T_MIDDOT; end -- matching '•' if not x then x = f:match('^%s+', i); t = T_SPACE; end -- matching whitespace if x then i = i + x:len(); else error("Invalid character in formula beginning at '"..f:sub(i).."'") end end return t, x end end function c.mm(frame) -- molar mass of the formula 'f' local f = frame.args[1] local sum, cur = {0}, {0} -- stacks to handle '()' ; 'cur' awaits to be multiplied (or not) local t, x for t, x in item(f) do if t == T_ELEM then if not am[x] then error("Unknown element : "..x) end sum[#sum] = sum[#sum] + cur[#cur]; cur[#cur] = am[x] elseif t == T_NUM then sum[#sum] = sum[#sum] + cur[#cur] * tonumber(x); cur[#cur] = 0 elseif t == T_O then sum[#sum] = sum[#sum] + cur[#cur]; cur[#cur] = 0;sum[#sum+1] = 0; cur[#cur+1] = 0 -- push elseif t == T_C then if #sum < 2 then error("Too many ')' in "..f) end sum[#sum] = sum[#sum] + cur[#cur]; cur[#cur-1] = sum[#sum]; sum[#sum], cur[#cur] = nil, nil -- pop elseif t == T_WATER then if string.match(x, '•%d+') then sum[#sum] = sum[#sum] + ((2*am.H + am.O) * tonumber(string.match(x, '[%d.]+'))) else -- sum[#sum] = sum[#sum] + (2*am.H + am.O) end elseif t == T_MIDDOT then error("Hydration syntax (•) not yet supported") elseif t ~= T_SPACE then error('???') end -- ignore whitespace end if #sum > 1 then error("Too many '(' in "..f) end -- check leading number (2XyZ) if string.match(f, '^[%d.]+') then return (sum[1] + cur[1]) * tonumber(string.match(f, '^[%d.]+')) else return sum[1] + cur[1] end end --[[ tests ------------- c.frame = {} c.frame.args = {} function pm(f) c.frame.args[1] = f print('The molar mass of '..f..' is '..c.mm(c.frame)) end pm("NaCl") pm("NaOH") pm("CaCO3") pm("H2SO4") pm("C10H8") pm("CO2") pm("Mo") pm("HCl") pm("Si(OH)4") pm("CuSO4(H20)5") --------------- --]] return c -- exports c.mm()