Posted By

Scooter on 05/16/09


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

SnipItPrpDemo
benrudolph


Chemistry Library


 / Published in: ASP
 

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

Report this snippet  

You need to login to post a comment.