KingsburyLab / pyEQL

A Python library for solution chemistry
Other
66 stars 21 forks source link

get_amount() ignores [mass]/[volume] and [mass]/[mass] units #12

Closed rkingsbury closed 9 years ago

rkingsbury commented 9 years ago

get_amount() fails to return any output when mass/volume or mass/mass units are specified. It works fine with substance/volume, substance/mass, substance, or mass units

>>> s1=pyEQL.Solution()
>>> s1.get_amount('H+','mol/L')
<Quantity(1e-07, 'mole / liter')>
>>> s1.get_amount('H+','mol')
<Quantity(1e-07, 'mole')>
>>> s1.get_amount('H+','mg')
<Quantity(0.000100794, 'milligram')>
>>> s1.get_amount('H+','mg/L')
>>>
rkingsbury commented 9 years ago

I've traced this to some improperly written conditional statements in the get_amount() method. Since the malfunctioning unit types are listed after the or, they are never property compared with the dimensionality of the input units.

        if unit(units).dimensionality == ('[substance]/[length]**3' or '[mass]/[length]**3'):
            return moles.to(units,'chem',mw=mw,volume=self.get_volume())
        elif unit(units).dimensionality == ('[substance]/[mass]' or '[mass]/[mass]'):
            return moles.to(units,'chem',mw=mw,solvent_mass=self.get_solvent_mass())
        elif unit(units).dimensionality == ('[mass]'):
            return moles.to(units,'chem',mw=mw)
        elif unit(units).dimensionality == ('[substance]'):
            return moles.to(units)
        else:
            logger.error('Unsupported unit specified for get_amount')
            return None
rkingsbury commented 9 years ago

Fixed in v0.2.2. Closing.