def initialize(self, grid_size=(5, 5), report_interval=5.0, grid_orientation="vertical", node_layout="rect", show_plots=False, cts_type="oriented_hex", run_duration=1.0, output_interval=1.0e99, plot_every_transition=False, **kwds): # Remember the clock time, and calculate when we next want to report # progress. self.current_real_time = time.time() self.next_report = self.current_real_time + report_interval self.report_interval = report_interval # Interval for output self.output_interval = output_interval # Duration for run self.run_duration = run_duration # Create a grid self.create_grid_and_node_state_field(grid_size[0], grid_size[1], grid_orientation, node_layout, cts_type) # Create the node-state dictionary ns_dict = self.node_state_dictionary() # Initialize values of the node-state grid nsg = self.initialize_node_state_grid() # Create the transition list xn_list = self.transition_list() # Create the CA object if cts_type == "raster": from landlab.ca.raster_cts import RasterCTS self.ca = RasterCTS(self.grid, ns_dict, xn_list, nsg) elif cts_type == "oriented_raster": from landlab.ca.oriented_raster_cts import OrientedRasterCTS self.ca = OrientedRasterCTS(self.grid, ns_dict, xn_list, nsg) elif cts_type == "hex": from landlab.ca.hex_cts import HexCTS self.ca = HexCTS(self.grid, ns_dict, xn_list, nsg) else: from landlab.ca.oriented_hex_cts import OrientedHexCTS self.ca = OrientedHexCTS(self.grid, ns_dict, xn_list, nsg) # Initialize graphics self._show_plots = show_plots if show_plots: self.initialize_plotting(**kwds)
def test_raster_cts(): """ Tests instantiation of a RasterCTS and implementation of one transition, with a callback function. """ # Set up a small grid with no events scheduled mg = RasterModelGrid((4, 4)) mg.set_closed_boundaries_at_grid_edges(True, True, True, True) node_state_grid = mg.add_ones("node", "node_state_map", dtype=int) node_state_grid[6] = 0 ns_dict = {0: "black", 1: "white"} xn_list = [] xn_list.append( Transition((1, 0, 0), (0, 1, 0), 0.1, "", True, callback_function)) pd = mg.add_zeros("node", "property_data", dtype=int) pd[5] = 50 ca = RasterCTS(mg, ns_dict, xn_list, node_state_grid, prop_data=pd, prop_reset_value=0) # Test the data structures assert ca.num_link_states == 4, "wrong number of link states" assert ca.prop_data[5] == 50, "error in property data" assert ca.num_node_states == 2, "error in num_node_states" assert ca.link_orientation[-1] == 0, "error in link orientation array" assert ca.link_state_dict[(1, 0, 0)] == 2, "error in link state dict" assert ca.n_trn[2] == 1, "error in n_trn" assert ca.node_pair[1] == (0, 1, 0), "error in cell_pair list" assert len(ca.priority_queue._queue) == 1, "event queue has wrong size" assert ca.next_trn_id.size == 24, "wrong size next_trn_id" assert ca.trn_id.shape == (4, 1), "wrong size for trn_to" assert ca.trn_id[2][0] == 0, "wrong value in trn_to" assert ca.trn_to[0] == 1, "wrong trn_to state" assert ca.trn_rate[0] == 0.1, "wrong trn rate" assert ca.trn_propswap[0] == 1, "wrong trn propswap" assert ca.trn_prop_update_fn == callback_function, "wrong prop upd" # Manipulate the data in the event queue for testing: # pop the scheduled event off the queue (event_time, index, event_link) = ca.priority_queue.pop() assert ca.priority_queue._queue == [], "event queue should now be empty but is not" # engineer an event ca.priority_queue.push(8, 1.0) ca.next_update[8] = 1.0 ca.next_trn_id[8] = 0 # run the CA ca.run(2.0) # some more tests. # Is current time advancing correctly? (should only go to 1.0, not 2.0) # Did the two nodes (5 and 6) correctly exchange states? # Did the property ID and data arrays get updated? Note that the "propswap" # should switch propids between nodes 5 and 6, and the callback function # should increase the value of prop_data in the "swap" node from 50 to 150. assert ca.current_time == 1.0, "current time incorrect" assert ca.node_state[5] == 0, "error in node state 5" assert ca.node_state[6] == 1, "error in node state 6"
def main(): # INITIALIZE # User-defined parameters nr = 200 # number of rows in grid nc = 200 # number of columns in grid plot_interval = 0.05 # time interval for plotting (unscaled) run_duration = 5.0 # duration of run (unscaled) report_interval = 10.0 # report interval, in real-time seconds frac_spacing = 10 # average fracture spacing, nodes outfilename = 'wx' # name for netCDF files # Remember the clock time, and calculate when we next want to report # progress. current_real_time = time.time() next_report = current_real_time + report_interval # Counter for output files time_slice = 0 # Create grid mg = RasterModelGrid(nr, nc, 1.0) # Make the boundaries be walls mg.set_closed_boundaries_at_grid_edges(True, True, True, True) # Set up the states and pair transitions. ns_dict = { 0 : 'rock', 1 : 'saprolite' } xn_list = setup_transition_list() # Create the node-state array and attach it to the grid. # (Note use of numpy's uint8 data type. This saves memory AND allows us # to write output to a netCDF3 file; netCDF3 does not handle the default # 64-bit integer type) node_state_grid = mg.add_zeros('node', 'node_state_map', dtype=np.uint8) node_state_grid[:] = make_frac_grid(frac_spacing, model_grid=mg) # Create the CA model ca = RasterCTS(mg, ns_dict, xn_list, node_state_grid) # Set up the color map rock_color = (0.8, 0.8, 0.8) sap_color = (0.4, 0.2, 0) clist = [rock_color, sap_color] my_cmap = matplotlib.colors.ListedColormap(clist) # Create a CAPlotter object for handling screen display ca_plotter = CAPlotter(ca, cmap=my_cmap) # Plot the initial grid ca_plotter.update_plot() # Output the initial grid to file write_netcdf((outfilename+str(time_slice)+'.nc'), mg, #format='NETCDF3_64BIT', names='node_state_map') # RUN current_time = 0.0 while current_time < run_duration: # Once in a while, print out simulation and real time to let the user # know that the sim is running ok current_real_time = time.time() if current_real_time >= next_report: print('Current sim time', current_time, '(', 100 * current_time/run_duration, '%)') next_report = current_real_time + report_interval # Run the model forward in time until the next output step ca.run(current_time+plot_interval, ca.node_state, plot_each_transition=False) current_time += plot_interval # Plot the current grid ca_plotter.update_plot() # Output the current grid to a netCDF file time_slice += 1 write_netcdf((outfilename+str(time_slice)+'.nc'), mg, #format='NETCDF3_64BIT', names='node_state_map') # FINALIZE # Plot ca_plotter.finalize()
def main(): # INITIALIZE # User-defined parameters nr = 100 # number of rows in grid nc = 64 # number of columns in grid plot_interval = 0.5 # time interval for plotting, sec run_duration = 200.0 # duration of run, sec report_interval = 10.0 # report interval, in real-time seconds # Remember the clock time, and calculate when we next want to report # progress. current_real_time = time.time() next_report = current_real_time + report_interval # Create grid mg = RasterModelGrid(nr, nc, 1.0) # Make the boundaries be walls mg.set_closed_boundaries_at_grid_edges(True, True, True, True) # Set up the states and pair transitions. ns_dict = {0: 'fluid', 1: 'particle'} xn_list = setup_transition_list() # Create the node-state array and attach it to the grid node_state_grid = mg.add_zeros('node', 'node_state_map', dtype=int) # Initialize the node-state array: here, the initial condition is a pile of # resting grains at the bottom of a container. bottom_rows = where(mg.node_y < 0.1 * nr)[0] node_state_grid[bottom_rows] = 1 # For visual display purposes, set all boundary nodes to fluid node_state_grid[mg.closed_boundary_nodes] = 0 # Create the CA model ca = RasterCTS(mg, ns_dict, xn_list, node_state_grid) grain = '#5F594D' fluid = '#D0E4F2' clist = [fluid, grain] my_cmap = matplotlib.colors.ListedColormap(clist) # Create a CAPlotter object for handling screen display ca_plotter = CAPlotter(ca, cmap=my_cmap) # Plot the initial grid ca_plotter.update_plot() # RUN current_time = 0.0 while current_time < run_duration: # Once in a while, print out simulation and real time to let the user # know that the sim is running ok current_real_time = time.time() if current_real_time >= next_report: print 'Current sim time', current_time, '(', 100 * current_time / run_duration, '%)' next_report = current_real_time + report_interval # Run the model forward in time until the next output step ca.run(current_time + plot_interval, ca.node_state, plot_each_transition=False) current_time += plot_interval # Plot the current grid ca_plotter.update_plot() # FINALIZE # Plot ca_plotter.finalize()
def main(): # INITIALIZE # User-defined parameters nr = 100 # number of rows in grid nc = 64 # number of columns in grid plot_interval = 0.5 # time interval for plotting, sec run_duration = 200.0 # duration of run, sec report_interval = 10.0 # report interval, in real-time seconds # Remember the clock time, and calculate when we next want to report # progress. current_real_time = time.time() next_report = current_real_time + report_interval # Create grid mg = RasterModelGrid(nr, nc, 1.0) # Make the boundaries be walls mg.set_closed_boundaries_at_grid_edges(True, True, True, True) # Set up the states and pair transitions. ns_dict = { 0 : 'fluid', 1 : 'particle' } xn_list = setup_transition_list() # Create the node-state array and attach it to the grid node_state_grid = mg.add_zeros('node', 'node_state_map', dtype=int) # Initialize the node-state array: here, the initial condition is a pile of # resting grains at the bottom of a container. bottom_rows = where(mg.node_y<0.1*nr)[0] node_state_grid[bottom_rows] = 1 # For visual display purposes, set all boundary nodes to fluid node_state_grid[mg.closed_boundary_nodes] = 0 # Create the CA model ca = RasterCTS(mg, ns_dict, xn_list, node_state_grid) grain = '#5F594D' fluid = '#D0E4F2' clist = [fluid,grain] my_cmap = matplotlib.colors.ListedColormap(clist) # Create a CAPlotter object for handling screen display ca_plotter = CAPlotter(ca, cmap=my_cmap) # Plot the initial grid ca_plotter.update_plot() # RUN current_time = 0.0 while current_time < run_duration: # Once in a while, print out simulation and real time to let the user # know that the sim is running ok current_real_time = time.time() if current_real_time >= next_report: print 'Current sim time',current_time,'(',100*current_time/run_duration,'%)' next_report = current_real_time + report_interval # Run the model forward in time until the next output step ca.run(current_time+plot_interval, ca.node_state, plot_each_transition=False) current_time += plot_interval # Plot the current grid ca_plotter.update_plot() # FINALIZE # Plot ca_plotter.finalize()
def main(): # INITIALIZE # User-defined parameters nr = 5 # number of rows in grid nc = 5 # number of columns in grid plot_interval = 10.0 # time interval for plotting, sec run_duration = 10.0 # duration of run, sec report_interval = 10.0 # report interval, in real-time seconds # Remember the clock time, and calculate when we next want to report # progress. current_real_time = time.time() next_report = current_real_time + report_interval # Create grid mg = RasterModelGrid(nr, nc, 1.0) # Make the boundaries be walls mg.set_closed_boundaries_at_grid_edges(True, True, True, True) # Set up the states and pair transitions. ns_dict = {0: 'black', 1: 'white'} xn_list = setup_transition_list() # Create the node-state array and attach it to the grid node_state_grid = mg.add_zeros('node', 'node_state_map', dtype=int) # For visual display purposes, set all boundary nodes to fluid node_state_grid[mg.closed_boundary_nodes] = 0 # Create the CA model ca = RasterCTS(mg, ns_dict, xn_list, node_state_grid) # Create a CAPlotter object for handling screen display ca_plotter = CAPlotter(ca) # Plot the initial grid ca_plotter.update_plot() # RUN current_time = 0.0 while current_time < run_duration: # Once in a while, print out simulation and real time to let the user # know that the sim is running ok current_real_time = time.time() if current_real_time >= next_report: print('Current sim time', current_time, '(', 100 * current_time / run_duration, '%)') next_report = current_real_time + report_interval # Run the model forward in time until the next output step ca.run(current_time + plot_interval, ca.node_state, plot_each_transition=True, plotter=ca_plotter) current_time += plot_interval # Plot the current grid ca_plotter.update_plot() # FINALIZE # Plot ca_plotter.finalize() print('ok, here are the keys') print(ca.__dict__.keys())
def test_raster_cts(): """ Tests instantiation of a RasterCTS and implementation of one transition, with a callback function. """ # Set up a small grid with no events scheduled mg = RasterModelGrid(4, 4, 1.0) mg.set_closed_boundaries_at_grid_edges(True, True, True, True) node_state_grid = mg.add_ones('node', 'node_state_map', dtype=int) node_state_grid[6] = 0 ns_dict = { 0 : 'black', 1 : 'white' } xn_list = [] xn_list.append( Transition((1,0,0), (0,1,0), 0.1, '', True, callback_function)) pd = mg.add_zeros('node', 'property_data', dtype=int) pd[5] = 50 ca = RasterCTS(mg, ns_dict, xn_list, node_state_grid, prop_data=pd, prop_reset_value=0) # Test the data structures assert (ca.xn_to.size==4), 'wrong size for xn_to' assert (ca.xn_to.shape==(4, 1)), 'wrong size for xn_to' assert (ca.xn_to[2][0]==1), 'wrong value in xn_to' assert (len(ca.event_queue)==1), 'event queue has wrong size' assert (ca.num_link_states==4), 'wrong number of link states' assert (ca.prop_data[5]==50), 'error in property data' assert (ca.xn_rate[2][0]==0.1), 'error in transition rate array' assert (ca._active_links_at_node[1][6]==8), 'error in active link array' assert (ca.num_node_states==2), 'error in num_node_states' assert (ca.link_orientation[-1]==0), 'error in link orientation array' assert (ca.link_state_dict[(1, 0, 0)]==2), 'error in link state dict' assert (ca.n_xn[2]==1), 'error in n_xn' assert (ca.node_pair[1]==(0, 1, 0)), 'error in cell_pair list' # Manipulate the data in the event queue for testing: # pop the scheduled event off the queue ev = heappop(ca.event_queue) assert (ca.event_queue==[]), 'event queue should now be empty but is not' # engineer an event ev.time = 1.0 ev.link = 8 ev.xn_to = 1 ev.propswap = True ev.prop_update_fn = callback_function ca.next_update[8] = 1.0 # push it onto the event queue heappush(ca.event_queue, ev) # run the CA ca.run(2.0) # some more tests. # Is current time advancing correctly? (should only go to 1.0, not 2.0) # Did the two nodes (5 and 6) correctly exchange states? # Did the property ID and data arrays get updated? Note that the "propswap" # should switch propids between nodes 5 and 6, and the callback function # should increase the value of prop_data in the "swap" node from 50 to 150. assert (ca.current_time==1.0), 'current time incorrect' assert (ca.node_state[5]==0), 'error in node state 5' assert (ca.node_state[6]==1), 'error in node state 6'
def main(): # INITIALIZE # User-defined parameters nr = 5 # number of rows in grid nc = 5 # number of columns in grid plot_interval = 10.0 # time interval for plotting, sec run_duration = 10.0 # duration of run, sec report_interval = 10.0 # report interval, in real-time seconds # Remember the clock time, and calculate when we next want to report # progress. current_real_time = time.time() next_report = current_real_time + report_interval # Create grid mg = RasterModelGrid(nr, nc, 1.0) # Make the boundaries be walls mg.set_closed_boundaries_at_grid_edges(True, True, True, True) # Set up the states and pair transitions. ns_dict = {0: "black", 1: "white"} xn_list = setup_transition_list() # Create the node-state array and attach it to the grid node_state_grid = mg.add_zeros("node", "node_state_map", dtype=int) # For visual display purposes, set all boundary nodes to fluid node_state_grid[mg.closed_boundary_nodes] = 0 # Create the CA model ca = RasterCTS(mg, ns_dict, xn_list, node_state_grid) # Create a CAPlotter object for handling screen display ca_plotter = CAPlotter(ca) # Plot the initial grid ca_plotter.update_plot() # RUN current_time = 0.0 while current_time < run_duration: # Once in a while, print out simulation and real time to let the user # know that the sim is running ok current_real_time = time.time() if current_real_time >= next_report: print( "Current sim time", current_time, "(", 100 * current_time / run_duration, "%)", ) next_report = current_real_time + report_interval # Run the model forward in time until the next output step ca.run( current_time + plot_interval, ca.node_state, plot_each_transition=True, plotter=ca_plotter, ) current_time += plot_interval # Plot the current grid ca_plotter.update_plot() # FINALIZE # Plot ca_plotter.finalize() print("ok, here are the keys") print(ca.__dict__.keys())
def initialize(self, grid_size=(5, 5), report_interval=5.0, grid_orientation='vertical', grid_shape='rect', show_plots=False, cts_type='oriented_hex', run_duration=1.0, output_interval=1.0e99, plot_every_transition=False, initial_state_grid=None, prop_data=None, prop_reset_value=None, **kwds): """Initialize CTSModel.""" # Remember the clock time, and calculate when we next want to report # progress. self.current_real_time = time.time() self.next_report = self.current_real_time + report_interval self.report_interval = report_interval # Interval for output self.output_interval = output_interval # Duration for run self.run_duration = run_duration # Create a grid self.create_grid_and_node_state_field(grid_size[0], grid_size[1], grid_orientation, grid_shape, cts_type) # If prop_data is a string, we assume it is a field name if isinstance(prop_data, string_types): prop_data = self.grid.add_zeros('node', prop_data) # Create the node-state dictionary ns_dict = self.node_state_dictionary() # Initialize values of the node-state grid if initial_state_grid is None: nsg = self.initialize_node_state_grid() else: try: nsg = initial_state_grid self.grid.at_node['node_state'][:] = nsg except: #TODO: use new Messaging capability print('If initial_state_grid given, must be array of int') raise # Create the transition list xn_list = self.transition_list() # Create the CA object if cts_type == 'raster': from landlab.ca.raster_cts import RasterCTS self.ca = RasterCTS(self.grid, ns_dict, xn_list, nsg, prop_data, prop_reset_value) elif cts_type == 'oriented_raster': from landlab.ca.oriented_raster_cts import OrientedRasterCTS self.ca = OrientedRasterCTS(self.grid, ns_dict, xn_list, nsg, prop_data, prop_reset_value) elif cts_type == 'hex': from landlab.ca.hex_cts import HexCTS self.ca = HexCTS(self.grid, ns_dict, xn_list, nsg, prop_data, prop_reset_value) else: from landlab.ca.oriented_hex_cts import OrientedHexCTS self.ca = OrientedHexCTS(self.grid, ns_dict, xn_list, nsg, prop_data, prop_reset_value) # Initialize graphics self._show_plots = show_plots if show_plots == True: self.initialize_plotting(**kwds)
#Create the transition list xn_list = setup_transition_list() # Create the node-state array and attach it to the grid node_state_grid = mg.add_zeros('node', 'node_state_map', dtype=int) # Initialize the node-state array: here, the initial condition is a set of fracture lines. node_state_grid.shape=((mg.number_of_node_rows,mg.number_of_node_columns)) node_state_grid=make_frac_grid(10, numrows=nr, numcols=nc, model_grid=node_state_grid) # For visual display purposes, set all boundary nodes to fluid node_state_grid[mg.closed_boundary_nodes] = 0 # Create the CA model ca = RasterCTS(mg, ns_dict, xn_list, node_state_grid) # Set up colors for plotting grain = '#5F594D' fluid = '#D0E4F2' clist = [fluid,grain] my_cmap = matplotlib.colors.ListedColormap(clist) # Create a CAPlotter object for handling screen display ca_plotter = CAPlotter(ca, cmap=my_cmap) # Plot the initial grid ca_plotter.update_plot() # RUN current_time = 0.0
def main(): # INITIALIZE # User-defined parameters nr = 100 # number of rows in grid nc = 64 # number of columns in grid plot_interval = 1.0 # time interval for plotting, sec -- each plot is 10^7 years run_duration = 100.0 # duration of run, sec report_interval = 10.0 # report interval, in real-time seconds # Remember the clock time, and calculate when we next want to report # progress. current_real_time = time.time() next_report = current_real_time + report_interval # Create grid mg = RasterModelGrid(nr, nc, 1.0) # Make array to record He count through time he = zeros(int(run_duration/plot_interval) + 1) time_step_count = 1 # Make the boundaries be walls #mg.set_closed_boundaries_at_grid_edges(True, True, True, True) # Set up the states and pair transitions. ns_dict = { 0 : 'empty', 1 : 'filled lattice', 2 : 'helium', 3 : 'uranium238right', 4 : 'uranium238left', 5 : 'uranium235right', 6 : 'uranium235left', 7 : 'thorium232right', 8 : 'thorium232left'} xn_list = setup_transition_list() # Create the node-state array and attach it to the grid node_state_grid = mg.add_zeros('node', 'node_state_map', dtype=int) # Initialize the node-state array: here, the initial condition is a pile of # resting grains at the bottom of a container. #odd_rows = where(mg.node_y<0.1*nr)[0] node_state_grid[:] = arange(nr*nc, dtype=int) % 2 for r in range(0, nr, 2): for c in range(nc): n = r*nc + c node_state_grid[n] = 1 - node_state_grid[n] print(node_state_grid) node_state_grid[0]=8 # For visual display purposes, set all boundary nodes to fluid node_state_grid[mg.closed_boundary_nodes] = 0 # Create the CA model ca = RasterCTS(mg, ns_dict, xn_list, node_state_grid) filled = '#cdcdc1' empty = '#ffffff' helium = '#7fffd4' uranium238right = '#ff1493' #pink uranium238left = '#ff1493' #pink uranium235right = '#8a2be2' #purple uranium235left = '#8a2be2' #purple thorium232right = '#f08080' #coral thorium232left = '#f08080' #coral clist = [empty, filled, helium, uranium238right, uranium238left, uranium235right, uranium235left, thorium232right, thorium232left] my_cmap = matplotlib.colors.ListedColormap(clist) # Create a CAPlotter object for handling screen display ca_plotter = CAPlotter(ca, cmap=my_cmap) # Plot the initial grid ca_plotter.update_plot() # RUN current_time = 0.0 while current_time < run_duration: # Once in a while, print out simulation and real time to let the user # know that the sim is running ok current_real_time = time.time() if current_real_time >= next_report: print 'Current sim time',current_time,'(',100*current_time/run_duration,'%)' next_report = current_real_time + report_interval # Run the model forward in time until the next output step ca.run(current_time+plot_interval, ca.node_state, plot_each_transition=True) current_time += plot_interval # Plot the current grid ca_plotter.update_plot() print bincount(node_state_grid) he[time_step_count] = bincount(node_state_grid)[2] time_step_count += 1 #plt.plot(bincount(node_state_grid)) #plt.ylabel('number of particles') # FINALIZE # Plot ca_plotter.finalize() plt.figure(2) plt.plot(he) plt.ylabel('Number of Helium Atoms') plt.xlabel('Run') plt.title('Number of helium particles present in model') plt.show()
def test_raster_cts(): """ Tests instantiation of a RasterCTS and implementation of one transition, with a callback function. """ # Set up a small grid with no events scheduled mg = RasterModelGrid((4, 4)) mg.set_closed_boundaries_at_grid_edges(True, True, True, True) node_state_grid = mg.add_ones("node", "node_state_map", dtype=int) node_state_grid[6] = 0 ns_dict = {0: "black", 1: "white"} xn_list = [] xn_list.append(Transition((1, 0, 0), (0, 1, 0), 0.1, "", True, callback_function)) pd = mg.add_zeros("node", "property_data", dtype=int) pd[5] = 50 ca = RasterCTS( mg, ns_dict, xn_list, node_state_grid, prop_data=pd, prop_reset_value=0 ) # Test the data structures assert ca.num_link_states == 4, "wrong number of link states" assert ca.prop_data[5] == 50, "error in property data" assert ca.num_node_states == 2, "error in num_node_states" assert ca.link_orientation[-1] == 0, "error in link orientation array" assert ca.link_state_dict[(1, 0, 0)] == 2, "error in link state dict" assert ca.n_trn[2] == 1, "error in n_trn" assert ca.node_pair[1] == (0, 1, 0), "error in cell_pair list" assert len(ca.priority_queue._queue) == 1, "event queue has wrong size" assert ca.next_trn_id.size == 24, "wrong size next_trn_id" assert ca.trn_id.shape == (4, 1), "wrong size for trn_to" assert ca.trn_id[2][0] == 0, "wrong value in trn_to" assert ca.trn_to[0] == 1, "wrong trn_to state" assert ca.trn_rate[0] == 0.1, "wrong trn rate" assert ca.trn_propswap[0] == 1, "wrong trn propswap" assert ca.trn_prop_update_fn == callback_function, "wrong prop upd" # Manipulate the data in the event queue for testing: # pop the scheduled event off the queue (event_time, index, event_link) = ca.priority_queue.pop() assert ca.priority_queue._queue == [], "event queue should now be empty but is not" # engineer an event ca.priority_queue.push(8, 1.0) ca.next_update[8] = 1.0 ca.next_trn_id[8] = 0 # run the CA ca.run(2.0) # some more tests. # Is current time advancing correctly? (should only go to 1.0, not 2.0) # Did the two nodes (5 and 6) correctly exchange states? # Did the property ID and data arrays get updated? Note that the "propswap" # should switch propids between nodes 5 and 6, and the callback function # should increase the value of prop_data in the "swap" node from 50 to 150. assert ca.current_time == 1.0, "current time incorrect" assert ca.node_state[5] == 0, "error in node state 5" assert ca.node_state[6] == 1, "error in node state 6"
def test_user_guide_example(): # INITIALIZE # User-defined parameters nr = 80 # number of rows in grid nc = 50 # number of columns in grid plot_interval = 0.5 # time interval for plotting, sec run_duration = 1.0 # duration of run, sec report_interval = 10.0 # report interval, in real-time seconds # Remember the clock time, and calculate when we next want to report # progress. current_real_time = time.time() next_report = current_real_time + report_interval # Create grid mg = RasterModelGrid(shape=(nr, nc), xy_spacing=1.0) # Make the boundaries be walls mg.set_closed_boundaries_at_grid_edges(True, True, True, True) # Create a node-state dictionary ns_dict = {0: "fluid", 1: "particle"} # Create the transition list xn_list = setup_transition_list() # Create an array containing the initial node-state values # Create the node-state array and attach it to the grid node_state_grid = mg.add_zeros("node", "node_state_map", dtype=int) # Initialize the node-state array: here, the initial condition is a pile of # resting grains at the bottom of a container. bottom_rows = where(mg.node_y < 0.1 * nr)[0] node_state_grid[bottom_rows] = 1 # For visual display purposes, set all boundary nodes to fluid node_state_grid[mg.closed_boundary_nodes] = 0 # Create the CA model ca = RasterCTS(mg, ns_dict, xn_list, node_state_grid) # Set up plotting # Set up colors for plotting # (commented out for CI testing) # grain = '#5F594D' # fluid = '#D0E4F2' # clist = [fluid,grain] # my_cmap = matplotlib.colors.ListedColormap(clist) # Create a CAPlotter object for handling screen display # (commented out for CI testing) # ca_plotter = CAPlotter(ca, cmap=my_cmap) # Plot the initial grid (commented out for CI testing) # ca_plotter.update_plot() # RUN current_time = 0.0 while current_time < run_duration: # Once in a while, print out simulation real time to let the user # know that the sim is running ok current_real_time = time.time() if current_real_time >= next_report: print("Current simulation time " + str(current_time) + " \ (" + str(int(100 * current_time / run_duration)) + "%)") next_report = current_real_time + report_interval # Run the model forward in time until the next output step ca.run(current_time + plot_interval, ca.node_state, plot_each_transition=False) current_time += plot_interval
# Create the node-state array and attach it to the grid node_state_grid = mg.add_zeros('node', 'node_state_map', dtype=int) # Initialize the node-state array: here, the initial condition is a set of fracture lines. node_state_grid.shape = ((mg.number_of_node_rows, mg.number_of_node_columns)) node_state_grid = make_frac_grid(10, numrows=nr, numcols=nc, model_grid=node_state_grid) # For visual display purposes, set all boundary nodes to fluid node_state_grid[mg.closed_boundary_nodes] = 0 # Create the CA model ca = RasterCTS(mg, ns_dict, xn_list, node_state_grid) # Set up colors for plotting grain = '#5F594D' fluid = '#D0E4F2' clist = [fluid, grain] my_cmap = matplotlib.colors.ListedColormap(clist) # Create a CAPlotter object for handling screen display ca_plotter = CAPlotter(ca, cmap=my_cmap) # Plot the initial grid ca_plotter.update_plot() # RUN current_time = 0.0