Python API
from soliton import Simulation, kdv_soliton, nls_bright_solitonSimulation(model, points, length, dt)
Section titled “Simulation(model, points, length, dt)”model is "kdv" or "nls".
| Member | Description |
|---|---|
x | grid positions, float64[points] |
state | field in physical space, complex128[points]; assignable |
spectrum | Fourier coefficients in FFT order |
time, dt, points, length | read-only properties |
step(n=1) | advance n time steps |
invariants() | dict of conserved quantities |
reset_time() | set the clock back to zero |
Exact solutions
Section titled “Exact solutions”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 npfrom 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.timeprint("fast soliton advanced by", crest - free_flight)Mechanics
Section titled “Mechanics”from soliton import mechanics
mechanics.catalogue() # list of scenario specs with parameter schemasscene = 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, conservedrun.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 Nonescene.save("rolling.json"); mechanics.load("rolling.json")mechanics.Scene.from_json(text) # validated; errors name the problem
# Class 12: charges and fieldsscene = mechanics.scenario("field-of-two-charges")scene.electric_field(0.0, 1.0) # (Ex, Ey) in N/C at a point, as the scene startsscene.electric_potential(0.0, 1.0) # voltsrun.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 opticsfrom soliton import opticsbench = optics.scenario("convex-lens", focal=15, distance=40)bench.answers() # {'Image distance, v (cm)': 24.0, ...} from the formulasbench.measured() # the same, read off the traced raysbench.rays() # list of (N, 2) arrays of points, metresbench.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 matterfrom soliton import matterrun = matter.scenario("buoyancy", block=920, liquid=1030) # ice in sea waterwhile not run.finished: run.advance(0.02) # secondsrun.readings(), run.answers(), run.measured()
# Currents and magnetismfrom soliton import magnetismmagnetism.scenario("straight-wire-field", current=5, distance=5).measured() # 20 µT, from Biot–Savartrun = 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 physicsfrom soliton import modernmodern.electron_wavelength(54) # 0.167 nmmodern.line_wavelength(3, 2) # 656 nmmodern.alpha_deflection(5.5, 79, 30) # degrees, by integrating the pathrun = modern.scenario("radioactive-decay", half_life=4, nuclei=2500)run.advance(10) # secondsrun.readings(), run.answers(), run.measured()
# Statistical mechanicsfrom soliton import statmechrun = statmech.scenario("ising-model", temperature=2.0, size=48)run.advance(2000) # sweepsrun.spins() # (48, 48) of +1 and -1run.answers(), run.measured() # Onsager's exact values; the averages foundstatmech.scenario("phase-transition").points() # (n, 5): T, m, e, specific heat, susceptibilitystatmech.onsager_magnetisation(2.0), statmech.CRITICAL_TEMPERATURE
# Circuitsfrom soliton import circuitsbridge = circuits.scenario("wheatstone-bridge", s=30)bridge.current("G"), bridge.voltage("P") # amperes from a to b; volts, a above brc = circuits.scenario("charging-capacitor")rc.advance(1.0) # secondst, v, i = rc.series("C")V, I = circuits.scenario("series-lcr").phasor("R") # complex amplitudes
# Thermal physicsfrom soliton import thermalgas = thermal.scenario("adiabatic-process", temperature=300)while gas.stage < 2: gas.advance(100) # picosecondsgas.temperature, gas.volume # K, nm³gas.answers(), gas.measured() # the formulas, and what the molecules gavegas.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