Did I find the right examples for you? yes no      Crawl my project      Python Jobs

All Samples(13)  |  Call(9)  |  Derive(0)  |  Import(4)
Evaluates an expression for a typical kMC rate constant.
External parameters can be passed in as dictionary, like the
following:
   parameters = {'p_CO':{'value':1},
                 'T':{'value':1}}

or as a list of parameters:
   parameters = [Parameter(), ... ]

        def evaluate_rate_expression(rate_expr, parameters={}):
    """Evaluates an expression for a typical kMC rate constant.
     External parameters can be passed in as dictionary, like the
     following:
        parameters = {'p_CO':{'value':1},
                      'T':{'value':1}}

     or as a list of parameters:
        parameters = [Parameter(), ... ]
     """
    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

    if not rate_expr:
        rate_const = 0.0
    else:
        replaced_tokens = []

        # replace some aliases
        rate_expr = rate_expr.replace('beta', '(1./(kboltzmann*T))')
        try:
            input = StringIO.StringIO(rate_expr).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 in dir(units):
                replaced_tokens.append((i, str(eval('units.' + token))))
            elif token.startswith('m_'):
                from ase.atoms import string2symbols
                from ase.data import atomic_masses
                from ase.data import atomic_numbers
                species_name = '_'.join(token.split('_')[1:])
                symbols = string2symbols(species_name)
                replaced_tokens.append((i,
                            '%s' % sum([atomic_masses[atomic_numbers[symbol]]
                            for symbol in symbols])))
            elif token.startswith('mu_'):
                # evaluate gas phase chemical potential if among
                # available JANAF tables if from current temperature
                # and corresponding 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 chemical potential.')

                    if not ('p_%s' % species_name) in parameters:
                        raise Exception('Need "p_%s" in parameters to evaluate chemical potential.' % species_name)

                    replaced_tokens.append((i, 'species.%s.mu(%s,%s)' % (
                                   species_name,
                                   parameters['T']['value'],
                                   parameters['p_%s' % species_name]['value'],
                                   )))
                else:
                    print('No JANAF table assigned for %s' % species_name)
                    print('Setting chemical potential to zero')
                    replaced_tokens.append((i, '0'))
            elif token in parameters:
                parameter_str = str(parameters[token]['value'])
                # 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))
            else:
                replaced_tokens.append((i, token))

        rate_expr = tokenize.untokenize(replaced_tokens)
        try:
            rate_const = eval(rate_expr)
        except Exception, e:
            raise UserWarning(
            "Could not evaluate rate expression: %s\nException: %s" \
                % (rate_expr, e))

    return rate_const
        


src/p/y/python-kmos-0.3.6/kmos/run.py   python-kmos(Download)
from copy import deepcopy
from fnmatch import fnmatch
from kmos import evaluate_rate_expression
from kmos.utils import OrderedDict
from math import log
            if pattern is None or fnmatch(proc, pattern):
                rate_expr = settings.rate_constants[proc][0]
                rate_const = evaluate_rate_expression(rate_expr,
                                                      settings.parameters)
                res += ('# %s: %s = %.2e s^{-1}\n' % (proc, rate_expr,
        """
        rate_expr = settings.rate_constants[proc][0]
        return evaluate_rate_expression(rate_expr, settings.parameters)
 
    def inverse(self):
        for proc in sorted(settings.rate_constants):
            rate_expr = settings.rate_constants[proc][0]
            rate_const = evaluate_rate_expression(rate_expr,
                                                  settings.parameters)
            res += '# %s: %.2e s^{-1} = %s\n' % (proc, rate_const, rate_expr)
            parameters = settings.parameters
        if type(rate_constant) is str:
            rate_constant = evaluate_rate_expression(rate_constant,
                                                     parameters)
        try:

src/p/y/python-kmos-0.3.6/kmos/types.py   python-kmos(Download)
    def evaluate_rate_expression(self, parameters={}):
        import kmos.evaluate_rate_expression
        return kmos.evaluate_rate_expression(self.rate_constant, parameters)
 
 

src/p/y/python-kmos-0.3.6/kmos/io.py   python-kmos(Download)
        """
 
        from kmos import evaluate_rate_expression
 
        data = self.data
 
            try:
                evaluate_rate_expression(process.rate_constant, parameters)
            except Exception, e:
                raise UserWarning('Could not evaluate (%s)\n%s\nProcess: %s'

src/p/y/python-kmos-0.3.6/kmos/gui/forms.py   python-kmos(Download)
                       ConditionAction, Site
 
from kmos import evaluate_rate_expression
from kmos.types import parse_chemical_expression
 
                self.rate_constant.set_tooltip_text(
                    'Current value: %.5e s^{-1}' %
                    evaluate_rate_expression(expr,
                        self.project_tree.get_parameters()))
            except Exception, e:
    def on_rate_constant__validate(self, _widget, expr):
        try:
            self.rate_constant.set_tooltip_text('Current value: %.2e s^{-1}' %
                evaluate_rate_expression(expr,
                    self.project_tree.get_parameters()))