Skip to content

Instantly share code, notes, and snippets.

@M-Barnett
Last active February 15, 2018 10:10
Show Gist options
  • Save M-Barnett/78afb513929a3e628f46a335e52492c3 to your computer and use it in GitHub Desktop.
Save M-Barnett/78afb513929a3e628f46a335e52492c3 to your computer and use it in GitHub Desktop.
"""
This code is to set up a class that represents nuclei in a 2D-array
"""
import random
import numpy as np
class Grid2D(object):
"""grid set representing nuclei"""
def __init__(self, dims, decay, time):
"""Creates a new representation with values pertaining to nuclei"""
self.grid = np.ones(dims)
self.decay = float(decay)
self.time = float(time)
self.dims = dims
self.decayed = False
self.sHL = 0.0
self.HL = np.log(2) / self.decay
self.p = decay*time
def printGrid(self):
s = ""
for row in self.grid:
for element in row:
s += str(element) + "\t"
s += "\n"
print(s)
def printStats(self):
counter = 0
for row in self.grid:
for element in row:
if element == 0:
counter += 1
print("The initial number was " + str(self.dims[0]**2) + " the final number of undecayed nuclei is " + str(counter))
print("The simulated value for the half life is " + str(self.sHL) + " mins")
print("The actual value of the half-life as given by the decay constant is " + str(self.HL) + " mins")
def decayer(self):
counter = 0
timer = 0
p = self.p
for row in self.grid:
for element in row:
if element == 0:
counter += 1
while counter <= (float(self.dims[0]**2) / 2.0):
for row in self.grid:
for i in range(0, self.dims[1]):
if row[i] == 1:
if (random.random() * 100) <= (p):
row[i] = 0
counter += 1
timer += 1
else:
timer += 1
self.decayed = True
self.sHL = timer*self.time
def main():
n = 10
m = n
dims = (n,m)
g = Grid2D(dims, 0.02775, 0.01 )
g.printGrid()
g.decayer()
g.printGrid()
g.printStats()
main()
@gfarrell
Copy link

from numpy import random

def init_grid(n, m):
    return [([1] * n)] * m

def tick(grid, p):
    def decay(cell):
        if random.random() < p:
            return 0
        else:
            return cell
    return [[decay(cell) for cell in row] for row in grid]

@gfarrell
Copy link

def find_ratio_of_living_cells(grid):
    living = 0
    count = 0
    for row in grid:
        for cell in row:
            living = living + cell
            count = count + 1
    return living / count

@gfarrell
Copy link

gfarrell commented Feb 15, 2018

def find_half_life(grid, p):
    count = 0
    while find_ratio_of_living_cells(grid) > 0.5:
        grid = tick(grid, p)
        count = count + 1
    return count

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment