Exemple #1
0
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)

    # 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.node_active_links[1][6] == 16, "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 = 16
    ev.xn_to = 1
    ev.propswap = True
    ev.prop_update_fn = callback_function
    ca.next_update[16] = 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 = 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 = 80  # number of rows in grid
    nc = 50  # number of columns in grid
    plot_interval = 0.5   # time interval for plotting, sec
    run_duration = 0.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)

    # 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
    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 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

        # Plot the current grid
        ca_plotter.update_plot()


    # FINALIZE

    # Plot
    ca_plotter.finalize()
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()