Beispiel #1
0
 def setUp(self):
     self.seq_tc = [t for t in np.arange(4 * 3**3).reshape((4, 3, 3, 3))]
     self.seq_tc = TensorCollection(self.seq_tc)
     self.rand_tc = TensorCollection(
         [t for t in np.random.random((4, 3, 3))])
     self.diff_rank = TensorCollection(
         [np.ones([3] * i) for i in range(2, 5)])
     self.struct = self.get_structure("Si")
     ieee_file_path = os.path.join(PymatgenTest.TEST_FILES_DIR,
                                   "ieee_conversion_data.json")
     self.ieee_data = loadfn(ieee_file_path)
Beispiel #2
0
    def test_serialization(self):
        # Test base serialize-deserialize
        d = self.seq_tc.as_dict()
        new = TensorCollection.from_dict(d)
        for t, t_new in zip(self.seq_tc, new):
            self.assertArrayAlmostEqual(t, t_new)

        # Suppress vsym warnings and test voigt
        with warnings.catch_warnings(record=True):
            vsym = self.rand_tc.voigt_symmetrized
            d = vsym.as_dict(voigt=True)
            new_vsym = TensorCollection.from_dict(d)
            for t, t_new in zip(vsym, new_vsym):
                self.assertArrayAlmostEqual(t, t_new)
Beispiel #3
0
 def get_compliance_expansion(self):
     """
     Gets a compliance tensor expansion from the elastic
     tensor expansion.
     """
     # TODO: this might have a general form
     if not self.order <= 4:
         raise ValueError("Compliance tensor expansion only "
                          "supported for fourth-order and lower")
     ce_exp = [ElasticTensor(self[0]).compliance_tensor]
     einstring = "ijpq,pqrsuv,rskl,uvmn->ijklmn"
     ce_exp.append(
         np.einsum(einstring, -ce_exp[-1], self[1], ce_exp[-1], ce_exp[-1]))
     if self.order == 4:
         # Four terms in the Fourth-Order compliance tensor
         # pylint: disable=E1130
         einstring_1 = "pqab,cdij,efkl,ghmn,abcdefgh"
         tensors_1 = [ce_exp[0]] * 4 + [self[-1]]
         temp = -np.einsum(einstring_1, *tensors_1)
         einstring_2 = "pqab,abcdef,cdijmn,efkl"
         einstring_3 = "pqab,abcdef,efklmn,cdij"
         einstring_4 = "pqab,abcdef,cdijkl,efmn"
         for es in [einstring_2, einstring_3, einstring_4]:
             temp -= np.einsum(es, ce_exp[0], self[-2], ce_exp[1],
                               ce_exp[0])
         ce_exp.append(temp)
     return TensorCollection(ce_exp)
Beispiel #4
0
    def list_based_function_check(self, attribute, coll, *args, **kwargs):
        """
        This function allows for more efficient testing of list-based
        functions in a "collection"-style class like TensorCollection

        It ensures that the test function
        """
        tc_orig = TensorCollection(coll)
        tc_mod = getattr(tc_orig, attribute)
        if callable(tc_mod):
            tc_mod = tc_mod(*args, **kwargs)
        for t_orig, t_mod in zip(tc_orig, tc_mod):
            this_mod = getattr(t_orig, attribute)
            if callable(this_mod):
                this_mod = this_mod(*args, **kwargs)
            if isinstance(this_mod, np.ndarray):
                self.assertArrayAlmostEqual(this_mod, t_mod)
Beispiel #5
0
    def test_list_based_functions(self):
        # zeroed
        tc = TensorCollection([1e-4 * Tensor(np.eye(3))] * 4)
        for t in tc.zeroed():
            self.assertArrayEqual(t, np.zeros((3, 3)))
        for t in tc.zeroed(1e-5):
            self.assertArrayEqual(t, 1e-4 * np.eye(3))
        self.list_based_function_check("zeroed", tc)
        self.list_based_function_check("zeroed", tc, tol=1e-5)

        # transform
        symm_op = SymmOp.from_axis_angle_and_translation([0, 0, 1], 30, False,
                                                         [0, 0, 1])
        self.list_based_function_check("transform",
                                       self.seq_tc,
                                       symm_op=symm_op)

        # symmetrized
        self.list_based_function_check("symmetrized", self.seq_tc)

        # rotation
        a = 3.14 * 42.5 / 180
        rotation = SquareTensor([[math.cos(a), 0, math.sin(a)], [0, 1, 0],
                                 [-math.sin(a), 0,
                                  math.cos(a)]])
        self.list_based_function_check("rotate",
                                       self.diff_rank,
                                       matrix=rotation)

        # is_symmetric
        self.assertFalse(self.seq_tc.is_symmetric())
        self.assertTrue(self.diff_rank.is_symmetric())

        # fit_to_structure
        self.list_based_function_check("fit_to_structure", self.diff_rank,
                                       self.struct)
        self.list_based_function_check("fit_to_structure", self.seq_tc,
                                       self.struct)

        # fit_to_structure
        self.list_based_function_check("fit_to_structure", self.diff_rank,
                                       self.struct)
        self.list_based_function_check("fit_to_structure", self.seq_tc,
                                       self.struct)

        # voigt
        self.list_based_function_check("voigt", self.diff_rank)

        # is_voigt_symmetric
        self.assertTrue(self.diff_rank.is_voigt_symmetric())
        self.assertFalse(self.seq_tc.is_voigt_symmetric())

        # Convert to ieee
        for entry in self.ieee_data[:2]:
            entry["xtal"]
            tc = TensorCollection([entry["original_tensor"]] * 3)
            struct = entry["structure"]
            self.list_based_function_check("convert_to_ieee", tc, struct)

        # from_voigt
        tc_input = [t for t in np.random.random((3, 6, 6))]
        tc = TensorCollection.from_voigt(tc_input)
        for t_input, t in zip(tc_input, tc):
            self.assertArrayAlmostEqual(Tensor.from_voigt(t_input), t)
Beispiel #6
0
class TensorCollectionTest(PymatgenTest):
    def setUp(self):
        self.seq_tc = [t for t in np.arange(4 * 3**3).reshape((4, 3, 3, 3))]
        self.seq_tc = TensorCollection(self.seq_tc)
        self.rand_tc = TensorCollection(
            [t for t in np.random.random((4, 3, 3))])
        self.diff_rank = TensorCollection(
            [np.ones([3] * i) for i in range(2, 5)])
        self.struct = self.get_structure("Si")
        ieee_file_path = os.path.join(PymatgenTest.TEST_FILES_DIR,
                                      "ieee_conversion_data.json")
        self.ieee_data = loadfn(ieee_file_path)

    def list_based_function_check(self, attribute, coll, *args, **kwargs):
        """
        This function allows for more efficient testing of list-based
        functions in a "collection"-style class like TensorCollection

        It ensures that the test function
        """
        tc_orig = TensorCollection(coll)
        tc_mod = getattr(tc_orig, attribute)
        if callable(tc_mod):
            tc_mod = tc_mod(*args, **kwargs)
        for t_orig, t_mod in zip(tc_orig, tc_mod):
            this_mod = getattr(t_orig, attribute)
            if callable(this_mod):
                this_mod = this_mod(*args, **kwargs)
            if isinstance(this_mod, np.ndarray):
                self.assertArrayAlmostEqual(this_mod, t_mod)

    def test_list_based_functions(self):
        # zeroed
        tc = TensorCollection([1e-4 * Tensor(np.eye(3))] * 4)
        for t in tc.zeroed():
            self.assertArrayEqual(t, np.zeros((3, 3)))
        for t in tc.zeroed(1e-5):
            self.assertArrayEqual(t, 1e-4 * np.eye(3))
        self.list_based_function_check("zeroed", tc)
        self.list_based_function_check("zeroed", tc, tol=1e-5)

        # transform
        symm_op = SymmOp.from_axis_angle_and_translation([0, 0, 1], 30, False,
                                                         [0, 0, 1])
        self.list_based_function_check("transform",
                                       self.seq_tc,
                                       symm_op=symm_op)

        # symmetrized
        self.list_based_function_check("symmetrized", self.seq_tc)

        # rotation
        a = 3.14 * 42.5 / 180
        rotation = SquareTensor([[math.cos(a), 0, math.sin(a)], [0, 1, 0],
                                 [-math.sin(a), 0,
                                  math.cos(a)]])
        self.list_based_function_check("rotate",
                                       self.diff_rank,
                                       matrix=rotation)

        # is_symmetric
        self.assertFalse(self.seq_tc.is_symmetric())
        self.assertTrue(self.diff_rank.is_symmetric())

        # fit_to_structure
        self.list_based_function_check("fit_to_structure", self.diff_rank,
                                       self.struct)
        self.list_based_function_check("fit_to_structure", self.seq_tc,
                                       self.struct)

        # fit_to_structure
        self.list_based_function_check("fit_to_structure", self.diff_rank,
                                       self.struct)
        self.list_based_function_check("fit_to_structure", self.seq_tc,
                                       self.struct)

        # voigt
        self.list_based_function_check("voigt", self.diff_rank)

        # is_voigt_symmetric
        self.assertTrue(self.diff_rank.is_voigt_symmetric())
        self.assertFalse(self.seq_tc.is_voigt_symmetric())

        # Convert to ieee
        for entry in self.ieee_data[:2]:
            entry["xtal"]
            tc = TensorCollection([entry["original_tensor"]] * 3)
            struct = entry["structure"]
            self.list_based_function_check("convert_to_ieee", tc, struct)

        # from_voigt
        tc_input = [t for t in np.random.random((3, 6, 6))]
        tc = TensorCollection.from_voigt(tc_input)
        for t_input, t in zip(tc_input, tc):
            self.assertArrayAlmostEqual(Tensor.from_voigt(t_input), t)

    def test_serialization(self):
        # Test base serialize-deserialize
        d = self.seq_tc.as_dict()
        new = TensorCollection.from_dict(d)
        for t, t_new in zip(self.seq_tc, new):
            self.assertArrayAlmostEqual(t, t_new)

        # Suppress vsym warnings and test voigt
        with warnings.catch_warnings(record=True):
            vsym = self.rand_tc.voigt_symmetrized
            d = vsym.as_dict(voigt=True)
            new_vsym = TensorCollection.from_dict(d)
            for t, t_new in zip(vsym, new_vsym):
                self.assertArrayAlmostEqual(t, t_new)