Posted By

Scooter on 05/16/09


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

kurokikaze


Chemistry Library


 / Published in: PHP
 

URL: http://reusablecode.blogspot.com/2009/05/chemistry-library.html

A collection of chemistry-related functions: lookup element symbol by atomic number, lookup element name by atomic number, determine electron configuration by atomic number. Requires my proper case function

  1. <?php
  2. /*
  3.   PHP Chemistry Library
  4.  
  5.   Copyright (c) 2009, reusablecode.blogspot.com; some rights reserved.
  6.  
  7.   This work is licensed under the Creative Commons Attribution License. To view
  8.   a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  9.   send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  10.   94305, USA.
  11.   */
  12.  
  13. // Elements by name
  14. $elementNames = array(null,
  15. "Hydrogen", "Helium", "Lithium", "Beryllium", "Boron", "Carbon", "Nitrogen", "Oxygen", "Fluorine", "Neon",
  16. "Sodium", "Magnesium", "Aluminium", "Silicon", "Phosphorus", "Sulfur", "Chlorine", "Argon", "Potassium", "Calcium",
  17. "Scandium", "Titanium", "Vanadium", "Chromium", "Manganese", "Iron", "Cobalt", "Nickel", "Copper", "Zinc",
  18. "Gallium", "Germanium", "Arsenic", "Selenium", "Bromine", "Krypton", "Rubidium", "Strontium", "Yttrium", "Zirconium",
  19. "Niobium", "Molybdenum", "Technetium", "Ruthenium", "Rhodium", "Palladium", "Silver", "Cadmium", "Indium", "Tin",
  20. "Antimony", "Tellurium", "Iodine", "Xenon", "Caesium", "Barium", "Lanthanum", "Cerium", "Praseodymium", "Neodymium",
  21. "Promethium", "Samarium", "Europium", "Gadolinium", "Terbium", "Dysprosium", "Holmium", "Erbium", "Thulium", "Ytterbium",
  22. "Lutetium", "Hafnium", "Tantalum", "Tungsten", "Rhenium", "Osmium", "Iridium", "Platinum", "Gold", "Mercury",
  23. "Thallium", "Lead", "Bismuth", "Polonium", "Astatine", "Radon", "Francium", "Radium", "Actinium", "Thorium",
  24. "Protactinium", "Uranium", "Neptunium", "Plutonium", "Americium", "Curium", "Berkelium", "Californium", "Einsteinium", "Fermium",
  25. "Mendelevium", "Nobelium", "Lawrencium", "Rutherfordium", "Dubnium", "Seaborgium", "Bohrium", "Hassium", "Meitnerium", "Darmstadtium",
  26. "Roentgenium"
  27. );
  28.  
  29. // Elements by symbol
  30. $elementSymbols = array(null,
  31. "H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne",
  32. "Na", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca",
  33. "Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn",
  34. "Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr",
  35. "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn",
  36. "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd",
  37. "Pm", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb",
  38. "Lu", "Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg",
  39. "Tl", "Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Ac", "Th",
  40. "Pa", "U", "Np", "Pu", "Am", "Cm", "Bk", "Cf", "Es", "Fm",
  41. "Md", "No", "Lr", "Rf", "Db", "Sg", "Bh", "Hs", "Mt", "Ds",
  42. "Rg"
  43. );
  44.  
  45. // List of subshell names in order of filling.
  46. $subshells = array("1s", "2s", "2p", "3s", "3p", "4s", "3d", "4p", "5s", "4d", "5p", "6s", "4f", "5d", "6p", "7s", "5f", "6d", "7p", "8s", "5g", "6f", "7d", "8p", "9s", "6g", "7f", "8d", "9p", "10s", "6h", "7g", "8f", "9d", "10p", "11s", "7h", "8g", "9f", "10d", "11p", "12s", "7i", "8h", "9g", "10f", "11d", "12p", "13s", "8i", "9h", "10g", "11f", "12d", "13p", "14s");
  47.  
  48. // Retrieve element name, with support for elements that don't have names yet.
  49. // REQUIRES: strtoproper()
  50. function getElementName($atomicNumber)
  51. {
  52. $result = "";
  53. $roots = array("nil", "un", "bi", "tri", "quad", "pent", "hex", "sept", "oct", "enn");
  54.  
  55. if ($atomicNumber <= count($elementNames) - 1)
  56. {
  57. $result = $elementNames[$atomicNumber];
  58. }
  59. else
  60. {
  61. for ($i = 1; $i <= strlen($atomicNumber); $i++)
  62. {
  63. $result .= $roots[substr($atomicNumber, $i, 1)];
  64. }
  65.  
  66. $result = strtoproper(result) . "ium";
  67.  
  68. // If bi or tri is followed by the ending ium (i.e. the last digit is 2 or 3), the result is '-bium' or -'trium', not '-biium' or '-triium'.
  69. $result = str_replace("ii", "i", $result);
  70.  
  71. // If enn is followed by nil (i.e. the sequence -90- occurs), the result is '-ennil-', not '-ennnil-'.
  72. $result = str_replace("nnn", "nn", $result);
  73. }
  74.  
  75. return $result;
  76. }
  77.  
  78. // Retrieve element symbol, with support for elements that don't have symbols yet.
  79. // REQUIRES: strtoproper()
  80. function getElementSymbol($atomicNumber)
  81. {
  82. $result = "";
  83. $symbols = array("n", "u", "b", "t", "q", "p", "h", "s", "o", "e");
  84.  
  85. if ($atomicNumber <= count($elementSymbols) - 1)
  86. {
  87. $result = $elementSymbols[$atomicNumber];
  88. }
  89. else
  90. {
  91. for ($i = 1; $i <= strlen($atomicNumber); $i++)
  92. {
  93. $result .= $symbols[substr($atomicNumber, $i, 1)];
  94. }
  95.  
  96. $result = strtoproper($result);
  97. }
  98.  
  99. return $result;
  100. }
  101.  
  102. // Returns the maximum number of electrons allowed in the specified subshell.
  103. function maxElectrons($subshell)
  104. {
  105. switch (strtolower(substr($subshell, -1)))
  106. {
  107. case "s":
  108. return 2;
  109. break;
  110. case "p":
  111. return 6;
  112. break;
  113. case "d":
  114. return 10;
  115. break;
  116. case "f":
  117. return 14;
  118. break;
  119. case "g":
  120. return 18;
  121. break;
  122. case "h":
  123. return 23;
  124. break;
  125. case "i":
  126. return 26;
  127. break;
  128. }
  129. }
  130.  
  131. // Calculate the electron configuration for a given atom.
  132. function electronConfiguration($atomicNumber)
  133. {
  134. $electrons = $atomicNumber;
  135. $configuration = "";
  136.  
  137. foreach ($subshells as $subshell)
  138. {
  139. if ($electrons > $maxElectrons($subshell))
  140. {
  141. // A d subshell that is half-filled or full (ie 5 or 10 electrons) is more stable than the s subshell of the next shell.
  142. if ($subshell == "4s" && ($atomicNumber == 24 or $atomicNumber == 29)) || ($subshell == "5s" && ($atomicNumber == 41 || $atomicNumber == 42 || $atomicNumber == 44 || $atomicNumber == 45 || $atomicNumber == 47)) || ($subshell == "6s" && ($atomicNumber == 78 || $atomicNumber == 79))
  143. {
  144. $configuration .= " $subshell^1";
  145. $electrons--;
  146. }
  147. // Special exception for palladium.
  148. elseif ($subshell == "5s" && $atomicNumber == 46)
  149. {
  150. // do nothing; this subshell gets no electrons
  151. }
  152. else
  153. {
  154. $configuration .= " $subshell^" . $maxElectrons($subshell);
  155. $electrons -= $maxElectrons($subshell);
  156. }
  157. }
  158. else
  159. {
  160. $configuration .= " $subshell^$electrons";
  161. $electrons = 0;
  162. break;
  163. }
  164. }
  165.  
  166. return trim($configuration);
  167. }
  168. ?>

Report this snippet  

You need to login to post a comment.