From b5e3758146ed085da3caf49c317982aec224aa71 Mon Sep 17 00:00:00 2001 From: Janosh Riebesell Date: Thu, 5 Oct 2023 10:58:53 -0700 Subject: [PATCH] add apply_strain() arg inplace: bool=True --- pymatgen/core/structure.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pymatgen/core/structure.py b/pymatgen/core/structure.py index e739d672d9e..626eb0f37cf 100644 --- a/pymatgen/core/structure.py +++ b/pymatgen/core/structure.py @@ -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: @@ -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