Skip to content

Python API

from soliton import Simulation, kdv_soliton, nls_bright_soliton

model is "kdv" or "nls".

MemberDescription
xgrid positions, float64[points]
statefield in physical space, complex128[points]; assignable
spectrumFourier coefficients in FFT order
time, dt, points, lengthread-only properties
step(n=1)advance n time steps
invariants()dict of conserved quantities
reset_time()set the clock back to zero
kdv_soliton(x, t, speed, center) # complex128 array (imaginary part zero)
nls_bright_soliton(x, t, amplitude, velocity, center)

Example: measure the phase shift in a KdV collision

Section titled “Example: measure the phase shift in a KdV collision”
import numpy as np
from soliton import Simulation, kdv_soliton
sim = Simulation("kdv", 1024, 80.0, 5e-4)
sim.state = kdv_soliton(sim.x, 0, 6.0, -25.0) + kdv_soliton(sim.x, 0, 1.5, -5.0)
sim.step(16000)
crest = sim.x[np.argmax(sim.state.real)]
free_flight = -25.0 + 6.0 * sim.time
print("fast soliton advanced by", crest - free_flight)
from soliton import mechanics
mechanics.catalogue() # list of scenario specs with parameter schemas
scene = mechanics.scenario("rolling", angle=30, inertia_ratio=1.0)
scene.title, scene.labels, scene.answers()
run = scene.run(until=3.0, record_every=10)
run.t # (N,)
run.body("disc") # dict: position, velocity, acceleration (N, 2); angle, angular_velocity (N,)
run.force("disc", "friction") # (N, 2)
run.energy() # dict of (N,) arrays: kinetic, potential, dissipated, external_work, conserved
run.events # [{'time': 2.37, 'kind': 'contact_begin', 'bodies': ['ground', 'disc']}, ...]
run.finished # {'time': 2.37, 'reason': 'disc touched ground'} once the problem has ended, else None
scene.save("rolling.json"); mechanics.load("rolling.json")
mechanics.Scene.from_json(text) # validated; errors name the problem
# Class 12: charges and fields
scene = mechanics.scenario("field-of-two-charges")
scene.electric_field(0.0, 1.0) # (Ex, Ey) in N/C at a point, as the scene starts
scene.electric_potential(0.0, 1.0) # volts
run.force("test charge", "electric") # also "magnetic"
mechanics.scenario("gauss-law").electric_flux(-1.0, 0.0, 0.7) # N·m²/C out of a sphere: q inside / ε₀
mechanics.scenario("bar-magnet-field").magnetic_field(0.2, 0.0) # (Bx, By) in tesla, in the page
# Ray optics
from soliton import optics
bench = optics.scenario("convex-lens", focal=15, distance=40)
bench.answers() # {'Image distance, v (cm)': 24.0, ...} from the formulas
bench.measured() # the same, read off the traced rays
bench.rays() # list of (N, 2) arrays of points, metres
bench.images() # [{'base': (0.24, 0.0), 'tip': (0.24, -0.018), 'real': True}]
optics.Bench.from_json(bench.to_json()) # scenes are data here too
# Properties of matter
from soliton import matter
run = matter.scenario("buoyancy", block=920, liquid=1030) # ice in sea water
while not run.finished:
run.advance(0.02) # seconds
run.readings(), run.answers(), run.measured()
# Currents and magnetism
from soliton import magnetism
magnetism.scenario("straight-wire-field", current=5, distance=5).measured() # 20 µT, from Biot–Savart
run = magnetism.scenario("motional-emf", pull=0.1)
while not run.finished:
run.advance(0.02)
run.answers(), run.measured() # terminal speed FR/B²l², both ways
# Modern physics
from soliton import modern
modern.electron_wavelength(54) # 0.167 nm
modern.line_wavelength(3, 2) # 656 nm
modern.alpha_deflection(5.5, 79, 30) # degrees, by integrating the path
run = modern.scenario("radioactive-decay", half_life=4, nuclei=2500)
run.advance(10) # seconds
run.readings(), run.answers(), run.measured()
# Statistical mechanics
from soliton import statmech
run = statmech.scenario("ising-model", temperature=2.0, size=48)
run.advance(2000) # sweeps
run.spins() # (48, 48) of +1 and -1
run.answers(), run.measured() # Onsager's exact values; the averages found
statmech.scenario("phase-transition").points() # (n, 5): T, m, e, specific heat, susceptibility
statmech.onsager_magnetisation(2.0), statmech.CRITICAL_TEMPERATURE
# Circuits
from soliton import circuits
bridge = circuits.scenario("wheatstone-bridge", s=30)
bridge.current("G"), bridge.voltage("P") # amperes from a to b; volts, a above b
rc = circuits.scenario("charging-capacitor")
rc.advance(1.0) # seconds
t, v, i = rc.series("C")
V, I = circuits.scenario("series-lcr").phasor("R") # complex amplitudes
# Thermal physics
from soliton import thermal
gas = thermal.scenario("adiabatic-process", temperature=300)
while gas.stage < 2:
gas.advance(100) # picoseconds
gas.temperature, gas.volume # K, nm³
gas.answers(), gas.measured() # the formulas, and what the molecules gave
gas.velocities() # (N, 3) in m/s; also positions() in nm and species()
gas.series()["pressure"] # atm once a picosecond; also time, volume, temperature, work, heat, energy