|
| 1 | +"""Module for defining the Burgers equation.""" |
| 2 | + |
| 3 | +from pina._src.core.operator import laplacian, grad |
| 4 | +from pina._src.core.utils import check_consistency |
| 5 | +from pina._src.equation.equation import Equation |
| 6 | +import torch |
| 7 | + |
| 8 | + |
| 9 | +class BurgersEquation(Equation): |
| 10 | + r""" |
| 11 | + Implementation of the N-dimensional Burgers equation, defined as follows: |
| 12 | +
|
| 13 | + .. math:: |
| 14 | +
|
| 15 | + \frac{\partial u}{\partial t} + u \cdot \nabla u = \nu \Delta u |
| 16 | +
|
| 17 | + Here, :math:`\nu` is the viscosity coefficient. |
| 18 | + """ |
| 19 | + |
| 20 | + def __init__(self, nu): |
| 21 | + """ |
| 22 | + Initialization of the :class:`BurgersEquation` class. |
| 23 | +
|
| 24 | + :param nu: The viscosity coefficient. |
| 25 | + :type nu: float | int |
| 26 | + :raises ValueError: If ``nu`` is not a float or an int. |
| 27 | + :raises ValueError: If ``nu`` is negative. |
| 28 | + """ |
| 29 | + # Check consistency |
| 30 | + check_consistency(nu, (float, int)) |
| 31 | + if nu < 0: |
| 32 | + raise ValueError( |
| 33 | + "The viscosity ``nu`` must be a positive float or int." |
| 34 | + ) |
| 35 | + |
| 36 | + # Store viscosity coefficient |
| 37 | + self.nu = nu |
| 38 | + |
| 39 | + def equation(input_, output_): |
| 40 | + """ |
| 41 | + Implementation of the Burgers equation. |
| 42 | +
|
| 43 | + :param LabelTensor input_: The input data of the problem. |
| 44 | + :param LabelTensor output_: The output data of the problem. |
| 45 | + :raises ValueError: If the number of output components does not |
| 46 | + match the number of spatial dimensions. |
| 47 | + :raises ValueError: If the ``input_`` labels do not contain the time |
| 48 | + variable 't'. |
| 49 | + :return: The residual of the Burgers equation. |
| 50 | + :rtype: LabelTensor |
| 51 | + """ |
| 52 | + # Store labels |
| 53 | + spatial_d = [di for di in input_.labels if di != "t"] |
| 54 | + |
| 55 | + # Ensure consistency between output and spatial dimensions |
| 56 | + if len(output_.labels) != len(spatial_d): |
| 57 | + raise ValueError( |
| 58 | + f"The number of output components must match the number of " |
| 59 | + f"spatial dimensions. Got {len(output_.labels)} and " |
| 60 | + f"{len(spatial_d)}." |
| 61 | + ) |
| 62 | + |
| 63 | + # Ensure time is passed as input |
| 64 | + if "t" not in input_.labels: |
| 65 | + raise ValueError( |
| 66 | + "The ``input_`` labels must contain the time 't' variable." |
| 67 | + ) |
| 68 | + |
| 69 | + # Compute the differential terms |
| 70 | + u_t = grad(output_, input_, d=["t"]) |
| 71 | + u_x = grad(output_, input_, d=spatial_d) |
| 72 | + u_xx = laplacian(output_, input_, d=spatial_d) |
| 73 | + |
| 74 | + # Compute the convective term componentwise |
| 75 | + convection = torch.zeros_like(output_) |
| 76 | + for i, c in enumerate(output_.labels): |
| 77 | + convection[:, i] = sum( |
| 78 | + output_[output_.labels[j]] * u_x[f"d{c}d{spatial_d[j]}"] |
| 79 | + for j in range(len(spatial_d)) |
| 80 | + ).reshape(-1) |
| 81 | + |
| 82 | + return u_t + convection - self.nu * u_xx |
| 83 | + |
| 84 | + super().__init__(equation) |
0 commit comments