From 16094f9739ab9549512b02d42037498b58252084 Mon Sep 17 00:00:00 2001
From: Richard West <r.west@northeastern.edu>
Date: Sat, 16 Nov 2019 21:34:38 -0500
Subject: [PATCH] Don't add radicals or lone pairs to surface sites.

This may hide bugs, or it may be exactly the right
thing to do. I'm not entirely sure.
But it currently seems like a good idea.

The presumption is:
surface sites of metals should not have unpaired
or pairs of electrons - they just have a big sea
of delocalized electrons at their disposal.

See https://github.com/ReactionMechanismGenerator/RMG-Py/issues/1820
---
 rmgpy/molecule/molecule.py     |  4 ++++
 rmgpy/molecule/moleculeTest.py | 10 ++++++++--
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/rmgpy/molecule/molecule.py b/rmgpy/molecule/molecule.py
index bbd8da2431e..4ba70b63028 100644
--- a/rmgpy/molecule/molecule.py
+++ b/rmgpy/molecule/molecule.py
@@ -410,6 +410,7 @@ def increment_radical(self):
         where `radical` specifies the number of radical electrons to add.
         """
         # Set the new radical electron count
+        if self.is_surface_site(): return # do nothing
         self.radical_electrons += 1
         if self.radical_electrons <= 0:
             raise gr.ActionError('Unable to update Atom due to GAIN_RADICAL action: '
@@ -421,6 +422,7 @@ def decrement_radical(self):
         where `radical` specifies the number of radical electrons to remove.
         """
         cython.declare(radical_electrons=cython.short)
+        if self.is_surface_site(): return # do nothing
         # Set the new radical electron count
         radical_electrons = self.radical_electrons = self.radical_electrons - 1
         if radical_electrons < 0:
@@ -443,6 +445,7 @@ def increment_lone_pairs(self):
         Update the lone electron pairs pattern as a result of applying a GAIN_PAIR action.
         """
         # Set the new lone electron pairs count
+        if self.is_surface_site(): return # do nothing
         self.lone_pairs += 1
         if self.lone_pairs <= 0:
             raise gr.ActionError('Unable to update Atom due to GAIN_PAIR action: '
@@ -453,6 +456,7 @@ def decrement_lone_pairs(self):
         """
         Update the lone electron pairs pattern as a result of applying a LOSE_PAIR action.
         """
+        if self.is_surface_site(): return # do nothing
         # Set the new lone electron pairs count
         self.lone_pairs -= 1
         if self.lone_pairs < 0:
diff --git a/rmgpy/molecule/moleculeTest.py b/rmgpy/molecule/moleculeTest.py
index 40a998bba73..36c872ec123 100644
--- a/rmgpy/molecule/moleculeTest.py
+++ b/rmgpy/molecule/moleculeTest.py
@@ -312,7 +312,10 @@ def test_apply_action_gain_radical(self):
             atom = atom0.copy()
             atom.apply_action(action)
             self.assertEqual(atom0.element, atom.element)
-            self.assertEqual(atom0.radical_electrons, atom.radical_electrons - 1)
+            if element.symbol == 'X':
+                self.assertEqual(atom0.radical_electrons, atom.radical_electrons)
+            else:
+                self.assertEqual(atom0.radical_electrons, atom.radical_electrons - 1)
             self.assertEqual(atom0.charge, atom.charge)
             self.assertEqual(atom0.label, atom.label)
 
@@ -326,7 +329,10 @@ def test_apply_action_lose_radical(self):
             atom = atom0.copy()
             atom.apply_action(action)
             self.assertEqual(atom0.element, atom.element)
-            self.assertEqual(atom0.radical_electrons, atom.radical_electrons + 1)
+            if element.symbol == 'X':
+                self.assertEqual(atom0.radical_electrons, atom.radical_electrons)
+            else:
+                self.assertEqual(atom0.radical_electrons, atom.radical_electrons + 1)
             self.assertEqual(atom0.charge, atom.charge)
             self.assertEqual(atom0.label, atom.label)