-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateDynamics.py
More file actions
2408 lines (1924 loc) · 107 KB
/
GenerateDynamics.py
File metadata and controls
2408 lines (1924 loc) · 107 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 4 14:43:40 2024
@author: jefis
"""
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 26 13:06:30 2023
@author: jefis
"""
import numpy as np
import networkx as nx
from copy import deepcopy
from scipy.integrate import odeint
from sklearn.neighbors import KernelDensity
from matplotlib import pyplot as plt
from scipy.linalg import schur,expm
from scipy.spatial.distance import pdist
from tqdm import tqdm
class non_laplacian_dynamics:
def __init__(self):
self.graph = None
self.init_cond = None
def set_graph(self,G):
"""A function to set the graph. Note it is assumed to be a networkx graph...
Inputs: G - a networkx graph.
"""
self.graph = G
def set_init_cond(self,init_cond):
"""For setting a particular initial condition"""
self.init_cond=init_cond
def return_graph(self):
return self.graph
def return_init_cond(self):
return self.init_cond
def convert_adjacency(self,A,Type='DiGraph'):
"""To convert an adacency matrix to a networkx graph and store the graph
internally
Inputs: A - (n x n) Adjacency matrix in numpy array form. Note must be acceptable
form for networkx to convert to a graph
Type - either 'Graph' or 'DiGraph' depending on the type of graph
Outputs:
graph - The networkx graph.
"""
if Type == 'DiGraph':
self.graph = nx.DiGraph(A)
elif Type == 'Graph':
self.graph = nx.Graph(A)
else:
raise ValueError("Only Type = 'DiGraph' or Type = 'Graph' is allowed")
return self.graph
def mondal_scm(self,x,t,L,params):
"""A coupled supply chain network model
Inputs: x - a (3n x 1) vector containing the 3 state variables for each oscillator
t - the time
L - the graph Laplacian
params - either a list with 6 entries or a (6n x 1) array ordered as follows
[m,n,a,r,p,k].
Outputs: arr - a (3n x 1) array pushed forward by the integration time unit
NOTE: This model comes from the paper
'A new supply chain model and its synchronization behaviour'
by: Sayantani Mondal
Suggested parameters m = 10, n = 9, a = 0, r = 28, p = 1, k = 5/3
"""
if len(params) == 6:
m,n,a,r,p,k = params
#do stuff here
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:3]
Y = x[1:lenx:3]
Z = x[2:lenx:3]
Div = 1+a*Y
Div2= np.dot(np.matrix(Div).T,np.matrix(Div))
np.fill_diagonal(Div2,1)
XZ = X*Z
XY = X*Y
LX = np.dot(L/Div2,X)
LY = np.dot(L/Div2,Y)
dx[0:lenx:3] = m*Y/Div -n*X -m*LY
dx[1:lenx:3] = r*X - p*Y/Div - XZ +np.dot(L,XZ) - r*np.dot(L,X)+(p*LY)-np.dot(L,Y)
dx[2:lenx:3] = X*Y - k*Z -np.dot(L,XY)
else:
#THIS NEEDS TO BE FINISHED...
lenparams = len(params)
m = params[0:lenparams:6]
n = params[1:lenparams:6]
a = params[2:lenparams:6]
r = params[3:lenparams:6]
p = params[4:lenparams:6]
k = params[5:lenparams:6]
#
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:3]
Y = x[1:lenx:3]
Z = x[2:lenx:3]
Div = 1+a*Y
Div2= np.dot(np.matrix(Div).T,np.matrix(Div))
np.fill_diagonal(Div2,1)
XZ = X*Z
XY = X*Y
Xshape = X.shape
LX = np.asarray(np.dot(L/Div2,X)).reshape(Xshape)
LY = np.asarray(np.dot(L/Div2,Y)).reshape(Xshape)
#print(Xshape,k.shape,Div.shape,dx.shape,LY.shape)
#print(a.shape,LX.shape)
dx[0:lenx:3] = m*Y/np.prod(Div) -n*X -m*LY
dx[1:lenx:3] = r*X - p*Y/np.prod(Div) - XZ +np.dot(L,XZ) - r*np.dot(L,X)+(p*LY)-np.dot(L,Y)
dx[2:lenx:3] = X*Y - k*Z -np.dot(L,XY)
return dx
def convert_adjacency(self,A,Type='DiGraph'):
"""To convert an adacency matrix to a networkx graph and store the graph
internally
Inputs: A - (n x n) Adjacency matrix in numpy array form. Note must be acceptable
form for networkx to convert to a graph
Type - either 'Graph' or 'DiGraph' depending on the type of graph
Outputs:
graph - The networkx graph.
"""
if Type == 'DiGraph':
self.graph = nx.DiGraph(A)
elif Type == 'Graph':
self.graph = nx.Graph(A)
else:
raise ValueError("Only Type = 'DiGraph' or Type = 'Graph' is allowed")
return self.graph
def convert_graph_to_laplacian(self,G):
A = nx.adjacency_matrix(G).todense()
L = self.get_Laplacian(A)
return L
def get_Laplacian(self,A,return_eigvals=False):
L = np.matrix(np.zeros((A.shape[0],A.shape[0])))
np.fill_diagonal(L,np.sum(A,1))
L = L-A
if not return_eigvals:
return L
else:
E = np.linalg.eigvals(L)
return L,E
def generate_initial_condition(self,lenx0,method='random',init_cond_type='normal',init_cond_params=[0,1],init_cond_offset=0,p_norm=2,scale=1):
"""Method for generating initial conditions.
Inputs: lenx0 - the size of the initial condition vector
method - the method for generating initial conditions. 'random' gives random ic's, normalized gives normalized ic's
init_cond_type - the type of random numbers to draw from, for example to draw from a normal distribution 'normal'
init_cond_params - the parameters for the init_cond_type distribution. Must be an iterable
init_cond_offset - how much to offset the initial condition by.
p_norm - if method is 'normalized', the value of the p_norm to normalize by
scale - if method is 'normalized' this will scale the initial condition to be of a certain size.
Outputs:
x0 - the initial condition.
"""
if init_cond_type =='normal':
x0 = np.random.normal(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type =='uniform':
x0 = np.random.uniform(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'laplace':
x0 = np.random.uniform(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'exponential':
x0 = np.random.exponential(init_cond_params[0],lenx0)+init_cond_offset
elif init_cond_type == 'rayleigh':
x0 = np.random.exponential(init_cond_params[0],lenx0)+init_cond_offset
elif init_cond_type == 'beta':
x0 = np.random.beta(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'gamma':
x0 = np.random.beta(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'gumbel':
x0 = np.random.gumbel(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'chisquare':
x0 = np.random.chisquare(init_cond_params[0],lenx0)+init_cond_offset
elif init_cond_type == 'logistic':
x0 = np.random.logistic(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'lognormal':
x0 = np.random.lognormal(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'pareto':
x0 = np.random.pareto(init_cond_params[0],lenx0)+init_cond_offset
elif init_cond_type == 'f':
x0 = np.random.f(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'vonmises':
x0 = np.random.vonmises(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'wald':
x0 = np.random.wald(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'weibull':
x0 = np.random.beta(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'zipf':
x0 = np.random.beta(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
else:
raise ValueError("This init_cond_type is not implemented see docs for types which are currently implemented")
if method=='normalized':
x0 = (x0/np.linalg.norm(x0,p_norm))*scale
elif method =='random':
do_nothing =1
else:
raise ValueError("This method for initial conditions is not allowed. See docs for allowable methods.")
return x0
def continuous_time_nonlinear_dynamics(self,G=None,L=None,tmax=100,timestep=0.1,method='random',init_cond_type='normal',init_cond=None,init_cond_params=[0,1],init_cond_offset=0,p_norm=2,scale=1,dynamics_type='Mondal',dynamics_params=[10,9,0,28,1,5/3],coupling_strength=1):
"""Generate continuous time Laplacian (i.e. diffusive) type non-linear dynamics
Inputs:
G - a networkx graph, if set to None then the internally stored graph will be used
L - the graph Laplacian, note this will be ignored if G is not None
tmax - the max value to integrate to
timestep - the integration time steps
method - the method for drawing initial conditions. See docs for more information
init_cond_type - the type of random initial conditions (can be 'normal' or 'uniform' for instance, see docs for available types)
init_cond - you can specify the initial condition here, if this is specified then init_cond_type will be ignored, if this is None then the initial condition will be generated according to init_cond_type
init_cond_parameters - a list of the parameters for the distribution type. For instance if init_cond_type = 'normal' then two parameters should be specified, the mean and the variance
init_cond_offset - how much to offset the initial condition by, added to each entry of the initial condition
p_norm - the value of p in the p-norm if method = 'normalized'
scale - how much to scale the initial condition by if method = 'normalized'
dynamics_type - the type of dynamics to use, for instance 'Rossler' will give Rossler type dynamics, see docs for allowable dynamics types
dynamics_params - a list of the parameters for the dynamics type, for instance 'Rossler' has 3 parameters to specify
coupling_strength - the coupling strength
Outputs:
sol - the solution after integration
t - the vector of integration time points
"""
if G is None:
if self.graph is not None:
G = deepcopy(self.graph)
elif L is not None:
do_nothing = 1
else:
raise ValueError("No graph to perform dynamics on...")
else:
self.graph = G
if G is not None:
L = self.convert_graph_to_laplacian(G)
n = len(L)
t = np.arange(0,tmax,step=timestep)
if dynamics_type =='Mondal':
lenx0 = 3*n
if init_cond is None:
self.init_cond = self.generate_initial_condition(lenx0,method=method,init_cond_type=init_cond_type,init_cond_params=init_cond_params,init_cond_offset=init_cond_offset,p_norm=p_norm,scale=scale)
else:
self.init_cond = init_cond
x0 = self.init_cond
sol = odeint(self.mondal_scm,x0,t,args=(L,dynamics_params))
else:
raise ValueError("Dynamics must be of allowed type, see docs for allowed types")
return sol,t
class laplacian_dynamics:
def __init__(self):
self.graph = None
self.init_cond = None
def set_graph(self,G):
"""A function to set the graph. Note it is assumed to be a networkx graph...
Inputs: G - a networkx graph.
"""
self.graph = G
def set_init_cond(self,init_cond):
"""For setting a particular initial condition"""
self.init_cond=init_cond
def return_graph(self):
return self.graph
def return_init_cond(self):
return self.init_cond
def convert_adjacency(self,A,Type='DiGraph'):
"""To convert an adacency matrix to a networkx graph and store the graph
internally
Inputs: A - (n x n) Adjacency matrix in numpy array form. Note must be acceptable
form for networkx to convert to a graph
Type - either 'Graph' or 'DiGraph' depending on the type of graph
Outputs:
graph - The networkx graph.
"""
if Type == 'DiGraph':
self.graph = nx.DiGraph(A)
elif Type == 'Graph':
self.graph = nx.Graph(A)
else:
raise ValueError("Only Type = 'DiGraph' or Type = 'Graph' is allowed")
return self.graph
def linear_dynamics(self,x,t,A):
x = np.dot(A,x)
return np.array(x).flatten()
def rossler(self,x,t,LH,params):
a,b,c = params
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:3]
Y = x[1:lenx:3]
Z = x[2:lenx:3]
dx[0:lenx:3] = -Y-Z
dx[1:lenx:3] = X+a*Y
dx[2:lenx:3] = b+Z*(X-c)
return np.array(dx-np.dot(LH,x)).flatten()
def perturbed_rossler(self,x,t,LH,params):
a,b,c,d = params
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:3]
Y = x[1:lenx:3]
Z = x[2:lenx:3]
dx[0:lenx:3] = -Y-Z
dx[1:lenx:3] = X+a*Y
dx[2:lenx:3] = b+Z*(X-c)-d*Y
return np.array(dx-np.dot(LH,x)).flatten()
def aizawa(self,x,t,LH,params):
"""See https://sequelaencollection.home.blog/3d-chaotic-attractors/
Suggested params a = 0.95, b = 0.7, c = 0.6, d = 3.5, e = 0.25, f = 0.1
"""
a,b,c,d,e,f = params
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:3]
Y = x[1:lenx:3]
Z = x[2:lenx:3]
dx[0:lenx:3] = (Z-b)*X-d*Y
dx[1:lenx:3] = d*X+(Z-b)*Y
dx[2:lenx:3] = c+a*Z-Z**3/3-(X**2+Y**2)*(1+e*Z)+f*Z*X**3
return np.array(dx-np.dot(LH,x)).flatten()
def chen_lee(self,x,t,LH,params):
"""See https://sequelaencollection.home.blog/3d-chaotic-attractors/
Suggested params a = 5, b = -10, c = -0.38
"""
a,b,c = params
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:3]
Y = x[1:lenx:3]
Z = x[2:lenx:3]
dx[0:lenx:3] = a*X-Y*Z
dx[1:lenx:3] = b*Y+X*Z
dx[2:lenx:3] = c*Z+(X*Y)/3
return np.array(dx-np.dot(LH,x)).flatten()
def arneodo(self,x,t,LH,params):
"""See https://sequelaencollection.home.blog/3d-chaotic-attractors/
Suggested params a = -5.5, b = 3.5, c = -1
"""
a,b,c = params
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:3]
Y = x[1:lenx:3]
Z = x[2:lenx:3]
dx[0:lenx:3] = Y
dx[1:lenx:3] = Z
dx[2:lenx:3] = -a*X-b*Y-Z+c*X**3
return np.array(dx-np.dot(LH,x)).flatten()
def sprottb(self,x,t,LH,params):
"""See https://sequelaencollection.home.blog/3d-chaotic-attractors/
Suggested params a = 0.4 b = 1.2, c = 1
"""
a,b,c = params
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:3]
Y = x[1:lenx:3]
Z = x[2:lenx:3]
dx[0:lenx:3] = a*Y*Z
dx[1:lenx:3] = X-b*Y
dx[2:lenx:3] = c-X*Y
return np.array(dx-np.dot(LH,x)).flatten()
def sprott_linzf(self,x,t,LH,params):
"""See https://sequelaencollection.home.blog/3d-chaotic-attractors/
Suggested params a = 0.5
"""
a = params[0]
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:3]
Y = x[1:lenx:3]
Z = x[2:lenx:3]
dx[0:lenx:3] = Y+Z
dx[1:lenx:3] = -X+a*Y
dx[2:lenx:3] = X**2-Z
return np.array(dx-np.dot(LH,x)).flatten()
def dadras(self,x,t,LH,params):
"""See https://sequelaencollection.home.blog/3d-chaotic-attractors/
Suggested params a = 3,b = 2.7, c = 1.7, d = 2, e = 9
"""
a,b,c,d,e = params
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:3]
Y = x[1:lenx:3]
Z = x[2:lenx:3]
dx[0:lenx:3] = Y-a*X+b*Y*Z
dx[1:lenx:3] = c*Y-X*Z+Z
dx[2:lenx:3] = d*X*Y-e*Z
return np.array(dx-np.dot(LH,x)).flatten()
def halvorsen(self,x,t,LH,params):
"""See https://sequelaencollection.home.blog/3d-chaotic-attractors/
Suggested params a = 1.4
"""
a = params[0]
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:3]
Y = x[1:lenx:3]
Z = x[2:lenx:3]
dx[0:lenx:3] = -a*X-4*Y-4*Z-Y**2
dx[1:lenx:3] = -a*Y-4*Z-4*X-Z**2
dx[2:lenx:3] = -a*Z-4*X-4*Y-X**2
return np.array(dx-np.dot(LH,x)).flatten()
def thomas(self,x,t,LH,params):
"""See https://www.dynamicmath.xyz/strange-attractors/
Suggested params a = 0.208186
"""
a = params[0]
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:3]
Y = x[1:lenx:3]
Z = x[2:lenx:3]
dx[0:lenx:3] = np.sin(Y)-a*X
dx[1:lenx:3] = np.sin(Z)-a*Y
dx[2:lenx:3] = np.sin(X)-a*Z
return np.array(dx-np.dot(LH,x)).flatten()
def rabinovich_fabrikant(self,x,t,LH,params):
"""See https://www.dynamicmath.xyz/strange-attractors/
Suggested params a = 0.14, b = 0.1
"""
a,b = params
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:3]
Y = x[1:lenx:3]
Z = x[2:lenx:3]
dx[0:lenx:3] = Y*(Z-1+X**2)+b*X
dx[1:lenx:3] = X*(3*Z+1-X**2)+b*Y
dx[2:lenx:3] = -2*Z*(a+X*Y)
return np.array(dx-np.dot(LH,x)).flatten()
def three_scroll(self,x,t,LH,params):
"""See https://www.dynamicmath.xyz/strange-attractors/
Suggested params a = 32.48, b = 45.84, c = 1.18, d = 0.13, e = 0.57, f = 14.7
"""
a,b,c,d,e,f = params
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:3]
Y = x[1:lenx:3]
Z = x[2:lenx:3]
dx[0:lenx:3] = a*(Y-X)+d*X*Z
dx[1:lenx:3] = b*X-X*Z+f*Y
dx[2:lenx:3] = c*Z+X*Y-e*X**2
return np.array(dx-np.dot(LH,x)).flatten()
def four_wing(self,x,t,LH,params):
"""See https://www.dynamicmath.xyz/strange-attractors/
Suggested params a = 0.2, b = 0.01, c =-0.4
"""
a,b,c = params
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:3]
Y = x[1:lenx:3]
Z = x[2:lenx:3]
dx[0:lenx:3] = a*X+Y*Z
dx[1:lenx:3] = b*X+c*Y-X*Z
dx[2:lenx:3] = -Z-X*Y
return np.array(dx-np.dot(LH,x)).flatten()
def perturbed_four_wing(self,x,t,LH,params):
"""See https://www.dynamicmath.xyz/strange-attractors/
Suggested params a = 0.2, b = 0.01, c =-0.4, d = ?,e = ?, f =?, g=?
"""
a,b,c,d,e,f,g,h,k,l = params
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:3]
Y = x[1:lenx:3]
Z = x[2:lenx:3]
dx[0:lenx:3] = a*X+d*Y*Z+h
dx[1:lenx:3] = b*X+c*Y-e*X*Z+k
dx[2:lenx:3] = -f*Z-g*X*Y+l
return np.array(dx-np.dot(LH,x)).flatten()
def lorenz(self,x,t,LH,params):
"""Suggested params a =10, b = 28, c = 8/3"""
if len(params) == 3:
a,b,c = params
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:3]
Y = x[1:lenx:3]
Z = x[2:lenx:3]
dx[0:lenx:3] = a*(Y-X)
dx[1:lenx:3] = X*(b-Z)-Y
dx[2:lenx:3] = X*Y-c*Z
else:
lenparams = len(params)
a = params[0:lenparams:3]
b = params[1:lenparams:3]
c = params[2:lenparams:3]
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:3]
Y = x[1:lenx:3]
Z = x[2:lenx:3]
dx[0:lenx:3] = a*(Y-X)
dx[1:lenx:3] = X*(b-Z)-Y
dx[2:lenx:3] = X*Y-c*Z
return np.array(dx-np.dot(LH,x)).flatten()
def brusselator(self,x,t,LH,params):
a,b = params
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:2]
Y = x[1:lenx:2]
dx[0:lenx:2] = 1-(a+1)*X+b*Y*X**2
dx[1:lenx:2] = a*X-b*Y*X**2
return np.array(dx-np.dot(LH,x)).flatten()
def vanderpol(self,x,t,LH,params):
a = params[0]
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:2]
Y = x[1:lenx:2]
dx[0:lenx:2] = Y
dx[1:lenx:2] = -X+a*(1-X**2)*Y
return np.array(dx-np.dot(LH,x)).flatten()
def wienbridge(self,x,t,LH,params):
a,b,c = params
lenx = len(x)
dx = deepcopy(x)
X = x[0:lenx:2]
Y = x[1:lenx:2]
dx[0:lenx:2] = -X+Y-(a*Y-b*Y**3+c*Y**5)
dx[1:lenx:2] = -(-X+Y-(a*Y-b*Y**3+c*Y**5))-Y
return np.array(dx-np.dot(LH,x)).flatten()
def convert_graph_to_laplacian(self,G):
A = nx.adjacency_matrix(G).todense()
L = self.get_Laplacian(A)
return L
def get_Laplacian(self,A,return_eigvals=False):
L = np.matrix(np.zeros((A.shape[0],A.shape[0])))
np.fill_diagonal(L,np.sum(A,1))
L = L-A
if not return_eigvals:
return L
else:
E = np.linalg.eigvals(L)
return L,E
def continuous_time_linear_dynamics(self,G=None,tmax=100,timestep=0.1,init_cond_type='normal',init_cond=None,init_cond_params=[0,1],init_cond_offset=0):
"""Generate continuous time Laplacian (i.e. diffusive) type linear dynamics
Inputs:
G - a networkx graph, if set to None then the internally stored graph will be used
tmax - the max value to integrate to
timestep - the integration time steps
init_cond_type - the type of random initial conditions (can be 'normal' or 'uniform' for instance, see docs for available types)
init_cond - you can specify the initial condition here, if this is specified then init_cond_type will be ignored, if this is None then the initial condition will be generated according to init_cond_type
init_cond_parameters - a list of the parameters for the distribution type. For instance if init_cond_type = 'normal' then two parameters should be specified, the mean and the variance
init_cond_offset - offset to be added to all initial condition values (to shift the distribution...)
Outputs:
sol - the solution after integration
t - the vector of integration time points
"""
if G is None:
if self.graph is not None:
G = deepcopy(self.graph)
else:
raise ValueError("No graph to perform dynamics on...")
else:
self.graph=G
L = self.convert_graph_to_laplacian(G)
t = np.arange(0,tmax,step=timestep)
n = L.shape[0]
lenx0 = n
if init_cond_type =='normal':
x0 = np.random.normal(init_cond_params[0],init_cond_params[1],n)
elif init_cond_type =='uniform':
x0 = np.random.uniform(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'laplace':
x0 = np.random.uniform(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'exponential':
x0 = np.random.exponential(init_cond_params[0],lenx0)+init_cond_offset
elif init_cond_type == 'rayleigh':
x0 = np.random.exponential(init_cond_params[0],lenx0)+init_cond_offset
elif init_cond_type == 'beta':
x0 = np.random.beta(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'gamma':
x0 = np.random.beta(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'gumbel':
x0 = np.random.gumbel(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'chisquare':
x0 = np.random.chisquare(init_cond_params[0],lenx0)+init_cond_offset
elif init_cond_type == 'logistic':
x0 = np.random.logistic(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'lognormal':
x0 = np.random.lognormal(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'pareto':
x0 = np.random.pareto(init_cond_params[0],lenx0)+init_cond_offset
elif init_cond_type == 'f':
x0 = np.random.f(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'vonmises':
x0 = np.random.vonmises(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'wald':
x0 = np.random.wald(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'weibull':
x0 = np.random.weibull(init_cond_params[0],lenx0)+init_cond_offset
elif init_cond_type == 'zipf':
x0 = np.random.zipf(init_cond_params[0],lenx0)+init_cond_offset
else:
raise ValueError("This init_cond_type is not implemented see docs for types which are currently implemented")
sol = odeint(self.linear_dynamics,x0,t,args=(-L,))
return sol,t
def generate_initial_condition(self,lenx0,method='random',init_cond_type='normal',init_cond_params=[0,1],init_cond_offset=0,p_norm=2,scale=1):
"""Method for generating initial conditions.
Inputs: lenx0 - the size of the initial condition vector
method - the method for generating initial conditions. 'random' gives random ic's, normalized gives normalized ic's
init_cond_type - the type of random numbers to draw from, for example to draw from a normal distribution 'normal'
init_cond_params - the parameters for the init_cond_type distribution. Must be an iterable
init_cond_offset - how much to offset the initial condition by.
p_norm - if method is 'normalized', the value of the p_norm to normalize by
scale - if method is 'normalized' this will scale the initial condition to be of a certain size.
Outputs:
x0 - the initial condition.
"""
if init_cond_type =='normal':
x0 = np.random.normal(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type =='uniform':
x0 = np.random.uniform(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'laplace':
x0 = np.random.uniform(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'exponential':
x0 = np.random.exponential(init_cond_params[0],lenx0)+init_cond_offset
elif init_cond_type == 'rayleigh':
x0 = np.random.exponential(init_cond_params[0],lenx0)+init_cond_offset
elif init_cond_type == 'beta':
x0 = np.random.beta(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'gamma':
x0 = np.random.beta(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'gumbel':
x0 = np.random.gumbel(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'chisquare':
x0 = np.random.chisquare(init_cond_params[0],lenx0)+init_cond_offset
elif init_cond_type == 'logistic':
x0 = np.random.logistic(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'lognormal':
x0 = np.random.lognormal(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'pareto':
x0 = np.random.pareto(init_cond_params[0],lenx0)+init_cond_offset
elif init_cond_type == 'f':
x0 = np.random.f(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'vonmises':
x0 = np.random.vonmises(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'wald':
x0 = np.random.wald(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'weibull':
x0 = np.random.beta(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
elif init_cond_type == 'zipf':
x0 = np.random.beta(init_cond_params[0],init_cond_params[1],lenx0)+init_cond_offset
else:
raise ValueError("This init_cond_type is not implemented see docs for types which are currently implemented")
if method=='normalized':
x0 = (x0/np.linalg.norm(x0,p_norm))*scale
elif method =='random':
do_nothing =1
else:
raise ValueError("This method for initial conditions is not allowed. See docs for allowable methods.")
return x0
def continuous_time_nonlinear_dynamics(self,G=None,L=None,tmax=100,timestep=0.1,method='random',init_cond_type='normal',init_cond=None,init_cond_params=[0,1],init_cond_offset=0,p_norm=2,scale=1,dynamics_type='Rossler',dynamics_params=[0.2,0.2,7],coupling_matrix=None,coupling_strength=1,dynamics_constant_params=True,dynamics_non_constant_params=None,changing_laplacian=False):
"""Generate continuous time Laplacian (i.e. diffusive) type non-linear dynamics
Inputs:
G - a networkx graph, if set to None then the internally stored graph will be used
L - the graph Laplacian, note this will be ignored if G is not None
tmax - the max value to integrate to
timestep - the integration time steps
method - the method for drawing initial conditions. See docs for more information
init_cond_type - the type of random initial conditions (can be 'normal' or 'uniform' for instance, see docs for available types)
init_cond - you can specify the initial condition here, if this is specified then init_cond_type will be ignored, if this is None then the initial condition will be generated according to init_cond_type
init_cond_parameters - a list of the parameters for the distribution type. For instance if init_cond_type = 'normal' then two parameters should be specified, the mean and the variance
init_cond_offset - how much to offset the initial condition by, added to each entry of the initial condition
p_norm - the value of p in the p-norm if method = 'normalized'
scale - how much to scale the initial condition by if method = 'normalized'
dynamics_type - the type of dynamics to use, for instance 'Rossler' will give Rossler type dynamics, see docs for allowable dynamics types
dynamics_params - a list of the parameters for the dynamics type, for instance 'Rossler' has 3 parameters to specify
coupling_matrix - if coupling_function_type is matrix then this is the matrix
coupling_strength - the coupling strength
dynamics_constant_params - boolean. If True uses dynamics_params, if false uses dynamics_non_constant_params
dynamics_non_constant_params - A dictionary which contains the following entries,
start_times - a list or array of starting times for parameter changes (first start time should be 0)
end_time - the last end time
parameters - a dictionary or array of the parameters, should be the same length as start_times
Outputs:
sol - the solution after integration
t - the vector of integration time points
"""
if G is None:
if self.graph is not None:
G = deepcopy(self.graph)
elif L is not None:
do_nothing = 1
else:
raise ValueError("No graph to perform dynamics on...")
else:
self.graph = G
if G is not None:
L = self.convert_graph_to_laplacian(G)
n = len(L)
if dynamics_constant_params:
t = np.arange(0,tmax,step=timestep)
if dynamics_type =='Rossler':
lenx0 = 3*n
if init_cond is None:
self.init_cond = self.generate_initial_condition(lenx0,method=method,init_cond_type=init_cond_type,init_cond_params=init_cond_params,init_cond_offset=init_cond_offset,p_norm=p_norm,scale=scale)
else:
self.init_cond = init_cond
if coupling_matrix is None:
#assume coupling only through the x component
H = np.eye(3)
H[1,1] = 0
H[2,2] = 0
else:
H = coupling_matrix
x0 = self.init_cond
LH = coupling_strength*np.kron(L,H)
sol = odeint(self.rossler,x0,t,args=(LH,dynamics_params))
elif dynamics_type =='Perturbed_Rossler':
lenx0 = 3*n
if init_cond is None:
self.init_cond = self.generate_initial_condition(lenx0,method=method,init_cond_type=init_cond_type,init_cond_params=init_cond_params,init_cond_offset=init_cond_offset,p_norm=p_norm,scale=scale)
else:
self.init_cond = init_cond
if coupling_matrix is None:
#assume coupling only through the x component
H = np.eye(3)
H[1,1] = 0
H[2,2] = 0
else:
H = coupling_matrix
x0 = self.init_cond
LH = coupling_strength*np.kron(L,H)
sol = odeint(self.perturbed_rossler,x0,t,args=(LH,dynamics_params))
elif dynamics_type =='Aizawa':
lenx0 = 3*n
if init_cond is None:
self.init_cond = self.generate_initial_condition(lenx0,method=method,init_cond_type=init_cond_type,init_cond_params=init_cond_params,init_cond_offset=init_cond_offset,p_norm=p_norm,scale=scale)
else:
self.init_cond = init_cond
if coupling_matrix is None:
#assume coupling only through the x component
H = np.eye(3)
H[1,1] = 0
H[2,2] = 0
else:
H = coupling_matrix
x0 = self.init_cond
LH = coupling_strength*np.kron(L,H)
sol = odeint(self.aizawa,x0,t,args=(LH,dynamics_params))
elif dynamics_type =='Chen-Lee':
lenx0 = 3*n
if init_cond is None:
self.init_cond = self.generate_initial_condition(lenx0,method=method,init_cond_type=init_cond_type,init_cond_params=init_cond_params,init_cond_offset=init_cond_offset,p_norm=p_norm,scale=scale)
else:
self.init_cond = init_cond
if coupling_matrix is None:
#assume coupling only through the x component
H = np.eye(3)
H[1,1] = 0
H[2,2] = 0
else:
H = coupling_matrix
x0 = self.init_cond
LH = coupling_strength*np.kron(L,H)
sol = odeint(self.chen_lee,x0,t,args=(LH,dynamics_params))
elif dynamics_type =='Arneodo':
lenx0 = 3*n
if init_cond is None:
self.init_cond = self.generate_initial_condition(lenx0,method=method,init_cond_type=init_cond_type,init_cond_params=init_cond_params,init_cond_offset=init_cond_offset,p_norm=p_norm,scale=scale)
else:
self.init_cond = init_cond
if coupling_matrix is None:
#assume coupling only through the x component
H = np.eye(3)
H[1,1] = 0
H[2,2] = 0
else:
H = coupling_matrix
x0 = self.init_cond
LH = coupling_strength*np.kron(L,H)
sol = odeint(self.arneodo,x0,t,args=(LH,dynamics_params))
elif dynamics_type =='Sprott-B':
lenx0 = 3*n
if init_cond is None:
self.init_cond = self.generate_initial_condition(lenx0,method=method,init_cond_type=init_cond_type,init_cond_params=init_cond_params,init_cond_offset=init_cond_offset,p_norm=p_norm,scale=scale)
else:
self.init_cond = init_cond
if coupling_matrix is None:
#assume coupling only through the x component
H = np.eye(3)
H[1,1] = 0
H[2,2] = 0
else:
H = coupling_matrix
x0 = self.init_cond
LH = coupling_strength*np.kron(L,H)
sol = odeint(self.sprottb,x0,t,args=(LH,dynamics_params))
elif dynamics_type =='Sprott-Linz-F':
lenx0 = 3*n
if init_cond is None:
self.init_cond = self.generate_initial_condition(lenx0,method=method,init_cond_type=init_cond_type,init_cond_params=init_cond_params,init_cond_offset=init_cond_offset,p_norm=p_norm,scale=scale)
else:
self.init_cond = init_cond
if coupling_matrix is None:
#assume coupling only through the x component
H = np.eye(3)
H[1,1] = 0
H[2,2] = 0
else:
H = coupling_matrix
x0 = self.init_cond
LH = coupling_strength*np.kron(L,H)
sol = odeint(self.sprott_linzf,x0,t,args=(LH,dynamics_params))
elif dynamics_type =='Dadras':
lenx0 = 3*n
if init_cond is None:
self.init_cond = self.generate_initial_condition(lenx0,method=method,init_cond_type=init_cond_type,init_cond_params=init_cond_params,init_cond_offset=init_cond_offset,p_norm=p_norm,scale=scale)