Exemplo n.º 1
0
class SimplexTest(unittest.TestCase):

    def setUp(self):
        coords = []
        coords.append([0, 0, 0])
        coords.append([0, 1, 0])
        coords.append([0, 0, 1])
        coords.append([1, 0, 0])
        self.simplex = Simplex(coords)

    def test_in_simplex(self):
        self.assertTrue(self.simplex.in_simplex([0.1, 0.1, 0.1]))
        self.assertFalse(self.simplex.in_simplex([0.6, 0.6, 0.6]))
        for i in range(10):
            coord = np.random.random_sample(size=3) / 3
            self.assertTrue(self.simplex.in_simplex(coord))

    def test_2dtriangle(self):
        s = Simplex([[0, 1], [1, 1], [1, 0]])
        np.testing.assert_almost_equal(s.bary_coords([0.5, 0.5]), [0.5, 0, 0.5])
        np.testing.assert_almost_equal(s.bary_coords([0.5, 1]), [0.5, 0.5, 0])
        np.testing.assert_almost_equal(s.bary_coords([0.5, 0.75]), [0.5, 0.25, 0.25])
        np.testing.assert_almost_equal(s.bary_coords([0.75, 0.75]), [0.25, 0.5, 0.25])

        s = Simplex([[1, 1], [1, 0]])
        self.assertRaises(ValueError, s.bary_coords, [0.5, 0.5])

    def test_volume(self):
        # Should be value of a right tetrahedron.
        self.assertAlmostEqual(self.simplex.volume, 1/6)
Exemplo n.º 2
0
class SimplexTest(unittest.TestCase):
    def setUp(self):
        coords = []
        coords.append([0, 0, 0])
        coords.append([0, 1, 0])
        coords.append([0, 0, 1])
        coords.append([1, 0, 0])
        self.simplex = Simplex(coords)

    def test_in_simplex(self):
        self.assertTrue(self.simplex.in_simplex([0.1, 0.1, 0.1]))
        self.assertFalse(self.simplex.in_simplex([0.6, 0.6, 0.6]))
        for i in range(10):
            coord = np.random.random_sample(size=3) / 3
            self.assertTrue(self.simplex.in_simplex(coord))

    def test_2dtriangle(self):
        s = Simplex([[0, 1], [1, 1], [1, 0]])
        np.testing.assert_almost_equal(s.bary_coords([0.5, 0.5]),
                                       [0.5, 0, 0.5])
        np.testing.assert_almost_equal(s.bary_coords([0.5, 1]), [0.5, 0.5, 0])
        np.testing.assert_almost_equal(s.bary_coords([0.5, 0.75]),
                                       [0.5, 0.25, 0.25])
        np.testing.assert_almost_equal(s.bary_coords([0.75, 0.75]),
                                       [0.25, 0.5, 0.25])

        s = Simplex([[1, 1], [1, 0]])
        self.assertRaises(ValueError, s.bary_coords, [0.5, 0.5])

    def test_volume(self):
        # Should be value of a right tetrahedron.
        self.assertAlmostEqual(self.simplex.volume, 1 / 6)
Exemplo n.º 3
0
class SimplexTest(unittest.TestCase):
    def setUp(self):
        coords = []
        coords.append([0, 0, 0])
        coords.append([0, 1, 0])
        coords.append([0, 0, 1])
        coords.append([1, 0, 0])
        self.simplex = Simplex(coords)

    def test_in_simplex(self):
        self.assertTrue(self.simplex.in_simplex([0.1, 0.1, 0.1]))
        self.assertFalse(self.simplex.in_simplex([0.6, 0.6, 0.6]))
        for i in xrange(10):
            coord = np.random.random_sample(size=(3)) / 3
            self.assertTrue(self.simplex.in_simplex(coord))
Exemplo n.º 4
0
class SimplexTest(unittest.TestCase):

    def setUp(self):
        coords = []
        coords.append([0, 0, 0])
        coords.append([0, 1, 0])
        coords.append([0, 0, 1])
        coords.append([1, 0, 0])
        self.simplex = Simplex(coords)

    def test_in_simplex(self):
        self.assertTrue(self.simplex.in_simplex([0.1, 0.1, 0.1]))
        self.assertFalse(self.simplex.in_simplex([0.6, 0.6, 0.6]))
        for i in xrange(10):
            coord = np.random.random_sample(size=(3)) / 3
            self.assertTrue(self.simplex.in_simplex(coord))
Exemplo n.º 5
0
    def _in_facet(self, facet, comp):
        """
        Checks if a composition is in a facet.

        Args:
            facet: facet to test.
            comp: Composition to test.
        """
        els = self._pd.elements
        dim = len(els)
        if dim > 1:
            coords = self._pd.qhull_data[facet, :-1]
            simplex = Simplex(coords)
            comp_point = [comp.get_atomic_fraction(e) for e in els[1:]]
            return simplex.in_simplex(comp_point, PDAnalyzer.numerical_tol)
        else:
            return True
Exemplo n.º 6
0
    def _in_facet(self, facet, comp):
        """
        Checks if a composition is in a facet.

        Args:
            facet: facet to test.
            comp: Composition to test.
        """
        els = self._pd.elements
        dim = len(els)
        if dim > 1:
            coords = self._pd.qhull_data[facet, :-1]
            simplex = Simplex(coords)
            comp_point = [comp.get_atomic_fraction(e) for e in els[1:]]
            return simplex.in_simplex(comp_point, PDAnalyzer.numerical_tol)
        else:
            return True
Exemplo n.º 7
0
    def _in_facet(self, facet, entry):
        """
        Checks if a Pourbaix Entry is in a facet.

        Args:
            facet: facet to test.
            entry: Pourbaix Entry to test.
        """
        dim = len(self._keys)
        if dim > 1:
            coords = [np.array(self._pd.qhull_data[facet[i]][0:dim - 1])
                      for i in xrange(len(facet))]
            simplex = Simplex(coords)
            comp_point = [entry.npH, entry.nPhi]
            return simplex.in_simplex(comp_point,
                                      PourbaixAnalyzer.numerical_tol)
        else:
            return True
Exemplo n.º 8
0
    def _in_facet(self, facet, entry):
        """
        Checks if a Pourbaix Entry is in a facet.

        Args:
            facet: facet to test.
            entry: Pourbaix Entry to test.
        """
        dim = len(self._keys)
        if dim > 1:
            coords = [np.array(self._pd.qhull_data[facet[i]][0:dim - 1])
                      for i in range(len(facet))]
            simplex = Simplex(coords)
            comp_point = [entry.npH, entry.nPhi]
            return simplex.in_simplex(comp_point,
                                      PourbaixAnalyzer.numerical_tol)
        else:
            return True
Exemplo n.º 9
0
    def _in_facet(self, facet, comp):
        """
        Checks if a composition is in a facet.

        Args:
            facet:
                facet to test.
            comp:
                Composition to test.
        """
        dim = len(self._pd.elements)
        if dim > 1:
            coords = [np.array(self._pd.qhull_data[facet[i]][0:dim - 1])
                      for i in xrange(len(facet))]
            simplex = Simplex(coords)
            comp_point = [comp.get_atomic_fraction(self._pd.elements[i])
                          for i in xrange(1, len(self._pd.elements))]
            return simplex.in_simplex(comp_point, PDAnalyzer.numerical_tol)
        else:
            return True
Exemplo n.º 10
0
    def _in_facet(self, facet, comp):
        """
        Checks if a composition is in a facet.

        Args:
            facet:
                facet to test.
            comp:
                Composition to test.
        """
        els = self._pd.elements
        dim = len(els)
        if dim > 1:
            coords = [
                np.array(self._pd.qhull_data[facet[i]][0:dim - 1])
                for i in xrange(len(facet))
            ]
            simplex = Simplex(coords)
            comp_point = [
                comp.get_atomic_fraction(els[i]) for i in xrange(1, len(els))
            ]
            return simplex.in_simplex(comp_point, PDAnalyzer.numerical_tol)
        else:
            return True