utils¶
Utility modules: quick-start presets, batched parameter sweeps, and HDF5 I/O.
Presets¶
Quick-start factory functions for common bubble configurations.
jbubble.utils.presets.BubblePreset
¶
Bases: NamedTuple
Assembled (EoM, pulse) pair returned by preset factory functions.
Can be unpacked directly::
eom, pulse = free_bubble()
or accessed by name::
preset = free_bubble()
result = run_simulation(preset.eom, preset.pulse)
jbubble.utils.presets.free_bubble(R0=2e-06, freq=1000000.0, pressure=100000.0, cycle_num=5, *, gamma=1.4, sigma=0.072, mu=0.001, P_amb=101325.0, rho_L=998.0, c_L=1500.0)
¶
Uncoated gas bubble in a Newtonian liquid (Keller-Miksis).
The simplest physically meaningful preset: a polytropic gas bubble with Laplace surface tension only — no shell coating. Suitable for free cavitation bubbles and as a baseline before adding a shell.
Physics: Keller-Miksis EoM + PolytropicGas + NoShell + NewtonianMedium.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
R0
|
float
|
Equilibrium radius [m]. Default: 2 µm. |
2e-06
|
freq
|
float
|
Driving frequency [Hz]. Default: 1 MHz. |
1000000.0
|
pressure
|
float
|
Peak acoustic pressure amplitude [Pa]. Default: 100 kPa. |
100000.0
|
cycle_num
|
int
|
Number of tone-burst cycles. Default: 5. |
5
|
gamma
|
float
|
Polytropic exponent. Default: 1.4 (diatomic gas / air). |
1.4
|
sigma
|
float
|
Surface tension [N/m]. Default: 0.072 (air–water interface). |
0.072
|
mu
|
float
|
Liquid dynamic viscosity [Pa·s]. Default: 1e-3 (water, 20 °C). |
0.001
|
P_amb
|
float
|
Ambient pressure [Pa]. Default: 101 325 (1 atm). |
101325.0
|
rho_L
|
float
|
Liquid density [kg/m³]. Default: 998 (water, 20 °C). |
998.0
|
c_L
|
float
|
Speed of sound in the liquid [m/s]. Default: 1500 (water). |
1500.0
|
Returns:
| Type | Description |
|---|---|
BubblePreset
|
|
Source code in jbubble/utils/presets.py
jbubble.utils.presets.lipid_bubble(R0=2e-06, freq=1000000.0, pressure=100000.0, cycle_num=5, *, kappa_s=2.4e-09, chi=0.55, sigma_rupture=0.072, R_buckle_ratio=0.98, gamma=1.4, mu=0.001, P_amb=101325.0, rho_L=998.0, c_L=1500.0)
¶
Lipid-shelled ultrasound contrast agent — Marmottant (2005) model.
Models a thin lipid monolayer with surface-dilatational viscosity and a smooth Gompertz surface tension law (preferred over the piecewise Marmottant law for gradient-based parameter fitting). Representative of clinical agents such as SonoVue and Definity.
Physics: Keller-Miksis EoM + PolytropicGas + LipidShell(GompertzSurfaceTension) + NewtonianMedium.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
R0
|
float
|
Equilibrium radius [m]. Default: 2 µm. |
2e-06
|
freq
|
float
|
Driving frequency [Hz]. Default: 1 MHz. |
1000000.0
|
pressure
|
float
|
Peak acoustic pressure amplitude [Pa]. Default: 100 kPa. |
100000.0
|
cycle_num
|
int
|
Number of tone-burst cycles. Default: 5. |
5
|
kappa_s
|
float
|
Shell surface-dilatational viscosity [N·s/m]. Default: 2.4e-9 (Marmottant 2005, BR14 / SonoVue-type). |
2.4e-09
|
chi
|
float
|
Shell elasticity [N/m]. Default: 0.55. |
0.55
|
sigma_rupture
|
float
|
Asymptotic (ruptured) surface tension [N/m]. Default: 0.072 (water). |
0.072
|
R_buckle_ratio
|
float
|
Buckling radius as fraction of R0. Default: 0.98 — gives an
initial surface tension |
0.98
|
gamma
|
float
|
Polytropic exponent. Default: 1.4. |
1.4
|
mu
|
float
|
Liquid dynamic viscosity [Pa·s]. Default: 1e-3 (water). |
0.001
|
P_amb
|
float
|
Ambient pressure [Pa]. Default: 101 325 (1 atm). |
101325.0
|
rho_L
|
float
|
Liquid density [kg/m³]. Default: 998 (water). |
998.0
|
c_L
|
float
|
Speed of sound in the liquid [m/s]. Default: 1500 (water). |
1500.0
|
Returns:
| Type | Description |
|---|---|
BubblePreset
|
|
References
Marmottant et al., J. Acoust. Soc. Am. 118 (2005) 3499–3505.
Source code in jbubble/utils/presets.py
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 199 200 201 202 203 204 205 206 207 208 209 210 | |
jbubble.utils.presets.thick_shell_bubble(R0=2e-06, freq=1000000.0, pressure=100000.0, cycle_num=5, *, d_s=1.5e-08, G_s=10000000.0, mu_s=0.5, sigma=0.04, gamma=1.4, mu=0.001, P_amb=101325.0, rho_L=998.0, c_L=1500.0)
¶
Polymer thick-shell bubble — Church (1995) model.
Models a thick viscoelastic shell with elastic and viscous contributions. Representative of polymer-shelled agents such as Optison (albumin), or experimental PLGA microbubbles.
Physics: Keller-Miksis EoM + PolytropicGas + ThickShell + NewtonianMedium.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
R0
|
float
|
Equilibrium radius [m]. Default: 2 µm. |
2e-06
|
freq
|
float
|
Driving frequency [Hz]. Default: 1 MHz. |
1000000.0
|
pressure
|
float
|
Peak acoustic pressure amplitude [Pa]. Default: 100 kPa. |
100000.0
|
cycle_num
|
int
|
Number of tone-burst cycles. Default: 5. |
5
|
d_s
|
float
|
Shell thickness [m]. Default: 15 nm. |
1.5e-08
|
G_s
|
float
|
Shell shear modulus [Pa]. Default: 10 MPa (stiff polymer shell). |
10000000.0
|
mu_s
|
float
|
Shell viscosity [Pa·s]. Default: 0.5. |
0.5
|
sigma
|
float
|
Surface tension [N/m]. Default: 0.04 (reduced by polymer coating). |
0.04
|
gamma
|
float
|
Polytropic exponent. Default: 1.4. |
1.4
|
mu
|
float
|
Liquid dynamic viscosity [Pa·s]. Default: 1e-3 (water). |
0.001
|
P_amb
|
float
|
Ambient pressure [Pa]. Default: 101 325 (1 atm). |
101325.0
|
rho_L
|
float
|
Liquid density [kg/m³]. Default: 998 (water). |
998.0
|
c_L
|
float
|
Speed of sound in the liquid [m/s]. Default: 1500 (water). |
1500.0
|
Returns:
| Type | Description |
|---|---|
BubblePreset
|
|
References
Church, J. Acoust. Soc. Am. 97 (1995) 1510–1521.
Source code in jbubble/utils/presets.py
GridSweep¶
Memory-efficient batched Cartesian-product parameter sweeps.
jbubble.utils.gridsweep.GridSweep(fn, search_space, batch_size=512, progress=True)
¶
Batched Cartesian-product sweep over named parameter axes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable
|
|
required |
search_space
|
dict[str, ndarray]
|
|
required |
batch_size
|
int
|
Number of grid points evaluated per vmapped call. |
512
|
progress
|
bool
|
Show a |
True
|
Source code in jbubble/utils/gridsweep.py
grid_shape
property
¶
Shape of the full parameter grid (one int per axis).
total_points
property
¶
Total number of grid points.
axes
property
¶
Parameter axes in sweep order.
batches()
¶
Lazy iterator over the grid.
Yields:
| Name | Type | Description |
|---|---|---|
params |
dict[str, ndarray]
|
Batch of parameter vectors, one per grid point in the batch. |
outputs |
PyTree
|
Corresponding vmapped outputs from |
Source code in jbubble/utils/gridsweep.py
run()
¶
Run the full sweep and return a grid-shaped PyTree.
Returns:
| Type | Description |
|---|---|
PyTree
|
Each leaf has shape |
Source code in jbubble/utils/gridsweep.py
Example¶
import jax.numpy as jnp
from jbubble.utils.gridsweep import GridSweep
from jbubble import run_simulation, SaveSpec
from jbubble.utils.presets import free_bubble
def peak_ratio(R0, pressure):
preset = free_bubble(R0=R0, pressure=pressure)
result = run_simulation(
preset.eom, preset.pulse,
save_spec=SaveSpec(500),
t_max=10e-6,
)
return result.radius.max() / R0
sweep = GridSweep(
fn=peak_ratio,
search_space={
"R0": jnp.linspace(1e-6, 5e-6, 20),
"pressure": jnp.array([50e3, 100e3, 200e3, 400e3]),
},
batch_size=256,
)
grid = sweep.run() # shape (20, 4)
print(sweep.grid_shape) # (20, 4)
print(sweep.total_points) # 80
HDF5 I/O¶
jbubble.utils.io.export_hdf5(path, *, metadata=None, **arrays)
¶
Save named arrays and optional metadata to an HDF5 file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
Output |
required |
metadata
|
dict
|
JSON-serialisable metadata stored as an attribute on the root group. |
None
|
**arrays
|
Any
|
Keyword arguments are saved as datasets. Values are converted to
NumPy arrays via |
{}
|
Examples:
::
result = run_simulation(eom, pulse, ...)
p_em = jax.vmap(lambda r: emission(result, r))(distances)
export_hdf5(
"training_data.h5",
ts=result.ts,
R=result.state.R,
R_dot=result.state.R_dot,
p_emission=p_em,
distances=distances,
metadata={"R0": 2e-6, "freq": 1e6},
)
Source code in jbubble/utils/io.py
jbubble.utils.io.load_hdf5(path)
¶
Load arrays and metadata from an HDF5 file written by :func:export_hdf5.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
Path to the |
required |
Returns:
| Name | Type | Description |
|---|---|---|
arrays |
dict[str, ndarray]
|
All datasets in the file, keyed by name. |
metadata |
dict
|
The metadata dict, or |
Source code in jbubble/utils/io.py
Example¶
from jbubble.utils.io import export_hdf5, load_hdf5
import jax.numpy as jnp
export_hdf5(
"results.h5",
metadata={"description": "sweep", "freq": 1e6, "n_cycles": 5},
R0=jnp.linspace(1e-6, 5e-6, 20),
peak_expansion=grid,
)
arrays, meta = load_hdf5("results.h5")
print(meta["description"]) # "sweep"
print(arrays["peak_expansion"].shape) # (20, 4)