<commit_before># This file is part of the pyMOR project (http://www.pymor.org).
# Copyright Holders: <NAME>, <NAME>, <NAME>
# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
#
# Contributors: <NAME> <<EMAIL>>

from __future__ import absolute_import, division, print_function

import numpy as np

from pymor.tools.floatcmp import float_cmp
from pymor.tools.relations import inverse_relation


def flatten_grid(grid):
    """This method is used by our visualizers to render n-dimensional grids which cannot
    be embedded into R^n by duplicating vertices which would have to be mapped to multiple
    points at once. (Think of grids on rectangular domains with identified edges.)

    Parameters
    ----------
    grid
        The |Grid| to flatten.

    Returns
    -------
    subentities
        The `subentities(0, grid.dim)` relation for the flattened grid.
    coordinates
        The coordinates of the codim-`grid.dim` entities.
    entity_map
        Maps the indices of the codim-`grid.dim` entities of the flattened
        grid to the indices of the corresponding entities in the original grid.
    """<commit_msg>[grids] speed up flatten_grid for known flat grids<commit_after># This file is part of the pyMOR project (http://www.pymor.org).
# Copyright Holders: <NAME>, <NAME>, <NAME>
# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
#
# Contributors: <NAME> <<EMAIL>>

from __future__ import absolute_import, division, print_function

import numpy as np

from pymor.tools.floatcmp import float_cmp
from pymor.tools.relations import inverse_relation
from pymor.grids.rect import RectGrid
from pymor.grids.tria import TriaGrid


def flatten_grid(grid):
    """This method is used by our visualizers to render n-dimensional grids which cannot
    be embedded into R^n by duplicating vertices which would have to be mapped to multiple
    points at once. (Think of grids on rectangular domains with identified edges.)

    Parameters
    ----------
    grid
        The |Grid| to flatten.

    Returns
    -------
    subentities
        The `subentities(0, grid.dim)` relation for the flattened grid.
    coordinates
        The coordinates of the codim-`grid.dim` entities.
    entity_map
        Maps the indices of the codim-`grid.dim` entities of the flattened
        grid to the indices of the corresponding entities in the original grid.
    """
    # special handling of known flat grids
    if isinstance(grid, (RectGrid, TriaGrid)) and not grid.identify_left_right and not grid.identify_bottom_top:
        subentities = grid.subentities(0, grid.dim)
        coordinates = grid.centers(grid.dim)
        entity_map = np.arange(grid.size(grid.dim), dtype=np.int32)
        return subentities, coordinates, entity_map

{'fraction_non_alphanumeric': 0.08729874776386404, 'fraction_numerical': 0.0032200357781753132, 'mean_word_length': 3.268702290076336, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5607620', 'n_tokens_mistral': 809, 'n_tokens_neox': 788, 'n_words': 303}