Ejemplo n.º 1
0
    def _add_variables_for_lattice_references(self):
        # iterate all values, adding "x->y" lattice referenes as variables "x.y"
        from sirepo.template.code_variable import CodeVar

        def _fix_value(value, names):
            value = re.sub(r'\-\>', '.', value)
            expr = CodeVar.infix_to_postfix(value.lower())
            for v in expr.split(' '):
                if CodeVar.is_var_value(v):
                    m = re.match(r'^(.*?)\.(.*)', v)
                    if m:
                        names[v] = [m.group(1), m.group(2)]
            return value

        names = {}
        for v in self.data.models.rpnVariables:
            if CodeVar.is_var_value(v.value):
                v.value = _fix_value(v.value, names)
        for el in self.data.models.elements:
            for f in el:
                v = el[f]
                if CodeVar.is_var_value(v):
                    el[f] = _fix_value(v, names)
        for name in names:
            for el in self.data.models.elements:
                if el.name.lower() == names[name][0]:
                    f = names[name][1]
                    if f in el:
                        self.data.models.rpnVariables.append(
                            PKDict(
                                name=name,
                                value=el[f],
                            ))
Ejemplo n.º 2
0
def test_infix_to_postfix():
    from pykern.pkcollections import PKDict
    from pykern.pkunit import pkeq
    from sirepo.template.code_variable import CodeVar, PurePythonEval
    code_var = CodeVar([], PurePythonEval(PKDict()))
    pkeq(code_var.infix_to_postfix('x + y * 2'), 'x y 2 * +')
    pkeq(code_var.infix_to_postfix('-(x)'), 'x chs')
    pkeq(code_var.infix_to_postfix('-(x + +x)'), 'x x + chs')
Ejemplo n.º 3
0
def test_cache():
    from pykern.pkcollections import PKDict
    from pykern.pkunit import pkeq
    from sirepo.template.code_variable import CodeVar, PurePythonEval
    code_var = CodeVar(
        [
            PKDict(
                name='x',
                value='123',
            ),
            PKDict(
                name='y',
                value='x + x',
            ),
            PKDict(
                name='z',
                value='y * -20',
            ),
        ],
        PurePythonEval(),
    )
    pkeq(
        code_var.compute_cache(
            # data
            PKDict(models=PKDict(
                beamlines=[],
                elements=[
                    PKDict(
                        _id=1,
                        type='point',
                        p1='x + y',
                        p2=234,
                    ),
                ],
                commands=[],
            )),
            # schema
            PKDict(model=PKDict(point=PKDict(
                p1=["P1", "RPNValue", 0],
                p2=["P2", "RPNValue", 0],
            )), ),
        ),
        PKDict({
            'x + x': 246,
            'x + y': 369,
            'x': 123,
            'y * -20': -4920,
            'y': 246,
            'z': -4920,
        }))
    pkeq(
        code_var.get_expr_dependencies('x x * x +'),
        ['x'],
    )
    pkeq(
        code_var.get_expr_dependencies('y 2 pow'),
        ['x', 'y'],
    )
Ejemplo n.º 4
0
 def _fix_value(value, names):
     value = re.sub(r'\-\>', '.', value)
     expr = CodeVar.infix_to_postfix(value.lower())
     for v in expr.split(' '):
         if CodeVar.is_var_value(v):
             m = re.match(r'^(.*?)\.(.*)', v)
             if m:
                 names[v] = [m.group(1), m.group(2)]
     return value
Ejemplo n.º 5
0
def test_eval():
    from pykern.pkcollections import PKDict
    from pykern.pkunit import pkeq
    from sirepo.template.code_variable import CodeVar, PurePythonEval
    code_var = CodeVar(
        [
            PKDict(
                name='gamma',
                value='(bend_energy * 1e-3 + EMASS) / EMASS',
            ),
            PKDict(
                name='bend_energy',
                value=6.50762633,
            ),
        ],
        PurePythonEval(PKDict(
            CLIGHT=299792458,
            EMASS=0.00051099892,
        )),
    )
    pkeq(0.9973461123895662, code_var.eval_var('sqrt(1 - (1/pow(gamma, 2)))')[0])
    pkeq('unknown token: abc', code_var.eval_var('abc + 123')[1])
    pkeq('division by zero', code_var.eval_var('100 / 0')[1])
    pkeq('1 1 gamma 2 pow / - sqrt', code_var.infix_to_postfix('sqrt(1 - (1/pow(gamma, 2)))'))
    pkeq(True, code_var.is_var_value('abc'))
    pkeq(False, code_var.is_var_value('-1.234e-6'))
    pkeq(False, code_var.is_var_value('0'))
Ejemplo n.º 6
0
def test_cache():
    from pykern.pkcollections import PKDict
    from pykern.pkunit import pkeq
    from sirepo.template.code_variable import CodeVar, PurePythonEval
    code_var = CodeVar(
        [
            PKDict(
                name='x',
                value='123',
            ),
            PKDict(
                name='y',
                value='x + x',
            ),
            PKDict(
                name='z',
                value='y * -20',
            ),
        ],
        PurePythonEval(),
    )
    pkeq(
        code_var.compute_cache(
            PKDict(
                models=PKDict(
                    beamlines=[],
                    elements=[],
                    commands=[],
                )
            ),
            PKDict(),
        ),
        PKDict({
            'x': 123,
            'y': 246,
            'x + x': 246,
            'y * -20': -4920,
            'z': -4920,
        })
    )
    pkeq(
        code_var.get_expr_dependencies('x x * x +'),
        ['x'],
    )
    pkeq(
        code_var.get_expr_dependencies('y 2 pow'),
        ['x', 'y'],
    )
Ejemplo n.º 7
0
 def render_lattice(self,
                    fields,
                    quote_name=False,
                    want_semicolon=False,
                    want_name=True,
                    want_var_assign=False):
     """Render lattice elements.
     """
     from sirepo.template.code_variable import CodeVar
     res = ''
     for el in fields:
         # el is [model, [[f, v], [f, v]...]]
         el_type = self.type_for_data(el[0])
         if want_name:
             name = el[0].name.upper()
             if quote_name:
                 name = '"{}"'.format(name)
             res += '{}: {},'.format(name, el_type)
         else:
             res += '{},'.format(el_type)
         for f in el[1]:
             var_assign = ''
             if want_var_assign:
                 s = self.schema.model[el_type]
                 if f[0] in s and s[
                         f[0]][1] == 'RPNValue' and CodeVar.is_var_value(
                             f[1]):
                     var_assign = ':'
             res += '{}{}={},'.format(f[0], var_assign, f[1])
         res = res[:-1]
         if want_semicolon:
             res += ';'
         res += '\n'
     return res
Ejemplo n.º 8
0
def test_case_insensitive():
    # tests case insensitive and attribute like variables
    from pykern.pkcollections import PKDict
    from pykern.pkunit import pkeq
    from sirepo.template.code_variable import CodeVar, PurePythonEval
    code_var = CodeVar(
        [
            PKDict(
                name='x.x7.x',
                value='123',
            ),
            PKDict(
                name='Y',
                value='x.X7.x + x.x7.x',
            ),
        ],
        PurePythonEval(),
        case_insensitive=True,
    )
    pkeq(
        code_var.compute_cache(
            PKDict(
                models=PKDict(
                    beamlines=[],
                    elements=[],
                    commands=[],
                )
            ),
            PKDict(),
        ),
        PKDict({
            'x.x7.x': 123,
            'y': 246,
            'x.x7.x + x.x7.x': 246,
        })
    )
    pkeq(
        code_var.get_expr_dependencies('Y y +'),
        ['x.x7.x', 'y'],
    )
Ejemplo n.º 9
0
def test_case_insensitive():
    # tests case insensitive and attribute like variables
    from pykern.pkcollections import PKDict
    from pykern.pkunit import pkeq
    from sirepo.template.code_variable import CodeVar, PurePythonEval
    code_var = CodeVar(
        [
            PKDict(
                name='x.x7.x',
                value='123',
            ),
            PKDict(
                name='Y',
                value='x.X7.x + x.x7.x',
            ),
        ],
        PurePythonEval(),
        case_insensitive=True,
    )
    pkeq(code_var.compute_cache(
        _empty_data(),
        PKDict(),
    ), PKDict({
        'x.x7.x': 123,
        'y': 246,
        'x.x7.x + x.x7.x': 246,
    }))
    pkeq(
        code_var.get_expr_dependencies('Y y +'),
        ['x.x7.x', 'y'],
    )
    pkeq(
        code_var.validate_var_delete(
            'X.X7.X',
            _empty_data(),
            PKDict(),
        ),
        '"X.X7.X" is in use in variable(s): y',
    )
Ejemplo n.º 10
0
 def __is_default(self, model, field_schema, field):
     from sirepo.template.code_variable import CodeVar
     if len(field_schema) < 3:
         return True
     default_value = field_schema[2]
     value = model[field]
     if value is not None and default_value is not None:
         if value == default_value:
             return True
         if field_schema[1] == 'RPNValue':
             if value and not CodeVar.is_var_value(value):
                 return float(value) == default_value
         return str(value) == str(default_value)
     if value is not None:
         return False
     return True
Ejemplo n.º 11
0
def test_eval2():
    from pykern.pkcollections import PKDict
    from pykern.pkunit import pkeq
    from sirepo.template.code_variable import CodeVar, PurePythonEval
    code_var = CodeVar(
        [
            PKDict(name=x[0], value=x[1]) for x in (
                ['lcend0d5', '66.0000298158'],
                ['thd0', '0.0036745387'],
                ['lpld0d5', 'lcend0d5 / cos(thd0)'],
                ['lpld0q1', '1.2646485614'],
                ['lplq1q2', '1.6766315'],
                ['lplq2q3', '1.8607395'],
                ['lcor', '0.5'],
                ['lcq4', '0.5442755 - (lcor /2.0)'],
                ['lsxt', '0.75'],
                ['lqs', '0.50605 - (lsxt / 2.0)'],
                ['lqb', '1.1072776'],
                ['lsb', '(lqb - lqs) - lsxt'],
                ['lplq4q5', '6.0190255 - lcq4 - lqs - lsxt - lsb - lcor'],
                ['lq', '1.11'],
                ['lplq5q6', '14.8110470957 - lq'],
                ['lcq', '0.54525 - (lcor / 2.0)'],
                ['lplq5d5', '(lplq5q6 / 2.0) - lcq - lcor'],
                [
                    'lpltemp',
                    'lpld0d5 - lpld0q1 - lplq1q2 - lplq2q3 - lplq4q5 - lplq5d5'
                ],
                ['lq1', '1.44'],
                ['lq2', '3.391633'],
                ['lq3', '2.100484'],
                ['lq4', '1.811949'],
                ['lqb4', '1.1326896'],
                ['lsb4', 'lqb4 - lqs - lsxt'],
                ['lqs4', '0.5050755 - (lsxt / 2.0)'],
                [
                    'lplq3q4',
                    'lpltemp - lq1 - lq2 - lq3 - lq4 - lq - lsb4 - lsb - lqs4 - lqs - lcq - lcq4 - (2.0 * (lsxt + lcor))'
                ],
                ['lbfl3', '1.53902'],
                ['lbc3', '0.47142826 - (lcor / 2.0)'],
                ['lcq3a', '0.597968 - (lcor / 2.0)'],
                ['lbfl4', '0.61279'],
                ['l3space', 'lplq3q4 - lbfl3 - lbc3 - lcor - lcq3a - lbfl4'],
                ['lbsk12iy', '0.042545'],
                ['lbsk12ly', '0.048638'],
                ['lpolfll', '0.837184'],
                ['lpolflm', '0.508'],
                ['lpolfls', '0.760984'],
                ['lipmfls', '0.886116'],
                ['lipmfll', '0.282284'],
                ['lcpufl', '0.3937'],
                ['leldfl', '0.119634'],
                [
                    'l3y12sum',
                    'lpolfll + lpolflm + lpolfls + lipmfls + lipmfll  + 2.0 * ((2.0 * lcpufl) + leldfl)'
                ],
                [
                    'l3y12l29',
                    'l3space - lbsk12iy - lbsk12ly - l3y12sum - 28.899634'
                ],
            )
        ],
        PurePythonEval(PKDict()),
    )
    pkeq(0.042466423001805254, code_var.eval_var('l3y12l29')[0])