Skip to content

Commit 0ed988f

Browse files
committed
Changes based on PR SCI-1404
1 parent 2279f8e commit 0ed988f

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

scripts/surface_charge/surface_charge_calculator.py

+14-21
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,28 @@ def __init__(self, crystal, use_existing_charges: bool = False):
3131
self.surface_atom_charge = np.nan
3232
self.node_projected_charge = np.nan
3333
self.node_representative_charge = np.nan
34+
self.triangles_properties = dict()
3435

3536
@staticmethod
3637
def sum_atom_charge(atoms: List[object]) -> float:
3738
return np.round(np.sum([atom.partial_charge for atom in atoms]), 3)
3839

39-
def get_average_node_charge(self):
40+
def get_node_charge(self):
4041
self.node_charge_dictionary = {}
4142
node_list = list(self.surface.topology.nodes)
4243
for node, atoms in self.surface.surface_node_atom_contacts.items():
4344
node_index = node_list.index(node)
44-
average_node_charge = 0
45+
total_node_charge = 0
4546
if len(atoms) > 0:
46-
average_node_charge = self.sum_atom_charge(atoms)
47-
self.node_charge_dictionary[node_index] = average_node_charge
47+
total_node_charge = self.sum_atom_charge(atoms)
48+
self.node_charge_dictionary[node_index] = total_node_charge
4849

4950
def calculate_triangles_properties(self,
50-
tri_index: List[Tuple[int, int, int]]) -> Dict[
51-
Tuple[int, int, int], Dict[str, float]]:
51+
tri_index: List[Tuple[int, int, int]]) -> None:
5252
surface_area = self.surface.descriptors.surface_area
5353
self.triangles_properties = {}
5454
triangle_areas = self.calculate_area_of_triangles(list(self.surface.topology.triangles))
55-
total_triangle_area = sum(triangle_areas)
55+
5656
for node_index, triangle_area in zip(tri_index, triangle_areas):
5757
average_triangle_charge = np.mean([self.node_charge_dictionary[i] for i in node_index])
5858
triangle_representation = triangle_area / surface_area
@@ -68,7 +68,7 @@ def calculate_triangles_properties(self,
6868
def calculate_node_charges(self):
6969
tri_index = self.calculated_node_index_values(list(self.surface.topology.nodes),
7070
list(self.surface.topology.triangles))
71-
self.get_average_node_charge()
71+
self.get_node_charge()
7272
self.calculate_triangles_properties(tri_index)
7373
self.representative_charge = np.sum(
7474
[triangle['Node Representative Charge'] for triangle in self.triangles_properties.values()])
@@ -83,22 +83,14 @@ def calculate_length(origin: np.ndarray, target: np.ndarray) -> float:
8383
return np.linalg.norm(target - origin)
8484

8585
@staticmethod
86-
def compute_triangle_area(a: float, b: float, c: float) -> float:
87-
"""Calculates area of triangle using Heron's formula"""
88-
s = (a + b + c) / 2
89-
return np.sqrt(s * (s - a) * (s - b) * (s - c))
86+
def compute_simplex_area(simplex: np.ndarray) -> float:
87+
vec_1 = simplex[1] - simplex[0]
88+
vec_2 = simplex[2] - simplex[0]
89+
return np.linalg.norm(np.cross(vec_1, vec_2)) / 2
9090

9191
def calculate_area_of_triangles(self, triangles: List) -> List:
9292
""" Calculates area of individual triangles from node positions using Heron's formula"""
93-
triangle_areas = []
94-
for triangle in triangles:
95-
pos_0, pos_1, pos_2 = np.array(triangle[0]), np.array(triangle[1]), np.array(triangle[2]),
96-
a_dist = self.calculate_length(pos_0, pos_1)
97-
b_dist = self.calculate_length(pos_0, pos_2)
98-
c_dist = self.calculate_length(pos_1, pos_2)
99-
triangle_areas.append(self.compute_triangle_area(a_dist, b_dist, c_dist))
100-
101-
return triangle_areas
93+
return [self.compute_simplex_area(np.array(triangle)) for triangle in triangles]
10294

10395
@staticmethod
10496
def calculated_node_index_values(nodes: List, triangles: List) -> List:
@@ -222,6 +214,7 @@ def generate_html_table(self):
222214
html += """
223215
</table>
224216
<p><i> *-Surface charge is based on gasteiger partial charges <a href="https://www.sciencedirect.com/science/article/pii/S0040403901949779?via%3Dihub">10.1016/S0040-4039(01)94977-9</a></i> </p>
217+
<p> Topological surface charge is defined as the average triangle charge on the surface multiplied by the % area contribution towards the total surface. </p>
225218
</body>
226219
</html>
227220
"""

0 commit comments

Comments
 (0)