Solver plugins¶
A solver integrates the condensate and reservoir fields. The controller builds a
Model containing the grid, batched physics,
potential, pump, reservoir, boundary condition, and optional stochastic noise.
A plugin receives this model at construction and for every step.
Contract¶
from polarism.solver import AbstractSolver, ACCEPTED, SolverCapabilities, register_solver
POLARISM_PLUGIN_API = 2
@register_solver("my-solver")
class MySolver(AbstractSolver):
capabilities = SolverCapabilities(ndim={1, 2}, kinetic_relaxation=True,
description="Example solver")
def step(self, model, state, dt):
rate, reservoir_rates = model.rhs(
state.field, model.reservoir.get_state(), state.t
)
state.field = model.boundary.after_step_action(state.field + dt * rate)
model.reservoir.set_state(tuple(
field + dt * derivative
for field, derivative in zip(model.reservoir.get_state(), reservoir_rates)
))
state.step += 1
state.t = state.t0 + state.step * dt
return ACCEPTED
AbstractSolver.__init__(config, grid, model) sets self.config,
self.physics, self.batch_physics, and self.xp. The factory always calls
SolverClass(config, grid, model). model.physics is BatchPhysics, whose
get(name) returns a scalar or an array broadcast across batch members.
step(model, state, dt) returns a frozen StepReport. Fixed-step solvers return
the shared ACCEPTED object. An accepted step updates state.step and sets
state.t = state.t0 + state.step * dt; this keeps stage times stable through
chunked runs and checkpoint resume. state.field and reservoir fields use
(B, 1, *S) arrays, with S=(nx,) or (ny, nx). Assigning state.field
invalidates its spectral cache.
Adaptive solvers declare capabilities.adaptive=True and provide an embedded
tableau. In adaptive mode, step(model, state, h) is a trial: it commits the
field, spectral field, reservoir, and state.t += h only when the normalized
error is at most one. It leaves state.step unchanged. A rejected trial leaves
the state untouched. Return StepReport(accepted, error, dt_next), with a
finite normalized error and optionally a proposed next trial size. When
dt_next is None, the inherited advance uses its PI controller. The
driver counts schedule ticks and sets the final tick time. Adaptive solvers
must route advance through that driver.
The inherited advance(model, state, n_steps) calls step and applies
model.noise after each accepted step. Override advance only when a solver
can combine multiple steps efficiently. The override owns the same time and
step updates, and must apply stochastic noise at the end of each completed
step. Pump samples come from model.pump(t) at the method's stage times.
model.rhs(field, reservoir_fields, t, out=None) is the finite difference
reference derivative. It includes the configured potential, interactions,
reservoir blueshifts and gain, decay, kinetic relaxation, configured extra
terms, and reservoir rates. A simple plugin can use it directly. A solver with fused kernels can
read model components and use its own stage implementation.
Capabilities¶
The controller checks SolverClass.capabilities during validation. The frozen
SolverCapabilities fields describe component counts, dimensions, grid
boundaries, built-in reservoirs, plugin reservoir support, potential, kinetic
relaxation, dispersion, stochastic noise, adaptive stepping, coherent drive,
quantitative reservoir support, and a description. Every field has a
conservative default (one component, 2D, periodic grid, all built-in
reservoirs through the generic interface, parabolic dispersion, both noise
modes, no kinetic relaxation), so declare only what differs. Set-valued
fields accept plain sets. Declare only modes that your solver actually
implements. A plugin with no declaration receives a
warning and conservative defaults: one component, 2D periodic grid, generic
reservoir interface, parabolic dispersion, no kinetic relaxation.
terms (default True) says whether the solver adds the extra
terms from cfg.terms; a solver that integrates with model.rhs
gets them automatically, and a solver with its own kernels declares
terms=False until it implements them. dispersion lists the
dispersion names the solver accepts, or {"*"} when it builds
its kinetic propagator from model.dispersion.energy(grid, model.physics) for
every registered kind. Validation rejects a configuration outside either
declaration.
custom_reservoirs is "quantitative", "diagnostic", or "unsupported".
The quantitative_reservoirs set identifies built-in reservoirs appropriate
for quantitative coupling comparisons. rk4-fdm-fused rejects
reservoir_type: none; the other built-in solvers support it.
Midpoint example¶
"""A small midpoint solver using the reference model right-hand side."""
from polarism.solver import AbstractSolver, ACCEPTED, SolverCapabilities, register_solver
POLARISM_PLUGIN_API = 2
@register_solver("example-midpoint-fdm")
class MidpointFDMSolver(AbstractSolver):
"""Integrate the model with the explicit midpoint method."""
capabilities = SolverCapabilities(
ndim={1, 2}, grid_types={"periodic", "closed-interval"},
kinetic_relaxation=True,
description="Reference finite-difference midpoint solver.",
)
def step(self, model, state, dt):
psi = state.field
fields = model.reservoir.get_state()
rate, derivatives = model.rhs(psi, fields, state.t)
midpoint = psi + 0.5 * dt * rate
middle_fields = tuple(field + 0.5 * dt * derivative
for field, derivative in zip(fields, derivatives))
middle_rate, middle_derivatives = model.rhs(
midpoint, middle_fields, state.t + 0.5 * dt)
state.field = model.boundary.after_step_action(psi + dt * middle_rate).astype(psi.dtype, copy=False)
model.reservoir.set_state(tuple(field + dt * derivative
for field, derivative in zip(fields, middle_derivatives)))
state.step += 1
state.t = state.t0 + state.step * dt
return ACCEPTED
The example evaluates model.rhs at the start and midpoint and updates the
condensate and reservoir together. Compare it with rk4-fdm using a small
step size when changing the model or solver.