Source code for Tests.Methods.Simulation.test_InCurrentDQ_meth

# -*- coding: utf-8 -*-

from unittest import TestCase
from os.path import join
from ddt import data, ddt
from numpy import linspace, ones, pi, zeros, array, sqrt, transpose
from numpy.testing import assert_array_almost_equal

from pyleecan.Classes.ImportGenMatrixSin import ImportGenMatrixSin
from pyleecan.Classes.ImportGenVectLin import ImportGenVectLin
from pyleecan.Classes.ImportGenVectSin import ImportGenVectSin
from pyleecan.Classes.ImportMatrixVal import ImportMatrixVal
from pyleecan.Classes.InputCurrentDQ import InputCurrentDQ
from pyleecan.Classes.LamSlotWind import LamSlotWind
from pyleecan.Classes.MachineIPMSM import MachineIPMSM
from pyleecan.Classes.MachineDFIM import MachineDFIM
from pyleecan.Classes.Simulation import Simulation
from pyleecan.Classes.Output import Output
from pyleecan.Classes.WindingDW1L import WindingDW1L
from pyleecan.Methods.Simulation.Input import InputError
from pyleecan.Functions.load import load
from pyleecan.definitions import DATA_DIR

IPMSM_A = load(join(DATA_DIR, "Machine", "IPMSM_A.json"))
InputCurrentDQ_Error_test = list()
time_wrong = ImportMatrixVal(value=zeros((10, 2)))
time = ImportGenVectLin(0, 10, 100)

angle_wrong = ImportMatrixVal(value=zeros((10, 4)))
angle = ImportGenVectLin(0, 2 * pi, 1024)

I_1 = ImportMatrixVal(value=zeros((100, 2)))
I_2 = ImportMatrixVal(value=zeros((100, 3)))
I_3 = ImportMatrixVal(value=zeros((2, 100)))
I_4 = ImportMatrixVal(value=zeros((100)))

angle_rotor_wrong = ImportMatrixVal(value=zeros((10, 2)))
angle_rotor_wrong2 = ImportMatrixVal(value=zeros((102)))
angle_rotor = ImportMatrixVal(value=zeros((100)))

Nr_wrong = ImportMatrixVal(value=zeros((10, 2)))
Nr_wrong2 = ImportMatrixVal(value=zeros((102)))
Nr = ImportMatrixVal(value=zeros((100)))

# Wrong time
test_obj = Simulation()
test_obj.input = InputCurrentDQ(time=None)
InputCurrentDQ_Error_test.append(
    {"test_obj": test_obj, "exp": "ERROR: InputCurrentDQ.time missing"}
)
test_obj = Simulation()
test_obj.input = InputCurrentDQ(time=time_wrong)
InputCurrentDQ_Error_test.append(
    {
        "test_obj": test_obj,
        "exp": "ERROR: InputCurrentDQ.time should be a vector, (10, 2) shape found",
    }
)
# Wrong angle
test_obj = Simulation()
test_obj.input = InputCurrentDQ(time=time, angle=None)
InputCurrentDQ_Error_test.append(
    {"test_obj": test_obj, "exp": "ERROR: InputCurrentDQ.angle missing"}
)
test_obj = Simulation()
test_obj.input = InputCurrentDQ(time=time, angle=angle_wrong)
InputCurrentDQ_Error_test.append(
    {
        "test_obj": test_obj,
        "exp": "ERROR: InputCurrentDQ.angle should be a vector, (10, 4) shape found",
    }
)
# Wrong Is
test_obj = Simulation(machine=IPMSM_A)
test_obj.input = InputCurrentDQ(time=time, angle=angle, Is=None)
InputCurrentDQ_Error_test.append(
    {"test_obj": test_obj, "exp": "ERROR: InputCurrentDQ.Is missing"}
)
test_obj = Simulation(machine=IPMSM_A)
test_obj.input = InputCurrentDQ(time=time, angle=angle, Is=I_3)
InputCurrentDQ_Error_test.append(
    {
        "test_obj": test_obj,
        "exp": "ERROR: InputCurrentDQ.Is must be a matrix with the shape (100, 2) (len(time), stator phase number), (2, 100) returned",
    }
)
test_obj = Simulation(machine=IPMSM_A)
test_obj.input = InputCurrentDQ(time=time, angle=angle, Is=I_4)
InputCurrentDQ_Error_test.append(
    {
        "test_obj": test_obj,
        "exp": "ERROR: InputCurrentDQ.Is must be a matrix with the shape (100, 2) (len(time), stator phase number), (100,) returned",
    }
)

# Wrong Nr, alpha_rotor
test_obj = Simulation(machine=IPMSM_A)
test_obj.input = InputCurrentDQ(
    time=time, angle=angle, Is=I_1, Ir=None, angle_rotor=None, Nr=None
)
InputCurrentDQ_Error_test.append(
    {
        "test_obj": test_obj,
        "exp": "ERROR: InputCurrentDQ.angle_rotor and InputCurrentDQ.Nr can't be None at the same time",
    }
)
test_obj = Simulation(machine=IPMSM_A)
test_obj.input = InputCurrentDQ(
    time=time, angle=angle, Is=I_1, Ir=None, angle_rotor=angle_rotor_wrong, Nr=None
)
InputCurrentDQ_Error_test.append(
    {
        "test_obj": test_obj,
        "exp": "ERROR: InputCurrentDQ.angle_rotor should be a vector of the same length as time, (10, 2) shape found, (100,) expected",
    }
)
test_obj = Simulation(machine=IPMSM_A)
test_obj.input = InputCurrentDQ(
    time=time, angle=angle, Is=I_1, Ir=None, angle_rotor=angle_rotor_wrong2, Nr=None
)
InputCurrentDQ_Error_test.append(
    {
        "test_obj": test_obj,
        "exp": "ERROR: InputCurrentDQ.angle_rotor should be a vector of the same length as time, (102,) shape found, (100,) expected",
    }
)
test_obj = Simulation(machine=IPMSM_A)
test_obj.input = InputCurrentDQ(
    time=time, angle=angle, Is=I_1, Ir=None, angle_rotor=angle_rotor, Nr=Nr_wrong
)
InputCurrentDQ_Error_test.append(
    {
        "test_obj": test_obj,
        "exp": "ERROR: InputCurrentDQ.Nr should be a vector of the same length as time, (10, 2) shape found, (100,) expected",
    }
)
test_obj = Simulation(machine=IPMSM_A)
test_obj.input = InputCurrentDQ(
    time=time, angle=angle, Is=I_1, Ir=None, angle_rotor=angle_rotor, Nr=Nr_wrong2
)
InputCurrentDQ_Error_test.append(
    {
        "test_obj": test_obj,
        "exp": "ERROR: InputCurrentDQ.Nr should be a vector of the same length as time, (102,) shape found, (100,) expected",
    }
)


[docs]@ddt class unittest_InputCurrentDQ_meth(TestCase): """unittest for InputCurrentDQ object methods""" @data(*InputCurrentDQ_Error_test) def test_InputCurrentDQ_Error_test(self, test_dict): """Check that the input current raises the correct errors """ output = Output(simu=test_dict["test_obj"]) with self.assertRaises( InputError, msg="Expect: " + test_dict["exp"] ) as context: output.simu.input.gen_input() self.assertEqual(test_dict["exp"], str(context.exception))
[docs] def test_InputCurrentDQ_Ok(self): """Check that the input current can return a correct output """ test_obj = Simulation(machine=IPMSM_A) output = Output(simu=test_obj) time = ImportGenVectLin(0, 1, 7) angle = ImportGenVectLin(0, 2 * pi, 20) Is = ImportMatrixVal( value=transpose(array([[2, 2, 2, 2, 2, 2, 2], [0, 0, 0, 0, 0, 0, 0]])) ) Is_exp = transpose( array( [ [2, 1, -1, -2, -1, 1, 2], [-1, -2, -1, 1, 2, 1, -1], [-1, 1, 2, 1, -1, -2, -1], ] ) ) zp = IPMSM_A.stator.get_pole_pair_number() angle_rotor_initial = IPMSM_A.comp_angle_offset_initial() angle_rotor_exp = linspace(0, 2 * pi / zp, 7) + angle_rotor_initial Nr = ImportMatrixVal(value=ones(7) * 60 / zp) test_obj.input = InputCurrentDQ( time=time, angle=angle, Is=Is, Ir=None, angle_rotor=None, Nr=Nr, angle_rotor_initial=angle_rotor_initial, rot_dir=1, ) test_obj.input.gen_input() assert_array_almost_equal(output.elec.time, linspace(0, 1, 7)) assert_array_almost_equal(output.elec.angle, linspace(0, 2 * pi, 20)) assert_array_almost_equal(output.elec.Is, Is_exp) assert_array_almost_equal(output.elec.angle_rotor, angle_rotor_exp) assert_array_almost_equal(output.elec.Nr, ones(7) * 60 / zp)