Skip to content

Commit

Permalink
Add pymatgen.io.pwmat module (#3512)
Browse files Browse the repository at this point in the history
* 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 <41718895+lhycms@users.noreply.github.com>
Co-authored-by: Janosh Riebesell <janosh.riebesell@gmail.com>
  • Loading branch information
lhycms and janosh authored Jan 18, 2024
1 parent e6cc11c commit 0d7e6cd
Show file tree
Hide file tree
Showing 12 changed files with 1,470 additions and 5 deletions.
18 changes: 15 additions & 3 deletions pymatgen/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

from pymatgen.util.typing import CompositionLike, SpeciesLike

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


class Neighbor(Site):
Expand Down Expand Up @@ -2671,7 +2671,7 @@ def to(self, filename: str | Path = "", fmt: FileFormats = "", **kwargs) -> str:
fmt (str): Format to output to. Defaults to JSON unless filename
is provided. If fmt is specifies, it overrides whatever the
filename is. Options include "cif", "poscar", "cssr", "json",
"xsf", "mcsqs", "prismatic", "yaml", "yml", "fleur-inpgen".
"xsf", "mcsqs", "prismatic", "yaml", "yml", "fleur-inpgen", "pwmat".
Non-case sensitive.
**kwargs: Kwargs passthru to relevant methods. E.g., This allows
the passing of parameters like symprec to the
Expand Down Expand Up @@ -2752,6 +2752,10 @@ def to(self, filename: str | Path = "", fmt: FileFormats = "", **kwargs) -> str:
with zopen(filename, mode="wt", encoding="utf8") as file:
file.write(res_str)
return res_str
elif fmt == "pwmat" or fnmatch(filename.lower(), "*.pwmat") or fnmatch(filename.lower(), "*.config"):
from pymatgen.io.pwmat import AtomConfig

writer = AtomConfig(self, **kwargs)
else:
if fmt == "":
raise ValueError(f"Format not specified and could not infer from {filename=}")
Expand Down Expand Up @@ -2832,6 +2836,10 @@ def from_str( # type: ignore[override]
from pymatgen.io.res import ResIO

struct = ResIO.structure_from_str(input_string, **kwargs)
elif fmt == "pwmat":
from pymatgen.io.pwmat import AtomConfig

struct = AtomConfig.from_str(input_string, **kwargs).structure
else:
raise ValueError(f"Invalid {fmt=}, valid options are {get_args(FileFormats)}")

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

struct = ResIO.structure_from_file(filename, **kwargs)
elif fnmatch(fname.lower(), "*.config*") or fnmatch(fname.lower(), "*.pwmat*"):
from pymatgen.io.pwmat import AtomConfig

struct = AtomConfig.from_file(filename, **kwargs).structure
else:
raise ValueError("Unrecognized file extension!")
raise ValueError(f"Unrecognized extension in {filename=}")
if sort:
struct = struct.get_sorted_structure()
if merge_tol:
Expand Down
5 changes: 5 additions & 0 deletions pymatgen/io/pwmat/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""This package implements modules for input and output to and from PWmat."""
from __future__ import annotations

from .inputs import AtomConfig
from .outputs import DosSpin, Movement, OutFermi, Report
Loading

0 comments on commit 0d7e6cd

Please sign in to comment.