Skip to content

Commit 8cda42b

Browse files
modified doc and tests
1 parent 92addc3 commit 8cda42b

2 files changed

Lines changed: 85 additions & 28 deletions

File tree

pina/equation/system_equation.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,51 @@
88

99
class SystemEquation(EquationInterface):
1010
"""
11-
Implementation of the System of Equations. Every ``equation`` passed to a
12-
:class:`~pina.condition.condition.Condition` object must be either a
13-
:class:`~pina.equation.equation.Equation` or a
14-
:class:`~pina.equation.system_equation.SystemEquation` instance.
11+
Implementation of the System of Equations, to be passed to a
12+
:class:`~pina.condition.condition.Condition` object.
13+
14+
Unlike the :class:`~pina.equation.equation.Equation` class, which represents
15+
a single equation, the :class:`SystemEquation` class allows multiple
16+
equations to be grouped together into a system. This is particularly useful
17+
when dealing with multi-component outputs or coupled physical models, where
18+
the residual must be computed collectively across several constraints.
19+
20+
Each equation in the system must be either:
21+
- An instance of :class:`~pina.equation.equation.Equation`;
22+
- A callable function.
23+
24+
The residuals from each equation are computed independently and then
25+
aggregated using an optional reduction strategy (e.g., ``mean``, ``sum``).
26+
The resulting residual is returned as a single :class:`~pina.LabelTensor`.
27+
28+
:Example:
29+
30+
>>> from pina.equation import SystemEquation, FixedValue, FixedGradient
31+
>>> from pina import LabelTensor
32+
>>> import torch
33+
>>> pts = LabelTensor(torch.rand(10, 2), labels=["x", "y"])
34+
>>> pts.requires_grad = True
35+
>>> output_ = torch.pow(pts, 2)
36+
>>> output_.labels = ["u", "v"]
37+
>>> system_equation = SystemEquation(
38+
... [
39+
... FixedValue(value=1.0, components=["u"]),
40+
... FixedGradient(value=0.0, components=["v"],d=["y"]),
41+
... ],
42+
... reduction="mean",
43+
... )
44+
>>> residual = system_equation.residual(pts, output_)
45+
1546
"""
1647

1748
def __init__(self, list_equation, reduction=None):
1849
"""
1950
Initialization of the :class:`SystemEquation` class.
2051
21-
:param Callable equation: A ``torch`` callable function used to compute
22-
the residual of a mathematical equation.
52+
:param list_equation: A list containing either callable functions or
53+
instances of :class:`~pina.equation.equation.Equation`, used to
54+
compute the residuals of mathematical equations.
55+
:type list_equation: list[Callable] | list[Equation]
2356
:param str reduction: The reduction method to aggregate the residuals of
2457
each equation. Available options are: ``None``, ``mean``, ``sum``,
2558
``callable``.
@@ -46,7 +79,7 @@ def __init__(self, list_equation, reduction=None):
4679
self.reduction = reduction
4780
else:
4881
raise NotImplementedError(
49-
"Only mean and sum reductions implemented."
82+
"Only mean and sum reductions are currenly supported."
5083
)
5184

5285
def residual(self, input_, output_, params_=None):

tests/test_equations/test_system_equation.py

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,54 +24,78 @@ def foo():
2424
pass
2525

2626

27-
def test_constructor():
27+
@pytest.mark.parametrize("reduction", [None, "mean", "sum"])
28+
def test_constructor(reduction):
2829

29-
SystemEquation([eq1, eq2])
30-
SystemEquation([eq1, eq2], reduction="sum")
30+
# Constructor with callable functions
31+
SystemEquation([eq1, eq2], reduction=reduction)
32+
33+
# Constructor with Equation instances
3134
SystemEquation(
3235
[
3336
FixedValue(value=0.0, components=["u1"]),
3437
FixedGradient(value=0.0, components=["u2"]),
3538
],
36-
reduction="mean",
39+
reduction=reduction,
3740
)
3841

42+
# Constructor with mixed types
43+
SystemEquation(
44+
[
45+
FixedValue(value=0.0, components=["u1"]),
46+
eq1,
47+
],
48+
reduction=reduction,
49+
)
50+
51+
# Non-standard reduction not implemented
3952
with pytest.raises(NotImplementedError):
4053
SystemEquation([eq1, eq2], reduction="foo")
4154

55+
# Invalid input type
4256
with pytest.raises(ValueError):
4357
SystemEquation(foo)
4458

4559

46-
def test_residual():
60+
@pytest.mark.parametrize("reduction", [None, "mean", "sum"])
61+
def test_residual(reduction):
4762

63+
# Generate random points and output
4864
pts = LabelTensor(torch.rand(10, 2), labels=["x", "y"])
4965
pts.requires_grad = True
5066
u = torch.pow(pts, 2)
5167
u.labels = ["u1", "u2"]
5268

53-
eq_1 = SystemEquation([eq1, eq2], reduction="mean")
54-
res = eq_1.residual(pts, u)
55-
assert res.shape == torch.Size([10])
69+
# System with callable functions
70+
system_eq = SystemEquation([eq1, eq2], reduction=reduction)
71+
res = system_eq.residual(pts, u)
5672

57-
eq_1 = SystemEquation([eq1, eq2], reduction="sum")
58-
res = eq_1.residual(pts, u)
59-
assert res.shape == torch.Size([10])
73+
# Checks on the shape of the residual
74+
shape = torch.Size([10, 3]) if reduction is None else torch.Size([10])
75+
assert res.shape == shape
6076

61-
eq_1 = SystemEquation([eq1, eq2], reduction=None)
62-
res = eq_1.residual(pts, u)
63-
assert res.shape == torch.Size([10, 3])
77+
# System with Equation instances
78+
system_eq = SystemEquation(
79+
[
80+
FixedValue(value=0.0, components=["u1"]),
81+
FixedGradient(value=0.0, components=["u2"]),
82+
],
83+
reduction=reduction,
84+
)
6485

65-
eq_1 = SystemEquation([eq1, eq2])
66-
res = eq_1.residual(pts, u)
67-
assert res.shape == torch.Size([10, 3])
86+
# Checks on the shape of the residual
87+
shape = torch.Size([10, 3]) if reduction is None else torch.Size([10])
88+
assert res.shape == shape
6889

90+
# System with mixed types
6991
system_eq = SystemEquation(
7092
[
7193
FixedValue(value=0.0, components=["u1"]),
72-
FixedGradient(value=0.0, components=["u2"]),
94+
eq1,
7395
],
74-
reduction="mean",
96+
reduction=reduction,
7597
)
76-
res = system_eq.residual(pts, u)
77-
assert res.shape == torch.Size([10])
98+
99+
# Checks on the shape of the residual
100+
shape = torch.Size([10, 3]) if reduction is None else torch.Size([10])
101+
assert res.shape == shape

0 commit comments

Comments
 (0)