Search is not available for this dataset
text
stringlengths
75
104k
def load_all_methods(self): r'''Method which picks out coefficients for the specified chemical from the various dictionaries and DataFrames storing it. All data is stored as attributes. This method also sets :obj:`Tmin`, :obj:`Tmax`, :obj:`all_methods` and obj:`all_methods_P` as a set of methods for which the data exists for. Called on initialization only. See the source code for the variables at which the coefficients are stored. The coefficients can safely be altered once the class is initialized. This method can be called again to reset the parameters. ''' methods, methods_P = [], [] Tmins, Tmaxs = [], [] if self.CASRN in _VDISaturationDict: methods.append(VDI_TABULAR) Ts, props = VDI_tabular_data(self.CASRN, 'Mu (g)') self.VDI_Tmin = Ts[0] self.VDI_Tmax = Ts[-1] self.tabular_data[VDI_TABULAR] = (Ts, props) Tmins.append(self.VDI_Tmin); Tmaxs.append(self.VDI_Tmax) if has_CoolProp and self.CASRN in coolprop_dict: methods.append(COOLPROP); methods_P.append(COOLPROP) self.CP_f = coolprop_fluids[self.CASRN] Tmins.append(self.CP_f.Tmin); Tmaxs.append(self.CP_f.Tmax) if self.CASRN in Perrys2_312.index: methods.append(DIPPR_PERRY_8E) _, C1, C2, C3, C4, self.Perrys2_312_Tmin, self.Perrys2_312_Tmax = _Perrys2_312_values[Perrys2_312.index.get_loc(self.CASRN)].tolist() self.Perrys2_312_coeffs = [C1, C2, C3, C4] Tmins.append(self.Perrys2_312_Tmin); Tmaxs.append(self.Perrys2_312_Tmax) if self.CASRN in VDI_PPDS_8.index: methods.append(VDI_PPDS) self.VDI_PPDS_coeffs = _VDI_PPDS_8_values[VDI_PPDS_8.index.get_loc(self.CASRN)].tolist()[1:] self.VDI_PPDS_coeffs.reverse() # in format for horner's scheme if all([self.Tc, self.Pc, self.MW]): methods.append(GHARAGHEIZI) methods.append(YOON_THODOS) methods.append(STIEL_THODOS) Tmins.append(0); Tmaxs.append(5E3) # Intelligently set limit # GHARAGHEIZI turns nonsesical at ~15 K, YOON_THODOS fine to 0 K, # same as STIEL_THODOS if all([self.Tc, self.Pc, self.Zc, self.MW]): methods.append(LUCAS_GAS) Tmins.append(0); Tmaxs.append(1E3) self.all_methods = set(methods) self.all_methods_P = set(methods_P) if Tmins and Tmaxs: self.Tmin, self.Tmax = min(Tmins), max(Tmaxs)
def calculate(self, T, method): r'''Method to calculate low-pressure gas viscosity at tempearture `T` with a given method. This method has no exception handling; see `T_dependent_property` for that. Parameters ---------- T : float Temperature of the gas, [K] method : str Name of the method to use Returns ------- mu : float Viscosity of the gas at T and a low pressure, [Pa*S] ''' if method == GHARAGHEIZI: mu = Gharagheizi_gas_viscosity(T, self.Tc, self.Pc, self.MW) elif method == COOLPROP: mu = CoolProp_T_dependent_property(T, self.CASRN, 'V', 'g') elif method == DIPPR_PERRY_8E: mu = EQ102(T, *self.Perrys2_312_coeffs) elif method == VDI_PPDS: mu = horner(self.VDI_PPDS_coeffs, T) elif method == YOON_THODOS: mu = Yoon_Thodos(T, self.Tc, self.Pc, self.MW) elif method == STIEL_THODOS: mu = Stiel_Thodos(T, self.Tc, self.Pc, self.MW) elif method == LUCAS_GAS: mu = lucas_gas(T, self.Tc, self.Pc, self.Zc, self.MW, self.dipole, CASRN=self.CASRN) elif method in self.tabular_data: mu = self.interpolate(T, method) return mu
def calculate_P(self, T, P, method): r'''Method to calculate pressure-dependent gas viscosity at temperature `T` and pressure `P` with a given method. This method has no exception handling; see `TP_dependent_property` for that. Parameters ---------- T : float Temperature at which to calculate gas viscosity, [K] P : float Pressure at which to calculate gas viscosity, [K] method : str Name of the method to use Returns ------- mu : float Viscosity of the gas at T and P, [Pa*] ''' if method == COOLPROP: mu = PropsSI('V', 'T', T, 'P', P, self.CASRN) elif method in self.tabular_data: mu = self.interpolate_P(T, P, method) return mu
def load_all_methods(self): r'''Method to initialize the object by precomputing any values which may be used repeatedly and by retrieving mixture-specific variables. All data are stored as attributes. This method also sets :obj:`Tmin`, :obj:`Tmax`, and :obj:`all_methods` as a set of methods which should work to calculate the property. Called on initialization only. See the source code for the variables at which the coefficients are stored. The coefficients can safely be altered once the class is initialized. This method can be called again to reset the parameters. ''' methods = [SIMPLE] if none_and_length_check((self.MWs, self.molecular_diameters, self.Stockmayers)): methods.append(BROKAW) if none_and_length_check([self.MWs]): methods.extend([WILKE, HERNING_ZIPPERER]) self.all_methods = set(methods) Tmins = [i.Tmin for i in self.ViscosityGases if i.Tmin] Tmaxs = [i.Tmax for i in self.ViscosityGases if i.Tmax] if Tmins: self.Tmin = max(Tmins) if Tmaxs: self.Tmax = max(Tmaxs)
def calculate(self, T, P, zs, ws, method): r'''Method to calculate viscosity of a gas mixture at temperature `T`, pressure `P`, mole fractions `zs` and weight fractions `ws` with a given method. This method has no exception handling; see `mixture_property` for that. Parameters ---------- T : float Temperature at which to calculate the property, [K] P : float Pressure at which to calculate the property, [Pa] zs : list[float] Mole fractions of all species in the mixture, [-] ws : list[float] Weight fractions of all species in the mixture, [-] method : str Name of the method to use Returns ------- mu : float Viscosity of gas mixture, [Pa*s] ''' if method == SIMPLE: mus = [i(T, P) for i in self.ViscosityGases] return mixing_simple(zs, mus) elif method == HERNING_ZIPPERER: mus = [i(T, P) for i in self.ViscosityGases] return Herning_Zipperer(zs, mus, self.MWs) elif method == WILKE: mus = [i(T, P) for i in self.ViscosityGases] return Wilke(zs, mus, self.MWs) elif method == BROKAW: mus = [i(T, P) for i in self.ViscosityGases] return Brokaw(T, zs, mus, self.MWs, self.molecular_diameters, self.Stockmayers) else: raise Exception('Method not valid')
def ppmv_to_mgm3(ppmv, MW, T=298.15, P=101325.): r'''Converts a concentration in ppmv to units of mg/m^3. Used in industrial toxicology. .. math:: \frac{mg}{m^3} = \frac{ppmv\cdot P}{RT}\cdot \frac{MW}{1000} Parameters ---------- ppmv : float Concentratoin of a component in a gas mixure [parts per million, volumetric] MW : float Molecular weight of the trace gas [g/mol] T : float, optional Temperature of the gas at which the ppmv is reported P : float, optional Pressure of the gas at which the ppmv is reported Returns ------- mgm3 : float Concentration of a substance in an ideal gas mixture [mg/m^3] Notes ----- The term P/(RT)/1000 converts to 0.040874 at STP. Its inverse is reported as 24.45 in [1]_. Examples -------- >>> ppmv_to_mgm3(1, 40) 1.6349623351068687 References ---------- .. [1] ACGIH. Industrial Ventilation: A Manual of Recommended Practice, 23rd Edition. American Conference of Governmental and Industrial Hygenists, 2004. ''' parts = ppmv*1E-6 n = parts*P/(R*T) mgm3 = MW*n*1000 # mol toxin /m^3 * g/mol toxis * 1000 mg/g return mgm3
def mgm3_to_ppmv(mgm3, MW, T=298.15, P=101325.): r'''Converts a concentration in mg/m^3 to units of ppmv. Used in industrial toxicology. .. math:: ppmv = \frac{1000RT}{MW\cdot P} \cdot \frac{mg}{m^3} Parameters ---------- mgm3 : float Concentration of a substance in an ideal gas mixture [mg/m^3] MW : float Molecular weight of the trace gas [g/mol] T : float, optional Temperature of the gas at which the ppmv is reported P : float, optional Pressure of the gas at which the ppmv is reported Returns ------- ppmv : float Concentration of a component in a gas mixure [parts per million, volumetric] Notes ----- The term P/(RT)/1000 converts to 0.040874 at STP. Its inverse is reported as 24.45 in [1]_. Examples -------- >>> mgm3_to_ppmv(1.635, 40) 1.0000230371625833 References ---------- .. [1] ACGIH. Industrial Ventilation: A Manual of Recommended Practice, 23rd Edition. American Conference of Governmental and Industrial Hygenists, 2004. ''' n = mgm3/MW/1000. parts = n*R*T/P ppm = parts/1E-6 return ppm
def TWA(CASRN, AvailableMethods=False, Method=None): # pragma: no cover '''This function handles the retrieval of Time-Weighted Average limits on worker exposure to dangerous chemicals. This API is considered experimental, and is expected to be removed in a future release in favor of a more complete object-oriented interface. >>> TWA('98-00-0') (10.0, 'ppm') >>> TWA('1303-00-0') (5.0742430905659505e-05, 'ppm') >>> TWA('7782-42-5', AvailableMethods=True) ['Ontario Limits', 'None'] ''' def list_methods(): methods = [] if CASRN in _OntarioExposureLimits and (_OntarioExposureLimits[CASRN]["TWA (ppm)"] or _OntarioExposureLimits[CASRN]["TWA (mg/m^3)"]): methods.append(ONTARIO) methods.append(NONE) return methods if AvailableMethods: return list_methods() if not Method: Method = list_methods()[0] if Method == ONTARIO: if _OntarioExposureLimits[CASRN]["TWA (ppm)"]: _TWA = (_OntarioExposureLimits[CASRN]["TWA (ppm)"], 'ppm') elif _OntarioExposureLimits[CASRN]["TWA (mg/m^3)"]: _TWA = (_OntarioExposureLimits[CASRN]["TWA (mg/m^3)"], 'mg/m^3') elif Method == NONE: _TWA = None else: raise Exception('Failure in in function') return _TWA
def STEL(CASRN, AvailableMethods=False, Method=None): # pragma: no cover '''This function handles the retrieval of Short-term Exposure Limit on worker exposure to dangerous chemicals. This API is considered experimental, and is expected to be removed in a future release in favor of a more complete object-oriented interface. >>> STEL('67-64-1') (750.0, 'ppm') >>> STEL('7664-38-2') (0.7489774978301237, 'ppm') >>> STEL('55720-99-5') (2.0, 'mg/m^3') >>> STEL('86290-81-5', AvailableMethods=True) ['Ontario Limits', 'None'] ''' def list_methods(): methods = [] if CASRN in _OntarioExposureLimits and (_OntarioExposureLimits[CASRN]["STEL (ppm)"] or _OntarioExposureLimits[CASRN]["STEL (mg/m^3)"]): methods.append(ONTARIO) methods.append(NONE) return methods if AvailableMethods: return list_methods() if not Method: Method = list_methods()[0] if Method == ONTARIO: if _OntarioExposureLimits[CASRN]["STEL (ppm)"]: _STEL = (_OntarioExposureLimits[CASRN]["STEL (ppm)"], 'ppm') elif _OntarioExposureLimits[CASRN]["STEL (mg/m^3)"]: _STEL = (_OntarioExposureLimits[CASRN]["STEL (mg/m^3)"], 'mg/m^3') elif Method == NONE: _STEL = None else: raise Exception('Failure in in function') return _STEL
def Ceiling(CASRN, AvailableMethods=False, Method=None): # pragma: no cover '''This function handles the retrieval of Ceiling limits on worker exposure to dangerous chemicals. This API is considered experimental, and is expected to be removed in a future release in favor of a more complete object-oriented interface. >>> Ceiling('75-07-0') (25.0, 'ppm') >>> Ceiling('1395-21-7') (6e-05, 'mg/m^3') >>> Ceiling('7572-29-4', AvailableMethods=True) ['Ontario Limits', 'None'] ''' def list_methods(): methods = [] if CASRN in _OntarioExposureLimits and (_OntarioExposureLimits[CASRN]["Ceiling (ppm)"] or _OntarioExposureLimits[CASRN]["Ceiling (mg/m^3)"]): methods.append(ONTARIO) methods.append(NONE) return methods if AvailableMethods: return list_methods() if not Method: Method = list_methods()[0] if Method == ONTARIO: if _OntarioExposureLimits[CASRN]["Ceiling (ppm)"]: _Ceiling = (_OntarioExposureLimits[CASRN]["Ceiling (ppm)"], 'ppm') elif _OntarioExposureLimits[CASRN]["Ceiling (mg/m^3)"]: _Ceiling = (_OntarioExposureLimits[CASRN]["Ceiling (mg/m^3)"], 'mg/m^3') elif Method == NONE: _Ceiling = None else: raise Exception('Failure in in function') return _Ceiling
def Skin(CASRN, AvailableMethods=False, Method=None): # pragma: no cover '''This function handles the retrieval of whether or not a chemical can be absorbed through the skin, relevant to chemical safety calculations. This API is considered experimental, and is expected to be removed in a future release in favor of a more complete object-oriented interface. >>> Skin('108-94-1') True >>> Skin('1395-21-7') False >>> Skin('7572-29-4', AvailableMethods=True) ['Ontario Limits', 'None'] ''' def list_methods(): methods = [] if CASRN in _OntarioExposureLimits: methods.append(ONTARIO) methods.append(NONE) return methods if AvailableMethods: return list_methods() if not Method: Method = list_methods()[0] if Method == ONTARIO: _Skin = (_OntarioExposureLimits[CASRN]["Skin"]) elif Method == NONE: _Skin = None else: raise Exception('Failure in in function') return _Skin
def Carcinogen(CASRN, AvailableMethods=False, Method=None): r'''Looks up if a chemical is listed as a carcinogen or not according to either a specifc method or with all methods. Returns either the status as a string for a specified method, or the status of the chemical in all available data sources, in the format {source: status}. Parameters ---------- CASRN : string CASRN [-] Returns ------- status : str or dict Carcinogen status information [-] methods : list, only returned if AvailableMethods == True List of methods which can be used to obtain carcinogen status with the given inputs Other Parameters ---------------- Method : string, optional A string for the method name to use, as defined by constants in Carcinogen_methods AvailableMethods : bool, optional If True, function will determine which methods can be used to obtain if a chemical is listed as carcinogenic, and will return methods instead of the status Notes ----- Supported methods are: * **IARC**: International Agency for Research on Cancer, [1]_. As extracted with a last update of February 22, 2016. Has listing information of 843 chemicals with CAS numbers. Chemicals without CAS numbers not included here. If two listings for the same CAS were available, that closest to the CAS number was used. If two listings were available published at different times, the latest value was used. All else equal, the most pessimistic value was used. * **NTP**: National Toxicology Program, [2]_. Has data on 226 chemicals. Examples -------- >>> Carcinogen('61-82-5') {'National Toxicology Program 13th Report on Carcinogens': 'Reasonably Anticipated', 'International Agency for Research on Cancer': 'Not classifiable as to its carcinogenicity to humans (3)'} References ---------- .. [1] International Agency for Research on Cancer. Agents Classified by the IARC Monographs, Volumes 1-115. Lyon, France: IARC; 2016 Available from: http://monographs.iarc.fr/ENG/Classification/ .. [2] NTP (National Toxicology Program). 2014. Report on Carcinogens, Thirteenth Edition. Research Triangle Park, NC: U.S. Department of Health and Human Services, Public Health Service. http://ntp.niehs.nih.gov/pubhealth/roc/roc13/ ''' methods = [COMBINED, IARC, NTP] if AvailableMethods: return methods if not Method: Method = methods[0] if Method == IARC: if CASRN in IARC_data.index: status = IARC_codes[IARC_data.at[CASRN, 'group']] else: status = UNLISTED elif Method == NTP: if CASRN in NTP_data.index: status = NTP_codes[NTP_data.at[CASRN, 'Listing']] else: status = UNLISTED elif Method == COMBINED: status = {} for method in methods[1:]: status[method] = Carcinogen(CASRN, Method=method) else: raise Exception('Failure in in function') return status
def Tautoignition(CASRN, AvailableMethods=False, Method=None): r'''This function handles the retrieval or calculation of a chemical's autoifnition temperature. Lookup is based on CASRNs. No predictive methods are currently implemented. Will automatically select a data source to use if no Method is provided; returns None if the data is not available. Prefered source is 'IEC 60079-20-1 (2010)' [1]_, with the secondary source 'NFPA 497 (2008)' [2]_ having very similar data. Examples -------- >>> Tautoignition(CASRN='71-43-2') 771.15 Parameters ---------- CASRN : string CASRN [-] Returns ------- Tautoignition : float Autoignition point of the chemical, [K] methods : list, only returned if AvailableMethods == True List of methods which can be used to obtain Tautoignition with the given inputs Other Parameters ---------------- Method : string, optional A string for the method name to use, as defined by constants in Tautoignition_methods AvailableMethods : bool, optional If True, function will determine which methods can be used to obtain Tautoignition for the desired chemical, and will return methods instead of Tautoignition Notes ----- References ---------- .. [1] IEC. “IEC 60079-20-1:2010 Explosive atmospheres - Part 20-1: Material characteristics for gas and vapour classification - Test methods and data.” https://webstore.iec.ch/publication/635. See also https://law.resource.org/pub/in/bis/S05/is.iec.60079.20.1.2010.pdf .. [2] National Fire Protection Association. NFPA 497: Recommended Practice for the Classification of Flammable Liquids, Gases, or Vapors and of Hazardous. NFPA, 2008. ''' def list_methods(): methods = [] if CASRN in IEC_2010.index and not np.isnan(IEC_2010.at[CASRN, 'Tautoignition']): methods.append(IEC) if CASRN in NFPA_2008.index and not np.isnan(NFPA_2008.at[CASRN, 'Tautoignition']): methods.append(NFPA) methods.append(NONE) return methods if AvailableMethods: return list_methods() if not Method: Method = list_methods()[0] if Method == IEC: return float(IEC_2010.at[CASRN, 'Tautoignition']) elif Method == NFPA: return float(NFPA_2008.at[CASRN, 'Tautoignition']) elif Method == NONE: return None else: raise Exception('Failure in in function')
def LFL(Hc=None, atoms={}, CASRN='', AvailableMethods=False, Method=None): r'''This function handles the retrieval or calculation of a chemical's Lower Flammability Limit. Lookup is based on CASRNs. Two predictive methods are currently implemented. Will automatically select a data source to use if no Method is provided; returns None if the data is not available. Prefered source is 'IEC 60079-20-1 (2010)' [1]_, with the secondary source 'NFPA 497 (2008)' [2]_ having very similar data. If the heat of combustion is provided, the estimation method `Suzuki_LFL` can be used. If the atoms of the molecule are available, the method `Crowl_Louvar_LFL` can be used. Examples -------- >>> LFL(CASRN='71-43-2') 0.012 Parameters ---------- Hc : float, optional Heat of combustion of gas [J/mol] atoms : dict, optional Dictionary of atoms and atom counts CASRN : string, optional CASRN [-] Returns ------- LFL : float Lower flammability limit of the gas in an atmosphere at STP, [mole fraction] methods : list, only returned if AvailableMethods == True List of methods which can be used to obtain LFL with the given inputs Other Parameters ---------------- Method : string, optional A string for the method name to use, as defined by constants in LFL_methods AvailableMethods : bool, optional If True, function will determine which methods can be used to obtain the Lower Flammability Limit for the desired chemical, and will return methods instead of Lower Flammability Limit. Notes ----- References ---------- .. [1] IEC. “IEC 60079-20-1:2010 Explosive atmospheres - Part 20-1: Material characteristics for gas and vapour classification - Test methods and data.” https://webstore.iec.ch/publication/635. See also https://law.resource.org/pub/in/bis/S05/is.iec.60079.20.1.2010.pdf .. [2] National Fire Protection Association. NFPA 497: Recommended Practice for the Classification of Flammable Liquids, Gases, or Vapors and of Hazardous. NFPA, 2008. ''' def list_methods(): methods = [] if CASRN in IEC_2010.index and not np.isnan(IEC_2010.at[CASRN, 'LFL']): methods.append(IEC) if CASRN in NFPA_2008.index and not np.isnan(NFPA_2008.at[CASRN, 'LFL']): methods.append(NFPA) if Hc: methods.append(SUZUKI) if atoms: methods.append(CROWLLOUVAR) methods.append(NONE) return methods if AvailableMethods: return list_methods() if not Method: Method = list_methods()[0] if Method == IEC: return float(IEC_2010.at[CASRN, 'LFL']) elif Method == NFPA: return float(NFPA_2008.at[CASRN, 'LFL']) elif Method == SUZUKI: return Suzuki_LFL(Hc=Hc) elif Method == CROWLLOUVAR: return Crowl_Louvar_LFL(atoms=atoms) elif Method == NONE: return None else: raise Exception('Failure in in function')
def UFL(Hc=None, atoms={}, CASRN='', AvailableMethods=False, Method=None): r'''This function handles the retrieval or calculation of a chemical's Upper Flammability Limit. Lookup is based on CASRNs. Two predictive methods are currently implemented. Will automatically select a data source to use if no Method is provided; returns None if the data is not available. Prefered source is 'IEC 60079-20-1 (2010)' [1]_, with the secondary source 'NFPA 497 (2008)' [2]_ having very similar data. If the heat of combustion is provided, the estimation method `Suzuki_UFL` can be used. If the atoms of the molecule are available, the method `Crowl_Louvar_UFL` can be used. Examples -------- >>> UFL(CASRN='71-43-2') 0.086 Parameters ---------- Hc : float, optional Heat of combustion of gas [J/mol] atoms : dict, optional Dictionary of atoms and atom counts CASRN : string, optional CASRN [-] Returns ------- UFL : float Upper flammability limit of the gas in an atmosphere at STP, [mole fraction] methods : list, only returned if AvailableMethods == True List of methods which can be used to obtain UFL with the given inputs Other Parameters ---------------- Method : string, optional A string for the method name to use, as defined by constants in UFL_methods AvailableMethods : bool, optional If True, function will determine which methods can be used to obtain the Upper Flammability Limit for the desired chemical, and will return methods instead of Upper Flammability Limit. Notes ----- References ---------- .. [1] IEC. “IEC 60079-20-1:2010 Explosive atmospheres - Part 20-1: Material characteristics for gas and vapour classification - Test methods and data.” https://webstore.iec.ch/publication/635. See also https://law.resource.org/pub/in/bis/S05/is.iec.60079.20.1.2010.pdf .. [2] National Fire Protection Association. NFPA 497: Recommended Practice for the Classification of Flammable Liquids, Gases, or Vapors and of Hazardous. NFPA, 2008. ''' def list_methods(): methods = [] if CASRN in IEC_2010.index and not np.isnan(IEC_2010.at[CASRN, 'UFL']): methods.append(IEC) if CASRN in NFPA_2008.index and not np.isnan(NFPA_2008.at[CASRN, 'UFL']): methods.append(NFPA) if Hc: methods.append(SUZUKI) if atoms: methods.append(CROWLLOUVAR) methods.append(NONE) return methods if AvailableMethods: return list_methods() if not Method: Method = list_methods()[0] if Method == IEC: return float(IEC_2010.at[CASRN, 'UFL']) elif Method == NFPA: return float(NFPA_2008.at[CASRN, 'UFL']) elif Method == SUZUKI: return Suzuki_UFL(Hc=Hc) elif Method == CROWLLOUVAR: return Crowl_Louvar_UFL(atoms=atoms) elif Method == NONE: return None else: raise Exception('Failure in in function')
def fire_mixing(ys=None, FLs=None): # pragma: no cover ''' Crowl, Daniel A., and Joseph F. Louvar. Chemical Process Safety: Fundamentals with Applications. 2E. Upper Saddle River, N.J: Prentice Hall, 2001. >>> fire_mixing(ys=normalize([0.0024, 0.0061, 0.0015]), FLs=[.012, .053, .031]) 0.02751172136637643 >>> fire_mixing(ys=normalize([0.0024, 0.0061, 0.0015]), FLs=[.075, .15, .32]) 0.12927551844869378 ''' return 1./sum([yi/FLi for yi, FLi in zip(ys, FLs)])
def LFL_mixture(ys=None, LFLs=None, CASRNs=None, AvailableMethods=False, Method=None): # pragma: no cover '''Inert gases are ignored. This API is considered experimental, and is expected to be removed in a future release in favor of a more complete object-oriented interface. >>> LFL_mixture(ys=normalize([0.0024, 0.0061, 0.0015]), LFLs=[.012, .053, .031]) 0.02751172136637643 >>> LFL_mixture(LFLs=[None, None, None, None, None, None, None, None, None, None, None, None, None, None, 0.025, 0.06, 0.073, 0.020039, 0.011316], ys=[0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.10, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05], CASRNs=['7440-37-1', '124-38-9', '7440-59-7', '7440-01-9', '7727-37-9', '7440-63-3', '10102-43-9', '7782-44-7', '132259-10-0', '7439-90-9', '10043-92-2', '7732-18-5', '7782-50-5', '7782-41-4', '67-64-1', '67-56-1', '75-52-5', '590-19-2', '277-10-1']) 0.023964903630937385 ''' def list_methods(): methods = [] if CASRNs: CASRNs2 = list(CASRNs) LFLs2 = list(LFLs) for i in inerts: if i in CASRNs2: ind = CASRNs.index(i) CASRNs2.remove(i) LFLs2.remove(LFLs[ind]) if none_and_length_check([LFLs2]): methods.append('Summed Inverse, inerts removed') else: if none_and_length_check([LFLs]): methods.append('Summed Inverse') methods.append('None') return methods if AvailableMethods: return list_methods() if not Method: Method = list_methods()[0] # This is the calculate, given the method section # if not none_and_length_check([LFLs, ys]): # raise Exception('Function inputs are incorrect format') if Method == 'Summed Inverse': return fire_mixing(ys, LFLs) elif Method == 'Summed Inverse, inerts removed': CASRNs2 = list(CASRNs) LFLs2 = list(LFLs) ys2 = list(ys) for i in inerts: if i in CASRNs2: ind = CASRNs2.index(i) CASRNs2.remove(i) LFLs2.pop(ind) ys2.pop(ind) return fire_mixing(normalize(ys2), LFLs2) elif Method == 'None': return None else: raise Exception('Failure in in function')
def UFL_mixture(ys=None, UFLs=None, CASRNs=None, AvailableMethods=False, Method=None): # pragma: no cover '''Inert gases are ignored. This API is considered experimental, and is expected to be removed in a future release in favor of a more complete object-oriented interface. >>> UFL_mixture(ys=normalize([0.0024, 0.0061, 0.0015]), UFLs=[.075, .15, .32]) 0.12927551844869378 >>> LFL_mixture(LFLs=[None, None, None, None, None, None, None, None, None, None, None, None, None, None, 0.143, 0.36, 0.63, 0.1097, 0.072], ys=[0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.10, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05], CASRNs=['7440-37-1', '124-38-9', '7440-59-7', '7440-01-9', '7727-37-9', '7440-63-3', '10102-43-9', '7782-44-7', '132259-10-0', '7439-90-9', '10043-92-2', '7732-18-5', '7782-50-5', '7782-41-4', '67-64-1', '67-56-1', '75-52-5', '590-19-2', '277-10-1']) 0.14550641757359664 ''' def list_methods(): methods = [] if CASRNs: CASRNs2 = list(CASRNs) UFLs2 = list(UFLs) for i in inerts: if i in CASRNs2: ind = CASRNs.index(i) CASRNs2.remove(i) UFLs2.remove(UFLs[ind]) if none_and_length_check([UFLs2]): methods.append('Summed Inverse, inerts removed') if none_and_length_check([UFLs, ys]): methods.append('Summed Inverse') methods.append('None') return methods if AvailableMethods: return list_methods() if not Method: Method = list_methods()[0] # This is the calculate, given the method section # if not none_and_length_check([UFLs, ys]): # check same-length inputs # raise Exception('Function inputs are incorrect format') if Method == 'Summed Inverse': return fire_mixing(ys, UFLs) elif Method == 'Summed Inverse, inerts removed': CASRNs2 = list(CASRNs) UFLs2 = list(UFLs) ys2 = list(ys) for i in inerts: if i in CASRNs2: ind = CASRNs2.index(i) CASRNs2.remove(i) UFLs2.pop(ind) ys2.pop(ind) return fire_mixing(normalize(ys2), UFLs2) elif Method == 'None': return None else: raise Exception('Failure in in function')
def Suzuki_LFL(Hc=None): r'''Calculates lower flammability limit, using the Suzuki [1]_ correlation. Uses heat of combustion only. The lower flammability limit of a gas is air is: .. math:: \text{LFL} = \frac{-3.42}{\Delta H_c^{\circ}} + 0.569 \Delta H_c^{\circ} + 0.0538\Delta H_c^{\circ 2} + 1.80 Parameters ---------- Hc : float Heat of combustion of gas [J/mol] Returns ------- LFL : float Lower flammability limit, mole fraction [-] Notes ----- Fit performed with 112 compounds, r^2 was 0.977. LFL in percent volume in air. Hc is at standard conditions, in MJ/mol. 11 compounds left out as they were outliers. Equation does not apply for molecules with halogen atoms, only hydrocarbons with oxygen or nitrogen or sulfur. No sample calculation provided with the article. However, the equation is straightforward. Limits of equations's validity are -6135596 J where it predicts a LFL of 0, and -48322129 J where it predicts a LFL of 1. Examples -------- Pentane, 1.5 % LFL in literature >>> Suzuki_LFL(-3536600) 0.014276107095811815 References ---------- .. [1] Suzuki, Takahiro. "Note: Empirical Relationship between Lower Flammability Limits and Standard Enthalpies of Combustion of Organic Compounds." Fire and Materials 18, no. 5 (September 1, 1994): 333-36. doi:10.1002/fam.810180509. ''' Hc = Hc/1E6 LFL = -3.42/Hc + 0.569*Hc + 0.0538*Hc*Hc + 1.80 return LFL/100.
def Crowl_Louvar_LFL(atoms): r'''Calculates lower flammability limit, using the Crowl-Louvar [1]_ correlation. Uses molecular formula only. The lower flammability limit of a gas is air is: .. math:: C_mH_xO_y + zO_2 \to mCO_2 + \frac{x}{2}H_2O \text{LFL} = \frac{0.55}{4.76m + 1.19x - 2.38y + 1} Parameters ---------- atoms : dict Dictionary of atoms and atom counts Returns ------- LFL : float Lower flammability limit, mole fraction Notes ----- Coefficient of 0.55 taken from [2]_ Examples -------- Hexane, example from [1]_, lit. 1.2 % >>> Crowl_Louvar_LFL({'H': 14, 'C': 6}) 0.011899610558199915 References ---------- .. [1] Crowl, Daniel A., and Joseph F. Louvar. Chemical Process Safety: Fundamentals with Applications. 2E. Upper Saddle River, N.J: Prentice Hall, 2001. .. [2] Jones, G. W. "Inflammation Limits and Their Practical Application in Hazardous Industrial Operations." Chemical Reviews 22, no. 1 (February 1, 1938): 1-26. doi:10.1021/cr60071a001 ''' nC, nH, nO = 0, 0, 0 if 'C' in atoms and atoms['C']: nC = atoms['C'] else: return None if 'H' in atoms: nH = atoms['H'] if 'O' in atoms: nO = atoms['O'] return 0.55/(4.76*nC + 1.19*nH - 2.38*nO + 1.)
def Crowl_Louvar_UFL(atoms): r'''Calculates upper flammability limit, using the Crowl-Louvar [1]_ correlation. Uses molecular formula only. The upper flammability limit of a gas is air is: .. math:: C_mH_xO_y + zO_2 \to mCO_2 + \frac{x}{2}H_2O \text{UFL} = \frac{3.5}{4.76m + 1.19x - 2.38y + 1} Parameters ---------- atoms : dict Dictionary of atoms and atom counts Returns ------- UFL : float Upper flammability limit, mole fraction Notes ----- Coefficient of 3.5 taken from [2]_ Examples -------- Hexane, example from [1]_, lit. 7.5 % >>> Crowl_Louvar_UFL({'H': 14, 'C': 6}) 0.07572479446127219 References ---------- .. [1] Crowl, Daniel A., and Joseph F. Louvar. Chemical Process Safety: Fundamentals with Applications. 2E. Upper Saddle River, N.J: Prentice Hall, 2001. .. [2] Jones, G. W. "Inflammation Limits and Their Practical Application in Hazardous Industrial Operations." Chemical Reviews 22, no. 1 (February 1, 1938): 1-26. doi:10.1021/cr60071a001 ''' nC, nH, nO = 0, 0, 0 if 'C' in atoms and atoms['C']: nC = atoms['C'] else: return None if 'H' in atoms: nH = atoms['H'] if 'O' in atoms: nO = atoms['O'] return 3.5/(4.76*nC + 1.19*nH - 2.38*nO + 1.)
def Vfls(self, T=None, P=None): r'''Volume fractions of all species in a hypothetical pure-liquid phase at the current or specified temperature and pressure. If temperature or pressure are specified, the non-specified property is assumed to be that of the mixture. Note this is a method, not a property. Volume fractions are calculated based on **pure species volumes only**. Examples -------- >>> Mixture(['hexane', 'pentane'], zs=[.5, .5], T=315).Vfls() [0.5299671144566751, 0.47003288554332484] >>> S = Mixture(['hexane', 'decane'], zs=[0.25, 0.75]) >>> S.Vfls(298.16, 101326) [0.18301434895886864, 0.8169856510411313] ''' if (T is None or T == self.T) and (P is None or P == self.P): Vmls = self.Vmls else: if T is None: T = self.T if P is None: P = self.P Vmls = [i(T, P) for i in self.VolumeLiquids] if none_and_length_check([Vmls]): return zs_to_Vfs(self.zs, Vmls) return None
def Vfgs(self, T=None, P=None): r'''Volume fractions of all species in a hypothetical pure-gas phase at the current or specified temperature and pressure. If temperature or pressure are specified, the non-specified property is assumed to be that of the mixture. Note this is a method, not a property. Volume fractions are calculated based on **pure species volumes only**. Examples -------- >>> Mixture(['sulfur hexafluoride', 'methane'], zs=[.2, .9], T=315).Vfgs() [0.18062059238682632, 0.8193794076131737] >>> S = Mixture(['sulfur hexafluoride', 'methane'], zs=[.1, .9]) >>> S.Vfgs(P=1E2) [0.0999987466608421, 0.9000012533391578] ''' if (T is None or T == self.T) and (P is None or P == self.P): Vmgs = self.Vmgs else: if T is None: T = self.T if P is None: P = self.P Vmgs = [i(T, P) for i in self.VolumeGases] if none_and_length_check([Vmgs]): return zs_to_Vfs(self.zs, Vmgs) return None
def atom_fractions(self): r'''Dictionary of atomic fractions for each atom in the mixture. Examples -------- >>> Mixture(['CO2', 'O2'], zs=[0.5, 0.5]).atom_fractions {'C': 0.2, 'O': 0.8} ''' things = dict() for zi, atoms in zip(self.zs, self.atomss): for atom, count in atoms.iteritems(): if atom in things: things[atom] += zi*count else: things[atom] = zi*count tot = sum(things.values()) return {atom : value/tot for atom, value in things.iteritems()}
def mass_fractions(self): r'''Dictionary of mass fractions for each atom in the mixture. Examples -------- >>> Mixture(['CO2', 'O2'], zs=[0.5, 0.5]).mass_fractions {'C': 0.15801826905745822, 'O': 0.8419817309425419} ''' things = dict() for zi, atoms in zip(self.zs, self.atomss): for atom, count in atoms.iteritems(): if atom in things: things[atom] += zi*count else: things[atom] = zi*count return mass_fractions(things)
def charge_balance(self): r'''Charge imbalance of the mixture, in units of [faraday]. Mixtures meeting the electroneutrality condition will have an imbalance of 0. Examples -------- >>> Mixture(['Na+', 'Cl-', 'water'], zs=[.01, .01, .98]).charge_balance 0.0 ''' return sum([zi*ci for zi, ci in zip(self.zs, self.charges)])
def Cpsm(self): r'''Solid-phase heat capacity of the mixture at its current temperature and composition, in units of [J/mol/K]. For calculation of this property at other temperatures or compositions, or specifying manually the method used to calculate it, and more - see the object oriented interface :obj:`thermo.heat_capacity.HeatCapacitySolidMixture`; each Mixture instance creates one to actually perform the calculations. Examples -------- >>> Mixture(['silver', 'platinum'], ws=[0.95, 0.05]).Cpsm 25.32745796347474 ''' return self.HeatCapacitySolidMixture(self.T, self.P, self.zs, self.ws)
def Cplm(self): r'''Liquid-phase heat capacity of the mixture at its current temperature and composition, in units of [J/mol/K]. For calculation of this property at other temperatures or compositions, or specifying manually the method used to calculate it, and more - see the object oriented interface :obj:`thermo.heat_capacity.HeatCapacityLiquidMixture`; each Mixture instance creates one to actually perform the calculations. Examples -------- >>> Mixture(['toluene', 'decane'], ws=[.9, .1], T=300).Cplm 168.29127923518843 ''' return self.HeatCapacityLiquidMixture(self.T, self.P, self.zs, self.ws)
def Cpgm(self): r'''Gas-phase heat capacity of the mixture at its current temperature and composition, in units of [J/mol/K]. For calculation of this property at other temperatures or compositions, or specifying manually the method used to calculate it, and more - see the object oriented interface :obj:`thermo.heat_capacity.HeatCapacityGasMixture`; each Mixture instance creates one to actually perform the calculations. Examples -------- >>> Mixture(['oxygen', 'nitrogen'], ws=[.4, .6], T=350, P=1E6).Cpgm 29.361044582498046 ''' return self.HeatCapacityGasMixture(self.T, self.P, self.zs, self.ws)
def Cps(self): r'''Solid-phase heat capacity of the mixture at its current temperature and composition, in units of [J/kg/K]. For calculation of this property at other temperatures or compositions, or specifying manually the method used to calculate it, and more - see the object oriented interface :obj:`thermo.heat_capacity.HeatCapacitySolidMixture`; each Mixture instance creates one to actually perform the calculations. Note that that interface provides output in molar units. Examples -------- >>> Mixture(['silver', 'platinum'], ws=[0.95, 0.05]).Cps 229.55166388430328 ''' Cpsm = self.HeatCapacitySolidMixture(self.T, self.P, self.zs, self.ws) if Cpsm: return property_molar_to_mass(Cpsm, self.MW) return None
def Cpl(self): r'''Liquid-phase heat capacity of the mixture at its current temperature and composition, in units of [J/kg/K]. For calculation of this property at other temperatures or compositions, or specifying manually the method used to calculate it, and more - see the object oriented interface :obj:`thermo.heat_capacity.HeatCapacityLiquidMixture`; each Mixture instance creates one to actually perform the calculations. Note that that interface provides output in molar units. Examples -------- >>> Mixture(['water', 'sodium chloride'], ws=[.9, .1], T=301.5).Cpl 3735.4604049449786 ''' Cplm = self.HeatCapacityLiquidMixture(self.T, self.P, self.zs, self.ws) if Cplm: return property_molar_to_mass(Cplm, self.MW) return None
def Cpg(self): r'''Gas-phase heat capacity of the mixture at its current temperature , and composition in units of [J/kg/K]. For calculation of this property at other temperatures or compositions, or specifying manually the method used to calculate it, and more - see the object oriented interface :obj:`thermo.heat_capacity.HeatCapacityGasMixture`; each Mixture instance creates one to actually perform the calculations. Note that that interface provides output in molar units. Examples -------- >>> Mixture(['oxygen', 'nitrogen'], ws=[.4, .6], T=350, P=1E6).Cpg 995.8911053614883 ''' Cpgm = self.HeatCapacityGasMixture(self.T, self.P, self.zs, self.ws) if Cpgm: return property_molar_to_mass(Cpgm, self.MW) return None
def Cvgm(self): r'''Gas-phase ideal-gas contant-volume heat capacity of the mixture at its current temperature and composition, in units of [J/mol/K]. Subtracts R from the ideal-gas heat capacity; does not include pressure-compensation from an equation of state. Examples -------- >>> Mixture(['water'], ws=[1], T=520).Cvgm 27.13366316134193 ''' Cpgm = self.HeatCapacityGasMixture(self.T, self.P, self.zs, self.ws) if Cpgm: return Cpgm - R return None
def Vml(self): r'''Liquid-phase molar volume of the mixture at its current temperature, pressure, and composition in units of [m^3/mol]. For calculation of this property at other temperatures or pressures or compositions, or specifying manually the method used to calculate it, and more - see the object oriented interface :obj:`thermo.volume.VolumeLiquidMixture`; each Mixture instance creates one to actually perform the calculations. Examples -------- >>> Mixture(['cyclobutane'], ws=[1], T=225).Vml 7.42395423425395e-05 ''' return self.VolumeLiquidMixture(T=self.T, P=self.P, zs=self.zs, ws=self.ws)
def Vmg(self): r'''Gas-phase molar volume of the mixture at its current temperature, pressure, and composition in units of [m^3/mol]. For calculation of this property at other temperatures or pressures or compositions, or specifying manually the method used to calculate it, and more - see the object oriented interface :obj:`thermo.volume.VolumeGasMixture`; each Mixture instance creates one to actually perform the calculations. Examples -------- >>> Mixture(['hexane'], ws=[1], T=300, P=2E5).Vmg 0.010888694235142216 ''' return self.VolumeGasMixture(T=self.T, P=self.P, zs=self.zs, ws=self.ws)
def SGg(self): r'''Specific gravity of a hypothetical gas phase of the mixture, . [dimensionless]. The reference condition is air at 15.6 °C (60 °F) and 1 atm (rho=1.223 kg/m^3). The definition for gases uses the compressibility factor of the reference gas and the mixture both at the reference conditions, not the conditions of the mixture. Examples -------- >>> Mixture('argon').SGg 1.3800407778218216 ''' Vmg = self.VolumeGasMixture(T=288.70555555555552, P=101325, zs=self.zs, ws=self.ws) if Vmg: rho = Vm_to_rho(Vmg, self.MW) return SG(rho, rho_ref=1.2231876628642968) # calculated with Mixture return None
def mul(self): r'''Viscosity of the mixture in the liquid phase at its current temperature, pressure, and composition in units of [Pa*s]. For calculation of this property at other temperatures and pressures, or specifying manually the method used to calculate it, and more - see the object oriented interface :obj:`thermo.viscosity.ViscosityLiquidMixture`; each Mixture instance creates one to actually perform the calculations. Examples -------- >>> Mixture(['water'], ws=[1], T=320).mul 0.0005767262693751547 ''' return self.ViscosityLiquidMixture(self.T, self.P, self.zs, self.ws)
def mug(self): r'''Viscosity of the mixture in the gas phase at its current temperature, pressure, and composition in units of [Pa*s]. For calculation of this property at other temperatures and pressures, or specifying manually the method used to calculate it, and more - see the object oriented interface :obj:`thermo.viscosity.ViscosityGasMixture`; each Mixture instance creates one to actually perform the calculations. Examples -------- >>> Mixture(['water'], ws=[1], T=500).mug 1.7298722343367148e-05 ''' return self.ViscosityGasMixture(self.T, self.P, self.zs, self.ws)
def sigma(self): r'''Surface tension of the mixture at its current temperature and composition, in units of [N/m]. For calculation of this property at other temperatures, or specifying manually the method used to calculate it, and more - see the object oriented interface :obj:`thermo.interface.SurfaceTensionMixture`; each Mixture instance creates one to actually perform the calculations. Examples -------- >>> Mixture(['water'], ws=[1], T=300, P=1E5).sigma 0.07176932405246211 ''' return self.SurfaceTensionMixture(self.T, self.P, self.zs, self.ws)
def kl(self): r'''Thermal conductivity of the mixture in the liquid phase at its current temperature, pressure, and composition in units of [Pa*s]. For calculation of this property at other temperatures and pressures, or specifying manually the method used to calculate it, and more - see the object oriented interface :obj:`thermo.thermal_conductivity.ThermalConductivityLiquidMixture`; each Mixture instance creates one to actually perform the calculations. Examples -------- >>> Mixture(['water'], ws=[1], T=320).kl 0.6369957248212118 ''' return self.ThermalConductivityLiquidMixture(self.T, self.P, self.zs, self.ws)
def kg(self): r'''Thermal conductivity of the mixture in the gas phase at its current temperature, pressure, and composition in units of [Pa*s]. For calculation of this property at other temperatures and pressures, or specifying manually the method used to calculate it, and more - see the object oriented interface :obj:`thermo.thermal_conductivity.ThermalConductivityGasMixture`; each Mixture instance creates one to actually perform the calculations. Examples -------- >>> Mixture(['water'], ws=[1], T=500).kg 0.036035173297862676 ''' return self.ThermalConductivityGasMixture(self.T, self.P, self.zs, self.ws)
def rhom(self): r'''Molar density of the mixture at its current phase and temperature and pressure, in units of [mol/m^3]. Available only if single phase. Examples -------- >>> Mixture(['1-hexanol'], ws=[1]).rhom 7983.414573003429 ''' return phase_select_property(phase=self.phase, s=None, l=self.rholm, g=self.rhogm)
def SG(self): r'''Specific gravity of the mixture, [dimensionless]. For gas-phase conditions, this is calculated at 15.6 °C (60 °F) and 1 atm for the mixture and the reference fluid, air. For liquid and solid phase conditions, this is calculated based on a reference fluid of water at 4°C at 1 atm, but the with the liquid or solid mixture's density at the currently specified conditions. Examples -------- >>> Mixture('MTBE').SG 0.7428160596603596 ''' return phase_select_property(phase=self.phase, s=self.SGs, l=self.SGl, g=self.SGg)
def Vml_STP(self): r'''Liquid-phase molar volume of the mixture at 298.15 K and 101.325 kPa, and the current composition in units of [m^3/mol]. Examples -------- >>> Mixture(['cyclobutane'], ws=[1]).Vml_STP 8.143327329133706e-05 ''' return self.VolumeLiquidMixture(T=298.15, P=101325, zs=self.zs, ws=self.ws)
def Vmg_STP(self): r'''Gas-phase molar volume of the mixture at 298.15 K and 101.325 kPa, and the current composition in units of [m^3/mol]. Examples -------- >>> Mixture(['nitrogen'], ws=[1]).Vmg_STP 0.02445443688838904 ''' return self.VolumeGasMixture(T=298.15, P=101325, zs=self.zs, ws=self.ws)
def rhol_STP(self): r'''Liquid-phase mass density of the mixture at 298.15 K and 101.325 kPa, and the current composition in units of [kg/m^3]. Examples -------- >>> Mixture(['cyclobutane'], ws=[1]).rhol_STP 688.9851989526821 ''' Vml = self.Vml_STP if Vml: return Vm_to_rho(Vml, self.MW) return None
def rhog_STP(self): r'''Gas-phase mass density of the mixture at 298.15 K and 101.325 kPa, and the current composition in units of [kg/m^3]. Examples -------- >>> Mixture(['nitrogen'], ws=[1]).rhog_STP 1.145534453639403 ''' Vmg = self.Vmg_STP if Vmg: return Vm_to_rho(Vmg, self.MW) return None
def Zl_STP(self): r'''Liquid-phase compressibility factor of the mixture at 298.15 K and 101.325 kPa, and the current composition, [dimensionless]. Examples -------- >>> Mixture(['cyclobutane'], ws=[1]).Zl_STP 0.0033285083663950068 ''' Vml = self.Vml if Vml: return Z(self.T, self.P, Vml) return None
def Zg_STP(self): r'''Gas-phase compressibility factor of the mixture at 298.15 K and 101.325 kPa, and the current composition, [dimensionless]. Examples -------- >>> Mixture(['nitrogen'], ws=[1]).Zg_STP 0.9995520809691023 ''' Vmg = self.Vmg if Vmg: return Z(self.T, self.P, Vmg) return None
def API(self): r'''API gravity of the hypothetical liquid phase of the mixture, [degrees]. The reference condition is water at 15.6 °C (60 °F) and 1 atm (rho=999.016 kg/m^3, standardized). Examples -------- >>> Mixture(['hexane', 'decane'], ws=[0.5, 0.5]).API 71.34707841728181 ''' Vml = self.VolumeLiquidMixture(T=288.70555555555552, P=101325, zs=self.zs, ws=self.ws) if Vml: rho = Vm_to_rho(Vml, self.MW) sg = SG(rho, rho_ref=999.016) return SG_to_API(sg)
def draw_2d(self, Hs=False): # pragma: no cover r'''Interface for drawing a 2D image of all the molecules in the mixture. Requires an HTML5 browser, and the libraries RDKit and IPython. An exception is raised if either of these libraries is absent. Parameters ---------- Hs : bool Whether or not to show hydrogen Examples -------- Mixture(['natural gas']).draw_2d() ''' try: from rdkit.Chem import Draw from rdkit.Chem.Draw import IPythonConsole if Hs: mols = [i.rdkitmol_Hs for i in self.Chemicals] else: mols = [i.rdkitmol for i in self.Chemicals] return Draw.MolsToImage(mols) except: return 'Rdkit is required for this feature.'
def Tt(CASRN, AvailableMethods=False, Method=None): r'''This function handles the retrieval of a chemical's triple temperature. Lookup is based on CASRNs. Will automatically select a data source to use if no Method is provided; returns None if the data is not available. Returns data from [1]_, or a chemical's melting point if available. Parameters ---------- CASRN : string CASRN [-] Returns ------- Tt : float Triple point temperature, [K] methods : list, only returned if AvailableMethods == True List of methods which can be used to obtain Tt with the given inputs Other Parameters ---------------- Method : string, optional A string for the method name to use, as defined by constants in Tt_methods AvailableMethods : bool, optional If True, function will determine which methods can be used to obtain the Tt for the desired chemical, and will return methods instead of the Tt Notes ----- Median difference between melting points and triple points is 0.02 K. Accordingly, this should be more than good enough for engineering applications. Temperatures are on the ITS-68 scale. Examples -------- Ammonia >>> Tt('7664-41-7') 195.47999999999999 References ---------- .. [1] Staveley, L. A. K., L. Q. Lobo, and J. C. G. Calado. "Triple-Points of Low Melting Substances and Their Use in Cryogenic Work." Cryogenics 21, no. 3 (March 1981): 131-144. doi:10.1016/0011-2275(81)90264-2. ''' def list_methods(): methods = [] if CASRN in Staveley_data.index: methods.append(STAVELEY) if Tm(CASRN): methods.append(MELTING) methods.append(NONE) return methods if AvailableMethods: return list_methods() if not Method: Method = list_methods()[0] if Method == STAVELEY: Tt = Staveley_data.at[CASRN, "Tt68"] elif Method == MELTING: Tt = Tm(CASRN) elif Method == NONE: Tt = None else: raise Exception('Failure in in function') return Tt
def Pt(CASRN, AvailableMethods=False, Method=None): r'''This function handles the retrieval of a chemical's triple pressure. Lookup is based on CASRNs. Will automatically select a data source to use if no Method is provided; returns None if the data is not available. Returns data from [1]_, or attempts to calculate the vapor pressure at the triple temperature, if data is available. Parameters ---------- CASRN : string CASRN [-] Returns ------- Pt : float Triple point pressure, [Pa] methods : list, only returned if AvailableMethods == True List of methods which can be used to obtain Pt with the given inputs Other Parameters ---------------- Method : string, optional A string for the method name to use, as defined by constants in Pt_methods AvailableMethods : bool, optional If True, function will determine which methods can be used to obtain the Pt for the desired chemical, and will return methods instead of the Pt Notes ----- Examples -------- Ammonia >>> Pt('7664-41-7') 6079.5 References ---------- .. [1] Staveley, L. A. K., L. Q. Lobo, and J. C. G. Calado. "Triple-Points of Low Melting Substances and Their Use in Cryogenic Work." Cryogenics 21, no. 3 (March 1981): 131-144. doi:10.1016/0011-2275(81)90264-2. ''' def list_methods(): methods = [] if CASRN in Staveley_data.index and not np.isnan(Staveley_data.at[CASRN, 'Pt']): methods.append(STAVELEY) if Tt(CASRN) and VaporPressure(CASRN=CASRN).T_dependent_property(T=Tt(CASRN)): methods.append(DEFINITION) methods.append(NONE) return methods if AvailableMethods: return list_methods() if not Method: Method = list_methods()[0] if Method == STAVELEY: Pt = Staveley_data.at[CASRN, 'Pt'] elif Method == DEFINITION: Pt = VaporPressure(CASRN=CASRN).T_dependent_property(T=Tt(CASRN)) elif Method == NONE: Pt = None else: raise Exception('Failure in in function') return Pt
def to_num(values): r'''Legacy function to turn a list of strings into either floats (if numeric), stripped strings (if not) or None if the string is empty. Accepts any numeric formatting the float function does. Parameters ---------- values : list list of strings Returns ------- values : list list of floats, strings, and None values [-] Examples -------- >>> to_num(['1', '1.1', '1E5', '0xB4', '']) [1.0, 1.1, 100000.0, '0xB4', None] ''' for i in range(len(values)): try: values[i] = float(values[i]) except: if values[i] == '': values[i] = None else: values[i] = values[i].strip() pass return values
def Parachor(MW, rhol, rhog, sigma): r'''Calculate Parachor for a pure species, using its density in the liquid and gas phases, surface tension, and molecular weight. .. math:: P = \frac{\sigma^{0.25} MW}{\rho_L - \rho_V} Parameters ---------- MW : float Molecular weight, [g/mol] rhol : float Liquid density [kg/m^3] rhog : float Gas density [kg/m^3] sigma : float Surface tension, [N/m] Returns ------- P : float Parachor, [N^0.25*m^2.75/mol] Notes ----- To convert the output of this function to units of [mN^0.25*m^2.75/kmol], multiply by 5623.4132519. Values in group contribution tables for Parachor are often listed as dimensionless, in which they are multiplied by 5623413 and the appropriate units to make them dimensionless. Examples -------- Calculating Parachor from a known surface tension for methyl isobutyl ketone at 293.15 K >>> Parachor(100.15888, 800.8088185536124, 4.97865317223119, 0.02672166960656005) 5.088443542210164e-05 Converting to the `dimensionless` form: >>> 5623413*5.088443542210164e-05 286.14419565030687 Compared to 274.9 according to a group contribution method described in [3]_. References ---------- .. [1] Poling, Bruce E. The Properties of Gases and Liquids. 5th edition. New York: McGraw-Hill Professional, 2000. .. [2] Green, Don, and Robert Perry. Perry's Chemical Engineers' Handbook, 8E. McGraw-Hill Professional, 2007. .. [3] Danner, Ronald P, and Design Institute for Physical Property Data. Manual for Predicting Chemical Process Design Data. New York, N.Y, 1982. ''' rhol, rhog = rhol*1000., rhog*1000. # Convert kg/m^3 to g/m^3 return sigma**0.25*MW/(rhol-rhog)
def phase_identification_parameter(V, dP_dT, dP_dV, d2P_dV2, d2P_dVdT): r'''Calculate the Phase Identification Parameter developed in [1]_ for the accurate and efficient determination of whether a fluid is a liquid or a gas based on the results of an equation of state. For supercritical conditions, this provides a good method for choosing which property correlations to use. .. math:: \Pi = V \left[\frac{\frac{\partial^2 P}{\partial V \partial T}} {\frac{\partial P }{\partial T}}- \frac{\frac{\partial^2 P}{\partial V^2}}{\frac{\partial P}{\partial V}} \right] Parameters ---------- V : float Molar volume at `T` and `P`, [m^3/mol] dP_dT : float Derivative of `P` with respect to `T`, [Pa/K] dP_dV : float Derivative of `P` with respect to `V`, [Pa*mol/m^3] d2P_dV2 : float Second derivative of `P` with respect to `V`, [Pa*mol^2/m^6] d2P_dVdT : float Second derivative of `P` with respect to both `V` and `T`, [Pa*mol/m^3/K] Returns ------- PIP : float Phase Identification Parameter, [-] Notes ----- Heuristics were used by process simulators before the invent of this parameter. The criteria for liquid is Pi > 1; for vapor, Pi <= 1. There is also a solid phase mechanism available. For solids, the Solid Phase Identification Parameter is greater than 1, like liquids; however, unlike liquids, d2P_dVdT is always >0; it is < 0 for liquids and gases. Examples -------- Calculated for hexane from the PR EOS at 299 K and 1 MPa (liquid): >>> phase_identification_parameter(0.000130229900874, 582169.397484, ... -3.66431747236e+12, 4.48067893805e+17, -20518995218.2) 11.33428990564796 References ---------- .. [1] Venkatarathnam, G., and L. R. Oellrich. "Identification of the Phase of a Fluid Using Partial Derivatives of Pressure, Volume, and Temperature without Reference to Saturation Properties: Applications in Phase Equilibria Calculations." Fluid Phase Equilibria 301, no. 2 (February 25, 2011): 225-33. doi:10.1016/j.fluid.2010.12.001. .. [2] Jayanti, Pranava Chaitanya, and G. Venkatarathnam. "Identification of the Phase of a Substance from the Derivatives of Pressure, Volume and Temperature, without Prior Knowledge of Saturation Properties: Extension to Solid Phase." Fluid Phase Equilibria 425 (October 15, 2016): 269-277. doi:10.1016/j.fluid.2016.06.001. ''' return V*(d2P_dVdT/dP_dT - d2P_dV2/dP_dV)
def phase_identification_parameter_phase(d2P_dVdT, V=None, dP_dT=None, dP_dV=None, d2P_dV2=None): r'''Uses the Phase Identification Parameter concept developed in [1]_ and [2]_ to determine if a chemical is a solid, liquid, or vapor given the appropriate thermodynamic conditions. The criteria for liquid is PIP > 1; for vapor, PIP <= 1. For solids, PIP(solid) is defined to be d2P_dVdT. If it is larger than 0, the species is a solid. It is less than 0 for all liquids and gases. Parameters ---------- d2P_dVdT : float Second derivative of `P` with respect to both `V` and `T`, [Pa*mol/m^3/K] V : float, optional Molar volume at `T` and `P`, [m^3/mol] dP_dT : float, optional Derivative of `P` with respect to `T`, [Pa/K] dP_dV : float, optional Derivative of `P` with respect to `V`, [Pa*mol/m^3] d2P_dV2 : float, optionsl Second derivative of `P` with respect to `V`, [Pa*mol^2/m^6] Returns ------- phase : str Either 's', 'l' or 'g' Notes ----- The criteria for being a solid phase is checked first, which only requires d2P_dVdT. All other inputs are optional for this reason. However, an exception will be raised if the other inputs become needed to determine if a species is a liquid or a gas. Examples -------- Calculated for hexane from the PR EOS at 299 K and 1 MPa (liquid): >>> phase_identification_parameter_phase(-20518995218.2, 0.000130229900874, ... 582169.397484, -3.66431747236e+12, 4.48067893805e+17) 'l' References ---------- .. [1] Venkatarathnam, G., and L. R. Oellrich. "Identification of the Phase of a Fluid Using Partial Derivatives of Pressure, Volume, and Temperature without Reference to Saturation Properties: Applications in Phase Equilibria Calculations." Fluid Phase Equilibria 301, no. 2 (February 25, 2011): 225-33. doi:10.1016/j.fluid.2010.12.001. .. [2] Jayanti, Pranava Chaitanya, and G. Venkatarathnam. "Identification of the Phase of a Substance from the Derivatives of Pressure, Volume and Temperature, without Prior Knowledge of Saturation Properties: Extension to Solid Phase." Fluid Phase Equilibria 425 (October 15, 2016): 269-277. doi:10.1016/j.fluid.2016.06.001. ''' if d2P_dVdT > 0: return 's' else: PIP = phase_identification_parameter(V=V, dP_dT=dP_dT, dP_dV=dP_dV, d2P_dV2=d2P_dV2, d2P_dVdT=d2P_dVdT) return 'l' if PIP > 1 else 'g'
def speed_of_sound(V, dP_dV, Cp, Cv, MW=None): r'''Calculate a real fluid's speed of sound. The required derivatives should be calculated with an equation of state, and `Cp` and `Cv` are both the real fluid versions. Expression is given in [1]_ and [2]_; a unit conversion is further performed to obtain a result in m/s. If MW is not provided the result is returned in units of m*kg^0.5/s/mol^0.5. .. math:: w = \left[-V^2 \left(\frac{\partial P}{\partial V}\right)_T \frac{C_p} {C_v}\right]^{1/2} Parameters ---------- V : float Molar volume of fluid, [m^3/mol] dP_dV : float Derivative of `P` with respect to `V`, [Pa*mol/m^3] Cp : float Real fluid heat capacity at constant pressure, [J/mol/K] Cv : float Real fluid heat capacity at constant volume, [J/mol/K] MW : float, optional Molecular weight, [g/mol] Returns ------- w : float Speed of sound for a real gas, [m/s or m*kg^0.5/s/mol^0.5 or MW missing] Notes ----- An alternate expression based on molar density is as follows: .. math:: w = \left[\left(\frac{\partial P}{\partial \rho}\right)_T \frac{C_p} {C_v}\right]^{1/2} The form with the unit conversion performed inside it is as follows: .. math:: w = \left[-V^2 \frac{1000}{MW}\left(\frac{\partial P}{\partial V} \right)_T \frac{C_p}{C_v}\right]^{1/2} Examples -------- Example from [2]_: >>> speed_of_sound(V=0.00229754, dP_dV=-3.5459e+08, Cp=153.235, Cv=132.435, MW=67.152) 179.5868138460819 References ---------- .. [1] Gmehling, Jurgen, Barbel Kolbe, Michael Kleiber, and Jurgen Rarey. Chemical Thermodynamics for Process Simulation. 1st edition. Weinheim: Wiley-VCH, 2012. .. [2] Pratt, R. M. "Thermodynamic Properties Involving Derivatives: Using the Peng-Robinson Equation of State." Chemical Engineering Education 35, no. 2 (March 1, 2001): 112-115. ''' if not MW: return (-V**2*dP_dV*Cp/Cv)**0.5 else: return (-V**2*1000./MW*dP_dV*Cp/Cv)**0.5
def Joule_Thomson(T, V, Cp, dV_dT=None, beta=None): r'''Calculate a real fluid's Joule Thomson coefficient. The required derivative should be calculated with an equation of state, and `Cp` is the real fluid versions. This can either be calculated with `dV_dT` directly, or with `beta` if it is already known. .. math:: \mu_{JT} = \left(\frac{\partial T}{\partial P}\right)_H = \frac{1}{C_p} \left[T \left(\frac{\partial V}{\partial T}\right)_P - V\right] = \frac{V}{C_p}\left(\beta T-1\right) Parameters ---------- T : float Temperature of fluid, [K] V : float Molar volume of fluid, [m^3/mol] Cp : float Real fluid heat capacity at constant pressure, [J/mol/K] dV_dT : float, optional Derivative of `V` with respect to `T`, [m^3/mol/K] beta : float, optional Isobaric coefficient of a thermal expansion, [1/K] Returns ------- mu_JT : float Joule-Thomson coefficient [K/Pa] Examples -------- Example from [2]_: >>> Joule_Thomson(T=390, V=0.00229754, Cp=153.235, dV_dT=1.226396e-05) 1.621956080529905e-05 References ---------- .. [1] Walas, Stanley M. Phase Equilibria in Chemical Engineering. Butterworth-Heinemann, 1985. .. [2] Pratt, R. M. "Thermodynamic Properties Involving Derivatives: Using the Peng-Robinson Equation of State." Chemical Engineering Education 35, no. 2 (March 1, 2001): 112-115. ''' if dV_dT: return (T*dV_dT - V)/Cp elif beta: return V/Cp*(beta*T - 1.) else: raise Exception('Either dV_dT or beta is needed')
def Z_from_virial_density_form(T, P, *args): r'''Calculates the compressibility factor of a gas given its temperature, pressure, and molar density-form virial coefficients. Any number of coefficients is supported. .. math:: Z = \frac{PV}{RT} = 1 + \frac{B}{V} + \frac{C}{V^2} + \frac{D}{V^3} + \frac{E}{V^4} \dots Parameters ---------- T : float Temperature, [K] P : float Pressure, [Pa] B to Z : float, optional Virial coefficients, [various] Returns ------- Z : float Compressibility factor at T, P, and with given virial coefficients, [-] Notes ----- For use with B or with B and C or with B and C and D, optimized equations are used to obtain the compressibility factor directly. If more coefficients are provided, uses numpy's roots function to solve this equation. This takes substantially longer as the solution is numerical. If no virial coefficients are given, returns 1, as per the ideal gas law. The units of each virial coefficient are as follows, where for B, n=1, and C, n=2, and so on. .. math:: \left(\frac{\text{m}^3}{\text{mol}}\right)^n Examples -------- >>> Z_from_virial_density_form(300, 122057.233762653, 1E-4, 1E-5, 1E-6, 1E-7) 1.2843496002100001 References ---------- .. [1] Prausnitz, John M., Rudiger N. Lichtenthaler, and Edmundo Gomes de Azevedo. Molecular Thermodynamics of Fluid-Phase Equilibria. 3rd edition. Upper Saddle River, N.J: Prentice Hall, 1998. .. [2] Walas, Stanley M. Phase Equilibria in Chemical Engineering. Butterworth-Heinemann, 1985. ''' l = len(args) if l == 1: return 1/2. + (4*args[0]*P + R*T)**0.5/(2*(R*T)**0.5) # return ((R*T*(4*args[0]*P + R*T))**0.5 + R*T)/(2*P) if l == 2: B, C = args # A small imaginary part is ignored return (P*(-(3*B*R*T/P + R**2*T**2/P**2)/(3*(-1/2 + csqrt(3)*1j/2)*(-9*B*R**2*T**2/(2*P**2) - 27*C*R*T/(2*P) + csqrt(-4*(3*B*R*T/P + R**2*T**2/P**2)**(3+0j) + (-9*B*R**2*T**2/P**2 - 27*C*R*T/P - 2*R**3*T**3/P**3)**(2+0j))/2 - R**3*T**3/P**3)**(1/3.+0j)) - (-1/2 + csqrt(3)*1j/2)*(-9*B*R**2*T**2/(2*P**2) - 27*C*R*T/(2*P) + csqrt(-4*(3*B*R*T/P + R**2*T**2/P**2)**(3+0j) + (-9*B*R**2*T**2/P**2 - 27*C*R*T/P - 2*R**3*T**3/P**3)**(2+0j))/2 - R**3*T**3/P**3)**(1/3.+0j)/3 + R*T/(3*P))/(R*T)).real if l == 3: # Huge mess. Ideally sympy could optimize a function for quick python # execution. Derived with kate's text highlighting B, C, D = args P2 = P**2 RT = R*T BRT = B*RT T2 = T**2 R2 = R**2 RT23 = 3*R2*T2 mCRT = -C*RT P2256 = 256*P2 RT23P2256 = RT23/(P2256) big1 = (D*RT/P - (-BRT/P - RT23/(8*P2))**2/12 - RT*(mCRT/(4*P) - RT*(BRT/(16*P) + RT23P2256)/P)/P) big3 = (-BRT/P - RT23/(8*P2)) big4 = (mCRT/P - RT*(BRT/(2*P) + R2*T2/(8*P2))/P) big5 = big3*(-D*RT/P + RT*(mCRT/(4*P) - RT*(BRT/(16*P) + RT23P2256)/P)/P) big2 = 2*big1/(3*(big3**3/216 - big5/6 + big4**2/16 + csqrt(big1**3/27 + (-big3**3/108 + big5/3 - big4**2/8)**2/4))**(1/3)) big7 = 2*BRT/(3*P) - big2 + 2*(big3**3/216 - big5/6 + big4**2/16 + csqrt(big1**3/27 + (-big3**3/108 + big5/3 - big4**2/8)**2/4))**(1/3) + R2*T2/(4*P2) return (P*(((csqrt(big7)/2 + csqrt(4*BRT/(3*P) - (-2*C*RT/P - 2*RT*(BRT/(2*P) + R2*T2/(8*P2))/P)/csqrt(big7) + big2 - 2*(big3**3/216 - big5/6 + big4**2/16 + csqrt(big1**3/27 + (-big3**3/108 + big5/3 - big4**2/8)**2/4))**(1/3) + R2*T2/(2*P2))/2 + RT/(4*P))))/R/T).real args = list(args) args.reverse() args.extend([1, -P/R/T]) solns = np.roots(args) rho = [i for i in solns if not i.imag and i.real > 0][0].real # Quicker than indexing where imag ==0 return P/rho/R/T
def Z_from_virial_pressure_form(P, *args): r'''Calculates the compressibility factor of a gas given its pressure, and pressure-form virial coefficients. Any number of coefficients is supported. .. math:: Z = \frac{Pv}{RT} = 1 + B'P + C'P^2 + D'P^3 + E'P^4 \dots Parameters ---------- P : float Pressure, [Pa] B to Z : float, optional Pressure form Virial coefficients, [various] Returns ------- Z : float Compressibility factor at P, and with given virial coefficients, [-] Notes ----- Note that although this function does not require a temperature input, it is still dependent on it because the coefficients themselves normally are regressed in terms of temperature. The use of this form is less common than the density form. Its coefficients are normally indicated with the "'" suffix. If no virial coefficients are given, returns 1, as per the ideal gas law. The units of each virial coefficient are as follows, where for B, n=1, and C, n=2, and so on. .. math:: \left(\frac{1}{\text{Pa}}\right)^n Examples -------- >>> Z_from_virial_pressure_form(102919.99946855308, 4.032286555169439e-09, 1.6197059494442215e-13, 6.483855042486911e-19) 1.00283753944 References ---------- .. [1] Prausnitz, John M., Rudiger N. Lichtenthaler, and Edmundo Gomes de Azevedo. Molecular Thermodynamics of Fluid-Phase Equilibria. 3rd edition. Upper Saddle River, N.J: Prentice Hall, 1998. .. [2] Walas, Stanley M. Phase Equilibria in Chemical Engineering. Butterworth-Heinemann, 1985. ''' return 1 + P*sum([coeff*P**i for i, coeff in enumerate(args)])
def zs_to_ws(zs, MWs): r'''Converts a list of mole fractions to mass fractions. Requires molecular weights for all species. .. math:: w_i = \frac{z_i MW_i}{MW_{avg}} MW_{avg} = \sum_i z_i MW_i Parameters ---------- zs : iterable Mole fractions [-] MWs : iterable Molecular weights [g/mol] Returns ------- ws : iterable Mass fractions [-] Notes ----- Does not check that the sums add to one. Does not check that inputs are of the same length. Examples -------- >>> zs_to_ws([0.5, 0.5], [10, 20]) [0.3333333333333333, 0.6666666666666666] ''' Mavg = sum(zi*MWi for zi, MWi in zip(zs, MWs)) ws = [zi*MWi/Mavg for zi, MWi in zip(zs, MWs)] return ws
def ws_to_zs(ws, MWs): r'''Converts a list of mass fractions to mole fractions. Requires molecular weights for all species. .. math:: z_i = \frac{\frac{w_i}{MW_i}}{\sum_i \frac{w_i}{MW_i}} Parameters ---------- ws : iterable Mass fractions [-] MWs : iterable Molecular weights [g/mol] Returns ------- zs : iterable Mole fractions [-] Notes ----- Does not check that the sums add to one. Does not check that inputs are of the same length. Examples -------- >>> ws_to_zs([0.3333333333333333, 0.6666666666666666], [10, 20]) [0.5, 0.5] ''' tot = sum(w/MW for w, MW in zip(ws, MWs)) zs = [w/MW/tot for w, MW in zip(ws, MWs)] return zs
def zs_to_Vfs(zs, Vms): r'''Converts a list of mole fractions to volume fractions. Requires molar volumes for all species. .. math:: \text{Vf}_i = \frac{z_i V_{m,i}}{\sum_i z_i V_{m,i}} Parameters ---------- zs : iterable Mole fractions [-] VMs : iterable Molar volumes of species [m^3/mol] Returns ------- Vfs : list Molar volume fractions [-] Notes ----- Does not check that the sums add to one. Does not check that inputs are of the same length. Molar volumes are specified in terms of pure components only. Function works with any phase. Examples -------- Acetone and benzene example >>> zs_to_Vfs([0.637, 0.363], [8.0234e-05, 9.543e-05]) [0.5960229712956298, 0.4039770287043703] ''' vol_is = [zi*Vmi for zi, Vmi in zip(zs, Vms)] tot = sum(vol_is) return [vol_i/tot for vol_i in vol_is]
def Vfs_to_zs(Vfs, Vms): r'''Converts a list of mass fractions to mole fractions. Requires molecular weights for all species. .. math:: z_i = \frac{\frac{\text{Vf}_i}{V_{m,i}}}{\sum_i \frac{\text{Vf}_i}{V_{m,i}}} Parameters ---------- Vfs : iterable Molar volume fractions [-] VMs : iterable Molar volumes of species [m^3/mol] Returns ------- zs : list Mole fractions [-] Notes ----- Does not check that the sums add to one. Does not check that inputs are of the same length. Molar volumes are specified in terms of pure components only. Function works with any phase. Examples -------- Acetone and benzene example >>> Vfs_to_zs([0.596, 0.404], [8.0234e-05, 9.543e-05]) [0.6369779395901142, 0.3630220604098858] ''' mols_i = [Vfi/Vmi for Vfi, Vmi in zip(Vfs, Vms)] mols = sum(mols_i) return [mol_i/mols for mol_i in mols_i]
def none_and_length_check(all_inputs, length=None): r'''Checks inputs for suitability of use by a mixing rule which requires all inputs to be of the same length and non-None. A number of variations were attempted for this function; this was found to be the quickest. Parameters ---------- all_inputs : array-like of array-like list of all the lists of inputs, [-] length : int, optional Length of the desired inputs, [-] Returns ------- False/True : bool Returns True only if all inputs are the same length (or length `length`) and none of the inputs contain None [-] Notes ----- Does not check for nan values. Examples -------- >>> none_and_length_check(([1, 1], [1, 1], [1, 30], [10,0]), length=2) True ''' if not length: length = len(all_inputs[0]) for things in all_inputs: if None in things or len(things) != length: return False return True
def allclose_variable(a, b, limits, rtols=None, atols=None): '''Returns True if two arrays are element-wise equal within several different tolerances. Tolerance values are always positive, usually very small. Based on numpy's allclose function. Only atols or rtols needs to be specified; both are used if given. Parameters ---------- a, b : array_like Input arrays to compare. limits : array_like Fractions of elements allowed to not match to within each tolerance. rtols : array_like The relative tolerance parameters. atols : float The absolute tolerance parameters. Returns ------- allclose : bool Returns True if the two arrays are equal within the given tolerances; False otherwise. Examples -------- 10 random similar variables, all of them matching to within 1E-5, allowing up to half to match up to 1E-6. >>> x = [2.7244322249597719e-08, 3.0105683900110473e-10, 2.7244124924802327e-08, 3.0105259397637556e-10, 2.7243929226310193e-08, 3.0104990272770901e-10, 2.7243666849384451e-08, 3.0104101821236015e-10, 2.7243433745917367e-08, 3.0103707421519949e-10] >>> y = [2.7244328304561904e-08, 3.0105753470546008e-10, 2.724412872417824e-08, 3.0105303055834564e-10, 2.7243914341030203e-08, 3.0104819238021998e-10, 2.7243684057561379e-08, 3.0104299541023674e-10, 2.7243436694839306e-08, 3.010374130526363e-10] >>> allclose_variable(x, y, limits=[.0, .5], rtols=[1E-5, 1E-6]) True ''' l = float(len(a)) if rtols is None and atols is None: raise Exception('Either absolute errors or relative errors must be supplied.') elif rtols is None: rtols = [0 for i in atols] elif atols is None: atols = [0 for i in rtols] for atol, rtol, lim in zip(atols, rtols, limits): matches = np.count_nonzero(np.isclose(a, b, rtol=rtol, atol=atol)) if 1-matches/l > lim: return False return True
def polylog2(x): r'''Simple function to calculate PolyLog(2, x) from ranges 0 <= x <= 1, with relative error guaranteed to be < 1E-7 from 0 to 0.99999. This is a Pade approximation, with three coefficient sets with splits at 0.7 and 0.99. An exception is raised if x is under 0 or above 1. Parameters ---------- x : float Value to evaluate PolyLog(2, x) T Returns ------- y : float Evaluated result Notes ----- Efficient (2-4 microseconds). No implementation of this function exists in SciPy. Derived with mpmath's pade approximation. Required for the entropy integral of :obj:`thermo.heat_capacity.Zabransky_quasi_polynomial`. Examples -------- >>> polylog2(0.5) 0.5822405264516294 ''' if 0 <= x <= 0.7: p = [0.06184590404457956, -0.7460693871557973, 2.2435704485433376, -2.1944070385048526, 0.3382265629285811, 0.2791966558569478] q = [-0.005308735283483908, 0.1823421262956287, -1.2364596896290079, 2.9897802200092296, -2.9365321202088004, 1.0] offset = 0.26 elif 0.7 < x <= 0.99: p = [7543860.817140365, -10254250.429758755, -4186383.973408412, 7724476.972409749, -3130743.609030545, 600806.068543299, -62981.15051292659, 3696.7937385473397, -114.06795167646395, 1.4406337969700391] q = [-1262997.3422452002, 10684514.56076485, -16931658.916668657, 10275996.02842749, -3079141.9506451315, 511164.4690136096, -49254.56172495263, 2738.0399260270983, -81.36790509581284, 1.0] offset = 0.95 elif 0.99 < x <= 1: p = [8.548256176424551e+34, 1.8485781239087334e+35, -2.1706889553798647e+34, 8.318563643438321e+32, -1.559802348661511e+31, 1.698939241177209e+29, -1.180285031647229e+27, 5.531049937687143e+24, -1.8085903366375877e+22, 4.203276811951035e+19, -6.98211620300421e+16, 82281997048841.92, -67157299796.61345, 36084814.54808544, -11478.108105137717, 1.6370226052761176] q = [-1.9763570499484274e+35, 1.4813997374958851e+35, -1.4773854824041134e+34, 5.38853721252814e+32, -9.882387315028929e+30, 1.0635231532999732e+29, -7.334629044071992e+26, 3.420655574477631e+24, -1.1147787784365177e+22, 2.584530363912858e+19, -4.285376337404043e+16, 50430830490687.56, -41115254924.43107, 22072284.971253656, -7015.799744041691, 1.0] offset = 0.999 else: raise Exception('Approximation is valid between 0 and 1 only.') x = x - offset return horner(p, x)/horner(q, x)
def mixing_simple(fracs, props): r'''Simple function calculates a property based on weighted averages of properties. Weights could be mole fractions, volume fractions, mass fractions, or anything else. .. math:: y = \sum_i \text{frac}_i \cdot \text{prop}_i Parameters ---------- fracs : array-like Fractions of a mixture props: array-like Properties Returns ------- prop : value Calculated property Notes ----- Returns None if any fractions or properties are missing or are not of the same length. Examples -------- >>> mixing_simple([0.1, 0.9], [0.01, 0.02]) 0.019000000000000003 ''' if not none_and_length_check([fracs, props]): return None result = sum(frac*prop for frac, prop in zip(fracs, props)) return result
def mixing_logarithmic(fracs, props): r'''Simple function calculates a property based on weighted averages of logarithmic properties. .. math:: y = \sum_i \text{frac}_i \cdot \log(\text{prop}_i) Parameters ---------- fracs : array-like Fractions of a mixture props: array-like Properties Returns ------- prop : value Calculated property Notes ----- Does not work on negative values. Returns None if any fractions or properties are missing or are not of the same length. Examples -------- >>> mixing_logarithmic([0.1, 0.9], [0.01, 0.02]) 0.01866065983073615 ''' if not none_and_length_check([fracs, props]): return None return exp(sum(frac*log(prop) for frac, prop in zip(fracs, props)))
def phase_select_property(phase=None, s=None, l=None, g=None, V_over_F=None): r'''Determines which phase's property should be set as a default, given the phase a chemical is, and the property values of various phases. For the case of liquid-gas phase, returns None. If the property is not available for the current phase, or if the current phase is not known, returns None. Parameters ---------- phase : str One of {'s', 'l', 'g', 'two-phase'} s : float Solid-phase property l : float Liquid-phase property g : float Gas-phase property V_over_F : float Vapor phase fraction Returns ------- prop : float The selected/calculated property for the relevant phase Notes ----- Could calculate mole-fraction weighted properties for the two phase regime. Could also implement equilibria with solid phases. Examples -------- >>> phase_select_property(phase='g', l=1560.14, g=3312.) 3312.0 ''' if phase == 's': return s elif phase == 'l': return l elif phase == 'g': return g elif phase == 'two-phase': return None #TODO: all two-phase properties? elif phase is None: return None else: raise Exception('Property not recognized')
def set_user_methods(self, user_methods, forced=False): r'''Method used to select certain property methods as having a higher priority than were set by default. If `forced` is true, then methods which were not specified are excluded from consideration. As a side effect, `method` is removed to ensure than the new methods will be used in calculations afterwards. An exception is raised if any of the methods specified aren't available for the chemical. An exception is raised if no methods are provided. Parameters ---------- user_methods : str or list Methods by name to be considered or prefered forced : bool, optional If True, only the user specified methods will ever be considered; if False other methods will be considered if no user methods suceed ''' # Accept either a string or a list of methods, and whether # or not to only consider the false methods if isinstance(user_methods, str): user_methods = [user_methods] # The user's order matters and is retained for use by select_valid_methods self.user_methods = user_methods self.forced = forced # Validate that the user's specified methods are actual methods if set(self.user_methods).difference(self.all_methods): raise Exception("One of the given methods is not available for this chemical") if not self.user_methods and self.forced: raise Exception('Only user specified methods are considered when forced is True, but no methods were provided') # Remove previously selected methods self.method = None self.sorted_valid_methods = [] self.T_cached = None
def select_valid_methods(self, T): r'''Method to obtain a sorted list of methods which are valid at `T` according to `test_method_validity`. Considers either only user methods if forced is True, or all methods. User methods are first tested according to their listed order, and unless forced is True, then all methods are tested and sorted by their order in `ranked_methods`. Parameters ---------- T : float Temperature at which to test methods, [K] Returns ------- sorted_valid_methods : list Sorted lists of methods valid at T according to `test_method_validity` ''' # Consider either only the user's methods or all methods # Tabular data will be in both when inserted if self.forced: considered_methods = list(self.user_methods) else: considered_methods = list(self.all_methods) # User methods (incl. tabular data); add back later, after ranking the rest if self.user_methods: [considered_methods.remove(i) for i in self.user_methods] # Index the rest of the methods by ranked_methods, and add them to a list, sorted_methods preferences = sorted([self.ranked_methods.index(i) for i in considered_methods]) sorted_methods = [self.ranked_methods[i] for i in preferences] # Add back the user's methods to the top, in order. if self.user_methods: [sorted_methods.insert(0, i) for i in reversed(self.user_methods)] sorted_valid_methods = [] for method in sorted_methods: if self.test_method_validity(T, method): sorted_valid_methods.append(method) return sorted_valid_methods
def T_dependent_property(self, T): r'''Method to calculate the property with sanity checking and without specifying a specific method. `select_valid_methods` is used to obtain a sorted list of methods to try. Methods are then tried in order until one succeeds. The methods are allowed to fail, and their results are checked with `test_property_validity`. On success, the used method is stored in the variable `method`. If `method` is set, this method is first checked for validity with `test_method_validity` for the specified temperature, and if it is valid, it is then used to calculate the property. The result is checked for validity, and returned if it is valid. If either of the checks fail, the function retrieves a full list of valid methods with `select_valid_methods` and attempts them as described above. If no methods are found which succeed, returns None. Parameters ---------- T : float Temperature at which to calculate the property, [K] Returns ------- prop : float Calculated property, [`units`] ''' # Optimistic track, with the already set method if self.method: # retest within range if self.test_method_validity(T, self.method): try: prop = self.calculate(T, self.method) if self.test_property_validity(prop): return prop except: # pragma: no cover pass # get valid methods at T, and try them until one yields a valid # property; store the method and return the answer self.sorted_valid_methods = self.select_valid_methods(T) for method in self.sorted_valid_methods: try: prop = self.calculate(T, method) if self.test_property_validity(prop): self.method = method return prop except: # pragma: no cover pass # Function returns None if it does not work. return None
def plot_T_dependent_property(self, Tmin=None, Tmax=None, methods=[], pts=50, only_valid=True, order=0): # pragma: no cover r'''Method to create a plot of the property vs temperature according to either a specified list of methods, or user methods (if set), or all methods. User-selectable number of points, and temperature range. If only_valid is set,`test_method_validity` will be used to check if each temperature in the specified range is valid, and `test_property_validity` will be used to test the answer, and the method is allowed to fail; only the valid points will be plotted. Otherwise, the result will be calculated and displayed as-is. This will not suceed if the method fails. Parameters ---------- Tmin : float Minimum temperature, to begin calculating the property, [K] Tmax : float Maximum temperature, to stop calculating the property, [K] methods : list, optional List of methods to consider pts : int, optional A list of points to calculate the property at; if Tmin to Tmax covers a wide range of method validities, only a few points may end up calculated for a given method so this may need to be large only_valid : bool If True, only plot successful methods and calculated properties, and handle errors; if False, attempt calculation without any checking and use methods outside their bounds ''' # This function cannot be tested if not has_matplotlib: raise Exception('Optional dependency matplotlib is required for plotting') if Tmin is None: if self.Tmin is not None: Tmin = self.Tmin else: raise Exception('Minimum temperature could not be auto-detected; please provide it') if Tmax is None: if self.Tmax is not None: Tmax = self.Tmax else: raise Exception('Maximum temperature could not be auto-detected; please provide it') if not methods: if self.user_methods: methods = self.user_methods else: methods = self.all_methods Ts = np.linspace(Tmin, Tmax, pts) if order == 0: for method in methods: if only_valid: properties, Ts2 = [], [] for T in Ts: if self.test_method_validity(T, method): try: p = self.calculate(T=T, method=method) if self.test_property_validity(p): properties.append(p) Ts2.append(T) except: pass plt.semilogy(Ts2, properties, label=method) else: properties = [self.calculate(T=T, method=method) for T in Ts] plt.semilogy(Ts, properties, label=method) plt.ylabel(self.name + ', ' + self.units) plt.title(self.name + ' of ' + self.CASRN) elif order > 0: for method in methods: if only_valid: properties, Ts2 = [], [] for T in Ts: if self.test_method_validity(T, method): try: p = self.calculate_derivative(T=T, method=method, order=order) properties.append(p) Ts2.append(T) except: pass plt.semilogy(Ts2, properties, label=method) else: properties = [self.calculate_derivative(T=T, method=method, order=order) for T in Ts] plt.semilogy(Ts, properties, label=method) plt.ylabel(self.name + ', ' + self.units + '/K^%d derivative of order %d' % (order, order)) plt.title(self.name + ' derivative of order %d' % order + ' of ' + self.CASRN) plt.legend(loc='best') plt.xlabel('Temperature, K') plt.show()
def interpolate(self, T, name): r'''Method to perform interpolation on a given tabular data set previously added via :obj:`set_tabular_data`. This method will create the interpolators the first time it is used on a property set, and store them for quick future use. Interpolation is cubic-spline based if 5 or more points are available, and linearly interpolated if not. Extrapolation is always performed linearly. This function uses the transforms `interpolation_T`, `interpolation_property`, and `interpolation_property_inv` if set. If any of these are changed after the interpolators were first created, new interpolators are created with the new transforms. All interpolation is performed via the `interp1d` function. Parameters ---------- T : float Temperature at which to interpolate the property, [K] name : str The name assigned to the tabular data set Returns ------- prop : float Calculated property, [`units`] ''' key = (name, self.interpolation_T, self.interpolation_property, self.interpolation_property_inv) # If the interpolator and extrapolator has already been created, load it # if isinstance(self.tabular_data_interpolators, dict) and key in self.tabular_data_interpolators: # extrapolator, spline = self.tabular_data_interpolators[key] if key in self.tabular_data_interpolators: extrapolator, spline = self.tabular_data_interpolators[key] else: Ts, properties = self.tabular_data[name] if self.interpolation_T: # Transform ths Ts with interpolation_T if set Ts2 = [self.interpolation_T(T2) for T2 in Ts] else: Ts2 = Ts if self.interpolation_property: # Transform ths props with interpolation_property if set properties2 = [self.interpolation_property(p) for p in properties] else: properties2 = properties # Only allow linear extrapolation, but with whatever transforms are specified extrapolator = interp1d(Ts2, properties2, fill_value='extrapolate') # If more than 5 property points, create a spline interpolation if len(properties) >= 5: spline = interp1d(Ts2, properties2, kind='cubic') else: spline = None # if isinstance(self.tabular_data_interpolators, dict): # self.tabular_data_interpolators[key] = (extrapolator, spline) # else: # self.tabular_data_interpolators = {key: (extrapolator, spline)} self.tabular_data_interpolators[key] = (extrapolator, spline) # Load the stores values, tor checking which interpolation strategy to # use. Ts, properties = self.tabular_data[name] if T < Ts[0] or T > Ts[-1] or not spline: tool = extrapolator else: tool = spline if self.interpolation_T: T = self.interpolation_T(T) prop = tool(T) # either spline, or linear interpolation if self.interpolation_property: prop = self.interpolation_property_inv(prop) return float(prop)
def set_tabular_data(self, Ts, properties, name=None, check_properties=True): r'''Method to set tabular data to be used for interpolation. Ts must be in increasing order. If no name is given, data will be assigned the name 'Tabular data series #x', where x is the number of previously added tabular data series. The name is added to all methods and iserted at the start of user methods, Parameters ---------- Ts : array-like Increasing array of temperatures at which properties are specified, [K] properties : array-like List of properties at Ts, [`units`] name : str, optional Name assigned to the data check_properties : bool If True, the properties will be checked for validity with `test_property_validity` and raise an exception if any are not valid ''' # Ts must be in increasing order. if check_properties: for p in properties: if not self.test_property_validity(p): raise Exception('One of the properties specified are not feasible') if not all(b > a for a, b in zip(Ts, Ts[1:])): raise Exception('Temperatures are not sorted in increasing order') if name is None: name = 'Tabular data series #' + str(len(self.tabular_data)) # Will overwrite a poorly named series self.tabular_data[name] = (Ts, properties) # TODO set Tmin and Tmax if not set self.method = None self.user_methods.insert(0, name) self.all_methods.add(name) self.set_user_methods(user_methods=self.user_methods, forced=self.forced)
def solve_prop(self, goal, reset_method=True): r'''Method to solve for the temperature at which a property is at a specified value. `T_dependent_property` is used to calculate the value of the property as a function of temperature; if `reset_method` is True, the best method is used at each temperature as the solver seeks a solution. This slows the solution moderately. Checks the given property value with `test_property_validity` first and raises an exception if it is not valid. Requires that Tmin and Tmax have been set to know what range to search within. Search is performed with the brenth solver from SciPy. Parameters ---------- goal : float Propoerty value desired, [`units`] reset_method : bool Whether or not to reset the method as the solver searches Returns ------- T : float Temperature at which the property is the specified value [K] ''' if self.Tmin is None or self.Tmax is None: raise Exception('Both a minimum and a maximum value are not present indicating there is not enough data for temperature dependency.') if not self.test_property_validity(goal): raise Exception('Input property is not considered plausible; no method would calculate it.') def error(T): if reset_method: self.method = None return self.T_dependent_property(T) - goal try: return brenth(error, self.Tmin, self.Tmax) except ValueError: raise Exception('To within the implemented temperature range, it is not possible to calculate the desired value.')
def calculate_derivative(self, T, method, order=1): r'''Method to calculate a derivative of a property with respect to temperature, of a given order using a specified method. Uses SciPy's derivative function, with a delta of 1E-6 K and a number of points equal to 2*order + 1. This method can be overwritten by subclasses who may perfer to add analytical methods for some or all methods as this is much faster. If the calculation does not succeed, returns the actual error encountered. Parameters ---------- T : float Temperature at which to calculate the derivative, [K] method : str Method for which to find the derivative order : int Order of the derivative, >= 1 Returns ------- derivative : float Calculated derivative property, [`units/K^order`] ''' return derivative(self.calculate, T, dx=1e-6, args=[method], n=order, order=1+order*2)
def T_dependent_property_derivative(self, T, order=1): r'''Method to obtain a derivative of a property with respect to temperature, of a given order. Methods found valid by `select_valid_methods` are attempted until a method succeeds. If no methods are valid and succeed, None is returned. Calls `calculate_derivative` internally to perform the actual calculation. .. math:: \text{derivative} = \frac{d (\text{property})}{d T} Parameters ---------- T : float Temperature at which to calculate the derivative, [K] order : int Order of the derivative, >= 1 Returns ------- derivative : float Calculated derivative property, [`units/K^order`] ''' if self.method: # retest within range if self.test_method_validity(T, self.method): try: return self.calculate_derivative(T, self.method, order) except: # pragma: no cover pass sorted_valid_methods = self.select_valid_methods(T) for method in sorted_valid_methods: try: return self.calculate_derivative(T, method, order) except: pass return None
def calculate_integral(self, T1, T2, method): r'''Method to calculate the integral of a property with respect to temperature, using a specified method. Uses SciPy's `quad` function to perform the integral, with no options. This method can be overwritten by subclasses who may perfer to add analytical methods for some or all methods as this is much faster. If the calculation does not succeed, returns the actual error encountered. Parameters ---------- T1 : float Lower limit of integration, [K] T2 : float Upper limit of integration, [K] method : str Method for which to find the integral Returns ------- integral : float Calculated integral of the property over the given range, [`units*K`] ''' return float(quad(self.calculate, T1, T2, args=(method))[0])
def T_dependent_property_integral(self, T1, T2): r'''Method to calculate the integral of a property with respect to temperature, using a specified method. Methods found valid by `select_valid_methods` are attempted until a method succeeds. If no methods are valid and succeed, None is returned. Calls `calculate_integral` internally to perform the actual calculation. .. math:: \text{integral} = \int_{T_1}^{T_2} \text{property} \; dT Parameters ---------- T1 : float Lower limit of integration, [K] T2 : float Upper limit of integration, [K] method : str Method for which to find the integral Returns ------- integral : float Calculated integral of the property over the given range, [`units*K`] ''' Tavg = 0.5*(T1+T2) if self.method: # retest within range if self.test_method_validity(Tavg, self.method): try: return self.calculate_integral(T1, T2, self.method) except: # pragma: no cover pass sorted_valid_methods = self.select_valid_methods(Tavg) for method in sorted_valid_methods: try: return self.calculate_integral(T1, T2, method) except: pass return None
def calculate_integral_over_T(self, T1, T2, method): r'''Method to calculate the integral of a property over temperature with respect to temperature, using a specified method. Uses SciPy's `quad` function to perform the integral, with no options. This method can be overwritten by subclasses who may perfer to add analytical methods for some or all methods as this is much faster. If the calculation does not succeed, returns the actual error encountered. Parameters ---------- T1 : float Lower limit of integration, [K] T2 : float Upper limit of integration, [K] method : str Method for which to find the integral Returns ------- integral : float Calculated integral of the property over the given range, [`units`] ''' return float(quad(lambda T: self.calculate(T, method)/T, T1, T2)[0])
def T_dependent_property_integral_over_T(self, T1, T2): r'''Method to calculate the integral of a property over temperature with respect to temperature, using a specified method. Methods found valid by `select_valid_methods` are attempted until a method succeeds. If no methods are valid and succeed, None is returned. Calls `calculate_integral_over_T` internally to perform the actual calculation. .. math:: \text{integral} = \int_{T_1}^{T_2} \frac{\text{property}}{T} \; dT Parameters ---------- T1 : float Lower limit of integration, [K] T2 : float Upper limit of integration, [K] method : str Method for which to find the integral Returns ------- integral : float Calculated integral of the property over the given range, [`units`] ''' Tavg = 0.5*(T1+T2) if self.method: # retest within range if self.test_method_validity(Tavg, self.method): try: return self.calculate_integral_over_T(T1, T2, self.method) except: # pragma: no cover pass sorted_valid_methods = self.select_valid_methods(Tavg) for method in sorted_valid_methods: try: return self.calculate_integral_over_T(T1, T2, method) except: pass return None
def load_all_methods(self): r'''Method to load all data, and set all_methods based on the available data and properties. Demo function for testing only; must be implemented according to the methods available for each individual method. ''' methods = [] Tmins, Tmaxs = [], [] if self.CASRN in ['7732-18-5', '67-56-1', '64-17-5']: methods.append(TEST_METHOD_1) self.TEST_METHOD_1_Tmin = 200. self.TEST_METHOD_1_Tmax = 350 self.TEST_METHOD_1_coeffs = [1, .002] Tmins.append(self.TEST_METHOD_1_Tmin); Tmaxs.append(self.TEST_METHOD_1_Tmax) if self.CASRN in ['67-56-1']: methods.append(TEST_METHOD_2) self.TEST_METHOD_2_Tmin = 300. self.TEST_METHOD_2_Tmax = 400 self.TEST_METHOD_2_coeffs = [1, .003] Tmins.append(self.TEST_METHOD_2_Tmin); Tmaxs.append(self.TEST_METHOD_2_Tmax) self.all_methods = set(methods) if Tmins and Tmaxs: self.Tmin = min(Tmins) self.Tmax = max(Tmaxs)
def calculate(self, T, method): r'''Method to calculate a property with a specified method, with no validity checking or error handling. Demo function for testing only; must be implemented according to the methods available for each individual method. Include the interpolation call here. Parameters ---------- T : float Temperature at which to calculate the property, [K] method : str Method name to use Returns ------- prop : float Calculated property, [`units`] ''' if method == TEST_METHOD_1: prop = self.TEST_METHOD_1_coeffs[0] + self.TEST_METHOD_1_coeffs[1]*T elif method == TEST_METHOD_2: prop = self.TEST_METHOD_2_coeffs[0] + self.TEST_METHOD_2_coeffs[1]*T elif method in self.tabular_data: prop = self.interpolate(T, method) return prop
def set_user_methods_P(self, user_methods_P, forced_P=False): r'''Method to set the pressure-dependent property methods desired for consideration by the user. Can be used to exclude certain methods which might have unacceptable accuracy. As a side effect, the previously selected method is removed when this method is called to ensure user methods are tried in the desired order. Parameters ---------- user_methods_P : str or list Methods by name to be considered or preferred for pressure effect. forced : bool, optional If True, only the user specified methods will ever be considered; if False other methods will be considered if no user methods suceed. ''' # Accept either a string or a list of methods, and whether # or not to only consider the false methods if isinstance(user_methods_P, str): user_methods_P = [user_methods_P] # The user's order matters and is retained for use by select_valid_methods self.user_methods_P = user_methods_P self.forced_P = forced_P # Validate that the user's specified methods are actual methods if set(self.user_methods_P).difference(self.all_methods_P): raise Exception("One of the given methods is not available for this chemical") if not self.user_methods_P and self.forced: raise Exception('Only user specified methods are considered when forced is True, but no methods were provided') # Remove previously selected methods self.method_P = None self.sorted_valid_methods_P = [] self.TP_cached = None
def select_valid_methods_P(self, T, P): r'''Method to obtain a sorted list methods which are valid at `T` according to `test_method_validity`. Considers either only user methods if forced is True, or all methods. User methods are first tested according to their listed order, and unless forced is True, then all methods are tested and sorted by their order in `ranked_methods`. Parameters ---------- T : float Temperature at which to test methods, [K] P : float Pressure at which to test methods, [Pa] Returns ------- sorted_valid_methods_P : list Sorted lists of methods valid at T and P according to `test_method_validity` ''' # Same as select_valid_methods but with _P added to variables if self.forced_P: considered_methods = list(self.user_methods_P) else: considered_methods = list(self.all_methods_P) if self.user_methods_P: [considered_methods.remove(i) for i in self.user_methods_P] preferences = sorted([self.ranked_methods_P.index(i) for i in considered_methods]) sorted_methods = [self.ranked_methods_P[i] for i in preferences] if self.user_methods_P: [sorted_methods.insert(0, i) for i in reversed(self.user_methods_P)] sorted_valid_methods_P = [] for method in sorted_methods: if self.test_method_validity_P(T, P, method): sorted_valid_methods_P.append(method) return sorted_valid_methods_P
def TP_dependent_property(self, T, P): r'''Method to calculate the property with sanity checking and without specifying a specific method. `select_valid_methods_P` is used to obtain a sorted list of methods to try. Methods are then tried in order until one succeeds. The methods are allowed to fail, and their results are checked with `test_property_validity`. On success, the used method is stored in the variable `method_P`. If `method_P` is set, this method is first checked for validity with `test_method_validity_P` for the specified temperature, and if it is valid, it is then used to calculate the property. The result is checked for validity, and returned if it is valid. If either of the checks fail, the function retrieves a full list of valid methods with `select_valid_methods_P` and attempts them as described above. If no methods are found which succeed, returns None. Parameters ---------- T : float Temperature at which to calculate the property, [K] P : float Pressure at which to calculate the property, [Pa] Returns ------- prop : float Calculated property, [`units`] ''' # Optimistic track, with the already set method if self.method_P: # retest within range if self.test_method_validity_P(T, P, self.method_P): try: prop = self.calculate_P(T, P, self.method_P) if self.test_property_validity(prop): return prop except: # pragma: no cover pass # get valid methods at T, and try them until one yields a valid # property; store the method_P and return the answer self.sorted_valid_methods_P = self.select_valid_methods_P(T, P) for method_P in self.sorted_valid_methods_P: try: prop = self.calculate_P(T, P, method_P) if self.test_property_validity(prop): self.method_P = method_P return prop except: # pragma: no cover pass # Function returns None if it does not work. return None
def set_tabular_data_P(self, Ts, Ps, properties, name=None, check_properties=True): r'''Method to set tabular data to be used for interpolation. Ts and Psmust be in increasing order. If no name is given, data will be assigned the name 'Tabular data series #x', where x is the number of previously added tabular data series. The name is added to all methods and is inserted at the start of user methods, Parameters ---------- Ts : array-like Increasing array of temperatures at which properties are specified, [K] Ps : array-like Increasing array of pressures at which properties are specified, [Pa] properties : array-like List of properties at Ts, [`units`] name : str, optional Name assigned to the data check_properties : bool If True, the properties will be checked for validity with `test_property_validity` and raise an exception if any are not valid ''' # Ts must be in increasing order. if check_properties: for p in np.array(properties).ravel(): if not self.test_property_validity(p): raise Exception('One of the properties specified are not feasible') if not all(b > a for a, b in zip(Ts, Ts[1:])): raise Exception('Temperatures are not sorted in increasing order') if not all(b > a for a, b in zip(Ps, Ps[1:])): raise Exception('Pressures are not sorted in increasing order') if name is None: name = 'Tabular data series #' + str(len(self.tabular_data)) # Will overwrite a poorly named series self.tabular_data[name] = (Ts, Ps, properties) self.method_P = None self.user_methods_P.insert(0, name) self.all_methods_P.add(name) self.set_user_methods_P(user_methods_P=self.user_methods_P, forced_P=self.forced_P)
def interpolate_P(self, T, P, name): r'''Method to perform interpolation on a given tabular data set previously added via `set_tabular_data_P`. This method will create the interpolators the first time it is used on a property set, and store them for quick future use. Interpolation is cubic-spline based if 5 or more points are available, and linearly interpolated if not. Extrapolation is always performed linearly. This function uses the transforms `interpolation_T`, `interpolation_P`, `interpolation_property`, and `interpolation_property_inv` if set. If any of these are changed after the interpolators were first created, new interpolators are created with the new transforms. All interpolation is performed via the `interp2d` function. Parameters ---------- T : float Temperature at which to interpolate the property, [K] T : float Pressure at which to interpolate the property, [Pa] name : str The name assigned to the tabular data set Returns ------- prop : float Calculated property, [`units`] ''' key = (name, self.interpolation_T, self.interpolation_P, self.interpolation_property, self.interpolation_property_inv) # If the interpolator and extrapolator has already been created, load it if key in self.tabular_data_interpolators: extrapolator, spline = self.tabular_data_interpolators[key] else: Ts, Ps, properties = self.tabular_data[name] if self.interpolation_T: # Transform ths Ts with interpolation_T if set Ts2 = [self.interpolation_T(T2) for T2 in Ts] else: Ts2 = Ts if self.interpolation_P: # Transform ths Ts with interpolation_T if set Ps2 = [self.interpolation_P(P2) for P2 in Ps] else: Ps2 = Ps if self.interpolation_property: # Transform ths props with interpolation_property if set properties2 = [self.interpolation_property(p) for p in properties] else: properties2 = properties # Only allow linear extrapolation, but with whatever transforms are specified extrapolator = interp2d(Ts2, Ps2, properties2) # interpolation if fill value is missing # If more than 5 property points, create a spline interpolation if len(properties) >= 5: spline = interp2d(Ts2, Ps2, properties2, kind='cubic') else: spline = None self.tabular_data_interpolators[key] = (extrapolator, spline) # Load the stores values, tor checking which interpolation strategy to # use. Ts, Ps, properties = self.tabular_data[name] if T < Ts[0] or T > Ts[-1] or not spline or P < Ps[0] or P > Ps[-1]: tool = extrapolator else: tool = spline if self.interpolation_T: T = self.interpolation_T(T) if self.interpolation_P: P = self.interpolation_T(P) prop = tool(T, P) # either spline, or linear interpolation if self.interpolation_property: prop = self.interpolation_property_inv(prop) return float(prop)
def plot_isotherm(self, T, Pmin=None, Pmax=None, methods_P=[], pts=50, only_valid=True): # pragma: no cover r'''Method to create a plot of the property vs pressure at a specified temperature according to either a specified list of methods, or the user methods (if set), or all methods. User-selectable number of points, and pressure range. If only_valid is set, `test_method_validity_P` will be used to check if each condition in the specified range is valid, and `test_property_validity` will be used to test the answer, and the method is allowed to fail; only the valid points will be plotted. Otherwise, the result will be calculated and displayed as-is. This will not suceed if the method fails. Parameters ---------- T : float Temperature at which to create the plot, [K] Pmin : float Minimum pressure, to begin calculating the property, [Pa] Pmax : float Maximum pressure, to stop calculating the property, [Pa] methods_P : list, optional List of methods to consider pts : int, optional A list of points to calculate the property at; if Pmin to Pmax covers a wide range of method validities, only a few points may end up calculated for a given method so this may need to be large only_valid : bool If True, only plot successful methods and calculated properties, and handle errors; if False, attempt calculation without any checking and use methods outside their bounds ''' # This function cannot be tested if not has_matplotlib: raise Exception('Optional dependency matplotlib is required for plotting') if Pmin is None: if self.Pmin is not None: Pmin = self.Pmin else: raise Exception('Minimum pressure could not be auto-detected; please provide it') if Pmax is None: if self.Pmax is not None: Pmax = self.Pmax else: raise Exception('Maximum pressure could not be auto-detected; please provide it') if not methods_P: if self.user_methods_P: methods_P = self.user_methods_P else: methods_P = self.all_methods_P Ps = np.linspace(Pmin, Pmax, pts) for method_P in methods_P: if only_valid: properties, Ps2 = [], [] for P in Ps: if self.test_method_validity_P(T, P, method_P): try: p = self.calculate_P(T, P, method_P) if self.test_property_validity(p): properties.append(p) Ps2.append(P) except: pass plt.plot(Ps2, properties, label=method_P) else: properties = [self.calculate_P(T, P, method_P) for P in Ps] plt.plot(Ps, properties, label=method_P) plt.legend(loc='best') plt.ylabel(self.name + ', ' + self.units) plt.xlabel('Pressure, Pa') plt.title(self.name + ' of ' + self.CASRN) plt.show()
def plot_isobar(self, P, Tmin=None, Tmax=None, methods_P=[], pts=50, only_valid=True): # pragma: no cover r'''Method to create a plot of the property vs temperature at a specific pressure according to either a specified list of methods, or user methods (if set), or all methods. User-selectable number of points, and temperature range. If only_valid is set,`test_method_validity_P` will be used to check if each condition in the specified range is valid, and `test_property_validity` will be used to test the answer, and the method is allowed to fail; only the valid points will be plotted. Otherwise, the result will be calculated and displayed as-is. This will not suceed if the method fails. Parameters ---------- P : float Pressure for the isobar, [Pa] Tmin : float Minimum temperature, to begin calculating the property, [K] Tmax : float Maximum temperature, to stop calculating the property, [K] methods_P : list, optional List of methods to consider pts : int, optional A list of points to calculate the property at; if Tmin to Tmax covers a wide range of method validities, only a few points may end up calculated for a given method so this may need to be large only_valid : bool If True, only plot successful methods and calculated properties, and handle errors; if False, attempt calculation without any checking and use methods outside their bounds ''' if not has_matplotlib: raise Exception('Optional dependency matplotlib is required for plotting') if Tmin is None: if self.Tmin is not None: Tmin = self.Tmin else: raise Exception('Minimum pressure could not be auto-detected; please provide it') if Tmax is None: if self.Tmax is not None: Tmax = self.Tmax else: raise Exception('Maximum pressure could not be auto-detected; please provide it') if not methods_P: if self.user_methods_P: methods_P = self.user_methods_P else: methods_P = self.all_methods_P Ts = np.linspace(Tmin, Tmax, pts) for method_P in methods_P: if only_valid: properties, Ts2 = [], [] for T in Ts: if self.test_method_validity_P(T, P, method_P): try: p = self.calculate_P(T, P, method_P) if self.test_property_validity(p): properties.append(p) Ts2.append(T) except: pass plt.plot(Ts2, properties, label=method_P) else: properties = [self.calculate_P(T, P, method_P) for T in Ts] plt.plot(Ts, properties, label=method_P) plt.legend(loc='best') plt.ylabel(self.name + ', ' + self.units) plt.xlabel('Temperature, K') plt.title(self.name + ' of ' + self.CASRN) plt.show()
def plot_TP_dependent_property(self, Tmin=None, Tmax=None, Pmin=None, Pmax=None, methods_P=[], pts=15, only_valid=True): # pragma: no cover r'''Method to create a plot of the property vs temperature and pressure according to either a specified list of methods, or user methods (if set), or all methods. User-selectable number of points for each variable. If only_valid is set,`test_method_validity_P` will be used to check if each condition in the specified range is valid, and `test_property_validity` will be used to test the answer, and the method is allowed to fail; only the valid points will be plotted. Otherwise, the result will be calculated and displayed as-is. This will not suceed if the any method fails for any point. Parameters ---------- Tmin : float Minimum temperature, to begin calculating the property, [K] Tmax : float Maximum temperature, to stop calculating the property, [K] Pmin : float Minimum pressure, to begin calculating the property, [Pa] Pmax : float Maximum pressure, to stop calculating the property, [Pa] methods_P : list, optional List of methods to consider pts : int, optional A list of points to calculate the property at for both temperature and pressure; pts^2 points will be calculated. only_valid : bool If True, only plot successful methods and calculated properties, and handle errors; if False, attempt calculation without any checking and use methods outside their bounds ''' if not has_matplotlib: raise Exception('Optional dependency matplotlib is required for plotting') from mpl_toolkits.mplot3d import axes3d from matplotlib.ticker import FormatStrFormatter import numpy.ma as ma if Pmin is None: if self.Pmin is not None: Pmin = self.Pmin else: raise Exception('Minimum pressure could not be auto-detected; please provide it') if Pmax is None: if self.Pmax is not None: Pmax = self.Pmax else: raise Exception('Maximum pressure could not be auto-detected; please provide it') if Tmin is None: if self.Tmin is not None: Tmin = self.Tmin else: raise Exception('Minimum pressure could not be auto-detected; please provide it') if Tmax is None: if self.Tmax is not None: Tmax = self.Tmax else: raise Exception('Maximum pressure could not be auto-detected; please provide it') if not methods_P: methods_P = self.user_methods_P if self.user_methods_P else self.all_methods_P Ps = np.linspace(Pmin, Pmax, pts) Ts = np.linspace(Tmin, Tmax, pts) Ts_mesh, Ps_mesh = np.meshgrid(Ts, Ps) fig = plt.figure() ax = fig.gca(projection='3d') handles = [] for method_P in methods_P: if only_valid: properties = [] for T in Ts: T_props = [] for P in Ps: if self.test_method_validity_P(T, P, method_P): try: p = self.calculate_P(T, P, method_P) if self.test_property_validity(p): T_props.append(p) else: T_props.append(None) except: T_props.append(None) else: T_props.append(None) properties.append(T_props) properties = ma.masked_invalid(np.array(properties, dtype=np.float).T) handles.append(ax.plot_surface(Ts_mesh, Ps_mesh, properties, cstride=1, rstride=1, alpha=0.5)) else: properties = [[self.calculate_P(T, P, method_P) for P in Ps] for T in Ts] handles.append(ax.plot_surface(Ts_mesh, Ps_mesh, properties, cstride=1, rstride=1, alpha=0.5)) ax.yaxis.set_major_formatter(FormatStrFormatter('%.4g')) ax.zaxis.set_major_formatter(FormatStrFormatter('%.4g')) ax.xaxis.set_major_formatter(FormatStrFormatter('%.4g')) ax.set_xlabel('Temperature, K') ax.set_ylabel('Pressure, Pa') ax.set_zlabel(self.name + ', ' + self.units) plt.title(self.name + ' of ' + self.CASRN) plt.show(block=False) # The below is a workaround for a matplotlib bug ax.legend(handles, methods_P) plt.show(block=False)
def calculate_derivative_T(self, T, P, method, order=1): r'''Method to calculate a derivative of a temperature and pressure dependent property with respect to temperature at constant pressure, of a given order using a specified method. Uses SciPy's derivative function, with a delta of 1E-6 K and a number of points equal to 2*order + 1. This method can be overwritten by subclasses who may perfer to add analytical methods for some or all methods as this is much faster. If the calculation does not succeed, returns the actual error encountered. Parameters ---------- T : float Temperature at which to calculate the derivative, [K] P : float Pressure at which to calculate the derivative, [Pa] method : str Method for which to find the derivative order : int Order of the derivative, >= 1 Returns ------- d_prop_d_T_at_P : float Calculated derivative property at constant pressure, [`units/K^order`] ''' return derivative(self.calculate_P, T, dx=1e-6, args=[P, method], n=order, order=1+order*2)
def calculate_derivative_P(self, P, T, method, order=1): r'''Method to calculate a derivative of a temperature and pressure dependent property with respect to pressure at constant temperature, of a given order using a specified method. Uses SciPy's derivative function, with a delta of 0.01 Pa and a number of points equal to 2*order + 1. This method can be overwritten by subclasses who may perfer to add analytical methods for some or all methods as this is much faster. If the calculation does not succeed, returns the actual error encountered. Parameters ---------- P : float Pressure at which to calculate the derivative, [Pa] T : float Temperature at which to calculate the derivative, [K] method : str Method for which to find the derivative order : int Order of the derivative, >= 1 Returns ------- d_prop_d_P_at_T : float Calculated derivative property at constant temperature, [`units/Pa^order`] ''' f = lambda P: self.calculate_P(T, P, method) return derivative(f, P, dx=1e-2, n=order, order=1+order*2)
def TP_dependent_property_derivative_T(self, T, P, order=1): r'''Method to calculate a derivative of a temperature and pressure dependent property with respect to temperature at constant pressure, of a given order. Methods found valid by `select_valid_methods_P` are attempted until a method succeeds. If no methods are valid and succeed, None is returned. Calls `calculate_derivative_T` internally to perform the actual calculation. .. math:: \text{derivative} = \frac{d (\text{property})}{d T}|_{P} Parameters ---------- T : float Temperature at which to calculate the derivative, [K] P : float Pressure at which to calculate the derivative, [Pa] order : int Order of the derivative, >= 1 Returns ------- d_prop_d_T_at_P : float Calculated derivative property, [`units/K^order`] ''' sorted_valid_methods_P = self.select_valid_methods_P(T, P) for method in sorted_valid_methods_P: try: return self.calculate_derivative_T(T, P, method, order) except: pass return None
def TP_dependent_property_derivative_P(self, T, P, order=1): r'''Method to calculate a derivative of a temperature and pressure dependent property with respect to pressure at constant temperature, of a given order. Methods found valid by `select_valid_methods_P` are attempted until a method succeeds. If no methods are valid and succeed, None is returned. Calls `calculate_derivative_P` internally to perform the actual calculation. .. math:: \text{derivative} = \frac{d (\text{property})}{d P}|_{T} Parameters ---------- T : float Temperature at which to calculate the derivative, [K] P : float Pressure at which to calculate the derivative, [Pa] order : int Order of the derivative, >= 1 Returns ------- d_prop_d_P_at_T : float Calculated derivative property, [`units/Pa^order`] ''' sorted_valid_methods_P = self.select_valid_methods_P(T, P) for method in sorted_valid_methods_P: try: return self.calculate_derivative_P(P, T, method, order) except: pass return None
def set_user_method(self, user_methods, forced=False): r'''Method to set the T, P, and composition dependent property methods desired for consideration by the user. Can be used to exclude certain methods which might have unacceptable accuracy. As a side effect, the previously selected method is removed when this method is called to ensure user methods are tried in the desired order. Parameters ---------- user_methods : str or list Methods by name to be considered for calculation of the mixture property, ordered by preference. forced : bool, optional If True, only the user specified methods will ever be considered; if False, other methods will be considered if no user methods suceed. ''' # Accept either a string or a list of methods, and whether # or not to only consider the false methods if isinstance(user_methods, str): user_methods = [user_methods] # The user's order matters and is retained for use by select_valid_methods self.user_methods = user_methods self.forced = forced # Validate that the user's specified methods are actual methods if set(self.user_methods).difference(self.all_methods): raise Exception("One of the given methods is not available for this mixture") if not self.user_methods and self.forced: raise Exception('Only user specified methods are considered when forced is True, but no methods were provided') # Remove previously selected methods self.method = None self.sorted_valid_methods = [] self.TP_zs_ws_cached = (None, None, None, None)
def mixture_property(self, T, P, zs, ws): r'''Method to calculate the property with sanity checking and without specifying a specific method. `select_valid_methods` is used to obtain a sorted list of methods to try. Methods are then tried in order until one succeeds. The methods are allowed to fail, and their results are checked with `test_property_validity`. On success, the used method is stored in the variable `method`. If `method` is set, this method is first checked for validity with `test_method_validity` for the specified temperature, and if it is valid, it is then used to calculate the property. The result is checked for validity, and returned if it is valid. If either of the checks fail, the function retrieves a full list of valid methods with `select_valid_methods` and attempts them as described above. If no methods are found which succeed, returns None. Parameters ---------- T : float Temperature at which to calculate the property, [K] P : float Pressure at which to calculate the property, [Pa] zs : list[float] Mole fractions of all species in the mixture, [-] ws : list[float] Weight fractions of all species in the mixture, [-] Returns ------- prop : float Calculated property, [`units`] ''' # Optimistic track, with the already set method if self.method: # retest within range if self.test_method_validity(T, P, zs, ws, self.method): try: prop = self.calculate(T, P, zs, ws, self.method) if self.test_property_validity(prop): return prop except: # pragma: no cover pass # get valid methods at conditions, and try them until one yields a valid # property; store the method and return the answer self.sorted_valid_methods = self.select_valid_methods(T, P, zs, ws) for method in self.sorted_valid_methods: try: prop = self.calculate(T, P, zs, ws, method) if self.test_property_validity(prop): self.method = method return prop except: # pragma: no cover pass # Function returns None if it does not work. return None