| 1 |
.. _formula: |
| 2 |
|
| 3 |
|
| 4 |
******************** |
| 5 |
Chemical Composition |
| 6 |
******************** |
| 7 |
|
| 8 |
Some properties are available for groups of elements. Groups are specified |
| 9 |
as a chemical formula string and either density or cell volume for the crystal |
| 10 |
structure. While it does not provide any information about molecular |
| 11 |
structure, a formula does provide complete control over chemical composition. |
| 12 |
|
| 13 |
A formula string is translated into a formula using |
| 14 |
:func:`periodictable.formulas.formula`: |
| 15 |
|
| 16 |
* Formula strings consist of counts and atoms, where individual atoms are |
| 17 |
represented by periodic table symbol. The atoms are case sensitive, |
| 18 |
so "CO" is different from "Co". Here is an example of calcium carbonate: |
| 19 |
|
| 20 |
>>> from periodictable import formula |
| 21 |
>>> print(formula("CaCO3")) |
| 22 |
CaCO3 |
| 23 |
|
| 24 |
* Formulas can contain multiple groups separated by space or plus or by using |
| 25 |
parentheses. Whole groups can have a repeat count. The following are |
| 26 |
equivalent definitions of hydrated calcium carbonate: |
| 27 |
|
| 28 |
>>> print(formula("CaCO3+6H2O")) |
| 29 |
CaCO3(H2O)6 |
| 30 |
>>> print(formula("CaCO3 6H2O")) |
| 31 |
CaCO3(H2O)6 |
| 32 |
>>> print(formula("CaCO3(H2O)6")) |
| 33 |
CaCO3(H2O)6 |
| 34 |
|
| 35 |
* Parentheses can nest, e.g., in polyethylene glycol: |
| 36 |
|
| 37 |
>>> print(formula("HO ((CH2)2O)6 H")) |
| 38 |
HO((CH2)2O)6H |
| 39 |
|
| 40 |
* Isotopes are represented by index, such as O[18] = :sup:`18`\ O: |
| 41 |
|
| 42 |
>>> print(formula("CaCO[18]3+6H2O")) |
| 43 |
CaCO[18]3(H2O)6 |
| 44 |
|
| 45 |
* Ions are represented by charge, such as O{2-} = O\ :sup:`2-`: |
| 46 |
|
| 47 |
>>> print(formula("P{5+}O{2-}4")) |
| 48 |
P{5+}O{2-}4 |
| 49 |
|
| 50 |
If charge is +/- 1 then the number is optional: |
| 51 |
|
| 52 |
>>> print(formula("Na{+}Cl{1-}")) |
| 53 |
Na{+}Cl{-} |
| 54 |
|
| 55 |
When specifying both charge and isotope, isotope comes first: |
| 56 |
|
| 57 |
>>> print(formula("Fe[56]{2+}")) |
| 58 |
Fe[56]{2+} |
| 59 |
|
| 60 |
Even though the charge is on the individual atoms, the entire formula |
| 61 |
has a charge: |
| 62 |
|
| 63 |
>>> print(formula("P{5+}O{2-}4").charge) |
| 64 |
-3 |
| 65 |
|
| 66 |
* Counts can be integer or decimal: |
| 67 |
|
| 68 |
>>> print(formula("CaCO3+(3HO1.5)2")) |
| 69 |
CaCO3((HO1.5)3)2 |
| 70 |
|
| 71 |
* Unicode subscripts can be used for counts, with the usual decimal point: |
| 72 |
|
| 73 |
>>> print(formula("CaCO₃+(3HO₁.₅)₂")) |
| 74 |
CaCO3((HO1.5)3)2 |
| 75 |
|
| 76 |
* Print formulas with unicode using the pretty() function: |
| 77 |
|
| 78 |
>>> from periodictable.formulas import pretty |
| 79 |
>>> print(pretty(formula("CaCO3+(3HO1.5)2"))) |
| 80 |
CaCO₃((HO₁.₅)₃)₂ |
| 81 |
|
| 82 |
* Formula density can be specified using the special '@' tag: |
| 83 |
|
| 84 |
>>> print(formula("[email protected]").density) |
| 85 |
2.16 |
| 86 |
|
| 87 |
Density gives the isotopic density of the compound, so for example, D2O |
| 88 |
could be specified using: |
| 89 |
|
| 90 |
>>> print("%.3f"%formula("[email protected]").density) |
| 91 |
1.112 |
| 92 |
|
| 93 |
It can also be specified using the natural density of the compound, |
| 94 |
assuming the isotopes substitution does not change the unit cell volume: |
| 95 |
|
| 96 |
>>> print("%.3f"%formula("D2O@1n").density) |
| 97 |
1.112 |
| 98 |
|
| 99 |
Density applies to the entire formula, so for example a D2O-H2O |
| 100 |
2:1 mixture (not by mass or by volume) would be: |
| 101 |
|
| 102 |
>>> print("%.3f"%formula("2D2O + H2O@1n").density) |
| 103 |
1.074 |
| 104 |
|
| 105 |
* Mass fractions use wt%, with the final portion adding to 100%: |
| 106 |
|
| 107 |
>>> print(formula("10wt% Fe // 15% Co // Ni")) |
| 108 |
FeCo1.4214Ni7.13602 |
| 109 |
|
| 110 |
Only the first item needs to specify that it is a mass fraction, |
| 111 |
and the remainder can use a bare %. |
| 112 |
|
| 113 |
* Volume fractions use vol%, with the final portion adding to 100%: |
| 114 |
|
| 115 |
>>> print(formula("10vol% Fe // Ni")) |
| 116 |
FeNi9.68121 |
| 117 |
|
| 118 |
Only the first item needs to specify that it is a volume fraction, and |
| 119 |
the remainder can use a bare %. |
| 120 |
|
| 121 |
Volume fraction mixing is only possible if the densities are known for |
| 122 |
the individual components, which will require the formula density tag |
| 123 |
if the component is not an element. A density estimate is given for |
| 124 |
the mixture but in general it will not be correct, and should be set |
| 125 |
explicitly for the resulting compound. |
| 126 |
|
| 127 |
* Specific mass can be giving with count follwed by mass units: |
| 128 |
|
| 129 |
>>> print(formula("5g NaCl // 50mL H2O@1")) |
| 130 |
NaCl(H2O)32.4395 |
| 131 |
|
| 132 |
Density will be required for materials given by volume. Mass will be |
| 133 |
stored in the *total_mass* attribute of the resulting formula. |
| 134 |
|
| 135 |
* Multilayers can be specified by thickness: |
| 136 |
|
| 137 |
>>> print(formula("1 um Si // 5 nm Cr // 10 nm Au")) |
| 138 |
Si119.992CrAu1.41722 |
| 139 |
|
| 140 |
Density will be required for each layer. Thickness will be stored in |
| 141 |
the *total_thickness* attribute of the resulting formula. Thickness can |
| 142 |
be converted to *total_volume* by multiplying by cross section, and to |
| 143 |
*total_mass* by multiplying that by *density*. |
| 144 |
|
| 145 |
* Mixtures can nest. The following is a 10% salt solution by weight mixed |
| 146 |
20:80 by volume with D2O: |
| 147 |
|
| 148 |
>>> print(formula("20vol% (10 wt% [email protected] // H2O@1) // D2O@1n")) |
| 149 |
NaCl(H2O)29.1956(D2O)122.79 |
| 150 |
|
| 151 |
* Empty formulas are supported, e.g., for air or vacuum: |
| 152 |
|
| 153 |
>>> print(formula()) |
| 154 |
<BLANKLINE> |
| 155 |
>>> formula() |
| 156 |
formula('') |
| 157 |
|
| 158 |
The grammar used for parsing formula strings is the following: |
| 159 |
|
| 160 |
:: |
| 161 |
|
| 162 |
formula :: compound | mixture | nothing |
| 163 |
mixture :: quantity | percentage |
| 164 |
quantity :: number unit part ('//' number unit part)* |
| 165 |
percentage :: number 'wt%|vol%' part ('//' number '%' part)* '//' part |
| 166 |
part :: compound | '(' mixture ')' |
| 167 |
compound :: (composite | fasta) density? |
| 168 |
fasta :: ('dna' | 'rna' | 'aa') ':' [A-Z -*]+ |
| 169 |
composite :: group (separator group)* |
| 170 |
group :: number element+ | '(' formula ')' number |
| 171 |
element :: symbol isotope? ion? number? |
| 172 |
symbol :: [A-Z][a-z]* |
| 173 |
isotope :: '[' integer ']' |
| 174 |
ion :: '{' integer? [+-] '}' |
| 175 |
density :: '@' number [ni]? |
| 176 |
number :: integer | fraction |
| 177 |
integer :: [1-9][0-9]* |
| 178 |
fraction :: ([1-9][0-9]* | 0)? '.' [0-9]* |
| 179 |
separator :: space? '+'? space? |
| 180 |
unit :: mass | volume | length |
| 181 |
mass :: 'kg' | 'g' | 'mg' | 'ug' | 'ng' |
| 182 |
volume :: 'L' | 'mL' | 'uL' | 'nL' |
| 183 |
length :: 'cm' | 'mm' | 'um' | 'nm' |
| 184 |
|
| 185 |
Formulas can also be constructed from atoms or other formulas: |
| 186 |
|
| 187 |
* A simple formula can be created from a bare atom: |
| 188 |
|
| 189 |
>>> from periodictable import Ca, C, O, H |
| 190 |
>>> print(formula(Ca)) |
| 191 |
Ca |
| 192 |
|
| 193 |
* More complex structures will require a sequences of counts and fragments. |
| 194 |
The fragment itself can be a structure: |
| 195 |
|
| 196 |
>>> print(formula( [ (1,Ca), (1,C), (3,O), (6,[(2,H),(1,O)]) ] )) |
| 197 |
CaCO3(H2O)6 |
| 198 |
|
| 199 |
* Structures can also be built with simple formula math: |
| 200 |
|
| 201 |
>>> print(formula("CaCO3") + 6*formula("H2O")) |
| 202 |
CaCO3(H2O)6 |
| 203 |
|
| 204 |
* Formulas can be easily cloned: |
| 205 |
|
| 206 |
>>> print(formula(formula("CaCO3+6H2O"))) |
| 207 |
CaCO3(H2O)6 |
| 208 |
|
| 209 |
Density |
| 210 |
------- |
| 211 |
|
| 212 |
Density can be specified directly when the formula is created, or updated |
| 213 |
within a formula. For isotope specific formulas, the density can be given |
| 214 |
either as the density of the formula using naturally occurring abundance |
| 215 |
if the unit cell is approximately the same, or using the density specific |
| 216 |
to those isotopes used. |
| 217 |
|
| 218 |
This makes heavy water density easily specified as: |
| 219 |
|
| 220 |
>>> D2O = formula("D2O", natural_density=1) |
| 221 |
>>> print(f"{D2O} {D2O.density:.4g}") |
| 222 |
D2O 1.112 |
| 223 |
|
| 224 |
Density can also be estimated from the volume of the unit cell, either |
| 225 |
by using the covalent radii of the constituent atoms and assuming some |
| 226 |
packing factor, or by knowing the lattice parameters of the crystal |
| 227 |
which makes up the material. Standard packing factors for hcp, fcc, |
| 228 |
bcc, cubic and diamond on uniform spheres can be used if the components |
| 229 |
are of about the same size. The formula should specify the number of |
| 230 |
atoms in the unit cell, which is 1 for cubic, 2 for bcc and 4 for fcc. |
| 231 |
Be sure to use the molecular mass (M.molecular_mass in g) rather |
| 232 |
than the molar mass (M.mass in u = g/mol) in your calculations. |
| 233 |
|
| 234 |
Because the packing fraction method relies on the covalent radius |
| 235 |
estimate it is not very accurate: |
| 236 |
|
| 237 |
>>> from periodictable import elements, formula |
| 238 |
>>> Fe = formula("2Fe") # bcc lattice has 2 atoms per unit cell |
| 239 |
>>> Fe.density = Fe.molecular_mass/Fe.volume('bcc') |
| 240 |
>>> print(f"{Fe.density:.3g}") |
| 241 |
6.55 |
| 242 |
>>> print(f"{elements.Fe.density:.3g}") |
| 243 |
7.87 |
| 244 |
|
| 245 |
Using lattice parameters the results are much better: |
| 246 |
|
| 247 |
>>> Fe.density = Fe.molecular_mass/Fe.volume(a=2.8664) |
| 248 |
>>> print(f"{Fe.density:.3g}") |
| 249 |
7.88 |
| 250 |
|
| 251 |
Mixtures |
| 252 |
-------- |
| 253 |
|
| 254 |
Mixtures can be created by weight or volume ratios, with the density of |
| 255 |
the result computed from the density of the materials. For example, the |
| 256 |
following is a 2:1 mixture of water and heavy water: |
| 257 |
|
| 258 |
>>> from periodictable import formula, mix_by_volume, mix_by_weight |
| 259 |
>>> H2O = formula('H2O',natural_density=1) |
| 260 |
>>> D2O = formula('D2O',natural_density=1) |
| 261 |
>>> mix = mix_by_volume(H2O,2,D2O,1) |
| 262 |
>>> print(f"{mix} {mix.density:.4g}") |
| 263 |
(H2O)2D2O 1.037 |
| 264 |
|
| 265 |
Note that this is different from a 2:1 mixture by weight: |
| 266 |
|
| 267 |
>>> mix = mix_by_weight(H2O,2,D2O,1) |
| 268 |
>>> print(f"{mix} {mix.density:.4g}") |
| 269 |
(H2O)2.22339D2O 1.035 |
| 270 |
|
| 271 |
Except in the simplest of cases, the density of the mixture cannot be |
| 272 |
computed from the densities of the components, and the resulting density |
| 273 |
should be set explicitly. |
| 274 |
|
| 275 |
Derived values |
| 276 |
-------------- |
| 277 |
|
| 278 |
Once a formula has been created, it can be used for summary calculations. |
| 279 |
The following is an example of hydrated quartz, which shows how to |
| 280 |
compute molar mass and neutron/xray scattering length density: |
| 281 |
|
| 282 |
>>> import periodictable |
| 283 |
>>> SiO2 = periodictable.formula('SiO2') |
| 284 |
>>> hydrated = SiO2 + periodictable.formula('3H2O') |
| 285 |
>>> print(f"{hydrated} mass {hydrated.mass:.3f}") |
| 286 |
SiO2(H2O)3 mass 114.128 |
| 287 |
>>> rho,mu,inc = periodictable.neutron_sld('SiO2+3H2O',density=1.5,wavelength=4.75) |
| 288 |
>>> print(f"{hydrated} neutron sld {rho:.3g}") |
| 289 |
SiO2(H2O)3 neutron sld 0.849 |
| 290 |
>>> rho,mu = periodictable.xray_sld(hydrated,density=1.5, |
| 291 |
... wavelength=periodictable.Cu.K_alpha) |
| 292 |
>>> print(f"{hydrated} X-ray sld {rho:.3g}") |
| 293 |
SiO2(H2O)3 X-ray sld 13.5 |
| 294 |
|
| 295 |
Biomolecules |
| 296 |
------------ |
| 297 |
|
| 298 |
The :mod:`periodictable.fasta` module can be used to load and manage bio |
| 299 |
molecules. These can be used to compute molecular weights, approximate |
| 300 |
volumes and scattering for various lipids and proteins. In addition it |
| 301 |
supports labile hydrogen calculations, allowing you to compute the neutron |
| 302 |
scattering length density of the molecule in the presence of D2O as a |
| 303 |
solvent, assuming all labile hydrogens are substituted. |