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/test_model_sol.py
2021-08-30 16:08:33 -05:00

77 lines
3.0 KiB
Python

import numpy
import pathfinder.model
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 numpy.sort(chunked_pts, axis=0)
def print_result(msg, result):
print(msg)
print(f"\tResult: {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.StaticDipole([1, 2, 3], [0, 4, 0])
expected_result = numpy.array([1, 2, 3, 0, 4, 0])
dipole_arrangement = pathfinder.model.StaticDipoleArrangement([dipole])
dot_measurements = dipole_arrangement.get_dot_measurements(dot_positions)
model = pathfinder.model.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)
def test_two_dipole_twelve_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.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()
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)
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.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()
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)