diff --git a/phasecalculator/classes/phase.py b/phasecalculator/classes/phase.py index 4c6afc59eaf58c405c79c593d5e4494bfc5f5ae0..741b7531a29d2e2674debe0fa6a37e6647028bbd 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.