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': 0.0, 'kind': 'contact_begin', 'bodies': ['incline', 'disc']}, ...]
scene.save("rolling.json"); mechanics.load("rolling.json")
mechanics.Scene.from_json(text) # validated; errors name the problem