PYTHON/ Computational stoichiometry Make an organized output of the species and stoichiometric data for the reaction series : r0 :  C(s) -> C(g)r1 :  2 C(g) + O2 -> 2 COr2 :  CO + 0.5 O2 -> CO2r3 :  C(g) + H2O -> CO + H2r4 :  CH4 <=> C(g) + 2 H2 A sample code has been provided to guide your results, please follow as closely as you can to obtain the right answer. '''Create the species list''' species_tmp = list()  # temporary list for species for r in reactions:        left  = r.split('<=>')[0].strip() # reactants side    right = r.split('<=>')[1].strip() # products side        left_terms  = left.split('+')   # reactant species w/ stoichiometric coeff.    right_terms = right.split('+')  # product  species w/ stoichiometric coeff.        terms = [t.strip() for t in left_terms] + [t.strip() for t in right_terms] # concatenate list comprehensions        for i in terms:                tmp = i.split(' ') # split stoichiometric coefficient from species name                assert len(tmp)==1 or len(tmp)==2,' terms = %r, i = %r, tmp = %r '%(terms, i, tmp)                if len(tmp) == 2:            species_tmp.append(tmp[1].strip()) # species name if there is a stoichiometric coeff.        else:            species_tmp.append(i.strip()) # species name if there is no stoichiometric coeff. species_filtered = set(species_tmp) # filter species as a set species = list(species_filtered)  # convert species set to list print('\nspecies =\n',species)print('# of species =',len(species)) '''Create the stoichiometric matrix''' import numpy as np # Initialize the stoichiometric matrix as zeros_mtrx = np.zeros((len(reactions), len(species))) for (i_row, r) in enumerate(reactions):        left  = r.split('<=>')[0].strip()    right = r.split('<=>')[1].strip()        left_terms = left.split('+')    left_terms = [t.strip() for t in left_terms] # in-place clean up        right_terms = right.split('+')    right_terms = [t.strip() for t in right_terms] # in-place clean up        for t in left_terms: # reactants                tmp = t.split(' ') # split stoichiometric coeff and species name                if len(tmp) == 2: # stoich coeff and species name            coeff = float(tmp[0].strip())            species_member = tmp[1].strip()            j_col = species.index(species_member) # find id of species in the species list             assert s_mtrx[i_row,j_col] == 0.0, \                   'duplicates not allowed r%r: %r %r r'%\                   (i_row,r,species_member,s_mtrx[i_row,j_col])                        s_mtrx[i_row,j_col] = -1.0 * coeff      else: # only species name            species_member = tmp[0].strip()            j_col = species.index(species_member)                        assert s_mtrx[i_row,j_col] == 0.0, \                   'duplicates not allowed r%r: %r %r r'%\                   (i_row,r,species_member,s_mtrx[i_row,j_col])                        s_mtrx[i_row,j_col] = -1.0   for t in right_terms: # products                tmp = t.split(' ')                if len(tmp) == 2:            coeff = float(tmp[0].strip())            species_member = tmp[1].strip()            j_col = species.index(species_member)                        assert s_mtrx[i_row,j_col] == 0.0, \                   'duplicates not allowed r%r: %r %r r'%\                   (i_row,r,species_member,s_mtrx[i_row,j_col])                        s_mtrx[i_row,j_col] = 1.0 * coeff        else:            species_member = tmp[0].strip()            j_col = species.index(species_member)                        assert s_mtrx[i_row,j_col] == 0.0, \                   'duplicates not allowed r%r: %r %r r'%\                   (i_row,r,species_member,s_mtrx[i_row,j_col])            s_mtrx[i_row,j_col] = 1.0 print('m x n =',s_mtrx.shape)print('s_mtrx =\n',s_mtrx)print('')print('mole balance vector =\n', s_mtrx@np.ones(len(species)))print('mole balance vector =\n', s_mtrx.sum(1))

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

PYTHON/ Computational stoichiometry

Make an organized output of the species and stoichiometric data for the reaction series :

r0 :  C(s) -> C(g)
r1 :  2 C(g) + O2 -> 2 CO
r2 :  CO + 0.5 O2 -> CO2
r3 :  C(g) + H2O -> CO + H2
r4 :  CH4 <=> C(g) + 2 H2

A sample code has been provided to guide your results, please follow as closely as you can to obtain the right answer.

'''Create the species list'''

species_tmp = list()  # temporary list for species

for r in reactions:
    
    left  = r.split('<=>')[0].strip() # reactants side
    right = r.split('<=>')[1].strip() # products side
    
    left_terms  = left.split('+')   # reactant species w/ stoichiometric coeff.
    right_terms = right.split('+')  # product  species w/ stoichiometric coeff.
    
    terms = [t.strip() for t in left_terms] + [t.strip() for t in right_terms] # concatenate list comprehensions
    
    for i in terms:
        
        tmp = i.split(' ') # split stoichiometric coefficient from species name
        
        assert len(tmp)==1 or len(tmp)==2,' terms = %r, i = %r, tmp = %r '%(terms, i, tmp)
        
        if len(tmp) == 2:
            species_tmp.append(tmp[1].strip()) # species name if there is a stoichiometric coeff.
        else:
            species_tmp.append(i.strip()) # species name if there is no stoichiometric coeff.

species_filtered = set(species_tmp) # filter species as a set

species = list(species_filtered)  # convert species set to list 
print('\nspecies =\n',species)
print('# of species =',len(species))

'''Create the stoichiometric matrix'''

import numpy as np

# Initialize the stoichiometric matrix as zero
s_mtrx = np.zeros((len(reactions), len(species)))

for (i_row, r) in enumerate(reactions):
    
    left  = r.split('<=>')[0].strip()
    right = r.split('<=>')[1].strip()
    
    left_terms = left.split('+')
    left_terms = [t.strip() for t in left_terms] # in-place clean up
    
    right_terms = right.split('+')
    right_terms = [t.strip() for t in right_terms] # in-place clean up
    
    for t in left_terms: # reactants
        
        tmp = t.split(' ') # split stoichiometric coeff and species name
        
        if len(tmp) == 2: # stoich coeff and species name
            coeff = float(tmp[0].strip())
            species_member = tmp[1].strip()
            j_col = species.index(species_member) # find id of species in the species list

            assert s_mtrx[i_row,j_col] == 0.0, \
                   'duplicates not allowed r%r: %r %r r'%\
                   (i_row,r,species_member,s_mtrx[i_row,j_col])
            
            s_mtrx[i_row,j_col] = -1.0 * coeff

     else: # only species name
            species_member = tmp[0].strip()
            j_col = species.index(species_member)
            
            assert s_mtrx[i_row,j_col] == 0.0, \
                   'duplicates not allowed r%r: %r %r r'%\
                   (i_row,r,species_member,s_mtrx[i_row,j_col])
            
            s_mtrx[i_row,j_col] = -1.0

  for t in right_terms: # products
        
        tmp = t.split(' ')
        
        if len(tmp) == 2:
            coeff = float(tmp[0].strip())
            species_member = tmp[1].strip()
            j_col = species.index(species_member)
            
            assert s_mtrx[i_row,j_col] == 0.0, \
                   'duplicates not allowed r%r: %r %r r'%\
                   (i_row,r,species_member,s_mtrx[i_row,j_col])
            
            s_mtrx[i_row,j_col] = 1.0 * coeff

       else:
            species_member = tmp[0].strip()
            j_col = species.index(species_member)
            
            assert s_mtrx[i_row,j_col] == 0.0, \
                   'duplicates not allowed r%r: %r %r r'%\
                   (i_row,r,species_member,s_mtrx[i_row,j_col])
            s_mtrx[i_row,j_col] = 1.0

print('m x n =',s_mtrx.shape)
print('s_mtrx =\n',s_mtrx)
print('')
print('mole balance vector =\n', s_mtrx@np.ones(len(species)))
print('mole balance vector =\n', s_mtrx.sum(1))

 

 

 

 

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Use of XOR function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education