Skip to content

Commit

Permalink
Merge pull request #12 from mieand/scaling
Browse files Browse the repository at this point in the history
Scaling
  • Loading branch information
mieand authored Oct 5, 2017
2 parents 7d3e0ef + c71768b commit 39e8480
Show file tree
Hide file tree
Showing 3 changed files with 379 additions and 26 deletions.
290 changes: 289 additions & 1 deletion kmos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,121 @@
__version__ = "0.3.21"
VERSION = __version__

def evaluate_param_expression(param, parameters={}):
import tokenize
import StringIO
import math
from kmos import units

# convert parameters to dict if passed as list of Parameters()
if type(parameters) is list:
param_dict = {}
for parameter in parameters:
param_dict[parameter.name] = {'value': parameter.value}
parameters = param_dict

parameter_str = str(parameters[param]['value'])

# replace some aliases
parameter_str = parameter_str.replace('beta', '(1./(kboltzmann*T))')
# replace units used in parameters
for unit in units.keys:
parameter_str = parameter_str.replace(
unit, '%s' % eval('units.%s' % unit))
try:
return eval(parameter_str)
except:
try:
replaced_tokens=[]
input = StringIO.StringIO(parameter_str).readline
tokens = list(tokenize.generate_tokens(input))
except:
raise Exception('Could not tokenize expression: %s' % input)
for i, token, _, _, _ in tokens:
if token in ['sqrt', 'exp', 'sin', 'cos', 'pi', 'pow', 'log']:
replaced_tokens.append((i, 'math.' + token))
elif token.startswith('GibbsGas_'):
#evaluate gas phase gibbs free energy using ase thermochemistry module,
#experimental data from NIST CCCBDB, the electronic energy
#and current temperature and partial pressure
from kmos import species
species_name = '_'.join(token.split('_')[1:])
if species_name in dir(species):
if not 'T' in parameters:
raise Exception('Need "T" in parameters to evaluate gas phase gibbs free energy.')

if not ('p_%s' % species_name) in parameters:
raise Exception('Need "p_%s" in parameters to evaluate gas phase gibbs free energy.' % species_name)

replaced_tokens.append((i, 'species.%s.GibbsGas(%s,%s,%s)' % (
species_name,
parameters['E_'+species_name]['value'],
parameters['T']['value'],
parameters['p_%s' % species_name]['value'],
)))
else:
print('No NIST data assigned for %s' % species_name)
print('Setting chemical potential to zero')
replaced_tokens.append((i, '0'))

#gibbs=eval(replaced_tokens2[-1][-1])
#print species_name+': %.3f'%gibbs

elif token.startswith('GibbsAds_'):
#evaluate gibbs free energy of adsorbate using ase thermochemistry module,
#calculated frequencies and electronic energy and current temperature
from kmos import species
species_name = '_'.join(token.split('_')[1:])
if not 'T' in parameters:
raise Exception('Need "T" in parameters to evaluate adsorbate gibbs free energy.')
energy=parameters['E_'+species_name]['value']
try:
eval(energy)
except:
try:
replaced_tokens2=[]
input = StringIO.StringIO(energy).readline
tokens2 = list(tokenize.generate_tokens(input))
except:
raise Exception('Could not tokenize expression: %s' % input)
for j, token2, _, _, _ in tokens2:
if token2 in parameters:
parameter_str2 = str(parameters[token2]['value'])
try:
eval(parameter_str2)
replaced_tokens2.append((j, parameter_str2))
except:
try:
input = StringIO.StringIO(parameter_str2).readline
tokens3 = list(tokenize.generate_tokens(input))
except:
raise Exception('Could not tokenize expression: %s' % input)
for k, token3, _, _, _ in tokens3:
if token3 in parameters:
parameter_str3 = str(parameters[token3]['value'])
replaced_tokens2.append((k, parameter_str3))
else:
replaced_tokens2.append((k, token3))
else:
replaced_tokens2.append((j, token2))
energy = tokenize.untokenize(replaced_tokens2)
replaced_tokens.append((i, 'species.GibbsAds(%s,%s,%s)' % (
energy,
parameters['f_'+species_name]['value'],
parameters['T']['value'],
)))

#gibbs=eval(replaced_tokens2[-1][-1])
#print species_name+': %.3f'%gibbs

elif token in parameters:
replaced_tokens.append((i, str(parameters[token]['value'])))
else:
replaced_tokens.append((i, token))
parameter_str = tokenize.untokenize(replaced_tokens)
return eval(parameter_str)
#print parameter_str
#print token+': %.7f'%eval(parameter_str)

rate_aliases = { 'beta' : '(1/(kboltzmann*T))'}

Expand Down Expand Up @@ -127,13 +242,186 @@ def evaluate_rate_expression(rate_expr, parameters={}):
print('No JANAF table assigned for %s' % species_name)
print('Setting chemical potential to zero')
replaced_tokens.append((i, '0'))

elif token.startswith('GibbsGas_'):
#evaluate gas phase gibbs free energy using ase thermochemistry module,
#experimental data from NIST CCCBDB, the electronic energy
#and current temperature and partial pressure
from kmos import species
species_name = '_'.join(token.split('_')[1:])
if species_name in dir(species):
if not 'T' in parameters:
raise Exception('Need "T" in parameters to evaluate gas phase gibbs free energy.')

if not ('p_%s' % species_name) in parameters:
raise Exception('Need "p_%s" in parameters to evaluate gas phase gibbs free energy.' % species_name)

replaced_tokens.append((i, 'species.%s.GibbsGas(%s,%s,%s)' % (
species_name,
parameters['E_'+species_name]['value'],
parameters['T']['value'],
parameters['p_%s' % species_name]['value'],
)))
else:
print('No NIST data assigned for %s' % species_name)
print('Setting chemical potential to zero')
replaced_tokens.append((i, '0'))

#gibbs=eval(replaced_tokens[-1][-1])
#print species_name+': %.3f'%gibbs

elif token.startswith('GibbsAds_'):
#evaluate gibbs free energy of adsorbate using ase thermochemistry module,
#calculated frequencies and electronic energy and current temperature
from kmos import species
species_name = '_'.join(token.split('_')[1:])
if not 'T' in parameters:
raise Exception('Need "T" in parameters to evaluate adsorbate gibbs free energy.')
energy=parameters['E_'+species_name]['value']
try:
eval(energy)
except:
try:
replaced_tokens2=[]
input = StringIO.StringIO(energy).readline
tokens2 = list(tokenize.generate_tokens(input))
except:
raise Exception('Could not tokenize expression: %s' % input)
for j, token2, _, _, _ in tokens2:
if token2 in parameters:
parameter_str = str(parameters[token2]['value'])
try:
eval(parameter_str)
replaced_tokens2.append((j, parameter_str))
except:
try:
input = StringIO.StringIO(parameter_str).readline
tokens3 = list(tokenize.generate_tokens(input))
except:
raise Exception('Could not tokenize expression: %s' % input)
for k, token3, _, _, _ in tokens3:
if token3 in parameters:
parameter_str = str(parameters[token3]['value'])
replaced_tokens2.append((k, parameter_str))
else:
replaced_tokens2.append((k, token3))
else:
replaced_tokens2.append((j, token2))
energy = tokenize.untokenize(replaced_tokens2)
replaced_tokens.append((i, 'species.GibbsAds(%s,%s,%s)' % (
energy,
parameters['f_'+species_name]['value'],
parameters['T']['value'],
)))

#gibbs=eval(replaced_tokens[-1][-1])
#print species_name+': %.3f'%gibbs

elif token in parameters:
parameter_str = str(parameters[token]['value'])
# replace some aliases
parameter_str = parameter_str.replace('beta', '(1./(kboltzmann*T))')
# replace units used in parameters
for unit in units.keys:
parameter_str = parameter_str.replace(
unit, '%s' % eval('units.%s' % unit))
replaced_tokens.append((i, parameter_str))
try:
eval(parameter_str)
replaced_tokens.append((i, parameter_str))
except:
try:
replaced_tokens2=[]
input = StringIO.StringIO(parameter_str).readline
tokens2 = list(tokenize.generate_tokens(input))
except:
raise Exception('Could not tokenize expression: %s' % input)
for i, token2, _, _, _ in tokens2:
if token2 in ['sqrt', 'exp', 'sin', 'cos', 'pi', 'pow', 'log']:
replaced_tokens2.append((i, 'math.' + token2))
elif token2.startswith('GibbsGas_'):
#evaluate gas phase gibbs free energy using ase thermochemistry module,
#experimental data from NIST CCCBDB, the electronic energy
#and current temperature and partial pressure
from kmos import species
species_name = '_'.join(token2.split('_')[1:])
if species_name in dir(species):
if not 'T' in parameters:
raise Exception('Need "T" in parameters to evaluate gas phase gibbs free energy.')

if not ('p_%s' % species_name) in parameters:
raise Exception('Need "p_%s" in parameters to evaluate gas phase gibbs free energy.' % species_name)

replaced_tokens2.append((i, 'species.%s.GibbsGas(%s,%s,%s)' % (
species_name,
parameters['E_'+species_name]['value'],
parameters['T']['value'],
parameters['p_%s' % species_name]['value'],
)))
else:
print('No NIST data assigned for %s' % species_name)
print('Setting chemical potential to zero')
replaced_tokens2.append((i, '0'))

#gibbs=eval(replaced_tokens2[-1][-1])
#print species_name+': %.3f'%gibbs

elif token2.startswith('GibbsAds_'):
#evaluate gibbs free energy of adsorbate using ase thermochemistry module,
#calculated frequencies and electronic energy and current temperature
from kmos import species
species_name = '_'.join(token2.split('_')[1:])
if not 'T' in parameters:
raise Exception('Need "T" in parameters to evaluate adsorbate gibbs free energy.')
energy=parameters['E_'+species_name]['value']
try:
eval(energy)
except:
try:
replaced_tokens3=[]
input = StringIO.StringIO(energy).readline
tokens3 = list(tokenize.generate_tokens(input))
except:
raise Exception('Could not tokenize expression: %s' % input)
for j, token3, _, _, _ in tokens3:
if token3 in parameters:
parameter_str = str(parameters[token3]['value'])
try:
eval(parameter_str)
replaced_tokens3.append((j, parameter_str))
except:
try:
input = StringIO.StringIO(parameter_str).readline
tokens4 = list(tokenize.generate_tokens(input))
except:
raise Exception('Could not tokenize expression: %s' % input)
for k, token4, _, _, _ in tokens4:
if token4 in parameters:
parameter_str = str(parameters[token4]['value'])
replaced_tokens3.append((k, parameter_str))
else:
replaced_tokens3.append((k, token4))
else:
replaced_tokens3.append((j, token3))
energy = tokenize.untokenize(replaced_tokens3)
replaced_tokens2.append((i, 'species.GibbsAds(%s,%s,%s)' % (
energy,
parameters['f_'+species_name]['value'],
parameters['T']['value'],
)))

#gibbs=eval(replaced_tokens2[-1][-1])
#print species_name+': %.3f'%gibbs

elif token2 in parameters:
replaced_tokens2.append((i, str(parameters[token2]['value'])))
else:
replaced_tokens2.append((i, token2))
parameter_str = tokenize.untokenize(replaced_tokens2)

#print parameter_str
#print token+': %.7f'%eval(parameter_str)

replaced_tokens.append((i, parameter_str))
else:
replaced_tokens.append((i, token))

Expand Down
5 changes: 5 additions & 0 deletions kmos/run/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from copy import deepcopy
from fnmatch import fnmatch
from kmos import evaluate_rate_expression
from kmos import evaluate_param_expression
from kmos.utils import OrderedDict
import kmos.run.acf
import kmos.utils.progressbar
Expand Down Expand Up @@ -416,6 +417,10 @@ def get_param_header(self):
for param_name in sorted(self.settings.parameters)
if self.settings.parameters[param_name].get('adjustable', False))

def get_param_value(self,param):
"""Return the evaluated value of a parameter"""
return evaluate_param_expression(param, settings.parameters)

def get_occupation_header(self):
"""Return the names of the fields returned by
self.get_atoms().occupation.
Expand Down
Loading

0 comments on commit 39e8480

Please sign in to comment.