Skip to content

Commit

Permalink
add apply_strain() arg inplace: bool=True
Browse files Browse the repository at this point in the history
  • Loading branch information
janosh committed Oct 5, 2023
1 parent 0bf11eb commit b5e3758
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pymatgen/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -3968,7 +3968,7 @@ def operate_site(site):

return self

def apply_strain(self, strain: ArrayLike) -> None:
def apply_strain(self, strain: ArrayLike, inplace: bool = True) -> Structure:
"""Apply a strain to the lattice.
Args:
Expand All @@ -3977,9 +3977,17 @@ def apply_strain(self, strain: ArrayLike) -> None:
vectors are increased by 1%. This is equivalent to calling
modify_lattice with a lattice with lattice parameters that
are 1% larger.
inplace (bool): True applies the strain in-place, False returns a
Structure copy. Defaults to True.
Returns:
Structure: Structure with strain applied.
"""
strain_matrix = (1 + np.array(strain)) * np.eye(3)
self.lattice = Lattice(np.dot(self._lattice.matrix.T, strain_matrix).T)
new_lattice = Lattice(np.dot(self._lattice.matrix.T, strain_matrix).T)
struct = self if inplace else self.copy()
struct.lattice = new_lattice
return struct

def sort(self, key: Callable | None = None, reverse: bool = False) -> None:
"""Sort a structure in place. The parameters have the same meaning as in
Expand Down

0 comments on commit b5e3758

Please sign in to comment.