def test_primitive_cell_for_cubic_lattice(self):
        with self.assertRaises(ValueError):
            PrimitiveCell.for_cubic_lattice(-1)

        pc = PrimitiveCell.for_cubic_lattice(self.a)
        self.assertIsInstance(pc, PrimitiveCell)
        self.assertEqual(pc.bravais_lattice, BravaisLattice.CUBIC)
        assert_array_equal(pc.p1, (self.a, 0, 0))
        assert_array_equal(pc.p2, (0, self.a, 0))
        assert_array_equal(pc.p3, (0, 0, self.a))
示例#2
0
    def test_primitive_cell_for_cubic_lattice(self):
        with self.assertRaises(ValueError):
            PrimitiveCell.for_cubic_lattice(-1)

        pc = PrimitiveCell.for_cubic_lattice(self.a)
        self.assertIsInstance(pc, PrimitiveCell)
        self.assertEqual(pc.bravais_lattice, BravaisLattice.CUBIC)
        assert_array_equal(pc.p1, (self.a, 0, 0))
        assert_array_equal(pc.p2, (0, self.a, 0))
        assert_array_equal(pc.p3, (0, 0, self.a))
 def setUp(self):
     self.addTypeEqualityFunc(
         DataContainer, partial(compare_data_containers, testcase=self))
     self.addTypeEqualityFunc(
         LatticeNode, partial(compare_lattice_nodes, testcase=self))
     self.primitive_cell = PrimitiveCell.for_cubic_lattice(0.2)
     self.size = (5, 10, 15)
     self.origin = (-2.0, 0.0, 1.0)
     self.container = self.container_factory(
         'my_name', self.primitive_cell, self.size, self.origin)
    def test_lattice_source_name(self):
        # given
        primitive_cell = PrimitiveCell.for_cubic_lattice(0.1)
        lattice = VTKLattice.empty('my_lattice', primitive_cell,
                                   (5, 10, 12), (0, 0, 0))

        # when
        source = self.tested_class(cuds=lattice)

        # then
        self.assertEqual(source.name, 'my_lattice (CUDS Lattice)')
    def test_source_from_a_vtk_lattice(self):
        # given
        primitive_cell = PrimitiveCell.for_cubic_lattice(0.1)
        lattice = VTKLattice.empty(
            'test', primitive_cell, (5, 10, 12), (0, 0, 0))

        # when
        source = self.tested_class(cuds=lattice)

        # then
        self.assertIs(source._vtk_cuds, lattice)
        self.assertIs(source.data, lattice.data_set)
示例#6
0
def make_cubic_lattice(name, h, size, origin=(0, 0, 0)):
    """Create and return a 3D cubic lattice.

    Parameters
    ----------
    name : str
    h : float
        lattice spacing
    size : int[3]
        Number of lattice nodes in each axis direction.
    origin : float[3], default value = (0, 0, 0)
        lattice origin

    Returns
    -------
    lattice : Lattice
        A reference to a Lattice object.
    """
    pc = PrimitiveCell.for_cubic_lattice(h)
    return Lattice(name, pc, size, origin)
    def test_version(self):
        filename = os.path.join(self.temp_dir, "test_file.cuds")
        group_name = "dummy_component_name"
        with tables.open_file(filename, "w") as handle:
            group = handle.create_group(handle.root, group_name)

            # given/when
            H5Lattice.create_new(group, PrimitiveCell.for_cubic_lattice(0.2), size=(5, 10, 15), origin=(-2, 0, 1))

            # then
            self.assertTrue(isinstance(group._v_attrs.cuds_version, int))

        # when
        with tables.open_file(filename, "a") as handle:
            handle.get_node("/{}".format(group_name))._v_attrs.cuds_version = -1

        # then
        with tables.open_file(filename, "a") as handle:
            with self.assertRaises(ValueError):
                H5Lattice(handle.get_node("/" + group_name))
示例#8
0
    def test_version(self):
        filename = os.path.join(self.temp_dir, 'test_file.cuds')
        group_name = "dummy_component_name"
        with tables.open_file(filename, 'w') as handle:
            group = handle.create_group(handle.root, group_name)

            # given/when
            H5Lattice.create_new(group,
                                 PrimitiveCell.for_cubic_lattice(0.2),
                                 size=(5, 10, 15),
                                 origin=(-2, 0, 1))

            # then
            self.assertTrue(isinstance(group._v_attrs.cuds_version, int))

        # when
        with tables.open_file(filename, 'a') as handle:
            handle.get_node(
                "/{}".format(group_name))._v_attrs.cuds_version = -1

        # then
        with tables.open_file(filename, 'a') as handle:
            with self.assertRaises(ValueError):
                H5Lattice(handle.get_node("/" + group_name))
import numpy

from simphony.core.cuba import CUBA
from simphony.cuds.primitive_cell import PrimitiveCell
from simphony.visualisation import mayavi_tools

cubic = mayavi_tools.VTKLattice.empty(
    "test", PrimitiveCell.for_cubic_lattice(0.1),
    (5, 10, 12), (0, 0, 0))

lattice = cubic

new_nodes = []
for node in lattice.iter(item_type=CUBA.NODE):
    index = numpy.array(node.index) + 1.0
    node.data[CUBA.TEMPERATURE] = numpy.prod(index)
    new_nodes.append(node)

lattice.update(new_nodes)


if __name__ == '__main__':

    # Visualise the Lattice object
    mayavi_tools.show(lattice)
示例#10
0
    def from_dataset(cls, name, data_set, data=None):
        """ Create a new Lattice and try to guess the ``primitive_cell``

        Parameters
        ----------
        name : str

        data_set : tvtk.ImageData or tvtk.PolyData
            The dataset to wrap in the CUDS api.  If it is a PolyData,
            the points are assumed to be arranged in C-contiguous order

        data : DataContainer
            The data attribute to attach to the container. Default is None.

        Returns
        -------
        lattice : VTKLattice

        Raises
        ------
        TypeError :
            If data_set is not either tvtk.ImageData or tvtk.PolyData

        IndexError:
            If the lattice nodes are not arranged in C-contiguous order
        """
        if isinstance(data_set, tvtk.ImageData):
            spacing = data_set.spacing
            unique_spacing = numpy.unique(spacing)
            if len(unique_spacing) == 1:
                primitive_cell = PrimitiveCell.for_cubic_lattice(spacing[0])
            elif len(unique_spacing) == 2:
                a, c = unique_spacing
                if sum(spacing == a) == 2:
                    primitive_cell = PrimitiveCell.for_tetragonal_lattice(a, c)
                else:
                    primitive_cell = PrimitiveCell.for_tetragonal_lattice(c, a)
            else:
                factory = PrimitiveCell.for_orthorhombic_lattice
                primitive_cell = factory(*spacing)

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

        if not isinstance(data_set, tvtk.PolyData):
            # Not ImageData nor PolyData
            message = 'Cannot convert {} to a cuds Lattice'
            raise TypeError(message.format(type(data_set)))

        # data_set is an instance of tvtk.PolyData
        points = data_set.points.to_array()

        # Assumed C-contiguous order of points
        p1, p2, p3 = guess_primitive_vectors(points)

        # This will raise a TypeError if no bravais lattice type matches
        bravais_lattice = find_lattice_type(p1, p2, p3)

        primitive_cell = PrimitiveCell(p1, p2, p3, bravais_lattice)

        return cls(name=name, primitive_cell=primitive_cell,
                   data=data, data_set=data_set)
    '''
    # rotate x-y for angle1 about z
    xy_rot = numpy.array([[numpy.cos(angle1), numpy.sin(angle1), 0],
                          [numpy.sin(-angle1), numpy.cos(angle1), 0],
                          [0, 0, 1]])
    # rotate y-z for angle2 about x
    yz_rot = numpy.array([[1, 0, 0],
                          [0, numpy.cos(angle2), numpy.sin(angle2)],
                          [0, numpy.sin(-angle2), numpy.cos(angle2)]])
    # rotate x-y, then y-z
    xyz_rot = numpy.inner(xy_rot, yz_rot)
    return tuple(numpy.inner(xyz_rot, p) for p in (pc.p1, pc.p2, pc.p3))


# a cubic lattice on a rotated coordinates represented using PolyData
pcs = rotate_primitive_cell(PrimitiveCell.for_cubic_lattice(1.))
datasets.append(create_polydata_from_pc(*pcs))

# BCC lattice in a rotated coor. sys.
pcs = rotate_primitive_cell(PrimitiveCell.for_body_centered_cubic_lattice(1.))
datasets.append(create_polydata_from_pc(*pcs))

# FCC lattice in a rotated coor. sys.
pcs = rotate_primitive_cell(PrimitiveCell.for_face_centered_cubic_lattice(1.))
datasets.append(create_polydata_from_pc(*pcs))

# rhombohedral lattice in a rotated coor. sys.
pcs = rotate_primitive_cell(PrimitiveCell.for_rhombohedral_lattice(1., 1.))
datasets.append(create_polydata_from_pc(*pcs))

# tetragonal lattice in a rotated coor. sys.