Skip to content

Commit

Permalink
fix io, phonon and symmetry
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Dec 6, 2024
1 parent 9d09765 commit 3f7b180
Show file tree
Hide file tree
Showing 14 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/pymatgen/io/abinit/abitimer.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def parse(self, filenames):
read_ok = []
for filename in filenames:
try:
file = open(filename) # noqa: SIM115
file = open(filename, encoding="utf-8") # noqa: SIM115
except OSError:
logger.warning(f"Cannot open file {filename}")
continue
Expand Down Expand Up @@ -684,7 +684,7 @@ def to_csv(self, fileobj=sys.stdout):
is_str = isinstance(fileobj, str)

if is_str:
fileobj = open(fileobj, mode="w") # noqa: SIM115
fileobj = open(fileobj, mode="w", encoding="utf-8") # noqa: SIM115

for idx, section in enumerate(self.sections):
fileobj.write(section.to_csvline(with_header=(idx == 0)))
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/io/abinit/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ def write(self, filepath="run.abi"):
os.makedirs(dirname, exist_ok=True)

# Write the input file.
with open(filepath, mode="w") as file:
with open(filepath, mode="w", encoding="utf-8") as file:
file.write(str(self))

def deepcopy(self):
Expand Down
4 changes: 2 additions & 2 deletions src/pymatgen/io/adf.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ def write_file(self, molecule, inp_file):
unres_block = AdfKey("Unrestricted")
mol_blocks.append(unres_block)

with open(inp_file, "w+") as file:
with open(inp_file, "w+", encoding="utf-8") as file:
for block in mol_blocks:
file.write(str(block) + "\n")
file.write(str(self.task) + "\n")
Expand Down Expand Up @@ -656,7 +656,7 @@ def _parse_logfile(self, logfile):
return
break

with open(logfile) as file:
with open(logfile, encoding="utf-8") as file:
for line in file:
if match := error_patt.search(line):
self.is_failed = True
Expand Down
4 changes: 2 additions & 2 deletions src/pymatgen/io/aims/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def write_file(self, directory: str | Path | None = None, overwrite: bool = Fals
if not overwrite and file_name.exists():
raise ValueError(f"geometry.in file exists in {directory}")

with open(f"{directory}/geometry.in", mode="w") as file:
with open(f"{directory}/geometry.in", mode="w", encoding="utf-8") as file:
file.write(self.get_header(file_name.as_posix()))
file.write(self.content)
file.write("\n")
Expand Down Expand Up @@ -665,7 +665,7 @@ def write_file(

content = self.get_content(structure, verbose_header)

with open(f"{directory}/control.in", mode="w") as file:
with open(f"{directory}/control.in", mode="w", encoding="utf-8") as file:
file.write(f"#{'=' * 72}\n")
file.write(f"# FHI-aims geometry file: {directory}/geometry.in\n")
file.write("# File generated from pymatgen\n")
Expand Down
4 changes: 2 additions & 2 deletions src/pymatgen/io/aims/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ def read_aims_header_info(
with gzip.open(filename, mode="rt") as file:
content = file.read()
else:
with open(filename) as file:
with open(filename, encoding="utf-8") as file:
content = file.read()

if content is None:
Expand Down Expand Up @@ -1196,7 +1196,7 @@ def read_aims_output(
with gzip.open(path, mode="rt") as file:
content = file.read()
else:
with open(path) as file:
with open(path, encoding="utf-8") as file:
content = file.read()

if content is None:
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/io/aims/sets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def _read_previous(
# Should be checked with Fireworks, will not for sure work with
# jobflow_remote)
split_prev_dir = str(prev_dir).split(":")[-1]
with open(f"{split_prev_dir}/parameters.json") as param_file:
with open(f"{split_prev_dir}/parameters.json", encoding="utf-8") as param_file:
prev_params = json.load(param_file, cls=MontyDecoder)

try:
Expand Down
4 changes: 2 additions & 2 deletions src/pymatgen/io/feff/sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ def write_input(self, output_dir=".", make_dir_if_not_present=True):
)

for k, v in feff.items():
with open(os.path.join(output_dir, k), mode="w") as file:
with open(os.path.join(output_dir, k), mode="w", encoding="utf-8") as file:
file.write(str(v))

with open(f"{output_dir}/feff.inp", mode="w") as file:
with open(f"{output_dir}/feff.inp", mode="w", encoding="utf-8") as file:
file.write(feff_input)

# write the structure to CIF file
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/io/lammps/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def run(self, site_property: str | None = None) -> Molecule:
with tempfile.TemporaryDirectory() as scratch_dir:
self._write_input(input_dir=scratch_dir)
with (
open(os.path.join(scratch_dir, self.input_file)) as packmol_input,
open(os.path.join(scratch_dir, self.input_file), encoding="utf-8") as packmol_input,
Popen(self.packmol_bin, stdin=packmol_input, stdout=PIPE, stderr=PIPE) as proc,
):
stdout, stderr = proc.communicate()
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/io/multiwfn.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def get_qtaim_descs(file: PathLike) -> dict[str, dict[str, Any]]:
cp_sections = list()
descriptors = dict()

with open(file) as f:
with open(file, encoding="utf-8") as f:
lines = f.readlines()
lines = [line[:-1] for line in lines]

Expand Down
6 changes: 3 additions & 3 deletions src/pymatgen/io/pwmat/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def get_types(self) -> np.ndarray:
"""
content = "POSITION"
idx_row = LineLocator.locate_all_lines(file_path=self.atom_config_path, content=content)[0]
with open(self.atom_config_path) as file:
with open(self.atom_config_path, encoding="utf-8") as file:
atom_config_content = file.readlines()
atomic_numbers_content = atom_config_content[idx_row : idx_row + self.n_atoms]
atomic_numbers_lst = [int(row.split()[0]) for row in atomic_numbers_content] # convert str to int
Expand All @@ -151,7 +151,7 @@ def get_coords(self) -> np.ndarray:
coords_lst: list[np.ndarray] = []
content: str = "POSITION"
idx_row: int = LineLocator.locate_all_lines(file_path=self.atom_config_path, content=content)[0]
with open(self.atom_config_path) as file:
with open(self.atom_config_path, encoding="utf-8") as file:
atom_config_content = file.readlines()
"""
row_content:
Expand All @@ -174,7 +174,7 @@ def get_magmoms(self) -> np.ndarray:
try: # Error: not containing magmoms info.
idx_row = LineLocator.locate_all_lines(file_path=self.atom_config_path, content=content)[-1]

with open(self.atom_config_path) as file:
with open(self.atom_config_path, encoding="utf-8") as file:
atom_config_content = file.readlines()

magnetic_moments_content = atom_config_content[idx_row : idx_row + self.n_atoms]
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/io/qchem/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ def multiple_outputs_from_file(filename, keep_sub_files=True):
if text[0] == "":
text = text[1:]
for i, sub_text in enumerate(text):
with open(f"{filename}.{i}", mode="w") as temp:
with open(f"{filename}.{i}", mode="w", encoding="utf-8") as temp:
temp.write(sub_text)
tempOutput = QCOutput(f"{filename}.{i}")
to_return.append(tempOutput)
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/io/xtb/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def write_input_files(self):
reference_fnm=self.coords_filename,
constraints=self.constraints,
)
with open(".constrains", mode="w") as file:
with open(".constrains", mode="w", encoding="utf-8") as file:
file.write(constrains_string)

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/phonon/thermal_displacements.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def write_cif(self, filename: str) -> None:
writer.write_file(filename)
# This will simply append the thermal displacement part to the CIF from the CifWriter
# In the long run, CifWriter could be extended to handle thermal displacement matrices
with open(filename, mode="a") as file:
with open(filename, mode="a", encoding="utf-8") as file:
file.write("loop_ \n")
file.write("_atom_site_aniso_label\n")
file.write("_atom_site_aniso_U_11\n")
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/symmetry/maggroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,5 +560,5 @@ def _write_all_magnetic_space_groups_to_file(filename):
all_msgs = list(map(MagneticSpaceGroup, range(1, 1652)))
for msg in all_msgs:
out += f"\n{msg.data_str()}\n\n--------\n"
with open(filename, mode="w") as file:
with open(filename, mode="w", encoding="utf-8") as file:
file.write(out)

0 comments on commit 3f7b180

Please sign in to comment.