A Computational Soap Box Slide
Project description
A Computational Soap Box Slide
This Python package is written by Toon Verstraelen for students of the course "Python for Scientists" (C004212) of the B.Sc. Physics and Astronomy at Ghent University.
Quick Start
You can install the soapboxslide Python module with:
pip install soapboxslide
This module implements a 2D surface that resembles a curved slide, suitable for computational soapbox racing.
To give you a quick idea, the following figure is created with the plot.py script in this repository,
and visualizes the slides boxcar_blitz.toml and brutal_bends.toml included in this repository:
Your eyes may need some time to adapt to the correct depth perception: black ridges are high-altitude separations between the colored valleys.
Concept
The overall idea is that students use the surface implemented in soapboxslide to simulate the dynamics of a particle (or a connected set of particles) sliding down.
In addition to correctly implementing the dynamics, an additional challenge is do so as quickly as possible without missing any of the waypoints shown as dotted circles.
Two classes of physical models can be considered:
-
The most convenient is to assume a model of point particles strictly bound to the surface with holonomic constraints. In this case, equations of motion can be derived using a Lagrangian, possibly with a generalized force to include non-conservative friction forces.
-
A more challenging scenario (not used for Py4Sci, but closer in spirit to soap box races) is to impose inequality constraints, allow particles to detach from the surface.
Slide Class Usage
One can load a surface from a TOML file and calculate the altitude at a given point, e.g. x=5 and y=38, as follows:
import numpy as np
from soapboxslide import Slide
slide = Slide.from_file("boxcar_blitz.toml")
print(slide(np.array([5.0, 38.0])))
This will show three results:
(array(2.05331579), array(0.20172586), array(14.75904983))
These three values have the following meaning (in meter):
- The progress along the track.
- The orthogonal(ish) deviation from the bottom of the track.
- The altitude of the track.
The slide() function is fully vectorized: one may also provide an array of points to calculate many altitudes efficiently.
(This is useful for plotting.)
The slide() function optionally supports alternative NumPy wrappers, such as those of JAX and its predecessor Autograd.
This allows for an efficient evaluation of analytical partial derivatives of the altitude.
For example, a vectorized calculation of many gradients is implemented as follows:
from functools import partial
import jax
import jax.numpy as jnp
import numpy as np
from soapboxslide import Slide
# We recommend double precision:
jax.config.update("jax_enable_x64", True)
# Define the slide and its gradient
slide = Slide.from_file("boxcar_blitz.toml")
alt_grad = jax.jit(jax.vmap(jax.grad(
lambda pos: slide(pos, npw=jnp)[2]
)))
# Compute the gradient at several points
pos = np.array([
[5.0, 38.0],
[12.1, 7.3],
[10.7, 25.5],
])
print(alt_grad(pos))
This will show three gradients, one for each row of pos:
[[ 0.21289734 0.10176584]
[-1.85311237 1.84445056]
[-2.16704263 -2.9280058 ]]
Storing and Sharing Trajectory Data
The soapboxslide module also implements a Trajectory class for storing the results of a numerical integration of one or more particles sliding over the surface.
This class performs an initial validation on your trajectory data upon construction features to_npz and from_npz methods with which trajectories can be saved to and loaded from files.
This is useful if you want to share your trajectory with someone, e.g. for review, and to implement separate computation and visualization scripts (or notebooks).
To use the Trajectory class, create an instance as follows after completing the numerical integration of the equations of motion:
traj = Trajectory(
time=...,
mass=...,
gamma=...,
pos=...,
vel=...,
grad=...,
hess=...,
spring_idx=...,
spring_par=...,
end_state=...,
stop_time=...,
stop_pos=...,
stop_vel=...,
)
traj.to_file("traj.npz")
In this Python snippet, you need to replace all triple dots by arrays you defined or computed.
The meaning of and requirements for all attributes can be found in the docstrings in soapboxslide.py.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file soapboxslide-1.0.0.tar.gz.
File metadata
- Download URL: soapboxslide-1.0.0.tar.gz
- Upload date:
- Size: 165.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0199816de5a7875b73c1bcf8e9949f116d6cb8de92ce122a72032a2238545d25
|
|
| MD5 |
8227556cab72e38fcb89a568bfd0429e
|
|
| BLAKE2b-256 |
4f6cfeb4b7e311a7e252545f1fdd5950ac355f4e1692daca917fd0f0a5cc02c5
|
File details
Details for the file soapboxslide-1.0.0-py3-none-any.whl.
File metadata
- Download URL: soapboxslide-1.0.0-py3-none-any.whl
- Upload date:
- Size: 23.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc9e9f452b1583d652025b1792246664c23e2193caa1fb55339a5d04b67f4d7b
|
|
| MD5 |
c2687d73764501bbab00371f9c379a88
|
|
| BLAKE2b-256 |
39cf5200a2cf31a18eea657366292c3abbea8b42e17ea5c8e90b0883a84b23e4
|