Skip to content

Commit c5ea4b8

Browse files
add checks on Fixed equations
1 parent 5cba39e commit c5ea4b8

9 files changed

Lines changed: 126 additions & 31 deletions

File tree

pina/_src/equation/zoo/fixed_flux.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from pina._src.equation.equation import Equation
44
from pina._src.core.operator import div
5+
from pina._src.core.utils import check_consistency
56

67

78
class FixedFlux(Equation):
@@ -13,15 +14,29 @@ def __init__(self, value, components=None, d=None):
1314
"""
1415
Initialization of the :class:`FixedFlux` class.
1516
16-
:param float value: The fixed value to be enforced to the flux.
17-
:param list[str] components: The name of the output variables for which
18-
the fixed flux condition is applied. It should be a subset of the
19-
output labels. If ``None``, all output variables are considered.
20-
Default is ``None``.
21-
:param list[str] d: The name of the input variables on which the flux
22-
is computed. It should be a subset of the input labels. If ``None``,
23-
all the input variables are considered. Default is ``None``.
17+
:param value: The fixed value to be enforced to the flux.
18+
:type value: float | int
19+
:param components: The name of the output variables for which the fixed
20+
flux condition is applied. It should be a subset of the output
21+
labels. If ``None``, all output variables are considered. Default is
22+
``None``.
23+
:type components: str | list[str]
24+
:param d: The name of the input variables on which the flux is computed.
25+
It should be a subset of the input labels. If ``None``, all the
26+
input variables are considered. Default is ``None``.
27+
:type d: str | list[str]
28+
:raises ValueError: If ``value`` is neither a float nor an integer.
29+
:raises ValueError: If, when provided, ``components`` is neither a
30+
string nor a list of strings.
31+
:raises ValueError: If, when provided, ``d`` is neither a string nor a
32+
list of strings.
2433
"""
34+
# Check consistency
35+
check_consistency(value, (float, int))
36+
if components is not None:
37+
check_consistency(components, str)
38+
if d is not None:
39+
check_consistency(d, str)
2540

2641
def equation(input_, output_):
2742
"""

pina/_src/equation/zoo/fixed_gradient.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from pina._src.equation.equation import Equation
44
from pina._src.core.operator import grad
5+
from pina._src.core.utils import check_consistency
56

67

78
class FixedGradient(Equation):
@@ -14,15 +15,27 @@ def __init__(self, value, components=None, d=None):
1415
Initialization of the :class:`FixedGradient` class.
1516
1617
:param float value: The fixed value to be enforced to the gradient.
17-
:param list[str] components: The name of the output variables for which
18-
the fixed gradient condition is applied. It should be a subset of
19-
the output labels. If ``None``, all output variables are considered.
20-
Default is ``None``.
21-
:param list[str] d: The name of the input variables on which the
22-
gradient is computed. It should be a subset of the input labels.
23-
If ``None``, all the input variables are considered.
24-
Default is ``None``.
18+
:param components: The name of the output variables for which the fixed
19+
gradient condition is applied. It should be a subset of the output
20+
labels. If ``None``, all output variables are considered. Default is
21+
``None``.
22+
:type components: str | list[str]
23+
:param d: The name of the input variables on which the gradient is
24+
computed. It should be a subset of the input labels. If ``None``,
25+
all the input variables are considered. Default is ``None``.
26+
:type d: str | list[str]
27+
:raises ValueError: If ``value`` is neither a float nor an integer.
28+
:raises ValueError: If, when provided, ``components`` is neither a
29+
string nor a list of strings.
30+
:raises ValueError: If, when provided, ``d`` is neither a string nor a
31+
list of strings.
2532
"""
33+
# Check consistency
34+
check_consistency(value, (float, int))
35+
if components is not None:
36+
check_consistency(components, str)
37+
if d is not None:
38+
check_consistency(d, str)
2639

2740
def equation(input_, output_):
2841
"""

pina/_src/equation/zoo/fixed_laplacian.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import warnings
44
from pina._src.equation.equation import Equation
55
from pina._src.core.operator import laplacian
6+
from pina._src.core.utils import check_consistency
67

78

89
class FixedLaplacian(Equation):
@@ -14,16 +15,29 @@ def __init__(self, value, components=None, d=None):
1415
"""
1516
Initialization of the :class:`FixedLaplacian` class.
1617
17-
:param float value: The fixed value to be enforced to the laplacian.
18-
:param list[str] components: The name of the output variables for which
19-
the fixed laplace condition is applied. It should be a subset of the
20-
output labels. If ``None``, all output variables are considered.
21-
Default is ``None``.
22-
:param list[str] d: The name of the input variables on which the
23-
laplacian is computed. It should be a subset of the input labels.
24-
If ``None``, all the input variables are considered.
25-
Default is ``None``.
18+
:param value: The fixed value to be enforced to the laplacian.
19+
:type value: float | int
20+
:param components: The name of the output variables for which the fixed
21+
laplace condition is applied. It should be a subset of the output
22+
labels. If ``None``, all output variables are considered. Default is
23+
``None``.
24+
:type components: str | list[str]
25+
:param d: The name of the input variables on which the laplacian is
26+
computed. It should be a subset of the input labels. If ``None``,
27+
all the input variables are considered. Default is ``None``.
28+
:type d: str | list[str]
29+
:raises ValueError: If ``value`` is neither a float nor an integer.
30+
:raises ValueError: If, when provided, ``components`` is neither a
31+
string nor a list of strings.
32+
:raises ValueError: If, when provided, ``d`` is neither a string nor a
33+
list of strings.
2634
"""
35+
# Check consistency
36+
check_consistency(value, (float, int))
37+
if components is not None:
38+
check_consistency(components, str)
39+
if d is not None:
40+
check_consistency(d, str)
2741

2842
def equation(input_, output_):
2943
"""

pina/_src/equation/zoo/fixed_value.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Module for defining the fixed value equation."""
22

33
from pina._src.equation.equation import Equation
4+
from pina._src.core.utils import check_consistency
45

56

67
class FixedValue(Equation):
@@ -13,12 +14,21 @@ def __init__(self, value, components=None):
1314
"""
1415
Initialization of the :class:`FixedValue` class.
1516
16-
:param float value: The fixed value to be enforced.
17-
:param list[str] components: The name of the output variables for which
18-
the fixed value condition is applied. It should be a subset of the
19-
output labels. If ``None``, all output variables are considered.
20-
Default is ``None``.
17+
:param value: The fixed value to be enforced.
18+
:type value: float | int
19+
:param components: The name of the output variables for which the fixed
20+
value condition is applied. It should be a subset of the output
21+
labels. If ``None``, all output variables are considered. Default is
22+
``None``.
23+
:type components: str | list[str]
24+
:raises ValueError: If ``value`` is neither a float nor an integer.
25+
:raises ValueError: If, when provided, ``components`` is neither a
26+
string nor a list of strings.
2127
"""
28+
# Check consistency
29+
check_consistency(value, (float, int))
30+
if components is not None:
31+
check_consistency(components, str)
2232

2333
def equation(_, output_):
2434
"""

pina/equation/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from pina._src.equation.equation import Equation
2121
from pina._src.equation.system_equation import SystemEquation
2222

23-
2423
# Back-compatibility with version 0.2, to be removed soon
2524
import warnings
2625
import importlib

tests/test_equation_zoo/test_fixed_flux.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,15 @@ def test_fixed_flux(value, components, d):
2626
# Residual
2727
residual = equation.residual(pts, u)
2828
assert residual.shape == (pts.shape[0], 1)
29+
30+
# Should fail if value is neither a float nor an integer
31+
with pytest.raises(ValueError):
32+
FixedFlux(value="invalid", components=components, d=d)
33+
34+
# Should fail if components is neither a string nor a list of strings
35+
with pytest.raises(ValueError):
36+
FixedFlux(value=value, components=123, d=d)
37+
38+
# Should fail if d is neither a string nor a list of strings
39+
with pytest.raises(ValueError):
40+
FixedFlux(value=value, components=components, d=123)

tests/test_equation_zoo/test_fixed_gradient.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,15 @@ def test_fixed_gradient(value, components, d):
2222
len_c = len(components) if components is not None else u.shape[1]
2323
len_d = len(d) if d is not None else pts.shape[1]
2424
assert residual.shape == (pts.shape[0], len_c * len_d)
25+
26+
# Should fail if value is neither a float nor an integer
27+
with pytest.raises(ValueError):
28+
FixedGradient(value="invalid", components=components, d=d)
29+
30+
# Should fail if components is neither a string nor a list of strings
31+
with pytest.raises(ValueError):
32+
FixedGradient(value=value, components=123, d=d)
33+
34+
# Should fail if d is neither a string nor a list of strings
35+
with pytest.raises(ValueError):
36+
FixedGradient(value=value, components=components, d=123)

tests/test_equation_zoo/test_fixed_laplacian.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ def test_fixed_laplacian(value, components, d):
2121
residual = equation.residual(pts, u)
2222
len_c = len(components) if components is not None else u.shape[1]
2323
assert residual.shape == (pts.shape[0], len_c)
24+
25+
# Should fail if value is neither a float nor an integer
26+
with pytest.raises(ValueError):
27+
FixedLaplacian(value="invalid", components=components, d=d)
28+
29+
# Should fail if components is neither a string nor a list of strings
30+
with pytest.raises(ValueError):
31+
FixedLaplacian(value=value, components=123, d=d)
32+
33+
# Should fail if d is neither a string nor a list of strings
34+
with pytest.raises(ValueError):
35+
FixedLaplacian(value=value, components=components, d=123)

tests/test_equation_zoo/test_fixed_value.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@ def test_fixed_value(value, components):
2020
residual = equation.residual(pts, u)
2121
len_c = len(components) if components is not None else u.shape[1]
2222
assert residual.shape == (pts.shape[0], len_c)
23+
24+
# Should fail if value is neither a float nor an integer
25+
with pytest.raises(ValueError):
26+
FixedValue(value="not a number", components=components)
27+
28+
# Should fail if components is neither a string nor a list of strings
29+
with pytest.raises(ValueError):
30+
FixedValue(value=value, components=123)

0 commit comments

Comments
 (0)