Beispiel #1
0
def check_dict_entries(main_dict=None, other_dict=None):
    '''Check if dictionary entry exists in main dictionary.'''
    if 'order' in main_dict.keys():
        main_keys = main_dict['order']
    else:
        main_keys = np.sort(list(main_dict.keys()))

    main_entries = np.array([main_dict[main_key] for main_key in main_keys])

    if 'order' in other_dict.keys():
        other_keys = other_dict['order']
    else:
        other_keys = np.sort(list(other_dict.keys()))

    out_key_order = []
    new_dict = other_dict.copy()
    replaced_keys = []
    for ikey, other_key in enumerate(other_keys):

        other_entry = new_dict[other_key]
        ind_found = pyres_utils.match_xy(main_entries, np.array(other_entry))
        if len(ind_found.squeeze()) > 0:
            old_key = new_dict.pop(other_key, None)
            replaced_keys.append([other_key, main_keys[ind_found[0]]])
            out_key_order.append(main_keys[ind_found[0]])
            new_dict[main_keys[ind_found[0]]] = main_dict[main_keys[
                ind_found[0]]]
        else:
            out_key_order.append(other_key)

    new_dict.update({'order': out_key_order})
    return new_dict, replaced_keys
Beispiel #2
0
def quad_mesh_region(mesh_xy=[None, None],
                     target_xypos=None,
                     target_res=None,
                     target_firstnrows=None,
                     region_elem_list=None,
                     background_res=None):
    """Define quadrilateral mesh region."""
    xx_mesh, yy_mesh = mesh_xy
    if len(xx_mesh.shape) == 1:
        elem_numbering = np.reshape(np.arange(
            xx_mesh.shape[0] * yy_mesh.shape[0]),
                                    (yy_mesh.shape[0], xx_mesh.shape[0]),
                                    order='F') + 1
        XX, YY = np.meshgrid(xx_mesh, yy_mesh)
    else:  # 2d mesh coordinates input
        elem_numbering = np.reshape(np.arange(
            xx_mesh.shape[0] * xx_mesh.shape[1]),
                                    (xx_mesh.shape[0], xx_mesh.shape[1]),
                                    order='F') + 1
        XX, YY = np.array(xx_mesh), np.array(yy_mesh)

    fill_cols = None

    if target_xypos is not None:
        all_xy_pairs = np.hstack([XX.reshape((-1, 1)), YY.reshape((-1, 1))])
        matched_inds = pyres_utils.match_xy(
            XY=all_xy_pairs,
            xy_to_match=np.array(target_xypos).astype(float)).squeeze()
        unraveled_inds = np.array(np.unravel_index(matched_inds, XX.shape)).T

        unique_cols = np.unique(unraveled_inds[:, 1])
        fill_cols = np.arange(
            unique_cols[0],
            unique_cols[-1])  # can have +1 at end to keep last match.
        start_end_rows = []
        for unq_col in unique_cols:
            rows_found = np.sort(unraveled_inds[unraveled_inds[:,
                                                               1] == unq_col,
                                                0])
            start_end_rows.append([rows_found[0], rows_found[-1]])

        start_end_rows = np.array(start_end_rows)
        start_rows_fill = np.round(
            np.interp(fill_cols, unique_cols, start_end_rows[:, 0]),
            0).astype(int)
        end_rows_fill = np.round(
            np.interp(fill_cols, unique_cols, start_end_rows[:, 1]),
            0).astype(int)

    elif target_firstnrows is not None:
        fill_cols = np.arange(XX.shape[1])
        start_rows_fill = np.zeros(XX.shape[1] - 1)
        end_rows_fill = target_firstnrows * np.ones(XX.shape[1] - 1)

    # Make background element region first
    if region_elem_list is None:
        # Initialize list with whole domain
        region_elem_list = [[
            1, int((XX.shape[0] - 1) * (XX.shape[1] - 1)), background_res
        ]]

    # Add other regions if needed
    if fill_cols is not None:
        for icol, irowstart, irowend in zip(fill_cols, start_rows_fill,
                                            end_rows_fill):
            elems_found = np.sort(elem_numbering[irowstart:irowend + 1, icol])
            if len(elems_found) > 1:
                region_elem_list.append([
                    elems_found[0] - icol, elems_found[-1] - icol - 1,
                    target_res
                ])
            else:
                region_elem_list.append(
                    [elems_found[0] - icol, elems_found[0] - icol, target_res])

    return region_elem_list