# -*- coding: utf-8 -*-
"""
@date Created on Thu Dec 18 13:56:33 2014
@copyright (C) 2014-2015 EOMYS ENGINEERING.
@author pierre_b
"""

from unittest import TestCase

from ddt import ddt, data

from pyleecan.Classes.Arc1 import Arc1
from pyleecan.Classes.Segment import Segment

from pyleecan.Classes.MagnetType11 import MagnetType11
from pyleecan.Classes.LamSlotMag import LamSlotMag

from pyleecan.Classes.SlotMPolar import SlotMPolar
from numpy import pi, exp, angle, array
from pyleecan.Methods.Machine.Magnet.comp_surface import comp_surface

Mag11_test = list()
# Internal Slot surface
lam = LamSlotMag(is_internal=True, Rext=0.5)
lam.slot = SlotMPolar(H0=0, W0=pi / 4, Zs=4)
lam.slot.magnet = [MagnetType11(Hmag=1, Wmag=pi / 4)]
Mag11_test.append({"test_obj": lam, "S_exp": 0.78539616, "Ao": pi / 4, "H_exp": 1})

# Internal Slot inset
lam = LamSlotMag(is_internal=True, Rext=0.5)
lam.slot = SlotMPolar(H0=40e-3, W0=pi / 4, Zs=4)
lam.slot.magnet = [MagnetType11(Hmag=20e-3, Wmag=pi / 4)]
Mag11_test.append({"test_obj": lam, "S_exp": 7.3827e-3, "Ao": pi / 4, "H_exp": 20e-3})

# Outward Slot inset
lam = LamSlotMag(is_internal=False, Rext=0.1325)
lam.slot = SlotMPolar(H0=5e-3, W0=pi / 10, Zs=8)
lam.slot.magnet = [MagnetType11(Hmag=8e-3, Wmag=pi / 12)]
Mag11_test.append({"test_obj": lam, "S_exp": 2.09439e-6, "Ao": pi / 12, "H_exp": 8e-3})

# For AlmostEqual
DELTA = 1e-4


@ddt
class test_Magnet_Type_11_meth(TestCase):
    """unittest for MagnetType11 methods
    """

    @data(*Mag11_test)
    def test_comp_surface(self, test_dict):
        """Check that the computation of the surface is correct
        """
        test_obj = test_dict["test_obj"]
        result = test_obj.slot.magnet[0].comp_surface()

        a = result
        b = test_dict["S_exp"]
        msg = "Return " + str(a) + " expected " + str(b)
        self.assertAlmostEqual((a - b) / a, 0, delta=DELTA, msg=msg)

        # Compare numerical and analytical results
        b = comp_surface(test_obj.slot.magnet[0])
        msg = "Analytical: " + str(a) + " Numerical " + str(b)
        self.assertAlmostEqual((a - b) / a, 0, delta=DELTA, msg=msg)

    @data(*Mag11_test)
    def test_comp_height(self, test_dict):
        """Check that the computation of the height is correct
        """
        test_obj = test_dict["test_obj"]
        result = test_obj.slot.magnet[0].comp_height()

        a = result
        b = test_dict["H_exp"]
        msg = "Return " + str(a) + " expected " + str(b)
        self.assertAlmostEqual((a - b) / a, 0, delta=DELTA, msg=msg)

    @data(*Mag11_test)
    def test_comp_angle_op(self, test_dict):
        """Check that the computation of the opening angle is correct
        """
        test_obj = test_dict["test_obj"]
        result = test_obj.slot.magnet[0].comp_angle_opening()

        a = result
        b = test_dict["Ao"]
        msg = "Return " + str(a) + " expected " + str(b)
        self.assertAlmostEqual((a - b) / a, 0, delta=DELTA, msg=msg)

    def test_build_geometry_out(self):
        """check that curve_list is correct (outwards magnet)"""
        lam = LamSlotMag(
            Rint=40e-3,
            Rext=90e-3,
            is_internal=False,
            is_stator=False,
            L1=0.45,
            Nrvd=1,
            Wrvd=0.05,
        )
        magnet = [MagnetType11(Wmag=pi / 10, Hmag=0.2)]
        lam.slot = SlotMPolar(Zs=8, W0=pi / 10, H0=0.2, magnet=magnet)
        test_obj = lam.slot.magnet[0]
        Z1 = (40e-3 + 0.2) * exp(-1j * pi / 10 / 2)
        Z2 = (40e-3 + 0.2) * exp(1j * pi / 10 / 2)

        Z = abs(Z1)

        Z3 = (Z - 0.2) * exp(1j * angle(Z1))
        Z4 = (Z - 0.2) * exp(1j * angle(Z2))

        # # Creation of curve
        curve_list = list()
        curve_list.append(Segment(Z1, Z3))
        curve_list.append(Arc1(Z3, Z4, abs(Z3)))
        curve_list.append(Segment(Z4, Z2))
        curve_list.append(Arc1(Z2, Z1, -abs(Z2)))

        surface = test_obj.build_geometry()
        result = surface[0].get_lines()
        for i in range(0, len(result)):
            a = result[i].begin
            b = curve_list[i].begin
            self.assertAlmostEqual((a - b) / a, 0, delta=DELTA)

            a = result[i].end
            b = curve_list[i].end
            self.assertAlmostEqual((a - b) / a, 0, delta=DELTA)

    def test_build_geometry_in(self):
        """check that curve_list is correct (inwards magnet)"""
        lam = LamSlotMag(
            Rint=40e-1,
            Rext=90e-1,
            is_internal=True,
            is_stator=False,
            L1=0.45,
            Nrvd=1,
            Wrvd=0.05,
        )
        magnet = [MagnetType11(Wmag=pi / 10, Hmag=0.2)]
        lam.slot = SlotMPolar(Zs=8, W0=pi / 10, H0=0.2, magnet=magnet)
        test_obj = lam.slot.magnet[0]
        Z1 = (90e-1 - 0.2) * exp(-1j * pi / 10 / 2)
        Z2 = (90e-1 - 0.2) * exp(1j * pi / 10 / 2)

        Z = abs(Z1)

        Z3 = (Z + 0.2) * exp(1j * angle(Z1))
        Z4 = (Z + 0.2) * exp(1j * angle(Z2))

        # # Creation of curve
        curve_list = list()
        curve_list.append(Segment(Z1, Z3))
        curve_list.append(Arc1(Z3, Z4, abs(Z3)))
        curve_list.append(Segment(Z4, Z2))
        curve_list.append(Arc1(Z2, Z1, -abs(Z2)))

        surface = test_obj.build_geometry()
        result = surface[0].get_lines()
        for i in range(0, len(result)):
            a = result[i].begin
            b = curve_list[i].begin
            self.assertAlmostEqual((a - b) / a, 0, delta=DELTA)

            a = result[i].end
            b = curve_list[i].end
            self.assertAlmostEqual((a - b) / a, 0, delta=DELTA)

[((24, 6, 24, 44), 'pyleecan.Classes.LamSlotMag.LamSlotMag', 'LamSlotMag', (), '', False, 'from pyleecan.Classes.LamSlotMag import LamSlotMag\n'), ((25, 11, 25, 44), 'pyleecan.Classes.SlotMPolar.SlotMPolar', 'SlotMPolar', (), '', False, 'from pyleecan.Classes.SlotMPolar import SlotMPolar\n'), ((30, 6, 30, 44), 'pyleecan.Classes.LamSlotMag.LamSlotMag', 'LamSlotMag', (), '', False, 'from pyleecan.Classes.LamSlotMag import LamSlotMag\n'), ((31, 11, 31, 48), 'pyleecan.Classes.SlotMPolar.SlotMPolar', 'SlotMPolar', (), '', False, 'from pyleecan.Classes.SlotMPolar import SlotMPolar\n'), ((36, 6, 36, 48), 'pyleecan.Classes.LamSlotMag.LamSlotMag', 'LamSlotMag', (), '', False, 'from pyleecan.Classes.LamSlotMag import LamSlotMag\n'), ((37, 11, 37, 48), 'pyleecan.Classes.SlotMPolar.SlotMPolar', 'SlotMPolar', (), '', False, 'from pyleecan.Classes.SlotMPolar import SlotMPolar\n'), ((26, 19, 26, 52), 'pyleecan.Classes.MagnetType11.MagnetType11', 'MagnetType11', (), '', False, 'from pyleecan.Classes.MagnetType11 import MagnetType11\n'), ((32, 19, 32, 56), 'pyleecan.Classes.MagnetType11.MagnetType11', 'MagnetType11', (), '', False, 'from pyleecan.Classes.MagnetType11 import MagnetType11\n'), ((38, 19, 38, 56), 'pyleecan.Classes.MagnetType11.MagnetType11', 'MagnetType11', (), '', False, 'from pyleecan.Classes.MagnetType11 import MagnetType11\n'), ((50, 5, 50, 22), 'ddt.data', 'data', ({(50, 10, 50, 21): '*Mag11_test'}, {}), '(*Mag11_test)', False, 'from ddt import ddt, data\n'), ((67, 5, 67, 22), 'ddt.data', 'data', ({(67, 10, 67, 21): '*Mag11_test'}, {}), '(*Mag11_test)', False, 'from ddt import ddt, data\n'), ((79, 5, 79, 22), 'ddt.data', 'data', ({(79, 10, 79, 21): '*Mag11_test'}, {}), '(*Mag11_test)', False, 'from ddt import ddt, data\n'), ((63, 12, 63, 49), 'pyleecan.Methods.Machine.Magnet.comp_surface.comp_surface', 'comp_surface', ({(63, 25, 63, 48): 'test_obj.slot.magnet[0]'}, {}), '(test_obj.slot.magnet[0])', False, 'from pyleecan.Methods.Machine.Magnet.comp_surface import comp_surface\n'), ((93, 14, 101, 9), 'pyleecan.Classes.LamSlotMag.LamSlotMag', 'LamSlotMag', (), '', False, 'from pyleecan.Classes.LamSlotMag import LamSlotMag\n'), ((103, 19, 103, 70), 'pyleecan.Classes.SlotMPolar.SlotMPolar', 'SlotMPolar', (), '', False, 'from pyleecan.Classes.SlotMPolar import SlotMPolar\n'), ((133, 14, 141, 9), 'pyleecan.Classes.LamSlotMag.LamSlotMag', 'LamSlotMag', (), '', False, 'from pyleecan.Classes.LamSlotMag import LamSlotMag\n'), ((143, 19, 143, 70), 'pyleecan.Classes.SlotMPolar.SlotMPolar', 'SlotMPolar', (), '', False, 'from pyleecan.Classes.SlotMPolar import SlotMPolar\n'), ((102, 18, 102, 54), 'pyleecan.Classes.MagnetType11.MagnetType11', 'MagnetType11', (), '', False, 'from pyleecan.Classes.MagnetType11 import MagnetType11\n'), ((105, 29, 105, 51), 'numpy.exp', 'exp', ({(105, 33, 105, 50): '(-1.0j * pi / 10 / 2)'}, {}), '(-1.0j * pi / 10 / 2)', False, 'from numpy import pi, exp, angle, array\n'), ((106, 29, 106, 50), 'numpy.exp', 'exp', ({(106, 33, 106, 49): '(1.0j * pi / 10 / 2)'}, {}), '(1.0j * pi / 10 / 2)', False, 'from numpy import pi, exp, angle, array\n'), ((115, 26, 115, 41), 'pyleecan.Classes.Segment.Segment', 'Segment', ({(115, 34, 115, 36): 'Z1', (115, 38, 115, 40): 'Z3'}, {}), '(Z1, Z3)', False, 'from pyleecan.Classes.Segment import Segment\n'), ((117, 26, 117, 41), 'pyleecan.Classes.Segment.Segment', 'Segment', ({(117, 34, 117, 36): 'Z4', (117, 38, 117, 40): 'Z2'}, {}), '(Z4, Z2)', False, 'from pyleecan.Classes.Segment import Segment\n'), ((142, 18, 142, 54), 'pyleecan.Classes.MagnetType11.MagnetType11', 'MagnetType11', (), '', False, 'from pyleecan.Classes.MagnetType11 import MagnetType11\n'), ((145, 29, 145, 51), 'numpy.exp', 'exp', ({(145, 33, 145, 50): '(-1.0j * pi / 10 / 2)'}, {}), '(-1.0j * pi / 10 / 2)', False, 'from numpy import pi, exp, angle, array\n'), ((146, 29, 146, 50), 'numpy.exp', 'exp', ({(146, 33, 146, 49): '(1.0j * pi / 10 / 2)'}, {}), '(1.0j * pi / 10 / 2)', False, 'from numpy import pi, exp, angle, array\n'), ((155, 26, 155, 41), 'pyleecan.Classes.Segment.Segment', 'Segment', ({(155, 34, 155, 36): 'Z1', (155, 38, 155, 40): 'Z3'}, {}), '(Z1, Z3)', False, 'from pyleecan.Classes.Segment import Segment\n'), ((157, 26, 157, 41), 'pyleecan.Classes.Segment.Segment', 'Segment', ({(157, 34, 157, 36): 'Z4', (157, 38, 157, 40): 'Z2'}, {}), '(Z4, Z2)', False, 'from pyleecan.Classes.Segment import Segment\n'), ((110, 34, 110, 43), 'numpy.angle', 'angle', ({(110, 40, 110, 42): 'Z1'}, {}), '(Z1)', False, 'from numpy import pi, exp, angle, array\n'), ((111, 34, 111, 43), 'numpy.angle', 'angle', ({(111, 40, 111, 42): 'Z2'}, {}), '(Z2)', False, 'from numpy import pi, exp, angle, array\n'), ((150, 34, 150, 43), 'numpy.angle', 'angle', ({(150, 40, 150, 42): 'Z1'}, {}), '(Z1)', False, 'from numpy import pi, exp, angle, array\n'), ((151, 34, 151, 43), 'numpy.angle', 'angle', ({(151, 40, 151, 42): 'Z2'}, {}), '(Z2)', False, 'from numpy import pi, exp, angle, array\n')]