Skip to content

Discontinuities¤

exponax.ic.RandomDiscontinuities ¤

Bases: BaseRandomICGenerator

Source code in exponax/ic/_discontinuities.py
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
class RandomDiscontinuities(BaseRandomICGenerator):
    num_spatial_dims: int
    domain_extent: float
    num_discontinuities: int
    value_range: tuple[float, float]

    zero_mean: bool
    std_one: bool
    max_one: bool

    def __init__(
        self,
        num_spatial_dims: int,
        *,
        domain_extent: float = 1.0,
        num_discontinuities: int = 3,
        value_range: tuple[float, float] = (-1.0, 1.0),
        zero_mean: bool = False,
        std_one: bool = False,
        max_one: bool = False,
    ):
        """
        Random generator for initial states described by a collection of
        discontinuities.

        **Arguments**:
            - `num_spatial_dims`: The number of spatial dimensions.
            - `domain_extent`: The extent of the domain in each spatial direction.
            - `num_discontinuities`: The number of discontinuities.
            - `value_range`: The range of values for the discontinuities.
            - `zero_mean`: Whether the state should have zero mean.
            - `std_one`: Whether to normalize the state to have a standard
                deviation of one. Defaults to `False`. Only works if the offset
                is zero.
            - `max_one`: Whether to normalize the state to have the maximum
                absolute value of one. Defaults to `False`. Only one of
                `std_one` and `max_one` can be `True`.
        """
        if not zero_mean and std_one:
            raise ValueError("Cannot have `zero_mean=False` and `std_one=True`.")
        if std_one and max_one:
            raise ValueError("Cannot have `std_one=True` and `max_one=True`.")

        self.num_spatial_dims = num_spatial_dims
        self.domain_extent = domain_extent
        self.num_discontinuities = num_discontinuities
        self.value_range = value_range

        self.zero_mean = zero_mean
        self.std_one = std_one
        self.max_one = max_one

    def gen_one_ic_fn(self, *, key: PRNGKeyArray) -> Discontinuity:
        lower_limits = []
        upper_limits = []
        for i in range(self.num_spatial_dims):
            key_1, key_2, key = jr.split(key, 3)
            lim_1 = jr.uniform(key_1, (), minval=0.0, maxval=self.domain_extent)
            lim_2 = jr.uniform(key_2, (), minval=0.0, maxval=self.domain_extent)
            lower_limits.append(jnp.minimum(lim_1, lim_2))
            upper_limits.append(jnp.maximum(lim_1, lim_2))

        lower_limits = tuple(lower_limits)
        upper_limits = tuple(upper_limits)

        value = jr.uniform(
            key, (), minval=self.value_range[0], maxval=self.value_range[1]
        )

        return Discontinuity(
            lower_limits=lower_limits, upper_limits=upper_limits, value=value
        )

    def gen_ic_fun(self, *, key: PRNGKeyArray) -> BaseIC:
        disc_list = [
            self.gen_one_ic_fn(key=k) for k in jr.split(key, self.num_discontinuities)
        ]
        return Discontinuities(
            discontinuity_list=disc_list,
            zero_mean=self.zero_mean,
            std_one=self.std_one,
            max_one=self.max_one,
        )
__init__ ¤
__init__(
    num_spatial_dims: int,
    *,
    domain_extent: float = 1.0,
    num_discontinuities: int = 3,
    value_range: tuple[float, float] = (-1.0, 1.0),
    zero_mean: bool = False,
    std_one: bool = False,
    max_one: bool = False
)

Random generator for initial states described by a collection of discontinuities.

Arguments: - num_spatial_dims: The number of spatial dimensions. - domain_extent: The extent of the domain in each spatial direction. - num_discontinuities: The number of discontinuities. - value_range: The range of values for the discontinuities. - zero_mean: Whether the state should have zero mean. - std_one: Whether to normalize the state to have a standard deviation of one. Defaults to False. Only works if the offset is zero. - max_one: Whether to normalize the state to have the maximum absolute value of one. Defaults to False. Only one of std_one and max_one can be True.

Source code in exponax/ic/_discontinuities.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
def __init__(
    self,
    num_spatial_dims: int,
    *,
    domain_extent: float = 1.0,
    num_discontinuities: int = 3,
    value_range: tuple[float, float] = (-1.0, 1.0),
    zero_mean: bool = False,
    std_one: bool = False,
    max_one: bool = False,
):
    """
    Random generator for initial states described by a collection of
    discontinuities.

    **Arguments**:
        - `num_spatial_dims`: The number of spatial dimensions.
        - `domain_extent`: The extent of the domain in each spatial direction.
        - `num_discontinuities`: The number of discontinuities.
        - `value_range`: The range of values for the discontinuities.
        - `zero_mean`: Whether the state should have zero mean.
        - `std_one`: Whether to normalize the state to have a standard
            deviation of one. Defaults to `False`. Only works if the offset
            is zero.
        - `max_one`: Whether to normalize the state to have the maximum
            absolute value of one. Defaults to `False`. Only one of
            `std_one` and `max_one` can be `True`.
    """
    if not zero_mean and std_one:
        raise ValueError("Cannot have `zero_mean=False` and `std_one=True`.")
    if std_one and max_one:
        raise ValueError("Cannot have `std_one=True` and `max_one=True`.")

    self.num_spatial_dims = num_spatial_dims
    self.domain_extent = domain_extent
    self.num_discontinuities = num_discontinuities
    self.value_range = value_range

    self.zero_mean = zero_mean
    self.std_one = std_one
    self.max_one = max_one
__call__ ¤
__call__(
    num_points: int, *, key: PRNGKeyArray
) -> Float[Array, "1 ... N"]

Generate a random initial condition.

Arguments: - num_points: The number of grid points in each dimension. - key: A jax random key. - indexing: The indexing convention for the grid.

Returns: - u: The initial condition evaluated at the grid points.

Source code in exponax/ic/_base_ic.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def __call__(
    self,
    num_points: int,
    *,
    key: PRNGKeyArray,
) -> Float[Array, "1 ... N"]:
    """
    Generate a random initial condition.

    **Arguments**:
        - `num_points`: The number of grid points in each dimension.
        - `key`: A jax random key.
        - `indexing`: The indexing convention for the grid.

    **Returns**:
        - `u`: The initial condition evaluated at the grid points.
    """
    ic_fun = self.gen_ic_fun(key=key)
    grid = make_grid(
        self.num_spatial_dims,
        self.domain_extent,
        num_points,
        indexing=self.indexing,
    )
    return ic_fun(grid)

exponax.ic.Discontinuities ¤

Bases: BaseIC

Source code in exponax/ic/_discontinuities.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class Discontinuities(BaseIC):
    discontinuity_list: tuple[Discontinuity, ...]
    zero_mean: bool
    std_one: bool
    max_one: bool

    def __init__(
        self,
        discontinuity_list: tuple[Discontinuity, ...],
        *,
        zero_mean: bool = True,
        std_one: bool = False,
        max_one: bool = False,
    ):
        """
        A state described by a collection of discontinuities.

        **Arguments**:
            - `discontinuity_list`: A tuple of discontinuities.
            - `zero_mean`: Whether the state should have zero mean.
            - `std_one`: Whether to normalize the state to have a standard
                deviation of one. Defaults to `False`. Only works if the offset
                is zero.
            - `max_one`: Whether to normalize the state to have the maximum
                absolute value of one. Defaults to `False`. Only one of
                `std_one` and `max_one` can be `True`.
        """
        if not zero_mean and std_one:
            raise ValueError("Cannot have `zero_mean=False` and `std_one=True`.")
        if std_one and max_one:
            raise ValueError("Cannot have `std_one=True` and `max_one=True`.")

        self.discontinuity_list = discontinuity_list
        self.zero_mean = zero_mean
        self.std_one = std_one
        self.max_one = max_one

    def __call__(self, x: Array) -> Array:
        ic = sum(disc(x) for disc in self.discontinuity_list)

        if self.zero_mean:
            ic = ic - jnp.mean(ic)

        if self.std_one:
            ic = ic / jnp.std(ic)

        if self.max_one:
            ic = ic / jnp.max(jnp.abs(ic))

        return ic
__init__ ¤
__init__(
    discontinuity_list: tuple[Discontinuity, ...],
    *,
    zero_mean: bool = True,
    std_one: bool = False,
    max_one: bool = False
)

A state described by a collection of discontinuities.

Arguments: - discontinuity_list: A tuple of discontinuities. - zero_mean: Whether the state should have zero mean. - std_one: Whether to normalize the state to have a standard deviation of one. Defaults to False. Only works if the offset is zero. - max_one: Whether to normalize the state to have the maximum absolute value of one. Defaults to False. Only one of std_one and max_one can be True.

Source code in exponax/ic/_discontinuities.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def __init__(
    self,
    discontinuity_list: tuple[Discontinuity, ...],
    *,
    zero_mean: bool = True,
    std_one: bool = False,
    max_one: bool = False,
):
    """
    A state described by a collection of discontinuities.

    **Arguments**:
        - `discontinuity_list`: A tuple of discontinuities.
        - `zero_mean`: Whether the state should have zero mean.
        - `std_one`: Whether to normalize the state to have a standard
            deviation of one. Defaults to `False`. Only works if the offset
            is zero.
        - `max_one`: Whether to normalize the state to have the maximum
            absolute value of one. Defaults to `False`. Only one of
            `std_one` and `max_one` can be `True`.
    """
    if not zero_mean and std_one:
        raise ValueError("Cannot have `zero_mean=False` and `std_one=True`.")
    if std_one and max_one:
        raise ValueError("Cannot have `std_one=True` and `max_one=True`.")

    self.discontinuity_list = discontinuity_list
    self.zero_mean = zero_mean
    self.std_one = std_one
    self.max_one = max_one
__call__ ¤
__call__(x: Array) -> Array
Source code in exponax/ic/_discontinuities.py
64
65
66
67
68
69
70
71
72
73
74
75
76
def __call__(self, x: Array) -> Array:
    ic = sum(disc(x) for disc in self.discontinuity_list)

    if self.zero_mean:
        ic = ic - jnp.mean(ic)

    if self.std_one:
        ic = ic / jnp.std(ic)

    if self.max_one:
        ic = ic / jnp.max(jnp.abs(ic))

    return ic