Example #1
0
def test_mix_b():
    mu_h2 = cm.mu_gas('H2', 773.15)
    mu_n2 = cm.mu_gas('N2', 773.15)
    mw_h2 = cm.mw('H2')
    mw_n2 = cm.mw('N2')
    mu_mix = cm.mu_herning([mu_h2, mu_n2], [mw_h2, mw_n2], [0.85, 0.15])
    assert mu_mix == approx(252.81, rel=1e-2)
Example #2
0
def test_mix_b():
    mw_h2 = cm.mw('H2')
    mw_n2 = cm.mw('N2')
    mw_ch4 = cm.mw('CH4')
    mw = cm.mw_mix([mw_h2, mw_n2, mw_ch4], [0.4, 0.1, 0.5])
    assert mw == approx(11.6293, rel=1e-2)
Example #3
0
def test_mix_a():
    mw_h2 = cm.mw('H2')
    mw_n2 = cm.mw('N2')
    mw = cm.mw_mix([mw_h2, mw_n2], [0.8, 0.2])
    assert mw == approx(7.2156, rel=1e-2)
Example #4
0
def test_ammonium_sulfate():
    mw = cm.mw('(NH4)2SO4')
    assert mw == approx(132.13, rel=1e-2)
Example #5
0
def test_methane():
    mw = cm.mw('CH4')
    assert mw == approx(16.04, rel=1e-2)
Example #6
0
def test_carbon():
    mw = cm.mw('C')
    assert mw == 12.011
Example #7
0
"""
Determine molecular weight of an element, compound, or gas mixture.
"""

import chemics as cm

formula = 'C'
mw = cm.mw(formula)
print(f'Molecular weight of {formula} = {mw:.2f} g/mol')

formula = 'Co'
mw = cm.mw(formula)
print(f'Molecular weight of {formula} = {mw:.2f} g/mol')

formula = 'CH4'
mw = cm.mw(formula)
print(f'Molecular weight of {formula} = {mw:.2f} g/mol')

formula = '(CO2)2'
mw = cm.mw(formula)
print(f'Molecular weight of {formula} = {mw:.2f} g/mol')

formula = 'Ca(C2H3O2)2'
mw = cm.mw(formula)
print(f'Molecular weight of {formula} = {mw:.2f} g/mol')

formula = '(NH4)2SO4'
mw = cm.mw(formula)
print(f'Molecular weight of {formula} = {mw:.2f} g/mol')

formula = '(NH4)(NO3)'
Example #8
0
    def _eq_properties(self, eq_items):
        """
        Determine properties for each item in reactants or products.
        """
        eq_names = []
        eq_moles = []
        eq_species = []
        eq_molwts = []
        eq_masses = []
        eq_elements = Counter()

        for item in eq_items:

            # number of moles and name for each item
            if item[0].isdigit():
                item = item.split()
                mol = float(item[0])
                name = item[1]
                eq_names.append(name)
                eq_moles.append(mol)
            else:
                mol = 1.0
                name = item
                eq_names.append(name)
                eq_moles.append(mol)

            # species for each item is chemical formula specified in names
            # dictonary or as chemical formula (name) in the chemical equation
            # note - species is used for the molecular weight and elements
            if (self.names is not None) and (name in self.names.keys()):
                sp = self.names[name]
                eq_species.append(sp)
            else:
                sp = name
                eq_species.append(sp)

            # masses and molecular weights for each item
            mw = cm.mw(sp)
            mass = mol * mw
            eq_molwts.append(cm.mw(sp))
            eq_masses.append(mass)

            # count elements from each chemical species
            rex = re.findall('([A-Z][a-z]?)([0-9]*)', sp)

            for r in rex:
                element = r[0]

                if r[1] == '':
                    atoms = 1.0
                else:
                    atoms = float(r[1])

                # dict where key is element and value is number of atoms such as {'C': 6.0}
                d = {element: atoms * mol}
                eq_elements.update(d)

        # sum of moles and masses
        eq_sum_moles = sum(eq_moles)
        eq_sum_masses = sum(eq_masses)

        # mole fractions and mass fractions
        eq_mol_fracs = [m / eq_sum_moles for m in eq_moles]
        eq_mass_fracs = [m / eq_sum_masses for m in eq_masses]

        # dataframe for properties
        cols = eq_names
        idx = ['moles', 'species', 'molwt', 'mass', 'molfrac', 'massfrac']

        df = pd.DataFrame(columns=cols, index=idx)
        df.loc['moles'] = eq_moles
        df.loc['species'] = eq_species
        df.loc['molwt'] = eq_molwts
        df.loc['mass'] = eq_masses
        df.loc['molfrac'] = eq_mol_fracs
        df.loc['massfrac'] = eq_mass_fracs

        return df, dict(eq_elements), eq_sum_moles, eq_sum_masses