def test_valid_qcschema_slow(basis_name): basis_dict = api.get_basis(basis_name) qcs_str = api.get_basis(basis_name, fmt='qcschema') qcs_json = json.loads(qcs_str) el_list = [ lut.element_sym_from_Z(x, True) for x in basis_dict['elements'].keys() ] coords = [] for idx, el in enumerate(el_list): coords.extend((0.0, 0.0, float(idx))) qcs_json['atom_map'] = list(qcs_json['center_data'].keys()) assert len(qcs_json['atom_map']) == len(el_list) dummy_inp = { "schema_name": "qc_schema_input", "schema_version": 1, "keywords": {}, "molecule": { "schema_name": "qcschema_molecule", "schema_version": 2, "geometry": coords, "symbols": el_list }, 'driver': 'energy', 'model': { 'method': 'B3LYP', 'basis': qcs_json } } qcschema.validate(dummy_inp, 'input')
def test_electron_equal(basis1, basis2, element, expected): el1 = api.get_basis(basis1)['elements'][element] el2 = api.get_basis(basis2)['elements'][element] shells1 = el1['electron_shells'] shells2 = el2['electron_shells'] assert curate.electron_shells_are_equal(shells1, shells2, True) == expected assert curate.electron_shells_are_equal(shells2, shells1, True) == expected
def test_curate_roundtrip(basis, fmt): # Many formats have limitations on general contractions if fmt == 'gaussian94': uncontract_general = True uncontract_spdf = 1 if fmt == 'turbomole': uncontract_general = True uncontract_spdf = 0 if fmt == 'nwchem': uncontract_general = False uncontract_spdf = 1 bse_formatted = api.get_basis(basis, fmt=fmt) bse_dict = api.get_basis(basis, uncontract_general=uncontract_general) bse_dict = manip.uncontract_spdf(bse_dict, uncontract_spdf) outfile = tempfile.NamedTemporaryFile(mode='w', delete=False) outfile_path = outfile.name outfile.write(bse_formatted) outfile.close() test_dict = curate.read_formatted_basis(outfile_path, fmt) os.remove(outfile_path) test_dict = manip.sort_basis(test_dict) bse_dict = manip.sort_basis(bse_dict) # Compare, ignoring metadata (not stored in most formats) assert curate.compare_basis(bse_dict, test_dict, rel_tol=0.0)
def test_basis_metadata_duplicate(bs1, bs2): bsdata1 = api.get_basis(bs1) bsdata2 = api.get_basis(bs2) # The names are going to be different n1 = bsdata1.pop('name') n2 = bsdata2.pop('name') assert n1 != n2 assert bsdata1 == bsdata2
def test_manip_roundtrip(basis): bse_dict = api.get_basis(basis, uncontract_general=True, uncontract_spdf=True) bse_dict_gen = manip.make_general(bse_dict) bse_dict_unc = manip.uncontract_general(bse_dict_gen) assert curate.compare_basis(bse_dict, bse_dict_unc, rel_tol=0.0)
def test_printing(basis, element): el = api.get_basis(basis)['basis_set_elements'][element] shells = el['element_electron_shells'] curate.print_electron_shell(shells[0]) if 'element_ecp' in el: ecps = el['element_ecp'] curate.print_ecp_pot(ecps[0]) curate.print_element(element, el)
def test_curate_roundtrip(tmp_path, basis, fmt): tmp_path = str(tmp_path) # Needed for python 3.5 # Many formats have limitations on general contractions if fmt == 'gaussian94': uncontract_general = True make_general = False uncontract_spdf = 1 if fmt == 'turbomole': uncontract_general = True make_general = False uncontract_spdf = 0 if fmt == 'nwchem': uncontract_general = False make_general = False uncontract_spdf = 1 if fmt == 'cfour': uncontract_general = False make_general = True uncontract_spdf = 0 bse_formatted = api.get_basis(basis, fmt=fmt) bse_dict = api.get_basis(basis, uncontract_general=uncontract_general, make_general=make_general) bse_dict = manip.uncontract_spdf(bse_dict, uncontract_spdf) outfile_path = os.path.join(tmp_path, 'roundtrip.txt') with open(outfile_path, 'w', encoding='utf-8') as outfile: outfile.write(bse_formatted) test_dict = readers.read_formatted_basis_file(outfile_path, fmt) test_dict = sort.sort_basis(test_dict) bse_dict = sort.sort_basis(bse_dict) # Compare, ignoring metadata (not stored in most formats) assert curate.compare_basis(bse_dict, test_dict, rel_tol=0.0)
def test_v0_with_bse(basis_name, fmt, opt_gen): basis_meta = _bs_metadata[basis_name] fmt_info = _format_map[fmt] if not basis_name in _bse_v0_map: raise RuntimeError("Mapping from BSE to old BSE name doesn't exist for " + basis_name) # Read in the data from the old BSE bse_name = _bse_v0_map[basis_name] fmt_suffix = '.' + fmt_info[0] + '.bz2' # See if the bse optimized-general file is there. If not, it is the # same as the un-optimized if opt_gen: fmt_suffix2 = '.min' + fmt_suffix bse_file = os.path.join(_bse_data_dir, bse_name + fmt_suffix2) if os.path.isfile(bse_file): fmt_suffix = fmt_suffix2 bse_file = os.path.join(_bse_data_dir, bse_name + fmt_suffix) if not os.path.isfile(bse_file): raise FileNotFoundError("File {} does not exist (for comparing basis {})".format(bse_file, basis_name)) with bz2.open(bse_file, 'rt') as f: bse_data = f.readlines() bse_data = fmt_info[1](bse_data) # read in data from the new bse (version 0) new_data = api.get_basis(basis_name, version='0', fmt=fmt, optimize_general=opt_gen, header=False) new_data = new_data.split('\n') new_data = fmt_info[1](new_data) if len(new_data) != len(bse_data): print("BSE FILE: " + bse_file) #open('T.new', 'w').write("\n".join(new_data)) #open('T.old', 'w').write("\n".join(bse_data)) raise RuntimeError("Basis set: {} different number of lines: {} vs {}".format(basis_name, len(new_data), len(bse_data))) for i in range(len(new_data)): new_line = new_data[i] bse_line = bse_data[i] if not _lines_equivalent(new_line, bse_line): errstr = '''Difference found. Line {} New: {} Old: {}'''.format(i, new_line, bse_line) #open('T.new', 'w').write("\n".join(new_data)) #open('T.old', 'w').write("\n".join(bse_data)) raise RuntimeError(errstr)
def test_manip_roundtrip_slow(basis): bse_dict = api.get_basis(basis) bse_dict_gen = manip.make_general(bse_dict) bse_dict_unc = manip.uncontract_general(bse_dict_gen) bse_dict_unc = manip.prune_basis(bse_dict_unc) bse_dict_sort = sort.sort_basis(bse_dict_unc) bse_dict = manip.uncontract_general(bse_dict) bse_dict = manip.uncontract_spdf(bse_dict) assert curate.compare_basis(bse_dict, bse_dict_unc, rel_tol=0.0) assert curate.compare_basis(bse_dict, bse_dict_sort, rel_tol=0.0) bse_dict_gen = manip.prune_basis(bse_dict_gen) bse_dict_gen2 = manip.make_general(bse_dict_unc) bse_dict_gen2 = manip.prune_basis(bse_dict_gen2) assert curate.compare_basis(bse_dict_gen, bse_dict_gen2, rel_tol=0.0)
def test_add_basis(tmp_path): tmp_path = str(tmp_path) # Needed for python 3.5 for sf, refs in _source_data.items(): sf_path = os.path.join(auth_data_dir, sf) name = sf.split('.')[0] curate.add_basis(sf_path, tmp_path, 'test_subdir', 'test_' + name, 'test_basis_' + name, 'test_family', 'orbital', 'Test Basis Description: ' + name, '1', 'Test Basis Revision Description', 'Test Source', refs) md = api.get_metadata(tmp_path) assert len(md) == len(_source_data) # Re-read for sf in _source_data.keys(): name = name = sf.split('.')[0] name = 'test_basis_' + name bse_dict = api.get_basis(name, data_dir=tmp_path) assert bse_dict['family'] == 'test_family' # Compare against the file we created from sf_path = os.path.join(auth_data_dir, sf) assert curate.compare_basis_against_file(name, sf_path, data_dir=tmp_path) # Check that all the files exist and contain the right elements for fpath, elements in _expected_files.items(): fpath = os.path.join(tmp_path, fpath) assert os.path.isfile(fpath) fdata = fileio.read_json_basis(fpath) expect_elements = set(misc.expand_elements(elements, True)) assert set(fdata['elements'].keys()) == expect_elements # Validate the new data dir validator.validate_data_dir(tmp_path)
def test_stored_nelec_start_slow(basis_name): bs_data = api.get_basis(basis_name) for el in bs_data['elements'].values(): if not 'ecp_electrons' in el: continue ecp_electrons = el['ecp_electrons'] starting_shells = lut.electron_shells_start(ecp_electrons, 8) # Make sure the number of covered electrons matches nelec_sum = 0 for am,count in enumerate(starting_shells): # How many shells of AM are covered by the ECP covered = count - 1 # Adjust for the principal quantum number where the shells for the AM start # (ie, p start at 2, d start at 3) covered -= am # Number of orbs = 2*am+1. Multiply by 2 to get electrons nelec_sum += (2*am+1)*2*covered assert nelec_sum == ecp_electrons
def test_valid_complete(bs_name, bs_ver): '''Test that all basis set data is valid when obtained through get_basis''' data = api.get_basis(bs_name, version=bs_ver) validator.validate_data('complete', data)
def test_compare_elements(basis1, basis2, element, expected): el1 = api.get_basis(basis1)['elements'][element] el2 = api.get_basis(basis2)['elements'][element] assert curate.compare_elements(el1, el2, True, True, True) == expected assert curate.compare_elements(el2, el1, True, True, True) == expected
def test_ecp_equal(basis1, basis2, element, expected): el1 = api.get_basis(basis1)['elements'][element] el2 = api.get_basis(basis2)['elements'][element] ecps1 = el1['ecp_potentials'] ecps2 = el2['ecp_potentials'] assert curate.ecp_pots_are_equal(ecps1, ecps2, True) == expected
el2, name2, _, _, cont, _ = cont.split('.') assert el1 == el2 assert name2 == 'ANO-RCC' assert name1 in all_names el = lut.element_Z_from_sym(el1, as_str=True) assert el not in split_data[name1] cont = re.findall(r'\d+[a-z]+', cont) split_data[name1][el] = cont print(split_data) for name, el_cont_data in split_data.items(): print(name) new_basis = api.get_basis(name) for el, eldata in new_basis['elements'].items(): el_split = el_cont_data[el] am = [sh['angular_momentum'] for sh in eldata['electron_shells']] new_pattern = [] for sh in eldata['electron_shells']: new_pattern.append('{}{}'.format( len(sh['coefficients']), lut.amint_to_char(sh['angular_momentum']))) print(lut.element_sym_from_Z(el, True), ''.join(new_pattern)) assert new_pattern == el_split print()
def test_print_element_data(basis, element): eldata = api.get_basis(basis)['elements'][element] printing.element_data_str(element, eldata)