Skip to content

Commit 69255bd

Browse files
author
Daniel Precioso, PhD
committed
Enhance Vicsek animation documentation: add optional steps for order parameter and noise plots, and clarify implementation details for sliders and animation updates.
1 parent 9bd9ccd commit 69255bd

1 file changed

Lines changed: 63 additions & 14 deletions

File tree

modules/collective-motion/vicsek-animation.qmd

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ This avoids creating new arrays each frame.
180180

181181
## Initialize the Order-Parameter Plot
182182

183+
**Optional:** You can skip this step and just create an empty plot with `ax_order.plot([], [])` for now. We will fill in the details later.
184+
183185
The next plot will show the order parameter $r$ over time.
184186

185187
```{python}
@@ -237,6 +239,8 @@ def init_order_plot(ax_order: Axes, order_window: int):
237239

238240
## Initialize the Noise Diagnostic Plot
239241

242+
**Optional:** You can skip this step and just create an empty plot with `ax_noise.plot([], [])` for now. We will fill in the details later.
243+
240244
In this plot we will track how the order parameter $r$ changes as we explore different noise levels $\eta$ with the slider. Visually, we expect to see a curve that starts near $r=1$ at low noise, and drops towards $r=0$ as noise increases. See @fig-vicsek-anim-noise-plot for an example (using mock data).
241245

242246
```{python}
@@ -290,21 +294,21 @@ def init_noise_plot(ax_noise: Axes):
290294

291295
This is the core loop:
292296

293-
1. advance the Vicsek dynamics by one step (`vicsek_equations`)
294-
2. update `xy_tail`
295-
3. update the particle artists
296-
4. update order-parameter history `ls_order_param` and its plot
297-
5. update the noise diagnostic `dict_noise` and its plot
297+
1. advance the Vicsek dynamics by one step (`vicsek_equations`).
298+
2. update `xy_tail`.
299+
3. update the particle artists.
300+
4. update order-parameter history `ls_order_param` and its plot (only if you implemented the order plot in the previous step).
301+
5. update the noise diagnostic `dict_noise` and its plot (only if you implemented the noise plot in the previous step).
298302

299303
Use this checklist:
300304

301305
- [ ] call `vicsek_equations(...)`
302306
- [ ] update `xy_tail` with `np.roll(...)`
303307
- [ ] `plt_particles.set_data(...)` and `plt_current.set_data(...)`
304-
- [ ] append `vicsek_order_parameter(theta)` to the window list
305-
- [ ] set line data for the order parameter plot
306-
- [ ] compute a *recent average* (e.g. last third of the window)
307-
- [ ] update `dict_noise[noise_eta]` and redraw the noise curve
308+
- [ ] append `vicsek_order_parameter(theta)` to the window list (only if you implemented the order plot)
309+
- [ ] set line data for the order parameter plot (only if you implemented the order plot)
310+
- [ ] compute a *recent average* (only if you implemented the order plot)
311+
- [ ] update `dict_noise[noise_eta]` and redraw the noise curve (only if you implemented the noise plot)
308312

309313
You can follow the structure of the `update_animation` function below, which includes comments for each step.
310314

@@ -318,9 +322,12 @@ def update_animation(frame: int):
318322
# TODO: update the particle artists with set_data
319323

320324
# TODO: update the order parameter history and plot
325+
# (only if you implemented the order plot in the previous step)
321326

322327
# TODO: compute a recent average and update dict_noise
328+
# (only if you implemented the order plot in the previous step)
323329
# TODO: update the noise diagnostic plot with set_data
330+
# (only if you implemented the noise plot in the previous step)
324331

325332
return (plt_particles, plt_current, line_order_param, line_noise)
326333
```
@@ -396,6 +403,8 @@ ani = animation.FuncAnimation(fig, update_animation, interval=0, blit=True)
396403

397404
## Add Sliders
398405

406+
**Optional:** You can skip this step for now and just set default parameters at the top of `run_simulation()`.
407+
399408
We want sliders for:
400409

401410
- Number of boids $N$ (special: requires reinitializing particles).
@@ -446,9 +455,7 @@ def make_sliders(
446455
return slider_num_boids, slider_radius_interaction, slider_noise_eta, slider_v0, slider_box_size
447456
```
448457

449-
---
450-
451-
## Slider Callbacks
458+
### Slider Callbacks
452459

453460
We need **two** callbacks:
454461

@@ -489,13 +496,55 @@ This reduces glitches while you resize arrays or change plot limits.
489496

490497
---
491498

492-
## Put it all together (full script)
499+
## Full Script
493500

494501
At this point you have built every piece.
495502
Below is a sample of how your `run_simulation` function might look with all the pieces assembled.
496503

497504
::: {.callout-tip collapse="true"}
498-
## Full `run_simulation()`
505+
## Solution: Simplified `run_simulation()`
506+
507+
This is a simplified version that assumes you have not implemented the order plot or noise plot, neither the sliders, yet. It focuses on the particle animation.
508+
509+
```python
510+
def run_simulation(dt: float = 1.0) -> None:
511+
# Parameters
512+
num_boids = 100
513+
noise_eta = 0.5
514+
box_size = 10.0
515+
radius_interaction = 1.0
516+
v0 = 0.03
517+
518+
# Initialize state
519+
xy, theta = initialize_particles(num_boids, box_size)
520+
521+
# Order parameter history and noise diagnostic
522+
# will be initialized here
523+
524+
# Create figure and axes
525+
fig, ax_plane, ax_order, ax_sliders, ax_noise = make_figure()
526+
527+
# Initialize plots
528+
xy_tail, plt_particles, plt_current = init_particles_plot(ax_plane, xy, box_size, tail_len=20)
529+
# The order plot and noise plot will go here
530+
531+
# Sliders will be added here
532+
533+
# Define the animation update function
534+
def update_animation(frame: int):
535+
# See the previous step for the full implementation
536+
...
537+
538+
# Create the animation object
539+
ani = animation.FuncAnimation(fig, update_animation, interval=0, blit=True)
540+
541+
# The slider callbacks will come here
542+
543+
plt.show()
544+
```
545+
546+
::: {.callout-tip collapse="true"}
547+
## Solution: Full `run_simulation()`
499548

500549
```python
501550
def run_simulation(dt: float = 1.0) -> None:

0 commit comments

Comments
 (0)