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) # 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) if self.use_fixed_links: fixed_link_ids = links.fixed_link_ids(grid.shape, grid.node_status) fixed_horizontal_links = links.horizontal_fixed_link_ids( grid.shape, fixed_link_ids) fixed_vertical_links = links.vertical_fixed_link_ids( grid.shape, fixed_link_ids) self.horizontal_active_link_ids = np.maximum( self.horizontal_active_link_ids, fixed_horizontal_links) self.vertical_active_link_ids = np.maximum( self.vertical_active_link_ids, fixed_vertical_links) # Using the active vertical link ids we can find the north and south # vertical neighbors self.north_neighbors = links.vertical_north_link_neighbor( grid.shape, self.vertical_active_link_ids) self.south_neighbors = links.vertical_south_link_neighbor( grid.shape, self.vertical_active_link_ids) # Using the horizontal active link ids, we can find the west and east # neighbors self.west_neighbors = links.horizontal_west_link_neighbor( grid.shape, self.horizontal_active_link_ids) self.east_neighbors = links.horizontal_east_link_neighbor( grid.shape, self.horizontal_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
def test_active_links_ids(): status = np.empty((4, 5), dtype=int) status.fill(CLOSED_BOUNDARY) status[1, 2] = status[1, 3] = status[2, 2] = status[2, 3] = CORE_NODE link_ids = active_link_ids((4, 5), status) assert_array_equal(link_ids, [7, 8, 21, 25]) assert_array_is_int(link_ids)
def test_active_links_ids(): status = np.empty((4, 5), dtype=int) status.fill(CLOSED_BOUNDARY) status[1, 2] = status[1, 3] = status[2, 2] = status[2, 3] = CORE_NODE link_ids = active_link_ids((4, 5), status) assert_array_equal(link_ids, [11, 15, 16, 20]) # assert_array_equal(link_ids, [7, 8, 21, 25]) assert_array_is_int(link_ids)
# Left side has deep water leftside = mg.left_edge_node_ids() leftside = leftside + 1 # One column in to prevent issues with BC # Get a list of the core nodes core_nodes = mg.core_nodes active_links = mg.active_links # Display a message print ("Running ...") start_time = time.time() # These functions find link neighbors for horizontal and vertical active links. # First, we find all active links in the raster grid. active_ids = links.active_link_ids(mg.shape, mg.status_at_node) # 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
def set_up_neighbor_arrays(self): """Create and initialize link neighbor arrays. Set up arrays of neighboring horizontal and vertical links that are needed for the de Almeida solution. """ # First we identify all active links self._active_ids = links.active_link_ids( self._grid.shape, self._grid.status_at_node ) self._active_links_at_open_bdy = _active_links_at_node( self.grid, self.grid.open_boundary_nodes ).transpose() self._active_links_at_open_bdy = self._active_links_at_open_bdy[ np.where(self._active_links_at_open_bdy > -1) ] # And then find all horizontal link IDs (Active and Inactive) self._horizontal_ids = links.horizontal_link_ids(self._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( self._grid.shape, self._active_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(self._grid.shape).flatten() # Find the *active* verical link ids self._vertical_active_link_ids = links.vertical_active_link_ids( self._grid.shape, self._active_ids ) if self._default_fixed_links is True: fixed_link_ids = links.fixed_link_ids( self._grid.shape, self._grid.status_at_node ) fixed_horizontal_links = links.horizontal_fixed_link_ids( self._grid.shape, fixed_link_ids ) fixed_vertical_links = links.vertical_fixed_link_ids( self._grid.shape, fixed_link_ids ) self._horizontal_active_link_ids = np.maximum( self._horizontal_active_link_ids, fixed_horizontal_links ) self._vertical_active_link_ids = np.maximum( self._vertical_active_link_ids, fixed_vertical_links ) self._active_neighbors = find_active_neighbors_for_fixed_links(self._grid) self._vert_bdy_ids = self._active_links_at_open_bdy[ links.is_vertical_link(self._grid.shape, self._active_links_at_open_bdy) ] self._vert_bdy_ids = links.nth_vertical_link( self._grid.shape, self._vert_bdy_ids ) self._horiz_bdy_ids = self._active_links_at_open_bdy[ links.is_horizontal_link(self._grid.shape, self._active_links_at_open_bdy) ] self._horiz_bdy_ids = links.nth_horizontal_link( self._grid.shape, self._horiz_bdy_ids ) # Using the active vertical link ids we can find the north # and south vertical neighbors self._north_neighbors = links.vertical_north_link_neighbor( self._grid.shape, self._vertical_active_link_ids ) self._south_neighbors = links.vertical_south_link_neighbor( self._grid.shape, self._vertical_active_link_ids ) # Using the horizontal active link ids, we can find the west and # east neighbors self._west_neighbors = links.horizontal_west_link_neighbor( self._grid.shape, self._horizontal_active_link_ids ) self._east_neighbors = links.horizontal_east_link_neighbor( self._grid.shape, self._horizontal_active_link_ids ) # replace bdy condition links (ids,) = np.where(self._west_neighbors[self._horiz_bdy_ids] == -1) ids = self._horiz_bdy_ids[ids] self._west_neighbors[ids] = self._horizontal_active_link_ids[ids] (ids,) = np.where(self._east_neighbors[self._horiz_bdy_ids] == -1) ids = self._horiz_bdy_ids[ids] self._east_neighbors[ids] = self._horizontal_active_link_ids[ids] (ids,) = np.where(self._north_neighbors[self._vert_bdy_ids] == -1) ids = self._vert_bdy_ids[ids] self._north_neighbors[ids] = self._vertical_active_link_ids[ids] (ids,) = np.where(self._south_neighbors[self._vert_bdy_ids] == -1) ids = self._vert_bdy_ids[ids] self._south_neighbors[ids] = self._vertical_active_link_ids[ids] # Set up arrays for discharge in the horizontal & vertical directions. self._q_horizontal = np.zeros( links.number_of_horizontal_links(self._grid.shape) ) self._q_vertical = np.zeros(links.number_of_vertical_links(self._grid.shape)) # Once the neighbor arrays are set up, we change the flag to True! self._neighbor_flag = True
def set_up_neighbor_arrays(self): """Create and initialize link neighbor arrays. Set up arrays of neighboring horizontal and vertical links that are needed for the de Almeida solution. """ # First we identify all active links self.active_ids = links.active_link_ids(self.grid.shape, self.grid.status_at_node) # And then find all horizontal link IDs (Active and Inactive) self.horizontal_ids = links.horizontal_link_ids(self.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( self.grid.shape, self.active_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(self.grid.shape).flatten() # Find the *active* verical link ids self.vertical_active_link_ids = links.vertical_active_link_ids( self.grid.shape, self.active_ids) if self.default_fixed_links is True: fixed_link_ids = links.fixed_link_ids(self.grid.shape, self.grid.status_at_node) fixed_horizontal_links = links.horizontal_fixed_link_ids( self.grid.shape, fixed_link_ids) fixed_vertical_links = links.vertical_fixed_link_ids( self.grid.shape, fixed_link_ids) self.horizontal_active_link_ids = np.maximum( self.horizontal_active_link_ids, fixed_horizontal_links) self.vertical_active_link_ids = np.maximum( self.vertical_active_link_ids, fixed_vertical_links) self.active_neighbors = find_active_neighbors_for_fixed_links( self.grid) # Using the active vertical link ids we can find the north # and south vertical neighbors self.north_neighbors = links.vertical_north_link_neighbor( self.grid.shape, self.vertical_active_link_ids) self.south_neighbors = links.vertical_south_link_neighbor( self.grid.shape, self.vertical_active_link_ids) # Using the horizontal active link ids, we can find the west and # east neighbors self.west_neighbors = links.horizontal_west_link_neighbor( self.grid.shape, self.horizontal_active_link_ids) self.east_neighbors = links.horizontal_east_link_neighbor( self.grid.shape, self.horizontal_active_link_ids) # Set up arrays for discharge in the horizontal & vertical directions. self.q_horizontal = np.zeros( links.number_of_horizontal_links(self.grid.shape)) self.q_vertical = np.zeros( links.number_of_vertical_links(self.grid.shape)) # Once the neighbor arrays are set up, we change the flag to True! self.neighbor_flag = True
def set_up_neighbor_arrays(self): """Create and initialize link neighbor arrays. Set up arrays of neighboring horizontal and vertical links that are needed for the de Almeida solution. """ # First we identify all active links self.active_ids = links.active_link_ids( self.grid.shape, self.grid.status_at_node ) self.active_links_at_open_bdy = self.grid._active_links_at_node( self.grid.open_boundary_nodes ).transpose() self.active_links_at_open_bdy = self.active_links_at_open_bdy[ np.where(self.active_links_at_open_bdy > -1) ] # And then find all horizontal link IDs (Active and Inactive) self.horizontal_ids = links.horizontal_link_ids(self.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( self.grid.shape, self.active_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(self.grid.shape).flatten() # Find the *active* verical link ids self.vertical_active_link_ids = links.vertical_active_link_ids( self.grid.shape, self.active_ids ) if self.default_fixed_links is True: fixed_link_ids = links.fixed_link_ids( self.grid.shape, self.grid.status_at_node ) fixed_horizontal_links = links.horizontal_fixed_link_ids( self.grid.shape, fixed_link_ids ) fixed_vertical_links = links.vertical_fixed_link_ids( self.grid.shape, fixed_link_ids ) self.horizontal_active_link_ids = np.maximum( self.horizontal_active_link_ids, fixed_horizontal_links ) self.vertical_active_link_ids = np.maximum( self.vertical_active_link_ids, fixed_vertical_links ) self.active_neighbors = find_active_neighbors_for_fixed_links(self.grid) self.vert_bdy_ids = self.active_links_at_open_bdy[ links.is_vertical_link(self.grid.shape, self.active_links_at_open_bdy) ] self.vert_bdy_ids = links.nth_vertical_link(self.grid.shape, self.vert_bdy_ids) self.horiz_bdy_ids = self.active_links_at_open_bdy[ links.is_horizontal_link(self.grid.shape, self.active_links_at_open_bdy) ] self.horiz_bdy_ids = links.nth_horizontal_link( self.grid.shape, self.horiz_bdy_ids ) # Using the active vertical link ids we can find the north # and south vertical neighbors self.north_neighbors = links.vertical_north_link_neighbor( self.grid.shape, self.vertical_active_link_ids ) self.south_neighbors = links.vertical_south_link_neighbor( self.grid.shape, self.vertical_active_link_ids ) # Using the horizontal active link ids, we can find the west and # east neighbors self.west_neighbors = links.horizontal_west_link_neighbor( self.grid.shape, self.horizontal_active_link_ids ) self.east_neighbors = links.horizontal_east_link_neighbor( self.grid.shape, self.horizontal_active_link_ids ) # replace bdy condition links (ids,) = np.where(self.west_neighbors[self.horiz_bdy_ids] == -1) ids = self.horiz_bdy_ids[ids] self.west_neighbors[ids] = self.horizontal_active_link_ids[ids] (ids,) = np.where(self.east_neighbors[self.horiz_bdy_ids] == -1) ids = self.horiz_bdy_ids[ids] self.east_neighbors[ids] = self.horizontal_active_link_ids[ids] (ids,) = np.where(self.north_neighbors[self.vert_bdy_ids] == -1) ids = self.vert_bdy_ids[ids] self.north_neighbors[ids] = self.vertical_active_link_ids[ids] (ids,) = np.where(self.south_neighbors[self.vert_bdy_ids] == -1) ids = self.vert_bdy_ids[ids] self.south_neighbors[ids] = self.vertical_active_link_ids[ids] # Set up arrays for discharge in the horizontal & vertical directions. self.q_horizontal = np.zeros(links.number_of_horizontal_links(self.grid.shape)) self.q_vertical = np.zeros(links.number_of_vertical_links(self.grid.shape)) # Once the neighbor arrays are set up, we change the flag to True! self.neighbor_flag = True
def test_active_link_ids_with_shape_mismatch(): with pytest.raises(ValueError): active_link_ids((3, 4), np.zeros(3))
def test_active_links_with_edge_boundaries(): status = status_with_perimeter_as_boundary((3, 4)) link_ids = active_link_ids((3, 4), status) assert_array_equal(link_ids, [8]) assert_array_is_int(link_ids)
def test_active_link_ids_with_shape_mismatch(): active_link_ids((3, 4), np.zeros(3))
def find_active_neighbors_for_fixed_links(grid): ''' Specialized link ID function used to ID the active links that neighbor fixed links in the vertical and horizontal directions. If the user wants to assign fixed gradients or values to the fixed links dynamically, this function identifies the nearest active_link neighbor. Each fixed link can either have 0 or 1 active neighbor. This function finds if and where that active neighbor is and stores those IDs in an array. ''' shape = grid.shape status_at_node = grid.status_at_node # First, we identify fixed links using node status fixed_links = links.fixed_link_ids(shape, status_at_node) # Identifying *just* fixed links IDs. fixed_ids_only = fixed_links[np.where(fixed_links > -1)] # Identifying active link IDs. active_links = links.active_link_ids(shape, status_at_node) # Identifying vertical active link IDs. vertical_active_links = (links.vertical_active_link_ids(shape, active_links)) # Identifying horizontal active link IDs. horizontal_active_links = (links.horizontal_active_link_ids(shape, active_links)) # Identifying north vertical active link IDs. north_vert = (links.vertical_north_link_neighbor(shape, vertical_active_links)) # Identifying south verical active link IDs. south_vert = (links.vertical_south_link_neighbor(shape, vertical_active_links)) # Identifying horizontal east active link IDs. east_hori = (links.horizontal_east_link_neighbor(shape, horizontal_active_links)) # Identifying horizontal west active link IDs. west_hori = (links.horizontal_west_link_neighbor(shape, horizontal_active_links)) # Because each fixed link can have at most 1 active neighbor, there # is at least one "BAD_INDEX_VALUE" link neighbor (-1). The maximum # ID value will be the active neighbor. This finds the N/S vertical # active neighbor and the E/W horizontal neighbor. max_vertical_neighbor = np.maximum(north_vert, south_vert) max_horizontal_neighbor = np.maximum(east_hori, west_hori) # Concatenating the vertical and horizontal arrays to get one # neighbor array of len(all links) all_active_neighbors = (np.concatenate((max_vertical_neighbor, max_horizontal_neighbor), axis=0)) # Getting JUST the active neighbor IDs for fixed links. This # sets the array to a new length - that of len(fixed_links) all_active_neighbors = all_active_neighbors[fixed_ids_only] return all_active_neighbors
# Left side has deep water leftside = mg.left_edge_node_ids() leftside = leftside+1 # One column in to prevent issues with BC # Get a list of the core nodes core_nodes = mg.core_nodes active_links = mg.active_links # Display a message print( 'Running ...' ) start_time = time.time() # These functions find link neighbors for horizontal and vertical active links. # First, we find all active links in the raster grid. active_ids = links.active_link_ids(mg.shape, mg.node_status) # 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