Question: help resolve the error in my code :import numpy as np # Given reaction mechanism ( coefficients are already corrected ) finput = [

help resolve the error in my code :import numpy as np
# Given reaction mechanism (coefficients are already corrected)
finput =["C3H8=> CH3*+ C2H5*","C3H8+ H*=> H2+ C3H7*","C3H7*=> C3H6+ H*",
"C2H5*=> C2H4+ H*","C3H7*=> C2H4+ CH3*","C2H5*+ C3H8=> C2H6+ C3H7*",
"CH3+ C3H8=> CH4+ C3H7*","CH3*+ C3H7*=> C3H6+ CH4"]
reactions =[]
for line in finput:
stripped_line = line.replace('','') #
if '=>' in stripped_line:
left, right = stripped_line.split('=>')
elif '=>' in stripped_line:
left, right = stripped_line.split('=>')
else:
raise ValueError(f"Invalid delimiter in reaction: {line}")
reactions.append((left.strip(), right.strip()))
species_tmp =[]
for left, right in reactions:
left_terms = left.split('+')
right_terms = right.split('+')
terms =[t.strip() for t in left_terms]+[t.strip() for t in right_terms]
for term in terms:
tmp = term.split()
assert len(tmp)==1 or len(tmp)==2, 'terms =%r, term =%r, tmp =%r'%(terms, term, tmp)
if len(tmp)==2:
species_tmp.append(tmp[1].strip())
else:
species_tmp.append(term.strip())
species_filtered = set(species_tmp)
species = list(species_filtered)
# 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('species =', species)
print('# of species =', len(species))
help resolve the error in my code :import numpy

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!