diff --git a/bluemira/magnetostatics/polyhedral_prism.py b/bluemira/magnetostatics/polyhedral_prism.py index ca0e260d81..c1fe16ed41 100644 --- a/bluemira/magnetostatics/polyhedral_prism.py +++ b/bluemira/magnetostatics/polyhedral_prism.py @@ -420,13 +420,13 @@ def __init__( alpha: float, beta: float, current: float, + *, bypass_endcap_error: bool | None = False, endcap_warning: bool | None = True, ): alpha, beta = np.deg2rad(alpha), np.deg2rad(beta) self._origin = origin - if endcap_warning is False: - self._warning = False + self._warning = False length = np.linalg.norm(ds) self._halflength = 0.5 * length self._check_angle_values(alpha, beta, bypass_endcap_error, endcap_warning) @@ -461,14 +461,6 @@ def _check_angle_values(self, alpha, beta, bypass_endcap_error, endcap_warning): Check that end-cap angles are acceptable. """ if bypass_endcap_error is True: - if (endcap_warning is True) and (not np.isclose(alpha, beta)): - bluemira_warn( - "Unequal end cap angles will result in result not being precise." - " This inaccuracy will increase as the end cap angle" - " discrepency increases." - ) - elif (endcap_warning is False) and (not np.isclose(alpha, beta)): - self._warning = True if not (0 <= abs(alpha) < 0.5 * np.pi): raise MagnetostaticsError( f"{self.__class__.__name__} instantiation error: {alpha=:.3f}" @@ -479,6 +471,14 @@ def _check_angle_values(self, alpha, beta, bypass_endcap_error, endcap_warning): f"{self.__class__.__name__} instantiation error: {beta=:.3f}" " is outside bounds of [0, 180°)." ) + if (endcap_warning is True) and (not np.isclose(alpha, beta)): + bluemira_warn( + "Unequal end cap angles will result in result not being precise." + " This inaccuracy will increase as the end cap angle" + " discrepency increases." + ) + elif (endcap_warning is False) and (not np.isclose(alpha, beta)): + self._warning = True else: if not np.isclose(alpha, beta): raise MagnetostaticsError( diff --git a/tests/magnetostatics/test_circuits.py b/tests/magnetostatics/test_circuits.py index 378983ccfd..05595e2248 100644 --- a/tests/magnetostatics/test_circuits.py +++ b/tests/magnetostatics/test_circuits.py @@ -441,6 +441,14 @@ def test_continuity(self, point): np.testing.assert_allclose(Bz, Bz_coil, rtol=1e-3) +class TestPolyhedralSourceGeneration: + def test_polyhedralprism_endcapwarning(self, caplog): + shape = PrincetonD().create_shape() + xs = Coordinates({"x": [-1, 1, 1, -1], "z": [-1, -1, 1, 1]}) + circuit = ArbitraryPlanarPolyhedralXSCircuit(shape.discretize(30), xs, current=1) + assert len(caplog.records) == 1 + + class TestArbitraryPlanarPolyhedralPFCoil: coordinates = make_circle(10).discretise(31) xs = Coordinates({"x": [-1, 1, 1, -1], "z": [-1, -1, 1, 1]}) diff --git a/tests/magnetostatics/test_polyhedral_prism.py b/tests/magnetostatics/test_polyhedral_prism.py index 698ed4d86a..b7d63e3916 100644 --- a/tests/magnetostatics/test_polyhedral_prism.py +++ b/tests/magnetostatics/test_polyhedral_prism.py @@ -33,6 +33,49 @@ def test_diff_angle_error(self): current=1, ) + def test_diff_angle_warning(self, caplog): + PolyhedralPrismCurrentSource( + [10, 0, 0], + [1, 0, 0], + [0, 1, 0], + [0, 0, 1], + make_xs_from_bd(0.5, 0.5), + 40, + 35, + current=1, + bypass_endcap_error=True, + ) + assert len(caplog.records) == 1 + + @pytest.mark.parametrize("angle", [54, 45.0001]) + def test_angle_bounds(self, angle): + with pytest.raises(MagnetostaticsError): + PolyhedralPrismCurrentSource( + [10, 0, 0], + [1, 0, 0], + [0, 1, 0], + [0, 0, 1], + make_xs_from_bd(0.5, 0.5), + angle, + angle, + current=1, + ) + + @pytest.mark.parametrize(("angle1", "angle2"), [(10, 100), (100, 20)]) + def test_angle_bounds_bypass(self, angle1, angle2): + with pytest.raises(MagnetostaticsError): + PolyhedralPrismCurrentSource( + [10, 0, 0], + [1, 0, 0], + [0, 1, 0], + [0, 0, 1], + make_xs_from_bd(0.5, 0.5), + angle1, + angle2, + current=1, + bypass_endcap_error=True, + ) + class TestPolyhedralMaths: same_angle_1 = (