Skip to content

Flows

The CTFlows.Flows submodule is the central abstraction of CTFlows. A flow is a callable object that integrates a dynamical system over a time interval: you hand it an initial condition and a final time, and it returns the result.

CTFlows builds flows through a four-layer pipeline:

text
Data → Systems → Integrators → Flows → Trajectories

Each layer has a single responsibility. The arrows show the build order, not a data dependency: Systems and Integrators are constructed independently — one from Data, the other from integrator options — and only meet when build_flow(system, integrator) combines them into a Flow (see Building a flow):

LayerSubmoduleWhat it produces
DataCTBase.DataTyped function wrappers (VectorField, Hamiltonian, HamiltonianVectorField)
SystemsSystemsODE right-hand side + traits (VectorFieldSystem, HamiltonianSystem, …)
IntegratorsIntegratorsODE solver strategy (SciML)
FlowsFlowsCallable integration object (StateFlow, HamiltonianFlow, OptimalControlFlow, ControlledFlow)
TrajectoriesTrajectoriesResult container with semantic accessors (state, costate, control, time_grid)

Reading order

PageTopicKey types
Building a flowAssembling the pipelinebuild_system, build_flow, Flow
IntegratingCalling a flow, configuration objects, integrator optionsStateEndPointConfig, StateTrajectoryConfig
TrajectoriesReading the resultstate, costate, time_grid, plot
Multi-phase flowsConcatenating flows with switching timesMultiPhaseStateFlow, *
Optimal controlFlows from optimal control problemsOptimalControlFlow, Flow(ocp)
Control lawsFlows with control lawsControlledFlow, Flow(ocp, law), OpenLoop, ClosedLoop, DynClosedLoop
Constrained flowsPath-constraint terms on Flow(ocp, law)constraint, multiplier, hamiltonian_type
SciML flowsFlows from SciML functions and problemsFlow(::ODEFunction), Flow(::ODEProblem), SciMLProblemFlow

The data layer (wrapping functions as VectorField, Hamiltonian, HamiltonianVectorField) and the trait system (Autonomous, Fixed, InPlace, …) live in CTBase — see CTBase.Data and CTBase.Traits in the CTBase documentation. Likewise, the ODE solver strategy (SciML) is not implemented in CTFlows: it is provided by CTSolvers.Integrators and re-exported through CTFlows.Integrators — see SciML integrator internals for the split between the two packages.

Qualified access

CTFlows exports nothing at the package level. Bring submodules into scope explicitly:

julia
using CTFlows
using CTFlows.Flows        # StateFlow, HamiltonianFlow, Flow, build_flow
using CTBase.Data          # VectorField, Hamiltonian, HamiltonianVectorField
using CTFlows.Systems      # build_system
using CTFlows.Integrators  # SciML, build_integrator
using CTFlows.Trajectories # VectorFieldTrajectory, state, time_grid
using CTBase.Traits        # Autonomous, NonAutonomous, Fixed, NonFixed, InPlace, OutOfPlace
using CTFlows.Configs      # StateEndPointConfig, StateTrajectoryConfig, …
import OrdinaryDiffEqTsit5 # activates the SciML extension

Minimal end-to-end example

The fastest path from a function to an integrated trajectory:

julia
# 1. Wrap the dynamics as a VectorField
#    The function x -> -x is autonomous (no t) and fixed (no variable parameter)
vf = Data.VectorField(x -> -x)
VectorField: autonomous, fixed (no variable), out-of-place
  natural call: f(x)
  uniform call: f(t, x, v)
julia
# 2. Build the flow directly from the data (shortcut constructor)
flow = Flows.Flow(vf; reltol=1e-8, abstol=1e-8)
Flow
├─ system: VectorFieldSystem
          ├─ wraps: VectorField: autonomous, fixed (no variable), out-of-place
          └─ rhs: IPVFOoPRHS (out-of-place VF → in-place interface)
└─ integrator: SciML (abstol = 1.0e-8, reltol = 1.0e-8)
julia
# 3a. Point integration: final state only
xf = flow(0.0, [1.0, 0.0], 1.0)
2-element Vector{Float64}:
 0.36787944127643124
 0.0
julia
# 3b. Trajectory integration: full time history
sol = flow((0.0, 1.0), [1.0, 0.0])
VectorFieldTrajectory
├─ result: SciMLIntegrationResult
├─ tspan: (0.0, 1.0)
├─ time points: 16
└─ final state: [0.36787944127643124, 0.0]
julia
# 4. Read the result
ts = Trajectories.time_grid(sol)   # vector of time points
x  = Trajectories.state(sol)       # callable: x(t) → state at time t
x(0.5)                          # interpolate at t = 0.5
2-element Vector{Float64}:
 0.6065306592843308
 0.0

The shortcut Flows.Flow(vf; opts...) hides steps 2–3 of the pipeline (build_systembuild_integratorbuild_flow). See Building a flow for the explicit form.

Mathematical setting

We work on a state space  .

  • A vector field is a map    (or with time and/or variable arguments).

  • A Hamiltonian is a scalar map   ,   defined on the cotangent bundle.

  • A Hamiltonian vector field is the map    ,   .

Which extra arguments appear (, ) is encoded by the trait system — see CTBase.Traits.

An optimal control problem adds a control  , dynamics  , and a Mayer/Lagrange cost to minimise (or maximise). Its pseudo-Hamiltonian       (  ,    for :min/:max) still carries the control explicitly. Two routes turn it into a genuine Hamiltonian that a flow can integrate:

  • Control-free: no control at all — reduces directly to . This is Flow(ocp) — see Optimal control.

  • With a control law: a feedback (or ) closes the loop,  . This is Flow(ocp, law) / Flow(h̃, law) — see Control laws.

See also