|
118 | 118 | " # event triggered and print the value of x.\n",
|
119 | 119 | " ta.update_d_output(time)\n",
|
120 | 120 | " print(\"Value of x when v is zero: {}\".format(ta.d_output[0]))\n",
|
121 |
| - " \n", |
| 121 | + "\n", |
122 | 122 | " # Add the event time to zero_vel_times.\n",
|
123 | 123 | " zero_vel_times.append(time)\n",
|
124 | 124 | "\n",
|
| 125 | + "\n", |
125 | 126 | "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 | + ")" |
130 | 132 | ]
|
131 | 133 | },
|
132 | 134 | {
|
|
192 | 194 | " ((x, v), (v, -9.8 * hy.sin(x))),\n",
|
193 | 195 | " # Initial conditions\n",
|
194 | 196 | " # for x and v.\n",
|
195 |
| - " [-0.05, 0.],\n", |
| 197 | + " [-0.05, 0.0],\n", |
196 | 198 | " # Non-terminal events.\n",
|
197 |
| - " nt_events = [ev])" |
| 199 | + " nt_events=[ev],\n", |
| 200 | + ")" |
198 | 201 | ]
|
199 | 202 | },
|
200 | 203 | {
|
|
247 | 250 | "t_grid = np.linspace(0, 5, 1000)\n",
|
248 | 251 | "\n",
|
249 | 252 | "# 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", |
251 | 254 | "\n",
|
252 | 255 | "# Display the time evolution for the x variable.\n",
|
253 | 256 | "fig = plt.figure(figsize=(12, 6))\n",
|
|
258 | 261 | "# Put vertical lines in correspondence of\n",
|
259 | 262 | "# the detected events.\n",
|
260 | 263 | "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\")" |
262 | 265 | ]
|
263 | 266 | },
|
264 | 267 | {
|
|
329 | 332 | "# Redefine ev to detect only events\n",
|
330 | 333 | "# in the positive direction.\n",
|
331 | 334 | "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", |
335 | 340 | "\n",
|
336 | 341 | "# Reset zero_vel_times and the integrator.\n",
|
337 | 342 | "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])" |
339 | 344 | ]
|
340 | 345 | },
|
341 | 346 | {
|
|
367 | 372 | ],
|
368 | 373 | "source": [
|
369 | 374 | "# 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", |
371 | 376 | "\n",
|
372 | 377 | "# Display the time evolution for the x variable.\n",
|
373 | 378 | "fig = plt.figure(figsize=(12, 6))\n",
|
|
378 | 383 | "# Put vertical lines in correspondence of\n",
|
379 | 384 | "# the detected events.\n",
|
380 | 385 | "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\")" |
382 | 387 | ]
|
383 | 388 | },
|
384 | 389 | {
|
|
447 | 452 | "outputs": [],
|
448 | 453 | "source": [
|
449 | 454 | "# 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 | + ")" |
452 | 462 | ]
|
453 | 463 | },
|
454 | 464 | {
|
|
506 | 516 | ],
|
507 | 517 | "source": [
|
508 | 518 | "# 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", |
510 | 522 | "\n",
|
511 | 523 | "# Propagate for a few time units.\n",
|
512 |
| - "ta.propagate_until(5.)" |
| 524 | + "ta.propagate_until(5.0)" |
513 | 525 | ]
|
514 | 526 | },
|
515 | 527 | {
|
|
580 | 592 | "# Clear up zero_vel_times.\n",
|
581 | 593 | "zero_vel_times.clear()\n",
|
582 | 594 | "\n",
|
| 595 | + "\n", |
583 | 596 | "# Callback for the terminal event.\n",
|
584 | 597 | "def t_cb(ta, d_sgn):\n",
|
585 | 598 | " # NOTE: the value of the drag coefficient\n",
|
|
596 | 609 | " # Do not stop the integration.\n",
|
597 | 610 | " return True\n",
|
598 | 611 | "\n",
|
| 612 | + "\n", |
599 | 613 | "# Define a terminal event that turns air drag on/off\n",
|
600 | 614 | "# whenever the velocity goes to zero.\n",
|
601 | 615 | "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 | + ")" |
606 | 621 | ]
|
607 | 622 | },
|
608 | 623 | {
|
|
657 | 672 | "outputs": [],
|
658 | 673 | "source": [
|
659 | 674 | "# 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 | + ")" |
668 | 687 | ]
|
669 | 688 | },
|
670 | 689 | {
|
|
746 | 765 | "t_grid = np.linspace(ta.time, 10, 1000)\n",
|
747 | 766 | "\n",
|
748 | 767 | "# 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", |
750 | 769 | "\n",
|
751 | 770 | "# Display the time evolution for the x variable.\n",
|
752 | 771 | "fig = plt.figure(figsize=(12, 6))\n",
|
|
757 | 776 | "# Put vertical lines in correspondence of\n",
|
758 | 777 | "# the detected events.\n",
|
759 | 778 | "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", |
761 | 780 | "\n",
|
762 | 781 | "print(\"Final time: {}\".format(ta.time))"
|
763 | 782 | ]
|
|
0 commit comments