|
| 1 | +from pina.equation import FixedValue, FixedGradient, FixedFlux, FixedLaplacian |
| 2 | +from pina import LabelTensor |
| 3 | +import torch |
| 4 | +import pytest |
| 5 | + |
| 6 | +# Define input and output values |
| 7 | +pts = LabelTensor(torch.rand(10, 3, requires_grad=True), labels=["x", "y", "z"]) |
| 8 | +u = torch.pow(pts, 2) |
| 9 | +u.labels = ["u", "v", "w"] |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize("value", [0, 10, -7.5]) |
| 13 | +@pytest.mark.parametrize("components", [None, "u", ["u", "w"]]) |
| 14 | +def test_fixed_value(value, components): |
| 15 | + |
| 16 | + # Constructor |
| 17 | + equation = FixedValue(value=value, components=components) |
| 18 | + |
| 19 | + # Residual |
| 20 | + residual = equation.residual(pts, u) |
| 21 | + len_c = len(components) if components is not None else u.shape[1] |
| 22 | + assert residual.shape == (pts.shape[0], len_c) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.parametrize("value", [0, 10, -7.5]) |
| 26 | +@pytest.mark.parametrize("components", [None, "u", ["u", "w"]]) |
| 27 | +@pytest.mark.parametrize("d", [None, "x", ["x", "z"]]) |
| 28 | +def test_fixed_gradient(value, components, d): |
| 29 | + |
| 30 | + # Constructor |
| 31 | + equation = FixedGradient(value=value, components=components, d=d) |
| 32 | + |
| 33 | + # Residual |
| 34 | + residual = equation.residual(pts, u) |
| 35 | + len_c = len(components) if components is not None else u.shape[1] |
| 36 | + len_d = len(d) if d is not None else pts.shape[1] |
| 37 | + assert residual.shape == (pts.shape[0], len_c * len_d) |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.parametrize("value", [0, 10, -7.5]) |
| 41 | +@pytest.mark.parametrize("components", [None, "u", ["u", "w"]]) |
| 42 | +@pytest.mark.parametrize("d", [None, "x", ["x", "z"]]) |
| 43 | +def test_fixed_flux(value, components, d): |
| 44 | + |
| 45 | + # Divergence requires components and d to be of the same length |
| 46 | + len_c = len(components) if components is not None else u.shape[1] |
| 47 | + len_d = len(d) if d is not None else pts.shape[1] |
| 48 | + if len_c != len_d: |
| 49 | + return |
| 50 | + |
| 51 | + # Constructor |
| 52 | + equation = FixedFlux(value=value, components=components, d=d) |
| 53 | + |
| 54 | + # Residual |
| 55 | + residual = equation.residual(pts, u) |
| 56 | + assert residual.shape == (pts.shape[0], 1) |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.parametrize("value", [0, 10, -7.5]) |
| 60 | +@pytest.mark.parametrize("components", [None, "u", ["u", "w"]]) |
| 61 | +@pytest.mark.parametrize("d", [None, "x", ["x", "z"]]) |
| 62 | +def test_fixed_laplacian(value, components, d): |
| 63 | + |
| 64 | + # Constructor |
| 65 | + equation = FixedLaplacian(value=value, components=components, d=d) |
| 66 | + |
| 67 | + # Residual |
| 68 | + residual = equation.residual(pts, u) |
| 69 | + len_c = len(components) if components is not None else u.shape[1] |
| 70 | + assert residual.shape == (pts.shape[0], len_c) |
0 commit comments