Skip to content

beignet.func

beignet.func.space

space(box=None, *, normalized=True, parallelepiped=True, remapped=True)

Define a simulation space.

This function is fundamental in constructing simulation spaces derived from subsets of \(\mathbb{R}^{D}\) (where \(D = 1\), \(2\), or \(3\)) and is instrumental in setting up simulation environments with specific characteristics (e.g., periodic boundary conditions). The function returns a displacement function and a shift function to compute particle interactions and movements in space.

This function supports deformation of the simulation cell, crucial for certain types of simulations, such as those involving finite deformations or the computation of elastic constants.

Parameters:

Name Type Description Default
box Tensor | None

Interpretation varies based on the value of parallelepiped. If parallelepiped is True, must be an affine transformation, \(T\), specified in one of three ways:

  1. a cube, \(L\);
  2. an orthorhombic unit cell, \([L_{x}, L_{y}, L_{z}]\); or
  3. a triclinic cell, upper triangular matrix.

If parallelepiped is False, must be the edge lengths.

If box is None, the simulation space has free boundary conditions.

None
normalized bool

If normalized is True, positions are stored in the unit cube. Displacements and shifts are computed in a normalized simulation space and can be transformed back to real simulation space using the affine transformation. If normalized is False, positions are expressed and computations performed directly in the real simulation space.

True
parallelepiped bool

If True, the simulation space is defined as a \({1, 2, 3}\)-dimensional parallelepiped with periodic boundary conditions. If False, the space is defined on a \({1, 2, 3}\)-dimensional hypercube.

True
remapped bool

If True, positions and displacements are remapped to stay in the bounds of the defined simulation space. A rempapped simulation space is topologically equivalent to a torus, ensuring that particles exiting one boundary re-enter from the opposite side.

True

Returns:

Type Description
(Callable[[Tensor, Tensor], Tensor], Callable[[Tensor, Tensor], Tensor])

A pair of functions:

  1. The displacement function, \(\vec{d}\), measures the difference between two points in the simulation space, factoring in the geometry and boundary conditions. This function is used to calculate particle interactions and dynamics.

  2. The shift function, \(u\), applies a displacement vector to a point in the space, effectively moving it. This function is used to update simulated particle positions.

Examples:

transformation = torch.tensor([10.0])

displacement_fn, shift_fn = space(
    transformation,
    normalized=False,
)

normalized_displacement_fn, normalized_shift_fn = space(
    transformation,
    normalized=True,
)

normalized_position = torch.rand([4, 3])

position = transformation * normalized_position

displacement = torch.randn([4, 3])

torch.testing.assert_close(
    displacement_fn(position[0], position[1]),
    normalized_displacement_fn(
        normalized_position[0],
        normalized_position[1],
    ),
)
Source code in src/beignet/func/_space.py
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 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
 77
 78
 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
def space(
    box: Tensor | None = None,
    *,
    normalized: bool = True,
    parallelepiped: bool = True,
    remapped: bool = True,
) -> (Callable, Callable):
    r"""Define a simulation space.

    This function is fundamental in constructing simulation spaces derived from
    subsets of $\mathbb{R}^{D}$ (where $D = 1$, $2$, or $3$) and is
    instrumental in setting up simulation environments with specific
    characteristics (e.g., periodic boundary conditions). The function returns
    a displacement function and a shift function to compute particle
    interactions and movements in space.

    This function supports deformation of the simulation cell, crucial for
    certain types of simulations, such as those involving finite deformations
    or the computation of elastic constants.

    Parameters
    ----------
    box : Tensor | None, default=None
        Interpretation varies based on the value of `parallelepiped`. If
        `parallelepiped` is `True`, must be an affine transformation, $T$,
        specified in one of three ways:

        1. a cube, $L$;
        2. an orthorhombic unit cell, $[L_{x}, L_{y}, L_{z}]$; or
        3. a triclinic cell, upper triangular matrix.

        If `parallelepiped` is `False`, must be the edge lengths.

        If `box` is `None`, the simulation space has free boundary conditions.

    normalized : bool, default=True
        If `normalized` is `True`, positions are stored in the unit cube.
        Displacements and shifts are computed in a normalized simulation space
        and can be transformed back to real simulation space using the affine
        transformation. If `normalized` is `False`, positions are expressed and
        computations performed directly in the real simulation space.

    parallelepiped : bool, default=True
        If `True`, the simulation space is defined as a ${1, 2, 3}$-dimensional
        parallelepiped with periodic boundary conditions. If `False`, the space
        is defined on a ${1, 2, 3}$-dimensional hypercube.

    remapped : bool, default=True
        If `True`, positions and displacements are remapped to stay in the
        bounds of the defined simulation space. A rempapped simulation space is
        topologically equivalent to a torus, ensuring that particles exiting
        one boundary re-enter from the opposite side.

    Returns
    -------
    (Callable[[Tensor, Tensor], Tensor], Callable[[Tensor, Tensor], Tensor])
        A pair of functions:

        1.  The displacement function, $\vec{d}$, measures the
            difference between two points in the simulation space, factoring in
            the geometry and boundary conditions. This function is used to
            calculate particle interactions and dynamics.

        2.  The shift function, $u$, applies a displacement vector to a point
            in the space, effectively moving it. This function is used to
            update simulated particle positions.

    Examples
    --------
        transformation = torch.tensor([10.0])

        displacement_fn, shift_fn = space(
            transformation,
            normalized=False,
        )

        normalized_displacement_fn, normalized_shift_fn = space(
            transformation,
            normalized=True,
        )

        normalized_position = torch.rand([4, 3])

        position = transformation * normalized_position

        displacement = torch.randn([4, 3])

        torch.testing.assert_close(
            displacement_fn(position[0], position[1]),
            normalized_displacement_fn(
                normalized_position[0],
                normalized_position[1],
            ),
        )
    """
    if isinstance(box, (int, float)):
        box = torch.tensor([box])

    if box is None:

        def displacement_fn(
            input: Tensor,
            other: Tensor,
            *,
            perturbation: Tensor | None = None,
            **_,
        ) -> Tensor:
            if len(input.shape) != 1:
                raise ValueError

            if input.shape != other.shape:
                raise ValueError

            if perturbation is not None:
                transform = input - other

                match transform.ndim:
                    case 0:
                        return perturbation * transform
                    case 1:
                        return torch.einsum(
                            "i,...i->...i",
                            transform,
                            perturbation,
                        )
                    case 2:
                        return torch.einsum(
                            "ij,...j->...i",
                            transform,
                            perturbation,
                        )
                    case _:
                        raise ValueError

            return input - other

        def shift_fn(input: Tensor, other: Tensor, **_) -> Tensor:
            return input + other

        return displacement_fn, shift_fn

    if parallelepiped:
        inverted_transform = beignet.invert_transform(box)

        if normalized:

            def displacement_fn(
                input: Tensor,
                other: Tensor,
                *,
                perturbation: Tensor | None = None,
                **kwargs,
            ) -> Tensor:
                _transform = box

                _inverted_transform = inverted_transform

                if "transform" in kwargs:
                    _transform = kwargs["transform"]

                if "updated_transform" in kwargs:
                    _transform = kwargs["updated_transform"]

                if len(input.shape) != 1:
                    raise ValueError

                if input.shape != other.shape:
                    raise ValueError

                displacement = beignet.apply_transform(
                    torch.remainder(input - other + 1.0 * 0.5, 1.0) - 1.0 * 0.5,
                    _transform,
                )

                if perturbation is not None:
                    match displacement.ndim:
                        case 0:
                            return perturbation * displacement
                        case 1:
                            return torch.einsum(
                                "i,...i->...i",
                                displacement,
                                perturbation,
                            )
                        case 2:
                            return torch.einsum(
                                "ij,...j->...i",
                                displacement,
                                perturbation,
                            )
                        case _:
                            raise ValueError

                return displacement

            if remapped:

                def u(input: Tensor, other: Tensor) -> Tensor:
                    return torch.remainder(input + other, 1.0)

                def shift_fn(input: Tensor, other: Tensor, **kwargs) -> Tensor:
                    _transform = box

                    _inverted_transform = inverted_transform

                    if "transform" in kwargs:
                        _transform = kwargs["transform"]

                        _inverted_transform = beignet.invert_transform(_transform)

                    if "updated_transform" in kwargs:
                        _transform = kwargs["updated_transform"]

                    return u(input, beignet.apply_transform(other, _inverted_transform))

                return displacement_fn, shift_fn

            def shift_fn(input: Tensor, other: Tensor, **kwargs) -> Tensor:
                _transform = box

                _inverted_transform = inverted_transform

                if "transform" in kwargs:
                    _transform = kwargs["transform"]

                    _inverted_transform = beignet.invert_transform(_transform)

                if "updated_transform" in kwargs:
                    _transform = kwargs["updated_transform"]

                return input + beignet.apply_transform(other, _inverted_transform)

            return displacement_fn, shift_fn

        def displacement_fn(
            input: Tensor,
            other: Tensor,
            *,
            perturbation: Tensor | None = None,
            **kwargs,
        ) -> Tensor:
            _transform = box

            _inverted_transform = inverted_transform

            if "transform" in kwargs:
                _transform = kwargs["transform"]

                _inverted_transform = beignet.invert_transform(_transform)

            if "updated_transform" in kwargs:
                _transform = kwargs["updated_transform"]

            input = beignet.apply_transform(input, _inverted_transform)
            other = beignet.apply_transform(other, _inverted_transform)

            if len(input.shape) != 1:
                raise ValueError

            if input.shape != other.shape:
                raise ValueError

            displacement = beignet.apply_transform(
                torch.remainder(input - other + 1.0 * 0.5, 1.0) - 1.0 * 0.5,
                _transform,
            )

            if perturbation is not None:
                match displacement.ndim:
                    case 0:
                        return perturbation * displacement
                    case 1:
                        return torch.einsum(
                            "i,...i->...i",
                            displacement,
                            perturbation,
                        )
                    case 2:
                        return torch.einsum(
                            "ij,...j->...i",
                            displacement,
                            perturbation,
                        )
                    case _:
                        raise ValueError

            return displacement

        if remapped:

            def u(input: Tensor, other: Tensor) -> Tensor:
                return torch.remainder(input + other, 1.0)

            def shift_fn(input: Tensor, other: Tensor, **kwargs) -> Tensor:
                _transform = box

                _inverted_transform = inverted_transform

                if "transform" in kwargs:
                    _transform = kwargs["transform"]

                    _inverted_transform = beignet.invert_transform(
                        _transform,
                    )

                if "updated_transform" in kwargs:
                    _transform = kwargs["updated_transform"]

                return beignet.apply_transform(
                    u(
                        beignet.apply_transform(_inverted_transform, input),
                        beignet.apply_transform(_inverted_transform, other),
                    ),
                    _transform,
                )

            return displacement_fn, shift_fn

        def shift_fn(input: Tensor, other: Tensor, **_) -> Tensor:
            return input + other

        return displacement_fn, shift_fn

    def displacement_fn(
        input: Tensor,
        other: Tensor,
        *,
        perturbation: Tensor | None = None,
        **_,
    ) -> Tensor:
        if len(input.shape) != 1:
            raise ValueError

        if input.shape != other.shape:
            raise ValueError

        displacement = torch.remainder(
            input - other + box * 0.5,
            box,
        )

        if perturbation is not None:
            transform = displacement - box * 0.5

            match transform.ndim:
                case 0:
                    return perturbation * transform
                case 1:
                    return torch.einsum(
                        "i,...i->...i",
                        transform,
                        perturbation,
                    )
                case 2:
                    return torch.einsum(
                        "ij,...j->...i",
                        transform,
                        perturbation,
                    )
                case _:
                    raise ValueError

        return displacement - box * 0.5

    if remapped:

        def shift_fn(input: Tensor, other: Tensor, **_) -> Tensor:
            return torch.remainder(input + other, box)
    else:

        def shift_fn(input: Tensor, other: Tensor, **_) -> Tensor:
            return input + other

    return displacement_fn, shift_fn