@patch
def inflow(self:Simulation,cname,s):
=[x for x in self.components if x.name==cname]
c
if not c:
raise ValueError('No component named "%s"' % cname)
0].inflow(s)
c[
@patch
def outflow(self:Simulation,cname,s):
=[x for x in self.components if x.name==cname]
c
if not c:
raise ValueError('No component named "%s"' % cname)
0].outflow(s)
c[
@patch
def stock(self:Simulation,name,initial_value=0,
min=None,max=None,
=False,save=None):
plot
=Component(name+"'=",initial_value,min,max,plot,save)
cself.components.append(c)
return c
Core Simulation
Preliminaries
pyndamics3 version 0.0.32
patch
patch (f)
Decorator: add f
to the first parameter’s class (based on f’s type annotations)
patch_to
patch_to (cls, as_prop=False)
Decorator: add f
to cls
copy_func
copy_func (f)
Copy a non-builtin function (NB copy.copy
does not work for this)
RedirectStdStreams
RedirectStdStreams (stdout=None, stderr=None)
Initialize self. See help(type(self)) for accurate signature.
InterpFunction
InterpFunction (x, y, name)
Initialize self. See help(type(self)) for accurate signature.
array_wrap
array_wrap (_f)
from_values
from_values (var, *args)
Supporting functions for solving ODE and MAPS
simfunc
simfunc (_vec, t, _sim)
rk45
rk45 (function, y0, t_mat, _self, *args, **kwargs)
rkwrapper
rkwrapper (function, _self)
rk4
rk4 (function, y0, t_mat, *args, **kwargs)
rk2
rk2 (function, y0, t_mat, *args, **kwargs)
euler
euler (function, y0, t_mat, *args, **kwargs)
mapsolve
mapsolve (function, y0, t_mat, *args)
vector_field
vector_field (sim, rescale=False, **kwargs)
phase_plot
phase_plot (sim, x, y, z=None, **kwargs)
Make a Phase Plot of two or three variables.
Type | Default | Details | |
---|---|---|---|
sim | Simulation | This is a simulation object. | |
x | str | Name of the variable to plot on the x-axis | |
y | str | Name of the variable to plot on the y-axis | |
z | NoneType | None | Name of the variable to plot on the (optional) z-axis |
kwargs | |||
Returns |
Component
Component (diffstr, initial_value=0, min=None, max=None, plot=False, save=None)
Initialize self. See help(type(self)) for accurate signature.
Examples of Components
The Simulation
class is the primary one to use
Simulation
Simulation (method='odeint', verbose=False, plot_style='.-')
Initialize self. See help(type(self)) for accurate signature.
An alternate way of specifying the equations - stocks, inflows and outflows
=Simulation()
sim"y'=a - b*y",100)
sim.add(=10,b=2)
sim.params(aprint(sim.equations())
y'=a - b*y
a=10
b=2
#sim.add("y'=a - b*y",100)
=Simulation()
sim"y",100)
sim.stock('y','a')
sim.inflow('y','b*y')
sim.outflow(=10,b=2)
sim.params(aprint(sim.equations())
y'=+a-(b*y)
a=10
b=2
Some useful functions
mse_from_sim
mse_from_sim (params, extra)
model
model (params, xd, sim, varname, parameters)
repeat
repeat (S_orig, t_min, t_max, **kwargs)
This is my solution to an age-old problem of storing data in loops
Storage
Storage (save_every=1)
Initialize self. See help(type(self)) for accurate signature.
=1
y=0
x=0.01
dx=0.1
a
=Storage() # this object will store data
S+=x,y # adds this to the store, one data point at a time
Swhile x<=10:
=a*y*(50-y)*dx
dy+=dy
y+=dx
x
+=x,y # adds this to the store, one data point at a time
S
=S.arrays() # returns an array representation of all those data points
x,y plot(x,y)
x,y
(array([0.000e+00, 1.000e-02, 2.000e-02, ..., 9.990e+00, 1.000e+01,
1.001e+01]),
array([ 1. , 1.049 , 1.1003496, ..., 50. , 50. ,
50. ]))
pso_fit_sim
pso_fit_sim (varname, xd, yd, sim, parameters, n_particles=30, n_iterations=-1, progress_interval=100, plot=False)
swarm
swarm (parameters, fitness, number_of_particles=30, extra=None)
Initialize self. See help(type(self)) for accurate signature.
particle
particle (parameters, fitness_function, extra=None)
Initialize self. See help(type(self)) for accurate signature.
Stochastic Sims
Stochastic_Component
Stochastic_Component (name, initial_value=0, assignment_str=None, min=None, max=None, plot=False, save=None)
Initialize self. See help(type(self)) for accurate signature.
Stochastic_Simulation
Stochastic_Simulation ()
Initialize self. See help(type(self)) for accurate signature.
Struct
=0.2
β=0.1
γ=990
So=10
Io
=sim=Simulation()
dynamic_sim"N=S+I+R")
sim.add("S'=-β*S*I/N",So)
sim.add("I'=+β*S*I/N-γ*I",Io)
sim.add("R'=+γ*I",0)
sim.add(=β,γ=γ)
sim.params(β200)
sim.run(
=sim=Stochastic_Simulation()
stoch_sim"-S+I",'β*S*I/N',S=So,I=Io)
sim.add("-I +R",'γ*I',R=0)
sim.add("N=S+I+R")
sim.add(=β,γ=γ)
sim.params(β200,Nsims=100)
sim.run(
for i in range(100):
'bo',alpha=0.05)
plot(sim.t,sim.S[i],'ro',alpha=0.05)
plot(sim.t,sim.I[i],
'c-')
plot(dynamic_sim.t,dynamic_sim.S,'m-')
plot(dynamic_sim.t,dynamic_sim.I,
print(sim.func_str)
100%|██████████| 100/100 [00:00<00:00, 981.58it/s]
@numba.jit(nopython=True)
def _propensity_function(population, args):
S,I,R = population
β,γ = args
N=S+I+R
return np.array([
β*S*I/N,
γ*I,
])
sim.extinction_times
array([173.34641097, 147.2142087 , 173.25806905, 139.46093989,
141.46950986, 184.82415756, 177.24507978, 162.89244257,
135.74875203, 142.12308477, 124.1452361 , 103.78449248,
136.24764907, 141.64503261, 149.02049576, 154.08471155,
144.36046575, 119.08888338, 126.56068263, 150.96714048,
185.46651177, 137.75710629, 145.83280943, 161.45092743,
135.52623017, 158.22300444, 116.4663216 , 142.45833271,
131.58919096, 132.83514533, 158.67032537, 134.80159232,
138.67325803, 145.34862087, 175.71694956, 168.1073566 ,
138.83605961, 131.97878544, 132.29909228, 136.24188252,
141.72408479, 149.24895424, 140.01290625, 134.00439041,
128.82634577, 136.28831928, 121.92344439, 157.01860147,
130.87983952, 190.04334344, 16.78030852, 175.76630482,
116.42534703, 136.68052456, -1. , 136.61710555,
183.78243168, 159.79643709, 148.65710129, 154.9672915 ,
160.71863761, 166.15813836, 127.78792513, 139.85561763,
177.64067511, 131.71662539, 126.83004396, 135.63721802,
146.22285002, 170.34515699, 129.9729188 , 135.8394829 ,
161.82999371, 154.39877207, 165.24073111, -1. ,
134.46078024, 131.01546673, 121.36461215, 168.43992536,
160.32262449, 144.3013475 , 199.74725228, 111.65022834,
182.77090173, 113.66447594, 115.29110107, 138.87220524,
175.75330356, 127.62963742, 137.38328691, 137.9071455 ,
198.34523822, 144.94021945, 136.85662852, 166.1285249 ,
176.12888724, 141.47111244, 146.1237378 , 177.42190037])
=sim=Stochastic_Simulation()
stoch_sim"+X",'X-X**2/N',X=10)
sim.add("-X",'X**2/N')
sim.add(=10)
sim.params(N50,num_iterations=101) sim.run(
'-o') plot(sim.t,sim.X,
sim.extinction_times
array([-1])
Some Examples
Logistic
=Simulation()
sim"p'=a*p*(1-p/K)",100,plot=True)
sim.add(=1.5,K=300)
sim.params(a
0,50) sim.run(
<Figure size 864x576 with 0 Axes>
=Simulation()
sim"x=a*x*(1-x)",0.11,plot=1)
sim.add("y=a*y*(1-y)",0.12,plot=1)
sim.add(=3.5)
sim.params(a
0,50,discrete=True) sim.run(
<Figure size 864x576 with 0 Axes>
Map
=Simulation('map')
sim"x=a*x*(1-x)",0.11)
sim.add(=(12,8))
figure(figsizefor a in linspace(.1,4,1200):
=a)
sim.params(a0,1000)
sim.run(
=sim['x'][-100:]
x
*ones(x.shape),x,'k.',markersize=.5) plot(a
=Simulation('map')
sim"x=a*x*(1-x)",0.11)
sim.add(=(12,8))
figure(figsizefor a in linspace(3.2,4,1200):
=a)
sim.params(a0,1000)
sim.run(
=sim['x'][-100:]
x
*ones(x.shape),x,'k.',markersize=.5) plot(a
Repeat
=Simulation()
sim"growth_rate=a*(1-p/K)")
sim.add("p'=growth_rate*p",100)
sim.add(=1.5,K=300)
sim.params(a
=sim.repeat(0,10,a=[1,2,3,4])
result=sim['t']
t
for res in result:
=res['p']
p plot(t,p)
Higher Order
=Simulation()
sim"x''=-k*x/m -b*x'",[10,0],plot=True)
sim.add(=1.0,m=1.0,b=0.5)
sim.params(k
0,20) sim.run(
<Figure size 864x576 with 0 Axes>
"x","x_p_") phase_plot(sim,
Exploring parameters
explore_parameters
explore_parameters (sim, figsize=None, **kwargs)
=Simulation()
sim"p'=a*p*(1-p/K)",100,plot=True)
sim.add("K'=(50-K)/Kt",300,plot=False)
sim.add(=1.5,Kt=30)
sim.params(a
0,50) sim.run(
<Figure size 864x576 with 0 Axes>
=linspace(10,100,10)) explore_parameters(sim,Kt
=Simulation()
sim=(8,4)
sim.figsize"S'=-β*S*I/N",100,plot=1)
sim.add("I'=+β*S*I/N - γ*I",1,plot=2)
sim.add("R'=+γ*I",0,plot=0)
sim.add("N=S+I+R",plot=0)
sim.add(=0.2,γ=0.1)
sim.params(β150) sim.run(
<Figure size 864x576 with 0 Axes>
=(12,8),β=linspace(0,0.2,11)) explore_parameters(sim,figsize
=(12,8),β=[0,.1,.2,0,.1,.2],γ=[.1,.1,.1,.3,.3,.3]) explore_parameters(sim,figsize
=meshgrid([0,.1,.2],[0,.1,.2])
β,γ=(12,8),β=β,γ=γ) explore_parameters(sim,figsize
Functions of time
def a_vs_time(t):
return 20*t
=Simulation()
sim"a=a_vs_time(t)",plot=1)
sim.add("y'=-a*y",100,plot=2)
sim.add(
sim.functions(a_vs_time)
10) sim.run(
<Figure size 864x576 with 0 Axes>
Stochastic Simulation Examples
=0.2
β=0.1
γ=990
So=10
Io
=sim=Simulation()
dynamic_sim"N=S+I+R")
sim.add("S'=-β*S*I/N",So)
sim.add("I'=+β*S*I/N-γ*I",Io)
sim.add("R'=+γ*I",0)
sim.add(=β,γ=γ)
sim.params(β200)
sim.run(
=sim=Stochastic_Simulation()
stoch_sim"-S+I",'β*S*I/N',S=So,I=Io)
sim.add("-I +R",'γ*I',R=0)
sim.add("N=S+I+R")
sim.add(=β,γ=γ)
sim.params(β200) sim.run(
200,Nsims=100)
sim.run(
for i in range(100):
'bo',alpha=0.005)
plot(sim.t,sim.S[i],'ro',alpha=0.005)
plot(sim.t,sim.I[i],
'c-')
plot(dynamic_sim.t,dynamic_sim.S,'m-') plot(dynamic_sim.t,dynamic_sim.I,
100%|██████████| 100/100 [00:00<00:00, 1006.25it/s]