This repository has been archived on 2022-02-23. You can view files and clone it, but cannot push or open issues or pull requests.
pathfinder/tests/model/static/test_model_sol.py
Deepak ca8269b538
All checks were successful
gitea-physics/pathfinder/pipeline/head This commit looks good
Moves static model to its own module
2021-09-06 11:46:11 -05:00

121 lines
5.4 KiB
Python

import numpy
import pathfinder.model.static
import pytest
def chunk_n_sort(pts):
pt_length = 6
chunked_pts = [pts[i: i + pt_length] for i in range(0, len(pts), pt_length)]
return sorted(chunked_pts, key=lambda x: x[0])
def print_result(msg, result):
print(msg)
print(f"\tResult: {chunk_n_sort(result.x)}")
print(f"\tSuccess: {result.success}. {result.message}")
try:
print(f"\tFunc evals: {result.nfev}")
except AttributeError:
pass
try:
print(f"\tJacb evals: {result.njev}")
except AttributeError:
pass
def test_one_dipole_six_dot():
# setup
dot_positions = [[0, 0, 0], [-1, 0, 0], [-2, 0, 0], [-1, -1, 0], [-1, -1, 1], [0, -2, 0]]
dipole = pathfinder.model.static.StaticDipole([1, 2, 3], [0, 4, 0])
expected_result = numpy.array([1, 2, 3, 0, 4, 0])
dipole_arrangement = pathfinder.model.static.StaticDipoleArrangement([dipole])
dot_measurements = dipole_arrangement.get_dot_measurements(dot_positions)
model = pathfinder.model.static.DotDipoleModel(dot_measurements, 1)
res = model.sol()
print_result("one dipole six dots", res)
assert res.success, "The solution for a single dipole and six dots should have succeeded."
numpy.testing.assert_allclose(res.x, expected_result, err_msg="Dipole wasn't as expected.", rtol=1e-6, atol=1e-6)
@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.append((1, -1, 2))
# dot_positions.append((-1, -1, -2))
dipole = pathfinder.model.static.StaticDipole([0, 8, 0], [0, 5, 0])
dipole2 = pathfinder.model.static.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.static.StaticDipoleArrangement([dipole, dipole2])
dot_measurements = dipole_arrangement.get_dot_measurements(dot_positions)
model = pathfinder.model.static.DotDipoleModel(dot_measurements, 2)
res = model.sol(initial_dipole=(2, 2, 0), initial_position=(0, 3, 0))
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():
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.static.StaticDipole([0, 8, 0], [0, 5, 0])
dipole2 = pathfinder.model.static.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.static.StaticDipoleArrangement([dipole, dipole2])
dot_measurements = dipole_arrangement.get_dot_measurements(dot_positions)
model = pathfinder.model.static.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.static.StaticDipole([0, 1, 0], [0, 5, 0])
dipole2 = pathfinder.model.static.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.static.StaticDipoleArrangement([dipole, dipole2])
dot_measurements = dipole_arrangement.get_dot_measurements(dot_positions)
model = pathfinder.model.static.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)
def test_two_dipole_twelve_dot_2():
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))
dipole = pathfinder.model.static.StaticDipole([0, 8, 0], [0, 5, 0])
dipole2 = pathfinder.model.static.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.static.StaticDipoleArrangement([dipole, dipole2])
dot_measurements = dipole_arrangement.get_dot_measurements(dot_positions)
model = pathfinder.model.static.DotDipoleModel(dot_measurements, 2)
res = model.sol()
print(model.costs()(res.x))
print_result("two dipole twelve 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)