def test_delete(self): from aiida.orm.group import Group from aiida.common.exceptions import NotExistent n = Node().store() g = Group(name='testgroup3', description='some other desc') g.store() gcopy = Group.get(name='testgroup3') self.assertEquals(g.uuid, gcopy.uuid) g.add_nodes(n) self.assertEquals(len(g.nodes), 1) g.delete() with self.assertRaises(NotExistent): # The group does not exist anymore Group.get(name='testgroup3') # I should be able to restore it g.store() # Now, however, by deleting and recreating it, I lost the elements self.assertEquals(len(g.nodes), 0) self.assertEquals(g.name, 'testgroup3') self.assertEquals(g.description, 'some other desc') self.assertTrue(g.is_user_defined) # To avoid to find it in further tests g.delete()
def create_cif_data(cls): with tempfile.NamedTemporaryFile() as f: filename = f.name f.write(cls.valid_sample_cif_str) f.flush() a = CifData(file=filename, source={ 'version': '1234', 'db_name': 'COD', 'id': '0000001' }) a.store() g_ne = Group(name='non_empty_group') g_ne.store() g_ne.add_nodes(a) g_e = Group(name='empty_group') g_e.store() return { TestVerdiDataListable.NODE_ID_STR: a.id, TestVerdiDataListable.NON_EMPTY_GROUP_ID_STR: g_ne.id, TestVerdiDataListable.EMPTY_GROUP_ID_STR: g_e.id }
def test_name_desc(self): g = Group(name='testgroup2', description='some desc') self.assertEquals(g.name, 'testgroup2') self.assertEquals(g.description, 'some desc') self.assertTrue(g.is_user_defined) g.store() # Same checks after storing self.assertEquals(g.name, 'testgroup2') self.assertTrue(g.is_user_defined) self.assertEquals(g.description, 'some desc') # To avoid to find it in further tests g.delete()
def create_structure_data(): from aiida.orm.data.structure import StructureData, Site, Kind from aiida.orm.group import Group alat = 4. # angstrom cell = [ [ alat, 0., 0., ], [ 0., alat, 0., ], [ 0., 0., alat, ], ] # BaTiO3 cubic structure struc = StructureData(cell=cell) struc.append_atom(position=(0., 0., 0.), symbols='Ba') struc.append_atom(position=(alat / 2., alat / 2., alat / 2.), symbols='Ti') struc.append_atom(position=(alat / 2., alat / 2., 0.), symbols='O') struc.append_atom(position=(alat / 2., 0., alat / 2.), symbols='O') struc.append_atom(position=(0., alat / 2., alat / 2.), symbols='O') struc.store() # Create 2 groups and add the data to one of them g_ne = Group(name='non_empty_group') g_ne.store() g_ne.add_nodes(struc) g_e = Group(name='empty_group') g_e.store() return { TestVerdiDataListable.NODE_ID_STR: struc.id, TestVerdiDataListable.NON_EMPTY_GROUP_ID_STR: g_ne.id, TestVerdiDataListable.EMPTY_GROUP_ID_STR: g_e.id }
def load_example_structures(): """ Read input structures into the database Structures are read from subfolder "example-structures" and stored in the group "example-structures". :return: group of available structures """ from aiida.orm.group import Group try: group = Group.get(name=group_name) except NotExistent: import glob import os from ase.io import read from aiida.orm.data.structure import StructureData paths = glob.glob(group_name + '/*.cif') structure_nodes = [] for path in paths: fname = os.path.basename(path) name = os.path.splitext(fname)[0] structure = StructureData(ase=read(path)) if "ML" in name: # surface normal of monolayers should be oriented along z structure.set_pbc([True, True, False]) else: structure.set_pbc([True, True, True]) structure.label = name print("Storing {} in database".format(name)) structure.store() structure_nodes.append(structure) group = Group(name=group_name) group.store() group.description = "\ Set of atomic structures used by examples for AiiDA plugins of different codes" group.add_nodes(structure_nodes) return group
def test_add_nodes(self): """ Test different ways of adding nodes """ from aiida.orm.group import Group n1 = Node().store() n2 = Node().store() n3 = Node().store() n4 = Node().store() n5 = Node().store() n6 = Node().store() n7 = Node().store() n8 = Node().store() g = Group(name='test_adding_nodes') g.store() # Single node g.add_nodes(n1) # List of nodes g.add_nodes([n2, n3]) # Single DbNode g.add_nodes(n4.dbnode) # List of DbNodes g.add_nodes([n5.dbnode, n6.dbnode]) # List of Nodes and DbNodes g.add_nodes([n7, n8.dbnode]) # Check self.assertEquals( set([_.pk for _ in [n1, n2, n3, n4, n5, n6, n7, n8]]), set([_.pk for _ in g.nodes])) # Try to add a node that is already present: there should be no problem g.add_nodes(n1) self.assertEquals( set([_.pk for _ in [n1, n2, n3, n4, n5, n6, n7, n8]]), set([_.pk for _ in g.nodes])) # Cleanup g.delete()
def test_creation_from_dbgroup(self): n = Node().store() g = Group(name='testgroup_from_dbgroup') g.store() g.add_nodes(n) dbgroup = g.dbgroup with self.assertRaises(ValueError): # Cannot pass more parameters, even if valid, if # dbgroup is specified Group(dbgroup=dbgroup, name="test") gcopy = Group(dbgroup=dbgroup) self.assertEquals(g.pk, gcopy.pk) self.assertEquals(g.uuid, gcopy.uuid) # To avoid to find it in further tests g.delete()
def test_rename_existing(self): """ Test that renaming to an already existing name is not permitted """ from aiida.orm.group import Group name_group_a = 'group_a' name_group_b = 'group_b' name_group_c = 'group_c' group_a = Group(name=name_group_a, description='I am the Original G') group_a.store() # Before storing everything should be fine group_b = Group(name=name_group_a, description='They will try to rename me') group_c = Group(name=name_group_c, description='They will try to rename me') # Storing for duplicate group name should trigger UniquenessError with self.assertRaises(exceptions.IntegrityError): group_b.store() # Before storing everything should be fine group_c.name = name_group_a # Reverting to unique name before storing group_c.name = name_group_c group_c.store() # After storing name change to existing should raise with self.assertRaises(exceptions.IntegrityError): group_c.name = name_group_a
def test_description(self): """ Test the update of the description both for stored and unstored groups. """ from aiida.orm.group import Group n = Node().store() g1 = Group(name='testgroupdescription1', description="g1").store() g1.add_nodes(n) g2 = Group(name='testgroupdescription2', description="g2") # Preliminary checks self.assertTrue(g1.is_stored) self.assertFalse(g2.is_stored) self.assertEquals(g1.description, "g1") self.assertEquals(g2.description, "g2") # Change g1.description = "new1" g2.description = "new2" # Test that the groups remained in their proper stored state and that # the description was updated self.assertTrue(g1.is_stored) self.assertFalse(g2.is_stored) self.assertEquals(g1.description, "new1") self.assertEquals(g2.description, "new2") # Store g2 and check that the description is OK g2.store() self.assertTrue(g2.is_stored) self.assertEquals(g2.description, "new2") # clean-up g1.delete() g2.delete()
def test_creation(self): from aiida.orm.group import Group n = Node() stored_n = Node().store() with self.assertRaises(ValueError): # No name specified g = Group() g = Group(name='testgroup') with self.assertRaises(ValueError): # Too many parameters g = Group(name='testgroup', not_existing_kwarg=True) with self.assertRaises(ModificationNotAllowed): # g unstored g.add_nodes(n) with self.assertRaises(ModificationNotAllowed): # g unstored g.add_nodes(stored_n) g.store() with self.assertRaises(ValueError): # n unstored g.add_nodes(n) g.add_nodes(stored_n) nodes = list(g.nodes) self.assertEquals(len(nodes), 1) self.assertEquals(nodes[0].pk, stored_n.pk) # To avoid to find it in further tests g.delete()
def setUpClass(cls, *args, **kwargs): """ Create some data needed for the tests """ super(TestVerdiDataCommands, cls).setUpClass() from aiida.orm.user import User from aiida.orm.group import Group # Create a secondary user new_email = "[email protected]" new_user = User(email=new_email) new_user.force_save() # Create a group to add specific data inside g1 = Group(name=cls.group_name) g1.store() cls.group_id = g1.id cls.create_bands_data(cls.cmd_to_nodeid_map, cls.cmd_to_nodeid_map_for_groups, cls.cmd_to_nodeid_map_for_nuser, g1, new_user) cls.create_structure_data(cls.cmd_to_nodeid_map, cls.cmd_to_nodeid_map_for_groups, cls.cmd_to_nodeid_map_for_nuser, g1, new_user) cls.create_cif_data(cls.cmd_to_nodeid_map, cls.cmd_to_nodeid_map_for_groups, cls.cmd_to_nodeid_map_for_nuser, g1, new_user) cls.create_trajectory_data(cls.cmd_to_nodeid_map, cls.cmd_to_nodeid_map_for_groups, cls.cmd_to_nodeid_map_for_nuser, g1, new_user)
def test_rename_existing(self): """ Test that renaming to an already existing name is not permitted """ from aiida.backends.sqlalchemy import get_scoped_session from aiida.orm.group import Group name_group_a = 'group_a' name_group_b = 'group_b' name_group_c = 'group_c' group_a = Group(name=name_group_a, description='I am the Original G') group_a.store() # Before storing everything should be fine group_b = Group(name=name_group_a, description='They will try to rename me') group_c = Group(name=name_group_c, description='They will try to rename me') session = get_scoped_session() # Storing for duplicate group name should trigger Integrity try: session.begin_nested() with self.assertRaises(exceptions.IntegrityError): group_b.store() finally: session.rollback() # Before storing everything should be fine group_c.name = name_group_a # Reverting to unique name before storing group_c.name = name_group_c group_c.store() # After storing name change to existing should raise try: session.begin_nested() with self.assertRaises(exceptions.IntegrityError): group_c.name = name_group_a finally: session.rollback()
def create_trajectory_data(): from aiida.orm.data.array.trajectory import TrajectoryData from aiida.orm.group import Group import numpy # Create a node with two arrays n = TrajectoryData() # I create sample data stepids = numpy.array([60, 70]) times = stepids * 0.01 cells = numpy.array([[[ 2., 0., 0., ], [ 0., 2., 0., ], [ 0., 0., 2., ]], [[ 3., 0., 0., ], [ 0., 3., 0., ], [ 0., 0., 3., ]]]) symbols = numpy.array(['H', 'O', 'C']) positions = numpy.array([[[0., 0., 0.], [0.5, 0.5, 0.5], [1.5, 1.5, 1.5]], [[0., 0., 0.], [0.5, 0.5, 0.5], [1.5, 1.5, 1.5]]]) velocities = numpy.array([[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]], [[0.5, 0.5, 0.5], [0.5, 0.5, 0.5], [-0.5, -0.5, -0.5]]]) # I set the node n.set_trajectory(stepids=stepids, cells=cells, symbols=symbols, positions=positions, times=times, velocities=velocities) n.store() # Create 2 groups and add the data to one of them g_ne = Group(name='non_empty_group') g_ne.store() g_ne.add_nodes(n) g_e = Group(name='empty_group') g_e.store() return { TestVerdiDataListable.NODE_ID_STR: n.id, TestVerdiDataListable.NON_EMPTY_GROUP_ID_STR: g_ne.id, TestVerdiDataListable.EMPTY_GROUP_ID_STR: g_e.id }
def create_structure_bands(): alat = 4. # angstrom cell = [ [ alat, 0., 0., ], [ 0., alat, 0., ], [ 0., 0., alat, ], ] s = StructureData(cell=cell) s.append_atom(position=(0., 0., 0.), symbols='Fe') s.append_atom(position=(alat / 2., alat / 2., alat / 2.), symbols='O') s.store() @wf def connect_structure_bands(structure): alat = 4. cell = np.array([ [alat, 0., 0.], [0., alat, 0.], [0., 0., alat], ]) k = KpointsData() k.set_cell(cell) k.set_kpoints_path([('G', 'M', 2)]) b = BandsData() b.set_kpointsdata(k) b.set_bands([[1.0, 2.0], [3.0, 4.0]]) k.store() b.store() return b b = connect_structure_bands(s) # Create 2 groups and add the data to one of them g_ne = Group(name='non_empty_group') g_ne.store() g_ne.add_nodes(b) g_e = Group(name='empty_group') g_e.store() return { TestVerdiDataListable.NODE_ID_STR: b.id, TestVerdiDataListable.NON_EMPTY_GROUP_ID_STR: g_ne.id, TestVerdiDataListable.EMPTY_GROUP_ID_STR: g_e.id }