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
Example #2
0
    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
# 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.horizontal_west_link_neighbor(mg.shape, horizontal_ids)
east_neighbors = (links.horizontal_east_link_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.vertical_north_link_neighbor(mg.shape,
                                                    vertical_active_link_ids))
south_neighbors = (links.vertical_south_link_neighbor(mg.shape,
                                                    vertical_active_link_ids))


# First we calculate our updated boundary water depth
h_boundary = (((seven_over_three) * n * n * u * u * u * elapsed_time) **
    (three_over_seven))      # water depth at left side (m)

# And now we add it to the second column, in all rows that are not boundary rows.
h[inside_left_edge] = h_boundary

# Main loop
while elapsed_time <= run_time:
    # Calculate time-step size for this iteration (Bates et al., eq 14)
Example #4
0
    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 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