Exemple #1
0
def test_explicit_functional_trajectory():
    """Explicit functional trajectory 'sin_gen' computes sin(t*speed)"""

    sine_time_ev = makeZeroCrossEvent(
        't-2',
        1,
        {'name': 'sine_time_test', 'term': True}
    )

    # Make 'xdomain' argument smaller than known limits for sine wave:
    # [-1.001, 0.7]
    ef_args = {
        'tdomain': [-50, 50],
        'pars': {'speed': 1},
        'xdomain': {'s': [-1., 0.7]},
        'name': 'sine',
        'globalt0': 0.4,
        'pdomain': {'speed': [0, 200]},
        'varspecs': {'s': "sin(globalindepvar(t)*speed)"},
        'events': sine_time_ev
    }
    sin_gen = ExplicitFnGen(ef_args)
    sintraj = sin_gen.compute('sinewave')
    assert sintraj(0.0, checklevel=2)['s'] - 0.38941834 < 1e-7

    # Expect problem calling at t=0.8...
    with pytest.raises(PyDSTool_BoundsError):
        sintraj(0.8, checklevel=2)
    sin_gen.set(xdomain={'s': [-1., 1.]})
    sintraj2 = sin_gen.compute('sinewave2')
    # this doesn't raise an exception now
    sintraj2(0.8, checklevel=2)
    evts = sintraj.getEventTimes()
    assert len(evts) == 1
Exemple #2
0
def test_explicitfngen():
    """Test of Explicit and Implicit Function generators, global time, and deletion"""

    ev_args = {'name': 'threshold',
               'eventtol': 1e-4,
               'eventdelay': 1e-5,
               'starttime': 0,
               'active': True,
               'term': True,
               'precise': True}
    thresh_ev = Events.makePythonStateZeroCrossEvent('t', 20, 1, ev_args)

    DSargs = {'tdomain': [-50, 50],
            'pars': {'speed': 1},
            'xdomain': {'s': [-1., 1.]},
            'name': 'sine',
            'globalt0': 0.4,
            'pdomain': {'speed': [0, 200]},
            'varspecs': {'s': "sin(globalindepvar(t)*speed)"},
            'events': thresh_ev}
    sin_gen = ExplicitFnGen(DSargs)
    sintraj1 = sin_gen.compute('sine1')
    assert sintraj1.globalt0 == 0.4
    assert sintraj1(0) == sin(0.4)

    sin_gen.set(pars={'speed': 2})
    sintraj2 = sin_gen.compute('sine2')

    # sintraj2 independent variable domain truncated at terminal event
    assert allclose(sin_gen.getEventTimes()['threshold'], 20+sintraj1.globalt0)
    assert sintraj2.indepdomain[0] == -50
    assert abs(sintraj2.indepdomain[1] - 20) < 1e-4
def test_saveload_explicitfngen():
    """Test pickling for saving and loading 'ExplicitFnGen'"""

    args = {
        'tdomain': [-50, 50],
        'pars': {
            'speed': 1
        },
        'xdomain': {
            's': [-1., 1.]
        },
        'name': 'sine',
        'globalt0': 0.4,
        'pdomain': {
            'speed': [0, 200]
        },
        'varspecs': {
            's': "sin(globalindepvar(t)*speed)"
        }
    }
    sin_gen = ExplicitFnGen(args)
    sintraj1 = sin_gen.compute('sine1')
    sin_gen.set(pars={'speed': 2})
    sintraj2 = sin_gen.compute('sine2')
    saveObjects([sin_gen, sintraj1, sintraj2], 'temp_objects.pkl', True)
    objs_sin = loadObjects('temp_objects.pkl')
    assert sintraj1(0.55) == objs_sin[1](0.55)
    assert sintraj2(0.55) == objs_sin[2](0.55)
    os.remove('temp_objects.pkl')
Exemple #4
0
def test_explicitfngen():
    """Test of Explicit and Implicit Function generators, global time, and deletion"""

    ev_args = {
        'name': 'threshold',
        'eventtol': 1e-4,
        'eventdelay': 1e-5,
        'starttime': 0,
        'active': True,
        'term': True,
        'precise': True
    }
    thresh_ev = Events.makePythonStateZeroCrossEvent('t', 20, 1, ev_args)

    DSargs = {
        'tdomain': [-50, 50],
        'pars': {
            'speed': 1
        },
        'xdomain': {
            's': [-1., 1.]
        },
        'name': 'sine',
        'globalt0': 0.4,
        'pdomain': {
            'speed': [0, 200]
        },
        'varspecs': {
            's': "sin(globalindepvar(t)*speed)"
        },
        'events': thresh_ev
    }
    sin_gen = ExplicitFnGen(DSargs)
    sintraj1 = sin_gen.compute('sine1')
    assert sintraj1.globalt0 == 0.4
    assert sintraj1(0) == sin(0.4)

    sin_gen.set(pars={'speed': 2})
    sintraj2 = sin_gen.compute('sine2')

    # sintraj2 independent variable domain truncated at terminal event
    assert allclose(sin_gen.getEventTimes()['threshold'],
                    20 + sintraj1.globalt0)
    assert sintraj2.indepdomain[0] == -50
    assert abs(sintraj2.indepdomain[1] - 20) < 1e-4
def test_explicit_functional_trajectory():
    """Explicit functional trajectory 'sin_gen' computes sin(t*speed)"""

    sine_time_ev = makeZeroCrossEvent('t-2', 1, {
        'name': 'sine_time_test',
        'term': True
    })

    # Make 'xdomain' argument smaller than known limits for sine wave:
    # [-1.001, 0.7]
    ef_args = {
        'tdomain': [-50, 50],
        'pars': {
            'speed': 1
        },
        'xdomain': {
            's': [-1., 0.7]
        },
        'name': 'sine',
        'globalt0': 0.4,
        'pdomain': {
            'speed': [0, 200]
        },
        'varspecs': {
            's': "sin(globalindepvar(t)*speed)"
        },
        'events': sine_time_ev
    }
    sin_gen = ExplicitFnGen(ef_args)
    sintraj = sin_gen.compute('sinewave')
    assert sintraj(0.0, checklevel=2)['s'] - 0.38941834 < 1e-7

    # Expect problem calling at t=0.8...
    with pytest.raises(PyDSTool_BoundsError):
        sintraj(0.8, checklevel=2)
    sin_gen.set(xdomain={'s': [-1., 1.]})
    sintraj2 = sin_gen.compute('sinewave2')
    # this doesn't raise an exception now
    sintraj2(0.8, checklevel=2)
    evts = sintraj.getEventTimes()
    assert len(evts) == 1
def test_saveload_explicitfngen():
    """Test pickling for saving and loading 'ExplicitFnGen'"""

    args = {
        'tdomain': [-50, 50],
        'pars': {'speed': 1},
        'xdomain': {'s': [-1., 1.]},
        'name': 'sine',
        'globalt0': 0.4,
        'pdomain': {'speed': [0, 200]},
        'varspecs': {'s': "sin(globalindepvar(t)*speed)"}
    }
    sin_gen = ExplicitFnGen(args)
    sintraj1 = sin_gen.compute('sine1')
    sin_gen.set(pars={'speed': 2})
    sintraj2 = sin_gen.compute('sine2')
    saveObjects([sin_gen, sintraj1, sintraj2], 'temp_objects.pkl', True)
    objs_sin = loadObjects('temp_objects.pkl')
    assert sintraj1(0.55) == objs_sin[1](0.55)
    assert sintraj2(0.55) == objs_sin[2](0.55)
    os.remove('temp_objects.pkl')