|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +from pymatgen.io.core import InputSet |
| 8 | +from pymatgen.io.jdftx.inputs import JDFTXInfile, JDFTXStructure |
| 9 | + |
| 10 | +if TYPE_CHECKING: |
| 11 | + from pymatgen.core import Structure |
| 12 | + from pymatgen.util.typing import PathLike |
| 13 | + |
| 14 | +FILE_NAMES = {"in": "init.in", "out": "jdftx.out"} |
| 15 | + |
| 16 | + |
| 17 | +class JdftxInputSet(InputSet): |
| 18 | + """ |
| 19 | + A class to represent a JDFTx input file as a JDFTx InputSet. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + jdftxinput |
| 24 | + A JdftxInput object |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__(self, jdftxinput: JDFTXInfile, structure: Structure) -> None: |
| 28 | + self.structure = structure |
| 29 | + self.jdftxinput = jdftxinput |
| 30 | + |
| 31 | + def write_input( |
| 32 | + self, |
| 33 | + directory: str | Path, |
| 34 | + infile: PathLike = FILE_NAMES["in"], |
| 35 | + make_dir: bool = True, |
| 36 | + overwrite: bool = True, |
| 37 | + ) -> None: |
| 38 | + """Write JDFTx input file to a directory. |
| 39 | +
|
| 40 | + Parameters |
| 41 | + ---------- |
| 42 | + directory |
| 43 | + Directory to write input files to. |
| 44 | + make_dir |
| 45 | + Whether to create the directory if it does not already exist. |
| 46 | + overwrite |
| 47 | + Whether to overwrite an input file if it already exists. |
| 48 | + """ |
| 49 | + directory = Path(directory) |
| 50 | + if make_dir: |
| 51 | + os.makedirs(directory, exist_ok=True) |
| 52 | + |
| 53 | + if not overwrite and (directory / infile).exists(): |
| 54 | + raise FileExistsError(f"{directory / infile} already exists.") |
| 55 | + jdftx_structure = JDFTXStructure(structure=self.structure) |
| 56 | + jdftxinput = condense_jdftxinputs(self.jdftxinput, jdftx_structure) |
| 57 | + |
| 58 | + jdftxinput.write_file(filename=(directory / infile)) |
| 59 | + |
| 60 | + @staticmethod |
| 61 | + def from_file( |
| 62 | + file: PathLike, |
| 63 | + ) -> JdftxInputSet: |
| 64 | + """Load a set of JDFTx inputs from a filename. |
| 65 | +
|
| 66 | + Parameters |
| 67 | + ---------- |
| 68 | + directory |
| 69 | + Input file to read JDFTx inputs from. |
| 70 | + """ |
| 71 | + jdftxinput = JDFTXInfile.from_file(file) |
| 72 | + structure = jdftxinput.structure |
| 73 | + if structure is None: |
| 74 | + raise ValueError(f"Structure not defined in file {file}.") |
| 75 | + return JdftxInputSet(jdftxinput=jdftxinput, structure=structure) |
| 76 | + |
| 77 | + |
| 78 | +def condense_jdftxinputs(jdftxinput: JDFTXInfile, jdftxstructure: JDFTXStructure) -> JDFTXInfile: |
| 79 | + """ |
| 80 | + Combine JDFTXInfile and JDFTxStructure into complete JDFTXInfile. |
| 81 | +
|
| 82 | + Function combines a JDFTXInfile class with calculation |
| 83 | + settings and a JDFTxStructure that defines the structure |
| 84 | + into one JDFTXInfile instance. |
| 85 | +
|
| 86 | + Parameters |
| 87 | + ---------- |
| 88 | + jdftxinput: JDFTXInfile |
| 89 | + A JDFTXInfile object with calculation settings. |
| 90 | +
|
| 91 | + jdftxstructure: JDFTXStructure |
| 92 | + A JDFTXStructure object that defines the structure. |
| 93 | +
|
| 94 | + Returns |
| 95 | + ------- |
| 96 | + JDFTXInfile |
| 97 | + A JDFTXInfile that includes the calculation |
| 98 | + parameters and input structure. |
| 99 | + """ |
| 100 | + # force Cartesian coordinates |
| 101 | + coords_type = jdftxinput.get("coords-type") |
| 102 | + return jdftxinput + JDFTXInfile.from_str(jdftxstructure.get_str(in_cart_coords=(coords_type == "Cartesian"))) |
0 commit comments