landlab.ca.raster_cts¶
raster_cts.py: simple raster continuous-time stochastic cellular automaton
This file defines the RasterCTS class, which is a sub-class of CellLabCTSModel that implements a simple, non-oriented, raster-grid CA. Like its parent class, RasterCTS implements a continuous-time, stochastic, pair-based CA.
Created GT Sep 2014, starting from link_ca.py.
- class RasterCTS[source]¶
Bases:
CellLabCTSModel
Class RasterLCA implements a non-oriented raster CellLab-CTS model.
RasterLCA constructor: sets number of orientations to 1 and calls base-class constructor.
- Parameters:
model_grid (Landlab ModelGrid object) – Reference to the model’s grid
node_state_dict (dict) – Keys are node-state codes, values are the names associated with these codes
transition_list (list of Transition objects) – List of all possible transitions in the model
initial_node_states (array of ints (x number of nodes in grid)) – Starting values for node-state grid
prop_data (array (x number of nodes in grid) (optional)) – Array of properties associated with each node/cell
prop_reset_value (number or object, optional) – Default or initial value for a node/cell property (e.g., 0.0). Must be same type as prop_data.
seed (int (default 0)) – Seed for random number generator
Examples
>>> from landlab import RasterModelGrid >>> from landlab.ca.celllab_cts import Transition >>> from landlab.ca.raster_cts import RasterCTS
>>> mg = RasterModelGrid((3, 4)) >>> nsd = {0: "yes", 1: "no"} >>> xnlist = [] >>> xnlist.append(Transition((0, 1, 0), (1, 1, 0), 1.0, "frogging")) >>> nsg = mg.add_zeros("node_state_grid", at="node") >>> rcts = RasterCTS(mg, nsd, xnlist, nsg)
RasterLCA constructor: sets number of orientations to 1 and calls base-class constructor.
- Parameters:
model_grid (Landlab ModelGrid object) – Reference to the model’s grid
node_state_dict (dict) – Keys are node-state codes, values are the names associated with these codes
transition_list (list of Transition objects) – List of all possible transitions in the model
initial_node_states (array of ints (x number of nodes in grid)) – Starting values for node-state grid
prop_data (array (x number of nodes in grid) (optional)) – Array of properties associated with each node/cell
prop_reset_value (number or object, optional) – Default or initial value for a node/cell property (e.g., 0.0). Must be same type as prop_data.
- __init__(model_grid, node_state_dict, transition_list, initial_node_states, prop_data=None, prop_reset_value=None, seed=0)[source]¶
RasterLCA constructor: sets number of orientations to 1 and calls base-class constructor.
- Parameters:
model_grid (Landlab ModelGrid object) – Reference to the model’s grid
node_state_dict (dict) – Keys are node-state codes, values are the names associated with these codes
transition_list (list of Transition objects) – List of all possible transitions in the model
initial_node_states (array of ints (x number of nodes in grid)) – Starting values for node-state grid
prop_data (array (x number of nodes in grid) (optional)) – Array of properties associated with each node/cell
prop_reset_value (number or object, optional) – Default or initial value for a node/cell property (e.g., 0.0). Must be same type as prop_data.
- __new__(**kwargs)¶
- assign_link_states_from_node_types()¶
Assign link-state code for each link.
Takes lists/arrays of “tail” and “head” node IDs for each link, and a dictionary that associates pairs of node states (represented as a 3-element tuple, comprising the TAIL state, FROM state, and orientation) to link states.
creates:
self.link_state
: 1D numpy array
- create_link_state_dict_and_pair_list()¶
Create a dict of link-state to node-state.
Creates a dictionary that can be used as a lookup table to find out which link state corresponds to a particular pair of node states. The dictionary keys are 3-element tuples, each of which represents the state of the TAIL node, the HEAD node, and the orientation of the link. The values are integer codes representing the link state numbers.
Notes
Performance note: making self.node_pair a tuple does not appear to change time to lookup values in update_node_states. Changing it to a 2D array of int actually slows it down.
- push_transitions_to_event_queue()¶
Initializes the event queue by creating transition events for each cell pair that has one or more potential transitions and pushing these onto the queue. Also records scheduled transition times in the self.next_update array.
Examples
>>> from landlab import RasterModelGrid >>> from landlab.ca.celllab_cts import Transition >>> from landlab.ca.oriented_raster_cts import OrientedRasterCTS >>> import numpy as np >>> grid = RasterModelGrid((3, 5)) >>> nsd = {0: "zero", 1: "one"} >>> trn_list = [] >>> trn_list.append(Transition((0, 1, 0), (1, 0, 0), 1.0)) >>> trn_list.append(Transition((1, 0, 0), (0, 1, 0), 2.0)) >>> trn_list.append(Transition((0, 1, 1), (1, 0, 1), 3.0)) >>> trn_list.append(Transition((0, 1, 1), (1, 1, 1), 4.0)) >>> ins = np.arange(15) % 2 >>> cts = OrientedRasterCTS(grid, nsd, trn_list, ins) >>> ev0 = cts.priority_queue._queue[0] >>> np.round(100 * ev0[0]) 12.0 >>> ev0[2] # this is the link ID 16 >>> ev6 = cts.priority_queue._queue[6] >>> np.round(100 * ev6[0]) 27.0 >>> ev6[2] # this is the link ID 6 >>> cts.next_trn_id[ev0[2]] # ID of the transition to occur at this link 3 >>> cts.next_trn_id[cts.grid.active_links] array([-1, 2, -1, 1, 0, 1, 0, 2, -1, 3])
- run(run_to, node_state_grid=None, plot_each_transition=False, plotter=None)¶
Run the model forward for a specified period of time.
- Parameters:
run_to (float) – Time to run to, starting from self.current_time
node_state_grid (1D array of ints (x number of nodes) (optional)) – Node states (if given, replaces model’s current node state grid)
plot_each_transition (bool (optional)) – Option to display the grid after each transition
plotter (CAPlotter object (optional)) – Needed if caller wants to plot after every transition
Examples
>>> from landlab import RasterModelGrid >>> from landlab.ca.celllab_cts import Transition >>> from landlab.ca.oriented_raster_cts import OrientedRasterCTS >>> import numpy as np >>> grid = RasterModelGrid((3, 5)) >>> nsd = {0: "zero", 1: "one"} >>> trn_list = [] >>> trn_list.append(Transition((0, 1, 0), (1, 0, 0), 1.0)) >>> trn_list.append(Transition((1, 0, 0), (0, 1, 0), 2.0)) >>> trn_list.append(Transition((0, 1, 1), (1, 0, 1), 3.0)) >>> trn_list.append(Transition((0, 1, 1), (1, 1, 1), 4.0)) >>> ins = np.arange(15) % 2 >>> cts = OrientedRasterCTS(grid, nsd, trn_list, ins)
- set_node_state_grid(node_states)¶
Set the grid of node-state codes to node_states.
Sets the grid of node-state codes to node_states. Also checks to make sure node_states is in the proper format, which is to say, it’s a Numpy array of the same length as the number of nodes in the grid.
Creates:
self.node_state : 1D array of ints (x number of nodes in grid) The node-state array
- Parameters:
node_states (1D array of ints (x number of nodes in grid))
Notes
The node-state array is attached to the grid as a field with the name ‘node_state’.
- setup_array_of_orientation_codes()¶
Create array of active link orientation codes.
Creates and configures an array that contain the orientation code for each active link (and corresponding cell pair).
creates:
self.link_orientation
: 1D numpy array
Notes
The setup varies depending on the type of LCA. The default is non-oriented, in which case we just have an array of zeros. Subclasses will override this method to handle lattices in which orientation matters (for example, vertical vs. horizontal in an OrientedRasterLCA).
- setup_transition_data(xn_list)¶
Create transition data arrays.
- update_component_data(new_node_state_array)¶
Update all component data.
Call this method to update all data held by the component, if, for example, another component or boundary conditions modify the node statuses outside the component between run steps.
This method updates all necessary properties, including both node and link states.
new_node_state_array is the updated list of node states, which must still all be compatible with the state list originally supplied to this component.
Examples
>>> from landlab import RasterModelGrid >>> from landlab.ca.celllab_cts import Transition >>> from landlab.ca.raster_cts import RasterCTS >>> import numpy as np >>> grid = RasterModelGrid((3, 5)) >>> nsd = {0: "zero", 1: "one"} >>> trn_list = [] >>> trn_list.append(Transition((0, 1, 0), (1, 1, 0), 1.0)) >>> ins = np.zeros(15, dtype=int) >>> ca = RasterCTS(grid, nsd, trn_list, ins) >>> list(ca.node_state[6:9]) [0, 0, 0] >>> list(ca.link_state[9:13]) [0, 0, 0, 0] >>> len(ca.priority_queue._queue) # there are no transitions 0 >>> nns = np.arange(15) % 2 # make a new node-state grid... >>> ca.update_component_data(nns) # ...and assign it >>> list(ca.node_state[6:9]) [0, 1, 0] >>> list(ca.link_state[9:13]) [2, 1, 2, 1] >>> len(ca.priority_queue._queue) # now there are 5 transitions 5
- update_link_state_new(link, new_link_state, current_time)¶
Implements a link transition by updating the current state of the link and (if appropriate) choosing the next transition event and pushing it on to the event queue.