Sei sulla pagina 1di 3

24/10/13

Chemistry Toy Box

Chemistry Toy Box


Dr. Charles R. Denham, U.S. Geological Survey, Woods Hole, MA 02543 Send suggestions and comments to cdenham@usgs.gov

Motivation
We present a simple chemical algebra, implemented with some of the object-oriented features of Matlab-5. The main idea is to use the Periodic Table of the Elements to construct atoms from fermions (protons, electrons, and neutrons), and then to combine those atoms into compounds. Several Matlab operators are overloaded, in order to explore algebraic notation in elementary chemistry.

Elements
The "elements.m" function returns a "cell" or "struct" of the elements through #107, containing atomic symbol, number, weight, and common valences, one row per element. Because of a notational conflict between the Nitrogen +1 ion and Neptunium, the latter is given the symbol "Npt". Symbols for the "unnamed" elements (#104-107) are given in uppercase.

Fermions
Classes known as "proton", "electron" and "neutron" are available for the assembly atoms. For example, helium can be constructed as:
h e l i u m 4=2 * p r o t o n+2 * e l e c t r o n+2 * n e u t r o n h e l i u m 3=h e l i u m 4-n e u t r o n

The meaning of "2*proton" here is "two protons", not one proton with a super-charge of 2.

Atoms
The "atom" constructor accepts several arguments, starting with the atomic symbol. Alternatively, one can add components to an "empty-atom" to construct an atom of any atomic symbol, number, weight, and valence. Thus, for common helium:
H e=a t o m ( ' H e ' ,2 ,2 ,2 ) H e=a t o m ( ' H e ' )+2 * p r o t o n+2 * e l e c t r o n+2 * n e u t r o n H e=a t o m+' H e '+2 * p r o t o n+2 * e l e c t r o n+2 * n e u t r o n

The "atoms.m" function creates neutral atoms, using the Periodic Table available from the

woodshole.er.usgs.gov/staffpages/cdenham/public_html/matlab/chem.html

1/3

24/10/13

Chemistry Toy Box

"elements.m" function. By default, the atoms are placed in the caller's workspace, named by their atomic symbol. If an output argument is given, a "struct" is returned, whose fieldnames are the atomic symbols. Common ions can be returned as well, depending on the calling syntax. The "atom" class can be interrogated with several methods: atomic "symbol", "number", "weight", and "valence".

Valence
Valence can be imposed on an atom in several ways. To derive bivalent (ferrous) iron from iron, use any of the following:
F e p p=F e-2 * e l e c t r o n F e p p=F e ( + 2 ) F e p p=F e+2

Our notation gives names to ions according to their valence, using "p" for "plus" and "m" for "minus". Only one conflict exists in the Periodic Table: Np could be +1 valence Nitrogen or neutral Neptunium. We have given Neptunium the symbol Npt to resolve the problem. Note how the Matlab arithmetic operators are overloaded here to accomodate the fact that our objects are chemical entities, not numbers.

Compounds
Compounds can be assembled from atoms, typically ions, using the "+" operator:
w a t e r=2 * ( H + 1 )+( O 2 ) c o m m o n _ t a b l e _ s a l t=( N a + 1 )+( C l 1 ) a m m o n i a _ i o n=( N 3 )+4 * ( H + 1 )

Compounds are automatically created whenever an atom or compound is added to another atom or compound. Some common compounds are constructed in this fashion and placed in the caller's workspace by the "compounds.m" script.

Overloaded Operators
The "+", "-", "*", and "()" operators have been overloaded in "chem", for the purpose of assembling components and setting valence. These are handled by the "plus", "minus", "mtimes", and "subsref" methods.
f e r m i o n={ p r o t o n|e l e c t r o n|n e u t r o n } f e r m i o n s=f e r m i o n*c o u n t %D e f a u l tc o u n t=1 . a t o m=' s y m b o l '+f e r m i o n s a t o m=a t o m{ +|}f e r m i o n s a t o m s=a t o m*c o u n t i o n=a t o m{ +|}e l e c t r o n i o n=a t o m{ +|}v a l e n c e i o n=a t o m ( { +|}v a l e n c e ) i o n s=i o n*c o u n t c o m p o u n d={ a t o m s|i o n s|c o m p o u n d s }+{ a t o m s|i o n s|c o m p o u n d s } c o m p o u n d s=c o m p o u n d*c o u n t

woodshole.er.usgs.gov/staffpages/cdenham/public_html/matlab/chem.html

2/3

24/10/13

Chemistry Toy Box

Any of the Matlab symbolic operators can be overloaded easily, so the syntax of "chem" is not limited to the above. The most difficult methods to overload are "subsref" for interpreting indices on the right-hand side of an assignment; "subsasgn" on the left; and "subsindex", to return a value for an object when it is used as an index. The first two methods process ".", "()", and "{}", which typically refer to parts of a "struct", a "double-precision array", and a "cell", respectively. When overloaded, these and other Matlab operators can be given whatever meaning the programmer desires.

Future Work
Whether the "Chemistry Toy Box" should be extended depends on the utility of simple algebraic notation for the representation of meaningful chemistry. How can chemical expressions be represented for mass and equilibrium balancing? Example: the seawater bicarbonate system. Do we need a "system" object that would incorporate pressure, temperature, and solvent? It would seem essential for equilibrium work. Is there an existing, intuitive, "tty" notation for chemical formulas? Is there an analog in nuclear chemistry? Should "2*H" differ from "H*2", the former for concentration and the latter for structure? Organic compounds? Isomers?

Software
Here is chem_install.p for Matlab 5.2/5.3, an installer in the form of a Matlab P-code function. For Matlab 5.0/5.1, get chem_install.p50, then delete "50" from its name. Execute "chem_install" at the Matlab prompt to unbundle the software into a new or existing local folder called "chem". Then, add "chem" to your Matlab path, restart Matlab, and execute "chem_test".

Department of Interior / U.S. Geological Survey / Coastal and Marine Geology / Woods Hole Field Center U.S.G.S. Privacy Statement / Disclaimer / Accessibility This is http://woodshole.er.usgs.gov/staffpages/cdenham/public_html/matlab/chem.html Maintained by webmaster-woodshole@usgs.gov Modified Monday, 31-Jul-2006 09:03:42 EDT

woodshole.er.usgs.gov/staffpages/cdenham/public_html/matlab/chem.html

3/3

Potrebbero piacerti anche