def set_up_neighbor_arrays(self, grid): # This function gets arrays of neighboring horizontal and vertical # links which are needed for the de Almeida solution # First we identify all active links self.active_ids = links.active_link_ids(grid.shape, grid.node_status) # And then find all horizontal link IDs (Active and Inactive) self.horizontal_ids = links.horizontal_link_ids(grid.shape) # And make the array 1-D self.horizontal_ids = self.horizontal_ids.flatten() # Find all horizontal active link ids self.horizontal_active_link_ids = links.horizontal_active_link_ids(grid.shape, self.active_ids) # Using the horizontal active link ids, we can find the west and east neighbors self.west_neighbors = links.find_horizontal_west_neighbor(grid.shape, self.horizontal_active_link_ids) self.east_neighbors = links.find_horizontal_east_neighbor(grid.shape, self.horizontal_active_link_ids) # Now we repeat this process for the vertical links. # First find the vertical link ids and reshape it into a 1-D array self.vertical_ids = links.vertical_link_ids(grid.shape).flatten() # Find the *active* verical link ids self.vertical_active_link_ids = links.vertical_active_link_ids(grid.shape, self.active_ids) # Using the active vertical link ids we can find the north and south vertical neighbors self.north_neighbors = links.find_vertical_north_neighbor(grid.shape, self.vertical_active_link_ids) self.south_neighbors = links.find_vertical_south_neighbor(grid.shape, self.vertical_active_link_ids) # Set up arrays for discharge in the horizontal and vertical directions. self.q_horizontal = np.zeros(links.number_of_horizontal_links(grid.shape)) self.q_vertical = np.zeros(links.number_of_vertical_links(grid.shape)) # Once the neighbor arrays are set up, we change the flag to True! self.neighbor_flag = True
# Then we find all horizontal link ids... horizontal_ids = links.horizontal_link_ids(mg.shape) # Get ids of left-most link ids for boundary issues... left_inactive_ids = horizontal_ids[:, 0] # Then we flatten our array so we can use it elsewhere. horizontal_ids = horizontal_ids.flatten() # ... and narrow them down to the active horizontal link ids. horizontal_active_link_ids = links.horizontal_active_link_ids(mg.shape, active_ids) # Here we actually identify, for each link, the id of its W and E neighbor. For the # de Almeida solution, horizontal neighbors are west and east. Only # active link ids are given, any inactive link id is replaced with a '-1'. west_neighbors = links.find_horizontal_west_neighbor(mg.shape, horizontal_ids) east_neighbors = links.find_horizontal_east_neighbor(mg.shape, horizontal_active_link_ids) # Now we do the same with all vertical link ids. First, we get ALL vertical ids. vertical_ids = links.vertical_link_ids(mg.shape).flatten() # And then we narrow them down to just the active vertical link ids. vertical_active_link_ids = links.vertical_active_link_ids(mg.shape, active_ids) # For the de Almeida solution, we only need N and S neighbors for vertical active links, # so for each link, the N and S neighbor ids are given by these two function calls. Any # inactive link id is replaced with an index of '-1'. north_neighbors = links.find_vertical_north_neighbor(mg.shape, vertical_active_link_ids) south_neighbors = links.find_vertical_south_neighbor(mg.shape, vertical_active_link_ids) # To solve this 2-D problem as two 1-D sets, we need to create arrays for
# Now we'll identify our leftmost, but interior, column and the IDs of those nodes. leftside = rmg.left_edge_node_ids() leftside = leftside+1 # One column in to prevent issues with BC # Initializing our class... of = OverlandFlow(rmg) # Now, we need to set a fixed value on the left edge, so we find the link neighbor arrays... of.set_up_neighbor_arrays(rmg) # ... and get a list of all horizonal ids, not just active ids (which is what the deAlmeida solution uses) all_horizontal_ids = links.horizontal_link_ids(rmg.shape) # from there, we are going to reset our west neighbor array... of.west_neighbors = links.find_horizontal_west_neighbor(rmg.shape, all_horizontal_ids) # and find the ids of the arrays along the west edge of the grid. We actually # will set the discharge values here at every time step in the loop. left_inactive_ids = links.left_edge_horizontal_ids(rmg.shape) # Let's see how long this run takes... starttime = time() while elapsed_time < run_time: # First, we calculate our time step. dt = of.gear_time_step(rmg) # Now we are going to set the left edge horizontal links to their neighboring discharge value rmg['link']['water_discharge'][left_inactive_ids] = rmg['link']['water_discharge'][left_inactive_ids + 1]