Skip to content

Commit 57d7f57

Browse files
authored
adding radd dunder method to Volumetric data + test_outputs (#4417)
* adding radd dunder method to Volumetric data + test_outputs * more robust with warnings * swapped dos and volumetric, my bad * fixing indentation
1 parent 9fea170 commit 57d7f57

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/pymatgen/io/common.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ def get_axis_grid(self, ind):
129129
def __add__(self, other):
130130
return self.linear_add(other, 1.0)
131131

132+
def __radd__(self, other):
133+
if other == 0 or other is None:
134+
# sum() calls 0 + self first; we treat 0 as the identity element
135+
return self
136+
if isinstance(other, self.__class__):
137+
return self.__add__(other)
138+
139+
raise TypeError(f"Unsupported operand type(s) for +: '{type(other).__name__}' and '{type(self).__name__}'")
140+
132141
def __sub__(self, other):
133142
return self.linear_add(other, -1.0)
134143

tests/io/vasp/test_outputs.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,9 @@ def test_init(self):
15851585
chgcar = self.chgcar_spin + self.chgcar_spin
15861586
assert chgcar.get_integrated_diff(0, 1)[0, 1] == approx(-0.0043896932237534022 * 2)
15871587

1588+
chgcar = sum([self.chgcar_spin, self.chgcar_spin])
1589+
assert chgcar.get_integrated_diff(0, 1)[0, 1] == approx(-0.0043896932237534022 * 2)
1590+
15881591
chgcar = self.chgcar_spin - self.chgcar_spin
15891592
assert chgcar.get_integrated_diff(0, 1)[0, 1] == approx(0)
15901593

0 commit comments

Comments
 (0)