def run_task(self, fw_spec): lammps_input_set = self["lammps_input_set"] input_filename = self["input_filename"] data_filename = self.get("data_filename", None) if isinstance(lammps_input_set.lammps_data, dict): lammps_input_set.lammps_data = LammpsData.from_dict( lammps_input_set.lammps_data) lammps_input_set.write_input(input_filename, data_filename)
def writeLammpsStruct(struct, args): from pymatgen.io.lammps.data import LammpsData from pymatgen import Element from json import dumps, loads # Get the LammpsData object lammps_dict = LammpsData.from_structure(struct, atom_style="atomic").as_dict() # Relabel the atom types in the LammpsData object masses = { "columns": ["mass"], "index": [val for val in range(1, len(args["atom_mapping"]) + 1)] } mass_data = [None] * len(args["atom_mapping"]) for key in args["atom_mapping"].keys(): mass_data[args["atom_mapping"][key]] = [Element(key).atomic_mass] masses["data"] = mass_data lammps_dict["masses"] = dumps(masses) atoms = loads(lammps_dict["atoms"]) try: for i in range(len(atoms["data"])): atoms["data"][i][0] = args["atom_mapping"][ struct[i].species_string] + 1 except KeyError as e: raise Exception( "{} Missing From atom_mapping Keyword from Input File".format( e.args[0])) # Move the correct labels back to the LammpsData object lammps_dict["atoms"] = dumps(atoms) LammpsData.from_dict(lammps_dict).write_file("atom.data", distance=16, velocity=16, charge=16) return
def test_json_dict(self): encoded = json.dumps(self.ethane.as_dict()) decoded = json.loads(encoded) c2h6 = LammpsData.from_dict(decoded) pd.testing.assert_frame_equal(c2h6.masses, self.ethane.masses) pd.testing.assert_frame_equal(c2h6.atoms, self.ethane.atoms) ff = self.ethane.force_field key, target_df = random.sample(ff.items(), 1)[0] self.assertIsNone( pd.testing.assert_frame_equal(c2h6.force_field[key], target_df, check_dtype=False), key, ) topo = self.ethane.topology key, target_df = random.sample(topo.items(), 1)[0] self.assertIsNone(pd.testing.assert_frame_equal(c2h6.topology[key], target_df), key)
def test_json_dict(self): encoded = json.dumps(self.ethane.as_dict()) decoded = json.loads(encoded) c2h6 = LammpsData.from_dict(decoded) pd.testing.assert_frame_equal(c2h6.masses, self.ethane.masses) pd.testing.assert_frame_equal(c2h6.atoms, self.ethane.atoms) ff = self.ethane.force_field key, target_df = random.sample(ff.items(), 1)[0] self.assertIsNone( pd.testing.assert_frame_equal(c2h6.force_field[key], target_df, check_dtype=False), key ) topo = self.ethane.topology key, target_df = random.sample(topo.items(), 1)[0] self.assertIsNone( pd.testing.assert_frame_equal(c2h6.topology[key], target_df), key )