-
Notifications
You must be signed in to change notification settings - Fork 0
/
odelibrary.py
1322 lines (1059 loc) · 39.6 KB
/
odelibrary.py
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
import numpy as np
from scipy.integrate import solve_ivp
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, Matern, WhiteKernel, ConstantKernel
import json
from time import time
# from matplotlib import pyplot
# from numba import jitclass # import the decorator
# from numba import boolean, int64, float32, float64 # import the types
from pdb import set_trace as bp
# Correspondence with Dima via Whatsapp on Feb 24, 2020:
# RK45 (explicit) for slow-system-only
# RK45 (implicit) aka Radau for multi-scale-system
# In both cases, set abstol to 1e-6, reltol to 1e-3, dtmax to 1e-3
# L96spec = [
# ('K', int64), # a simple scalar field
# ('J', int64), # a simple scalar field
# ('hx', float64[:]), # a simple scalar field
# ('hy', float64), # a simple scalar field
# ('F', float64), # a simple scalar field
# ('eps', float64), # a simple scalar field
# ('k0', float64), # a simple scalar field
# ('slow_only', boolean), # a simple scalar field
# ('xk_star', float64[:]) # a simple scalar field
# ]
def file_to_dict(fname):
with open(fname) as f:
my_dict = json.load(f)
return my_dict
def generate_data_set(ode, delta_t=0.01, t_transient=100, t_data=100, rng_seed=None, solver_type='default'):
# load solver dict
solver_dict='Config/solver_settings.json'
foo = file_to_dict(solver_dict)
solver_settings = foo[solver_type]
# ode = L63()
f_ode = lambda t, y: ode.rhs(y,t)
def simulate_traj(T1, T2):
if rng_seed is not None:
np.random.seed(rng_seed)
t0 = 0
u0 = ode.get_inits()
print("Initial transients...")
tstart = time()
t_span = [t0, T1]
t_eval = np.array([t0+T1])
# sol = solve_ivp(fun=f_ode, t_span=t_span, y0=u0, t_eval=t_eval, **solver_settings)
sol = my_solve_ivp(ic=u0, f_rhs=f_ode, t_span=t_span, t_eval=t_eval, settings=solver_settings)
print('took', '{:.2f}'.format((time() - tstart)/60),'minutes')
print("Integration...")
tstart = time()
u0 = np.squeeze(sol)
t_span = [t0, T2]
t_eval = np.arange(t0, T2+delta_t, delta_t)
# sol = solve_ivp(fun=lambda t, y: self.rhs(t0, y0), t_span=t_span, y0=u0, method=testcontinuous_ode_int_method, rtol=testcontinuous_ode_int_rtol, atol=testcontinuous_ode_int_atol, max_step=testcontinuous_ode_int_max_step, t_eval=t_eval)
# sol = solve_ivp(fun=f_ode, t_span=t_span, y0=u0, t_eval=t_eval, **solver_settings)
sol = my_solve_ivp(ic=u0, f_rhs=f_ode, t_span=t_span, t_eval=t_eval, settings=solver_settings)
print('took', '{:.2f}'.format((time() - tstart)/60),'minutes')
return sol
# make 1 trajectory
x = simulate_traj(T1=t_transient, T2=t_data)
# get xdot
xdot = np.zeros_like(x)
for i in range(x.shape[-0]):
xdot[i] = ode.rhs(x[i], t=0)
return x, xdot
def my_solve_ivp(ic, f_rhs, t_eval, t_span, settings):
u0 = np.copy(ic)
if settings['method']=='Euler':
dt = settings['dt']
u_sol = np.zeros((len(t_eval), len(ic)))
u_sol[0] = u0
for i in range(len(t_eval)-1):
t = t_eval[i]
rhs = f_rhs(t, u0)
u0 += dt * rhs
u_sol[i] = u0
else:
sol = solve_ivp(fun=lambda t, y: f_rhs(t, y), t_span=t_span, y0=u0, t_eval=t_eval, **settings)
u_sol = sol.y.T
return np.squeeze(u_sol)
class LDS_COUPLED_X:
"""
A simple class that implements a coupled linear dynamical system
The class computes RHS's to make use of scipy's ODE solvers.
Parameters:
A
"""
def __init__(_s,
A = np.array([[0, 1], [-1, 0]]),
eps_min = 0.001,
eps_max = 0.05,
h = 3.0,
share_gp=True,
add_closure=False):
'''
Initialize an instance: setting parameters and xkstar
'''
_s.share_gp = share_gp
_s.A = A
_s.hx = h # just useful when re-using L96 code
_s.eps_min = eps_min
_s.eps_max = eps_max
_s.K = _s.A.shape[0] # slow state dims
_s.J = _s.A.shape[0] # fast state dims
_s.slow_only = False
_s.exchangeable_states = False
_s.add_closure = add_closure
def generate_data(_s, rng_seed, t_transient, t_data, delta_t, solver_type='default'):
return generate_data_set(_s, rng_seed, t_transient, t_data, delta_t, solver_type)
def get_inits(_s):
state_inits = np.random.uniform(low=-1, high=1, size=_s.K+_s.J)
# normalize inits so that slow and fast system both start on unit circle
state_inits[:_s.K] /= np.sqrt(np.sum(state_inits[:_s.K]**2))
state_inits[_s.K:] /= np.sqrt(np.sum(state_inits[_s.K:]**2))
return state_inits
def get_state_names(_s):
return ['X_'+ str(k+1) for k in range(_s.K)]
def plot_state_indices(_s):
return [0, _s.K]
def slow(_s, x, t):
''' Full system RHS '''
foo_rhs = _s.A @ x
return foo_rhs
def eps_f(_s, x):
return _s.eps_min + 2 * (_s.eps_max - _s.eps_min) * (np.prod(x))**2
def full(_s, z, t):
''' Full system RHS '''
x = z[:_s.K]
y = z[_s.K:]
foo_rhs = np.empty(_s.K + _s.J)
foo_rhs[:_s.K] = _s.A @ x + _s.hx*y
foo_rhs[_s.K:] = _s.A @ y / _s.eps_f(x)
return foo_rhs
def rhs(_s, z, t):
if _s.slow_only:
foo_rhs = _s.slow(z, t)
else:
foo_rhs = _s.full(z, t)
if _s.add_closure:
foo_rhs += _s.simulate(z)
return foo_rhs
def regressed(_s, x, t):
''' Only slow variables with RHS learned from data '''
rhs = _s.rhs(x,t)
# add data-learned coupling term
rhs += _s.simulate(x)
return rhs
def set_stencil(_s, left = 0, right = 0):
_s.stencil = np.arange(left, 1 + right)
def single_step_implied_Ybar(_s, Xnow, Xnext, delta_t):
# use an euler scheme to back-out the implied avg Ybar_t from X_t and X_t+1
Ybar = (Xnext - Xnow)/delta_t - _s.rhs(S=Xnow, t=None)
return Ybar
def implied_Ybar(_s, X_in, X_out, delta_t):
# the idea is that X_in are true data coming from a test/training set
# Xout(k) is the 1-step-ahed prediction associated to Xin(k).
# In other words Xout(k) = Psi-ML(Xin(k))
T = X_in.shape[0]
Ybar = np.zeros( (T, _s.K) )
for t in range(T):
Ybar[t,:] = _s.single_step_implied_Ybar(Xnow=X_in[t,:], Xnext=X_out[t,:], delta_t=delta_t)
return Ybar
def get_state_limits(_s):
lims = (None,None)
return lims
def set_predictor(_s, predictor):
_s.predictor = predictor
# def set_G0_predictor(_s):
# _s.predictor = lambda x: _s.hy * x
def set_null_predictor(_s):
_s.predictor = lambda x: 0
def simulate(_s, slow):
if _s.share_gp:
return np.reshape(_s.predictor(_s.apply_stencil(slow)), (-1,))
else:
return np.reshape(_s.predictor(slow.reshape(1,-1)), (-1,))
def apply_stencil(_s, slow):
# behold: the blackest of all black magic!
# (in a year, I will not understand what this does)
# the idea: shift xk's so that each row corresponds to the stencil:
# (x_{k-1}, x_{k}, x_{k+1}), for example,
# based on '_s.stencil' and 'slow' array (which is (x1,...,xK) )
return slow[np.add.outer(np.arange(_s.K), _s.stencil) % _s.K]
class LDS_COUPLED:
"""
A simple class that implements a coupled linear dynamical system
The class computes RHS's to make use of scipy's ODE solvers.
Parameters:
A
"""
def __init__(_s,
A = np.array([[0, 1], [-1, 0]]),
eps = 0.05,
h = 0.1,
share_gp=True,
add_closure=False):
'''
Initialize an instance: setting parameters and xkstar
'''
_s.share_gp = share_gp
_s.A = A
_s.hx = h # just useful when re-using L96 code
_s.eps = eps
_s.K = _s.A.shape[0] # slow state dims
_s.J = _s.A.shape[0] # fast state dims
_s.slow_only = False
_s.exchangeable_states = False
_s.add_closure = add_closure
def generate_data(_s, **kwargs):
return generate_data_set(_s, **kwargs)
def get_inits(_s):
state_inits = np.random.uniform(low=-1, high=1, size=_s.K+_s.J)
# normalize inits so that slow and fast system both start on unit circle
state_inits[:_s.K] /= np.sqrt(np.sum(state_inits[:_s.K]**2))
state_inits[_s.K:] /= np.sqrt(np.sum(state_inits[_s.K:]**2))
return state_inits
def get_state_names(_s):
return ['X_'+ str(k+1) for k in range(_s.K)]
def plot_state_indices(_s):
return [0, _s.K]
def slow(_s, x, t):
''' Full system RHS '''
foo_rhs = _s.A @ x
return foo_rhs
def full(_s, z, t):
''' Full system RHS '''
x = z[:_s.K]
y = z[_s.K:]
foo_rhs = np.empty(_s.K + _s.J)
foo_rhs[:_s.K] = _s.A @ x + _s.hx*y
foo_rhs[_s.K:] = _s.A @ y / _s.eps
return foo_rhs
def rhs(_s, z, t):
if _s.slow_only:
foo_rhs = _s.slow(z, t)
else:
foo_rhs = _s.full(z, t)
if _s.add_closure:
foo_rhs += _s.simulate(z)
return foo_rhs
def regressed(_s, x, t):
''' Only slow variables with RHS learned from data '''
rhs = _s.rhs(x,t)
# add data-learned coupling term
rhs += _s.simulate(x)
return rhs
def set_stencil(_s, left = 0, right = 0):
_s.stencil = np.arange(left, 1 + right)
def single_step_implied_Ybar(_s, Xnow, Xnext, delta_t):
# use an euler scheme to back-out the implied avg Ybar_t from X_t and X_t+1
Ybar = (Xnext - Xnow)/delta_t - _s.rhs(S=Xnow, t=None)
return Ybar
def implied_Ybar(_s, X_in, X_out, delta_t):
# the idea is that X_in are true data coming from a test/training set
# Xout(k) is the 1-step-ahed prediction associated to Xin(k).
# In other words Xout(k) = Psi-ML(Xin(k))
T = X_in.shape[0]
Ybar = np.zeros( (T, _s.K) )
for t in range(T):
Ybar[t,:] = _s.single_step_implied_Ybar(Xnow=X_in[t,:], Xnext=X_out[t,:], delta_t=delta_t)
return Ybar
def get_state_limits(_s):
lims = (None,None)
return lims
def set_predictor(_s, predictor):
_s.predictor = predictor
# def set_G0_predictor(_s):
# _s.predictor = lambda x: _s.hy * x
def set_null_predictor(_s):
_s.predictor = lambda x: 0
def simulate(_s, slow):
if _s.share_gp:
return np.reshape(_s.predictor(_s.apply_stencil(slow)), (-1,))
else:
return np.reshape(_s.predictor(slow.reshape(1,-1)), (-1,))
def apply_stencil(_s, slow):
# behold: the blackest of all black magic!
# (in a year, I will not understand what this does)
# the idea: shift xk's so that each row corresponds to the stencil:
# (x_{k-1}, x_{k}, x_{k+1}), for example,
# based on '_s.stencil' and 'slow' array (which is (x1,...,xK) )
return slow[np.add.outer(np.arange(_s.K), _s.stencil) % _s.K]
class LDS:
"""
A simple class that implements a linear dynamical system
The class computes RHS's to make use of scipy's ODE solvers.
Parameters:
A
"""
def __init__(_s,
A = np.array([[0, 5], [-5, 0]]), share_gp=True, add_closure=False):
'''
Initialize an instance: setting parameters and xkstar
'''
_s.share_gp = share_gp
_s.A = A
_s.K = _s.A.shape[0] # state dims
_s.hx = 1 # just useful when re-using L96 code
_s.slow_only = False
_s.exchangeable_states = False
_s.add_closure = add_closure
def generate_data(_s, **kwargs):
return generate_data_set(_s, **kwargs)
def get_inits(_s):
state_inits = np.random.randn(_s.K)
return state_inits
def get_state_names(_s):
return ['X_'+ str(k+1) for k in range(_s.K)]
def plot_state_indices(_s):
return [0, _s.K]
def slow(_s, y, t):
return _s.rhs(y,t)
def rhs(_s, S, t):
''' Full system RHS '''
foo_rhs = _s.A @ S
if _s.add_closure:
foo_rhs += _s.simulate(S)
return foo_rhs
def regressed(_s, x, t):
''' Only slow variables with RHS learned from data '''
rhs = _s.rhs(x,t)
# add data-learned coupling term
rhs += _s.simulate(x)
return rhs
def set_stencil(_s, left = 0, right = 0):
_s.stencil = np.arange(left, 1 + right)
def single_step_implied_Ybar(_s, Xnow, Xnext, delta_t):
# use an euler scheme to back-out the implied avg Ybar_t from X_t and X_t+1
Ybar = (Xnext - Xnow)/delta_t - _s.rhs(S=Xnow, t=None)
return Ybar
def implied_Ybar(_s, X_in, X_out, delta_t):
# the idea is that X_in are true data coming from a test/training set
# Xout(k) is the 1-step-ahed prediction associated to Xin(k).
# In other words Xout(k) = Psi-ML(Xin(k))
T = X_in.shape[0]
Ybar = np.zeros( (T, _s.K) )
for t in range(T):
Ybar[t,:] = _s.single_step_implied_Ybar(Xnow=X_in[t,:], Xnext=X_out[t,:], delta_t=delta_t)
return Ybar
def get_state_limits(_s):
lims = (None,None)
return lims
def set_predictor(_s, predictor):
_s.predictor = predictor
# def set_G0_predictor(_s):
# _s.predictor = lambda x: _s.hy * x
def set_null_predictor(_s):
_s.predictor = lambda x: 0
def simulate(_s, slow):
if _s.share_gp:
return np.reshape(_s.predictor(_s.apply_stencil(slow)), (-1,))
else:
return np.reshape(_s.predictor(slow.reshape(1,-1)), (-1,))
def apply_stencil(_s, slow):
# behold: the blackest of all black magic!
# (in a year, I will not understand what this does)
# the idea: shift xk's so that each row corresponds to the stencil:
# (x_{k-1}, x_{k}, x_{k+1}), for example,
# based on '_s.stencil' and 'slow' array (which is (x1,...,xK) )
return slow[np.add.outer(np.arange(_s.K), _s.stencil) % _s.K]
# @jitclass(L96spec)
class L96M:
"""
A simple class that implements Lorenz '96M model w/ slow and fast variables
The class computes RHS's to make use of scipy's ODE solvers.
Parameters:
K, J, hx, hy, F, eps
The convention is that the first K variables are slow, while the rest K*J
variables are fast.
"""
def __init__(_s,
K = 9, J = 8, hx = -0.8, hy = 1, F = 10, eps = 2**(-7), k0 = 0, slow_only=False, dima_style=False, share_gp=True, add_closure=False):
'''
Initialize an instance: setting parameters and xkstar
'''
hx = hx * np.ones(K)
if hx.size != K:
raise ValueError("'hx' must be a 1D-array of size 'K'")
_s.predictor = None
_s.dima_style = dima_style
_s.share_gp = share_gp # if true, then GP is R->R and is applied to each state independently.
# if share_gp=False, then GP is R^K -> R^K and is applied to the whole state vector at once.
_s.slow_only = slow_only
_s.K = K
_s.J = J
_s.hx = hx
_s.hy = hy
_s.F = F
_s.eps = eps
_s.k0 = k0 # for filtered integration
_s.exchangeable_states = True
# 0
#_s.xk_star = np.random.rand(K) * 15 - 5
# 1
#_s.xk_star = np.ones(K) * 5
# 2
#_s.xk_star = np.ones(K) * 2
#_s.xk_star[K//2:] = -0.2
# 3
_s.xk_star = 0.0 * np.zeros(K)
_s.xk_star[0] = 5
_s.xk_star[1] = 5
_s.xk_star[-1] = 5
_s.add_closure = add_closure
def generate_data(_s, **kwargs):
return generate_data_set(_s, **kwargs)
def get_inits(_s, sigma = 15, mu = -5):
z0 = np.zeros((_s.K + _s.K * _s.J))
z0[:_s.K] = mu + np.random.rand(_s.K) * sigma
if _s.slow_only:
return z0[:_s.K]
else:
for k_ in range(_s.K):
z0[_s.K + k_*_s.J : _s.K + (k_+1)*_s.J] = z0[k_]
return z0
def get_state_limits(_s):
if _s.K==4 and _s.J==4:
lims = (-27.5, 36.5)
elif _s.K==9 and _s.J==8:
lims = (-9.5, 14.5)
else:
lims = (None,None)
return lims
def get_fast_state_names(_s):
state_names = []
for k in range(_s.K):
state_names += ['Y_' + str(j+1) + ',' + str(k+1) for j in range(_s.J)]
return state_names
def get_slow_state_names(_s):
state_names = ['X_'+ str(k+1) for k in range(_s.K)]
return state_names
def get_state_names(_s, get_all=False):
state_names = _s.get_slow_state_names()
if get_all or not _s.slow_only:
state_names += _s.get_fast_state_names()
return state_names
def get_fast_state_indices(_s):
return np.arange(_s.K, _s.K + _s.K * _s.J)
def plot_state_indices(_s):
if _s.slow_only:
return [0, 1, _s.K-1, _s.K-2] # return a 4 coupled slow variables
else:
return [0, _s.K] # return 1st slow variable and 1st coupled fast variable
def set_predictor(_s, predictor):
_s.predictor = predictor
def set_G0_predictor(_s):
_s.predictor = lambda x: _s.hy * x
def set_null_predictor(_s):
_s.predictor = lambda x: 0
def set_stencil(_s, left = 0, right = 0):
_s.stencil = np.arange(left, 1 + right)
def hit_value(_s, k, val):
return lambda t, z: z[k] - val
def rhs(_s, z, t):
if _s.slow_only:
foo_rhs = _s.slow(z, t)
else:
foo_rhs = _s.full(z, t)
if _s.add_closure:
foo_rhs += _s.simulate(z)
return foo_rhs
def full(_s, z, t):
''' Full system RHS '''
K = _s.K
J = _s.J
rhs = np.empty(K + K*J)
x = z[:K]
y = z[K:]
### slow variables subsystem ###
# compute Yk averages
Yk = _s.compute_Yk(z)
# three boundary cases
rhs[0] = -x[K-1] * (x[K-2] - x[1]) - x[0]
rhs[1] = -x[0] * (x[K-1] - x[2]) - x[1]
rhs[K-1] = -x[K-2] * (x[K-3] - x[0]) - x[K-1]
# general case
rhs[2:K-1] = -x[1:K-2] * (x[0:K-3] - x[3:K]) - x[2:K-1]
# add forcing
rhs[:K] += _s.F
# add coupling w/ fast variables via averages
# XXX verify this (twice: sign and vector-vector multiplication)
rhs[:K] += _s.hx * Yk
#rhs[:K] -= _s.hx * Yk
### fast variables subsystem ###
# three boundary cases
rhs[K] = -y[1] * (y[2] - y[-1]) - y[0]
rhs[-2] = -y[-1] * (y[0] - y[-3]) - y[-2]
rhs[-1] = -y[0] * (y[1] - y[-2]) - y[-1]
# general case
rhs[K+1:-2] = -y[2:-1] * (y[3:] - y[:-3]) - y[1:-2]
# add coupling w/ slow variables
for k in range(K):
rhs[K + k*J : K + (k+1)*J] += _s.hy * x[k]
# divide by epsilon
rhs[K:] /= _s.eps
return rhs
def decoupled(_s, z, t):
''' Only fast variables with fixed slow ones to verify ergodicity '''
K = _s.K
J = _s.J
_i = _s.fidx_dec
rhs = np.empty(K*J)
## boundary: k = 0
# boundary: j = 0, j = J-2, j = J-1
rhs[_i(0,0)] = \
-z[_i(1,0)] * (z[_i(2,0)] - z[_i(J-1,K-1)]) - z[_i(0,0)]
rhs[_i(J-2,0)] = \
-z[_i(J-1,0)] * (z[_i(0,1)] - z[_i(J-3,0)]) - z[_i(J-2,0)]
rhs[_i(J-1,0)] = \
-z[_i(0,1)] * (z[_i(1,1)] - z[_i(J-2,0)]) - z[_i(J-1,0)]
# general (for k = 0)
for j in range(1, J-2):
rhs[_i(j,0)] = \
-z[_i(j+1,0)] * (z[_i(j+2,0)] - z[_i(j-1,0)]) - z[_i(j,0)]
## boundary: k = 0 (end)
## boundary: k = K-1
# boundary: j = 0, j = J-2, j = J-1
rhs[_i(0,K-1)] = \
-z[_i(1,K-1)] * (z[_i(2,K-1)] - z[_i(J-1,K-2)]) - z[_i(0,K-1)]
rhs[_i(J-2,K-1)] = \
-z[_i(J-1,K-1)] * (z[_i(0,0)] - z[_i(J-3,K-1)]) - z[_i(J-2,K-1)]
rhs[_i(J-1,K-1)] = \
-z[_i(0,0)] * (z[_i(1,0)] - z[_i(J-2,K-1)]) - z[_i(J-1,K-1)]
# general (for k = K-1)
for j in range(1, J-2):
rhs[_i(j,K-1)] = \
-z[_i(j+1,K-1)] * (z[_i(j+2,K-1)] - z[_i(j-1,K-1)]) - z[_i(j,K-1)]
## boundary: k = K-1 (end)
## general case for k (w/ corresponding inner boundary conditions)
for k in range(1, K-1):
# boundary: j = 0, j = J-2, j = J-1
rhs[_i(0,k)] = \
-z[_i(1,k)] * (z[_i(2,k)] - z[_i(J-1,k-1)]) - z[_i(0,k)]
rhs[_i(J-2,k)] = \
-z[_i(J-1,k)] * (z[_i(0,k+1)] - z[_i(J-3,k)]) - z[_i(J-2,k)]
rhs[_i(J-1,k)] = \
-z[_i(0,k+1)] * (z[_i(1,k+1)] - z[_i(J-2,k)]) - z[_i(J-1,k)]
# general case for j
for j in range(1, J-2):
rhs[_i(j,k)] = \
-z[_i(j+1,k)] * (z[_i(j+2,k)] - z[_i(j-1,k)]) - z[_i(j,k)]
## add coupling w/ slow variables
for k in range(0, K):
rhs[k*J : (k+1)*J] += _s.hy * _s.xk_star[k]
## divide by epsilon
rhs /= _s.eps
return rhs
def balanced(_s, x, t):
''' Only slow variables with balanced RHS '''
K = _s.K
rhs = np.empty(K)
# three boundary cases: k = 0, k = 1, k = K-1
rhs[0] = -x[K-1] * (x[K-2] - x[1]) - (1 - _s.hx[0]*_s.hy) * x[0]
rhs[1] = -x[0] * (x[K-1] - x[2]) - (1 - _s.hx[1]*_s.hy) * x[1]
rhs[K-1] = -x[K-2] * (x[K-3] - x[0]) - (1 - _s.hx[K-1]*_s.hy) * x[K-1]
# general case
for k in range(2, K-1):
rhs[k] = -x[k-1] * (x[k-2] - x[k+1]) - (1 - _s.hx[k]*_s.hy) * x[k]
# add forcing
rhs += _s.F
return rhs
def slow(_s, x, t):
''' Only slow variables with RHS learned from data '''
K = _s.K
rhs = np.empty(K)
# three boundary cases: k = 0, k = 1, k = K-1
rhs[0] = -x[K-1] * (x[K-2] - x[1]) - x[0]
rhs[1] = -x[0] * (x[K-1] - x[2]) - x[1]
rhs[K-1] = -x[K-2] * (x[K-3] - x[0]) - x[K-1]
# general case
for k in range(2, K-1):
rhs[k] = -x[k-1] * (x[k-2] - x[k+1]) - x[k]
# add forcing
rhs += _s.F
return rhs
def regressed(_s, x, t):
''' Only slow variables with RHS learned from data '''
K = _s.K
rhs = np.empty(K)
# three boundary cases: k = 0, k = 1, k = K-1
rhs[0] = -x[K-1] * (x[K-2] - x[1]) - x[0]
rhs[1] = -x[0] * (x[K-1] - x[2]) - x[1]
rhs[K-1] = -x[K-2] * (x[K-3] - x[0]) - x[K-1]
# general case
for k in range(2, K-1):
rhs[k] = -x[k-1] * (x[k-2] - x[k+1]) - x[k]
# add forcing
rhs += _s.F
# add data-learned coupling term
# XXX verify this (twice: sign and vector-vector multiplication)
if _s.dima_style:
rhs += _s.hx * _s.simulate(x)
else:
rhs += _s.simulate(x)
return rhs
def filtered(_s, t, z):
''' Only slow variables with one set of fast ones and RHS learned from data
Vector z is of size (K + J), i.e. all slow variables + fast variables at k0
'''
K = _s.K
J = _s.J
rhs = np.empty(K + J)
### slow variables subsystem ###
# compute Yk average for k0
Yk0 = z[K:].sum() / J
# three boundary cases: k = 0, k = 1, k = K-1
rhs[0] = -z[K-1] * (z[K-2] - z[1]) - z[0]
rhs[1] = -z[0] * (z[K-1] - z[2]) - z[1]
rhs[K-1] = -z[K-2] * (z[K-3] - z[0]) - z[K-1]
# general case
for k in range(2, K-1):
rhs[k] = -z[k-1] * (z[k-2] - z[k+1]) - z[k]
# add forcing
rhs[:K] += _s.F
# add coupling w/ fast variables via average for k0
# NOTE This has to be tested; maybe predictor everywhere is better
rhs[_s.k0] += _s.hx[_s.k0] * Yk0
# add coupling w/ the rest via simulation
wo_k0 = np.r_[:_s.k0, _s.k0+1:K]
Yk_simul = _s.simulate(z[:K])
rhs[wo_k0] += _s.hx[wo_k0] * Yk_simul[wo_k0]
#rhs[_s.k0] += _s.hx[_s.k0] * Yk_simul[_s.k0]
### fast variables subsystem ###
# boundary: j = 0, j = J-2, j = J-1
rhs[K] = -z[K+1] * (z[K+2] - z[-1]) - z[K]
rhs[K+J-2] = -z[K+J-1] * (z[K] - z[K+J-3]) - z[K+J-2]
rhs[K+J-1] = -z[K] * (z[K+1] - z[K+J-2]) - z[K+J-1]
# general case for j
for j in range(1, J-2):
rhs[K+j] = -z[K+j+1] * (z[K+j+2] - z[K+j-1]) - z[K+j]
## add coupling w/ the k0 slow variable
rhs[K:] += _s.hy * z[_s.k0]
## divide by epsilon
rhs[K:] /= _s.eps
return rhs
def fidx(_s, j, k):
"""Fast-index evaluation (based on the convention, see class description)"""
return _s.K + k*_s.J + j
def fidx_dec(_s, j, k):
"""Fast-index evaluation for the decoupled system"""
return k*_s.J + j
def simulate(_s, slow):
if _s.share_gp:
return np.reshape(_s.predictor(_s.apply_stencil(slow)), (-1,))
else:
return np.reshape(_s.predictor(slow.reshape(1,-1)), (-1,))
def simulate_OLD(_s, slow):
return np.reshape(_s.predictor(_s.apply_stencil(slow)), (-1,))
def single_step_implied_Ybar(_s, Xnow, Xnext, delta_t):
# use an euler scheme to back-out the implied avg Ybar_t from X_t and X_t+1
Ybar = (Xnext - Xnow)/delta_t - _s.slow(x=Xnow, t=None)
# divide by hx
Ybar /= _s.hx
return Ybar
def implied_Ybar(_s, X_in, X_out, delta_t):
# the idea is that X_in are true data coming from a test/training set
# Xout(k) is the 1-step-ahed prediction associated to Xin(k).
# In other words Xout(k) = Psi-ML(Xin(k))
T = X_in.shape[0]
Ybar = np.zeros( (T, _s.K) )
for t in range(T):
Ybar[t,:] = _s.single_step_implied_Ybar(Xnow=X_in[t,:], Xnext=X_out[t,:], delta_t=delta_t)
return Ybar
def compute_Yk(_s, z):
return z[_s.K:].reshape( (_s.J, _s.K), order = 'F').sum(axis = 0) / _s.J
# TODO delete these two lines after testing
#_s.Yk = z[_s.K:].reshape( (_s.J, _s.K), order = 'F').sum(axis = 0)
#_s.Yk /= _s.J
def gather_pairs(_s, tseries):
n = tseries.shape[1]
pairs = np.empty( (_s.K * n, _s.stencil.size + 1) )
for j in range(n):
pairs[_s.K * j : _s.K * (j+1), :-1] = _s.apply_stencil(tseries[:_s.K, j])
pairs[_s.K * j : _s.K * (j+1), -1] = _s.compute_Yk(tseries[:,j])
return pairs
def gather_pairs_k0(_s, tseries):
n = tseries.shape[1]
pairs = np.empty( (n, 2) )
for j in range(n):
pairs[j, 0] = tseries[_s.k0, j]
pairs[j, 1] = tseries[_s.K:, j].sum() / _s.J
return pairs
def apply_stencil(_s, slow):
# behold: the blackest of all black magic!
# (in a year, I will not understand what this does)
# the idea: shift xk's so that each row corresponds to the stencil:
# (x_{k-1}, x_{k}, x_{k+1}), for example,
# based on '_s.stencil' and 'slow' array (which is (x1,...,xK) )
return slow[np.add.outer(np.arange(_s.K), _s.stencil) % _s.K]
################################################################################
# end of L96M ##################################################################
################################################################################
# L63spec = [
# ('a', float32), # a simple scalar field
# ('b', float32), # a simple scalar field
# ('c', float32), # a simple scalar field
# ]
# @jitclass(L63spec)
class L63:
"""
A simple class that implements Lorenz 63 model
The class computes RHS's to make use of scipy's ODE solvers.
Parameters:
a, b, c
"""
def __init__(_s,
a = 10, b = 28, c = 8/3, epsGP=1, share_gp=False, add_closure=False, random_closure=False):
'''
Initialize an instance: setting parameters and xkstar
'''
_s.share_gp = share_gp
_s.a = a
_s.b = b
_s.c = c
_s.epsGP = epsGP
_s.K = 3 # state dims
_s.hx = 1 # just useful when re-using L96 code
_s.slow_only = False
_s.exchangeable_states = False
_s.add_closure = add_closure
_s.random_closure = random_closure
_s.has_diverged = False
if _s.random_closure:
_s.add_closure = True
_s.set_random_predictor()
def generate_data(_s, **kwargs):
return generate_data_set(_s, **kwargs)
def get_inits(_s):
(xmin, xmax) = (-10,10)
(ymin, ymax) = (-20,30)
(zmin, zmax) = (10,40)
xrand = xmin+(xmax-xmin)*np.random.random()
yrand = ymin+(ymax-ymin)*np.random.random()
zrand = zmin+(zmax-zmin)*np.random.random()
state_inits = np.array([xrand, yrand, zrand])
return state_inits
def get_state_names(_s):
return ['x','y','z']
def plot_state_indices(_s):
return [0,1,2]
def slow(_s, y, t):
return _s.rhs(y,t)
def is_divergent(_s, S):
''' define kill switch for when integration has blown up'''
if not _s.has_diverged:
_s.has_diverged = any(np.abs(S) > 200)
if _s.has_diverged:
print('INTEGRATION HAS DIVERGED!!!', S)
return _s.has_diverged
def rhs(_s, S, t):
''' Full system RHS '''
a = _s.a
b = _s.b
c = _s.c
x = S[0]
y = S[1]
z = S[2]
if _s.is_divergent(S):
return np.zeros(3)
foo_rhs = np.empty(3)
foo_rhs[0] = -a*x + a*y
foo_rhs[1] = b*x - y - x*z
foo_rhs[2] = -c*z + x*y
if _s.add_closure:
foo_rhs += (_s.epsGP * _s.simulate(S))
return foo_rhs
def regressed(_s, x, t):
''' Only slow variables with RHS learned from data '''
rhs = _s.rhs(x,t)
# add data-learned coupling term
rhs += _s.simulate(x)
return rhs
def set_stencil(_s, left = 0, right = 0):
_s.stencil = np.arange(left, 1 + right)
def single_step_implied_Ybar(_s, Xnow, Xnext, delta_t):
# use an euler scheme to back-out the implied avg Ybar_t from X_t and X_t+1
Ybar = (Xnext - Xnow)/delta_t - _s.rhs(S=Xnow, t=None)
return Ybar
def implied_Ybar(_s, X_in, X_out, delta_t):
# the idea is that X_in are true data coming from a test/training set
# Xout(k) is the 1-step-ahed prediction associated to Xin(k).
# In other words Xout(k) = Psi-ML(Xin(k))
T = X_in.shape[0]
Ybar = np.zeros( (T, _s.K) )
for t in range(T):
Ybar[t,:] = _s.single_step_implied_Ybar(Xnow=X_in[t,:], Xnext=X_out[t,:], delta_t=delta_t)
return Ybar
def get_state_limits(_s):
lims = (None,None)
return lims
def set_predictor(_s, predictor):
_s.predictor = predictor
def set_random_predictor(_s):
x1 = np.arange(-20, 20, 5)
x2 = np.arange(-25, 25, 5)
x3 = np.arange(5, 45, 5)
X = np.stack(np.meshgrid(x1, x2, x3), -1).reshape(-1, 3)
GP_ker = ConstantKernel(1.0, (1e-2, 1e5)) * RBF(10.0, (1e-2, 1e+6))
my_gpr = GaussianProcessRegressor(kernel = GP_ker, alpha=0, n_restarts_optimizer=10)
y = np.zeros(X.shape)
for k in range(_s.K):
y[:,k] = np.squeeze(my_gpr.sample_y(X, n_samples=1, random_state=k))
my_gpr.fit(X=X, y=y)