diff --git a/pymatgen/io/vasp/outputs.py b/pymatgen/io/vasp/outputs.py index f7503ea58f5..2bdfa425cf3 100644 --- a/pymatgen/io/vasp/outputs.py +++ b/pymatgen/io/vasp/outputs.py @@ -2922,6 +2922,17 @@ def __add__(self, other): def __sub__(self, other): return self.linear_add(other, -1.0) + def copy(self): + """ + :return: Copy of Volumetric object + """ + return VolumetricData( + self.structure, + {k: v.copy() for k, v in self.data.items()}, + distance_matrix=self._distance_matrix, + data_aug=self.data_aug + ) + def linear_add(self, other, scale_factor=1.0): """ Method to do a linear sum of volumetric objects. Used by + and - @@ -2936,9 +2947,12 @@ def linear_add(self, other, scale_factor=1.0): VolumetricData corresponding to self + scale_factor * other. """ if self.structure != other.structure: - raise ValueError("Adding or subtraction operations can only be " - "performed for volumetric data with the exact " - "same structure.") + warnings.warn("Structures are different. Make sure you know what " + "you are doing...") + if self.data.keys() != other.data.keys(): + raise ValueError("Data have different keys! Maybe one is spin-" + "polarized and the other is not?") + # To add checks data = {} for k in self.data.keys(): diff --git a/pymatgen/io/vasp/tests/test_outputs.py b/pymatgen/io/vasp/tests/test_outputs.py index 0cfecd63ff9..c5d00262a10 100644 --- a/pymatgen/io/vasp/tests/test_outputs.py +++ b/pymatgen/io/vasp/tests/test_outputs.py @@ -1135,6 +1135,22 @@ def test_spin_data(self): for k, v in d.items(): self.assertEqual(v.shape, (48, 48, 48)) + def test_add(self): + chgcar_sum = self.chgcar_spin + self.chgcar_spin + self.assertArrayAlmostEqual(chgcar_sum.data['total'], self.chgcar_spin.data['total'] * 2) + chgcar_copy = self.chgcar_spin.copy() + chgcar_copy.structure = self.get_structure("Li2O") + with warnings.catch_warnings(record=True) as w: + # Cause all warnings to always be triggered. + warnings.simplefilter("always") + # Trigger a warning. + chgcar_sum = chgcar_copy + self.chgcar_spin + # Verify some things + assert len(w) == 1 + assert "Structures are different. Make sure you know what you are doing..." in str(w[-1].message) + self.assertRaises(ValueError, self.chgcar_spin.__add__, self.chgcar_fe3o4) + self.assertRaises(ValueError, self.chgcar_spin.__add__, self.chgcar_no_spin) + def test_as_dict_and_from_dict(self): d = self.chgcar_NiO_SOC.as_dict() chgcar_from_dict = Chgcar.from_dict(d)