CalebBell / thermo

Thermodynamics and Phase Equilibrium component of Chemical Engineering Design Library (ChEDL)
MIT License
594 stars 114 forks source link

Creating instance of Chemical('helium') throws error #2

Closed kkremitzki closed 7 years ago

kkremitzki commented 7 years ago
>>> thermo.chemical.Chemical('helium')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/kurt/.anaconda3/lib/python3.5/site-packages/thermo/chemical.py", line 122, in __init__
    self.set_ref()
  File "/home/kurt/.anaconda3/lib/python3.5/site-packages/thermo/chemical.py", line 503, in set_ref
    self.H_int_l_Tm_to_Tb = integrators['l'](self.Tm, self.Tb)
  File "/home/kurt/.anaconda3/lib/python3.5/site-packages/thermo/utils.py", line 1630, in T_dependent_property_integral
    sorted_valid_methods = self.select_valid_methods(T1)
  File "/home/kurt/.anaconda3/lib/python3.5/site-packages/thermo/utils.py", line 1186, in select_valid_methods
    if self.test_method_validity(T, method):
  File "/home/kurt/.anaconda3/lib/python3.5/site-packages/thermo/heat_capacity.py", line 1253, in test_method_validity
    if T <= self.CP_f.Tt or T >= self.CP_f.Tc:
TypeError: unorderable types: NoneType() <= float()
kkremitzki commented 7 years ago

Appears to be Python 3 bug, can't reproduce in Python 2

CalebBell commented 7 years ago

This is a bug in the set_ref method run by default on a chemical's initialization. The following code has been edited to check that Tm and Tb are set before trying to use them, and wraps it in a trc/except block in case an integral fails unexpectedly. It will be a little while before this change makes it to pypi unfortunately.

    def set_ref(self, T_ref=298.15, P_ref=101325, phase_ref='calc', H_ref=0, S_ref=0):
        # Muse run after set_TP_sources, set_phase due to HeatCapacity*, phase_STP
        self.T_ref = getattr(self, T_ref) if isinstance(T_ref, str) else T_ref
        self.P_ref = getattr(self, P_ref) if isinstance(P_ref, str) else P_ref
        self.H_ref = getattr(self, H_ref) if isinstance(H_ref, str) else H_ref
        self.S_ref = getattr(self, S_ref) if isinstance(S_ref, str) else S_ref
        self.phase_ref = self.phase_STP if phase_ref == 'calc' else phase_ref

        integrators = {'s': self.HeatCapacitySolid.T_dependent_property_integral,
           'l': self.HeatCapacityLiquid.T_dependent_property_integral,
           'g': self.HeatCapacityGas.T_dependent_property_integral}

        integrators_T = {'s': self.HeatCapacitySolid.T_dependent_property_integral_over_T,
           'l': self.HeatCapacityLiquid.T_dependent_property_integral_over_T,
           'g': self.HeatCapacityGas.T_dependent_property_integral_over_T}

        # Integrals stored to avoid recalculation, all from T_low to T_high
        try:
            if self.phase_ref != 'l' and self.Tm and self.Tb:
                self.H_int_l_Tm_to_Tb = integrators['l'](self.Tm, self.Tb)
            if self.phase_ref == 's' and self.Tm:
                self.H_int_T_ref_s_to_Tm = integrators['s'](self.T_ref, self.Tm)
            if self.phase_ref == 'g' and self.Tb:
                self.H_int_Tb_to_T_ref_g = integrators['g'](self.Tb, self.T_ref)
            if self.phase_ref == 'l' and self.Tm and self.Tb:
                self.H_int_l_T_ref_l_to_Tb = integrators['l'](self.T_ref, self.Tb)
                self.H_int_l_Tm_to_T_ref_l = integrators['l'](self.Tm, self.T_ref)

            if self.phase_ref != 'l' and self.Tm and self.Tb:
                self.S_int_l_Tm_to_Tb = integrators_T['l'](self.Tm, self.Tb)
            if self.phase_ref == 's' and self.Tm:
                self.S_int_T_ref_s_to_Tm = integrators_T['s'](self.T_ref, self.Tm)
            if self.phase_ref == 'g' and self.Tb:
                self.S_int_Tb_to_T_ref_g = integrators_T['g'](self.Tb, self.T_ref)
            if self.phase_ref == 'l' and self.Tb and self.Tm:
                self.S_int_l_T_ref_l_to_Tb = integrators_T['l'](self.T_ref, self.Tb)
                self.S_int_l_Tm_to_T_ref_l = integrators_T['l'](self.Tm, self.T_ref)
        except:
            pass
CalebBell commented 7 years ago

I have closed this ticket after merging these changes. Please report any further bugs or feature requests you may have. I appreciate the report.