def demo_3d(): geo_grid = GeoCellGrid( cell_grid=CellGrid(shape=(2, 1, 1), coord_max=[3., 3., 3.])) print 'elem_X_map' print geo_grid.elem_X_map print 'cell_grid_shape' print geo_grid.cell_grid.cell_idx_grid.shape print 'index' print geo_grid.cell_grid.cell_idx_grid[0, 0, 0] print 'first element - direct access' print geo_grid.elem_X_map[0] print 'first element - mapped access' print geo_grid[0, 0, 0] print 'x_max dofs' print geo_grid[:, -1, :, -1].elems print 'X_grid' print geo_grid[:, -1, :, -1].point_x_arr print 'X_arr' print geo_grid[:, -1, :, -1].point_X_arr print 'point_grid' print geo_grid.point_X_grid
def simulate_neighbor_destruction(num_potential_neighbors) : dim = 3 if num_potential_neighbors != 8 : assert False filename = "prob_destruction_%f_%f_%f_%f_%d" % (params.FIRE_START_PROB_DEVEL, params.FIRE_CATCH_PROB_DEVEL, params.FIRE_START_PROB_WILD, params.FIRE_CATCH_PROB_WILD, num_potential_neighbors) if os.path.exists(filename) : chance_destruction = [0]*(num_potential_neighbors+1) with open(filename) as fin : for line in fin.readlines() : splt = line.strip().split(',') num_devel_neighbors = int(splt[0]) prob_dest = float(splt[1]) chance_destruction[num_devel_neighbors] = prob_dest return chance_destruction print "Running simulations to estimate destruction of center cell in neighborhood" chance_destruction = [] num_simulations = 10000 for num_devel_neighbors in range(num_potential_neighbors+1) : print "Simulating number develeoped neighbors = %d" % num_devel_neighbors times_burned = 0 all_neighbors = list(product(range(1,dim+1), range(1,dim+1))) for simulation in range(num_simulations) : cells = CellGrid(dim+1, dim+1) cells.set_state(dim/2+1, dim/2+1, DEVEL) for (row,col) in random.sample(all_neighbors, num_devel_neighbors) : cells.set_state(row, col, DEVEL) while True : cells.update_fire_state(susceptibility=1, no_new_start=False) if cells.state_counts[BURNING] == 0 : break if cells.get_state(dim/2+1, dim/2+1) == BURNT : times_burned += 1 chance_destruction.append(float(times_burned) / num_simulations) with open(filename, 'w') as fout : print "Saving file '%s' with probabilties to remember for next time" % filename for (i,prob) in enumerate(chance_destruction) : fout.write("%d,%f\n" % (i,prob)) return chance_destruction
def demo_1d(): # Get the intersected element of a one dimensional grid cell_grid = CellGrid(grid_cell_spec=CellSpec(node_coords=[[-1.0], [0.0], [1.0]], ), shape=(5, ), coord_max=[1.]) geo_grid = GeoCellGrid(cell_grid=cell_grid) ls_function = lambda x: x - 0.5 print 'vertex grid' print cell_grid.vertex_X_grid print 'cell_grid' print cell_grid.cell_idx_grid print 'intersected' print geo_grid.get_intersected_cells(ls_function) print 'negative' print geo_grid.get_negative_cells(ls_function)
def demo_2d(): # Get the intersected element of a one dimensional grid cell_grid = CellGrid(grid_cell_spec=CellSpec( node_coords=[[-1, -1], [-1, 0], [-1, 1], [1, -1], [1, 0], [1, 1]]), shape=(5, 2), coord_max=[1., 1.]) geo_grid = GeoCellGrid(cell_grid=cell_grid) print 'vertex_X_grid', print geo_grid.cell_grid.vertex_X_grid ls_function = lambda x, y: x - 0.5 ls_mask_function = lambda x, y: y <= 0.75 print 'cell_grid' print cell_grid.cell_idx_grid print 'intersected' print geo_grid.get_intersected_cells(ls_function, ls_mask_function) print 'negative' print geo_grid.get_negative_cells(ls_function)
if __name__ == '__main__': from cell_spec import CellSpec # cell_grid = CellGrid( shape = (1,1), # grid_cell_spec = CellSpec( node_coords = [[-1,-1],[1,-1],[1,1],[-1,1]] ) ) # dof_grid = DofCellGrid( cell_grid = cell_grid ) # # print 'dofs' # print dof_grid.dofs # print 'idx_grid' # print dof_grid.cell_grid.idx_grid dof_grid = DofCellGrid(cell_grid=CellGrid(shape=(1, 1, 1)), dof_offset=1000) print 'idx_grid' print dof_grid.cell_grid.point_idx_grid print 'base node array' print dof_grid.cell_grid.base_nodes print 'left' print dof_grid.get_left_dofs() print 'right' print dof_grid.get_right_dofs() print 'bottom' print dof_grid.get_bottom_dofs() print 'top' print dof_grid.get_top_dofs() print 'back' print dof_grid.get_back_dofs()
from tkinter import * from cell_grid import CellGrid from biomes import Biomes from constants import * from random import randint if __name__ == "__main__": app = Tk() infection_rate = randint(90, 100) grid = CellGrid(app, num_rows, num_cols, pixels_per_cell, infection_rate, Biomes.PLAINS) grid.pack() app.mainloop()
def main() : root = "C:/Users/BAP/Documents/Econ - Nat'l Resources/Final Project/econ_260a-master/output_test" #root = "C:/Users/BAP/Documents/Econ - Nat'l Resources/Final Project/econ_260a-master/output_test_coop" if not os.path.exists(root) : os.mkdir(root) else : for listing in os.listdir(root) : fullpath = os.path.join(root, listing) if os.path.isfile(fullpath): os.unlink(fullpath) num_rows = 25 num_cols = 100 chance_destruction_lookup = simulate_neighbor_destruction(8) cells = CellGrid(num_rows, num_cols, chance_destruction_lookup) time_steps = 10 outputfile = os.path.join(root, "log.txt") with open(outputfile, 'w') as fout : fout.write("%s\n" % "Time,Developed,Wild,Fires Started,Burn Iterations,Total Burnt") for time_step in range(time_steps) : name = "devel_%d" % (time_step) filename = os.path.join(root, "%s.png" % name) #render current state cells.display(filename) #decide to build or not num_developed = cells.update_developed_state() #See if anything catches fire burn_iteration = 0 fire_susceptibility = random.uniform(.5,1.5) num_fires_started = 0 while True : #random walk starting from the current susceptibility fire_susceptibility += random.uniform(-.1-burn_iteration/float(20),.1) cells.update_fire_state(susceptibility=fire_susceptibility, no_new_start=burn_iteration > 0) if cells.state_counts[BURNING] > 0 : if burn_iteration == 0 : num_fires_started = cells.state_counts[BURNING] burn_name = "%s_burn_%d" % (name, burn_iteration) filename = os.path.join(root, "%s.png" % burn_name) cells.display(filename) else : break burn_iteration += 1 fout.write("%d,%d,%d,%d,%d,%d\n" % (time_step, cells.state_counts[DEVEL], cells.state_counts[WILD], num_fires_started, burn_iteration, cells.state_counts[BURNT]))
# Parametric coordinates of nodes involved in the slice # Structured element by element # point_x_arr = Property def _get_point_x_arr(self): return self.cell_grid.point_x_arr[self.nodes] if __name__ == '__main__': from cell_grid import CellGrid from cell_spec import CellSpec cell_grid = CellGrid( shape=(2, 8), grid_cell_spec=CellSpec( node_coords=[[-1, -1], [1, -1], [0, 0], [1, 1], [-1, 1]])) print 'cell_grid' print cell_grid.cell_idx_grid print 'cell_grid points' print cell_grid.point_idx_grid print 'grid_cell' print cell_grid.grid_cell print 'grid_cell[:,-1]' print cell_grid.grid_cell[:, -1] print 'sliced cells'