1- from pina .equation import SystemEquation
1+ from pina .equation import SystemEquation , FixedValue , FixedGradient
22from pina .operator import grad , laplacian
33from pina import LabelTensor
44import torch
@@ -24,34 +24,78 @@ def foo():
2424 pass
2525
2626
27- def test_constructor ():
28- SystemEquation ([eq1 , eq2 ])
29- SystemEquation ([eq1 , eq2 ], reduction = "sum" )
27+ @pytest .mark .parametrize ("reduction" , [None , "mean" , "sum" ])
28+ def test_constructor (reduction ):
29+
30+ # Constructor with callable functions
31+ SystemEquation ([eq1 , eq2 ], reduction = reduction )
32+
33+ # Constructor with Equation instances
34+ SystemEquation (
35+ [
36+ FixedValue (value = 0.0 , components = ["u1" ]),
37+ FixedGradient (value = 0.0 , components = ["u2" ]),
38+ ],
39+ reduction = reduction ,
40+ )
41+
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
3052 with pytest .raises (NotImplementedError ):
3153 SystemEquation ([eq1 , eq2 ], reduction = "foo" )
54+
55+ # Invalid input type
3256 with pytest .raises (ValueError ):
3357 SystemEquation (foo )
3458
3559
36- def test_residual ():
60+ @pytest .mark .parametrize ("reduction" , [None , "mean" , "sum" ])
61+ def test_residual (reduction ):
3762
63+ # Generate random points and output
3864 pts = LabelTensor (torch .rand (10 , 2 ), labels = ["x" , "y" ])
3965 pts .requires_grad = True
4066 u = torch .pow (pts , 2 )
4167 u .labels = ["u1" , "u2" ]
4268
43- eq_1 = SystemEquation ([eq1 , eq2 ], reduction = "mean" )
44- res = eq_1 .residual (pts , u )
45- 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 )
72+
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
4676
47- eq_1 = SystemEquation ([eq1 , eq2 ], reduction = "sum" )
48- res = eq_1 .residual (pts , u )
49- assert res .shape == torch .Size ([10 ])
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+ )
5085
51- eq_1 = SystemEquation ([eq1 , eq2 ], reduction = None )
52- res = eq_1 .residual (pts , u )
53- 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
89+
90+ # System with mixed types
91+ system_eq = SystemEquation (
92+ [
93+ FixedValue (value = 0.0 , components = ["u1" ]),
94+ eq1 ,
95+ ],
96+ reduction = reduction ,
97+ )
5498
55- eq_1 = SystemEquation ([ eq1 , eq2 ])
56- res = eq_1 . residual ( pts , u )
57- assert res .shape == torch . Size ([ 10 , 3 ])
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