def test_ode_system(tb_args): fvarspecs = { "w": "k*w + a*itable + sin(t) + myauxfn1(t)*myauxfn2(w)", 'aux_wdouble': 'w*2 + globalindepvar(t)', 'aux_other': 'myauxfn1(2*t) + initcond(w)' } fnspecs = {'myauxfn1': (['t'], '2.5*cos(3*t)'), 'myauxfn2': (['w'], 'w/2')} DSargs = { 'tdomain': [0.1, 2.1], 'pars': { 'k': 2, 'a': -0.5 }, 'inputs': { 'itable': InterpolateTable(tb_args).variables['x1'] }, 'auxvars': ['aux_wdouble', 'aux_other'], 'algparams': { 'init_step': 0.01, 'strict': False }, 'checklevel': 2, 'name': 'ODEtest', 'fnspecs': fnspecs, 'varspecs': fvarspecs } testODE = Vode_ODEsystem(DSargs) assert testODE.pars == DSargs['pars'] assert (not testODE.defined) testODE.set(ics={'w': 3.0}, tdata=[0.11, 2.1]) testtraj = testODE.compute('test1') assert testODE.defined assert_almost_equal(testtraj(0.5, 'w'), 6.05867901304, 3) assert_almost_equal(testtraj(0.2, 'aux_other'), 3.90581993688, 3) assert testODE.indepvariable.depdomain == Interval('t', float64, [0.11, 2.1]) assert testODE.diagnostics.hasWarnings() assert testODE.diagnostics.findWarnings(21) != [] # Now adding a terminating co-ordinate threshold event... ev_args = { 'name': 'threshold', 'eventtol': 1e-4, 'eventdelay': 1e-5, 'starttime': 0, 'active': True, # = default 'term': True, 'precise': True # = default } thresh_ev = Events.makePythonStateZeroCrossEvent('w', 20, 1, ev_args) testODE.eventstruct.add(thresh_ev) traj2 = testODE.compute('test2') assert testODE.diagnostics.hasWarnings() assert testODE.diagnostics.findWarnings(10) != [] print(testODE.diagnostics.showWarnings()) assert_almost_equal(traj2.getEventTimes()['threshold'][0], 1.51449456, 3) assert testODE.indepvariable.depdomain == Interval('t', float64, [0.11, 2.1])
def test_ode_system(tb_args): fvarspecs = { "w": "k*w + a*itable + sin(t) + myauxfn1(t)*myauxfn2(w)", 'aux_wdouble': 'w*2 + globalindepvar(t)', 'aux_other': 'myauxfn1(2*t) + initcond(w)' } fnspecs = { 'myauxfn1': (['t'], '2.5*cos(3*t)'), 'myauxfn2': (['w'], 'w/2') } DSargs = { 'tdomain': [0.1, 2.1], 'pars': {'k': 2, 'a': -0.5}, 'inputs': {'itable': InterpolateTable(tb_args).variables['x1']}, 'auxvars': ['aux_wdouble', 'aux_other'], 'algparams': {'init_step': 0.01, 'strict': False}, 'checklevel': 2, 'name': 'ODEtest', 'fnspecs': fnspecs, 'varspecs': fvarspecs } testODE = Vode_ODEsystem(DSargs) assert testODE.pars == DSargs['pars'] assert (not testODE.defined) testODE.set( ics={'w': 3.0}, tdata=[0.11, 2.1] ) testtraj = testODE.compute('test1') assert testODE.defined assert_almost_equal(testtraj(0.5, 'w'), 6.05867901304, 3) assert_almost_equal(testtraj(0.2, 'aux_other'), 3.90581993688, 3) assert testODE.indepvariable.depdomain == Interval( 't', float64, [0.11, 2.1]) assert testODE.diagnostics.hasWarnings() assert testODE.diagnostics.findWarnings(21) != [] # Now adding a terminating co-ordinate threshold event... ev_args = { 'name': 'threshold', 'eventtol': 1e-4, 'eventdelay': 1e-5, 'starttime': 0, 'active': True, # = default 'term': True, 'precise': True # = default } thresh_ev = Events.makePythonStateZeroCrossEvent('w', 20, 1, ev_args) testODE.eventstruct.add(thresh_ev) traj2 = testODE.compute('test2') assert testODE.diagnostics.hasWarnings() assert testODE.diagnostics.findWarnings(10) != [] print(testODE.diagnostics.showWarnings()) assert_almost_equal(traj2.getEventTimes()['threshold'][0], 1.51449456, 3) assert testODE.indepvariable.depdomain == Interval( 't', float64, [0.11, 2.1])
def test_vode_events_with_external_input(my_input): """ Test Vode_ODEsystem with events involving external inputs. Robert Clewley, September 2006. """ xs = ['x1', 'x2', 'x3'] ys = [0, 0.5, 1] fvarspecs = {"w": "k*w + pcwfn(sin(t)) + myauxfn1(t)*myauxfn2(w)", 'aux_wdouble': 'w*2 + globalindepvar(t)', 'aux_other': 'myauxfn1(2*t) + initcond(w)'} fnspecs = {'myauxfn1': (['t'], '2.5*cos(3*t)'), 'myauxfn2': (['w'], 'w/2'), 'pcwfn': makeMultilinearRegrFn('x', xs, ys)} # targetlang is optional if the default python target is desired DSargs = args(fnspecs=fnspecs, name='ODEtest') DSargs.varspecs = fvarspecs DSargs.tdomain = [0.1, 2.1] DSargs.pars = {'k': 2, 'a': -0.5, 'x1': -3, 'x2': 0.5, 'x3': 1.5} DSargs.vars = 'w' DSargs.inputs = {'in': my_input.variables['example_input']} DSargs.algparams = {'init_step': 0.01} DSargs.checklevel = 2 testODE = Vode_ODEsystem(DSargs) assert not testODE.defined testODE.set(ics={'w': 3.0}, tdata=[0.11, 2.1]) traj1 = testODE.compute('traj1') assert testODE.defined assert_almost_equal(traj1(0.5, 'w'), 8.9771499, 5) assert not testODE.diagnostics.hasWarnings() assert_almost_equal(traj1(0.2, ['aux_other']), 3.905819936, 5) print("\nNow adding a terminating co-ordinate threshold event") print(" and non-terminating timer event") # Show off the general-purpose, language-independent event creator: # 'makeZeroCrossEvent' ev_args_nonterm = {'name': 'monitor', 'eventtol': 1e-4, 'eventdelay': 1e-5, 'starttime': 0, 'active': True, 'term': False, 'precise': True} thresh_ev_nonterm = Events.makeZeroCrossEvent('in', 0, ev_args_nonterm, inputnames=['in']) # Now show use of the python-target specific creator: # 'makePythonStateZeroCrossEvent', which is also only # able to make events for state variable threshold crossings ev_args_term = {'name': 'threshold', 'eventtol': 1e-4, 'eventdelay': 1e-5, 'starttime': 0, 'active': True, 'term': True, 'precise': True} thresh_ev_term = Events.makePythonStateZeroCrossEvent('w', 20, 1, ev_args_term) testODE.eventstruct.add([thresh_ev_nonterm, thresh_ev_term]) print("Recomputing trajectory:") print("traj2 = testODE.compute('traj2')") traj2 = testODE.compute('traj2') print("\ntestODE.diagnostics.showWarnings() => ") testODE.diagnostics.showWarnings() print("\ntraj2.indepdomain.get() => ", traj2.indepdomain.get()) indep1 = traj2.indepdomain[1] assert indep1 < 1.17 and indep1 > 1.16 mon_evs_found = testODE.getEvents('monitor') assert len(mon_evs_found) == 1