# Copyright (c) Pymatgen Development Team.
# Distributed under the terms of the MIT License.


"""
Test for the piezo tensor class
"""

__author__ = "Shyam Dwaraknath"
__version__ = "0.1"
__maintainer__ = "Shyam Dwaraknath"
__email__ = "shyamd@lbl.gov"
__status__ = "Development"
__date__ = "4/1/16"

import os
import unittest

import numpy as np

from pymatgen.analysis.piezo import PiezoTensor
from pymatgen.util.testing import PymatgenTest


class PiezoTest(PymatgenTest):
    def setUp(self):
        self.piezo_struc = self.get_structure("BaNiO3")
        self.voigt_matrix = np.array(
            [
                [0.0, 0.0, 0.0, 0.0, 0.03839, 0.0],
                [0.0, 0.0, 0.0, 0.03839, 0.0, 0.0],
                [6.89822, 6.89822, 27.46280, 0.0, 0.0, 0.0],
            ]
        )
        self.vasp_matrix = np.array(
            [
                [0.0, 0.0, 0.0, 0.0, 0.0, 0.03839],
                [0.0, 0.0, 0.0, 0.0, 0.03839, 0.0, 0.0],
                [6.89822, 6.89822, 27.46280, 0.0, 0.0, 0.0],
            ]
        )
        self.full_tensor_array = [
            [[0.0, 0.0, 0.03839], [0.0, 0.0, 0.0], [0.03839, 0.0, 0.0]],
            [[0.0, 0.0, 0.0], [0.0, 0.0, 0.03839], [0.0, 0.03839, 0.0]],
            [[6.89822, 0.0, 0.0], [0.0, 6.89822, 0.0], [0.0, 0.0, 27.4628]],
        ]

    def test_new(self):
        pt = PiezoTensor(self.full_tensor_array)
        self.assertArrayAlmostEqual(pt, self.full_tensor_array)
        bad_dim_array = np.zeros((3, 3))
        self.assertRaises(ValueError, PiezoTensor, bad_dim_array)

    def test_from_voigt(self):
        bad_voigt = np.zeros((3, 7))
        pt = PiezoTensor.from_voigt(self.voigt_matrix)
        self.assertArrayEqual(pt, self.full_tensor_array)
        self.assertRaises(ValueError, PiezoTensor.from_voigt, bad_voigt)
        self.assertArrayEqual(self.voigt_matrix, pt.voigt)

    def test_from_vasp_voigt(self):
        bad_voigt = np.zeros((3, 7))
        pt = PiezoTensor.from_vasp_voigt(self.vasp_matrix)
        self.assertArrayEqual(pt, self.full_tensor_array)
        self.assertRaises(ValueError, PiezoTensor.from_voigt, bad_voigt)
        self.assertArrayEqual(self.voigt_matrix, pt.voigt)


if __name__ == "__main__":
    unittest.main()

[((70, 4, 70, 19), 'unittest.main', 'unittest.main', ({}, {}), '()', False, 'import unittest\n'), ((28, 28, 34, 9), 'numpy.array', 'np.array', ({(29, 12, 33, 13): '[[0.0, 0.0, 0.0, 0.0, 0.03839, 0.0], [0.0, 0.0, 0.0, 0.03839, 0.0, 0.0], [\n    6.89822, 6.89822, 27.4628, 0.0, 0.0, 0.0]]'}, {}), '([[0.0, 0.0, 0.0, 0.0, 0.03839, 0.0], [0.0, 0.0, 0.0, 0.03839, 0.0,\n    0.0], [6.89822, 6.89822, 27.4628, 0.0, 0.0, 0.0]])', True, 'import numpy as np\n'), ((35, 27, 41, 9), 'numpy.array', 'np.array', ({(36, 12, 40, 13): '[[0.0, 0.0, 0.0, 0.0, 0.0, 0.03839], [0.0, 0.0, 0.0, 0.0, 0.03839, 0.0, 0.0\n    ], [6.89822, 6.89822, 27.4628, 0.0, 0.0, 0.0]]'}, {}), '([[0.0, 0.0, 0.0, 0.0, 0.0, 0.03839], [0.0, 0.0, 0.0, 0.0, 0.03839,\n    0.0, 0.0], [6.89822, 6.89822, 27.4628, 0.0, 0.0, 0.0]])', True, 'import numpy as np\n'), ((49, 13, 49, 48), 'pymatgen.analysis.piezo.PiezoTensor', 'PiezoTensor', ({(49, 25, 49, 47): 'self.full_tensor_array'}, {}), '(self.full_tensor_array)', False, 'from pymatgen.analysis.piezo import PiezoTensor\n'), ((51, 24, 51, 40), 'numpy.zeros', 'np.zeros', ({(51, 33, 51, 39): '(3, 3)'}, {}), '((3, 3))', True, 'import numpy as np\n'), ((55, 20, 55, 36), 'numpy.zeros', 'np.zeros', ({(55, 29, 55, 35): '(3, 7)'}, {}), '((3, 7))', True, 'import numpy as np\n'), ((56, 13, 56, 54), 'pymatgen.analysis.piezo.PiezoTensor.from_voigt', 'PiezoTensor.from_voigt', ({(56, 36, 56, 53): 'self.voigt_matrix'}, {}), '(self.voigt_matrix)', False, 'from pymatgen.analysis.piezo import PiezoTensor\n'), ((62, 20, 62, 36), 'numpy.zeros', 'np.zeros', ({(62, 29, 62, 35): '(3, 7)'}, {}), '((3, 7))', True, 'import numpy as np\n'), ((63, 13, 63, 58), 'pymatgen.analysis.piezo.PiezoTensor.from_vasp_voigt', 'PiezoTensor.from_vasp_voigt', ({(63, 41, 63, 57): 'self.vasp_matrix'}, {}), '(self.vasp_matrix)', False, 'from pymatgen.analysis.piezo import PiezoTensor\n')]