Skip to content

Commit

Permalink
Updated Site.charge() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
bjmorgan committed Jul 7, 2021
1 parent eba7f4c commit 91c6f0d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
14 changes: 8 additions & 6 deletions pyscses/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ def probabilities_as_list(self,
list(float): Probabilities of site occupation for each defect species.
"""
warnings.warn("Site.probabilities_as_list() is deprecated and targetted for removal. Please use Site.probabilities() instead.", DeprecationWarning)
warnings.warn("Site.probabilities_as_list() is deprecated and targeted for removal. Please use Site.probabilities() instead.", DeprecationWarning)
probabilities_dict = self.probabilities(phi=phi, temp=temp)
return [probabilities_dict[d.label] for d in self.defects]

def defect_valences(self) -> np.ndarray:
"""Returns an array of valences for each defect from self.defects """
"""Returns an array of valences for each defect in `self.defects`"""
return np.array([d.valence for d in self.defects])

def charge(self,
Expand All @@ -216,7 +216,9 @@ def charge(self,
float: The charge at this site.
"""
charge = (self.valence + np.sum(self.probabilities(phi, temp)
* self.defect_valences()
* self.scaling)) * fundamental_charge
return charge
defect_probabilities = self.probabilities(phi=phi, temp=temp)
charge = sum([defect_probabilities[d.label] * d.valence
for d in self.defects]) * self.scaling
charge += self.valence
charge *= fundamental_charge
return float(charge)
11 changes: 9 additions & 2 deletions tests/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pyscses.defect_at_site import DefectAtSite
from pyscses.site import Site, LabelError
from unittest.mock import Mock, patch
from pyscses.constants import fundamental_charge
import numpy as np

def create_mock_defect_species(n):
Expand Down Expand Up @@ -183,9 +184,15 @@ def test_probabilities_two(self):
temp=298.0),
{'A': exp_A, 'B': exp_B})

def test_probabilites_as_list(self):
def test_charge(self):
self.site.probabilities = Mock(return_value={'E': 0.1, 'D': 0.2})
self.site.probabilities_as_list(phi=1.0, temp=298.0)
self.site.defects[0].valence = 1.0
self.site.defects[1].valence = 2.0
self.site.scaling = 0.5
self.site.valence = 1.0
expected_value = ((1.0*0.1 + 2.0*0.2)*0.5 + 1.0) * fundamental_charge
self.assertEqual(self.site.charge(phi=1.0, temp=298.0),
expected_value)


if __name__ == '__main__':
Expand Down

0 comments on commit 91c6f0d

Please sign in to comment.