Skip to content

Commit 9985e15

Browse files
add tests on solutions and renamed by software standards
1 parent fe1f930 commit 9985e15

9 files changed

Lines changed: 117 additions & 31 deletions

tests/test_problem_zoo/test_acoustic_wave.py renamed to tests/test_problem_zoo/test_acoustic_wave_problem.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import torch
23
from pina.problem.zoo import AcousticWaveProblem
34
from pina.problem import SpatialProblem, TimeDependentProblem
45

@@ -17,3 +18,19 @@ def test_constructor(c):
1718
# Should fail if c is not a float or int
1819
with pytest.raises(ValueError):
1920
AcousticWaveProblem(c="invalid")
21+
22+
23+
@pytest.mark.parametrize("c", [0.1, 1])
24+
def test_solution(c):
25+
26+
# Find the solution to the problem
27+
problem = AcousticWaveProblem(c=c)
28+
problem.discretise_domain(n=10, mode="grid", domains=None)
29+
pts = problem.discretised_domains["D"]
30+
solution = problem.solution(pts.requires_grad_())
31+
32+
# Compute the residual
33+
residual = problem.conditions["D"].equation.residual(pts, solution).tensor
34+
35+
# Assert the residual of the PDE is close to zero
36+
assert torch.allclose(residual, torch.zeros_like(residual), atol=5e-5)

tests/test_problem_zoo/test_advection.py renamed to tests/test_problem_zoo/test_advection_problem.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import torch
23
from pina.problem.zoo import AdvectionProblem
34
from pina.problem import SpatialProblem, TimeDependentProblem
45

@@ -17,3 +18,19 @@ def test_constructor(c):
1718
# Should fail if c is not a float or int
1819
with pytest.raises(ValueError):
1920
AdvectionProblem(c="invalid")
21+
22+
23+
@pytest.mark.parametrize("c", [1.5, 3])
24+
def test_solution(c):
25+
26+
# Find the solution to the problem
27+
problem = AdvectionProblem(c=c)
28+
problem.discretise_domain(n=10, mode="grid", domains=None)
29+
pts = problem.discretised_domains["D"]
30+
solution = problem.solution(pts.requires_grad_())
31+
32+
# Compute the residual
33+
residual = problem.conditions["D"].equation.residual(pts, solution).tensor
34+
35+
# Assert the residual of the PDE is close to zero
36+
assert torch.allclose(residual, torch.zeros_like(residual), atol=5e-5)
File renamed without changes.

tests/test_problem_zoo/test_diffusion_reaction.py renamed to tests/test_problem_zoo/test_diffusion_reaction_problem.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import torch
23
from pina.problem.zoo import DiffusionReactionProblem
34
from pina.problem import TimeDependentProblem, SpatialProblem
45

@@ -17,3 +18,19 @@ def test_constructor(alpha):
1718
# Should fail if alpha is not a float or int
1819
with pytest.raises(ValueError):
1920
problem = DiffusionReactionProblem(alpha="invalid")
21+
22+
23+
@pytest.mark.parametrize("alpha", [0.1, 1])
24+
def test_solution(alpha):
25+
26+
# Find the solution to the problem
27+
problem = DiffusionReactionProblem(alpha=alpha)
28+
problem.discretise_domain(n=10, mode="grid", domains=None)
29+
pts = problem.discretised_domains["D"]
30+
solution = problem.solution(pts.requires_grad_())
31+
32+
# Compute the residual
33+
residual = problem.conditions["D"].equation.residual(pts, solution).tensor
34+
35+
# Assert the residual of the PDE is close to zero
36+
assert torch.allclose(residual, torch.zeros_like(residual), atol=5e-5)

tests/test_problem_zoo/test_helmholtz.py

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
import torch
3+
from pina.problem.zoo import HelmholtzProblem
4+
from pina.problem import SpatialProblem
5+
6+
7+
@pytest.mark.parametrize("k", [1.5, 3])
8+
@pytest.mark.parametrize("alpha_x", [1, 3])
9+
@pytest.mark.parametrize("alpha_y", [1, 3])
10+
def test_constructor(k, alpha_x, alpha_y):
11+
12+
problem = HelmholtzProblem(k=k, alpha_x=alpha_x, alpha_y=alpha_y)
13+
problem.discretise_domain(n=10, mode="random", domains=None)
14+
assert problem.are_all_domains_discretised
15+
assert isinstance(problem, SpatialProblem)
16+
assert hasattr(problem, "conditions")
17+
assert isinstance(problem.conditions, dict)
18+
19+
with pytest.raises(ValueError):
20+
HelmholtzProblem(k=1, alpha_x=1.5, alpha_y=1)
21+
22+
23+
@pytest.mark.parametrize("k", [1.5, 3])
24+
@pytest.mark.parametrize("alpha_x", [1, 3])
25+
@pytest.mark.parametrize("alpha_y", [1, 3])
26+
def test_solution(k, alpha_x, alpha_y):
27+
28+
# Find the solution to the problem
29+
problem = HelmholtzProblem(k=k, alpha_x=alpha_x, alpha_y=alpha_y)
30+
problem.discretise_domain(n=10, mode="grid", domains=None)
31+
pts = problem.discretised_domains["D"]
32+
solution = problem.solution(pts.requires_grad_())
33+
34+
# Compute the residual
35+
residual = problem.conditions["D"].equation.residual(pts, solution).tensor
36+
37+
# Assert the residual of the PDE is close to zero
38+
assert torch.allclose(residual, torch.zeros_like(residual), atol=5e-5)

tests/test_problem_zoo/test_inverse_poisson_2d_square.py renamed to tests/test_problem_zoo/test_inverse_poisson_problem.py

File renamed without changes.

tests/test_problem_zoo/test_poisson_2d_square.py

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import torch
2+
from pina.problem.zoo import Poisson2DSquareProblem
3+
from pina.problem import SpatialProblem
4+
5+
6+
def test_constructor():
7+
8+
problem = Poisson2DSquareProblem()
9+
problem.discretise_domain(n=10, mode="random", domains=None)
10+
assert problem.are_all_domains_discretised
11+
assert isinstance(problem, SpatialProblem)
12+
assert hasattr(problem, "conditions")
13+
assert isinstance(problem.conditions, dict)
14+
15+
16+
def test_solution():
17+
18+
# Find the solution to the problem
19+
problem = Poisson2DSquareProblem()
20+
problem.discretise_domain(n=10, mode="grid", domains=None)
21+
pts = problem.discretised_domains["D"]
22+
solution = problem.solution(pts.requires_grad_())
23+
24+
# Compute the residual
25+
residual = problem.conditions["D"].equation.residual(pts, solution).tensor
26+
27+
# Assert the residual of the PDE is close to zero
28+
assert torch.allclose(residual, torch.zeros_like(residual), atol=5e-5)

0 commit comments

Comments
 (0)