Skip to content

Commit 0d7e6cd

Browse files
lhycmsjanosh
andauthored
Add pymatgen.io.pwmat module (#3512)
* Add pymatgen.io.pwmat module * rm unitest inside pymatgen.io.pwmat dir * remove try,except expression: remove format expression: remove format expression: codespell * Add pwmat fmt within Structure.from_file() and Structure.from_str() * Remove abtractclass LineLocaterBase * Add output file of PWmat: Report, OUT.FERMI * Add output file of PWmat: DOS.totalspin, DOS.spinup, DOS.spindown * Rename part to part_upper to impove code quality to pass automatically test * add OutFermi, Report, Dosspin object to __init__.py in pwmat * Add input file named gen.kpt * Add high_symmetry_point * Add test for all new features * snake case variables, fix doc strings and return types * Revise comments and compress the test file. to pass test * Revise annotation again * Further modify the annotations * Further modify the annotations * Fix mispelling according to codespell * Modify annotation according to mypy * Modify annotation of filename: str -> PathLike * Add pwmat in FileFormats * Change the type annotation of filename to PathLike * Change type annotation: np.array -> np.ndarray * Utilize mypy check locally to pass 21 tests for pymatgen.io.pwmat * fix typos * del tests/io/pwmat/__init__.py * more informative unrecognized file extension error in Structure.from_file() * fnmatch compressed *.config* or *.pwmat* files * compress test files * snake_case dict keys and method names * don't write tmp test files to git repo * re.escape err msg * drop filename from expected err msg * Add test for Structure pwmat IO format * Add test for Structure pwmat IO format * Try to pass test in win * Decompress the test file and reduce the number of lines. * Add test for Structure.from_file() and Structure.to() in pwmat format * improve test_inputs.py assertions --------- Signed-off-by: LiuHanyu <[email protected]> Co-authored-by: Janosh Riebesell <[email protected]>
1 parent e6cc11c commit 0d7e6cd

File tree

12 files changed

+1470
-5
lines changed

12 files changed

+1470
-5
lines changed

pymatgen/core/structure.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
from pymatgen.util.typing import CompositionLike, SpeciesLike
6262

63-
FileFormats = Literal["cif", "poscar", "cssr", "json", "yaml", "yml", "xsf", "mcsqs", "res", ""]
63+
FileFormats = Literal["cif", "poscar", "cssr", "json", "yaml", "yml", "xsf", "mcsqs", "res", "pwmat", ""]
6464

6565

6666
class Neighbor(Site):
@@ -2671,7 +2671,7 @@ def to(self, filename: str | Path = "", fmt: FileFormats = "", **kwargs) -> str:
26712671
fmt (str): Format to output to. Defaults to JSON unless filename
26722672
is provided. If fmt is specifies, it overrides whatever the
26732673
filename is. Options include "cif", "poscar", "cssr", "json",
2674-
"xsf", "mcsqs", "prismatic", "yaml", "yml", "fleur-inpgen".
2674+
"xsf", "mcsqs", "prismatic", "yaml", "yml", "fleur-inpgen", "pwmat".
26752675
Non-case sensitive.
26762676
**kwargs: Kwargs passthru to relevant methods. E.g., This allows
26772677
the passing of parameters like symprec to the
@@ -2752,6 +2752,10 @@ def to(self, filename: str | Path = "", fmt: FileFormats = "", **kwargs) -> str:
27522752
with zopen(filename, mode="wt", encoding="utf8") as file:
27532753
file.write(res_str)
27542754
return res_str
2755+
elif fmt == "pwmat" or fnmatch(filename.lower(), "*.pwmat") or fnmatch(filename.lower(), "*.config"):
2756+
from pymatgen.io.pwmat import AtomConfig
2757+
2758+
writer = AtomConfig(self, **kwargs)
27552759
else:
27562760
if fmt == "":
27572761
raise ValueError(f"Format not specified and could not infer from {filename=}")
@@ -2832,6 +2836,10 @@ def from_str( # type: ignore[override]
28322836
from pymatgen.io.res import ResIO
28332837

28342838
struct = ResIO.structure_from_str(input_string, **kwargs)
2839+
elif fmt == "pwmat":
2840+
from pymatgen.io.pwmat import AtomConfig
2841+
2842+
struct = AtomConfig.from_str(input_string, **kwargs).structure
28352843
else:
28362844
raise ValueError(f"Invalid {fmt=}, valid options are {get_args(FileFormats)}")
28372845

@@ -2910,8 +2918,12 @@ def from_file( # type: ignore[override]
29102918
from pymatgen.io.res import ResIO
29112919

29122920
struct = ResIO.structure_from_file(filename, **kwargs)
2921+
elif fnmatch(fname.lower(), "*.config*") or fnmatch(fname.lower(), "*.pwmat*"):
2922+
from pymatgen.io.pwmat import AtomConfig
2923+
2924+
struct = AtomConfig.from_file(filename, **kwargs).structure
29132925
else:
2914-
raise ValueError("Unrecognized file extension!")
2926+
raise ValueError(f"Unrecognized extension in {filename=}")
29152927
if sort:
29162928
struct = struct.get_sorted_structure()
29172929
if merge_tol:

pymatgen/io/pwmat/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""This package implements modules for input and output to and from PWmat."""
2+
from __future__ import annotations
3+
4+
from .inputs import AtomConfig
5+
from .outputs import DosSpin, Movement, OutFermi, Report

0 commit comments

Comments
 (0)