Advertisement
Redxone

[CC -Atom structure calculator] Kind of works. lol

May 18th, 2016
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.77 KB | None | 0 0
  1.  
  2. --]] table of atom parts
  3. --]] DISCLAIMER: This program does not YET work with all atoms
  4. --]] The current atoms it doesnt work with are,
  5. --]] Doesnt work with group 9
  6. --]] Doesnt work with group 10: 2vE (All atoms in this group have n valence electrons)
  7. --]] Doesnt work with most lanthanides or actinides.
  8. --]] Doesnt work with Iron atom
  9.  
  10. local particles = {
  11.     ['-'] = {name="electron",graphic="-",tc=colors.white,bc=colors.blue},
  12.     ['+'] = {name="proton",graphic="+",tc=colors.white,bc=colors.red},
  13.     [' '] = {name="neutron",graphic=" ",tc=colors.white,bc=colors.yellow},
  14. }
  15. function newAtom(atomicnumber,huge)
  16.     --if(velectrons <= 0 or velectrons > 8)then error("Invalid number of valence electrons. ") end
  17.  
  18.     local atom = {
  19.         nucleus = {
  20.             protons = atomicnumber,
  21.             neutrons = atomicnumber + math.max(math.random(0,1)),
  22.         },
  23.         electrons = atomicnumber,
  24.         shells = {
  25.             {max=2,have=0},
  26.             {max=8,have=0},
  27.             {max=18,have=0},
  28.             {max=32,have=0},
  29.             {max=32 or 50,have=0},
  30.             {max=32 or 72,have=0},
  31.         },
  32.         rings = {},
  33.         maxpool = function(self)
  34.             print("Maximizing Possible electron shells...")
  35.             for i = #self.shells, 500 do
  36.                 --]] 2n^2 = max electrons per shell
  37.                 self.shells[#self.shells+1] = {max= 2 * (math.pow(i,2)),have=0}
  38.             end
  39.         end,
  40.         outermost = 1,
  41.  
  42.         init = function(self)
  43.             local nextorbit = 0
  44.             print("electrons - " .. self.electrons)
  45.              for i = 1, #self.shells do
  46.                  if(self.electrons >= self.shells[i].max)then
  47.                      self.shells[i].have = self.shells[i].max
  48.                      self.rings[#self.rings+1] = self.shells[i].have
  49.                      print("shells - " .. self.shells[i].max)
  50.                      self.electrons = self.electrons - self.shells[i].max
  51.                      print("electrons - " .. self.electrons)
  52.                  elseif(self.electrons <= 8 and self.shells[i].max >= 8)then
  53.                      self.shells[i].have = self.electrons
  54.                      self.rings[#self.rings+1] = self.shells[i].have
  55.                      print("shells - " .. self.shells[i].have)
  56.                      self.electrons = 0
  57.                      self.outermost = i
  58.                          if(self.shells[self.outermost].have == 0)then
  59.                             print("Sorry atom could not be built. ")
  60.                          else
  61.                             print("done!")
  62.                          end
  63.                      break
  64.                 elseif(self.electrons > 8)then
  65.                     for x = 1, i do
  66.                         if(self.shells[#self.shells-x] == nil)then break end
  67.                         if(self.shells[#self.shells-x].max <= self.electrons)then
  68.                             if(self.electrons < 2)then break end
  69.                             if(self.electrons < 8 and self.shells[#self.shells-x].max == 2)then
  70.                                 self.shells[#self.shells-x].have = self.electrons
  71.                                 self.rings[#self.rings+1] = self.electrons
  72.                                 self.electrons = 0
  73.                                 print("shells - " .. self.shells[#self.shells-x].have)
  74.                                 print("electrons - " .. self.electrons)
  75.                                 break
  76.                              end
  77.                             self.shells[#self.shells-x].have = self.shells[#self.shells-x].max
  78.                             self.rings[#self.rings+1] = self.shells[#self.shells-x].have
  79.                             self.electrons = self.electrons - self.shells[#self.shells-x].max
  80.                                 print("shells - " .. self.shells[#self.shells-x].have)
  81.                                 print("electrons - " .. self.electrons)
  82.                         end
  83.                     end
  84.                  
  85.                  elseif(self.electrons < 2)then
  86.                     self.shells[i].have = self.electrons
  87.                     self.rings[#self.rings+1] = self.shells[i].have
  88.                     print("shells - " .. self.shells[i].have)
  89.                     --self.electrons = 0
  90.                     self.outermost = i
  91.                          if(self.shells[self.outermost].have == 0)then
  92.                             print("Sorry atom could not be built. ")
  93.                          else
  94.                             print("done!")
  95.                          end
  96.                     break;
  97.                  end
  98.              end
  99.         end,
  100.  
  101.         draw = function(self)
  102.             local map = ""
  103.             print("Protons: " .. self.nucleus.protons)
  104.             print("Neutrons: " .. self.nucleus.neutrons)
  105.             print("Predicted electron orbits: ")
  106.             for i = 1, #self.rings do
  107.                 map = map .. tostring(self.rings[i])
  108.                 if(i < #self.rings)then map = map .. ", " end
  109.             end
  110.             print(map)
  111.             --return map
  112.         end,
  113.     }
  114.     if(huge)then atom:maxpool() end
  115.     atom:init()
  116.     return atom
  117. end
  118. --]] Shell and sub shells shell holds - 2n^2
  119. --]] EXAMPLE - K is n-1 so, it would have 2*1^2 maximum electrons. so, 2
  120. -- KEEP IN MIND MAX VALENCE ELECTRONS IS 8!
  121. -- K = 1s           : 2
  122. -- L = 2s2p         : 8
  123. -- M = 3s3p3d       : 18
  124. -- N = 4s4p4d4f     : 32
  125. -- O = 5s5p5d5f5g   : 50
  126. -- P - 6s6p6d6f6g   : 72
  127. --]] sub shell electrons - orbitals
  128. -- s = 1 orbital    : 2
  129. -- p = 3 orbitals   : 6
  130. -- d = 5 orbitals   : 10
  131. -- f = 7 orbitals   : 14
  132. -- g = 9 orbitals   : 18
  133. -- Electron distribution
  134. -- electrons = 12 so,
  135. -- atom would have 3 shells
  136. -- KLM and would have 2 valence electrons
  137. -- Electron distribution filling 3 shells or more
  138. -- first fill K so e-2
  139. -- then fill L so e-8
  140. -- then fill M if M cant be filled then fill it with 8
  141. -- and move the remaining electrons into the N shell do this
  142. -- with other shells aswell.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement