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))
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
print('mole balance vector =\n', s_mtrx.sum(1))
Step by step
Solved in 2 steps