Skip to content

Commit 4cb5b11

Browse files
authored
Merge pull request #218 from bluescarni/pr/notebook_ruff
Run ruff on all notebooks
2 parents 98b1335 + a143394 commit 4cb5b11

File tree

63 files changed

+4002
-2561
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+4002
-2561
lines changed

doc/notebooks/Batch mode overview.ipynb

+122-99
Large diffs are not rendered by default.

doc/notebooks/Box control for Formation Flying Satellites.ipynb

+178-92
Large diffs are not rendered by default.

doc/notebooks/Comparing coordinate systems.ipynb

+153-100
Large diffs are not rendered by default.

doc/notebooks/Customising the adaptive integrator.ipynb

+13-12
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@
6767
"\n",
6868
"# Create the integrator object.\n",
6969
"ta = hy.taylor_adaptive(\n",
70-
" # Definition of the ODE system:\n",
71-
" # x' = v\n",
72-
" # v' = -9.8 * sin(x)\n",
73-
" sys = [(x, v),\n",
74-
" (v, -9.8 * hy.sin(x))],\n",
75-
" # Initial conditions for x and v.\n",
76-
" state = [0.05, 0.025],\n",
77-
" # Set the tolerance to 1e-9\n",
78-
" tol = 1e-9)\n",
70+
" # Definition of the ODE system:\n",
71+
" # x' = v\n",
72+
" # v' = -9.8 * sin(x)\n",
73+
" sys=[(x, v), (v, -9.8 * hy.sin(x))],\n",
74+
" # Initial conditions for x and v.\n",
75+
" state=[0.05, 0.025],\n",
76+
" # Set the tolerance to 1e-9\n",
77+
" tol=1e-9,\n",
78+
")\n",
7979
"\n",
8080
"ta"
8181
]
@@ -117,8 +117,8 @@
117117
],
118118
"source": [
119119
"# Integrate forth to t = 10 and then back to t = 0.\n",
120-
"ta.propagate_until(t = 10.)\n",
121-
"ta.propagate_until(t = 0.)\n",
120+
"ta.propagate_until(t=10.0)\n",
121+
"ta.propagate_until(t=0.0)\n",
122122
"\n",
123123
"ta"
124124
]
@@ -163,7 +163,7 @@
163163
"outputs": [],
164164
"source": [
165165
"# Create an nbody system with 6 particles.\n",
166-
"sys = hy.model.nbody(n = 6)"
166+
"sys = hy.model.nbody(n=6)"
167167
]
168168
},
169169
{
@@ -184,6 +184,7 @@
184184
"source": [
185185
"# Create an initial state vector (6 values per body).\n",
186186
"import numpy as np\n",
187+
"\n",
187188
"sv = np.zeros(36)"
188189
]
189190
},

doc/notebooks/Dense output.ipynb

+7-9
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@
7272
"\n",
7373
"x, v = hy.make_vars(\"x\", \"v\")\n",
7474
"\n",
75-
"ta = hy.taylor_adaptive([(x, v),\n",
76-
" (v, -x)],\n",
77-
" state = [0., 1.])"
75+
"ta = hy.taylor_adaptive([(x, v), (v, -x)], state=[0.0, 1.0])"
7876
]
7977
},
8078
{
@@ -106,7 +104,7 @@
106104
}
107105
],
108106
"source": [
109-
"ta.step(write_tc = True)"
107+
"ta.step(write_tc=True)"
110108
]
111109
},
112110
{
@@ -227,7 +225,7 @@
227225
}
228226
],
229227
"source": [
230-
"ta.update_d_output(t = 0.5)"
228+
"ta.update_d_output(t=0.5)"
231229
]
232230
},
233231
{
@@ -455,7 +453,7 @@
455453
"ta.time = 0\n",
456454
"\n",
457455
"# Propagate and return the continuous output.\n",
458-
"c_out = ta.propagate_until(10., c_output=True)[4]"
456+
"c_out = ta.propagate_until(10.0, c_output=True)[4]"
459457
]
460458
},
461459
{
@@ -533,10 +531,10 @@
533531
],
534532
"source": [
535533
"# Print the state vector at t = 5.\n",
536-
"print(\"State vector at t=5: {}\\n\".format(c_out(5.)))\n",
534+
"print(\"State vector at t=5: {}\\n\".format(c_out(5.0)))\n",
537535
"\n",
538536
"# Print the state vectors at a few different times.\n",
539-
"print(\"State vectors at t=[1,2,3,4,5]:\\n{}\\n\".format(c_out([1,2,3,4,5])))"
537+
"print(\"State vectors at t=[1,2,3,4,5]:\\n{}\\n\".format(c_out([1, 2, 3, 4, 5])))"
540538
]
541539
},
542540
{
@@ -574,7 +572,7 @@
574572
"\n",
575573
"# Compare it to the exact solution.\n",
576574
"fig = plt.figure(figsize=(12, 6))\n",
577-
"plt.semilogy(t_grid, abs(x_c_out[:,0] - np.sin(t_grid)))\n",
575+
"plt.semilogy(t_grid, abs(x_c_out[:, 0] - np.sin(t_grid)))\n",
578576
"plt.xlabel(\"Time\")\n",
579577
"plt.ylabel(\"Absolute error\");"
580578
]

doc/notebooks/Event detection.ipynb

+52-33
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,17 @@
118118
" # event triggered and print the value of x.\n",
119119
" ta.update_d_output(time)\n",
120120
" print(\"Value of x when v is zero: {}\".format(ta.d_output[0]))\n",
121-
" \n",
121+
"\n",
122122
" # Add the event time to zero_vel_times.\n",
123123
" zero_vel_times.append(time)\n",
124124
"\n",
125+
"\n",
125126
"ev = hy.nt_event(\n",
126-
" # The left-hand side of the event equation\n",
127-
" v,\n",
128-
" # The callback.\n",
129-
" callback = cb)"
127+
" # The left-hand side of the event equation\n",
128+
" v,\n",
129+
" # The callback.\n",
130+
" callback=cb,\n",
131+
")"
130132
]
131133
},
132134
{
@@ -192,9 +194,10 @@
192194
" ((x, v), (v, -9.8 * hy.sin(x))),\n",
193195
" # Initial conditions\n",
194196
" # for x and v.\n",
195-
" [-0.05, 0.],\n",
197+
" [-0.05, 0.0],\n",
196198
" # Non-terminal events.\n",
197-
" nt_events = [ev])"
199+
" nt_events=[ev],\n",
200+
")"
198201
]
199202
},
200203
{
@@ -247,7 +250,7 @@
247250
"t_grid = np.linspace(0, 5, 1000)\n",
248251
"\n",
249252
"# Propagate over the time grid.\n",
250-
"x_hist = ta.propagate_grid(t_grid)[5][:,0]\n",
253+
"x_hist = ta.propagate_grid(t_grid)[5][:, 0]\n",
251254
"\n",
252255
"# Display the time evolution for the x variable.\n",
253256
"fig = plt.figure(figsize=(12, 6))\n",
@@ -258,7 +261,7 @@
258261
"# Put vertical lines in correspondence of\n",
259262
"# the detected events.\n",
260263
"for ev_time in zero_vel_times:\n",
261-
" plt.axvline(x = ev_time, linestyle='--', color='gray')"
264+
" plt.axvline(x=ev_time, linestyle=\"--\", color=\"gray\")"
262265
]
263266
},
264267
{
@@ -329,13 +332,15 @@
329332
"# Redefine ev to detect only events\n",
330333
"# in the positive direction.\n",
331334
"ev = hy.nt_event(\n",
332-
" v, callback = lambda ta, time, d_sgn: zero_vel_times.append(time),\n",
333-
" # Specify the direction.\n",
334-
" direction = hy.event_direction.positive)\n",
335+
" v,\n",
336+
" callback=lambda ta, time, d_sgn: zero_vel_times.append(time),\n",
337+
" # Specify the direction.\n",
338+
" direction=hy.event_direction.positive,\n",
339+
")\n",
335340
"\n",
336341
"# Reset zero_vel_times and the integrator.\n",
337342
"zero_vel_times.clear()\n",
338-
"ta = hy.taylor_adaptive(((x, v), (v, -9.8 * hy.sin(x))), [-0.05, 0.], nt_events = [ev])"
343+
"ta = hy.taylor_adaptive(((x, v), (v, -9.8 * hy.sin(x))), [-0.05, 0.0], nt_events=[ev])"
339344
]
340345
},
341346
{
@@ -367,7 +372,7 @@
367372
],
368373
"source": [
369374
"# Propagate over the time grid.\n",
370-
"x_hist = ta.propagate_grid(t_grid)[5][:,0]\n",
375+
"x_hist = ta.propagate_grid(t_grid)[5][:, 0]\n",
371376
"\n",
372377
"# Display the time evolution for the x variable.\n",
373378
"fig = plt.figure(figsize=(12, 6))\n",
@@ -378,7 +383,7 @@
378383
"# Put vertical lines in correspondence of\n",
379384
"# the detected events.\n",
380385
"for ev_time in zero_vel_times:\n",
381-
" plt.axvline(x = ev_time, linestyle='--', color='gray')"
386+
" plt.axvline(x=ev_time, linestyle=\"--\", color=\"gray\")"
382387
]
383388
},
384389
{
@@ -447,8 +452,13 @@
447452
"outputs": [],
448453
"source": [
449454
"# Define two close non-terminal events.\n",
450-
"ev0 = hy.nt_event(v, lambda ta, time, d_sgn: print(\"Event 0 triggering at t={}\".format(time)))\n",
451-
"ev1 = hy.nt_event(v * v - 1e-12, lambda ta, time, d_sgn: print(\"Event 1 triggering at t={}\".format(time)))"
455+
"ev0 = hy.nt_event(\n",
456+
" v, lambda ta, time, d_sgn: print(\"Event 0 triggering at t={}\".format(time))\n",
457+
")\n",
458+
"ev1 = hy.nt_event(\n",
459+
" v * v - 1e-12,\n",
460+
" lambda ta, time, d_sgn: print(\"Event 1 triggering at t={}\".format(time)),\n",
461+
")"
452462
]
453463
},
454464
{
@@ -506,10 +516,12 @@
506516
],
507517
"source": [
508518
"# Reset the integrator.\n",
509-
"ta = hy.taylor_adaptive(((x, v), (v, -9.8 * hy.sin(x))), [-0.05, 0.], nt_events = [ev0, ev1])\n",
519+
"ta = hy.taylor_adaptive(\n",
520+
" ((x, v), (v, -9.8 * hy.sin(x))), [-0.05, 0.0], nt_events=[ev0, ev1]\n",
521+
")\n",
510522
"\n",
511523
"# Propagate for a few time units.\n",
512-
"ta.propagate_until(5.)"
524+
"ta.propagate_until(5.0)"
513525
]
514526
},
515527
{
@@ -580,6 +592,7 @@
580592
"# Clear up zero_vel_times.\n",
581593
"zero_vel_times.clear()\n",
582594
"\n",
595+
"\n",
583596
"# Callback for the terminal event.\n",
584597
"def t_cb(ta, d_sgn):\n",
585598
" # NOTE: the value of the drag coefficient\n",
@@ -596,13 +609,15 @@
596609
" # Do not stop the integration.\n",
597610
" return True\n",
598611
"\n",
612+
"\n",
599613
"# Define a terminal event that turns air drag on/off\n",
600614
"# whenever the velocity goes to zero.\n",
601615
"t_ev = hy.t_event(\n",
602-
" # The event equation.\n",
603-
" v,\n",
604-
" # The callback.\n",
605-
" callback = t_cb)"
616+
" # The event equation.\n",
617+
" v,\n",
618+
" # The callback.\n",
619+
" callback=t_cb,\n",
620+
")"
606621
]
607622
},
608623
{
@@ -657,14 +672,18 @@
657672
"outputs": [],
658673
"source": [
659674
"# Construct the damped pendulum integrator.\n",
660-
"ta = hy.taylor_adaptive([(x, v),\n",
661-
" # NOTE: alpha is represented as\n",
662-
" # the first (and only) runtime\n",
663-
" # parameter: par[0].\n",
664-
" (v, -9.8 * hy.sin(x) - hy.par[0] * v)],\n",
665-
" [0.05, 0.025],\n",
666-
" # The list of terminal events.\n",
667-
" t_events = [t_ev])"
675+
"ta = hy.taylor_adaptive(\n",
676+
" [\n",
677+
" (x, v),\n",
678+
" # NOTE: alpha is represented as\n",
679+
" # the first (and only) runtime\n",
680+
" # parameter: par[0].\n",
681+
" (v, -9.8 * hy.sin(x) - hy.par[0] * v),\n",
682+
" ],\n",
683+
" [0.05, 0.025],\n",
684+
" # The list of terminal events.\n",
685+
" t_events=[t_ev],\n",
686+
")"
668687
]
669688
},
670689
{
@@ -746,7 +765,7 @@
746765
"t_grid = np.linspace(ta.time, 10, 1000)\n",
747766
"\n",
748767
"# Propagate over the time grid.\n",
749-
"x_hist = ta.propagate_grid(t_grid)[5][:,0]\n",
768+
"x_hist = ta.propagate_grid(t_grid)[5][:, 0]\n",
750769
"\n",
751770
"# Display the time evolution for the x variable.\n",
752771
"fig = plt.figure(figsize=(12, 6))\n",
@@ -757,7 +776,7 @@
757776
"# Put vertical lines in correspondence of\n",
758777
"# the detected events.\n",
759778
"for ev_time in zero_vel_times:\n",
760-
" plt.axvline(x = ev_time, linestyle='--', color='gray')\n",
779+
" plt.axvline(x=ev_time, linestyle=\"--\", color=\"gray\")\n",
761780
"\n",
762781
"print(\"Final time: {}\".format(ta.time))"
763782
]

0 commit comments

Comments
 (0)