def test_inline_export(self): from aiida.orm.data.cif import CifData from aiida.tools.dbexporters.tcod import export_values import tempfile with tempfile.NamedTemporaryFile() as f: f.write(''' data_test _cell_length_a 10 _cell_length_b 10 _cell_length_c 10 _cell_angle_alpha 90 _cell_angle_beta 90 _cell_angle_gamma 90 loop_ _atom_site_label _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z C 0 0 0 O 0.5 0.5 0.5 ''') f.flush() a = CifData(file=f.name) s = a._get_aiida_structure(store=True) val = export_values(s) script = val.first_block()['_tcod_file_contents'][1] function = '_get_aiida_structure_ase_inline' self.assertNotEqual(script.find(function), script.rfind(function))
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 _get_output_nodes(self, output_path, error_path): """ Extracts output nodes from the standard output and standard error files. """ import os from aiida.orm.data.cif import CifData from aiida.orm.data.parameter import ParameterData cif = None if output_path is not None and os.path.getsize(output_path) > 0: cif = CifData(file=output_path) messages = [] if error_path is not None: with open(error_path) as f: content = f.readlines() messages = [x.strip('\n') for x in content] self._check_failed(messages) output_nodes = [] success = True if cif is not None: output_nodes.append(('cif', cif)) else: success = False output_nodes.append( ('messages', ParameterData(dict={'output_messages': messages}))) return success, output_nodes
def _get_output_nodes(self, output_path, error_path): """ Extracts output nodes from the standard output and standard error files. """ from aiida.orm.data.cif import CifData from aiida.orm.data.parameter import ParameterData import os out_folder = self._calc.get_retrieved_node() output_nodes = [] success = False if error_path is not None: with open(error_path) as f: content = f.readlines() content = [x.strip('\n') for x in content] self._check_failed(content) if len(content) > 0: success = True for filename in content: path = os.path.join(out_folder.get_abs_path('.'), filename) output_nodes.append(('cif', CifData(file=path))) if output_path is not None: with open(output_path) as f: content = f.readlines() content = [x.strip('\n') for x in content] output_nodes.append( ('messages', ParameterData(dict={'output_messages': content}))) return success, output_nodes
def get_ase_structure(self): """ :return: ASE structure corresponding to the cif file. """ from aiida.orm.data.cif import CifData import StringIO return CifData.read_cif(StringIO.StringIO(self.get_corrected_cif()))
def get_ase_structure(self): """ :return: ASE structure corresponding to the cif file. """ import StringIO from aiida.orm.data.cif import CifData cif = correct_cif(self.cif) return CifData.read_cif(StringIO.StringIO(cif))
def get_ase_structure(self): """ Returns ASE representation of the CIF. .. note:: To be removed, as it is duplicated in :py:class:`aiida.orm.data.cif.CifData`. """ import StringIO from aiida.orm.data.cif import CifData return CifData.read_cif(StringIO.StringIO(self.cif))
def importfile(filename): """ Import structure into CifData object """ import os from aiida.orm.data.cif import CifData try: node, _ = CifData.get_or_create(os.path.abspath(filename)) echo.echo_success("imported {}".format(str(node))) except ValueError as err: echo.echo_critical(err)
def get_cif_node(self): """ Create a CIF node, that can be used in AiiDA workflow. :return: :py:class:`aiida.orm.data.cif.CifData` object """ from aiida.orm.data.cif import CifData import tempfile with tempfile.NamedTemporaryFile() as f: f.write(self.cif) f.flush() return CifData(file=f.name, source=self.source)
def get_cif_node(self, store=False): """ Creates a CIF node, that can be used in AiiDA workflow. :return: :py:class:`aiida.orm.data.cif.CifData` object """ from aiida.common.utils import md5_file from aiida.orm.data.cif import CifData import tempfile cifnode = None with tempfile.NamedTemporaryFile() as f: f.write(self.cif) f.flush() cifnode = CifData(file=f.name, source=self.source) # Maintaining backwards-compatibility. Parameter 'store' should # be removed in the future, as the new node can be stored later. if store: cifnode.store() return cifnode
def test_resource_validation(self): calc = CiffilterCalculation() calc.use_cif(CifData()) for key in ['num_machines', 'num_mpiprocs_per_machine', 'tot_num_mpiprocs']: with self.assertRaises(FeatureNotAvailable): calc.set_resources({key: 2}) # Inner modification of resource parameters: calc._set_attr('jobresource_params', {key: 2}) with self.assertRaises(FeatureNotAvailable): calc._validate_resources(**calc.get_resources()) calc.set_resources({key: 1})
def get_farthest_cif(): nodes = ParameterData.query() cif_type = CifData._query_type_string depth = models.DbPath.objects.filter( child__in=nodes, parent__type__contains=cif_type).distinct().order_by( '-depth').values_list('depth')[0][0] q = models.DbPath.objects.filter(parent__type__contains=cif_type, child__in=nodes, depth=depth).distinct() res = CifData.query(children__in=nodes, child_paths__in=q).distinct().order_by('ctime') return list(res)
def test_run_workchain(self): """Test running the WorkChain""" from aiida.work.run import run from aiida.orm.data.cif import CifData from aiida_phtools.workflows.dmatrix import DistanceMatrixWorkChain structure = CifData(file=os.path.join(pt.TEST_DIR, 'HKUST-1.cif'), parse_policy='lazy') outputs = run( DistanceMatrixWorkChain, structure=structure, zeopp_code=self.zeopp_code, pore_surface_code=self.pore_surface_code, distance_matrix_code=self.distance_matrix_code, rips_code=self.rips_code, ) print(outputs)
def get_farthest_cif(with_attr=False): nodes = ParameterData.query().with_entities('id') cif_type = CifData._query_type_string depth = (sa.session.query(DbPath.depth).filter( DbPath.child_id.in_(nodes)).join(DbNode, DbPath.parent).filter( DbNode.type.like("%{}%".format(cif_type))).order_by( DbPath.depth.desc()).distinct()[0])[0] q = (DbPath.query.filter(DbPath.child_id.in_(nodes)).join( DbNode, DbPath.parent).filter(DbNode.type.like( "%{}%".format(cif_type))).filter( DbPath.depth == depth).distinct().with_entities(DbPath.id)) res = (CifData.query(children__id__in=nodes, child_paths__id__in=q).distinct().order_by( DbNode.ctime)) if not with_attr: res = res.options(defer(DbNode.attributes), defer(DbNode.extras)) return res.all()
"ExternalPressure": 5e5, }, "Component": [{ "MoleculeName": "methane", "MoleculeDefinition": "TraPPE", "TranslationProbability": 0.5, "ReinsertionProbability": 0.5, "SwapProbability": 1.0, "CreateNumberOfMolecules": 0, }], }) calc.use_parameters(parameters) # framework structure pwd = os.path.dirname(os.path.realpath(__file__)) framework = CifData(file=pwd + '/test_raspa_attach_file/TCC1RS.cif') calc.use_structure(framework) # restart file calc.use_retrieved_parent_folder(load_node(parent_calc).out.retrieved) # resources calc.set_max_wallclock_seconds(30 * 60) # 30 min calc.set_resources({"num_machines": 1, "num_mpiprocs_per_machine": 1}) calc.set_withmpi(False) #calc.set_queue_name("serial") # store and submit calc.store_all() calc.submit()
"Framework": 0, "UnitCells": "1 1 1", "HeliumVoidFraction": 0.149, "ExternalTemperature": 300.0, "ExternalPressure": 5e5, }, "Component": [{ "MoleculeName": "methane", "MoleculeDefinition": "TraPPE", "TranslationProbability": 0.5, "ReinsertionProbability": 0.5, "SwapProbability": 1.0, "CreateNumberOfMolecules": 0, }], } parameters = ParameterData(dict=params_dict) # Additional files pwd = os.path.dirname(os.path.realpath(__file__)) structure = CifData(file=pwd + '/test_raspa_attach_file/TCC1RS.cif') code = test_and_get_code('raspa@deneb', expected_code_type='raspa') submit( RaspaConvergeWorkChain, code=code, structure=structure, parameters=parameters, options=options, _label='MyFirstWokchain', )
from __future__ import absolute_import from __future__ import print_function import os from aiida.common.example_helpers import test_and_get_code from aiida.orm.data.base import Float from aiida.orm.data.cif import CifData from aiida.work.run import submit from aiida_zeopp.workflows import ZeoppBlockPocketsWorkChain probe_radius = 1.8 pwd = os.path.dirname(os.path.realpath(__file__)) print((pwd + "/structure.cif")) structure = CifData(file=pwd + "/structure.cif") # replace 'network@localhost' with your zeo++ AiiDA code code = test_and_get_code('network@localhost', expected_code_type='zeopp.network') future = submit( ZeoppBlockPocketsWorkChain, probe_radius=Float(probe_radius), structure=structure, zeopp_code=code, ) print(future)
""" Example submission of work chain """ import os from aiida_phtools.workflows.dmatrix import DistanceMatrixWorkChain import aiida_phtools.tests as pt import aiida_zeopp.tests as zt import aiida_gudhi.tests as gt from aiida.work.run import submit from aiida.orm.data.cif import CifData from aiida_zeopp.tests import TEST_DIR structure = CifData(file=os.path.join(TEST_DIR, 'HKUST-1.cif'), parse_policy='lazy') outputs = submit( DistanceMatrixWorkChain, structure=structure, zeopp_code=zt.get_code(entry_point='zeopp.network'), pore_surface_code=pt.get_code(entry_point='phtools.surface'), distance_matrix_code=pt.get_code(entry_point='phtools.dmatrix'), rips_code=gt.get_code(entry_point='gudhi.rdm'), )
def test_cif_structure_roundtrip(self): from aiida.tools.dbexporters.tcod import export_cif, export_values from aiida.orm import Code from aiida.orm import JobCalculation from aiida.orm.data.cif import CifData from aiida.orm.data.parameter import ParameterData from aiida.orm.data.upf import UpfData from aiida.orm.data.folder import FolderData from aiida.common.folders import SandboxFolder from aiida.common.datastructures import calc_states import tempfile with tempfile.NamedTemporaryFile() as f: f.write(''' data_test _cell_length_a 10 _cell_length_b 10 _cell_length_c 10 _cell_angle_alpha 90 _cell_angle_beta 90 _cell_angle_gamma 90 loop_ _atom_site_label _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z C 0 0 0 O 0.5 0.5 0.5 ''') f.flush() a = CifData(file=f.name) c = a._get_aiida_structure() c.store() pd = ParameterData() code = Code(local_executable='test.sh') with tempfile.NamedTemporaryFile() as f: f.write("#/bin/bash\n\necho test run\n") f.flush() code.add_path(f.name, 'test.sh') code.store() calc = JobCalculation(computer=self.computer) calc.set_resources({'num_machines': 1, 'num_mpiprocs_per_machine': 1}) calc.add_link_from(code, "code") calc.set_environment_variables({ 'PATH': '/dev/null', 'USER': '******' }) with tempfile.NamedTemporaryFile(prefix="Fe") as f: f.write("<UPF version=\"2.0.1\">\nelement=\"Fe\"\n") f.flush() upf = UpfData(file=f.name) upf.store() calc.add_link_from(upf, "upf") with tempfile.NamedTemporaryFile() as f: f.write("data_test") f.flush() cif = CifData(file=f.name) cif.store() calc.add_link_from(cif, "cif") calc.store() calc._set_state(calc_states.SUBMITTING) with SandboxFolder() as f: calc._store_raw_input_folder(f.abspath) fd = FolderData() with open( fd._get_folder_pathsubfolder.get_abs_path( calc._SCHED_OUTPUT_FILE), 'w') as f: f.write("standard output") f.flush() with open( fd._get_folder_pathsubfolder.get_abs_path( calc._SCHED_ERROR_FILE), 'w') as f: f.write("standard error") f.flush() fd.store() fd.add_link_from(calc, calc._get_linkname_retrieved(), LinkType.CREATE) pd.add_link_from(calc, "calc", LinkType.CREATE) pd.store() with self.assertRaises(ValueError): export_cif(c, parameters=pd) c.add_link_from(calc, "calc", LinkType.CREATE) export_cif(c, parameters=pd) values = export_values(c, parameters=pd) values = values['0'] self.assertEquals(values['_tcod_computation_environment'], ['PATH=/dev/null\nUSER=unknown']) self.assertEquals(values['_tcod_computation_command'], ['cd 1; ./_aiidasubmit.sh'])
def store_structure(self, name, description=None): structure_ase = self.get_ase(self.tmp_folder + '/' + name) if structure_ase is None: return # determine data source if name.endswith('.cif'): source_format = 'CIF' else: source_format = 'ASE' # perform conversion if self.data_format.value == 'CifData': if source_format == 'CIF': from aiida.orm.data.cif import CifData structure_node = CifData(file=self.tmp_folder + '/' + name, scan_type='flex', parse_policy='lazy') else: from aiida.orm.data.cif import CifData structure_node = CifData() structure_node.set_ase(structure_ase) else: # Target format is StructureData from aiida.orm.data.structure import StructureData structure_node = StructureData(ase=structure_ase) #TODO: Figure out whether this is still necessary for StructureData # ensure that tags got correctly translated into kinds for t1, k in zip(structure_ase.get_tags(), structure_node.get_site_kindnames()): t2 = int(k[-1]) if k[-1].isnumeric() else 0 assert t1 == t2 if description is None: structure_node.description = self.get_description( structure_ase, name) else: structure_node.description = description structure_node.label = ".".join(name.split('.')[:-1]) structure_node.store() self.structure_node = structure_node print("Stored in AiiDA: " + repr(structure_node))
def create_cif_data(cls, cmd_to_nodeid_map, cmd_to_nodeid_map_for_groups, cmd_to_nodeid_map_for_nuser, group, new_user): from aiida.orm.data.cif import CifData from aiida.cmdline.commands.data import _Cif import tempfile # Create the CIF data nodes with tempfile.NamedTemporaryFile() as f: f.write(''' data_9012064 _space_group_IT_number 166 _symmetry_space_group_name_H-M 'R -3 m :H' _cell_angle_alpha 90 _cell_angle_beta 90 _cell_angle_gamma 120 _cell_length_a 4.395 _cell_length_b 4.395 _cell_length_c 30.440 _cod_database_code 9012064 loop_ _atom_site_label _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_U_iso_or_equiv Bi 0.00000 0.00000 0.40046 0.02330 Te1 0.00000 0.00000 0.00000 0.01748 Te2 0.00000 0.00000 0.79030 0.01912 ''') f.flush() c1 = CifData(file=f.name) c1.store() c2 = CifData(file=f.name) c2.store() # Keep track of the created objects cmd_to_nodeid_map[_Cif] = [c1.id, c2.id] # Add the second CIF data to the group group.add_nodes([c2]) # Keep track of the id of the node that you added to the group cmd_to_nodeid_map_for_groups[_Cif] = c2.id # Create a Cif node belonging to another user c3 = CifData(file=f.name) c3.dbnode.user = new_user._dbuser c3.store() # Put it is to the right map cmd_to_nodeid_map_for_nuser[_Cif] = [c3.id]
def xyz2cif(fname): """ Convert xyz file produced by DDEC program to a cif file. """ f = open(fname) lines = f.readlines() f.close() # Number of atoms natoms = int(lines[0]) # Extract an array of charges cell_string = lines[1] charges = [i.split()[4] for i in lines[2:natoms + 2]] # Extract cell cell_unformatted = re.search(r"\[.*?\]", cell_string).group(0)[1:-1].split(',') cell = [ re.search(r"\{.*?\}", e).group(0)[1:-1].split() for e in cell_unformatted ] # Create a temporary file that contains the structure # in cif format buf = tempfile.TemporaryFile() # Create an ase object, specify unitcell # write it into the buffer file ase_obj = read(fname) ase_obj.set_cell(cell) ase_obj.write(buf, format='cif') # Read the cif file into from the buffer a CifFile # object buf.seek(0) cf = CifFile.ReadCif(buf) buf.close() # Manipulate the cif parameters img0 = cf.dictionary['image0'] img0.RemoveItem('_atom_site_label') img0.RemoveItem('_atom_site_occupancy') img0.RemoveItem('_atom_site_thermal_displace_type') img0.RemoveItem('_atom_site_B_iso_or_equiv') img0.ChangeItemOrder('_atom_site_type_symbol', 0) # Add chaages and placing them into the _atom_site_charge # loop img0.AddItem('_atom_site_charge', charges) img0.AddLoopName('_atom_site_type_symbol', '_atom_site_charge') # Add _atom_site_label loop that is the same as _atom_site_type_symbol one asts = img0.GetFullItemValue('_atom_site_type_symbol')[0] img0.AddItem('_atom_site_label', asts) img0.AddLoopName('_atom_site_type_symbol', '_atom_site_label') img0.ChangeItemOrder('_atom_site_label', 1) # Add two more items and placing them before the loops img0.AddItem('_symmetry_space_group_name_H-M', 'P 1') img0.ChangeItemOrder('_symmetry_space_group_name_h-m', -1) img0.AddItem('_space_group_name_Hall', 'P 1') img0.ChangeItemOrder('_space_group_name_Hall', -1) ciffile = tempfile.NamedTemporaryFile(suffix='.cif') with open(ciffile.name, 'w') as f: f.write(cf.WriteOut() + '\n') return CifData(file=ciffile.name, scan_type='flex', parse_policy='lazy')