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
def create_example_particles(): points = array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]], "f") bonds = array([[0, 1], [0, 3], [1, 3, 2]]) temperature = array([10.0, 20.0, 30.0, 40.0]) particles = Particles("test") uids = particles.add_particles( Particle(coordinates=point, data=DataContainer(TEMPERATURE=temperature[index])) for index, point in enumerate(points) ) particles.add_bonds( Bond(particles=[uids[index] for index in indices], data=DataContainer(TEMPERATURE=temperature[index])) for indices in bonds ) return particles
def test_closed_file_not_usable(self): filename = os.path.join(self.temp_dir, 'test.cuds') with closing(H5CUDS.open(filename)) as handle: handle.add_dataset(Mesh(name="test_1")) handle.add_dataset(Particles(name="test_2")) lattice = make_cubic_lattice("test_3", 1.0, (2, 3, 4)) handle.add_dataset(lattice) test_h1 = handle.get_dataset("test_1") test_h2 = handle.get_dataset("test_2") test_h3 = handle.get_dataset("test_3") with self.assertRaises(Exception): handle.get_dataset('test_h1') with self.assertRaises(Exception): test_h1.name = 'foo' with self.assertRaises(Exception): test_h2.name = 'foo' with self.assertRaises(Exception): test_h3.name = 'foo'
def test_with_cuds_particles(self): # given points = [ [0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]] bonds = [[0, 1], [0, 3], [1, 3, 2]] point_temperature = [10., 20., 30., 40.] bond_temperature = [60., 80., 190.] cuds = Particles('test') particle_uids = cuds.add( Particle( coordinates=point, data=DataContainer( TEMPERATURE=point_temperature[index], MASS=index)) for index, point in enumerate(points)) cuds.add( Bond( particles=[particle_uids[uid] for uid in indices], data=DataContainer( TEMPERATURE=bond_temperature[index], MASS=index)) for index, indices in enumerate(bonds)) # when data_set = cuds2vtk(cuds) # then check points self.assertEqual(data_set.GetNumberOfPoints(), 4) coordinates = [ particle.coordinates for particle in cuds.iter( item_type=CUBA.PARTICLE)] vtk_points = [ data_set.GetPoint(index) for index in range(len(particle_uids))] self.assertItemsEqual(vtk_points, coordinates) point_data = data_set.GetPointData() self.assertEqual(point_data.GetNumberOfArrays(), 2) arrays = { point_data.GetArray(index).GetName(): point_data.GetArray(index) for index in range(2)} self.assertItemsEqual(arrays.keys(), ['MASS', 'TEMPERATURE']) mass = vtk_to_numpy(arrays['MASS']) self.assertItemsEqual(mass, range(4)) assert_array_equal( vtk_to_numpy(arrays['TEMPERATURE']), [point_temperature[int(index)] for index in mass]) # then check bonds self.assertEqual(data_set.GetNumberOfCells(), 3) links = [[ particle.coordinates for particle in cuds.iter(bond.particles)] for bond in cuds.iter(item_type=CUBA.BOND)] vtk_lines = data_set.GetLines() lines = [[ data_set.GetPoint(index) for index in line] for line in iter_cells(vtk_lines)] self.assertItemsEqual(lines, links) cell_data = data_set.GetCellData() self.assertEqual(cell_data.GetNumberOfArrays(), 2) arrays = { cell_data.GetArray(index).GetName(): cell_data.GetArray(index) for index in range(2)} self.assertItemsEqual(arrays.keys(), ['MASS', 'TEMPERATURE']) mass = vtk_to_numpy(arrays['MASS']) self.assertItemsEqual(mass, range(3)) assert_array_equal( vtk_to_numpy(arrays['TEMPERATURE']), [bond_temperature[int(index)] for index in mass])
from numpy import array from simphony.cuds import Particles, Particle, Bond from simphony.core.data_container import DataContainer from simphony.core.cuba import CUBA points = array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]], 'f') bonds = array([[0, 1], [0, 3], [1, 3, 2]]) temperature = array([10., 20., 30., 40.]) particles = Particles('test') uids = particles.add_particles( Particle( coordinates=point, data=DataContainer(TEMPERATURE=temperature[index])) for index, point in enumerate(points)) particles.add_bonds( Bond(particles=[uids[index] for index in indices]) for indices in bonds) if __name__ == '__main__': from simphony.visualisation import paraview_tools # Visualise the Particles object paraview_tools.show(particles, select=(CUBA.TEMPERATURE, 'particles'))