Examples¶
Each example in the first table repeats a calculation from the treams documentation with treams-rs. Its page shows the treams-rs script, the same calculation in treams 0.4.5 and the printed results.
| Example | What it computes | treams counterpart |
|---|---|---|
| Single sphere | Averaged extinction and scattering spectra up to l = 4 and for dipoles only; the intensity around the sphere | sphere.py |
| Cluster of spheres | Cross sections and near fields of four coupled spheres, with local, global and rotated T-matrices | cluster.py |
| Chain of spheres | The near field of a chain periodic along z | chain.py |
| Grid of spheres | The near field of a square array in the xy plane | grid.py |
| Photonic crystal | The smallest singular value of the lattice interaction matrix of a cubic crystal | crystal.py |
| Chiral slab | Transmission and reflection of a chiral slab for both helicities | slab.py |
| Array of spheres on a slab | Transmission and reflection of a square array of chiral spheres on a slab at oblique incidence | array_spheres.py |
| Band structure | Bloch wavenumbers of a periodic stack of sphere arrays and slabs | band_structure.py |
| Single cylinder | Averaged extinction and scattering cross widths up to m = 4 and for m = 0 only; the intensity around the cylinder | cylinder_tmatrixc.py |
| Chain of spheres in cylindrical waves | The near field of a chain periodic along z, from its cylindrical T-matrix and from spherical waves | chain_tmatrixc.py |
| Chain and cylinder | The near field of a chain of spheres coupled to an infinite cylinder | cluster_tmatrixc.py |
| Cylinder crystal | The smallest singular value of the lattice interaction matrix of a square lattice of cylinders | crystal_tmatrixc.py |
| Grating | The near field of chains of spheres and cylinders repeated along x | grating_tmatrixc.py |
| Array of spheres from cylindrical waves | Transmission and reflection of the sphere array on a slab, built from chains in cylindrical waves | array_spheres_tmatrixc.py |
Beyond treams¶
These examples differentiate a result with respect to the geometry, which treams cannot do. They have no treams counterpart; each checks its gradient against central differences.
| Example | What it computes | Framework |
|---|---|---|
| Sphere radius optimization | Five gradient-ascent steps on the radius of a sphere to maximize its scattering efficiency | Advect |
| Array transmission gradient | The derivatives of the transmission of a sphere array on a slab with respect to the sphere radius and the slab thickness | JAX |
| Changes across a field map | Forward-mode changes at every field point when the wavelength or common sphere radius changes | JAX |
| Cluster scattering gradient | Reverse-mode changes in one scattering cross section with respect to eight radii and all particle positions | JAX |
Choosing a differentiation mode compares both modes on the same results, with a runnable timing script.
Running the examples¶
Run a script from the repository root with treams-rs installed:
python docs/examples/sphere.py
The scripts print tables and draw no figures. Constants at the top, such as
FREQUENCIES and GRID_POINTS, set small sizes so that each script runs in
under a second. Enlarge them for full sweeps; the treams versions use 200
frequencies and grids of up to 101 x 101 points. The two gradient examples
need the treams-rs[advect] and treams-rs[jax] extras; the JAX example
takes about five seconds.
The test suite runs every script and compares what it prints with the recorded output, to a relative tolerance of 1e-6. When treams is installed, it also runs the treams versions and compares the results, to a relative tolerance of 1e-10. Values near zero also get an absolute tolerance. The grating and the array of spheres from cylindrical waves get a relative tolerance of 3e-8 and 2e-8, because treams sums a lattice of cylinders less accurately. The test states the reason for each tolerance.