def test_parse_kpoints(vasp_kpoints): """ Parse a reference KPOINTS file. Using the KpointsParser and compare the result to a reference kpoints-node. """ kpoints, _ = vasp_kpoints try: _ = kpoints.get_attribute('mesh') file_path = data_path('kpoints', 'KPOINTS_mesh') method = 'get_kpoints_mesh' param = 'mesh' except AttributeError: pass try: _ = kpoints.get_attribute('array|kpoints') file_path = data_path('kpoints', 'KPOINTS_list') method = 'get_kpoints' param = 'list' except AttributeError: pass parser = KpointsParser(file_path=file_path) result = parser.kpoints if param == 'list': assert getattr(result, method)().all() == getattr(kpoints, method)().all() if param == 'mesh': assert getattr(result, method)() == getattr(kpoints, method)()
def test_parse_doscar(fresh_aiida_env): """Parse a reference DOSCAR file with the DosParser and compare the result to a reference.""" file_name = 'DOSCAR' path = data_path('doscar', file_name) parser = DosParser(file_path=path) dos = numpy.array([(-3.44, -1.10400000e-43, -2.09900000e-43), (-1.539, 1.40000000e-01, 2.66100000e-01), (0.362, -3.62400000e-73, 2.00000000e+00), (2.264, -1.33800000e-05, 2.00000000e+00), (4.165, 3.15600000e+00, 8.00000000e+00), (6.066, -2.41200000e-15, 8.00000000e+00), (7.967, 3.15600000e+00, 1.40000000e+01), (9.868, -1.38100000e-27, 1.40000000e+01), (11.769, 2.90100000e+00, 1.95200000e+01), (13.67, 0.00000000e+00, 2.00000000e+01)], dtype=[('energy', '<f8'), ('total', '<f8'), ('integrated', '<f8')]) # yapf: disable result = parser.dos # The 'tdos' array is a nested array for some reason. result_dos = result.get_array('tdos') for i in range(0, dos.size): assert result_dos[i] == dos[i] file_name = 'DOSCAR.nopdos' path = data_path('doscar', file_name) parser = DosParser(file_path=path) result = parser.dos # The 'tdos' array is a nested array for some reason. result_dos = result.get_array('tdos') assert 'pdos' not in result.get_arraynames() for i in range(0, dos.size): assert result_dos[i] == dos[i]
def test_parse_kpoints(vasp_kpoints): """ Parse a reference KPOINTS file. Using the KpParser and compare the result to a reference kpoints-node. """ kpoints, _ = vasp_kpoints if kpoints.get_attrs().get('mesh'): file_path = data_path('kpoints', 'KPOINTS_mesh') method = 'get_kpoints_mesh' param = 'mesh' elif kpoints.get_attrs().get('array|kpoints'): file_path = data_path('kpoints', 'KPOINTS_list') method = 'get_kpoints' param = 'list' parser = KpParser(file_path=file_path) result = parser.get_quantity('kpoints-kpoints', {}) if param == 'list': assert getattr(result['kpoints-kpoints'], method)().all() == getattr(kpoints, method)().all() if param == 'mesh': assert getattr(result['kpoints-kpoints'], method)() == getattr(kpoints, method)()
def test_potcar_from_file(fresh_aiida_env): """Test creating a node pair from a file, creating the data node first.""" potcar_cls = get_data_node('vasp.potcar') _, created = potcar_cls.get_or_create_from_file(data_path('potcar', 'As', 'POTCAR')) assert created _, created = potcar_cls.get_or_create_from_file(data_path('potcar', 'As', 'POTCAR')) assert not created
def temp_pot_folder(tmpdir): """A temporary copy of the potcar test data folder, to avoid extracting tar files inside the repo.""" potcar_ga = py_path.local(data_path('potcar')).join('Ga') assert not potcar_ga.exists() pot_archive = py_path.local(data_path('potcar')) target = tmpdir.join('potentials') pot_archive.copy(target) return target
def temp_pot_folder(tmp_path): """A temporary copy of the potcar test data folder, to avoid extracting tar files inside the repo.""" potcar_ga = Path(data_path('potcar')) / 'Ga' assert not potcar_ga.exists() pot_archive = Path(data_path('potcar')) target = tmp_path / 'potentials' # Ensure that the target path exists Path(target).mkdir(exist_ok=True) copytree(pot_archive, target) return target
def test_relax_wc(fresh_aiida_env, vasp_params, potentials, mock_vasp): # def test_relax_wc(fresh_aiida_env, vasp_params, potentials, mock_vasp, mock_relax_wc): """Test submitting only, not correctness, with mocked vasp code.""" from aiida.orm import Code from aiida.plugins import WorkflowFactory from aiida.engine import run workchain = WorkflowFactory('vasp.relax') mock_vasp.store() create_authinfo(computer=mock_vasp.computer, store=True) structure = PoscarParser(file_path=data_path('test_relax_wc', 'inp', 'POSCAR')).structure kpoints = KpointsParser(file_path=data_path('test_relax_wc', 'inp', 'KPOINTS')).kpoints parameters = IncarParser(file_path=data_path('test_relax_wc', 'inp', 'INCAR')).incar parameters['system'] = 'test-case:test_relax_wc' parameters = {'incar': {k: v for k, v in parameters.items() if k not in ['isif', 'ibrion', 'nsw', 'ediffg']}} parameters['relax'] = {} parameters['relax']['perform'] = True parameters['relax']['algo'] = 'cg' parameters['relax']['force_cutoff'] = 0.01 inputs = AttributeDict() inputs.code = Code.get_from_string('mock-vasp@localhost') inputs.structure = structure inputs.kpoints = kpoints inputs.parameters = get_data_node('dict', dict=parameters) inputs.potential_family = get_data_node('str', POTCAR_FAMILY_NAME) inputs.potential_mapping = get_data_node('dict', dict=POTCAR_MAP) inputs.options = get_data_node('dict', dict={ 'withmpi': False, 'queue_name': 'None', 'max_wallclock_seconds': 1, 'import_sys_environment': True, 'resources': { 'num_machines': 1, 'num_mpiprocs_per_machine': 1 }, }) inputs.max_iterations = get_data_node('int', 1) inputs.clean_workdir = get_data_node('bool', False) inputs.verbose = get_data_node('bool', True) results, node = run.get_node(workchain, **inputs) assert node.exit_status == 0 assert 'relax' in results relax = results['relax'] assert 'structure' in relax sites = relax['structure'].sites assert sites[0].kind_name == 'Si' assert sites[1].kind_name == 'Si' np.testing.assert_allclose(sites[0].position, [4.8125, 4.8125, 4.8125]) np.testing.assert_allclose(sites[1].position, [0.6875, 0.6875, 0.715])
def test_file_get_or_create(fresh_aiida_env, potcar_node_pair): """Test get_or_create of PotcarFileData.""" potcar_as_path = data_path('potcar', 'As', 'POTCAR') potcar_file_cls = get_data_class('vasp.potcar_file') file_as = potcar_node_pair['file'] node_file_as, created_file_as = potcar_file_cls.get_or_create(potcar_as_path) assert not created_file_as assert file_as.pk == node_file_as.pk potcar_in_path = data_path('potcar', 'In_d', 'POTCAR') node_file_in, created_file_in = potcar_file_cls.get_or_create(potcar_in_path) assert created_file_in assert potcar_file_cls.exists(sha512=node_file_in.sha512)
def test_store_duplicate(fresh_aiida_env, potcar_node_pair): """ Storing a duplicate POTCAR node must fail. Uniqueness constraints to test for: * ``sha512`` attribute must be unique * the combination of all other attributes must be unique """ potcar_path = data_path('potcar', 'As', 'POTCAR') file_node = get_data_node('vasp.potcar_file', file=potcar_path) file_node.set_attribute('sha512', 'foo') with pytest.raises(UniquenessError): file_node.store() file_node = get_data_node('vasp.potcar_file', file=potcar_path) file_node.set_attribute('symbol', 'Ta') with pytest.raises(UniquenessError): file_node.store() data_node = get_data_node('vasp.potcar', potcar_file_node=potcar_node_pair['file']) data_node.set_attribute('sha512', 'foo') with pytest.raises(UniquenessError): data_node.store() data_node = get_data_node('vasp.potcar', potcar_file_node=potcar_node_pair['file']) data_node.set_attribute('symbol', 'Ta') with pytest.raises(UniquenessError): data_node.store() assert get_data_class('vasp.potcar').find_one(symbol='As') assert get_data_class('vasp.potcar_file').find_one(symbol='As')
def test_find_potcars(potcar_walker_cls, temp_data_folder): """Make sure the walker finds the right number fo POTCAR files.""" potcar_archive = py_path.local(data_path('.')).join('pot_archive') walker = potcar_walker_cls(temp_data_folder.strpath) walker.walk() assert len(walker.potcars) == 7 assert not potcar_archive.exists()
def parse_nac(aiida_env): """Give the parsing result of a retrieved NAC calculation (emulated).""" from aiida.orm import CalculationFactory, DataFactory calc = CalculationFactory('vasp.vasp')() calc.use_settings(DataFactory('parameter')(dict={'pymatgen_parser': {'parse_potcar_file': False, 'exception_on_bad_xml': False}})) parser = PymatgenParser(calc=calc) retrieved = DataFactory('folder')() retrieved.add_path(data_path('born_effective_charge', 'vasprun.xml'), '') retrieved.add_path(data_path('born_effective_charge', 'OUTCAR'), '') def parse(): success, nodes = parser.parse_with_retrieved({'retrieved': retrieved}) nodes = dict(nodes) return success, nodes return parse
def test_potcar_from_path(fresh_aiida_env): """Create a PotcarIo instance from a file path.""" potcar_path_as = data_path('potcar', 'As', 'POTCAR') from_ctor = PotcarIo(path=potcar_path_as) from_from = PotcarIo.from_(potcar_path_as) verify_potcario(from_from) assert from_ctor == from_from
def outcar_parser(request): """Return an instance of OutcarParser for a reference OUTCAR.""" from aiida_vasp.parsers.settings import ParserSettings file_name = 'OUTCAR' path = data_path(request.param, file_name) parser = OutcarParser(file_path=path, settings=ParserSettings({})) return parser
def vasprun_parser(request): """Return an instance of VasprunParser for a reference vasprun.xml.""" from aiida_vasp.parsers.parser_settings import ParserSettings file_name = 'vasprun.xml' path = data_path(request.param, file_name) parser = VasprunParser(file_path=path, settings=ParserSettings({})) return parser
def test_find_potcars(potcar_walker_cls, temp_data_folder): """Make sure the walker finds the right number fo POTCAR files.""" potcar_archive = Path(data_path('.')) / 'pot_archive' walker = potcar_walker_cls(str(temp_data_folder)) walker.walk() assert len(walker.potcars) == 7 assert not potcar_archive.exists()
def stream_parser(request): """Return an instance of StreamParser for a reference stream capture.""" from aiida_vasp.parsers.settings import ParserSettings file_name = 'vasp_output' path = data_path(*request.param, file_name) parser = StreamParser(file_path=path, settings=ParserSettings({})) return parser
def test_upload(fresh_aiida_env, temp_pot_folder): """Test uploading a family of POTCAR files.""" family_name = 'test_family' family_desc = 'Test Family' potcar_cls = get_data_class('vasp.potcar') pot_dir = temp_pot_folder.strpath potcar_ga = py_path.local(data_path('potcar')).join('Ga') assert not potcar_ga.exists() potcar_cls.upload_potcar_family(pot_dir, family_name, family_desc) assert potcar_cls.exists(element='In') assert potcar_cls.exists(element='As') assert potcar_cls.exists(element='Ga') assert not potcar_ga.exists() # this is supposed to return only one group, however it returns 8 (= number of uploaded files) assert [g.label for g in potcar_cls.get_potcar_groups()] == [family_name] assert len(potcar_cls.get_potcar_group(family_name).nodes) >= 3 with pytest.raises(ValueError): potcar_cls.upload_potcar_family(pot_dir, family_name, stop_if_existing=True) assert not potcar_ga.exists() num_files, num_added, num_uploaded = potcar_cls.upload_potcar_family(pot_dir, family_name + '_new', family_desc, stop_if_existing=False) assert num_files >= 3 assert num_added >= 3 assert num_uploaded == 0 assert not potcar_ga.exists()
def vasp_wavecar(fresh_aiida_env): """WAVECAR node and reference fixture.""" from aiida.plugins import DataFactory wavecar_path = data_path('wavecar', 'WAVECAR') wavecar = DataFactory('vasp.wavefun')(file=wavecar_path) with open(wavecar_path, 'r') as ref_wavecar_fo: ref_wavecar = ref_wavecar_fo.read() return wavecar, ref_wavecar
def test_hashing(aiida_env): """Ensure the file and content md5 hash equivalently for the same POTCAR.""" potcar_file_cls = get_data_class('vasp.potcar_file') potcar_path = ['potcar', 'As', 'POTCAR'] file_md5 = potcar_file_cls.get_file_md5(data_path(*potcar_path)) content_md5 = potcar_file_cls.get_contents_md5(read_file(*potcar_path)) assert file_md5 == content_md5
def vasp_chgcar(fresh_aiida_env): """CHGCAR node and reference fixture.""" from aiida.plugins import DataFactory chgcar_path = data_path('chgcar', 'CHGCAR') chgcar = DataFactory('vasp.chargedensity')(file=chgcar_path) with open(chgcar_path, 'r') as ref_chgcar_fo: ref_chgcar = ref_chgcar_fo.read() return chgcar, ref_chgcar
def test_parse_wavecar(fresh_aiida_env): """Parse a reference CHGCAR file with the ChargcarParser and compare the result to a reference string.""" path = data_path('wavecar', 'WAVECAR') parser = WavecarParser(file_path=path) result = parser.wavecar content = result.get_content() assert result.filename == 'WAVECAR' assert content == 'This is a test WAVECAR file.\n'
def test_hashing(fresh_aiida_env): """Ensure the file and content sha512 hash equivalently for the same POTCAR.""" potcar_file_cls = get_data_class('vasp.potcar_file') potcar_path = ['potcar', 'As', 'POTCAR'] file_sha512 = potcar_file_cls.get_file_sha512(data_path(*potcar_path)) content_sha512 = potcar_file_cls.get_contents_sha512(read_file(*potcar_path, mode='rb')) assert file_sha512 == content_sha512
def test_read_incar(): """Read an INCAR file and test that some of the keys are read correctly.""" incar_path = data_path('phonondb', 'INCAR') incar_io = IncarIo(file_path=incar_path) incar_dict = incar_io.get_dict() assert incar_dict['prec'] == 'Accurate' assert incar_dict['ibrion'] == -1 assert incar_dict['encut'] == 359.7399 assert incar_dict['lreal'] is False
def test_parse_chgcar(fresh_aiida_env): """Parse a reference CHGCAR file with the ChargcarParser and compare the result to a reference string.""" file_name = 'CHGCAR' path = data_path('chgcar', file_name) parser = ChgcarParser(file_path=path) result = parser.chgcar content = result.get_content() assert result.filename == file_name assert content == 'This is a test CHGCAR file.\n'
def vasprun_parser(request): """Return an instance of VasprunParser for a reference vasprun.xml.""" from aiida_vasp.parsers.settings import ParserSettings from aiida_vasp.calcs.vasp import VaspCalculation file_name = 'vasprun.xml' path = data_path(request.param, file_name) parser = VasprunParser(file_path=path, settings=ParserSettings({})) parser._vasp_parser = VaspCalculation return parser
def potcar_node_pair(fresh_aiida_env): """Create a POTCAR node pair.""" potcar_path = data_path('potcar', 'As', 'POTCAR') potcar_file_node = get_data_node('vasp.potcar_file', file=potcar_path) potcar_file_node.store() return { 'file': potcar_file_node, 'potcar': get_data_class('vasp.potcar').find_one(symbol='As') }
def test_parser_read(fresh_aiida_env): """Test to read a INCAR file.""" path = data_path('phonondb', 'INCAR') parser = IncarParser(file_path=path) incar = parser.incar assert incar['prec'] == 'Accurate' assert incar['ibrion'] == -1 assert incar['encut'] == 359.7399 assert incar['lreal'] is False
def test_parse_chgcar(): """Parse a reference CHGCAR file with the ChargcarParser and compare the result to a reference string.""" file_name = 'CHGCAR' path = data_path('chgcar', file_name) parser = ChgcarParser(file_path=path) result = parser.get_quantity('chgcar', {}) with open(result['chgcar'].get_file_abs_path(), 'r') as file_obj: content = file_obj.readline() assert result['chgcar'].filename == file_name assert content == 'This is a test CHGCAR file.\n'
def test_parse_eigenval(): """Parse a reference EIGENVAL file with the EigParser and compare the result to a reference.""" file_name = 'EIGENVAL' path = data_path('eigenval', file_name) parser = EigParser(file_path=path) bands = numpy.array([[[-1.439825, 2.964373, 2.964373, 2.964373, 7.254542, 7.254542, 7.254542, 11.451811, 11.670398, 11.670398]]]) inputs = {} result = parser.get_quantity('eigenval-eigenvalues', inputs) assert result['eigenval-eigenvalues'].all() == bands.all()
def vasprun_path(request, tmpdir): """Truncate vasprun.xml at the given line number and parse.""" original_path = data_path('phonondb', 'vasprun.xml') if request.param == -1: return original_path truncated_path = tmpdir.join('vasprun.xml') with open(original_path, 'r') as original_fo: truncated_content = '\n'.join(original_fo.readlines()[:request.param]) truncated_path.write(truncated_content) return str(truncated_path)