Adds some tests and lets skip root finding method

This commit is contained in:
Deepak Mallubhotla 2021-09-06 10:02:05 -05:00
parent 6af5d6f78c
commit b22a977b9c
Signed by: deepak
GPG Key ID: 64BF53A3369104E7
2 changed files with 52 additions and 8 deletions

View File

@ -36,9 +36,9 @@ class DotDipoleModel():
return jac_to_return return jac_to_return
def sol(self, initial_dipole=(0.1, 0.1, 0.1), initial_position=(.1, .1, .1)): def sol(self, initial_dipole=(0.1, 0.1, 0.1), initial_position=(.1, .1, .1), use_root=True):
initial = numpy.tile(numpy.append(initial_dipole, initial_position), self.n) initial = numpy.tile(numpy.append(initial_dipole, initial_position), self.n)
if self.m == 6 * self.n: if use_root and self.m == 6 * self.n:
# We are perfectly specified # We are perfectly specified
return scipy.optimize.root(self.costs(), initial, jac=self.jac(), tol=1e-12) return scipy.optimize.root(self.costs(), initial, jac=self.jac(), tol=1e-12)
return scipy.optimize.least_squares(self.costs(), initial, jac=self.jac(), ftol=1e-15, gtol=3e-16) return scipy.optimize.least_squares(self.costs(), initial, jac=self.jac(), ftol=1e-15, gtol=3e-16)

View File

@ -1,16 +1,19 @@
import numpy import numpy
import pathfinder.model import pathfinder.model
import pytest
def chunk_n_sort(pts): def chunk_n_sort(pts):
pt_length = 6 pt_length = 6
chunked_pts = [pts[i: i + pt_length] for i in range(0, len(pts), pt_length)] chunked_pts = [pts[i: i + pt_length] for i in range(0, len(pts), pt_length)]
return numpy.sort(chunked_pts, axis=0)
return sorted(chunked_pts, key=lambda x: x[0])
def print_result(msg, result): def print_result(msg, result):
print(msg) print(msg)
print(f"\tResult: {result.x}") print(f"\tResult: {chunk_n_sort(result.x)}")
print(f"\tSuccess: {result.success}. {result.message}") print(f"\tSuccess: {result.success}. {result.message}")
try: try:
print(f"\tFunc evals: {result.nfev}") print(f"\tFunc evals: {result.nfev}")
@ -38,10 +41,11 @@ def test_one_dipole_six_dot():
numpy.testing.assert_allclose(res.x, expected_result, err_msg="Dipole wasn't as expected.", rtol=1e-6, atol=1e-6) numpy.testing.assert_allclose(res.x, expected_result, err_msg="Dipole wasn't as expected.", rtol=1e-6, atol=1e-6)
def test_two_dipole_twelve_dot(): @pytest.mark.skip(reason="don't care at the moment")
def test_two_dipole_thirteen_dot():
dot_positions = [[x, y, 0] for x in (-4, -1, 1, 4) for y in (-1, -5, -9)] dot_positions = [[x, y, 0] for x in (-4, -1, 1, 4) for y in (-1, -5, -9)]
dot_positions.append((1, -1, 2)) dot_positions.append((1, -1, 2))
dot_positions.append((-1, -1, -2)) # dot_positions.append((-1, -1, -2))
dipole = pathfinder.model.StaticDipole([0, 8, 0], [0, 5, 0]) dipole = pathfinder.model.StaticDipole([0, 8, 0], [0, 5, 0])
dipole2 = pathfinder.model.StaticDipole([8, 1, 1], [2, 2, -2]) dipole2 = pathfinder.model.StaticDipole([8, 1, 1], [2, 2, -2])
expected_result = numpy.array([0, 8, 0, 0, 5, 0, 8, 1, 1, 2, 2, -2]) expected_result = numpy.array([0, 8, 0, 0, 5, 0, 8, 1, 1, 2, 2, -2])
@ -49,10 +53,50 @@ def test_two_dipole_twelve_dot():
dot_measurements = dipole_arrangement.get_dot_measurements(dot_positions) dot_measurements = dipole_arrangement.get_dot_measurements(dot_positions)
model = pathfinder.model.DotDipoleModel(dot_measurements, 2) model = pathfinder.model.DotDipoleModel(dot_measurements, 2)
res = model.sol() res = model.sol(initial_dipole=(2, 2, 0), initial_position=(0, 3, 0))
print(model.costs()(res.x)) print(model.costs()(res.x))
print(res.x)
print_result("two dipole 13 dot", res)
assert res.success, "The solution for two dipoles and twelve dots should have succeeded."
numpy.testing.assert_allclose(chunk_n_sort(res.x), chunk_n_sort(expected_result), err_msg="Dipole wasn't as expected.", rtol=1e-6, atol=1e-4)
print_result("two dipole twelve dot", res)
@pytest.mark.skip(reason="don't care at the moment")
def test_two_dipole_twelve_dot():
dot_positions = [[x, y, 0] for x in (-4, -1, 1, 4) for y in (-1, -5, -9)][:-1]
dot_positions.append((1, -1, 2))
# dot_positions.append((-1, -1, -2))
dipole = pathfinder.model.StaticDipole([0, 8, 0], [0, 5, 0])
dipole2 = pathfinder.model.StaticDipole([8, 1, 1], [2, 2, -2])
expected_result = numpy.array([0, 8, 0, 0, 5, 0, 8, 1, 1, 2, 2, -2])
dipole_arrangement = pathfinder.model.StaticDipoleArrangement([dipole, dipole2])
dot_measurements = dipole_arrangement.get_dot_measurements(dot_positions)
model = pathfinder.model.DotDipoleModel(dot_measurements, 2)
res = model.sol(initial_dipole=(2, 2, 0), initial_position=(0, 3, 0), use_root=False)
print(model.costs()(res.x))
print(res.x)
print_result("two dipole 13 dot", res)
assert res.success, "The solution for two dipoles and twelve dots should have succeeded."
numpy.testing.assert_allclose(chunk_n_sort(res.x), chunk_n_sort(expected_result), err_msg="Dipole wasn't as expected.", rtol=1e-6, atol=1e-4)
@pytest.mark.skip(reason="don't care at the moment")
def test_two_dipole_twelve_dot_smallguy():
dot_positions = [[x, y, 0] for x in (-4, -1, 1, 4) for y in (-1, -5, -9)][:-1]
dot_positions.append((1, -1, 2))
# dot_positions.append((-1, -1, -2))
dipole = pathfinder.model.StaticDipole([0, 1, 0], [0, 5, 0])
dipole2 = pathfinder.model.StaticDipole([8, 1, 1], [2, 2, -2])
expected_result = numpy.array([0, 1, 0, 0, 5, 0, 8, 1, 1, 2, 2, -2])
dipole_arrangement = pathfinder.model.StaticDipoleArrangement([dipole, dipole2])
dot_measurements = dipole_arrangement.get_dot_measurements(dot_positions)
model = pathfinder.model.DotDipoleModel(dot_measurements, 2)
res = model.sol(initial_dipole=(2, 2, 0), initial_position=(0, 3, 0), use_root=False)
print(model.costs()(res.x))
print(res.x)
print_result("two dipole 13 dot", res)
assert res.success, "The solution for two dipoles and twelve dots should have succeeded." assert res.success, "The solution for two dipoles and twelve dots should have succeeded."
numpy.testing.assert_allclose(chunk_n_sort(res.x), chunk_n_sort(expected_result), err_msg="Dipole wasn't as expected.", rtol=1e-6, atol=1e-4) numpy.testing.assert_allclose(chunk_n_sort(res.x), chunk_n_sort(expected_result), err_msg="Dipole wasn't as expected.", rtol=1e-6, atol=1e-4)