예제 #1
0
 def setUp(self):
     DDE = jitcdde(f)
     DDE.set_integration_parameters(**test_parameters)
     DDE.compile_C(extra_compile_args=compile_args)
     DDE.add_past_points(get_past_points())
     DDE.integrate(T)
     self.result = DDE.get_state()
예제 #2
0
파일: dde.py 프로젝트: alkasm/TimeSynth
 def __init__(self, tau=17., n=10., beta=0.2, gamma=0.1, initial_condition=None, burn_in=500):
     self.vectorizable = True
     
     # Set system of equations
     t, y, _, _, _ = provide_advanced_symbols()
     f = [- gamma * y(0) + beta * y(0, t-tau) / (1.0 + y(0, t-tau) ** n)]
     self.dde = jitcdde(f)
     
     # Set initial condition
     if initial_condition is None:
         y_initial = 0.5
         dy = lambda y: -gamma * y + beta * y / (1.0 + y ** n)
         dy_initial = dy(y_initial)
         self.dde.add_past_point(0.0, np.array([y_initial]), np.array([dy_initial]))
         self.dde.add_past_point(tau, np.array([1.0]), np.array([0.0]))
     else:
         for condition in initial_condition:
             time, value, derivative = condition
             self.dde.add_past_point(time, np.array([value]), np.array([derivative]))
     
     # Prepare DDE
     self.dde.generate_f_lambda()
     self.dde.set_integration_parameters()
     
     # Run burn_in
     self.burn_in = burn_in
     self.dde.integrate_blindly(self.burn_in)
예제 #3
0
def initializeJiTCDDE(self, y_prime_jitcdde, past_function, arg_max_delay,
                      arg_times_of_interest, c_backend):
    """
	Utility function to avoid repeated code.
	Sets up the "jitcdde" class to integrate over intervals.
	For more information see: https://jitcdde.readthedocs.io/en/stable/#the-main-class
	This function is used by the solve_dde_for_t() function.
	"""
    DDE = jitcdde.jitcdde(y_prime_jitcdde, max_delay=arg_max_delay)
    DDE.past_from_function(past_function,
                           times_of_interest=arg_times_of_interest)

    if c_backend == False:
        DDE.generate_lambdas()

    print()

    # print("state:")
    # x = DDE.get_state()

    # for y in x:
    # print(y)

    # print()

    return DDE
예제 #4
0
    def __init__(self,
                 tau=17.,
                 n=10.,
                 beta=0.2,
                 gamma=0.1,
                 initial_condition=None,
                 burn_in=500):
        self.vectorizable = True

        # Set system of equations
        f = [-gamma * y(0) + beta * y(0, t - tau) / (1.0 + y(0, t - tau)**n)]
        self.dde = jitcdde(f)

        # Set initial condition
        if initial_condition is None:
            y_initial = 0.5
            dy = lambda y: -gamma * y + beta * y / (1.0 + y**n)
            dy_initial = dy(y_initial)
            self.dde.add_past_point(0.0, np.array([y_initial]),
                                    np.array([dy_initial]))
            self.dde.add_past_point(tau, np.array([1.0]), np.array([0.0]))
        else:
            for condition in initial_condition:
                time, value, derivative = condition
                self.dde.add_past_point(time, np.array([value]),
                                        np.array([derivative]))

        # Prepare DDE
        self.dde.generate_lambdas()
        self.dde.set_integration_parameters()

        # Run burn_in
        self.burn_in = burn_in
        self.dde.integrate_blindly(self.burn_in)
def return_differential_equations(delta, k):
    """Returns the differential equations"""
    initially_susceptible = 99980
    initially_infected = 20
    beta = 0.000001
    gamma = 0.0002
    # pylint: disable=invalid-name
    mu = 0.1
    d_one = 0.005
    d_two = 0.0001
    tau = 5
    list_of_odes = [
        gamma * y(4) - beta * k * y(0) * y(2),
        beta * k * y(0) * y(2) - beta * k * y(0, t - tau) * y(2, t - tau),
        beta * k * y(0, t - tau) * y(2, t - tau) - d_one * y(2) - delta * y(2),
        delta * y(2) - d_two * y(3) - mu * y(3), mu * y(3) - gamma * y(4)
    ]

    delayed_differential_equation = jitcdde(list_of_odes)
    delayed_differential_equation.add_past_point(
        -10.0, [initially_susceptible, 0, initially_infected, 0, 0],
        [0, 0, 0, 0, 0])
    delayed_differential_equation.add_past_point(
        0, [initially_susceptible, 0, initially_infected, 0, 0],
        [0, 0, 0, 0, 0])

    delayed_differential_equation.step_on_discontinuities()
    return delayed_differential_equation
def display_endemic_progress():
    """Display how a typical endemic progresses in time"""
    beta = 0.000001
    k = 6
    gamma = 0.0002
    delta = 0.3
    # pylint: disable=invalid-name
    mu = 0.1
    d_one = 0.005
    d_two = 0.001
    tau = 5
    initially_susceptible = 99980
    initially_infected = 20

    list_of_odes = [
        gamma * y(4) - beta * k * y(0) * y(2),
        beta * k * y(0) * y(2) - beta * k * y(0, t - tau) * y(2, t - tau),
        beta * k * y(0, t - tau) * y(2, t - tau) - d_one * y(2) - delta * y(2),
        delta * y(2) - d_two * y(3) - mu * y(3), mu * y(3) - gamma * y(4)
    ]

    delayed_differential_equation = jitcdde(list_of_odes)
    delayed_differential_equation.add_past_point(
        -10.0, [initially_susceptible, 0, initially_infected, 0, 0],
        [0, 0, 0, 0, 0])
    delayed_differential_equation.add_past_point(
        0, [initially_susceptible, 0, initially_infected, 0, 0],
        [0, 0, 0, 0, 0])

    delayed_differential_equation.step_on_discontinuities()

    stoptime = 180
    numpoints = 18000
    times = delayed_differential_equation.t + \
        np.linspace(1, stoptime, numpoints)

    data = []
    for time in times:
        data.append(delayed_differential_equation.integrate(time))

    npdata = np.array(data)
    susceptible = npdata[:, 0]
    exposed = npdata[:, 1]
    infected = npdata[:, 2]
    quarantined = npdata[:, 3]
    recovered = npdata[:, 4]

    fig = plt.figure()
    subplot = fig.add_subplot(1, 1, 1)
    subplot.plot(times, susceptible, label='Susceptible people')
    subplot.plot(times, exposed, '-', label='Exposed people')
    subplot.plot(times, infected, '--', label='Infected people')
    subplot.plot(times, quarantined, '-.', label='Quarantined people')
    subplot.plot(times, recovered, ':', label='Recovered people')
    subplot.set_xlabel('Days from outbreak')
    subplot.set_ylabel('Population')
    subplot.legend(loc='best')
    plt.savefig('all_compartments.svg', format="svg", dpi=1200)
예제 #7
0
	def test_jump(self):
		DDE = jitcdde(f)
		DDE.set_integration_parameters(**test_parameters)
		DDE.add_past_points(get_past_points())
		DDE.compile_C(extra_compile_args=compile_args)
		old_state = DDE.integrate(T)
		
		width = 1e-5
		change = np.random.normal(0,5,n)
		DDE.jump(change,T,width)
		past = DDE.get_state()
		
		assert past[-2][0] == T
		assert past[-1][0] == T+width
		assert_allclose( past[-2][1], old_state )
		assert_allclose( past[-1][1], old_state+change, rtol=1e-3 )
예제 #8
0
	def setUpClass(self):
		self.DDE = jitcdde(f_with_tiny_delay)
		self.DDE.set_integration_parameters(pws_fuzzy_increase=False, **test_parameters)
예제 #9
0
if __name__ == "__main__":
	# example-start
	from jitcdde import jitcdde, y, t
	from numpy import pi, arange, random, max
	from symengine import sin
	
	n = 100
	ω = 1
	c = 42
	q = 0.05
	A = random.choice( [1,0], size=(n,n), p=[q,1-q] )
	τ = random.uniform( pi/5, 2*pi, size=(n,n) )
	
	def kuramotos():
		for i in range(n):
			yield ω + c/(n-1)*sum(
						sin(y(j,t-τ[i,j])-y(i))
						for j in range(n)
						if A[j,i]
					)
	
	I = jitcdde(kuramotos,n=n,verbose=False)
	I.set_integration_parameters(rtol=0,atol=1e-5)
	
	I.constant_past( random.uniform(0,2*pi,n), time=0.0 )
	I.integrate_blindly( max(τ) , 0.1 )
	
	for time in I.t + arange(0,400,0.2):
		print(*I.integrate(time) % (2*pi))
예제 #10
0
#!/usr/bin/python3
# -*- coding: utf-8 -*-

from jitcdde import jitcdde, y, t
import numpy

f = [ -y(0,t-2-y(0)**2) + 5 ]
DDE = jitcdde(f,max_delay=1e10)

eps = 0.1
DDE.add_past_point(-2.0    , [ 4.5], [0.0])
DDE.add_past_point(-1.0-eps, [ 4.5], [0.0])
DDE.add_past_point(-1.0+eps, [-0.5], [0.0])
DDE.add_past_point( 0.0    , [-0.5], [0.0])

DDE.integrate_blindly(0.01)

data = []
for time in numpy.arange(0,2.0,0.001)+DDE.t:
	data.append([time, DDE.integrate(time)])
numpy.savetxt("timeseries.dat", data)

예제 #11
0
import numpy as np

t, y, current_y, past_y, anchors = provide_advanced_symbols()

omega = np.random.normal(0.89, 0.0089, 2)
k = 0.25
delay = 4.5

f = [
    omega[0] * (-y(1) - y(2)), omega[0] * (y(0) + 0.165 * y(1)),
    omega[0] * (0.2 + y(2) * (y(0) - 10.0)),
    omega[1] * (-y(4) - y(5)) + k * (y(0, t - delay) - y(3)),
    omega[1] * (y(3) + 0.165 * y(4)), omega[1] * (0.2 + y(5) * (y(3) - 10.0))
]

DDE = jitcdde(f, max_delay=delay)

start_state = np.random.uniform(-0.5, 0.5, 6)

DDE.add_past_point(-delay, start_state, np.zeros(6))
DDE.add_past_point(0.0, start_state, np.zeros(6))

DDE.generate_f_C()
DDE.set_integration_parameters(rtol=1e-5)

pre_T = 100
DDE.integrate_blindly(pre_T)

dt = 0.1
with open("two_roesslers.dat", "w") as ausgabe:
    for T in np.arange(pre_T + dt, pre_T + 1000, dt):
예제 #12
0
def E(t):
    if abs(t) < 1:
        return math.exp(-1 / (1 - t**2))
    else:
        return 0


def initial(t):
    value = [500 * E(t + 1.5)]
    # print(t, value)
    return value


f = [p1 * y(0, t - tau)]
DDE = jitcdde(f, max_delay=tau, verbose=True)
DDE.past_from_function(initial)

# Breaking encapsulation in order to set initial value
DDE.generate_lambdas()
DDE.DDE.y = numpy.array([y0])
DDE.DDE.past[-1] = (0.0, numpy.array([y0]), numpy.array([0.]))

data = []
range = DDE.t + numpy.arange(0, 13, 0.5)
for time in range:
    data.append(DDE.integrate(time))

import matplotlib.pyplot as plt
plt.scatter(range, numpy.array(data))
plt.scatter(numpy.arange(0, 8.5, 0.5), real_data)
예제 #13
0
	def setUpClass(self):
		self.DDE = jitcdde(f_params, control_pars=[a,b,c,k])
		self.DDE.set_integration_parameters(**test_parameters)
예제 #14
0
ydot = [y(i) for i in range(3, 6)]
y_tot = sum(current_y(i) for i in range(3))
ydot_tot = sum(current_y(i) for i in range(3, 6))
y_past = sum(past_y(t - τ, i, anchors_past) for i in range(3))
ydot_past = sum(past_y(t - τ, i, anchors_past) for i in range(3, 6))
yddot_past = sum(past_dy(t - τ, i, anchors_past) for i in range(3, 6))

helpers = {
    (anchors_past, anchors(t - τ)),
    (difference, ybar_0 - y_past),
    (factor_μ,
     sech(difference)**2 * (yddot_past + 2 * ydot_past**2 * tanh(difference))),
    (factor_ζ, 2 * abs(y_tot) * ydot_tot),
}

f = {y(i): ydot[i] for i in range(3)}
f.update({
    ydot[i]:
    μ[i] * factor_μ - ζ[i] * factor_ζ - ε[i] * ν[i] * ydot[i] - ν[i]**2 * y(i)
    for i in range(3)
})

DDE = jitcdde(f, helpers=helpers, verbose=False)

np.random.seed(23)
DDE.constant_past(np.random.normal(0, 1, 6))
DDE.adjust_diff()

for time in DDE.t + np.arange(0.1, 100, 0.1):
    print(time, *DDE.integrate(time))
예제 #15
0
	def setUpClass(self):
		self.ODE = jitcdde(f_with_tiny_delay)
예제 #16
0
	def setUpClass(self):
		self.ODE = jitcdde(f)
예제 #17
0
	def setUp(self):
		double_mackey_glass = [
				.25*y(0,t-15) / (1+y(0,t-15)**10) - 0.1*y(0),
				.25*y(1,t-15) / (1+y(1,t-15)**10) - 0.1*y(1)
				]
		self.DDE = jitcdde(double_mackey_glass)
예제 #18
0
	def setUpClass(self):
		self.DDE = jitcdde(f)
		self.DDE.set_integration_parameters(**test_parameters)
예제 #19
0
	def test_check_undefined_variable(self):
		x = symengine.symbols("x")
		DDE = jitcdde([x])
		with self.assertRaises(ValueError):
			DDE.check()
예제 #20
0
	def test_check_index_too_high(self):
		DDE = jitcdde([y(1)])
		with self.assertRaises(ValueError):
			DDE.check()
예제 #21
0
	def test_check_index_negative(self):
		DDE = jitcdde([y(-1)])
		with self.assertRaises(ValueError):
			DDE.check()
예제 #22
0
	def setUpClass(self):
		self.DDE = jitcdde(f_with_tiny_delay)
		self.DDE.set_integration_parameters(pws_fuzzy_increase=False, **test_parameters)
예제 #23
0
	:lines: 75-78

Taking everything together, our code is:

.. literalinclude:: ../examples/mackey_glass.py
	:dedent: 1
	:lines: 60-78
"""


if __name__ == "__main__":
	from jitcdde import jitcdde, y, t
	import numpy
	
	τ = 15
	n = 10
	β = 0.25
	γ = 0.1
	
	f = [ β * y(0,t-τ) / (1 + y(0,t-τ)**n) - γ*y(0) ]
	DDE = jitcdde(f)
	
	DDE.constant_past([1.0])
	
	DDE.step_on_discontinuities()
	
	data = []
	for time in numpy.arange(DDE.t, DDE.t+10000, 10):
		data.append( DDE.integrate(time) )
	numpy.savetxt("timeseries.dat", data)
예제 #24
0
                  jumps=jumps,
                  atol=atol,
                  rtol=rtol)

t = sol.t
y = sol.y[0, :]
yp = sol.yp[0, :]
t45 = sol45.t
y45 = sol45.y[0, :]
yp45 = sol45.yp[0, :]

#jit cdde
f = [y_jit(0, t_jit - tau)]

DDE = jitcdde(
    f,
    max_delay=20  # for plotting; lest history is forgotten
)
DDE.set_integration_parameters(atol=atol, rtol=rtol)
DDE.past_from_function(h, times_of_interest=np.linspace(-1.1, -1e-8, 10))

DDE.adjust_diff()
data = []
dt_jit = []
times = np.linspace(DDE.t + 0.01, tf, 101)
for time in times:
    data.append(DDE.integrate(time))
    dt_jit.append(DDE.dt)
y_jit = np.asarray(data).T[0, :]

# sol matlab
import scipy.io as spio
예제 #25
0
"""
This is an example implementing laminar chaos as discovered by: https://doi.org/10.1103/PhysRevLett.120.084102
"""

from jitcdde import t, y, jitcdde
from symengine import sin
import numpy as np

T = 200
A = 0.9 / 2 / np.pi
τ_0 = 1.50
f = lambda z: 4 * z * (1 - z)
τ = τ_0 + A * sin(2 * np.pi * t)

model = [T * (-y(0) + f(y(0, t - τ)))]

DDE = jitcdde(model, max_delay=τ_0 + A, verbose=False)
DDE.past_from_function([0.4 + 0.2 * sin(t)])
DDE.set_integration_parameters(max_step=0.01, first_step=0.01)
DDE.integrate_blindly(τ_0 + A, 0.01)

for time in DDE.t + 100 + np.arange(0, 10, 0.01):
    print(DDE.integrate(time)[0])
    original_phage.death_rate * y(6) + in_lib(t) * (1 - R_pnn),  #c_poo
    new_phage.burst_size * y(5, t - l) -
    new_phage.infection_rate(y(2), new_phage.c0) - out_b(t) * y(7) -
    new_phage.death_rate * y(7) + in_lib(t) * (R_pnn)
]  #c_pnn


def dX():
    for i in range(n):
        yield equations[i]


initial_state = np.array([
    new_host.c0, s0, new_host.c0, s0, 0.0, 0.0, original_phage.c0, new_phage.c0
])
DDE = jitcdde(dX, delays=[l], max_delay=l, verbose=True)

DDE.constant_past([
    new_host.c0, s0, new_host.c0, s0, 0.0, 0.0, original_phage.c0, new_phage.c0
])
DDE.step_on_discontinuities()

data = []
x = np.arange(DDE.t, DDE.t + 220, 100 / 50)
for time in x:
    data.append(DDE.integrate(time))
np.savetxt("timeseries.dat", data)
#print(data)
#print(x)

plt.figure(figsize=(16, 16))
예제 #27
0
 def setUpClass(self):
     self.ODE = jitcdde(f)
예제 #28
0
#!/usr/bin/python3
# -*- coding: utf-8 -*-

from jitcdde import jitcdde, y, t
import numpy

f = [-y(0, t - 2 - y(0)**2) + 5]
DDE = jitcdde(f, max_delay=1e10)

eps = 0.1
DDE.add_past_point(-2.0, [4.5], [0.0])
DDE.add_past_point(-1.0 - eps, [4.5], [0.0])
DDE.add_past_point(-1.0 + eps, [-0.5], [0.0])
DDE.add_past_point(0.0, [-0.5], [0.0])

DDE.integrate_blindly(0.01)

data = []
for time in numpy.arange(0, 2.0, 0.001) + DDE.t:
    data.append([time, DDE.integrate(time)])
numpy.savetxt("timeseries.dat", data)
예제 #29
0
 def setUpClass(self):
     self.ODE = jitcdde(f_with_tiny_delay)
예제 #30
0
sol = solve_dde(fun,
                tspan,
                delays,
                y0,
                h,
                method='RK23',
                tracked_stages=None,
                atol=atol,
                rtol=rtol)
t = sol.t
y = sol.y[0, :]
yp = sol.yp[0, :]

#jit cdde
f = [-y_jit(0, t_jit - tau)]
DDE = jitcdde(f)
DDE.set_integration_parameters(atol=atol, rtol=rtol)
DDE.constant_past(y0)
DDE.step_on_discontinuities()
data = []
t_jit = np.linspace(DDE.t + 0.01, tf, 101)
dt_jit = []
for ti in t_jit:
    data.append(DDE.integrate(ti))
    dt_jit.append(DDE.dt)
y_jit = np.asarray(data).T[0, :]

# sol matlab
import scipy.io as spio
path_matlab = 'data_dde23/solConverging_dde23.mat'
mat = spio.loadmat(path_matlab, squeeze_me=True)
예제 #31
0
from jitcdde import t, y, jitcdde
from symengine import sin
import numpy as np

T = 200
A = 0.9/2/np.pi
τ_0 = 1.50
f = lambda z: 4*z*(1-z)
τ = τ_0 + A*sin(2*np.pi*t)

model = [ T*( -y(0) + f(y(0,t-τ)) ) ]

DDE = jitcdde(model,max_delay=τ_0+A)
DDE.past_from_function([0.4+0.2*sin(t)])
DDE.set_integration_parameters(max_step=0.01,first_step=0.01)
DDE.integrate_blindly(τ_0+A,0.01)

for time in DDE.t + 100 + np.arange(0,10,0.01):
	print(DDE.integrate(time)[0])
예제 #32
0
	def setUpClass(self):
		self.DDE = jitcdde(f)
		self.DDE.set_integration_parameters(pws_fuzzy_increase=True, **test_parameters)
예제 #33
0
	def setUpClass(self):
		self.DDE = jitcdde(f_params, control_pars=[a,b,c,k])
		self.DDE.set_integration_parameters(**test_parameters)
예제 #34
0
	def setUpClass(self):
		DDE_orig = jitcdde(f)
		filename = DDE_orig.save_compiled(overwrite=True)
		self.DDE = jitcdde(n=6, module_location=filename, delays=[delay])
		self.DDE.set_integration_parameters(**test_parameters)
예제 #35
0
	def test_check_index_too_high(self):
		DDE = jitcdde([y(1)])
		with self.assertRaises(ValueError):
			DDE.check()
예제 #36
0
	def setUpClass(self):
		self.DDE = jitcdde(f)
		self.DDE.set_integration_parameters(pws_fuzzy_increase=True, **test_parameters)
예제 #37
0
	def setUp(self):
		self.DDE = jitcdde(f)
		for point in get_past_points():
			self.DDE.add_past_point(*point)
		self.DDE.compile_C(extra_compile_args=compile_args)
예제 #38
0
	def setUp(self):
		self.DDE = jitcdde(f)
		self.DDE.add_past_points(get_past_points())
		self.DDE.compile_C(extra_compile_args=compile_args)
예제 #39
0
jitcdde_sympy_provisions = [
		jitcdde.sympy_symbols.t,
		jitcdde.sympy_symbols.y,
		symengine.cos,
	]

mixed = [
		jitcdde.sympy_symbols.t,
		jitcdde.y,
		sympy.cos,
	]

results = set()

for t,y,cos in [
			symengine_manually,
			sympy_manually,
			jitcdde_provisions,
			jitcdde_sympy_provisions,
			mixed,
		]:
	DDE = jitcdde.jitcdde( [cos(t)*y(0)-y(0,t-1)], verbose=False )
	DDE.constant_past([1],0.0)
	
	DDE.step_on_discontinuities()
	result = DDE.integrate(10)[0]
	results.add(result)

assert len(results)==1

예제 #40
0
	def test_check_index_negative(self):
		DDE = jitcdde([y(-1)])
		with self.assertRaises(ValueError):
			DDE.check()
예제 #41
0
	def setUpClass(self):
		self.DDE = jitcdde(f)
		self.DDE.set_integration_parameters(**test_parameters)
예제 #42
0
	def test_check_undefined_variable(self):
		x = symengine.symbols("x")
		DDE = jitcdde([x])
		with self.assertRaises(ValueError):
			DDE.check()
예제 #43
0
	def setUpClass(self):
		DDE_orig = jitcdde(f)
		filename = DDE_orig.save_compiled(overwrite=True)
		self.DDE = jitcdde(n=6, module_location=filename, delays=[delay])
		self.DDE.set_integration_parameters(**test_parameters)
예제 #44
0
#!/usr/bin/python3
# -*- coding: utf-8 -*-

from jitcdde import jitcdde, y, t
import numpy

τ = 15
n = 10
β = 0.25
γ = 0.1

f = [β * y(0, t - τ) / (1 + y(0, t - τ)**n) - γ * y(0)]
DDE = jitcdde(f, verbose=False)

DDE.constant_past([1.0])

DDE.step_on_discontinuities()

for time in numpy.arange(DDE.t, DDE.t + 1000, 1):
    print(time, *DDE.integrate(time))

DDE.jump(1, DDE.t)

for time in numpy.arange(DDE.t, DDE.t + 1000, 1):
    print(time, *DDE.integrate(time))
예제 #45
0
	def setUp(self):
		self.DDE = jitcdde(f)
		for point in get_past_points():
			self.DDE.add_past_point(*point)
		self.DDE.compile_C(extra_compile_args=compile_args)