def __init__(self):
        self.datasets = {}
        self.time = 0.
        self.CM = DataContainer()
        self.SP = DataContainer()
        self.BC = DataContainer()
        self.CM[CUBA.TIME_STEP] = 1.
        self.CM[CUBA.NUMBER_OF_TIME_STEPS] = 10

        # add lattice with temperature, mass and velocity data
        lattice = make_tetragonal_lattice("lattice", 1., 1.1, (4, 5, 6))
        size = numpy.prod(lattice.size)
        new_node = []
        for node in lattice.iter(item_type=CUBA.NODE):
            index = numpy.prod(numpy.array(node.index)) + 1.0
            node.data[CUBA.TEMPERATURE] = numpy.sin(index/size/2.)*size
            node.data[CUBA.MASS] = index
            new_node.append(node)
        lattice.update(new_node)
        self.datasets["lattice"] = lattice

        # add particles from lattice
        particles = Particles("particles")
        for node in lattice.iter(item_type=CUBA.NODE):
            particle = Particle(coordinates=node.index, data=node.data)
            particle.data[CUBA.VELOCITY] = numpy.random.uniform(-0.1, 0.1, 3)
            particles.add([particle])
        self.datasets["particles"] = particles

        # add mesh
        mesh = Mesh("mesh")
        points = numpy.array([
            [0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1],
            [2, 0, 0], [3, 0, 0], [3, 1, 0], [2, 1, 0],
            [2, 0, 1], [3, 0, 1], [3, 1, 1], [2, 1, 1]], 'f')
        faces = [[2, 7, 11]]
        # add points
        point_iter = (Point(coordinates=point,
                            data=DataContainer(TEMPERATURE=index))
                      for index, point in enumerate(points))
        uids = mesh.add(point_iter)
        face_iter = (Face(points=[uids[index] for index in element],
                          data=DataContainer(TEMPERATURE=index,
                                             VELOCITY=(index, 0., 0.)))
                     for index, element in enumerate(faces))
        mesh.add(face_iter)
        self.datasets["mesh"] = mesh
Example #2
0
def create_example_mesh():
    points = array(
        [
            [0, 0, 0],
            [1, 0, 0],
            [0, 1, 0],
            [0, 0, 1],
            [2, 0, 0],
            [3, 0, 0],
            [3, 1, 0],
            [2, 1, 0],
            [2, 0, 1],
            [3, 0, 1],
            [3, 1, 1],
            [2, 1, 1],
        ],
        "f",
    )

    cells = [[0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10, 11]]  # tetra  # hex

    faces = [[2, 7, 11]]
    edges = [[1, 4], [3, 8]]

    mesh = Mesh("example")

    # add points
    uids = mesh.add_points(
        Point(coordinates=point, data=DataContainer(TEMPERATURE=index)) for index, point in enumerate(points)
    )

    # add edges
    mesh.add_edges(
        Edge(points=[uids[index] for index in element], data=DataContainer(TEMPERATURE=index))
        for index, element in enumerate(edges)
    )

    # add faces
    mesh.add_faces(
        Face(points=[uids[index] for index in element], data=DataContainer(TEMPERATURE=index))
        for index, element in enumerate(faces)
    )

    # add cells
    mesh.add_cells(
        Cell(points=[uids[index] for index in element], data=DataContainer(TEMPERATURE=index))
        for index, element in enumerate(cells)
    )

    return mesh
    def test_with_cuds_mesh(self):
        # given
        points = numpy.array([
            [0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1],
            [2, 0, 0], [3, 0, 0], [3, 1, 0], [2, 1, 0],
            [2, 0, 1], [3, 0, 1], [3, 1, 1], [2, 1, 1]],
            'f')
        cells = [
            [0, 1, 2, 3],  # tetra
            [4, 5, 6, 7, 8, 9, 10, 11]]  # hex
        faces = [[2, 7, 11]]
        edges = [[1, 4], [3, 8]]
        count = itertools.count()
        points = [
            Point(coordinates=point, data=DataContainer(TEMPERATURE=index))
            for index, point in enumerate(points)]

        cuds = Mesh('test')
        cuds.add(points)

        faces = [
            Face(
                points=[points[index].uid for index in face],
                data=DataContainer(TEMPERATURE=next(count)))
            for face in faces]
        edges = [
            Edge(
                points=[points[index].uid for index in edge],
                data=DataContainer(TEMPERATURE=next(count)))
            for edge in edges]
        cells = [
            Cell(
                points=[points[index].uid for index in cell],
                data=DataContainer(TEMPERATURE=next(count)))
            for cell in cells]
        cuds.add(edges)
        cuds.add(faces)
        cuds.add(cells)

        # when
        data_set = cuds2vtk(cuds=cuds)

        # then check points
        self.assertEqual(data_set.GetNumberOfPoints(), 12)

        point_data = data_set.GetPointData()
        self.assertEqual(point_data.GetNumberOfArrays(), 1)
        temperature = point_data.GetArray(0)
        self.assertEqual(temperature.GetName(), 'TEMPERATURE')
        self.assertItemsEqual(
            vtk_to_numpy(temperature), range(12))

        # then check cells
        self.assertEqual(data_set.GetNumberOfCells(), 5)

        cell_data = data_set.GetCellData()
        self.assertEqual(cell_data.GetNumberOfArrays(), 1)
        temperature = cell_data.GetArray(0)
        self.assertEqual(temperature.GetName(), 'TEMPERATURE')
        self.assertItemsEqual(
            vtk_to_numpy(temperature), range(5))

        # For each cell in the container
        # find the corresponding cell in the vtkCellArray and
        # verify that they link to points that have the right coordinates.
        for cell in itertools.chain(
                cuds.iter(item_type=CUBA.EDGE),
                cuds.iter(item_type=CUBA.FACE),
                cuds.iter(item_type=CUBA.CELL)):
            # The temperature value is also the index that the cells
            # are expected to have in the vtkCellArray.
            value = cell.data[CUBA.TEMPERATURE]
            index = numpy.nonzero(vtk_to_numpy(temperature) == value)[0]
            vtk_cell = data_set.GetCell(index)
            ids = vtk_cell.GetPointIds()
            for i, uid in enumerate(cell.points):
                cell_point = cuds.get(uid)
                assert_array_equal(
                    data_set.GetPoint(ids.GetId(i)), cell_point.coordinates)
        [3, 1, 0],
        [2, 1, 0],
        [2, 0, 1],
        [3, 0, 1],
        [3, 1, 1],
        [2, 1, 1],
    ],
    "f",
)

cells = [[0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10, 11]]  # tetra  # hex

faces = [[2, 7, 11]]
edges = [[1, 4], [3, 8]]

mesh = Mesh("example")

# add points
uids = mesh.add_points(
    Point(coordinates=point, data=DataContainer(TEMPERATURE=index)) for index, point in enumerate(points)
)

# add edges
edge_uids = mesh.add_edges(Edge(points=[uids[index] for index in element]) for index, element in enumerate(edges))

# add faces
face_uids = mesh.add_faces(Face(points=[uids[index] for index in element]) for index, element in enumerate(faces))

# add cells
cell_uids = mesh.add_cells(Cell(points=[uids[index] for index in element]) for index, element in enumerate(cells))