PYTHON/COMPUTATIONAL STOICHIOMETRY Make an organized output of the species and stoichiometric data for the following reaction series . When running the code i ususally run into a list index out of range error at the highlighted Probnlem area in the code. Please help resolve, Thank you. r0 : C(s) -> C(g)r1 : 2 C(g) + O2 -> 2 COr2 : CO + H2O <-> CO2 + H2r3 : CO + 0.5 O2 -> CO2r4 : CO + 3 H2 -> CH4 + H2Or5 : CH4 + H2O -> CO + 3 H2r6 : C(g) + CO2 -> 2 COr7 : C(g) + H2O -> CO + H2r8 : CH4 <=> C(g) + 2 H2r9 : CO2 + H2 <=> CO + H2O A Sample of the code for the same problem with a different set of reactions has been provided, : (Please use to guide your solution) reactions = [ "C(s) -> C(g)", "2 C(g) + O2 -> 2 CO", "CO + 0.5 O2 -> CO2", "C(g) + H2O -> CO + H2", "CH4 <=> C(g) + 2 H2"]# Create the species listspecies_tmp = list()for r in reactions: left = r.split('<=>')[0].strip() # reactants side right = r.split('<=>')[1].strip() # products side #Problem Area 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 setspecies = list(species_filtered) # convert species set to list print('\nspecies =\n',species)print('# of species =',len(species))# Create the stoichiometric matriximport 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.0print('m x n =',s_mtrx.shape)print('s_mtrx =\n',s_mtrx)print('')print('mole balance vector =\n', s_mtrx.sum(1))
PYTHON/COMPUTATIONAL STOICHIOMETRY
Make an organized output of the species and stoichiometric data for the following reaction series . When running the code i ususally run into a list index out of range error at the highlighted Probnlem area in the code. Please help resolve, Thank you.
r0 : C(s) -> C(g)
r1 : 2 C(g) + O2 -> 2 CO
r2 : CO + H2O <-> CO2 + H2
r3 : CO + 0.5 O2 -> CO2
r4 : CO + 3 H2 -> CH4 + H2O
r5 : CH4 + H2O -> CO + 3 H2
r6 : C(g) + CO2 -> 2 CO
r7 : C(g) + H2O -> CO + H2
r8 : CH4 <=> C(g) + 2 H2
r9 : CO2 + H2 <=> CO + H2O
A Sample of the code for the same problem with a different set of reactions has been provided, :
(Please use to guide your solution)
reactions = [
"C(s) -> C(g)",
"2 C(g) + O2 -> 2 CO",
"CO + 0.5 O2 -> CO2",
"C(g) + H2O -> CO + H2",
"CH4 <=> C(g) + 2 H2"
]
# Create the species list
species_tmp = list()
for r in reactions:
left = r.split('<=>')[0].strip() # reactants side
right = r.split('<=>')[1].strip() # products side #Problem Area
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

Step by step
Solved in 2 steps









