Skip to content

Commit

Permalink
Relax criteria for structure to be the same. Now just raises warning.
Browse files Browse the repository at this point in the history
Further added tests to ensure correct behavior. Fixes #1615.
  • Loading branch information
shyuep committed Oct 2, 2019
1 parent 4fffd3e commit 1e23cdb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
20 changes: 17 additions & 3 deletions pymatgen/io/vasp/outputs.py
Expand Up @@ -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 -
Expand All @@ -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():
Expand Down
16 changes: 16 additions & 0 deletions pymatgen/io/vasp/tests/test_outputs.py
Expand Up @@ -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)
Expand Down

0 comments on commit 1e23cdb

Please sign in to comment.