def test_cluster_from_file(self): self.atoms.write_file("ATOMS_test") mol_1 = Atoms.cluster_from_file("ATOMS_test") mol_2 = Atoms.cluster_from_file(os.path.join(test_dir, "ATOMS")) self.assertEqual(mol_1.formula, mol_2.formula) self.assertEqual(len(mol_1), len(mol_2)) os.remove("ATOMS_test")
def test_get_string(self): header = Header.from_string(header_string) struc = header.struct central_atom = 'O' a = Atoms(struc, central_atom) atoms = a.get_string() self.assertEqual(atoms.splitlines()[3].split()[4], central_atom, "failed to create ATOMS string")
def test_as_dict_and_from_dict(self): file_name = os.path.join(test_dir, 'HEADER') header = Header.from_file(file_name) struct = header.struct atoms = Atoms(struct, 'O', radius=10.) d = atoms.as_dict() atoms2 = Atoms.from_dict(d) self.assertEqual(str(atoms), str(atoms2), "Atoms failed to and from dict test")
def from_directory(input_dir): """ Read in a set of FEFF input files from a directory, which is useful when existing FEFF input needs some adjustment. """ sub_d = {} for fname, ftype in [("HEADER", Header), ("PARAMETERS", Tags)]: fullzpath = zpath(os.path.join(input_dir, fname)) sub_d[fname.lower()] = ftype.from_file(fullzpath) # Generation of FEFFDict set requires absorbing atom, need to search # the index of absorption atom in the structure according to the # distance matrix and shell species information contained in feff.inp absorber_index = [] radius = None feffinp = zpath(os.path.join(input_dir, 'feff.inp')) if "RECIPROCAL" not in sub_d["parameters"]: input_atoms = Atoms.cluster_from_file(feffinp) shell_species = np.array([x.species_string for x in input_atoms]) # First row of distance matrix represents the distance from the absorber to # the rest atoms distance_matrix = input_atoms.distance_matrix[0, :] # Get radius value from math import ceil radius = int(ceil(input_atoms.get_distance(input_atoms.index(input_atoms[0]), input_atoms.index(input_atoms[-1])))) for site_index, site in enumerate(sub_d['header'].struct): if site.specie == input_atoms[0].specie: site_atoms = Atoms(sub_d['header'].struct, absorbing_atom=site_index, radius=radius) site_distance = np.array(site_atoms.get_lines())[:, 5].astype(np.float64) site_shell_species = np.array(site_atoms.get_lines())[:, 4] shell_overlap = min(shell_species.shape[0], site_shell_species.shape[0]) if np.allclose(distance_matrix[:shell_overlap], site_distance[:shell_overlap]) and \ np.all(site_shell_species[:shell_overlap] == shell_species[:shell_overlap]): absorber_index.append(site_index) if "RECIPROCAL" in sub_d["parameters"]: absorber_index = sub_d["parameters"]["TARGET"] absorber_index[0] = int(absorber_index[0]) - 1 # Generate the input set if 'XANES' in sub_d["parameters"]: CONFIG = loadfn(os.path.join(MODULE_DIR, "MPXANESSet.yaml")) if radius is None: radius = 10 return FEFFDictSet(absorber_index[0], sub_d['header'].struct, radius=radius, config_dict=CONFIG, edge=sub_d["parameters"]["EDGE"], nkpts=1000, user_tag_settings=sub_d["parameters"])
def test_cluster_from_file(self): r = CifParser(os.path.join(test_dir, "CoO19128.cif")) structure = r.get_structures()[0] atoms = Atoms(structure, "O", 10.0) atoms.write_file("ATOMS_test") mol_1 = Atoms.cluster_from_file("ATOMS_test") mol_2 = Atoms.cluster_from_file(os.path.join(test_dir, "ATOMS")) self.assertEqual(mol_1.formula, mol_2.formula) self.assertEqual(len(mol_1), len(mol_2)) os.remove("ATOMS_test")
def test_setup(self): with cd(test_dir): with ScratchDir('.', copy_from_current_on_enter=True): f = FeffJob("hello", backup=True) f.setup() parameter = Tags.from_file('feff.inp') parameter_orig = Tags.from_file('feff.inp.orig') self.assertEqual(parameter, parameter_orig) atom = Atoms.cluster_from_file('feff.inp') atom_origin = Atoms.cluster_from_file('feff.inp.orig') self.assertEqual(atom, atom_origin)
def test_distances(self): atoms_1 = self.atoms.get_lines() distances_1 = [float(a[5]) for a in atoms_1] atoms_2 = Atoms.atoms_string_from_file(os.path.join(test_dir, "ATOMS")) atoms_2 = atoms_2.splitlines()[3:] distances_2 = [float(a.split()[5]) for a in atoms_2] np.testing.assert_allclose(distances_1, distances_2)
def test_postfeffset(self): self.mp_xanes.write_input(os.path.join('.', 'xanes_3')) feff_dict_input = FEFFDictSet.from_directory(os.path.join('.', 'xanes_3')) self.assertTrue(feff_dict_input.tags == Tags.from_file(os.path.join('.', 'xanes_3/feff.inp'))) self.assertTrue(str(feff_dict_input.header()) == str(Header.from_file(os.path.join('.', 'xanes_3/HEADER')))) feff_dict_input.write_input('xanes_3_regen') origin_tags = Tags.from_file(os.path.join('.', 'xanes_3/PARAMETERS')) output_tags = Tags.from_file(os.path.join('.', 'xanes_3_regen/PARAMETERS')) origin_mole = Atoms.cluster_from_file(os.path.join('.', 'xanes_3/feff.inp')) output_mole = Atoms.cluster_from_file(os.path.join('.', 'xanes_3_regen/feff.inp')) original_mole_dist = np.array(origin_mole.distance_matrix[0, :]).astype(np.float64) output_mole_dist = np.array(output_mole.distance_matrix[0, :]).astype(np.float64) original_mole_shell = [x.species_string for x in origin_mole] output_mole_shell = [x.species_string for x in output_mole] self.assertTrue(np.allclose(original_mole_dist, output_mole_dist)) self.assertTrue(origin_tags == output_tags) self.assertTrue(original_mole_shell == output_mole_shell) shutil.rmtree(os.path.join('.', 'xanes_3')) shutil.rmtree(os.path.join('.', 'xanes_3_regen')) reci_mp_xanes = MPXANESSet(self.absorbing_atom, self.structure, user_tag_settings={"RECIPROCAL": ""}) reci_mp_xanes.write_input('xanes_reci') feff_reci_input = FEFFDictSet.from_directory(os.path.join('.', 'xanes_reci')) self.assertTrue("RECIPROCAL" in feff_reci_input.tags) feff_reci_input.write_input('Dup_reci') self.assertTrue(os.path.exists(os.path.join('.', 'Dup_reci', 'HEADER'))) self.assertTrue(os.path.exists(os.path.join('.', 'Dup_reci', 'feff.inp'))) self.assertTrue(os.path.exists(os.path.join('.', 'Dup_reci', 'PARAMETERS'))) self.assertFalse(os.path.exists(os.path.join('.', 'Dup_reci', 'ATOMS'))) self.assertFalse(os.path.exists(os.path.join('.', 'Dup_reci', 'POTENTIALS'))) tags_original = Tags.from_file(os.path.join('.', 'xanes_reci/feff.inp')) tags_output = Tags.from_file(os.path.join('.', 'Dup_reci/feff.inp')) self.assertTrue(tags_original == tags_output) stru_orig = Structure.from_file(os.path.join('.', 'xanes_reci/Co2O2.cif')) stru_reci = Structure.from_file(os.path.join('.', 'Dup_reci/Co2O2.cif')) self.assertTrue(stru_orig.__eq__(stru_reci)) shutil.rmtree(os.path.join('.', 'Dup_reci')) shutil.rmtree(os.path.join('.', 'xanes_reci'))
def test_post_distdiff(self): feff_dict_input = FEFFDictSet.from_directory(os.path.join(test_dir, 'feff_dist_test')) self.assertTrue(feff_dict_input.tags == Tags.from_file(os.path.join(test_dir, 'feff_dist_test/feff.inp'))) self.assertTrue( str(feff_dict_input.header()) == str(Header.from_file(os.path.join(test_dir, 'feff_dist_test/HEADER')))) feff_dict_input.write_input('feff_dist_regen') origin_tags = Tags.from_file(os.path.join(test_dir, 'feff_dist_test/PARAMETERS')) output_tags = Tags.from_file(os.path.join('.', 'feff_dist_regen/PARAMETERS')) origin_mole = Atoms.cluster_from_file(os.path.join(test_dir, 'feff_dist_test/feff.inp')) output_mole = Atoms.cluster_from_file(os.path.join('.', 'feff_dist_regen/feff.inp')) original_mole_dist = np.array(origin_mole.distance_matrix[0, :]).astype(np.float64) output_mole_dist = np.array(output_mole.distance_matrix[0, :]).astype(np.float64) original_mole_shell = [x.species_string for x in origin_mole] output_mole_shell = [x.species_string for x in output_mole] self.assertTrue(np.allclose(original_mole_dist, output_mole_dist)) self.assertTrue(origin_tags == output_tags) self.assertTrue(original_mole_shell == output_mole_shell) shutil.rmtree(os.path.join('.', 'feff_dist_regen'))
def run_task(self, fw_spec): calc_dir = os.getcwd() if "calc_dir" in self: calc_dir = self["calc_dir"] elif self.get("calc_loc"): calc_dir = get_calc_loc(self["calc_loc"], fw_spec["calc_locs"])["path"] logger.info("PARSING DIRECTORY: {}".format(calc_dir)) db_file = env_chk(self.get('db_file'), fw_spec) cluster_dict = None tags = Tags.from_file(filename="feff.inp") if "RECIPROCAL" not in tags: cluster_dict = Atoms.cluster_from_file("feff.inp").as_dict() doc = {"input_parameters": tags.as_dict(), "cluster": cluster_dict, "structure": self["structure"].as_dict(), "absorbing_atom": self["absorbing_atom"], "spectrum_type": self["spectrum_type"], "spectrum": np.loadtxt(os.path.join(calc_dir, self["output_file"])).tolist(), "edge": self.get("edge", None), "metadata": self.get("metadata", None), "dir_name": os.path.abspath(os.getcwd()), "last_updated": datetime.today()} if not db_file: with open("feff_task.json", "w") as f: f.write(json.dumps(doc, default=DATETIME_HANDLER)) # db insertion else: db = MMFeffDb.from_db_file(db_file, admin=True) db.insert(doc) logger.info("Finished parsing the spectrum") return FWAction(stored_data={"task_id": doc.get("task_id", None)})
def test_atoms_from_file(self): filepath = os.path.join(test_dir, 'ATOMS') atoms = Atoms.atoms_string_from_file(filepath) self.assertEqual(atoms.splitlines()[3].split()[4], 'O', "failed to read ATOMS file")