Skip to content

Commit

Permalink
pass kwargs to IStructure.to
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Feb 19, 2025
1 parent 389ec50 commit 992bcaa
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/pymatgen/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2925,17 +2925,17 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
fmt is not specified, the format is determined from the
filename. Defaults is None, i.e. string output.
fmt (str): Format to output to. Defaults to JSON unless filename
is provided. If fmt is specifies, it overrides whatever the
is provided. If specified, it overrides whatever the
filename is. Options include "cif", "poscar", "cssr", "json",
"xsf", "mcsqs", "prismatic", "yaml", "yml", "fleur-inpgen", "pwmat",
"aims".
Non-case sensitive.
**kwargs: Kwargs passthru to relevant methods. e.g. This allows
the passing of parameters like symprec to the
CifWriter.__init__ method for generation of symmetric CIFs.
Case insensitive.
**kwargs: Kwargs pass thru to relevant methods. This allows
the passing of parameters like `symprec` to the
`CifWriter.__init__ method` for generation of symmetric CIFs.
Returns:
str: String representation of molecule in given format. If a filename
str: String representation of structure in given format. If a filename
is provided, the same string is written to the file.
"""
filename, fmt = str(filename), cast(FileFormats, fmt.lower())
Expand All @@ -2944,24 +2944,29 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
from pymatgen.io.cif import CifWriter

writer: Any = CifWriter(self, **kwargs)

elif fmt == "mcif" or fnmatch(filename.lower(), "*.mcif*"):
from pymatgen.io.cif import CifWriter

writer = CifWriter(self, write_magmoms=True, **kwargs)

elif fmt == "poscar" or fnmatch(filename, "*POSCAR*"):
from pymatgen.io.vasp import Poscar

writer = Poscar(self, **kwargs)

elif fmt == "cssr" or fnmatch(filename.lower(), "*.cssr*"):
from pymatgen.io.cssr import Cssr

writer = Cssr(self)

elif fmt == "json" or fnmatch(filename.lower(), "*.json*"):
json_str = json.dumps(self.as_dict())
json_str = json.dumps(self.as_dict(), **kwargs)
if filename:
with zopen(filename, mode="wt", encoding="utf-8") as file:
file.write(json_str)
return json_str

elif fmt == "xsf" or fnmatch(filename.lower(), "*.xsf*"):
from pymatgen.io.xcrysden import XSF

Expand All @@ -2970,6 +2975,7 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
with zopen(filename, mode="wt", encoding="utf-8") as file:
file.write(res_str)
return res_str

elif (
fmt == "mcsqs"
or fnmatch(filename, "*rndstr.in*")
Expand All @@ -2983,10 +2989,12 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
with zopen(filename, mode="wt", encoding="ascii") as file:
file.write(res_str)
return res_str

elif fmt == "prismatic" or fnmatch(filename, "*prismatic*"):
from pymatgen.io.prismatic import Prismatic

return Prismatic(self).to_str()

elif fmt in ("yaml", "yml") or fnmatch(filename, "*.yaml*") or fnmatch(filename, "*.yml*"):
yaml = YAML()
str_io = io.StringIO()
Expand All @@ -2996,6 +3004,7 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
with zopen(filename, mode="wt", encoding="utf-8") as file:
file.write(yaml_str)
return yaml_str

elif fmt == "aims" or fnmatch(filename, "geometry.in"):
from pymatgen.io.aims.inputs import AimsGeometryIn

Expand All @@ -3006,11 +3015,13 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
file.write(geom_in.content)
file.write("\n")
return geom_in.content

# fleur support implemented in external namespace pkg https://github.com/JuDFTteam/pymatgen-io-fleur
elif fmt == "fleur-inpgen" or fnmatch(filename, "*.in*"):
from pymatgen.io.fleur import FleurInput

writer = FleurInput(self, **kwargs)

elif fmt == "res" or fnmatch(filename, "*.res"):
from pymatgen.io.res import ResIO

Expand All @@ -3019,10 +3030,12 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
with zopen(filename, mode="wt", encoding="utf-8") 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

0 comments on commit 992bcaa

Please sign in to comment.