def _test_NativeSys__first_step_cb_source_code(NativeSys, log10myconst, should_succeed, forgive=20, **kwargs): dec3 = _get_decay3() odesys = NativeSys.from_other( dec3, namespace_override={ 'p_first_step': 'return good_const()*y[0];', 'p_anon': 'double good_const(){ return std::pow(10, %.5g); }' % log10myconst }, namespace_extend={'p_includes': ['<cmath>']}) y0, k = [.7, 0, 0], [1e23, 2, 3.] xout, yout, info = odesys.integrate(5, y0, k, integrator='native', **kwargs) ref = np.array(bateman_full(y0, k, xout - xout[0], exp=np.exp)).T allclose_kw = dict(atol=kwargs['atol'] * forgive, rtol=kwargs['rtol'] * forgive) if should_succeed is None: assert not np.allclose(yout, ref, **allclose_kw) else: assert info['success'] == should_succeed info['nfev'] > 10 and info['nfev'] > 1 and info['time_cpu'] < 100 if should_succeed: assert np.allclose(yout, ref, **allclose_kw)
def _test_Decay_nonnegative(NativeSys): odesys = NativeSys.from_other(_get_decay3(lower_bounds=[0] * 3)) y0, k = [3., 2., 1.], [3.5, 2.5, 0] xout, yout, info = odesys.integrate([1e-10, 1], y0, k, integrator='native') ref = np.array(bateman_full(y0, k, xout - xout[0], exp=np.exp)).T assert info['success'] and info['nfev'] > 10 and info['nfev'] > 1 and info[ 'time_cpu'] < 100 assert np.allclose(yout, ref) and np.allclose(np.sum(yout, axis=1), sum(y0))
def _test_NativeSys__first_step_cb(NativeSys, forgive=20): dec3 = _get_decay3() dec3.first_step_expr = dec3.dep[0] * 1e-30 odesys = NativeSys.from_other(dec3) y0, k = [.7, 0, 0], [1e23, 2, 3.] kwargs = dict(atol=1e-8, rtol=1e-8) xout, yout, info = odesys.integrate(5, y0, k, integrator='native', **kwargs) ref = np.array(bateman_full(y0, k, xout - xout[0], exp=np.exp)).T allclose_kw = dict(atol=kwargs['atol'] * forgive, rtol=kwargs['rtol'] * forgive) assert info['success'] and info['nfev'] > 10 and info['nfev'] > 1 and info[ 'time_cpu'] < 100 assert np.allclose(yout, ref, **allclose_kw)
def _test_PartiallySolvedSystem_Native(NativeSys, integrator): class TransformedNativeSys(TransformedSys, NativeSys): pass logexp = get_logexp(1, 1e-24) NativeLogLogSys = symmetricsys(logexp, logexp, SuperClass=TransformedNativeSys) odesys = _get_decay3(lower_bounds=[0, 0, 0], linear_invariants=[[1, 1, 1]]) n_sys = NativeSys.from_other(odesys) assert odesys.linear_invariants == n_sys.linear_invariants scaledsys = ScaledSys.from_other(odesys, dep_scaling=42) ns_sys = NativeSys.from_other(scaledsys) partsys = PartiallySolvedSystem.from_linear_invariants(scaledsys) np_sys = NativeSys.from_other(partsys) LogLogSys = symmetricsys(logexp, logexp) ll_scaledsys = LogLogSys.from_other(scaledsys) ll_partsys = LogLogSys.from_other(partsys) nll_scaledsys = NativeLogLogSys.from_other(scaledsys) nll_partsys = NativeLogLogSys.from_other(partsys) assert len(ll_scaledsys.nonlinear_invariants) > 0 assert ll_scaledsys.nonlinear_invariants == nll_scaledsys.nonlinear_invariants y0 = [3.3, 2.4, 1.5] k = [3.5, 2.5, 0] systems = [ odesys, n_sys, scaledsys, ns_sys, partsys, np_sys, ll_scaledsys, ll_partsys, nll_scaledsys, nll_partsys ] for idx, system in enumerate(systems): result = system.integrate([0, .3, .5, .7, .9, 1.3], y0, k, integrator=integrator, atol=1e-9, rtol=1e-9) ref = np.array( bateman_full(y0, k, result.xout - result.xout[0], exp=np.exp)).T assert result.info['success'] assert np.allclose(result.yout, ref)
def _test_NativeSys__get_dx_max_source_code(NativeSys, forgive=20, **kwargs): dec3 = _get_decay3() odesys = NativeSys.from_other( dec3, namespace_override={ 'p_get_dx_max': 'AnyODE::ignore(y); return 1e-4*x + 1e-3;', }) y0, k = [.7, 0, 0], [7., 2, 3.] xout, yout, info = odesys.integrate(1, y0, k, integrator='native', get_dx_max_factor=1.0, **kwargs) ref = np.array(bateman_full(y0, k, xout - xout[0], exp=np.exp)).T allclose_kw = dict(atol=kwargs['atol'] * forgive, rtol=kwargs['rtol'] * forgive) assert np.allclose(yout, ref, **allclose_kw) assert info['success'] assert info['nfev'] > 10 if 'n_steps' in info: print(info['n_steps']) assert 750 < info['n_steps'] <= 1000
def _get_transformed_partially_solved_system(NativeSys, multiple=False): odesys = _get_decay3() if multiple: def substitutions(x0, y0, p0, be): analytic0 = y0[0] * be.exp(-p0[0] * (odesys.indep - x0)) analytic2 = y0[0] + y0[1] + y0[2] - analytic0 - odesys.dep[1] return {odesys.dep[0]: analytic0, odesys.dep[2]: analytic2} else: def substitutions(x0, y0, p0, be): return { odesys.dep[0]: y0[0] * sp.exp(-p0[0] * (odesys.indep - x0)) } partsys = PartiallySolvedSystem(odesys, substitutions) class TransformedNativeSys(TransformedSys, NativeSys): pass LogLogSys = symmetricsys(get_logexp(), get_logexp(), SuperClass=TransformedNativeSys) return LogLogSys.from_other(partsys)