Skip to content

Commit

Permalink
Merge pull request #34 from benrich37/inout_sepr
Browse files Browse the repository at this point in the history
input/output separation
  • Loading branch information
benrich37 authored Nov 21, 2024
2 parents 79fdae8 + 8c2ba37 commit 505d046
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 69 deletions.
38 changes: 38 additions & 0 deletions src/pymatgen/io/jdftx/_input_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Module for JDFTx IO module input utils.
Module for JDFTx IO module input utils. Functions kept in this module are here if they are
used by multiple submodules, or if they are anticipated to be used by multiple
submodules in the future.
"""

from __future__ import annotations

from typing import Any


def flatten_list(tag: str, list_of_lists: list[Any]) -> list[Any]:
"""Flatten list of lists into a single list, then stop.
Flatten list of lists into a single list, then stop.
Parameters
----------
tag : str
The tag to flatten the list of lists for.
list_of_lists : list[Any]
The list of lists to flatten.
Returns
-------
list[Any]
The flattened list.
"""
if not isinstance(list_of_lists, list):
raise TypeError(f"{tag}: You must provide a list to flatten_list()!")
flist = []
for v in list_of_lists:
if isinstance(v, list):
flist.extend(flatten_list(tag, v))
else:
flist.append(v)
return flist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Module for JDFTx IO module utils.
"""Module for JDFTx IO module output utils.
Module for JDFTx IO module utils. Functions kept in this module are here if they are
Module for JDFTx IO module output utils. Functions kept in this module are here if they are
used by multiple submodules, or if they are anticipated to be used by multiple
submodules in the future.
Expand Down Expand Up @@ -136,34 +136,6 @@ def multi_getattr(varbase: Any, varname: str):
return varbase


def flatten_list(tag: str, list_of_lists: list[Any]) -> list[Any]:
"""Flatten list of lists into a single list, then stop.
Flatten list of lists into a single list, then stop.
Parameters
----------
tag : str
The tag to flatten the list of lists for.
list_of_lists : list[Any]
The list of lists to flatten.
Returns
-------
list[Any]
The flattened list.
"""
if not isinstance(list_of_lists, list):
raise TypeError(f"{tag}: You must provide a list to flatten_list()!")
flist = []
for v in list_of_lists:
if isinstance(v, list):
flist.extend(flatten_list(tag, v))
else:
flist.append(v)
return flist


def _brkt_list_of_3_to_nparray(line: str) -> np.ndarray:
"""Return 3x1 numpy array.
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/io/jdftx/generic_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import numpy as np

from pymatgen.io.jdftx._utils import flatten_list
from pymatgen.io.jdftx._input_utils import flatten_list

__author__ = "Jacob Clary, Ben Rich"

Expand Down
64 changes: 33 additions & 31 deletions src/pymatgen/io/jdftx/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class is written.

from pymatgen.core import Structure
from pymatgen.core.periodic_table import Element
from pymatgen.io.jdftx._utils import multi_getattr, multi_hasattr
from pymatgen.io.jdftx._output_utils import multi_getattr, multi_hasattr
from pymatgen.io.jdftx.generic_tags import AbstractTag, BoolTagContainer, DumpTagContainer, MultiformatTag, TagContainer
from pymatgen.io.jdftx.jdftxinfile_master_format import (
__PHONON_TAGS__,
Expand All @@ -41,8 +41,6 @@ class is written.
from numpy.typing import ArrayLike
from typing_extensions import Self

from pymatgen.io.jdftx.jdftxoutfile import JDFTXOutfile
from pymatgen.io.jdftx.joutstructure import JOutStructure
from pymatgen.util.typing import PathLike

__author__ = "Jacob Clary, Ben Rich"
Expand Down Expand Up @@ -754,34 +752,38 @@ def from_jdftxinfile(cls, jdftxinfile: JDFTXInfile, sort_structure: bool = False
)
return cls(struct, selective_dynamics, sort_structure=sort_structure)

@classmethod
def from_jdftxoutfile(cls, jdftxoutfile: JDFTXOutfile) -> JDFTXStructure:
"""Get JDFTXStructure from JDFTXOutfile.
Args:
jdftxoutfile (JDFTXOutfile): JDFTXOutfile object.
Returns:
JDFTXStructure: The created JDFTXStructure object.
"""
if not len(jdftxoutfile.jstrucs):
raise ValueError("No structures found in JDFTXOutfile")
joutstructure = jdftxoutfile.jstrucs[-1]
return cls.from_joutstructure(joutstructure)

@classmethod
def from_joutstructure(cls, joutstructure: JOutStructure) -> JDFTXStructure:
"""Get JDFTXStructure from JOutStructure.
Args:
joutstructure (JOutStructure): JOutStructure object.
Returns:
JDFTXStructure: The created JDFTXStructure object.
"""
struct = Structure(joutstructure.lattice, joutstructure.species, joutstructure.coords)
selective_dynamics = joutstructure.selective_dynamics
return cls(struct, selective_dynamics, sort_structure=False)
# NOTE: The following methods are commented out so that the input and output sections of this code can be merged
# independently
# TODO: Uncomment these methods once the input and output sections of this code are merged, and add more methods
# to create input objects from output objects.
# @classmethod
# def from_jdftxoutfile(cls, jdftxoutfile: JDFTXOutfile) -> JDFTXStructure:
# """Get JDFTXStructure from JDFTXOutfile.

# Args:
# jdftxoutfile (JDFTXOutfile): JDFTXOutfile object.

# Returns:
# JDFTXStructure: The created JDFTXStructure object.
# """
# if not len(jdftxoutfile.jstrucs):
# raise ValueError("No structures found in JDFTXOutfile")
# joutstructure = jdftxoutfile.jstrucs[-1]
# return cls.from_joutstructure(joutstructure)

# @classmethod
# def from_joutstructure(cls, joutstructure: JOutStructure) -> JDFTXStructure:
# """Get JDFTXStructure from JOutStructure.

# Args:
# joutstructure (JOutStructure): JOutStructure object.

# Returns:
# JDFTXStructure: The created JDFTXStructure object.
# """
# struct = Structure(joutstructure.lattice, joutstructure.species, joutstructure.coords)
# selective_dynamics = joutstructure.selective_dynamics
# return cls(struct, selective_dynamics, sort_structure=False)

def get_str(self, in_cart_coords: bool = False) -> str:
"""Return a string to be written as JDFTXInfile tags.
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/io/jdftx/jdftxoutfileslice.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from pymatgen.core.periodic_table import Element
from pymatgen.core.trajectory import Trajectory
from pymatgen.core.units import Ha_to_eV, ang_to_bohr
from pymatgen.io.jdftx._utils import (
from pymatgen.io.jdftx._output_utils import (
find_all_key,
find_first_range_key,
find_key,
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/io/jdftx/jelstep.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from typing import Any, ClassVar

from pymatgen.core.units import Ha_to_eV
from pymatgen.io.jdftx._utils import get_colon_var_t1
from pymatgen.io.jdftx._output_utils import get_colon_var_t1

__author__ = "Ben Rich"

Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/io/jdftx/joutstructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from pymatgen.core.structure import Lattice, Structure
from pymatgen.core.units import Ha_to_eV, bohr_to_ang
from pymatgen.io.jdftx._utils import (
from pymatgen.io.jdftx._output_utils import (
_brkt_list_of_3x3_to_nparray,
correct_geom_opt_type,
get_colon_var_t1,
Expand Down
4 changes: 2 additions & 2 deletions src/pymatgen/io/jdftx/joutstructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

from pymatgen.core.structure import Structure
from pymatgen.core.units import bohr_to_ang
from pymatgen.io.jdftx._utils import correct_geom_opt_type, is_lowdin_start_line
from pymatgen.io.jdftx._output_utils import correct_geom_opt_type, is_lowdin_start_line

if TYPE_CHECKING:
from pymatgen.io.jdftx.jelstep import JElSteps
from pymatgen.io.jdftx._utils import find_first_range_key
from pymatgen.io.jdftx._output_utils import find_first_range_key
from pymatgen.io.jdftx.joutstructure import JOutStructure

__author__ = "Ben Rich"
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/io/jdftx/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class is written.
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any

from pymatgen.io.jdftx._utils import read_outfile_slices
from pymatgen.io.jdftx._output_utils import read_outfile_slices
from pymatgen.io.jdftx.jdftxoutfileslice import JDFTXOutfileSlice

if TYPE_CHECKING:
Expand Down
3 changes: 2 additions & 1 deletion tests/io/jdftx/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import pytest

from pymatgen.io.jdftx._utils import find_first_range_key, flatten_list, get_start_lines, multi_getattr, multi_hasattr
from pymatgen.io.jdftx._input_utils import flatten_list
from pymatgen.io.jdftx._output_utils import find_first_range_key, get_start_lines, multi_getattr, multi_hasattr
from pymatgen.io.jdftx.joutstructures import _get_joutstructures_start_idx


Expand Down

0 comments on commit 505d046

Please sign in to comment.