def test_add_get_dataset_with_cuba_keys_argument(self): engine = self.engine_factory() items = self.create_dataset_items() reference = self.create_dataset(name='test') expected = self.create_dataset(name='test') # Add some CUBA data for point in [p for p in items if isinstance(p, Point)]: point.data = DataContainer({CUBA.VELOCITY: [1, 0, 0]}) expected.add([point]) point.data = DataContainer( {CUBA.VELOCITY: [1, 0, 0], CUBA.MASS: 1}) reference.add([point]) for edge in [e for e in items if isinstance(e, Edge)]: expected.add([edge]) reference.add([edge]) for face in [f for f in items if isinstance(f, Face)]: expected.add([face]) reference.add([face]) for cell in [c for c in items if isinstance(c, Cell)]: expected.add([cell]) reference.add([cell]) # Store reference dataset along with its data engine.add_dataset(reference, {CUBA.POINT: [CUBA.VELOCITY]}) # Closing and reopening the file engine.close() engine = self.engine_factory() ds = engine.get_dataset('test') self.compare_dataset(ds, expected)
def main(): print(""" Benchmarking various operations between different data containers .. note: Only the relative time taken for each type of container within a section is comparable. """) print('Initialization:') print("dict:", bench(lambda: dict(dict_data))) print("DataContainer:", bench(lambda: DataContainer(dict_data))) print("dict == DataContainer", dict(dict_data) == DataContainer(dict_data)) print() print('Iterations:') print("dict:", bench(lambda: iteration(dict_data))) print("DataContainer:", bench(lambda: iteration(data_container))) print() print('getitem access:') print("dict:", bench(lambda: getitem_access(dict_data, indices))) print("DataContainer:", bench(lambda: getitem_access(data_container, indices))) print("dict == DataContainer", getitem_access(dict_data, indices) == getitem_access(data_container, indices)) # noqa print() print('setitem with CUBA keys:') print("dict:", bench(lambda: setitem_with_CUBA_keys(dict_data))) print("DataContainer:", bench(lambda: setitem_with_CUBA_keys(data_container))) print("dict == DataContainer", setitem_with_CUBA_keys(dict_data) == setitem_with_CUBA_keys( data_container)) # noqa
def test_update_with_a_dictionary(self): container = DataContainer() data = {key: 3 for key in CUBA} container.update(data) self.assertEqual(container, data) for key in container: self.assertIsInstance(key, CUBA)
def test_update_with_a_iterable(self): container = DataContainer() data = [(key, 3) for key in CUBA] container.update(data) # Check that has all the values for key, value in data: self.assertEqual(container[key], value) self.assertIsInstance(key, CUBA)
def test_update_with_keywords(self): container = DataContainer() data = {key: index + 3 for index, key in enumerate(CUBA.__members__)} container.update(**data) self.assertEqual( container, {key: index + 3 for index, key in enumerate(CUBA)}) for key in container: self.assertIsInstance(key, CUBA)
def test_update_with_keywords(self): container = DataContainer() data = {key: index + 3 for index, key in enumerate(CUBA.__members__)} container.update(**data) self.assertEqual(container, {key: index + 3 for index, key in enumerate(CUBA)}) for key in container: self.assertIsInstance(key, CUBA)
def test_compare_data_containers_equal(self): data = create_data_container() expected = create_data_container() # This should pass without a problem compare_data_containers(data, expected, testcase=self) compare_data_containers(DataContainer(), DataContainer(), testcase=self) compare_data_containers({}, DataContainer(), testcase=self)
def test_update_with_keywords_and_iterable(self): container = DataContainer() data = { key: 3 for index, key in enumerate(CUBA.__members__) if key != str(CUBA("POTENTIAL_ENERGY"))[5:]} container.update([(CUBA("POTENTIAL_ENERGY"), 23)], **data) expected = {key: index + 3 for index, key in enumerate(CUBA)} expected[CUBA("POTENTIAL_ENERGY")] = 23 for key in container: self.assertIsInstance(key, CUBA)
def test_update_with_keywords_and_iterable(self): container = DataContainer() data = { key: index + 3 for index, key in enumerate(CUBA.__members__) if key != str(CUBA(10))[5:]} container.update([(CUBA(10), 23)], **data) expected = {key: index + 3 for index, key in enumerate(CUBA)} expected[CUBA(10)] = 23 self.assertDictEqual(container, expected) for key in container: self.assertIsInstance(key, CUBA)
def test_update_with_keywords_and_iterable(self): container = DataContainer() data = { key: 3 for index, key in enumerate(CUBA.__members__) if key != str(CUBA("POTENTIAL_ENERGY"))[5:] } container.update([(CUBA("POTENTIAL_ENERGY"), 23)], **data) expected = {key: index + 3 for index, key in enumerate(CUBA)} expected[CUBA("POTENTIAL_ENERGY")] = 23 for key in container: self.assertIsInstance(key, CUBA)
def generate_particles(smp_particles, smp_conditions, smp_materials, smp_pe): # Define the particle containers. particles = SParticles(name='particles') # Fill the data ( particle ) data = DataContainer() data[CUBA.RADIUS] = 5e-5 particles.add([ SParticle( coordinates=(0.002, 0.004, 0.004), data=DataContainer(data), ) ]) material = api.Material(name='material_' + particles.name) materialData = material.data materialData[CUBA.DENSITY] = 950.0 materialData[CUBA.YOUNG_MODULUS] = 35e9 materialData[CUBA.POISSON_RATIO] = 0.20 materialData[CUBA.FRICTION_COEFFICIENT] = 0.5773502691896257 # materialData[CUBA.PARTICLE_COHESION] = 0.0 materialData[CUBA.RESTITUTION_COEFFICIENT] = 0.02 materialData[CUBA.ROLLING_FRICTION] = 0.01 # materialData[CUBA.DEM_CONTINUUM_CONSTITUTIVE_LAW_NAME] = \ # "DEM_KDEMFabric" # materialData[CUBA.DEM_DISCONTINUUM_CONSTITUTIVE_LAW_NAME] = \ # "DEM_D_Hertz_viscous_Coulomb" # materialData[CUBA.CONTACT_TAU_ZERO] = 25 # materialData[CUBA.CONTACT_SIGMA_MIN] = 5 # materialData[CUBA.CONTACT_INTERNAL_FRICC] = 1 material.data = materialData particlesData = particles.data particlesData[CUBA.MATERIAL] = material.name particles.data = particlesData # Pack the return objects smp_particles.append(particles) smp_materials.append(material) # Add the datasets that will be used by the wrapper smp_pe.data[CUBA.DATA_SET].append(particles.name) return { 'datasets': smp_particles, 'conditions': smp_conditions, 'materials': smp_materials, 'pe': smp_pe, }
def test_compare_mesh_datasets_equal(self): # given mesh = Mesh(name="test") reference = Mesh(name="test") point_list = create_points_with_id() edge_list = create_edges() face_list = create_faces() cell_list = create_cells() data = DataContainer() mesh.add(point_list) mesh.add(edge_list) mesh.add(face_list) mesh.add(cell_list) reference.add(point_list) reference.add(edge_list) reference.add(face_list) reference.add(cell_list) mesh.data = data reference.data = data # this should pass without problems compare_mesh_datasets(mesh, reference, testcase=self)
def test_simple_particle_custom(self): data = DataContainer() data[CUBA.RADIUS] = 3.0 particle = Particle([20.5, 30.5, 40.5], uuid.UUID(int=33), data) self.assertIsInstance(particle, Particle) self.assertEqual(particle.coordinates, (20.5, 30.5, 40.5)) self.assertEqual(particle.uid, uuid.UUID(int=33)) self.assertEqual(particle.data, data)
def test_compare_particles_datasets_not_equal(self): # given particles = Particles(name="test") reference = Particles(name="test_ref") particle_list = create_particles_with_id() bond_list = create_bonds() data = create_data_container() particles.add(particle_list) reference.add(particle_list) particles.add(bond_list) reference.add(bond_list) particles.data = data reference.data = data # when/then with self.assertRaises(AssertionError): compare_particles_datasets(particles, reference, testcase=self) # given test_particles = create_particles_with_id() particles = Particles(name=reference.name) particles.add(test_particles) particles.add(bond_list) particles.data = data # when/then with self.assertRaises(AssertionError): compare_particles_datasets(particles, reference, testcase=self) # given test_bonds = create_bonds() particles = Particles(name=reference.name) particles.add(particle_list) particles.add(test_bonds) particles.data = data # when/then with self.assertRaises(AssertionError): compare_particles_datasets(particles, reference, testcase=self) # given test_data = DataContainer() particles = Particles(name=reference.name) particles.add(particle_list) particles.add(bond_list) particles.data = test_data # when/then with self.assertRaises(AssertionError): compare_particles_datasets(particles, reference, testcase=self)
def setUp(self): register.OpenFOAMExtension() self.engine = EngineInterface.Internal self.mesh = TestMesh(name="mesh1") self.points = [ Point( (0.0, 0.0, 0.0)), Point( (1.0, 0.0, 0.0)), Point( (1.0, 0.0, 1.0)), Point( (0.0, 0.0, 1.0)), Point( (0.0, 1.0, 0.0)), Point( (1.0, 1.0, 0.0)), Point( (1.0, 1.0, 1.0)), Point( (0.0, 1.0, 1.0)) ] puids = self.mesh.add(self.points) self.faces = [ Face([puids[0], puids[3], puids[7], puids[4]]), Face([puids[1], puids[2], puids[6], puids[5]]), Face([puids[0], puids[1], puids[5], puids[4]]), Face([puids[3], puids[2], puids[6], puids[7]]), Face([puids[0], puids[1], puids[2], puids[3]]), Face([puids[4], puids[5], puids[6], puids[7]]) ] self.fuids = self.mesh.add(self.faces) self.cells = [ Cell(puids, data=DataContainer({CUBA.VELOCITY: [1, 0, 0], CUBA.PRESSURE: 4.0})) ] self.puids = puids self.mesh.add(self.cells) self.boundaries = {"boundary"+str(i): [self.fuids[i]] for i in range(6)} self.mesh._boundaries = self.boundaries self.cuds = CUDS(name='cuds') cfd = Cfd(name='default cfd model') self.cuds.add([cfd, self.mesh])
def test_simple_bond(self): data = DataContainer() data[CUBA.RADIUS] = 2.0 uuids = [uuid.UUID(int=i) for i in range(3)] bond = Bond(uuids, uuid.UUID(int=12), data) self.assertIsInstance(bond, Bond) self.assertEqual(bond.particles, tuple(uuids)) self.assertEqual(bond.uid, uuid.UUID(int=12)) self.assertEqual(bond.particles, tuple(uuids)) self.assertEqual(bond.data, data)
def test_initialization_with_keywords_and_iterable(self): data = { key: 3 for index, key in enumerate(CUBA.__members__) if key != str(CUBA("POTENTIAL_ENERGY"))[5:] } container = DataContainer([(CUBA("POTENTIAL_ENERGY"), 23)], **data) expected = {key: 3 for index, key in enumerate(CUBA)} expected[CUBA("POTENTIAL_ENERGY")] = 23 self.assertDictEqual(container, expected) for key in container: self.assertIsInstance(key, CUBA)
def setUp(self): self.mesh = TestMesh(name="mesh1") self.solver = 'pimpleFoam' self.points = [ Point((0.0, 0.0, 0.0)), Point((1.0, 0.0, 0.0)), Point((1.0, 0.0, 1.0)), Point((0.0, 0.0, 1.0)), Point((0.0, 1.0, 0.0)), Point((1.0, 1.0, 0.0)), Point((1.0, 1.0, 1.0)), Point((0.0, 1.0, 1.0)) ] puids = self.mesh.add(self.points) self.faces = [ Face([puids[0], puids[3], puids[7], puids[4]]), Face([puids[1], puids[2], puids[6], puids[5]]), Face([puids[0], puids[1], puids[5], puids[4]]), Face([puids[3], puids[2], puids[6], puids[7]]), Face([puids[0], puids[1], puids[2], puids[3]]), Face([puids[4], puids[5], puids[6], puids[7]]) ] self.edges = [Edge([puids[0], puids[3]])] self.fuids = self.mesh.add(self.faces) self.cells = [ Cell(puids, data=DataContainer({ CUBA.VELOCITY: [1, 0, 0], CUBA.PRESSURE: 4.0, CUBA.STRAIN_TENSOR: [1, 0, 0, 0, 1, 0, 0, 0, 1], CUBA.STRESS_TENSOR: [1, 0, 0, 0, 1, 0, 0, 0, 1], CUBA.HOMOGENIZED_STRESS_TENSOR: [1, 0, 0, 0, 1, 0, 0, 0, 1] })) ] self.puids = puids self.mesh.add(self.cells) self.boundaries = { "boundary" + str(i): [self.fuids[i]] for i in range(6) } self.mesh._boundaries = self.boundaries
def test_compare_lattice_datasets_not_equal(self): # given lattice = make_cubic_lattice('test', 1.0, (2, 3, 4)) reference = make_cubic_lattice('test_ref', 1.0, ( 2, 3, 4, )) data = create_data_container() lattice.data = data reference.data = data # when/then with self.assertRaises(AssertionError): compare_lattice_datasets(lattice, reference, testcase=self) # given test_data = DataContainer() lattice = make_cubic_lattice('test_ref', 1.0, (2, 3, 4)) lattice.data = test_data # when/then with self.assertRaises(AssertionError): compare_lattice_datasets(lattice, reference, testcase=self) # given lattice = make_cubic_lattice('test', 2.0, (2, 3, 4)) lattice.data = data # when/then with self.assertRaises(AssertionError): compare_lattice_datasets(lattice, reference, testcase=self) # given lattice = make_cubic_lattice('test_ref', 1.0, (4, 6, 8)) lattice.data = data # when/then with self.assertRaises(AssertionError): compare_lattice_datasets(lattice, reference, testcase=self) # given lattice = make_cubic_lattice('test_ref', 1.0, (2, 3, 4), (2.0, 2.0, 2.0)) lattice.data = data # when/then with self.assertRaises(AssertionError): compare_lattice_datasets(lattice, reference, testcase=self)
def test_add_get_dataset_with_cuba_keys_argument(self): engine = self.engine_factory() reference = self.create_dataset(name='test') expected = self.create_dataset(name='test') # Add some CUBA data for node in reference.iter(item_type=CUBA.NODE): node.data = DataContainer({CUBA.NAME: 'test_container'}) expected.update([node]) node.data = DataContainer({CUBA.NAME: 'test_container', CUBA.DENSITY: 2}) reference.update([node]) # Store reference dataset along with its data engine.add_dataset(reference, {CUBA.NODE: [CUBA.NAME]}) # Closing and reopening the file engine.close() engine = self.engine_factory() ds = engine.get_dataset('test') self.compare_dataset(ds, expected)
def test_get_data(self): saved_keys = self.saved_keys data = create_data_container(restrict=saved_keys) data1 = DataContainer(data) key = saved_keys[0] data[key] = dummy_cuba_value(key) + dummy_cuba_value(key) with self.new_table('my_data_table') as table: table.append(data) table.append(data1) with self.open_table('my_data_table') as table: self.assertEqual(len(table), 2) self.assertDataContainersEqual(table[0], data) self.assertDataContainersEqual(table[1], data1)
def test_delete_data(self): saved_keys = self.saved_keys data = create_data_container(restrict=saved_keys) with self.new_table('my_data_table') as table: uid0 = table.append(data) new_data = DataContainer(data) key = saved_keys[0] data[key] = dummy_cuba_value(key) + dummy_cuba_value(key) uid1 = table.append(new_data) with self.open_table('my_data_table', mode='a') as table: del table[uid0] loaded_data = table[uid1] self.assertEqual(len(table), 1) self.assertDataContainersEqual(loaded_data, new_data)
def test_add_get_dataset_with_cuba_keys_argument(self): engine = self.engine_factory() items = self.create_dataset_items() reference = self.create_dataset(name='test') expected = self.create_dataset(name='test') # Add some CUBA data for particle in items: particle.data = DataContainer({CUBA.VELOCITY: [1, 0, 0]}) expected.add([particle]) particle.data = DataContainer( {CUBA.VELOCITY: [1, 0, 0], CUBA.MASS: 1}) reference.add([particle]) # Store reference dataset along with its data engine.add_dataset(reference, {CUBA.PARTICLE: [CUBA.VELOCITY]}) # Closing and reopening the file engine.close() engine = self.engine_factory() ds = engine.get_dataset('test') self.compare_dataset(ds, expected)
def test_delete_data_with_invalid_uid(self): saved_keys = self.saved_keys data = create_data_container(restrict=saved_keys) with self.new_table('my_data_table') as table: uid0 = table.append(data) new_data = DataContainer(data) key = saved_keys[0] data[key] = dummy_cuba_value(key) + dummy_cuba_value(key) table.append(new_data) with self.open_table('my_data_table', mode='a') as table: del table[uid0] with self.assertRaises(KeyError): del table[uuid.uuid4()] with self.assertRaises(KeyError): del table[uid0]
def test_compare_lattice_datasets_equal(self): # given lattice = make_cubic_lattice('test', 1.0, (2, 3, 4)) reference = make_cubic_lattice('test', 1.0, ( 2, 3, 4, )) data = DataContainer() lattice.data = data reference.data = data # this should pass without problems compare_lattice_datasets(lattice, reference, testcase=self)
def test_compare_particles_datasets_equal(self): # given particles = Particles(name="test") reference = Particles(name="test") particle_list = create_particles_with_id() bond_list = create_bonds() data = DataContainer() particles.add(particle_list) reference.add(particle_list) particles.add(bond_list) reference.add(bond_list) particles.data = data reference.data = data # this should pass without problems compare_particles_datasets(particles, reference, testcase=self)
def test_compare_nodes_not_equal(self): # given node = LatticeNode(index=(0, 2, 1), data=create_data_container()) reference = LatticeNode(index=(1, 2, 1), data=create_data_container()) # when/then with self.assertRaises(AssertionError): compare_lattice_nodes(node, reference, testcase=self) # given node = LatticeNode(index=(0, 2, 1), data=DataContainer()) # when/then with self.assertRaises(AssertionError): compare_lattice_nodes(node, reference, testcase=self) # given node = LatticeNode((0, 0, 0)) # when/then with self.assertRaises(AssertionError): compare_lattice_nodes(node, reference, testcase=self)
def test_compare_bonds_not_equal(self): # given particles = [uuid.uuid4(), uuid.uuid4()], bond = Bond(uid=uuid.uuid4(), particles=particles, data=create_data_container()) reference = Bond(uid=uuid.uuid4(), particles=particles, data=create_data_container()) # when/then with self.assertRaises(AssertionError): compare_bonds(bond, reference, testcase=self) # given bond = Bond(uid=reference.uid, particles=particles[-1], data=create_data_container()) # when/then with self.assertRaises(AssertionError): compare_bonds(bond, reference, testcase=self) # given bond = Bond(uid=uuid.uuid4(), particles=particles, data=DataContainer()) # when/then with self.assertRaises(AssertionError): compare_bonds(bond, reference, testcase=self) # given bond = Bond([None]) # when/then with self.assertRaises(AssertionError): compare_bonds(bond, reference, testcase=self)
def test_compare_elements_not_equal(self): # given points = [uuid.uuid4(), uuid.uuid4(), uuid.uuid4()], element = Element(uid=uuid.uuid4(), points=points, data=create_data_container()) reference = Element(uid=uuid.uuid4(), points=points, data=create_data_container()) # when/then with self.assertRaises(AssertionError): compare_elements(element, reference, testcase=self) # given element = Element(uid=reference.uid, points=points[1:], data=create_data_container()) # when/then with self.assertRaises(AssertionError): compare_elements(element, reference, testcase=self) # given element = Element(uid=reference.uid, points=points, data=DataContainer()) # when/then with self.assertRaises(AssertionError): compare_elements(element, reference, testcase=self) # given element = Element(points=[]) # when/then with self.assertRaises(AssertionError): compare_elements(element, reference, testcase=self)
def test_compare_particles_not_equal(self): # given particle = Particle(uid=uuid.uuid4(), coordinates=(10.0, 0.0, 2.0), data=create_data_container()) reference = Particle(uid=uuid.uuid4(), coordinates=(10.0, 0.0, 2.0), data=create_data_container()) # when/then with self.assertRaises(AssertionError): compare_particles(particle, reference, testcase=self) # given particle = Particle(uid=reference.uid, coordinates=(340.0, 0.0, 2.0), data=create_data_container()) # when/then with self.assertRaises(AssertionError): compare_particles(particle, reference, testcase=self) # given particle = Particle(uid=reference.uid, coordinates=(10.0, 0.0, 2.0), data=DataContainer()) # when/then with self.assertRaises(AssertionError): compare_particles(particle, reference, testcase=self) # given particle = Particle() # when/then with self.assertRaises(AssertionError): compare_particles(particle, reference, testcase=self)
def test_compare_points_not_equal(self): # given point = Point(uid=uuid.uuid4(), coordinates=(10.0, 0.0, 2.0), data=create_data_container()) reference = Point(uid=uuid.uuid4(), coordinates=(10.0, 0.0, 2.0), data=create_data_container()) # when/then with self.assertRaises(AssertionError): compare_points(point, reference, testcase=self) # given point = Point(uid=reference.uid, coordinates=(10.0, 30.0, 2.0), data=create_data_container()) # when/then with self.assertRaises(AssertionError): compare_points(point, reference, testcase=self) # given point = Point(uid=reference.uid, coordinates=(10.0, 0.0, 2.0), data=DataContainer()) # when/then with self.assertRaises(AssertionError): compare_points(point, reference, testcase=self) # given point = Point((0, 0, 0)) # when/then with self.assertRaises(AssertionError): compare_points(point, reference, testcase=self)
def test_setitem_with_cuba_key(self): container = DataContainer() container[CUBA("POTENTIAL_ENERGY")] = 29 self.assertIsInstance(container.keys()[0], CUBA) self.assertEqual(container[CUBA("POTENTIAL_ENERGY")], 29)
def test_update_with_a_dictionary_of_strings(self): container = DataContainer() data = {str(key): 3 for key in CUBA} with self.assertRaises(ValueError): container.update(data)
def test_update_with_non_cuba_iterable(self): container = DataContainer() with self.assertRaises(ValueError): container.update([('foo', 5)])
def test_initialization_with_non_cuba_generator(self): generator = (('foo' + str(i), i) for i in range(5)) with self.assertRaises(ValueError): DataContainer(generator)
def test_update_with_non_cuba_dict(self): container = DataContainer() with self.assertRaises(ValueError): container.update({'foo': 5})
def test_update_with_non_cuba_kwards(self): container = DataContainer() with self.assertRaises(ValueError): container.update(bar=5)