-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorbit_shooting_tests.py
593 lines (493 loc) · 19.2 KB
/
orbit_shooting_tests.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
import numpy as np
from scipy.optimize import fsolve
from newtonrhapson import newton
from periodfinderforcheck import manual_period_finder
from shooting import orbit_shooting
from solve_ode import solve_ode
"""
tests for the orbit_shooting() function
"""
def input_tests():
"""
Tests for whether orbit_shooting() can handle good/bad input parameters
"""
"""
good u0 values
"""
u0 = np.array([0.5, 0.5, 15])
"""
good function
"""
def good_ode(t, u0, args):
x = u0[0]
y = u0[1]
a = args[0]
d = args[1]
b = args[2]
dxdt = x * (1 - x) - (a * x * y) / (d + x)
dydt = b * y * (1 - (y / x))
return np.array([dxdt, dydt])
"""
correct phase condition for good_f()
"""
def good_ode_pc(u0, args):
return good_ode(0, u0, args)[0]
"""
ode returning an output of wrong type
"""
def ode_wrong_output_type(t, u0, args):
return "wrong type"
"""
ode returning an output of wrong shape
"""
def ode_wrong_output_shape(t, u0, args):
return [u0, u0]
"""
pc returning an output of the wrong type
"""
def pc_wrong_output_type(u0, args):
return "wrong type"
"""
pc returning an output of the wrong shape (ie. not scalar)
"""
def pc_wrong_output_shape(u0, args):
return [1, 2, 3]
all_tests_passed = True
failed_tests = []
"""
Tests Start
"""
"""
good ode, pc, and u0 test
Testing when a good ODE and u0 are provided - no errors should be raised
"""
try:
orbit_shooting(good_ode, u0, good_ode_pc, fsolve, [1, 0.1, 0.16])
print("good ode, pc, and u0: test passed")
except (TypeError, ValueError):
all_tests_passed = False
failed_tests.append("good ode, pc, and u0")
print("good ode, pc, and u0: test failed")
"""
ode not a function test
Testing when ode param is not a function - a TypeError should be raised
"""
try:
orbit_shooting("not a function", u0, good_ode_pc, fsolve, [1, 0.1, 0.16])
all_tests_passed = False
failed_tests.append("ode not a function")
print("ode not a function test: failed")
except TypeError:
print("ode not a function test: passed")
"""
ode wrong output type test
Testing when ode param returns an output of the wrong type - a TypeError should be raised
"""
try:
orbit_shooting(ode_wrong_output_type, u0, good_ode_pc, fsolve, [1, 0.1, 0.16])
all_tests_passed = False
failed_tests.append("ode wrong output type")
print("ode wrong output type: test failed")
except TypeError:
print("ode wrong output type: test passed")
"""
ode wrong output shape test
Testing when ode param returns an output of the wrong shape - a ValueError should be raised
"""
try:
orbit_shooting(ode_wrong_output_shape, u0, good_ode_pc, fsolve, [1, 0.1, 0.16])
all_tests_passed = False
failed_tests.append("ode wrong output shape")
print("ode wrong output shape: test failed")
except ValueError:
print("ode wrong output shape: test passed")
"""
pc not a function test
Testing when pc param is not a function - a TypeError should be raised
"""
try:
orbit_shooting(good_ode, u0, "not a function", fsolve, [1, 0.1, 0.16])
all_tests_passed = False
failed_tests.append("pc not a function")
print("pc not a function test: failed")
except TypeError:
print("pc not a function test: passed")
"""
pc wrong output type test
Testing when pc param returns an output of the wrong type - a TypeError should be raised
"""
try:
orbit_shooting(good_ode, u0, pc_wrong_output_type, fsolve, [1, 0.1, 0.16])
all_tests_passed = False
failed_tests.append("pc wrong output type")
print("pc wrong output type: test failed")
except TypeError:
print("pc wrong output type: test passed")
"""
pc wrong output shape test
Testing when pc param returns an output of the wrong shape - a TypeError should be raised
"""
try:
orbit_shooting(good_ode, u0, pc_wrong_output_shape, fsolve, [1, 0.1, 0.16])
all_tests_passed = False
failed_tests.append("pc wrong output shape")
print("pc wrong output shape: test failed")
except TypeError:
print("pc wrong output shape: test passed")
"""
incorrect u0 type test
Testing when the u0 param is an incorrect type - a TypeError should be raised
"""
try:
orbit_shooting(good_ode, "wrong u0 type", good_ode_pc, fsolve, [1, 0.1, 0.16])
all_tests_passed = False
failed_tests.append("incorrect u0 type")
print("incorrect u0 type: test failed")
except TypeError:
print("incorrect u0 type: test passed")
"""
Results
"""
if all_tests_passed:
print("___________")
print("All input tests passed :)")
print("___________")
else:
print("___________")
print("Some input tests failed :(")
print("___________")
print("Tests Failed:")
[print(fail) for fail in failed_tests]
def value_tests():
"""
Tests for whether orbit_shooting() produces correct output values
"""
all_tests_passed = True
failed_tests = []
"""
Tests Start
"""
"""
predator-prey good initial values test
Testing using the predator-prey equations with good initial values provided
Explicit solution is a numeric estimate, not an analytical solution so comparison between explicit and shooting
solutions are made at 3 significant figures.
"""
"""
Function for predator-prey
"""
def predator_prey(t, u0, args):
x = u0[0]
y = u0[1]
a = args[0]
d = args[1]
b = args[2]
dxdt = x * (1 - x) - (a * x * y) / (d + x)
dydt = b * y * (1 - (y / x))
return np.array([dxdt, dydt])
"""
Phase condition for predator_prey
"""
def pc_predator_prey(u0, args):
p = predator_prey(1, u0, args)[0]
return p
"""
Initial values close to the solution for predator-prey
"""
good_predator_prey_u0 = np.array([0.07, 0.16, 23])
"""
Getting true value of periodic orbit for predator-prey
"""
t = np.linspace(0, 1000, 10000)
predator_prey_solution = solve_ode(predator_prey, good_predator_prey_u0[:-1], t, "rk4", 0.001, True, [1, 0.1, 0.16])
predator_prey_x = predator_prey_solution[0]
predator_prey_y = predator_prey_solution[1]
true_orbit = manual_period_finder(t, predator_prey_x, predator_prey_y)
# rounds to 3 significant figures
true_orbit = [np.float_("%.3g" % ele) for ele in true_orbit]
"""
Using fsolve solver
"""
shooting_orbit = orbit_shooting(predator_prey, good_predator_prey_u0, pc_predator_prey, fsolve, [1, 0.1, 0.16])
shooting_orbit = [np.float_("%.3g" % ele) for ele in shooting_orbit]
# checking if root finder did not converge
if len(shooting_orbit) == 0:
all_tests_passed = False
failed_tests.append("predator-prey good initial values fsolve")
print("predator-prey good initial values fsolve: test failed")
# checking if solution found by shooting is close to explicit solution
else:
if np.allclose(true_orbit, shooting_orbit):
print("predator-prey good initial values fsolve: test passed")
else:
all_tests_passed = False
failed_tests.append("predator-prey good initial values fsolve")
print("predator-prey good initial values fsolve: test failed")
"""
Using newton solver
"""
shooting_orbit = orbit_shooting(predator_prey, good_predator_prey_u0, pc_predator_prey, newton, [1, 0.1, 0.16])
shooting_orbit = [np.float_("%.3g" % ele) for ele in shooting_orbit]
# checking if root finder did not converge
if len(shooting_orbit) == 0:
all_tests_passed = False
failed_tests.append("predator-prey good initial values newton")
print("predator-prey good initial values newton: test failed")
# checking if solution found by shooting is close to explicit solution
else:
if np.allclose(true_orbit, shooting_orbit):
print("predator-prey good initial values newton: test passed")
else:
all_tests_passed = False
failed_tests.append("predator-prey good initial values newton")
print("predator-prey good initial values newton: test failed")
"""
predator-prey divergent initial values test
Testing using the predator-prey equations with initial values which cause the shooting to not converge.
In this case, an empty array, [], should be returned.
"""
divergent_predator_prey_u0 = np.array([55, 77, -100])
"""
Using fsolve solver
"""
orbit = orbit_shooting(predator_prey, divergent_predator_prey_u0, pc_predator_prey, fsolve, [1, 0.1, 0.16])
if len(orbit) == 0:
print("predator-prey divergent initial values fsolve: test passed")
else:
all_tests_passed = False
failed_tests.append("predator-prey divergent initial values fsolve")
print("predator-prey divergent initial values fsolve: test failed")
"""
Using newton solver
"""
orbit = orbit_shooting(predator_prey, divergent_predator_prey_u0, pc_predator_prey, newton, [1, 0.1, 0.16])
if len(orbit) == 0:
print("predator-prey divergent initial values newton: test passed")
else:
all_tests_passed = False
failed_tests.append("predator-prey divergent initial values newton")
print("predator-prey divergent initial values newton: test failed")
"""
hopf normal good initial values test
Testing using the Hopf normal form equations with good initial values provided
"""
"""
Function for Hopf normal form
"""
def hopf_normal(t, u, args):
beta = args[0]
sigma = args[1]
u1 = u[0]
u2 = u[1]
du1dt = beta * u1 - u2 + sigma * u1 * (u1 ** 2 + u2 ** 2)
du2dt = u1 + beta * u2 + sigma * u2 * (u1 ** 2 + u2 ** 2)
return np.array([du1dt, du2dt])
"""
Phase condition function for Hopf normal form
"""
def pc_hopf_normal(u0, args):
p = hopf_normal(1, u0, args)[0]
return p
"""
Explicit true solution for Hopf normal form
"""
def hopf_normal_explicit(t, phase, args):
beta = args[0]
u1 = np.sqrt(beta) * np.cos(t + phase)
u2 = np.sqrt(beta) * np.sin(t + phase)
return np.array([u1, u2])
"""
Initial values close to the solution for Hopf normal form
"""
good_u0_hopf_normal = np.array([1.4, 0, 6.3])
"""
Using fsolve solver
"""
orbit = orbit_shooting(hopf_normal, good_u0_hopf_normal, pc_hopf_normal, fsolve, [1, -1])
# checking if root finder did not converge
if len(orbit) == 0:
all_tests_passed = False
failed_tests.append("hopf normal good initial values fsolve")
print("hopf normal good initial values fsolve: test failed")
# checking if solution found by shooting is close to explicit solution
else:
shooting_u = orbit[:-1]
T = orbit[-1]
true_u = hopf_normal_explicit(0, T, [1, -1])
if np.allclose(true_u, shooting_u):
print("hopf normal good initial values fsolve: test passed")
else:
all_tests_passed = False
failed_tests.append("hopf normal good initial values fsolve")
print("hopf normal good initial values fsolve: test failed")
"""
Using newton solver
"""
orbit = orbit_shooting(hopf_normal, good_u0_hopf_normal, pc_hopf_normal, newton, [1, -1])
# checking if root finder did not converge
if len(orbit) == 0:
all_tests_passed = False
failed_tests.append("hopf normal good initial values newton")
print("hopf normal good initial values newton: test failed")
# checking if solution found by shooting is close to explicit solution
else:
shooting_u = orbit[:-1]
T = orbit[-1]
true_u = hopf_normal_explicit(0, T, [1, -1])
if np.allclose(true_u, shooting_u):
print("hopf normal good initial values newton: test passed")
else:
all_tests_passed = False
failed_tests.append("hopf normal good initial values newton")
print("hopf normal good initial values newton: test failed")
"""
hopf normal divergent initial values test
Testing using the Hopf normal form equations with initial values which cause the shooting to not converge.
In this case, an empty array, [], should be returned.
"""
divergent_u0_hopf_normal = np.array([-100, 234, -10])
"""
Using fsolve solver
"""
orbit = orbit_shooting(hopf_normal, divergent_u0_hopf_normal, pc_hopf_normal, fsolve, [1, -1])
if len(orbit) == 0:
print("hopf normal divergent initial values fsolve: test passed")
else:
all_tests_passed = False
failed_tests.append("hopf normal divergent values fsolve")
print("hopf normal divergent initial values fsolve: test failed")
"""
Using newton solver
"""
orbit = orbit_shooting(hopf_normal, divergent_u0_hopf_normal, pc_hopf_normal, newton, [1, -1])
if len(orbit) == 0:
print("hopf normal divergent initial values newton: test passed")
else:
all_tests_passed = False
failed_tests.append("hopf normal divergent values newton")
print("hopf normal divergent initial values newton: test failed")
"""
hopf normal varied dimensions good initial values test
Testing using the Hopf normal form equations with an extra dimension du3/dt = -u3 added and good initial values
provided
"""
"""
Function for Hopf normal form with added dimension
"""
def hopf_normal_vary_dim(t, u, args):
beta = args[0]
sigma = args[1]
u1 = u[0]
u2 = u[1]
u3 = u[2]
du1dt = beta * u1 - u2 + sigma * u1 * (u1 ** 2 + u2 ** 2)
du2dt = u1 + beta * u2 + sigma * u2 * (u1 ** 2 + u2 ** 2)
du3dt = -u3
return np.array([du1dt, du2dt, du3dt])
"""
Phase condition function for Hopf normal form with added dimension
"""
def pc_hopf_normal_vary_dim(u0, args):
p = hopf_normal_vary_dim(1, u0, args)[0]
return p
"""
Explicit true solution for Hopf normal form with added dimension
"""
def hopf_normal_vary_dim_explicit(t, phase, args):
beta = args[0]
u1 = np.sqrt(beta) * np.cos(t + phase)
u2 = np.sqrt(beta) * np.sin(t + phase)
u3 = np.exp(-(t + phase))
return np.array([u1, u2, u3])
"""
Initial values close to the solution for Hopf normal form with added dimension
"""
good_u0_hopf_normal_vary_dim = np.array([1.4, 0, 1, 6.3])
"""
Using fsolve solver
"""
orbit = orbit_shooting(hopf_normal_vary_dim, good_u0_hopf_normal_vary_dim, pc_hopf_normal_vary_dim, fsolve, [1, -1])
# checking if root finder did not converge
if len(orbit) == 0:
all_tests_passed = False
failed_tests.append("hopf normal added dimension good initial values fsolve")
print("hopf normal added dimension good initial values fsolve: test failed")
# checking if solution found by shooting is close to explicit solution
else:
shooting_u = orbit[:-1]
T = orbit[-1]
true_u = hopf_normal_vary_dim_explicit(10 * T, T, [1, -1])
if np.allclose(true_u, shooting_u):
print("hopf normal added dimension good initial values fsolve: test passed")
else:
all_tests_passed = False
failed_tests.append("hopf normal added dimension good initial values fsolve")
print("hopf normal added dimension good initial values fsolve: test failed")
"""
Using newton solver
"""
orbit = orbit_shooting(hopf_normal_vary_dim, good_u0_hopf_normal_vary_dim, pc_hopf_normal_vary_dim, newton, [1, -1])
# checking if root finder did not converge
if len(orbit) == 0:
all_tests_passed = False
failed_tests.append("hopf normal added dimension good initial values newton")
print("hopf normal added dimension good initial values newton: test failed")
# checking if solution found by shooting is close to explicit solution
else:
shooting_u = orbit[:-1]
T = orbit[-1]
true_u = hopf_normal_explicit(0, T, [1, -1])
if np.allclose(true_u, shooting_u):
print("hopf normal added dimension good initial values newton: test passed")
else:
all_tests_passed = False
failed_tests.append("hopf normal added dimension good initial values newton")
print("hopf normal added dimension good initial values newton: test failed")
"""
hopf normal added dimension divergent initial values test
Testing using the Hopf normal form equations with an added dimension du3/dt = -u3 with initial values which cause
the shooting to not converge. In this case, an empty array, [], should be returned.
"""
divergent_u0_hopf_normal_vary_dim = np.array([-100, 234, 10, -10])
"""
Using fsolve solver
"""
orbit = orbit_shooting(hopf_normal_vary_dim, divergent_u0_hopf_normal_vary_dim, pc_hopf_normal_vary_dim, fsolve,
[1, -1])
if len(orbit) == 0:
print("hopf normal added dimension divergent initial values fsolve: test passed")
else:
all_tests_passed = False
failed_tests.append("hopf normal added dimension divergent values fsolve")
print("hopf normal added dimension divergent initial values fsolve: test failed")
"""
Using newton solver
"""
orbit = orbit_shooting(hopf_normal_vary_dim, divergent_u0_hopf_normal_vary_dim, pc_hopf_normal_vary_dim, newton,
[1, -1])
if len(orbit) == 0:
print("hopf normal added dimension divergent initial values newton: test passed")
else:
all_tests_passed = False
failed_tests.append("hopf normal added dimension divergent values newton")
print("hopf normal added dimension divergent initial values newton: test failed")
"""
Results
"""
if all_tests_passed:
print("___________")
print("All value tests passed :)")
print("___________")
else:
print("___________")
print("Some value tests failed :(")
print("___________")
print("Tests Failed:")
[print(fail) for fail in failed_tests]
def main():
print("Input Tests:")
input_tests()
print("Value Tests:")
value_tests()
if __name__ == "__main__":
main()