Skip to content

Commit 6851783

Browse files
authored
Merge pull request #165 from sudarshanv01/bugfix_numbers_solver
BUGFIX: Numbers solver
2 parents 67ea2b6 + 57daa22 commit 6851783

File tree

7 files changed

+286
-256
lines changed

7 files changed

+286
-256
lines changed

catmap/mappers/min_resid_mapper.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,6 @@ def minresid_iteration(isMapped):
593593
[resid,sol_pt,cvg])
594594

595595
checked_dirs[k] = 1
596-
597596
if sol_cvgs:
598597
self._coverage = sol_cvgs
599598
if self.use_numbers_solver:

catmap/solvers/integrated_rate_control_solver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ def get_integrated_DRC_rate(self,params,*args,**kwargs):
4545
for dname in self.descriptor_names:
4646
Ef = self.species_definitions[dname]['formation_energy'][ref_idx]
4747
desc_energies.append(Ef)
48-
48+
4949
ref_dict = self.scaler.get_free_energies(desc_energies)
5050
self.reference_free_energies = ref_dict
5151
self._static_rate_controls = DRCs
5252
self._initialized = True
53-
53+
5454
Gs = {}
5555
for key,G in zip(self.adsorbate_names + self.transition_state_names, params):
5656
Gs[key] = G

catmap/solvers/mean_field_solver.py

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self,reaction_model=None):
2020
"stagnated or diverging (residual = ${resid})."+\
2121
" Assuming Jacobian is 0.",
2222
}
23-
# Create a log file of the solver
23+
# Create a log file of the solver
2424
self.outer_solver_log = 'outer_solver.log'
2525

2626
def get_rxn_rates(self,coverages,rate_constants):
@@ -110,7 +110,7 @@ def get_selectivity(self,rxn_parameters,weights=None):
110110
tofs = self.get_turnover_frequency(rxn_parameters)
111111
if weights is None:
112112
weights = [1]*len(tofs) #use for weighted selectivity (e.g. % carbon)
113-
113+
114114
if self.products is None:
115115
self.products = [g for g,r in zip(self.gas_names,tofs) if r >0]
116116
if self.reactants is None:
@@ -241,7 +241,7 @@ def get_rxn_order(self,rxn_parameters,epsilon=1e-10):
241241
dP = (new_p[i] - current_Ps[i])/current_Ps[i]
242242
DRC_i.append(float(dTOF/dP))
243243
DRC.append(DRC_i)
244-
self.gas_pressures = current_Ps
244+
self.gas_pressures = current_Ps
245245
self._rxn_order = DRC
246246
return DRC
247247

@@ -299,12 +299,12 @@ def rate_equation_term(self,species_list,rate_string,d_wrt=None,specific_ads_nam
299299
adsorbate_names = specific_ads_names
300300
else:
301301
adsorbate_names = self.adsorbate_names
302-
302+
303303
# Separate the species_list into gas and sites
304304
gas_idxs = [self.gas_names.index(gas)
305305
for gas in species_list if gas in self.gas_names]
306306
#allows for multiple site types
307-
sites = [s for s in species_list if s in self.site_names]
307+
sites = [s for s in species_list if s in self.site_names]
308308

309309
# Create an adsorbate list by making sure that the adsorbates do not
310310
# include elements already present in the sites list.
@@ -314,9 +314,9 @@ def rate_equation_term(self,species_list,rate_string,d_wrt=None,specific_ads_nam
314314
ads_idxs.append(adsorbate_names.index(ads))
315315
if len(gas_idxs+ads_idxs+sites) != len(species_list):
316316
raise ValueError('Undefined species in '+','.join(species_list))
317-
317+
318318
if not d_wrt:
319-
# If the derivative is not required, simply
319+
# If the derivative is not required, simply
320320
# return the rate string based on the pressures and coverages
321321
# of the relevant species
322322
for iden in gas_idxs:
@@ -346,17 +346,17 @@ def rate_equation_term(self,species_list,rate_string,d_wrt=None,specific_ads_nam
346346
# Also get the site in which the adsorbate is present
347347
d_site = self.species_definitions[d_wrt]['site']
348348

349-
if (d_wrt not in sites # Not differentiating with respect to a site
350-
and d_idx in ads_idxs # Differentiating with respect to an adsorbate
351-
and d_site not in sites # Site not present in the rate expression
352-
and self.fix_x_star == True): # Fixing x_star
349+
if (d_wrt not in sites # Not differentiating with respect to a site
350+
and d_idx in ads_idxs # Differentiating with respect to an adsorbate
351+
and d_site not in sites # Site not present in the rate expression
352+
and (self.use_numbers_solver == False or self.fix_x_star == True)): # Fixing x_star
353353
# Scenario 1: Differentiating with respect to an adsorbate
354-
# but there is no site term in the rate equation.
354+
# but there is no site term in the rate equation.
355355
multiplier = ads_idxs.count(d_idx) #get order
356356
ads_idxs.remove(d_idx) #reduce order by 1
357357
if multiplier != 1:
358358
rate_string = str(multiplier)+'*'+rate_string
359-
359+
360360
elif (len(ads_idxs)>0 # Adsorbate is present in the rate expression
361361
and d_site not in sites # Site not present in the rate expression
362362
and d_wrt in self.site_names # Differentiating with respect to a site
@@ -367,7 +367,7 @@ def rate_equation_term(self,species_list,rate_string,d_wrt=None,specific_ads_nam
367367
# of each adsorbate with respect to the site.
368368
temp_rate_string = ''
369369
for ie, ads_idx in enumerate(ads_idxs):
370-
# get the other adsorbates by getting
370+
# get the other adsorbates by getting
371371
# all other index of ads_idxs expect ie
372372
other_ads_idxs = []
373373
for je in range(len(ads_idxs)):
@@ -376,7 +376,7 @@ def rate_equation_term(self,species_list,rate_string,d_wrt=None,specific_ads_nam
376376
# the rate string will just be the coverage of
377377
# the other adsorbates multiplied to each other
378378
# multiplied by a -1 because the derivative with
379-
# respect to the site is negative of that of the
379+
# respect to the site is negative of that of the
380380
# adsorbate.
381381
if other_ads_idxs:
382382
temp_rate_string += '*'.join(['theta['+str(i)+']' for i in other_ads_idxs])
@@ -387,26 +387,26 @@ def rate_equation_term(self,species_list,rate_string,d_wrt=None,specific_ads_nam
387387
rate_string += '*-1*('+temp_rate_string + ')'
388388
ads_idxs = [] #no more adsorbates
389389

390-
elif ( d_site in sites # Site present in the rate expression
391-
and d_idx not in ads_idxs # Adsorbates not present in the rate expression
392-
and self.fix_x_star == True): # Fixing x_star
393-
# Scenario 3: There is an empty site in
390+
elif ( d_site in sites # Site present in the rate expression
391+
and d_idx not in ads_idxs # Adsorbates not present in the rate expression
392+
and (self.use_numbers_solver == False or self.fix_x_star == True)): # Fixing x_star
393+
# Scenario 3: There is an empty site in
394394
# the rate equation but there is no adsorbate term
395395
multiplier = sites.count(d_site)
396-
# Account for the fact that d/dsite does not have
396+
# Account for the fact that d/dsite does not have
397397
# a -1 pre-multiplier while d/dads does
398398
if d_wrt not in self.site_names:
399-
multiplier = -1 * multiplier # dsite/dtheta_* is negative
399+
multiplier = -1 * multiplier # dsite/dtheta_* is negative
400400
sites.remove(d_site) # reduce the order of site by 1
401401
rate_string = str(multiplier)+'*'+rate_string
402402

403-
elif ( d_site in sites # Site present in the rate expression
403+
elif ( d_site in sites # Site present in the rate expression
404404
and d_idx in ads_idxs # Adsorbates present in the rate expression
405405
and self.fix_x_star == True): # Fixing x_star
406406
# Scenario 4: Both an empty site and adsorbate in the rate equation
407407
# need to use chain rule.
408408
ads_mult = ads_idxs.count(d_idx)
409-
# Account for the fact that d/dsite doesnt have
409+
# Account for the fact that d/dsite doesnt have
410410
# a -1 pre-multiplier while d/dads does
411411
if d_wrt not in self.site_names:
412412
site_mult = -1 * sites.count(d_site) #negative 1 to account for d_site/d_ads
@@ -438,18 +438,18 @@ def rate_equation_term(self,species_list,rate_string,d_wrt=None,specific_ads_nam
438438
[ads_str]*(ads_mult-1))+ ')'
439439
rate_string += '*'+mult_rule
440440

441-
elif self.fix_x_star == False:
442-
# Scenario 5: If x_star is not fixed, then
443-
# the partial derivatives are to be taken
444-
# independently for each site and adsorbate
441+
elif (self.use_numbers_solver == True and self.fix_x_star == False):
442+
# Scenario 5: If x_star is not fixed, then
443+
# the partial derivatives are to be taken
444+
# independently for each site and adsorbate
445445
if d_idx in ads_idxs and d_wrt not in self.site_names:
446-
# Taking a derivative with respect to an
446+
# Taking a derivative with respect to an
447447
# adsorbate coverage
448448
multiplier = ads_idxs.count(d_idx) #get order
449449
ads_idxs.remove(d_idx) #reduce order by 1
450450
if multiplier != 1:
451451
rate_string = str(multiplier)+'*'+rate_string
452-
elif d_wrt in sites:
452+
elif d_wrt in sites:
453453
# Taking the derivative with respect to a site
454454
multiplier = sites.count(d_site)
455455
sites.remove(d_site) #reduce order by 1
@@ -463,9 +463,9 @@ def rate_equation_term(self,species_list,rate_string,d_wrt=None,specific_ads_nam
463463
else:
464464
# No dependence on either the adsorbate or the site
465465
return '0'
466-
467-
# Create the rate string for the derivative by taking into
468-
# account the derivative done by string manipulation in the
466+
467+
# Create the rate string for the derivative by taking into
468+
# account the derivative done by string manipulation in the
469469
# previous step
470470
for iden in gas_idxs:
471471
rate_string += '*p['+str(iden)+']'
@@ -577,7 +577,7 @@ def rate_equations(self):
577577

578578
if self.use_numbers_solver:
579579
# In case we use the numbers solver, the steady state
580-
# equations needs to be n+1 dimensional
580+
# equations needs to be n+1 dimensional
581581
adsorbate_names = self.adsorbate_names + self.site_names
582582
# Remove 'g' if adsorbate_names if present
583583
if 'g' in adsorbate_names:
@@ -609,7 +609,7 @@ def rate_equations(self):
609609
surface_names = list(surface_names)
610610
surface_names.remove('g')
611611
surface_names = tuple(surface_names)
612-
species_iterate = self.adsorbate_names + surface_names
612+
species_iterate = self.adsorbate_names + surface_names
613613
else:
614614
species_iterate = self.adsorbate_names
615615

@@ -630,7 +630,7 @@ def rate_equations(self):
630630
dcdt_strings.append(dcdt_str)
631631

632632
all_strings = rate_strings + dcdt_strings
633-
633+
634634
return all_strings
635635

636636
def jacobian_equations(self,adsorbate_interactions=True):
@@ -683,7 +683,7 @@ def jacobian_equations(self,adsorbate_interactions=True):
683683
# Standard CatMAP solver needs only an nxn Jacobian matrix
684684
adsorbate_names = self.adsorbate_names
685685

686-
if self.DEBUG:
686+
if self.DEBUG:
687687
with open('adsorbate_names.log','w') as f:
688688
f.write(str(adsorbate_names))
689689

@@ -693,10 +693,10 @@ def jacobian_equations(self,adsorbate_interactions=True):
693693
for i, ads_i in enumerate(adsorbate_names):
694694
# The second index is for the column
695695
for j, ads_j in enumerate(adsorbate_names):
696-
# Populate the Jacobian matrix based on the
696+
# Populate the Jacobian matrix based on the
697697
# required dimensions
698698
J_str = 'J['+str(i)+']['+str(j)+'] = 0'
699-
# Iterate over the elementary reactions such that
699+
# Iterate over the elementary reactions such that
700700
# we get how much of the species are present
701701
for k, rxn in enumerate(self.elementary_rxns):
702702
# Get the number of ads_i in the forward and reverse
@@ -705,7 +705,7 @@ def jacobian_equations(self,adsorbate_interactions=True):
705705
rxnCounts = [-1.0*rxn[0].count(ads_i),
706706
1.0*rxn[-1].count(ads_i)]
707707
rxnOrder = [o for o in rxnCounts if o]
708-
# In the following, the drdx term is constructed
708+
# In the following, the drdx term is constructed
709709
# and then multiplied by the reaction order to get
710710
# the df/dx term.
711711
if rxnOrder:
@@ -714,7 +714,7 @@ def jacobian_equations(self,adsorbate_interactions=True):
714714
rRate_string = self.rate_equation_term(rxn[-1], 'kr['+str(k)+']', ads_j, adsorbate_names)
715715
if adsorbate_interactions == True:
716716
if ads_j not in self.site_names:
717-
# If one of the "adsorbates" is an empty site then
717+
# If one of the "adsorbates" is an empty site then
718718
# there should be no inclusion of adsorbate-adsorbate interaction
719719
dfRate_string = self.rate_equation_term(rxn[0],'(kfkBT['+str(k)+'])*dEf['+str(k)+']['+str(j)+']')
720720
drRate_string = self.rate_equation_term(rxn[-1],'(krkBT['+str(k)+'])*dEr['+str(k)+']['+str(j)+']')

0 commit comments

Comments
 (0)