Esempio n. 1
0
def test_c_funcspec_with_massmatrix():
    args = {
        'name': 'fs_with_massmatrix',
        'targetlang': 'c',
        'vars': ['x', 'y'],
        'varspecs': {'y': '-1', 'x': 'y - x * x'},
        'fnspecs': {'massMatrix': (['t', 'x', 'y'], '[[0,0],[0,1]]')},
    }
    fs = FuncSpec(args)
    assert fs.spec[0].split('\n') == [
        'void vfieldfunc(unsigned n_, unsigned np_, double t, double *Y_, double *p_, double *f_, unsigned wkn_, double *wk_, unsigned xvn_, double *xv_){',
        '',
        'f_[0] = y-x*x;',
        'f_[1] = -1;',
        '',
        '}',
        '',
        '',
    ]

    assert fs.auxfns['massMatrix'][0].split('\n') == [
        'void massMatrix(unsigned n_, unsigned np_, double t, double *Y_, double *p_, double **f_, unsigned wkn_, double *wk_, unsigned xvn_, double *xv_) {',
        '',
        '',
        'f_[0][0] = 0;',
        'f_[0][1] = 0;',
        'f_[1][0] = 0;',
        'f_[1][1] = 1;',
        '',
        ' ;',
        '',
        '}',
    ]
Esempio n. 2
0
def test_dependencies():
    fs = FuncSpec({
        'vars': ['x', 'y', 'z'],
        'varspecs': {
            'x': 'y + myaux(x, z)',
            'y': 'z - input_y',
            'z': '1',
            'my': 'x + y + z',
        },
        'fnspecs': {
            'myaux': (['x', 'z'], 'x**2 + z^2'),
            'my': (['x', 'y'], 'x + y'),
        },
        'auxvars': ['my'],
        'inputs': ['input_y'],
        'targetlang': 'matlab',
    })

    assert ('x', 'y') in fs.dependencies
    assert ('x', 'x') in fs.dependencies
    assert ('x', 'z') in fs.dependencies
    assert ('y', 'z') in fs.dependencies
    assert ('y', 'input_y') in fs.dependencies
    assert ('my', 'x') in fs.dependencies
    assert ('my', 'y') in fs.dependencies
    assert ('my', 'z') in fs.dependencies
    assert all(v != 'z' for v, _ in fs.dependencies)
Esempio n. 3
0
def test_python_funcspec_inserts_additional_code_in_vfield():
    start = "    print('START')"
    end = "    print('END')"
    args = {
        'name': 'test_codeinsert',
        'vars': ['x'],
        'pars': ['p'],
        'varspecs': {
            'x': 'p * x - 1'
        },
        'fnspecs': {
            'myaux': (['x'], 'x**2 + p')
        },
        'codeinsert_start': start,
        'codeinsert_end': end,
    }

    fs = FuncSpec(args)
    assert fs.spec[0].split('\n') == [
        'def _specfn(ds, t, x, parsinps):',
        start,
        '    xnew0 = parsinps[0] * x[0] - 1 ',
        end,
        '    return array([xnew0])',
        '',
    ]

    assert fs.auxfns['myaux'][0].split('\n') == [
        'def _auxfn_myaux(ds, parsinps, x):',
        '    return math.pow(x,2)+parsinps[0]'
    ]
Esempio n. 4
0
def test_python_funcspec_with_massmatrix():
    args = {
        'name': 'fs_with_massmatrix',
        'vars': ['x', 'y'],
        'varspecs': {
            'y': '-1',
            'x': 'y - x * x'
        },
        'fnspecs': {
            'massMatrix': (['t', 'x', 'y'], '[[0,0],[0,1]]')
        },
    }
    fs = FuncSpec(args)
    assert fs.spec[0].split('\n') == [
        'def _specfn(ds, t, x, parsinps):',
        '    xnew0 = x[1] - x[0] * x[0] ',
        '    xnew1 = -1 ',
        '    return array([xnew0, xnew1])',
        '',
    ]

    assert fs.auxfns['massMatrix'][0].split('\n') == [
        'def _auxfn_massMatrix(ds, t, x, parsinps):',
        '    xmat0 = [0,0] ',
        '    xmat1 = [0,1] ',
        '    return array([xmat0, xmat1])'
        '',
    ]
Esempio n. 5
0
def test_funcspec_uses_default_name_if_not_set():
    args = {
        'vars': ['x'],
        'varspecs': {'x': 'x + 1'},
    }

    fs = FuncSpec(args)
    assert 'untitled' == fs.name
Esempio n. 6
0
def test_funcspec_sum_macro_raises_value_error():
    with pytest.raises(ValueError):
        FuncSpec({
            'vars': 'z',
            'varspecs': {
                'z': 'sum(i, 1, 3, [i] / [i+1]**2)',
            },
        })
Esempio n. 7
0
def test_matlab_funspec_for_macro_raises_exception():
    with pytest.raises(ValueError):
        FuncSpec({
            'vars': ['z1', 'z2', 'z3'],
            'targetlang': 'matlab',
            'varspecs': {
                'z[i]': 'for(i, 1, 3, z[i]**2)',
            },
        })
Esempio n. 8
0
def test_funcspec_raises_exception_if_there_are_invalid_keys():
    args = {
        'name': 'fs_with_invalid_key',
        'invalid_key': 'dummy',
        'myvars': ['a', 'b'],
    }

    with pytest.raises(PyDSTool_KeyError):
        FuncSpec(args)
Esempio n. 9
0
def test_matlab_funspec_if_raises_exception():
    with pytest.raises(NotImplementedError):
        FuncSpec({
            'name': 'single_var',
            'targetlang': 'matlab',
            'vars': ['x'],
            'varspecs': {
                'x': 'if(x < 0, x, x**3)'
            },
        })
Esempio n. 10
0
def test_matlab_funcspec_for_ds_with_single_var_and_single_param():
    args = {
        'name': 'fun_with_var_and_par',
        'targetlang': 'matlab',
        'vars': ['x'],
        'pars': ['p'],
        'varspecs': {
            'x': 'p * x ** 3 - 1'
        },
        'fnspecs': {
            'myaux': (['x'], 'x**2 + p')
        },
    }
    fs = FuncSpec(args)
    assert fs.spec[0].split('\n') == [
        'function [vf_, y_] = vfield(vf_, t_, x_, p_)',
        '% Vector field definition for model fun_with_var_and_par',
        '% Generated by PyDSTool for ADMC++ target',
        '',
        '',
        '% Parameter definitions',
        '',
        '\tp = p_(1);',
        '',
        '% Variable definitions',
        '',
        '\tx = x_(1);',
        '',
        '',
        'y_(1) = p*x^3-1;',
        '',
        '',
        '',
    ]

    assert fs.auxfns['myaux'][0].split('\n') == [
        'function y_ = myaux(x__,  p_)',
        '% Auxilliary function myaux for model fun_with_var_and_par',
        '% Generated by PyDSTool for ADMC++ target', '', '',
        '% Parameter definitions', '', '\tp = p_(1);', ' ', '', '',
        'y_ = x__^2+p;', '', ''
    ]

    assert all(fn in fs._pyauxfns.keys() for fn in [
        'getbound',
        'getindex',
        'globalindepvar',
        'heav',
        'if',
        'initcond',
    ])

    assert 'myaux' in fs._pyauxfns.keys()
Esempio n. 11
0
def test_funcspec_names_list_are_sorted():
    fs = FuncSpec({
        'vars': ['y', 'z', 'x'],
        'varspecs': {'x': 'y', 'y': 'z', 'z': 'x', 'aux2': 'x + y', 'aux11': 'z * x'},

        'auxvars': ['aux2', 'aux11'],
        'inputs': ['input_y', 'input_x'],
        'pars': ['k', 'o', 'v', 'j'],
    })
    assert sorted(fs.vars) == fs.vars
    assert sorted(fs.auxvars) == fs.auxvars
    assert sorted(fs.inputs) == fs.inputs
    assert sorted(fs.pars) == fs.pars
Esempio n. 12
0
def test_python_funcspec_for_ds_with_single_var():
    args = {
        'name': 'single_var',
        'vars': ['x'],
        'varspecs': {
            'x': 'x + 1'
        },
    }
    fs = FuncSpec(args)
    assert fs.spec == ('\n'.join([
        'def _specfn(ds, t, x, parsinps):', '    xnew0 = x[0] + 1 ',
        '    return array([xnew0])\n'
    ]), '_specfn')
Esempio n. 13
0
def test_python_funspec_ignoring_for_macro():
    fs = FuncSpec({
        'vars': 'z[i]',
        'varspecs': {
            'z[i]': 'for(i, 1, 3, z[i]**2)',
        },
    })

    assert fs.vars == ['z[i]']
    assert fs.spec[0].split('\n') == [
        'def _specfn(ds, t, x, parsinps):',
        '    return array([])',
        '',
    ]
Esempio n. 14
0
def test_python_funcspec_for_ds_with_if_builtin():
    args = {
        'name': 'single_var',
        'vars': ['x'],
        'varspecs': {
            'x': 'if(x < 0, x, x**3)'
        },
    }
    fs = FuncSpec(args)
    assert fs.spec[0].split('\n') == [
        'def _specfn(ds, t, x, parsinps):',
        '    xnew0 = ds._auxfn_if(parsinps, x[0]<0,x[0],math.pow(x[0],3))',
        '    return array([xnew0])', ''
    ]
Esempio n. 15
0
def test_c_funcspec_for_ds_with_if_builtin():
    args = {
        'name': 'single_var',
        'targetlang': 'c',
        'vars': ['x'],
        'varspecs': {
            'x': 'if(x < 0, x, x**3)'
        },
    }
    fs = FuncSpec(args)
    assert fs.spec[0].split('\n') == [
        'void vfieldfunc(unsigned n_, unsigned np_, double t, double *Y_, double *p_, double *f_, unsigned wkn_, double *wk_, unsigned xvn_, double *xv_){',
        '', 'f_[0] = __rhs_if(x<0,x,pow(x,3), p_, wk_, xv_);', '', '}', '', ''
    ]
Esempio n. 16
0
def test_python_funcspec_for_ds_with_two_vars():
    args = {
        'name': 'two_vars',
        'vars': ['x', 'y'],
        'varspecs': {
            'x': 'y + 1',
            'y': 'x - 1',
        },
    }

    fs = FuncSpec(args)
    assert fs.spec == ('\n'.join([
        'def _specfn(ds, t, x, parsinps):', '    xnew0 = x[1] + 1 ',
        '    xnew1 = x[0] - 1 ', '    return array([xnew0, xnew1])\n'
    ]), '_specfn')
Esempio n. 17
0
def test_c_funcspec_with_jacobian_and_auxfunc():
    args = {
        'name': 'ds_with_jac',
        'targetlang': 'c',
        'vars': ['y0', 'y1', 'y2'],
        'varspecs': {
            "y0": "ydot0(y0,y1,y2)",
            "y2": "ydot2(y0,y1,y2)",
            "y1": "-ydot0(y0,y1,y2)-ydot2(y0,y1,y2)"
        },
        'fnspecs': {
            'Jacobian': (['t', 'y0', 'y1',
                          'y2'], """[[-0.04,  1e4*y2       ,  1e4*y1 ],
                [ 0.04, -1e4*y2-6e7*y1, -1e4*y1 ],
                [ 0.0 ,  6e7*y1       ,  0.0    ]]"""),
            'ydot0': (['y0', 'y1', 'y2'], "-0.04*y0 + 1e4*y1*y2"),
            'ydot2': (['y0', 'y1', 'y2'], "3e7*y1*y1")
        },
    }

    fs = FuncSpec(args)

    assert fs.spec[0].split('\n') == [
        'void vfieldfunc(unsigned n_, unsigned np_, double t, double *Y_, double *p_, double *f_, unsigned wkn_, double *wk_, unsigned xvn_, double *xv_){',
        '', 'f_[0] = ydot0(y0,y1,y2, p_, wk_, xv_);',
        'f_[1] = -ydot0(y0,y1,y2, p_, wk_, xv_)-ydot2(y0,y1,y2, p_, wk_, xv_);',
        'f_[2] = ydot2(y0,y1,y2, p_, wk_, xv_);', '', '}', '', ''
    ]

    assert fs.auxfns['Jacobian'][0].split('\n') == [
        'void jacobian(unsigned n_, unsigned np_, double t, double *Y_, double *p_, double **f_, unsigned wkn_, double *wk_, unsigned xvn_, double *xv_) {',
        '', '', 'f_[0][0] = -0.04;', 'f_[0][1] = 0.04;', 'f_[0][2] = 0.0;',
        'f_[1][0] = 1e4*y2;', 'f_[1][1] = -1e4*y2-6e7*y1;',
        'f_[1][2] = 6e7*y1;', 'f_[2][0] = 1e4*y1;', 'f_[2][1] = -1e4*y1;',
        'f_[2][2] = 0.0;', '', ' ;', '', '}'
    ]

    assert fs.auxfns['ydot0'][0].split('\n') == [
        'double ydot0(double __y0__, double __y1__, double __y2__, double *p_, double *wk_, double *xv_) {',
        '', '', 'return -0.04*__y0__+1e4*__y1__*__y2__ ;', '', '}'
    ]

    assert fs.auxfns['ydot2'][0].split('\n') == [
        'double ydot2(double __y0__, double __y1__, double __y2__, double *p_, double *wk_, double *xv_) {',
        '', '', 'return 3e7*__y1__*__y1__ ;', '', '}'
    ]
Esempio n. 18
0
def test_python_funcspec_with_jacobian_and_auxfunc():
    args = {
        'name': 'ds_with_jac',
        'vars': ['y0', 'y1', 'y2'],
        'varspecs': {
            "y0": "ydot0(y0,y1,y2)",
            "y2": "ydot2(y0,y1,y2)",
            "y1": "-ydot0(y0,y1,y2)-ydot2(y0,y1,y2)"
        },
        'fnspecs': {
            'Jacobian': (['t', 'y0', 'y1',
                          'y2'], """[[-0.04,  1e4*y2       ,  1e4*y1 ],
                [ 0.04, -1e4*y2-6e7*y1, -1e4*y1 ],
                [ 0.0 ,  6e7*y1       ,  0.0    ]]"""),
            'ydot0': (['y0', 'y1', 'y2'], "-0.04*y0 + 1e4*y1*y2"),
            'ydot2': (['y0', 'y1', 'y2'], "3e7*y1*y1")
        },
    }

    fs = FuncSpec(args)

    assert fs.spec[0].split('\n') == [
        'def _specfn(ds, t, x, parsinps):',
        '    xnew0 = ds._auxfn_ydot0(parsinps, x[0],x[1],x[2])',
        '    xnew1 = -ds._auxfn_ydot0(parsinps, x[0],x[1],x[2])-ds._auxfn_ydot2(parsinps, x[0],x[1],x[2])',
        '    xnew2 = ds._auxfn_ydot2(parsinps, x[0],x[1],x[2])',
        '    return array([xnew0, xnew1, xnew2])',
        '',
    ]

    assert fs.auxfns['Jacobian'][0].split('\n') == [
        'def _auxfn_Jac(ds, t, x, parsinps):',
        '    xjac0 = [-0.04,1e4*x[2],1e4*x[1]] ',
        '    xjac1 = [0.04,-1e4*x[2]-6e7*x[1],-1e4*x[1]] ',
        '    xjac2 = [0.0,6e7*x[1],0.0] ',
        '    return array([xjac0, xjac1, xjac2])',
    ]

    assert fs.auxfns['ydot0'][0].split('\n') == [
        'def _auxfn_ydot0(ds, parsinps, y0, y1, y2):',
        '    return -0.04*y0 + 1e4*y1*y2'
    ]

    assert fs.auxfns['ydot2'][0].split('\n') == [
        'def _auxfn_ydot2(ds, parsinps, y0, y1, y2):', '    return 3e7*y1*y1'
    ]
Esempio n. 19
0
def test_c_funcspec_has_python_user_auxfn_interface():
    args = {
        'name': 'test_user_auxfn_interface',
        'targetlang': 'c',
        'vars': ['x'],
        'pars': ['p'],
        'varspecs': {'x': 'p * x - 1'},
        'fnspecs': {'myaux': (['x'], 'x**2 + p')},
    }
    fs = FuncSpec(args)

    assert fs._user_auxfn_interface['myaux'].split('\n') == [
        'def myaux(self,x,__parsinps__=None):',
        '\tif __parsinps__ is None:',
        '\t\t__parsinps__=self.map_ixs(self.genref)',
        '\treturn self.genref._auxfn_myaux(__parsinps__,x)',
        ''
    ]
Esempio n. 20
0
def test_c_funcspec_for_ds_with_single_var():
    args = {
        'name': 'single_var',
        'targetlang': 'c',
        'vars': ['x'],
        'varspecs': {
            'x': 'x + 1'
        },
    }
    fs = FuncSpec(args)
    assert fs.spec == ('\n'.join([
        'void vfieldfunc(unsigned n_, unsigned np_, double t, double *Y_, double *p_, double *f_, unsigned wkn_, double *wk_, unsigned xvn_, double *xv_){',
        '',
        'f_[0] = x+1;',
        '',
        '}',
        '\n',
    ]), 'vfieldfunc')
Esempio n. 21
0
def test_python_funcspec_with_reuseterms():
    args = {
        'name': 'fs_with_reuseterms',
        'vars': ['x', 'y'],
        'varspecs': {
            'x': 'cy',
            'y': 'sx'
        },
        'reuseterms': {
            'cos(y)': 'cy',
            'sin(x)': 'sx'
        },
    }

    fs = FuncSpec(args)
    assert fs.spec[0].split('\n') == [
        'def _specfn(ds, t, x, parsinps):', '    cy = math.cos(x[1])',
        '    sx = math.sin(x[0])', '    xnew0 = cy ', '    xnew1 = sx ',
        '    return array([xnew0, xnew1])', ''
    ]
Esempio n. 22
0
def test_c_funcspec_inserts_additional_code_in_vfield():
    start = r'fprintf("START\n");'
    end = r'fprintf("END\n");'
    args = {
        'name': 'test_codeinsert',
        'vars': ['x'],
        'pars': ['p'],
        'varspecs': {'x': 'p * x - 1'},
        'fnspecs': {'myaux': (['x'], 'x**2 + p')},
        'codeinsert_start': start,
        'codeinsert_end': end,
        'targetlang': 'c',
    }

    fs = FuncSpec(args)
    assert fs.spec[0].split('\n') == [
        'void vfieldfunc(unsigned n_, unsigned np_, double t, double *Y_, double *p_, double *f_, unsigned wkn_, double *wk_, unsigned xvn_, double *xv_){',
        '/* Verbose code insert -- begin */',
        start,
        '/* Verbose code insert -- end */',
        '',
        '',
        'f_[0] = p*x-1;',
        '',
        '/* Verbose code insert -- begin */',
        end,
        '/* Verbose code insert -- end */',
        '',
        '}',
        '',
        '',
    ]

    assert fs.auxfns['myaux'][0].split('\n') == [
        'double myaux(double __x__, double *p_, double *wk_, double *xv_) {',
        '',
        '',
        'return pow(__x__,2)+p ;',
        '',
        '}'
    ]
Esempio n. 23
0
def test_c_funcspec_for_ds_with_single_var_and_single_param():
    args = {
        'name': 'fun_with_var_and_par',
        'targetlang': 'c',
        'vars': ['x'],
        'pars': ['p'],
        'varspecs': {'x': 'p * x - 1'},
        'fnspecs': {'myaux': (['x'], 'x**2 + p')},
    }
    fs = FuncSpec(args)
    assert fs.spec == (
        '\n'.join([
            'void vfieldfunc(unsigned n_, unsigned np_, double t, double *Y_, double *p_, double *f_, unsigned wkn_, double *wk_, unsigned xvn_, double *xv_){',
            '',
            'f_[0] = p*x-1;',
            '',
            '}',
            '\n',
        ]),
        'vfieldfunc'
    )

    assert fs.auxfns['myaux'][0].split('\n') == [
        'double myaux(double __x__, double *p_, double *wk_, double *xv_) {',
        '',
        '',
        'return pow(__x__,2)+p ;',
        '',
        '}'
    ]

    assert all(fn in fs._pyauxfns.keys() for fn in [
        'getbound',
        'getindex',
        'globalindepvar',
        'heav',
        'if',
        'initcond',
    ])

    assert 'myaux' in fs._pyauxfns.keys()
Esempio n. 24
0
def test_matlab_funcspec_with_reuseterms():
    args = {
        'name': 'fs_with_reuseterms',
        'vars': ['x', 'y'],
        'varspecs': {
            'x': 'cy',
            'y': 'sx'
        },
        'reuseterms': {
            'cos(y)': 'cy',
            'sin(x)': 'sx'
        },
        'targetlang': 'matlab',
    }

    fs = FuncSpec(args)
    assert fs.spec[0].split('\n') == [
        'function [vf_, y_] = vfield(vf_, t_, x_, p_)',
        '% Vector field definition for model fs_with_reuseterms',
        '% Generated by PyDSTool for ADMC++ target',
        '',
        '',
        '% Parameter definitions',
        '',
        '',
        '% Variable definitions',
        '',
        '\tx = x_(1);',
        '\ty = x_(2);',
        '',
        '% reused term definitions ',
        'cy = cos(y);',
        'sx = sin(x);',
        '',
        'y_(1) = cy;',
        'y_(2) = sx;',
        '',
        '',
        '',
    ]
Esempio n. 25
0
def test_python_funcspec_for_ds_with_single_var_and_single_param():
    args = {
        'name': 'fun_with_var_and_par',
        'vars': ['x'],
        'pars': ['p'],
        'varspecs': {
            'x': 'p * x - 1'
        },
        'fnspecs': {
            'myaux': (['x'], 'x**2 + p')
        },
    }
    fs = FuncSpec(args)
    assert fs.spec == ('\n'.join([
        'def _specfn(ds, t, x, parsinps):',
        '    xnew0 = parsinps[0] * x[0] - 1 ', '    return array([xnew0])\n'
    ]), '_specfn')

    assert fs.auxfns['myaux'][0].split('\n') == [
        'def _auxfn_myaux(ds, parsinps, x):',
        '    return math.pow(x,2)+parsinps[0]'
    ]
Esempio n. 26
0
def test_c_funspec_with_powers():
    """Regression test for https://github.com/robclewley/pydstool/issues/90"""
    args = {
        'name': 'fs_with_powers',
        'targetlang': 'c',
        'vars': ['x', 'y'],
        'varspecs': {
            'y': 'x',
            'x': 'y + x**3 - x^4'
        },
    }
    fs = FuncSpec(args)
    assert fs.spec[0].split('\n') == [
        'void vfieldfunc(unsigned n_, unsigned np_, double t, double *Y_, double *p_, double *f_, unsigned wkn_, double *wk_, unsigned xvn_, double *xv_){',
        '',
        'f_[0] = y+pow(x,3)-pow(x,4);',
        'f_[1] = x;',
        '',
        '}',
        '',
        '',
    ]
Esempio n. 27
0
def test_c_funcspec_with_reuseterms():
    args = {
        'name': 'fs_with_reuseterms',
        'targetlang': 'c',
        'vars': ['x', 'y'],
        'varspecs': {
            'x': 'cy + sx',
            'y': 'sx * cy'
        },
        'reuseterms': {
            'cos(y)': 'cy',
            'sin(x)': 'sx',
            'sx * cy': 'sc'
        },
    }

    fs = FuncSpec(args)
    assert fs.spec[0].split('\n') == [
        'void vfieldfunc(unsigned n_, unsigned np_, double t, double *Y_, double *p_, double *f_, unsigned wkn_, double *wk_, unsigned xvn_, double *xv_){',
        '/* reused term definitions */', 'double cy = cos(y);',
        'double sx = sin(x);', 'double sc = sx*cy;', '', 'f_[0] = cy+sx;',
        'f_[1] = sc;', '', '}', '', ''
    ]
Esempio n. 28
0
def test_matlab_funcspec_inserts_additional_code_in_vfield():
    start = 'disp("START");'
    end = 'disp("END");'
    args = {
        'name': 'test_codeinsert',
        'targetlang': 'matlab',
        'vars': ['x'],
        'pars': ['p'],
        'varspecs': {
            'x': 'p * x - 1'
        },
        'fnspecs': {
            'myaux': (['x'], 'x**2 + p')
        },
        'codeinsert_start': start,
        'codeinsert_end': end,
    }
    fs = FuncSpec(args)
    assert fs.spec[0].split('\n') == [
        'function [vf_, y_] = vfield(vf_, t_, x_, p_)',
        '% Vector field definition for model test_codeinsert',
        '% Generated by PyDSTool for ADMC++ target',
        '',
        '',
        '% Parameter definitions',
        '',
        '\tp = p_(1);',
        '',
        '% Variable definitions',
        '',
        '\tx = x_(1);',
        '',
        '% Verbose code insert -- begin ',
        start,
        '% Verbose code insert -- end ',
        '',
        '',
        'y_(1) = p * x - 1;',
        '',
        '% Verbose code insert -- begin ',
        end,
        '% Verbose code insert -- end ',
        '',
        '',
        '',
    ]

    assert fs.auxfns['myaux'][0].split('\n') == [
        'function y_ = myaux(x__,  p_)',
        '% Auxilliary function myaux for model test_codeinsert',
        '% Generated by PyDSTool for ADMC++ target', '', '',
        '% Parameter definitions', '', '\tp = p_(1);', ' ', '', '',
        'y_ = x__^2+p;', '', ''
    ]

    assert all(fn in fs._pyauxfns.keys() for fn in [
        'getbound',
        'getindex',
        'globalindepvar',
        'heav',
        'if',
        'initcond',
    ])

    assert 'myaux' in fs._pyauxfns.keys()
Esempio n. 29
0
def test_funcspec_raises_exception_if_vars_is_neither_str_nor_iterable():
    with pytest.raises(TypeError):
        FuncSpec({'vars': 1, 'varspecs': {}})
Esempio n. 30
0
def test_matlab_funcspec_with_reuseterms_and_aux_funcs():
    args = {
        'name': 'fs_with_reuseterms',
        'vars': ['x', 'y'],
        'varspecs': {
            'x': 'cy + myaux(x)',
            'y': 'sx'
        },
        'reuseterms': {
            'cos(y)': 'cy',
            'sin(x)': 'sx'
        },
        'targetlang': 'matlab',
        'fnspecs': {
            'myaux': (['x'], 'x**2'),
            'aux_with_reusterms': (['x', 'y'], 'pow(x, 3) + cy'),
            'aux_with_reuseterms_and_myaux': (['x', 'y'], 'sx - myaux(x)'),
        },
    }

    fs = FuncSpec(args)
    assert fs.spec[0].split('\n') == [
        'function [vf_, y_] = vfield(vf_, t_, x_, p_)',
        '% Vector field definition for model fs_with_reuseterms',
        '% Generated by PyDSTool for ADMC++ target',
        '',
        '',
        '% Parameter definitions',
        '',
        '',
        '% Variable definitions',
        '',
        '\tx = x_(1);',
        '\ty = x_(2);',
        '',
        '% reused term definitions ',
        'cy = cos(y);',
        'sx = sin(x);',
        '',
        'y_(1) = cy + myaux(x, p_);',
        'y_(2) = sx;',
        '',
        '',
        '',
    ]

    assert fs.auxfns['myaux'][0].split('\n') == [
        'function y_ = myaux(x__,  p_)',
        '% Auxilliary function myaux for model fs_with_reuseterms',
        '% Generated by PyDSTool for ADMC++ target', '', '',
        '% Parameter definitions', '', ' ', '', '', 'y_ = x__^2;', '', ''
    ]

    assert fs.auxfns['myaux'][1].split('\n') == [
        'function y_ = myaux(x__,  p_)',
        '% Auxilliary function myaux for model fs_with_reuseterms',
        '% Generated by PyDSTool for ADMC++ target',
        '',
        '',
    ]

    assert fs.auxfns['aux_with_reusterms'][0].split('\n') == [
        'function y_ = aux_with_reusterms(x__, y__,  p_)',
        '% Auxilliary function aux_with_reusterms for model fs_with_reuseterms',
        '% Generated by PyDSTool for ADMC++ target', '', '',
        '% Parameter definitions', '', ' ', '', '',
        '% reused term definitions ', 'cy = cos(y__);', '', 'y_ = x__^3+cy;',
        '', ''
    ]

    assert fs.auxfns['aux_with_reuseterms_and_myaux'][0].split('\n') == [
        'function y_ = aux_with_reuseterms_and_myaux(x__, y__,  p_)',
        '% Auxilliary function aux_with_reuseterms_and_myaux for model fs_with_reuseterms',
        '% Generated by PyDSTool for ADMC++ target', '', '',
        '% Parameter definitions', '', ' ', '', '',
        '% reused term definitions ', 'sx = sin(x__);', '',
        'y_ = sx - myaux(x__, p_);', '', ''
    ]