Introduce add_points method in AbstractProblem, removed unused comments in Collector class and add the test for add_points and codacy corrections
This commit is contained in:
committed by
Nicola Demo
parent
004cbc00c0
commit
f578b2ed12
@@ -10,6 +10,7 @@ from pina.equation.equation_factory import FixedValue
|
||||
from pina.operators import laplacian
|
||||
from pina.collector import Collector
|
||||
|
||||
|
||||
# def test_supervised_tensor_collector():
|
||||
# class SupervisedProblem(AbstractProblem):
|
||||
# output_variables = None
|
||||
@@ -37,6 +38,7 @@ def test_pinn_collector():
|
||||
my_laplace = Equation(laplace_equation)
|
||||
in_ = LabelTensor(torch.tensor([[0., 1.]], requires_grad=True), ['x', 'y'])
|
||||
out_ = LabelTensor(torch.tensor([[0.]], requires_grad=True), ['u'])
|
||||
|
||||
class Poisson(SpatialProblem):
|
||||
output_variables = ['u']
|
||||
spatial_domain = CartesianDomain({'x': [0, 1], 'y': [0, 1]})
|
||||
@@ -78,7 +80,8 @@ def test_pinn_collector():
|
||||
|
||||
def poisson_sol(self, pts):
|
||||
return -(torch.sin(pts.extract(['x']) * torch.pi) *
|
||||
torch.sin(pts.extract(['y']) * torch.pi)) / (2 * torch.pi**2)
|
||||
torch.sin(pts.extract(['y']) * torch.pi)) / (
|
||||
2 * torch.pi ** 2)
|
||||
|
||||
truth_solution = poisson_sol
|
||||
|
||||
@@ -91,30 +94,34 @@ def test_pinn_collector():
|
||||
collector.store_fixed_data()
|
||||
collector.store_sample_domains()
|
||||
|
||||
for k,v in problem.conditions.items():
|
||||
for k, v in problem.conditions.items():
|
||||
if isinstance(v, InputOutputPointsCondition):
|
||||
assert list(collector.data_collections[k].keys()) == ['input_points', 'output_points']
|
||||
assert list(collector.data_collections[k].keys()) == [
|
||||
'input_points', 'output_points']
|
||||
|
||||
for k,v in problem.conditions.items():
|
||||
for k, v in problem.conditions.items():
|
||||
if isinstance(v, DomainEquationCondition):
|
||||
assert list(collector.data_collections[k].keys()) == ['input_points', 'equation']
|
||||
assert list(collector.data_collections[k].keys()) == [
|
||||
'input_points', 'equation']
|
||||
|
||||
|
||||
def test_supervised_graph_collector():
|
||||
pos = torch.rand((100,3))
|
||||
x = [torch.rand((100,3)) for _ in range(10)]
|
||||
pos = torch.rand((100, 3))
|
||||
x = [torch.rand((100, 3)) for _ in range(10)]
|
||||
graph_list_1 = RadiusGraph(pos=pos, x=x, build_edge_attr=True, r=.4)
|
||||
out_1 = torch.rand((10,100,3))
|
||||
pos = torch.rand((50,3))
|
||||
x = [torch.rand((50,3)) for _ in range(10)]
|
||||
out_1 = torch.rand((10, 100, 3))
|
||||
pos = torch.rand((50, 3))
|
||||
x = [torch.rand((50, 3)) for _ in range(10)]
|
||||
graph_list_2 = RadiusGraph(pos=pos, x=x, build_edge_attr=True, r=.4)
|
||||
out_2 = torch.rand((10,50,3))
|
||||
out_2 = torch.rand((10, 50, 3))
|
||||
|
||||
class SupervisedProblem(AbstractProblem):
|
||||
output_variables = None
|
||||
conditions = {
|
||||
'data1' : Condition(input_points=graph_list_1,
|
||||
output_points=out_1),
|
||||
'data2' : Condition(input_points=graph_list_2,
|
||||
output_points=out_2),
|
||||
'data1': Condition(input_points=graph_list_1,
|
||||
output_points=out_1),
|
||||
'data2': Condition(input_points=graph_list_2,
|
||||
output_points=out_2),
|
||||
}
|
||||
|
||||
problem = SupervisedProblem()
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import torch
|
||||
import pytest
|
||||
from pina.problem.zoo import Poisson2DSquareProblem as Poisson
|
||||
from pina import LabelTensor
|
||||
|
||||
|
||||
def test_discretise_domain():
|
||||
n = 10
|
||||
@@ -14,7 +16,7 @@ def test_discretise_domain():
|
||||
assert poisson_problem.discretised_domains[b].shape[0] == n
|
||||
|
||||
poisson_problem.discretise_domain(n, 'grid', domains=['D'])
|
||||
assert poisson_problem.discretised_domains['D'].shape[0] == n**2
|
||||
assert poisson_problem.discretised_domains['D'].shape[0] == n ** 2
|
||||
poisson_problem.discretise_domain(n, 'random', domains=['D'])
|
||||
assert poisson_problem.discretised_domains['D'].shape[0] == n
|
||||
|
||||
@@ -25,6 +27,8 @@ def test_discretise_domain():
|
||||
assert poisson_problem.discretised_domains['D'].shape[0] == n
|
||||
|
||||
poisson_problem.discretise_domain(n)
|
||||
|
||||
|
||||
'''
|
||||
def test_sampling_few_variables():
|
||||
n = 10
|
||||
@@ -36,8 +40,8 @@ def test_sampling_few_variables():
|
||||
assert poisson_problem.discretised_domains['D'].shape[1] == 1
|
||||
'''
|
||||
|
||||
def test_variables_correct_order_sampling():
|
||||
|
||||
def test_variables_correct_order_sampling():
|
||||
n = 10
|
||||
poisson_problem = Poisson()
|
||||
poisson_problem.discretise_domain(n,
|
||||
@@ -50,15 +54,15 @@ def test_variables_correct_order_sampling():
|
||||
assert poisson_problem.discretised_domains['D'].labels == sorted(
|
||||
poisson_problem.input_variables)
|
||||
|
||||
# def test_add_points():
|
||||
# poisson_problem = Poisson()
|
||||
# poisson_problem.discretise_domain(0,
|
||||
# 'random',
|
||||
# domains=['D'],
|
||||
# variables=['x', 'y'])
|
||||
# new_pts = LabelTensor(torch.tensor([[0.5, -0.5]]), labels=['x', 'y'])
|
||||
# poisson_problem.add_points({'D': new_pts})
|
||||
# assert torch.isclose(poisson_problem.discretised_domain['D'].extract('x'),
|
||||
# new_pts.extract('x'))
|
||||
# assert torch.isclose(poisson_problem.discretised_domain['D'].extract('y'),
|
||||
# new_pts.extract('y'))
|
||||
|
||||
def test_add_points():
|
||||
poisson_problem = Poisson()
|
||||
poisson_problem.discretise_domain(0,
|
||||
'random',
|
||||
domains=['D'])
|
||||
new_pts = LabelTensor(torch.tensor([[0.5, -0.5]]), labels=['x', 'y'])
|
||||
poisson_problem.add_points({'D': new_pts})
|
||||
assert torch.isclose(poisson_problem.discretised_domains['D'].extract('x'),
|
||||
new_pts.extract('x'))
|
||||
assert torch.isclose(poisson_problem.discretised_domains['D'].extract('y'),
|
||||
new_pts.extract('y'))
|
||||
|
||||
Reference in New Issue
Block a user