From 7501532a1daa09ed35ca0614d84d446b7aa0540b Mon Sep 17 00:00:00 2001
From: Mark Driver <mdd31@alumni.cam.ac.uk>
Date: Fri, 27 Mar 2020 16:52:59 +0000
Subject: [PATCH] update to add check to make sure phases have fully assigned
 molefractions during initialisation.

---
 phasecalculator/classes/phase.py | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/phasecalculator/classes/phase.py b/phasecalculator/classes/phase.py
index 4c6afc5..741b753 100755
--- a/phasecalculator/classes/phase.py
+++ b/phasecalculator/classes/phase.py
@@ -55,6 +55,8 @@ class Phase(object):
         """
         self.molecule_list = molecule_list
         self.temperature = temperature
+        if not self.total_molefraction_is_one():
+            raise ValueError("Total molefractions of phase not equal to one")
 
     def __eq__(self, other):
         """Overload equality comparison operator.
@@ -146,6 +148,20 @@ class Phase(object):
         """
         return Temperature.parse_xml(temperature_xml)
 
+    def total_molefraction_is_one(self):
+        """Calculate total molefraction of the molecule and check this is 1.0.
+
+        Returns
+        -------
+        bool
+            True if total molefraction of components is one.
+
+        """
+        molefraction_total = 0.0
+        for mole_frac in self.get_molefractions_by_molecule().values():
+            molefraction_total += mole_frac
+        return (abs(1.0-molefraction_total) < 1e-5)
+
     def write_to_xml(self):
         """Write information to Etree representation of XML.
 
-- 
GitLab