-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_v0.py
587 lines (485 loc) · 19.3 KB
/
main_v0.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
# -*- coding: utf-8 -*-
"""
@author: jl
"""
import numpy as np
from solvers.rk4_N import*
import matplotlib.pyplot as plt
from models.exact_solutions import*
from solvers.rk2_N import rk2_N
from solvers.euler_method import*
from models.SIS_model import SIS_model
from models.SIR_model import SIR_model
from models.automata import*
from models.SIS_model2 import SIS_model2
from models.SIR_model2 import SIR_model2
def test_SIR():
rel_err_S = []
rel_err_I = []
ErrS = []
ErrI = []
N = 22500
x01 = 22400/N
x02 = 100/N
x03 = 0/N
x0 = [x01, x02, x03] # change this according to the entries of your model
c_1 = 1.4
c_2 = 0.2
consts = [c_1, c_2] # change this according to the number of constants of your model
initial_time = 0
final_time = 20
total_number_of_steps = 50
y = rk4_N(SIR_model, x0, consts, initial_time, final_time, total_number_of_steps)
y1, y2, y3 = [], [], []
for ye in y[1]:
y1.append(ye[0])
y2.append(ye[1])
y3.append(ye[2])
plt.figure()
plt.title('Solution Using a RK4 model')
plt.plot(y[0], y1, '-o', label='S')
plt.plot(y[0], y2, '-o', label='I')
plt.plot(y[0], y3, '-o', label='R')
plt.xlabel('time')
plt.ylabel('population')
plt.legend(loc='best')
sir = SIR_sol(x01, x02, x03, c_1, c_2, final_time, total_number_of_steps)
plt.figure()
plt.title('Comparison of the analytical and numerical solution with $\\beta =1.4 $, $\gamma = 0.2$')
plt.plot(sir[3], sir[0], '--b', label='S, analytical solution')
plt.plot(sir[3], sir[1], '--r', label='I, analytical solution')
plt.plot(y[0], y1, '-ob', label='S, RK4')
plt.plot(y[0], y2, '-or', label='I, RK4')
plt.xlabel('time')
plt.ylabel('population')
plt.legend(loc='best')
for c in range(5):
y = rk4_N(SIR_model, x0, consts, initial_time, final_time, total_number_of_steps)
sir = SIR_sol(x01, x02, x03, c_1, c_2, final_time, total_number_of_steps)
y1, y2, y3 = [], [], []
for ye in y[1]:
y1.append(ye[0])
y2.append(ye[1])
y3.append(ye[2])
plt.figure()
plt.title('Analytical and numerical solution with $h$='+str(final_time/total_number_of_steps))
plt.plot(sir[3], sir[0], '--b', label='S, analytical solution')
plt.plot(sir[3], sir[1], '--r', label='I, analytical solution')
plt.plot(y[0], y1, '-ob', label='S, RK4')
plt.plot(y[0], y2, '-or', label='I, RK4')
plt.xlabel('time')
plt.ylabel('population')
plt.legend(loc='best')
rel_err_I.append((np.array(y1) - np.array(sir[0]))/np.array(sir[0]))
rel_err_S.append((np.array(y2) - np.array(sir[1]))/np.array(sir[1]))
plt.figure()
plt.title('Relative Error with $h$='+str(final_time/total_number_of_steps))
plt.plot(rel_err_I[c], '.-', label='I')
plt.plot(rel_err_S[c], '.-', label='S')
plt.xlabel('Step')
plt.ylabel('Error')
plt.legend(loc='best')
if c > 0:
ErrS.append(calc_error(y1_p, y1, 5))
ErrI.append(calc_error(y2_p, y2, 5))
plt.figure()
plt.title('Error of the numerical method with $h$=' + str(final_time / total_number_of_steps))
plt.plot(ErrS[c-1], '.-', label='S')
plt.plot(ErrI[c-1], '.-', label='I')
plt.xlabel('Step')
plt.ylabel('Error')
plt.legend(loc='best')
y1_p = copy.copy(y1)
y2_p = copy.copy(y2)
total_number_of_steps = total_number_of_steps*2
plt.figure()
for i, err_el in enumerate(ErrS):
plt.title('Error obtained using 4th order RK with different step sizes')
plt.plot(np.linspace(initial_time, final_time + 1, len(err_el)), err_el, '.-', label='S, $h$ = {}'.format(final_time/(2*(len(err_el)-1))))
plt.plot(np.linspace(initial_time, final_time + 1, len(err_el)), ErrI[i], '.-', label='I, $h$ = {}'.format(final_time/(2*(len(err_el)-1))))
plt.xlabel('Step')
plt.ylabel('Error')
plt.legend(loc='best')
plt.figure()
for i, err_el in enumerate(rel_err_S):
plt.title('Relative Error for different step sizes')
plt.plot(np.linspace(initial_time, final_time + 1, len(err_el)), err_el, '.-', label='S, $h$ = {}'.format(final_time/(2*(len(err_el)-1))))
plt.plot(np.linspace(initial_time, final_time + 1, len(err_el)), rel_err_I[i], '.-', label='I, $h$ = {}'.format(final_time/(2*(len(err_el)-1))))
plt.xlabel('Step')
plt.ylabel('Error')
plt.legend(loc='best')
for i, err_el in enumerate(ErrS):
plt.figure()
plt.title('Comparison of the errors')
plt.plot(np.linspace(initial_time, final_time + 1, len(err_el)), err_el, '.-', label='S error ($h$ = {})'.format(final_time/(2*(len(err_el)-1))))
plt.plot(np.linspace(initial_time, final_time + 1, len(err_el)), ErrI[i], '.-', label='I error ($h$ = {})'.format(final_time/(2*(len(err_el)-1))))
plt.plot(np.linspace(initial_time, final_time + 1, len(rel_err_S[i+1])), rel_err_S[i+1], '+-', label='S, relative error($h$ = {})'.format(final_time/(2*(len(err_el)-1))))
plt.plot(np.linspace(initial_time, final_time + 1, len(rel_err_I[i+1])), rel_err_I[i+1], '+-', label='I, relative error($h$ = {})'.format(final_time/(2*(len(err_el)-1))))
plt.xlabel('Step')
plt.ylabel('Error')
plt.legend(loc='best')
# this part is part is to play with the different values of your model, if you need it uncomment
"""
#c_1s = [0.1, 0.3, 0.4, 0.6, 0.9, 1.4, 1.5, 1.9, 2.0, 2.1]
#c_1s = [0.05, 0.1, 0.2, 0.5, 1.1, 1.4, 1.7, 1.9, 2.1, 2.5, 2.5]
#c_1s = [2.0, 2.1, 2.3, 2.5, 2.9, 4.1]
#c_1s = [0.1, 0.3, 0.4, 0.6, 0.9]
#c2s = [0.0005, 0.01, 0.05, 0.1]
c_1s = [0.1, 0.5, 0.9, 1.2, 1.4]
c2s = [0.2]
final_time = 10
for c_2 in c2s:
for c_1 in c_1s:
consts[0] = c_1
consts[1] = c_2
j = rk4_N(SIR_model, x0, consts, initial_time, final_time, total_number_of_steps)
g = rk2_N(SIR_model, x0, consts, initial_time, final_time, total_number_of_steps)
e = euler_method(SIR_model, x0, consts, initial_time, final_time, total_number_of_steps)
sir = SIR_sol(x01, x02, x03, c_1, c_2, final_time, total_number_of_steps)
g1, g2, g3 = [], [], []
j1, j2, j3 = [], [], []
e1, e2, e3, = [], [], []
for je in j[1]:
j1.append(je[0])
j2.append(je[1])
j3.append(je[2])
for ge in g[1]:
g1.append(ge[0])
g2.append(ge[1])
g3.append(ge[2])
for ee in e[1]:
e1.append(ee[0])
e2.append(ee[1])
e3.append(ee[2])
plt.figure()
plt.title("".join(['c1 = ', str(c_1), ',', 'c2 = ', str(c_2)]))
plt.plot(j[0], j1, 'xb')
plt.plot(j[0], j2, 'xr')
plt.plot(g[0], g1, '^b')
plt.plot(g[0], g2, '^r')
plt.plot(e[0], e1, 'ob')
plt.plot(e[0], e2, 'or')
plt.plot(sir[3], sir[0], '--b')
plt.plot(sir[3], sir[1], '--r')
plt.figure()
plt.title('Exact Solution')
plt.plot(sir[3], sir[0], '--b')
plt.plot(sir[3], sir[1], '--r')
"""
def test_SIS():
N = 22500
x01 = 22400/N
x02 = 100/N
x03 = 0/N
x0 = [x01, x02]
initial_time = 0
final_time = 20
total_number_of_steps = 50
c_1s = [0.1, 0.2, 0.3, 0.4, 0.6, 0.9, 1.1]
c_1s = [0.9]
c_2 = 0.01
# to save the relative errors
Errk2i = []
Errk2s = []
Errk4i = []
Errk4s = []
Erre2i = []
Erre2s = []
# to save the errors
Ee_rk4s = []
Ee_rk4i = []
Ee_rk2s = []
Ee_rk2i = []
Ee_euls = []
Ee_euli = []
for i in range(5):
for c_1 in c_1s:
consts = [c_1, c_2] # change this according to the number of constants of your model
consts[0] = c_1
j = rk4_N(SIS_model, x0, consts, initial_time, final_time, total_number_of_steps)
g = rk2_N(SIS_model, x0, consts, initial_time, final_time, total_number_of_steps)
e = euler_method(SIS_model, x0, consts, initial_time, final_time, total_number_of_steps)
g1, g2, g3 = [], [], []
j1, j2, j3 = [], [], []
e1, e2, = [], []
for je in j[1]:
j1.append(je[0])
j2.append(je[1])
for ge in g[1]:
g1.append(ge[0])
g2.append(ge[1])
for ee in e[1]:
e1.append(ee[0])
e2.append(ee[1])
plt.figure()
plt.title('Simulation of the SIS model with $\\beta$ = ' + str(c_1) + ' and $\gamma$ = ' + str(c_2))
plt.plot(j[0], j1, 'xr', label='S, 4th order RK')
plt.plot(j[0], j2, 'xb', label='I, 4th order RK')
plt.plot(g[0], g1, '^r', label='S, 2nd order RK')
plt.plot(g[0], g2, '^b', label='I, 2nd order RK')
plt.plot(e[0], e1, 'or', label='S, forward Euler Method ')
plt.plot(e[0], e2, 'ob', label='I, forward Euler Method')
sis = SIS_sol(x01, x02, c_1, c_2, final_time, total_number_of_steps)
plt.plot(sis[2], sis[0], '--r', label='S, exact solution')
plt.plot(sis[2], sis[1], '--b', label='I, exact solution')
plt.ylabel('Population')
plt.xlabel('time')
plt.legend(loc='best')
# Calculating the relative error
g1 = np.array(g1)
g2 = np.array(g2)
j1 = np.array(j1)
j2 = np.array(j2)
e1 = np.array(e1)
e2 = np.array(e2)
si0 = np.array(sis[0][:])
si1 = np.array(sis[1][:])
erk2i = (g1 - si0) / si0
erk2s = (g2 - si1) / si1
erk4i = (j1 - si0) / si0
erk4s = (j2 - si1) / si1
ee2i = (e1 - si0) / si0
ee2s = (e2 - si1) / si1
Errk2i.append(erk2s)
Errk2s.append(erk2i)
Errk4i.append(erk4s)
Errk4s.append(erk4i)
Erre2i.append(ee2s)
Erre2s.append(ee2i)
plt.figure()
plt.title('Relative error of the SIS model with $\\beta$ = ' + str(c_1) + ' and $\gamma$ = ' + str(c_2))
plt.plot(sis[2], erk2i, '-*', label='S, 2nd order RK')
plt.plot(sis[2], erk2s, '-*', label='I, 2nd order RK')
plt.plot(sis[2], erk4i, '-*', label='S, 4th order RK')
plt.plot(sis[2], erk4s, '-*', label='I, 4th order RK')
plt.plot(sis[2], ee2i, '-*', label='S, Forward Euler Method')
plt.plot(sis[2], ee2s, '-*', label='I, Forward Euler Method')
plt.ylabel('Relative error')
plt.legend(loc='best')
if i > 0:
E_rk4s = calc_error(js_p, j1, 5)
E_rk4i = calc_error(ji_p, j2, 5)
E_rk2s = calc_error(gs_p, g1, 3)
E_rk2i = calc_error(gi_p, g2, 3)
E_euls = calc_error(es_p, e1, 2)
E_euli = calc_error(ei_p, e2, 2)
Ee_rk4s.append(E_rk4s)
Ee_rk4i.append(E_rk4i)
Ee_rk2s.append(E_rk2s)
Ee_rk2i.append(E_rk2i)
Ee_euls.append(E_euls)
Ee_euli.append(E_euli)
plt.figure()
plt.title('Error with $h$ = {}'.format(final_time/total_number_of_steps))
plt.plot(E_euls, '-+', label='S, Euler Method')
plt.plot(E_euli, '-+', label='I, Euler Method')
plt.plot(E_rk2s, '-+', label='S, RK 2nd order')
plt.plot(E_rk2i, '-+', label='I, RK 2nd order')
plt.plot(E_rk4s, '-+', label='S, RK 4th order')
plt.plot(E_rk4i, '-+', label='I, RK 4th order')
plt.ylabel('Error')
plt.xlabel('Step')
plt.legend(loc='best')
js_p = copy.copy(j1)
gs_p = copy.copy(g1)
es_p = copy.copy(e1)
ji_p = copy.copy(j2)
gi_p = copy.copy(g2)
ei_p = copy.copy(e2)
total_number_of_steps = int(total_number_of_steps * 2)
plt.figure()
plt.title('Calculation of the error for difference size steps using a 4th RK Method')
for j, arr in enumerate(Ee_rk4s):
plt.plot(np.linspace(initial_time, final_time + 1, len(arr)), arr, '-.', label='S, $h$ = {}'.format(final_time/(2*(len(arr)-1))))
plt.plot(np.linspace(initial_time, final_time + 1, len(arr)), Ee_rk4i[j], '-.', label='I, $h$ = {}'.format(final_time/(2*(len(arr)-1))))
plt.ylabel('Error')
plt.xlabel('Step')
plt.legend(loc='best')
plt.figure()
plt.title('Calculation of the error for difference size steps using a 2nd RK Method')
for j, arr in enumerate(Ee_rk2s):
plt.plot(np.linspace(initial_time, final_time + 1, len(arr)), arr, '-.', label='S, $h$ = {}'.format(final_time/(2*(len(arr)-1))))
plt.plot(np.linspace(initial_time, final_time + 1, len(arr)), Ee_rk2i[j], '-.', label='I, $h$ = {}'.format(final_time/(2*(len(arr)-1))))
plt.ylabel('Error')
plt.xlabel('Step')
plt.legend(loc='best')
plt.figure()
plt.title('Calculation of the error for difference size steps using Euler Method')
for j, arr in enumerate(Ee_euls):
plt.plot(np.linspace(initial_time, final_time + 1, len(arr)), arr, '-.', label='S, $h$ = {}'.format(final_time/(2*(len(arr)-1))))
plt.plot(np.linspace(initial_time, final_time + 1, len(arr)), Ee_euli[j], '-.', label='I, $h$ = {}'.format(final_time/(2*(len(arr)-1))))
plt.ylabel('Error')
plt.xlabel('Step')
plt.legend(loc='best')
# to test the variable 2
"""c_1 = 0.2
c_2s = [0.01, 0.05, 0.1, 0.5, 0.9, 1.1, 1.5]
for c_2 in c_2s:
consts = [c_1, c_2] # change this according to the number of constants of your model
consts[0] = c_1
j = rk4_N(SIS_model, x0, consts, initial_time, final_time, total_number_of_steps)
g = rk2_N(SIS_model, x0, consts, initial_time, final_time, total_number_of_steps)
e = euler_method(SIS_model, x0, consts, initial_time, final_time, total_number_of_steps)
g1, g2, g3 = [], [], []
j1, j2, j3 = [], [], []
e1, e2, = [], []
for je in j[1]:
j1.append(je[0])
j2.append(je[1])
for ge in g[1]:
g1.append(ge[0])
g2.append(ge[1])
for ee in e[1]:
e1.append(ee[0])
e2.append(ee[1])
plt.figure()
plt.title('Simulation of the SIS model with $\\beta$ = ' + str(c_1) + ' and $\gamma$ = ' + str(c_2))
plt.plot(j[0], j1, 'xr', label='S, 4th order RK')
plt.plot(j[0], j2, 'xb', label='I, 4th order RK')
plt.plot(g[0], g1, '^r', label='S, 2nd order RK')
plt.plot(g[0], g2, '^b', label='I, 2nd order RK')
plt.plot(e[0], e1, 'or', label='S, forward Euler Method ')
plt.plot(e[0], e2, 'ob', label='I, forward Euler Method')
sis = SIS_sol(x01, x02, c_1, c_2, final_time)
plt.plot(sis[0], '--r', label='S, exact solution')
plt.plot(sis[1], '--b', label='I, exact solution')
plt.ylabel('Population')
plt.xlabel('time')
plt.legend(loc='best')"""
def calc_error(a_h, a_2h, m):
a_2h = np.array([element for i, element in enumerate(a_2h) if i % 2 == 0])
return (a_h - a_2h)/(2**m-1)
def test_SIR2():
N = 22500
x01 = 22400/N
x02 = 100/N
x03 = 0/N
x0 = [x01, x02, x03] # change this according to the entries of your model
c_1 = 0.6
c_2 = 0.2
consts = [c_1, c_2] # change this according to the number of constants of your model
initial_time = 0
final_time = 30
total_number_of_steps = 50
y = rk4_N(SIR_model2, x0, consts, initial_time, final_time, total_number_of_steps)
y1, y2, y3 = [], [], []
for ye in y[1]:
y1.append(ye[0])
y2.append(ye[1])
y3.append(ye[2])
plt.figure()
plt.title('Solution Using a RK4 model')
plt.plot(y[0], y1, '-o', label='S')
plt.plot(y[0], y2, '-o', label='I')
plt.plot(y[0], y3, '-o', label='R')
plt.xlabel('time')
plt.ylabel('population')
plt.legend(loc='best')
def test_automata():
A = AutomataModel(150, 22500, 100, 0)
total = 22500
# A = AutomataModel(10, 98, 2, 0)
# total = 100
tmax = 100
P_s = []
P_i = []
P_r = []
plt.figure()
for t in range(tmax):
p_s, p_i, p_r = measure(A.model)
P_s.append(p_s / total)
P_i.append(p_i / total)
P_r.append(p_r / total)
# plt.subplot(1, 2, 1)
plt.imshow(A.model)
# plt.subplot(1, 2, 2)
# plt.plot(P_s, '--b')
# plt.plot(P_i, '--r')
# plt.plot(P_r, '--g')
plt.pause(0.00001)
X = []
Y = []
for i in range(150 * 150):
x, y = simulate_SIR2(A.model, 0.72, 0.25, 0.05)
X.append(x)
Y.append(y)
# plt.figure()
# plt.subplot(1, 2, 1)
# plt.hist(X)
# plt.subplot(1, 2, 2)
# plt.hist(Y)
plt.figure()
plt.plot(P_s, 'b-^', label='S')
plt.plot(P_i, 'r-^', label='I')
plt.plot(P_r, 'g-^', label='R')
plt.legend(loc='best')
plt.xlabel('Time steps')
plt.ylabel('Population')
def test_comp():
A = AutomataModel(150, 22500, 100, 0)
total = 22500
# A = AutomataModel(10, 98, 2, 0)
# total = 100
tmax = 30
P_s = []
P_i = []
P_r = []
plt.figure()
for t in range(tmax):
p_s, p_i, p_r = measure(A.model)
P_s.append(p_s / total)
P_i.append(p_i / total)
P_r.append(p_r / total)
# plt.subplot(1, 2, 1)
plt.imshow(A.model)
# plt.subplot(1, 2, 2)
# plt.plot(P_s, '--b')
# plt.plot(P_i, '--r')
# plt.plot(P_r, '--g')
plt.pause(0.00001)
X = []
Y = []
for i in range(150 * 150):
x, y = simulate_SIR(A.model, 0.62, 0.15)
X.append(x)
Y.append(y)
# plt.figure()
# plt.subplot(1, 2, 1)
# plt.hist(X)
# plt.subplot(1, 2, 2)
# plt.hist(Y)
N = 22500
x01 = 22400/N
x02 = 100/N
x03 = 0/N
x0 = [x01, x02, x03] # change this according to the entries of your model
c_1 = 0.6
c_2 = 0.2
consts = [c_1, c_2] # change this according to the number of constants of your model
initial_time = 0
final_time = 30
total_number_of_steps = 50
y = rk4_N(SIR_model2, x0, consts, initial_time, final_time, total_number_of_steps)
y1, y2, y3 = [], [], []
for ye in y[1]:
y1.append(ye[0])
y2.append(ye[1])
y3.append(ye[2])
plt.figure()
plt.title('Solution Using a RK4 model')
plt.plot(y[0], y1, 'b-o', label='S, ODE system')
plt.plot(y[0], y2, 'r-o', label='I, ODE system')
plt.plot(y[0], y3, 'g-o', label='R, ODE system')
plt.plot(P_s, 'b-^', label='S, C.A.')
plt.plot(P_i, 'r-^', label='I, C.A.')
plt.plot(P_r, 'g-^', label='R, C.A.')
plt.xlabel('time')
plt.ylabel('population')
plt.legend(loc='best')
def main():
test_automata()
#test_SIR2()
#test_comp()
if __name__ == '__main__':
main()
plt.show()