1
- from pina .equation import SystemEquation
1
+ from pina .equation import SystemEquation , FixedValue , FixedGradient
2
2
from pina .operator import grad , laplacian
3
3
from pina import LabelTensor
4
4
import torch
@@ -24,34 +24,78 @@ def foo():
24
24
pass
25
25
26
26
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
30
52
with pytest .raises (NotImplementedError ):
31
53
SystemEquation ([eq1 , eq2 ], reduction = "foo" )
54
+
55
+ # Invalid input type
32
56
with pytest .raises (ValueError ):
33
57
SystemEquation (foo )
34
58
35
59
36
- def test_residual ():
60
+ @pytest .mark .parametrize ("reduction" , [None , "mean" , "sum" ])
61
+ def test_residual (reduction ):
37
62
63
+ # Generate random points and output
38
64
pts = LabelTensor (torch .rand (10 , 2 ), labels = ["x" , "y" ])
39
65
pts .requires_grad = True
40
66
u = torch .pow (pts , 2 )
41
67
u .labels = ["u1" , "u2" ]
42
68
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
46
76
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
+ )
50
85
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
+ )
54
98
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