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

import pytest
import numpy as np
from unittest import TestCase

from pyleecan.Classes.CellMat import CellMat
from pyleecan.Classes.MeshSolution import MeshSolution
from pyleecan.Classes.PointMat import PointMat
from pyleecan.Classes.MeshMat import MeshMat
from pyleecan.Classes.ScalarProductL2 import ScalarProductL2
from pyleecan.Classes.Interpolation import Interpolation
from pyleecan.Classes.RefSegmentP1 import RefSegmentP1
from pyleecan.Classes.FPGNSeg import FPGNSeg


@pytest.mark.MeshSol
class unittest_real_points(TestCase):
    """ Tests for interpolation method"""

    def test_line(self):
        DELTA = 1e-10

        mesh = MeshMat()
        mesh.cell["line"] = CellMat(nb_pt_per_cell=2)
        mesh.point = PointMat()
        mesh.point.add_point(np.array([0, 0]))
        mesh.point.add_point(np.array([1, 0]))
        mesh.point.add_point(np.array([0, 1]))
        mesh.point.add_point(np.array([2, 3]))
        mesh.point.add_point(np.array([3, 3]))

        mesh.add_cell(np.array([0, 1]), "line")
        mesh.add_cell(np.array([0, 2]), "line")
        mesh.add_cell(np.array([1, 2]), "line")

        c_line = mesh.cell["line"]

        c_line.interpolation = Interpolation()
        c_line.interpolation.ref_cell = RefSegmentP1()
        c_line.interpolation.scalar_product = ScalarProductL2()
        c_line.interpolation.gauss_point = FPGNSeg()

        meshsol = MeshSolution()
        meshsol.mesh = [mesh]

        vert = mesh.get_vertice(0)["line"]
        test_pt = np.array([0.7, 0])
        test_field = np.array([1, 1])
        sol = [1]
        func = c_line.interpolation.ref_cell.interpolation(test_pt, vert, test_field)
        testA = np.sum(abs(func - sol))
        msg = "Wrong result: returned " + str(func) + ", expected: " + str(test_field)
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

        vert = mesh.get_vertice(0)["line"]
        test_pt = np.array([0.7, 0])
        test_field = np.ones(
            (2, 120, 3)
        )  # Simulate a 3D vector field for 120 time step
        func = c_line.interpolation.ref_cell.interpolation(test_pt, vert, test_field)
        sol = np.ones((120, 3))
        testA = np.sum(abs(func - sol))
        msg = "Wrong result: returned " + str(func) + ", expected: " + str(sol)
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

        vert = mesh.get_vertice(2)["line"]
        test_pt = np.array([0.6, 0.4])
        test_field = np.zeros((2, 120, 3))
        test_field[0, :] = np.ones(
            (1, 120, 3)
        )  # Simulate a 3D vector field for 120 time step
        func = c_line.interpolation.ref_cell.interpolation(test_pt, vert, test_field)
        sol = 0.6 * np.ones((120, 3))
        testA = np.sum(abs(sol - func))
        msg = "Wrong result: returned " + str(func) + ", expected: " + str(sol)
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

        vert = mesh.get_vertice(1)["line"]
        test_pt = np.array([0, 0.4])
        test_field = np.zeros((2, 120, 3))
        test_field[1, :] = np.ones(
            (1, 120, 3)
        )  # Simulate a 3D vector field for 120 time step
        func = c_line.interpolation.ref_cell.interpolation(test_pt, vert, test_field)
        sol = 0.4 * np.ones((120, 3))
        testA = np.sum(abs(sol - func))
        msg = "Wrong result: returned " + str(func) + ", expected: " + str(sol)
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

[((24, 15, 24, 24), 'pyleecan.Classes.MeshMat.MeshMat', 'MeshMat', ({}, {}), '()', False, 'from pyleecan.Classes.MeshMat import MeshMat\n'), ((25, 28, 25, 53), 'pyleecan.Classes.CellMat.CellMat', 'CellMat', (), '', False, 'from pyleecan.Classes.CellMat import CellMat\n'), ((26, 21, 26, 31), 'pyleecan.Classes.PointMat.PointMat', 'PointMat', ({}, {}), '()', False, 'from pyleecan.Classes.PointMat import PointMat\n'), ((39, 31, 39, 46), 'pyleecan.Classes.Interpolation.Interpolation', 'Interpolation', ({}, {}), '()', False, 'from pyleecan.Classes.Interpolation import Interpolation\n'), ((40, 40, 40, 54), 'pyleecan.Classes.RefSegmentP1.RefSegmentP1', 'RefSegmentP1', ({}, {}), '()', False, 'from pyleecan.Classes.RefSegmentP1 import RefSegmentP1\n'), ((41, 46, 41, 63), 'pyleecan.Classes.ScalarProductL2.ScalarProductL2', 'ScalarProductL2', ({}, {}), '()', False, 'from pyleecan.Classes.ScalarProductL2 import ScalarProductL2\n'), ((42, 43, 42, 52), 'pyleecan.Classes.FPGNSeg.FPGNSeg', 'FPGNSeg', ({}, {}), '()', False, 'from pyleecan.Classes.FPGNSeg import FPGNSeg\n'), ((44, 18, 44, 32), 'pyleecan.Classes.MeshSolution.MeshSolution', 'MeshSolution', ({}, {}), '()', False, 'from pyleecan.Classes.MeshSolution import MeshSolution\n'), ((48, 18, 48, 36), 'numpy.array', 'np.array', ({(48, 27, 48, 35): '[0.7, 0]'}, {}), '([0.7, 0])', True, 'import numpy as np\n'), ((49, 21, 49, 37), 'numpy.array', 'np.array', ({(49, 30, 49, 36): '[1, 1]'}, {}), '([1, 1])', True, 'import numpy as np\n'), ((57, 18, 57, 36), 'numpy.array', 'np.array', ({(57, 27, 57, 35): '[0.7, 0]'}, {}), '([0.7, 0])', True, 'import numpy as np\n'), ((58, 21, 60, 9), 'numpy.ones', 'np.ones', ({(59, 12, 59, 23): '(2, 120, 3)'}, {}), '((2, 120, 3))', True, 'import numpy as np\n'), ((62, 14, 62, 31), 'numpy.ones', 'np.ones', ({(62, 22, 62, 30): '(120, 3)'}, {}), '((120, 3))', True, 'import numpy as np\n'), ((68, 18, 68, 38), 'numpy.array', 'np.array', ({(68, 27, 68, 37): '[0.6, 0.4]'}, {}), '([0.6, 0.4])', True, 'import numpy as np\n'), ((69, 21, 69, 42), 'numpy.zeros', 'np.zeros', ({(69, 30, 69, 41): '(2, 120, 3)'}, {}), '((2, 120, 3))', True, 'import numpy as np\n'), ((70, 27, 72, 9), 'numpy.ones', 'np.ones', ({(71, 12, 71, 23): '(1, 120, 3)'}, {}), '((1, 120, 3))', True, 'import numpy as np\n'), ((80, 18, 80, 36), 'numpy.array', 'np.array', ({(80, 27, 80, 35): '[0, 0.4]'}, {}), '([0, 0.4])', True, 'import numpy as np\n'), ((81, 21, 81, 42), 'numpy.zeros', 'np.zeros', ({(81, 30, 81, 41): '(2, 120, 3)'}, {}), '((2, 120, 3))', True, 'import numpy as np\n'), ((82, 27, 84, 9), 'numpy.ones', 'np.ones', ({(83, 12, 83, 23): '(1, 120, 3)'}, {}), '((1, 120, 3))', True, 'import numpy as np\n'), ((27, 29, 27, 45), 'numpy.array', 'np.array', ({(27, 38, 27, 44): '[0, 0]'}, {}), '([0, 0])', True, 'import numpy as np\n'), ((28, 29, 28, 45), 'numpy.array', 'np.array', ({(28, 38, 28, 44): '[1, 0]'}, {}), '([1, 0])', True, 'import numpy as np\n'), ((29, 29, 29, 45), 'numpy.array', 'np.array', ({(29, 38, 29, 44): '[0, 1]'}, {}), '([0, 1])', True, 'import numpy as np\n'), ((30, 29, 30, 45), 'numpy.array', 'np.array', ({(30, 38, 30, 44): '[2, 3]'}, {}), '([2, 3])', True, 'import numpy as np\n'), ((31, 29, 31, 45), 'numpy.array', 'np.array', ({(31, 38, 31, 44): '[3, 3]'}, {}), '([3, 3])', True, 'import numpy as np\n'), ((33, 22, 33, 38), 'numpy.array', 'np.array', ({(33, 31, 33, 37): '[0, 1]'}, {}), '([0, 1])', True, 'import numpy as np\n'), ((34, 22, 34, 38), 'numpy.array', 'np.array', ({(34, 31, 34, 37): '[0, 2]'}, {}), '([0, 2])', True, 'import numpy as np\n'), ((35, 22, 35, 38), 'numpy.array', 'np.array', ({(35, 31, 35, 37): '[1, 2]'}, {}), '([1, 2])', True, 'import numpy as np\n'), ((74, 20, 74, 37), 'numpy.ones', 'np.ones', ({(74, 28, 74, 36): '(120, 3)'}, {}), '((120, 3))', True, 'import numpy as np\n'), ((86, 20, 86, 37), 'numpy.ones', 'np.ones', ({(86, 28, 86, 36): '(120, 3)'}, {}), '((120, 3))', True, 'import numpy as np\n')]