init version..sort of

This commit is contained in:
Tangent 2019-10-03 19:57:29 -07:00
commit a9bf59c08f
3 changed files with 149 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.lua

19
nuclear_notes.txt Normal file
View File

@ -0,0 +1,19 @@
Z N s
odd odd 0.015
odd even 0.18
even odd 0.205
even even 0.6
Z > 82 -> unstable
Z <= 20 -> stable n/p is nearly 1 (0.775 - 1.29)
Z > 20 & Z <= 82 -> stable n/p between 1:1 and 1.5:1
Z > 92 -> spontaneous fission
Z > 52 -> alpha rays possible
Can induce fission by neutron bombardment.
Too many neutrons -> beta decay, neutron emission
Too few neutrons -> electron capture, positron emission, proton emission
Alpha: Emits 2Z-2N
Beta: Emits e, 1N -> 1Z
Electron capture & Positron emission: Emits e+, 1Z -> 1N

129
src/main.moon Normal file
View File

@ -0,0 +1,129 @@
class Isotopes
new: (@seed=0) =>
@rng = love.math.newRandomGenerator(@seed)
@randoms = {}
@normals = {}
get: (z, n) =>
@[z] = {} unless @[z]
@generate(z, n) unless @[z][n]
return @[z][n]
normal: (z, n) =>
i = n^2 - (z - 1)
if z > n
i -= (z - n)
while #@normals < i
@normals[#@normals+1] = @rng\randomNormal 0.25, 1
return @normals[i]
random: (z, n) =>
i = n^2 - (z - 1)
if z > n
i -= (z - n)
while #@randoms < i
@randoms[#@randoms+1] = @rng\random!
return @randoms[i]
generate: (z, n) =>
@[z] = {} unless @[z]
isotope = {}
-- isotopes only exist in this range
if n/z > 0.9 and n/z < 1.2
v = 2 / math.abs(n - z) -- base stability is proximity to n == z
-- v /= @rng\randomNormal 0.25, 1 -- a slight random variation
v /= @normal(z, n)
local odd_even -- based on statistics from real isotopes
if z % 2 == 0
if n % 2 == 0
odd_even = 0.6
else
odd_even = 0.205
else
if n % 2 == 0
odd_even = 0.18
else
odd_even = 0.015
-- if @rng\random! <= v * odd_even -- more random variation based on stats
if @random(z, n) <= v * odd_even
v *= odd_even
if v > 1 -- maximum stability should be close to 1
v = 1.05
v -= z / 1000 -- lighter elements should have a higher stability
isotope.stability = v
else
isotope.stability = 0 -- this isotope cannot exist
if isotope.stability < 1
isotope.half_life = 0.69314718056 / (1 - isotope.stability)
-- isotope.half_life = 0.69314718056 / isotope.stability
else
isotope.half_life = math.huge
@[z][n] = isotope
-- Z > 82 -> unstable
-- Z <= 20 -> stable n/p is nearly 1 (0.775 - 1.29)
-- Z > 20 & Z <= 82 -> stable n/p between 1:1 and 1.5:1
-- Z > 92 -> spontaneous fission
-- Z > 52 -> alpha rays possible
-- Can induce fission by neutron bombardment.
-- Too many neutrons -> beta decay, neutron emission
-- Too few neutrons -> electron capture, positron emission, proton emission
-- Alpha: Emits 2Z-2N
-- Beta: Emits e, 1N -> 1Z
-- Electron capture & Positron emission: Emits e+, 1Z -> 1N
export showUnstable = true
w, h = love.graphics.getDimensions!
isotopes = Isotopes!
love.load = ->
above_0 = 0
above_1 = 0
min_half_life = math.huge
max_half_life = 0
min_z, min_n = 0, 0
max_z, max_n = 0, 0
min_s = 0
max_s = 0
for z = 1, w
for n = 0, h
isotope = isotopes\get z, n
if isotope.stability > 0
above_0 += 1
if isotope.half_life < min_half_life
min_half_life = isotope.half_life
min_z = z
min_n = n
min_s = isotope.stability
if isotope.stability < 1
if isotope.half_life > max_half_life
max_half_life = isotope.half_life
max_z = z
max_n = n
max_s = isotope.stability
else
above_1 += 1
print "#{above_0} isotopes", "#{above_1} stable isotopes"
print "Shortest half life:", "#{min_half_life}", "#{min_z}P#{min_n}N", min_s
print "Longest half life:", "#{max_half_life}", "#{max_z}P#{max_n}N", max_s
-- decay constant (probability of decay per second): 1 - stability
-- half life: 0.69314718056 / decay constant
-- I have redefined stability to equal the decay constant
love.draw = ->
scale = 3
for z = 1, w
for n = 0, h
isotope = isotopes\get z, n
s = isotope.stability
if s >= 1 or showUnstable
-- c = 1 - s -- side effect -> stable isotopes are now invisible
love.graphics.setColor s, s, s, 1
love.graphics.rectangle "fill", (z - 1) * scale, (h / scale - n - 1) * scale, scale, scale
love.keypressed = (key) ->
love.event.quit! if key == "escape"
switch key
when "s"
showUnstable = not showUnstable