def test_flow__distance_irregular_grid_d4(): """Test to demonstrate that flow__distance utility works as expected with irregular grids""" # instantiate a model grid dx = 1.0 hmg = HexModelGrid(5, 3, dx) # instantiate and add the elevation field hmg.add_field("topographic__elevation", hmg.node_x + np.round(hmg.node_y), at="node") # instantiate the expected flow__distance array flow__distance_expected = np.array([ 0.0, 0.0, 0.0, 0.0, 0.0, dx, 0.0, 0.0, dx, dx, 2.0 * dx, 0.0, 0.0, 2.0 * dx, 2.0 * dx, 0.0, 0.0, 0.0, 0.0, ]) # setting boundary conditions hmg.set_closed_nodes(hmg.boundary_nodes) # calculating flow directions with FlowAccumulator component: D4 algorithm fr = FlowAccumulator(hmg, flow_director="D4") fr.run_one_step() # calculating flow distance map flow__distance = calculate_flow__distance(hmg, add_to_grid=True, noclobber=False) # test that the flow__distance utility works as expected assert_almost_equal(flow__distance_expected, flow__distance, decimal=10)
def test_flow__distance_irregular_grid_d4(): """Test to demonstrate that flow__distance utility works as expected with irregular grids""" # instantiate a model grid dx = 1.0 hmg = HexModelGrid(5, 3, dx) # instantiate and add the elevation field hmg.add_field( "topographic__elevation", hmg.node_x + np.round(hmg.node_y), at="node" ) # instantiate the expected flow__distance array flow__distance_expected = np.array( [ 0.0, 0.0, 0.0, 0.0, 0.0, dx, 0.0, 0.0, dx, dx, 2.0 * dx, 0.0, 0.0, 2.0 * dx, 2.0 * dx, 0.0, 0.0, 0.0, 0.0, ] ) # setting boundary conditions hmg.set_closed_nodes(hmg.boundary_nodes) # calculating flow directions with FlowAccumulator component: D4 algorithm fr = FlowAccumulator(hmg, flow_director="D4") fr.run_one_step() # calculating flow distance map flow__distance = calculate_flow__distance(hmg, add_to_grid=True, noclobber=False) # test that the flow__distance utility works as expected assert_almost_equal(flow__distance_expected, flow__distance, decimal=10)
def main(): # INITIALIZE # User-defined parameters nr = 21 nc = 21 plot_interval = 0.5 run_duration = 25.0 report_interval = 5.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 a grid hmg = HexModelGrid(nr, nc, 1.0, orientation='vertical', reorient_links=True) # Close the grid boundaries hmg.set_closed_nodes(hmg.open_boundary_nodes) # Set up the states and pair transitions. # Transition data here represent the disease status of a population. ns_dict = { 0 : 'fluid', 1 : 'grain' } xn_list = setup_transition_list() # Create data and initialize values. We start with the 3 middle columns full # of grains, and the others empty. node_state_grid = hmg.add_zeros('node', 'node_state_grid') middle = 0.25*(nc-1)*sqrt(3) is_middle_cols = logical_and(hmg.node_x<middle+1., hmg.node_x>middle-1.) node_state_grid[where(is_middle_cols)[0]] = 1 # Create the CA model ca = OrientedHexCTS(hmg, 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=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 = 21 nc = 21 plot_interval = 0.5 run_duration = 25.0 report_interval = 5.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 a grid hmg = HexModelGrid(nr, nc, 1.0, orientation='vertical', reorient_links=True) # Close the grid boundaries hmg.set_closed_nodes(hmg.open_boundary_nodes) # Set up the states and pair transitions. # Transition data here represent the disease status of a population. ns_dict = { 0 : 'fluid', 1 : 'grain' } xn_list = setup_transition_list() # Create data and initialize values. We start with the 3 middle columns full # of grains, and the others empty. node_state_grid = hmg.add_zeros('node', 'node_state_grid') middle = 0.25*(nc-1)*sqrt(3) is_middle_cols = logical_and(hmg.node_x<middle+1., hmg.node_x>middle-1.) node_state_grid[where(is_middle_cols)[0]] = 1 # Create the CA model ca = OrientedHexLCA(hmg, 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=False) current_time += plot_interval # Plot the current grid ca_plotter.update_plot() # FINALIZE # Plot ca_plotter.finalize()