Skip to content

MeshDistribution

Bases: ABC

Abstract mesh distribution base class.

See also
Source code in pymesh/mesh/mesh_distributions.py
class MeshDistribution(ABC):
    """Abstract mesh distribution base class.

    See also:
        - [LinearDistribution][pymesh.mesh.mesh_distributions.LinearDistribution]
        - [CosineDistribution][pymesh.mesh.mesh_distributions.CosineDistribution]
        - [PowerDistribution][pymesh.mesh.mesh_distributions.PowerDistribution]
        - [ExponentialDistribution][pymesh.mesh.mesh_distributions.ExponentialDistribution]
    """

    def __init__(self, flip_direction: bool = False):
        self.flip_direction = flip_direction

    def __repr__(self) -> str:
        return self.__class__.__name__

    @property
    def flip_direction(self) -> bool:
        return self._flip_direction

    @flip_direction.setter
    def flip_direction(self, value) -> None:
        if not isinstance(value, bool):
            raise TypeError("flip_direction must be of type 'bool'")
        self._flip_direction = value

    @abstractmethod
    def copy(self) -> Self:
        """Returns a copy of self"""

    @abstractmethod
    def get_dist_fn(self, flip_direction: bool):
        """Returns distribution function which takes a single float from 0 to 1
        and returns a float between 0 and 1 according to the distribution type.
        """

    @staticmethod
    def flip_exp(exp, flip_direction: bool):
        """Flips 'exp' to '1.0 - exp' if flip_direction is True"""
        if not flip_direction:
            return exp
        return 1.0 - exp

    @staticmethod
    def validate_fn_input(u: int | float, flip_direction: bool) -> float:
        """Validates type of inputs u and flip_direction"""
        if not isinstance(flip_direction, bool):
            raise TypeError("flip_direction mus be of type 'bool'")
        if not isinstance(u, (int, float)):
            raise TypeError("u must be of type 'int' or 'float'")
        if isinstance(u, int):
            u = float(u)
        if u < 0 or u > 1:
            raise ValueError("u must be a value between 0 and 1")
        if flip_direction:
            u = 1.0 - u
        return u

copy abstractmethod #

copy()

Returns a copy of self

Source code in pymesh/mesh/mesh_distributions.py
@abstractmethod
def copy(self) -> Self:
    """Returns a copy of self"""

get_dist_fn abstractmethod #

get_dist_fn(flip_direction)

Returns distribution function which takes a single float from 0 to 1 and returns a float between 0 and 1 according to the distribution type.

Source code in pymesh/mesh/mesh_distributions.py
@abstractmethod
def get_dist_fn(self, flip_direction: bool):
    """Returns distribution function which takes a single float from 0 to 1
    and returns a float between 0 and 1 according to the distribution type.
    """

flip_exp staticmethod #

flip_exp(exp, flip_direction)

Flips 'exp' to '1.0 - exp' if flip_direction is True

Source code in pymesh/mesh/mesh_distributions.py
@staticmethod
def flip_exp(exp, flip_direction: bool):
    """Flips 'exp' to '1.0 - exp' if flip_direction is True"""
    if not flip_direction:
        return exp
    return 1.0 - exp

validate_fn_input staticmethod #

validate_fn_input(u, flip_direction)

Validates type of inputs u and flip_direction

Source code in pymesh/mesh/mesh_distributions.py
@staticmethod
def validate_fn_input(u: int | float, flip_direction: bool) -> float:
    """Validates type of inputs u and flip_direction"""
    if not isinstance(flip_direction, bool):
        raise TypeError("flip_direction mus be of type 'bool'")
    if not isinstance(u, (int, float)):
        raise TypeError("u must be of type 'int' or 'float'")
    if isinstance(u, int):
        u = float(u)
    if u < 0 or u > 1:
        raise ValueError("u must be a value between 0 and 1")
    if flip_direction:
        u = 1.0 - u
    return u