From bfb5feb81975775c7e61f65f835fead585739f36 Mon Sep 17 00:00:00 2001 From: Caitlin Date: Mon, 26 Aug 2024 12:54:10 -0400 Subject: [PATCH 1/2] add lorenz example --- examples/desktop/misc/lorenz_animation.py | 92 +++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 examples/desktop/misc/lorenz_animation.py diff --git a/examples/desktop/misc/lorenz_animation.py b/examples/desktop/misc/lorenz_animation.py new file mode 100644 index 000000000..b50b504ad --- /dev/null +++ b/examples/desktop/misc/lorenz_animation.py @@ -0,0 +1,92 @@ +""" +Lorenz System Animation +======================= + +Example the Lorenz attractor. +""" + +# test_example = false +# sphinx_gallery_pygfx_docs = 'animate 10s' + +import fastplotlib as fpl +import numpy as np + + +# generate data +def lorenz(xyz, *, s=10, r=28, b=2.667): + """ + Parameters + ---------- + xyz : array-like, shape (3,) + Point of interest in three-dimensional space. + s, r, b : float + Parameters defining the Lorenz attractor. + + Returns + ------- + xyz_dot : array, shape (3,) + Values of the Lorenz attractor's partial derivatives at *xyz*. + """ + x, y, z = xyz + x_dot = s * (y - x) + y_dot = r * x - y - x * z + z_dot = x * y - b * z + return np.array([x_dot, y_dot, z_dot]) + + +dt = 0.01 +num_steps = 3_000 + +lorenz_data = list() + +for i in range(5): + xyzs = np.empty((num_steps + 1, 3)) # Need one more for the initial values + xyzs[0] = (0., (i * 0.3) + 1, 1.05) # Set initial values + # Step through "time", calculating the partial derivatives at the current point + # and using them to estimate the next point + for i in range(num_steps): + xyzs[i + 1] = xyzs[i] + lorenz(xyzs[i]) * dt + + lorenz_data.append(xyzs) + +figure = fpl.Figure( + cameras="3d", + controller_types="fly" +) + +lorenz_line = figure[0, 0].add_line_collection(data=lorenz_data, thickness=.1, cmap="tab10") + +scatter_markers = list() + +for graphic in lorenz_line: + marker = figure[0, 0].add_scatter(graphic.data.value[0], sizes=8) + scatter_markers.append(marker) + +# initialize time +time = 0 + + +def animate(supblot): + global time + + time += 2 + + if time >= xyzs.shape[0]: + time = 0 + + for scatter, g in zip(scatter_markers, lorenz_line): + scatter.data = g.data.value[time] + + +figure[0, 0].add_animations(animate) + +figure.show() + +# set initial camera position to make animation in gallery render better +figure[0, 0].camera.world.z = 75 + +# NOTE: `if __name__ == "__main__"` is NOT how to use fastplotlib interactively +# please see our docs for using fastplotlib interactively in ipython and jupyter +if __name__ == "__main__": + print(__doc__) + fpl.run() From a3c12186ffdbfc7403d4c400a93edc833e91fb27 Mon Sep 17 00:00:00 2001 From: Caitlin Date: Sun, 1 Sep 2024 11:08:15 -0400 Subject: [PATCH 2/2] requested changes --- examples/desktop/misc/lorenz_animation.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/desktop/misc/lorenz_animation.py b/examples/desktop/misc/lorenz_animation.py index b50b504ad..cf7a77b38 100644 --- a/examples/desktop/misc/lorenz_animation.py +++ b/examples/desktop/misc/lorenz_animation.py @@ -2,7 +2,7 @@ Lorenz System Animation ======================= -Example the Lorenz attractor. +Example of the Lorenz attractor. """ # test_example = false @@ -37,17 +37,17 @@ def lorenz(xyz, *, s=10, r=28, b=2.667): dt = 0.01 num_steps = 3_000 -lorenz_data = list() +lorenz_data = np.empty((5, num_steps + 1, 3)) for i in range(5): xyzs = np.empty((num_steps + 1, 3)) # Need one more for the initial values xyzs[0] = (0., (i * 0.3) + 1, 1.05) # Set initial values # Step through "time", calculating the partial derivatives at the current point # and using them to estimate the next point - for i in range(num_steps): - xyzs[i + 1] = xyzs[i] + lorenz(xyzs[i]) * dt + for j in range(num_steps): + xyzs[j + 1] = xyzs[j] + lorenz(xyzs[j]) * dt - lorenz_data.append(xyzs) + lorenz_data[i] = xyzs figure = fpl.Figure( cameras="3d", @@ -59,7 +59,7 @@ def lorenz(xyz, *, s=10, r=28, b=2.667): scatter_markers = list() for graphic in lorenz_line: - marker = figure[0, 0].add_scatter(graphic.data.value[0], sizes=8) + marker = figure[0, 0].add_scatter(graphic.data.value[0], sizes=8, colors=graphic.colors[0]) scatter_markers.append(marker) # initialize time