def test_changing_tolerance_lattice_type(self, lattices):
        for lattice_type, primitive_cell in lattices.items():
            # double precision (numpy.float64)
            p1, p2, p3 = self._get_primitive_vectors(primitive_cell)

            # given perturbation
            p1[0] += 1.e-4

            # when
            tolerance = 1.e-6

            # then
            actual = lattice_tools.is_bravais_lattice_consistent(p1, p2, p3,
                                                                 lattice_type,
                                                                 tolerance)
            self.assertFalse(actual)

            # when
            tolerance = 1.e-3

            # then
            actual = lattice_tools.is_bravais_lattice_consistent(p1, p2, p3,
                                                                 lattice_type,
                                                                 tolerance)
            self.assertTrue(actual)
    def test_exception_zero_length_vectors(self):
        # given
        p1, p2, p3 = (0., 1., 0.), (0., 0., 0.), (1., 0., 0.)

        # then
        for bravais_lattice in BravaisLattice:
            with self.assertRaises(ValueError):
                lattice_tools.is_bravais_lattice_consistent(
                    p1, p2, p3, bravais_lattice)
    def test_exception_for_vectors_with_inf(self):
        # given
        p1, p2, p3 = ((numpy.inf, 0., 0.), ) * 3

        # then
        for bravais_lattice in BravaisLattice:
            with self.assertRaises(ValueError):
                lattice_tools.is_bravais_lattice_consistent(
                    p1, p2, p3, bravais_lattice)
    def test_exception_zero_length_vectors(self):
        # given
        p1, p2, p3 = (0., 1., 0.), (0., 0., 0.), (1., 0., 0.)

        # then
        for bravais_lattice in BravaisLattice:
            with self.assertRaises(ValueError):
                lattice_tools.is_bravais_lattice_consistent(p1, p2, p3,
                                                            bravais_lattice)
    def test_exception_for_vectors_with_inf(self):
        # given
        p1, p2, p3 = ((numpy.inf, 0., 0.),)*3

        # then
        for bravais_lattice in BravaisLattice:
            with self.assertRaises(ValueError):
                lattice_tools.is_bravais_lattice_consistent(p1, p2, p3,
                                                            bravais_lattice)
    def test_incompatible_lattice_type(self):
        ''' Test if some specific lattices are incompatible with some others

        e.g. a strict BravaisLattice.TRICLINIC lattice
        cannot be considered as any other lattice of higher symmetry
        All other lattices are triclinic in its loose definition
        '''
        lattices = builder(get_specific_primitive_cell_factories()).example()

        for bravais_lattice, primitive_cell in lattices.items():
            # given
            aligned_vectors = self._get_primitive_vectors(primitive_cell)
            p1, p2, p3 = rotate_permute_flip(aligned_vectors)

            # when
            exclusives = (set(BravaisLattice) -
                          set(specific_map2_general[bravais_lattice]) -
                          set([bravais_lattice, BravaisLattice.TRICLINIC]))

            # then
            # bravais_lattice cannot be compatible with lattice
            # types in `exclusive`
            msg = "Expected {!r} not be considered as {!r}"
            for exclusive in exclusives:
                self.assertFalse(
                    lattice_tools.is_bravais_lattice_consistent(
                        p1, p2, p3, exclusive),
                    msg.format(bravais_lattice, exclusive))
    def test_incompatible_lattice_type(self):
        ''' Test if some specific lattices are incompatible with some others

        e.g. a strict BravaisLattice.TRICLINIC lattice
        cannot be considered as any other lattice of higher symmetry
        All other lattices are triclinic in its loose definition
        '''
        lattices = builder(get_specific_primitive_cell_factories()).example()

        for bravais_lattice, primitive_cell in lattices.items():
            # given
            aligned_vectors = self._get_primitive_vectors(primitive_cell)
            p1, p2, p3 = rotate_permute_flip(aligned_vectors)

            # when
            exclusives = (set(BravaisLattice) -
                          set(specific_map2_general[bravais_lattice]) -
                          set([bravais_lattice, BravaisLattice.TRICLINIC]))

            # then
            # bravais_lattice cannot be compatible with lattice
            # types in `exclusive`
            msg = "Expected {!r} not be considered as {!r}"
            for exclusive in exclusives:
                self.assertFalse(
                    lattice_tools.is_bravais_lattice_consistent(p1, p2, p3,
                                                                exclusive),
                    msg.format(bravais_lattice, exclusive))
    def test_changing_tolerance_lattice_type(self, lattices):
        for lattice_type, primitive_cell in lattices.items():
            # double precision (numpy.float64)
            p1, p2, p3 = self._get_primitive_vectors(primitive_cell)

            # given perturbation
            p1[0] += 1.e-4

            # when
            tolerance = 1.e-6

            # then
            actual = lattice_tools.is_bravais_lattice_consistent(
                p1, p2, p3, lattice_type, tolerance)
            self.assertFalse(actual)

            # when
            tolerance = 1.e-3

            # then
            actual = lattice_tools.is_bravais_lattice_consistent(
                p1, p2, p3, lattice_type, tolerance)
            self.assertTrue(actual)
    def test_subtype_of_general_lattices(self, lattices):
        ''' Test if more symmetric lattices are part of more general lattices
        '''
        for specific, primitive_cell in lattices.items():
            # given
            aligned_vectors = self._get_primitive_vectors(primitive_cell)
            p1, p2, p3 = rotate_permute_flip(aligned_vectors)

            # then
            msg = "Expected {!r} to be a symmetric case of {!r}. False."
            for general in specific_map2_general[specific]:
                self.assertTrue(
                    lattice_tools.is_bravais_lattice_consistent(
                        p1, p2, p3, general), msg.format(specific, general))
Exemplo n.º 10
0
    def test_subtype_of_general_lattices(self, lattices):
        ''' Test if more symmetric lattices are part of more general lattices
        '''
        for specific, primitive_cell in lattices.items():
            # given
            aligned_vectors = self._get_primitive_vectors(primitive_cell)
            p1, p2, p3 = rotate_permute_flip(aligned_vectors)

            # then
            msg = "Expected {!r} to be a symmetric case of {!r}. False."
            for general in specific_map2_general[specific]:
                self.assertTrue(
                    lattice_tools.is_bravais_lattice_consistent(p1, p2, p3,
                                                                general),
                    msg.format(specific, general))
Exemplo n.º 11
0
    def from_lattice(cls, lattice, node_keys=None):
        """ Create a new Lattice from the provided one.

        Parameters
        ----------
        lattice : simphony.cuds.lattice.Lattice

        node_keys : list
            A list of point CUBA keys that we want to copy, and only those.
            If None, all available and compatible keys will be copied.

        Returns
        -------
        lattice : VTKLattice

        Raises
        ------
        ValueError
            - if bravais_lattice attribute of the primitive cell indicates
              a cubic/tetragonal/orthorhombic lattice but the primitive vectors
              are inconsistent with this attribute
            - if bravais_lattice is not a member of BravaisLattice
        """
        origin = lattice.origin
        primitive_cell = lattice.primitive_cell
        lattice_type = primitive_cell.bravais_lattice
        size = lattice.size
        name = lattice.name
        node_data = CUBADataAccumulator(node_keys)
        data = lattice.data

        if lattice_type in (
                BravaisLattice.CUBIC, BravaisLattice.TETRAGONAL,
                BravaisLattice.ORTHORHOMBIC):
            # Cubic/Tetragonal/Orthorhombic lattice can be represented
            # by tvtk.ImageData, which is more efficient than PolyData
            # But we should make sure the primitive vectors do describe
            # such a regular lattice type as PrimitiveCell does not
            # perform this check
            consistent = is_bravais_lattice_consistent(primitive_cell.p1,
                                                       primitive_cell.p2,
                                                       primitive_cell.p3,
                                                       lattice_type)
            if not consistent:
                message = ("primitive vectors are not consistent with the "
                           "bravais_lattice: {}")
                raise ValueError(message.format(str(lattice_type)))

            # Compute the spacing from the primitive cell
            spacing = tuple(map(vector_len, (primitive_cell.p1,
                                             primitive_cell.p2,
                                             primitive_cell.p3)))
            origin = origin
            data_set = tvtk.ImageData(spacing=spacing, origin=origin)
            data_set.extent = 0, size[0] - 1, 0, size[1] - 1, 0, size[2] - 1
            # vtk ravels the point positions in a very weird way
            # this setup has been supported by tests.
            y, z, x = numpy.meshgrid(
                range(size[1]), range(size[2]), range(size[0]))
            indices = izip(x.ravel(), y.ravel(), z.ravel())
        elif lattice_type in BravaisLattice:
            # This includes any other BravaisLattice type that cannot be
            # represented by ImageData.  PolyData is required.
            y, z, x = numpy.meshgrid(
                range(size[1]), range(size[2]), range(size[0]))
            points = numpy.zeros(shape=(x.size, 3), dtype='double')

            # construct points using primitive cells
            for idim in range(3):
                points[:, idim] += primitive_cell.p1[idim]*x.ravel() +\
                    primitive_cell.p2[idim]*y.ravel() +\
                    primitive_cell.p3[idim]*z.ravel()
                points[:, idim] += origin[idim]

            data_set = tvtk.PolyData(points=points)
            indices = izip(x.ravel(), y.ravel(), z.ravel())
        else:
            message = 'Unknown lattice type: {}'.format(lattice_type)
            raise ValueError(message)

        for node in lattice.iter(indices):
            node_data.append(node.data)
        node_data.load_onto_vtk(data_set.point_data)

        return cls(name=name, primitive_cell=primitive_cell, data=data,
                   data_set=data_set)