simulation¶
High-level simulation runner and result container.
from jbubble import run_simulation, SaveSpec
SimulationResult
¶
SimulationResult(
ts: Array,
state: BubbleState,
state_dot: BubbleState,
driving_pressure: Array,
converged: Array,
)
Bases: Module
Output of run_simulation.
Generalises over any BubbleState
subclass: when you add degrees of freedom, such as temperature, to the
state, this class needs no changes.
All array quantities are in SI units, sampled at ts.
Attributes:
| Name | Type | Description |
|---|---|---|
ts |
(Array, shape(N))
|
Time points [s]. |
state |
BubbleState, each field shape (N,)
|
Full state trajectory. |
state_dot |
BubbleState, each field shape (N,)
|
Time-derivative trajectory, \(\mathrm{d}(\text{state})/\mathrm{d}t\),
that the equation of motion returns. |
driving_pressure |
(Array, shape(N))
|
Applied acoustic pressure at the bubble [Pa]. |
converged |
Array
|
Boolean scalar: |
run_simulation
¶
run_simulation(
eom: EquationOfMotion,
pulse: Pulse,
*,
save_spec: SaveSpec | None = None,
state0: Any = None,
t_max: ArrayLike | None = None,
config: SolverConfig | None = None,
adjoint: AbstractAdjoint | None = None,
progress: bool = False,
) -> SimulationResult
Run a simulation and return the results in SI units.
run_simulation works under jax.jit and jax.vmap, and you can
differentiate it with jax.grad. If the solver fails, for example
because it reaches config.max_steps, the samples after the failure
are inf and result.converged is False. Called eagerly,
run_simulation also warns. Under jax.jit or jax.vmap it can't
inspect the flag, so check result.converged yourself.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eom
|
EquationOfMotion
|
Assembled equation of motion, such as
|
required |
pulse
|
Pulse
|
Driving pulse. |
required |
save_spec
|
SaveSpec
|
Output specification (number of samples). |
None
|
state0
|
BubbleState
|
Initial state. |
None
|
t_max
|
float
|
Integration end time [s]. |
None
|
config
|
SolverConfig
|
ODE solver settings. |
None
|
adjoint
|
AbstractAdjoint
|
How |
None
|
progress
|
bool
|
Whether to show a text progress meter. Default: |
False
|
Returns:
| Type | Description |
|---|---|
SimulationResult
|
Trajectory, time derivatives, driving pressure, and convergence flag. |
Warns:
| Type | Description |
|---|---|
UserWarning
|
If the solver didn't converge and |
Source code in jbubble/simulation.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
Working with SimulationResult¶
SimulationResult is an Equinox module, so it is a JAX PyTree. All fields are jax.Array and can be passed through jax.jit, jax.vmap, and jax.grad.
result = jax.jit(run_simulation)(eom, pulse, save_spec=SaveSpec(1000), t_max=10e-6)
# Convenience properties
ts = result.ts # shape (N,) — time axis [s]
R = result.radius # shape (N,) — bubble radius [m]
Rd = result.radial_velocity # shape (N,) — wall velocity [m/s]
Rdd = result.radial_acceleration # shape (N,) — wall acceleration [m/s²]
pac = result.driving_pressure # shape (N,) — acoustic pressure [Pa]
# Full state and derivatives
state = result.state # BubbleState with shape-(N,) arrays
state_dot = result.state_dot # BubbleState time derivatives
# Solver diagnostics
ok = result.converged # bool — False if ODE hit max_steps