Skip to content

Shear rheology optimizer

vertax.bilevelopt.rheobop

Bilevel optimizer for bulk-driven shear rheometry: design \(\bm\theta\) against a compliance.

Implements the centered Equilibrium-Propagation estimator over the two driven conditions \(\pm a_0\), with an alternating (block-coordinate) nudged phase.

Why this lives at the optimizer layer

PbcMesh and BoundedMesh are distinct classes because they carry distinct data structures. The sheared problem carries the same structure as PbcMesh — same tables, same box, same offsets, same T1 rules. What is new is that there are two meshes and a protocol relating them. A ShearedPbcMesh would duplicate PbcMesh in full while leaving the genuinely new thing homeless.

The estimator

With \(\mathbf Z=(\mathbf X_+,\mathbf X_-)\), \(\mathcal E_{\rm tot}=\mathcal E_{+a_0}(\mathbf X_+)+\mathcal E_{-a_0}(\mathbf X_-)\), \(\widehat J=(U(\mathbf X_+)-U(\mathbf X_-))/(2a_0)\) and \(\widehat{\mathcal C}=(\widehat J/J_{\rm tgt}-1)^2\):

\[ \widehat{\nabla_{\bm\theta}\mathcal C}=\frac{1}{2\beta}\Big[ \big(\partial_{\bm\theta}\mathcal E(\mathbf X_+^{+\beta}) +\partial_{\bm\theta}\mathcal E(\mathbf X_-^{+\beta})\big) -\big(\partial_{\bm\theta}\mathcal E(\mathbf X_+^{-\beta}) +\partial_{\bm\theta}\mathcal E(\mathbf X_-^{-\beta})\big)\Big] \]

Four properties of the estimator that the implementation relies on:

  1. The outer cost carries no \(\bm\theta\). \(U\) is a linear functional of the vertex positions alone, so \(\partial_{\bm\theta}\widehat{\mathcal C}|_{\mathbf X}=0\) exactly and this collapses directly to the two bare parameter gradients. This is not a convenience: loss_function_outer is called without the parameters, and _compute_ep_grads differentiates \(\mathcal E+\beta\mathcal C\), so a \(\bm\theta\)-dependent cost would lose its explicit term silently.
  2. The \(\beta\) convention is fixed: nudges at \(\pm\beta\), prefactor \(1/(2\beta)\), not \(\beta/2\) with prefactor \(1/\beta\).
  3. Every nudged solve is warm-started from its own free equilibrium, and the \(+\beta\) and \(-\beta\) solves start from the same point. That pairing is what makes EP's residual error track the inner budget rather than \(\beta\) — measured to be \(\beta\)-independent across five decades.
  4. This is not two calls to outer_eq_prop. That routine performs its own free-and-nudged pair internally with a fixed partner and cannot express the alternation; the estimator above is evaluated directly from the four recorded states.

The solver is injected, not chosen here

The library does not fix the configuration of the inner solver, which for this problem should be a line-search conjugate gradient in double precision. solver_factory is a caller-supplied zero-argument callable returning a fresh optax.GradientTransformation. minimize is called directly rather than through inner_opt because inner_opt does not forward cap_vertex_step, and a line-search solver with T1 enabled needs it: it chooses its own step and can otherwise move a vertex across whole cells in one iteration, breaking the incremental T1 detector.

Choosing the nudge beta: it scales as the square of the amplitude, and this is not optional

The cost is written on the compliance \(\widehat J=(U_+-U_-)/(2a_0)\), so its curvature in \(U\) carries a factor \(1/(2a_0)^2\). At \(a_0=7\times10^{-4}\) that is \(5.1\times10^{5}\), while the drive mode's own stiffness is \(\sum\hat c^2/J\approx4.3\). The nudge therefore out-stiffens the tissue by four orders of magnitude at the conventional \(\beta=10^{-2}\): the nudged solve drives \(\widehat{\mathcal C}\) to zero and the state moves by exactly the distance to the target, independently of \(\beta\). Measured: \(\delta U/U=-9.27\%\) at \(\beta=3\times10^{-3}\) and \(-9.52\%\) at \(\beta=1\), a 333-fold \(\beta\) range. That is a hard constraint, not a nudge, and differencing two saturated states gives noise which \(1/(2\beta)\) then amplifies — the estimator came out \(3\times10^{4}\) times the finite-difference reference and scaled as \(1/\beta\).

Linear response requires \(\beta\,\dfrac{2}{(2a_0J_{\rm tgt})^2}\ll\dfrac{\sum\hat c^2}{J}\), i.e. \(\beta=O(a_0^2)\) — here \(\beta\lesssim8\times10^{-5}\). Use ShearRheologyOptimizer.beta_upper_bound to compute it, and verify linearity by halving \(\beta\) and checking that the response halves. A pilot \(\beta=0.01\), as used in line-tension inference, where the cost is not divided by a small amplitude, does not transfer.

Diagnostics are not optional

Every phase returns ‖∇ₓE‖ for every solve, T1 event counts, the connectivity hash of each nudged state, β·Ĉ against the inner energy scale, and the relative state change. A compliance attached to an unconverged equilibrium is not a compliance, and EP's paired cancellation is only valid when the \(\pm\beta\) solves cross the same T1 events — a connectivity mismatch invalidates that outer step and is recorded, not repaired.

ShearState dataclass

ShearState(vertices: Array, edges: Array, faces: Array, grad_norm: float, amplitude: float, t1_events: int, conn_hash: str, seconds: float, energy: float)

One relaxed configuration plus the diagnostics that make it interpretable.

amplitude instance-attribute

amplitude: float

The mode amplitude \(U\) of this state.

ShearRheologyOptimizer dataclass

ShearRheologyOptimizer(energy_theta: Callable[..., Array], mode: ShearMode, a0: float, j_target: float, beta: float, solver_factory: Callable[[], GradientTransformation], width: float, height: float, min_dist_T1: float = 0.005, free_iters: int = 20000, nudged_iters: int = 5000, theta_target: OptimizationTarget = OptimizationTarget.FACE_PARAMETERS, update_t1: bool = True, cap_vertex_step: bool = True, n_sweeps: int = 2, history: list[dict[str, Any]] = list())

Design \(\bm\theta\) so a periodic tissue attains a prescribed mode compliance.

Parameters:

Name Type Description Default
energy_theta Callable[..., Array]

the undriven vertex-model energy, in the six-argument inner-loss signature (vertTable, heTable, faceTable, vert_params, he_params, face_params). width and height are static_argnums throughout the library and must be closed over by the caller.

required
mode ShearMode

the drive mode. Re-reference it from the \(a=0\) relaxed state at each outer epoch and treat it as a constant of the estimator within the epoch.

required
a0 float

drive amplitude, chosen before a design run, small enough for the response to be linear.

required
j_target float

target compliance. Derive it as rheology.target_compliance(G_tgt, mode) with G_tgt taken on the measured forward curve, so that \(\widehat{\mathcal C}\) starts \(O(1)\). Setting it to a bare constant makes \(\beta\widehat{\mathcal C}\) comparable to the whole energy and the "nudge" is then a large perturbation

required
beta float

nudge magnitude. Code convention: solves at \(\pm\beta\), prefactor \(1/(2\beta)\).

required
solver_factory Callable[[], GradientTransformation]

zero-argument callable returning a fresh optax solver for the inner solve.

required
width float

box width.

required
height float

box height.

required
min_dist_T1 float

T1 threshold.

0.005
free_iters int

inner iteration budget of the free solves. See nudged_iters.

20000
nudged_iters int

inner iteration budget of the nudged solves. Measure these, do not assume them. A warm-started driven solve does not converge quickly merely because it travels a short distance: CG's iteration count is set by the spectrum, and the drive excites the softest modes. Measured on the 72-cell production box, from a free state at \(\|\nabla_xE\|\approx2\times10^{-13}\), the driven solve still needs ~5000 iterations for \(10^{-8}\) and ~20000 for \(10^{-13}\).

5000
theta_target OptimizationTarget

which parameter array is designed.

FACE_PARAMETERS
update_t1 bool

T1 during the inner solves. On for production — it is the fluidity mechanism.

True
cap_vertex_step bool

per-step vertex displacement cap. Keep True with a line-search solver and T1 on.

True
n_sweeps int

alternating half-sweep count in the nudged phase. Do not raise past 4: if the alternation is not contracting by then, block-coordinate is inadequate and a joint solver is needed — report rather than paper over it.

2

free_phase

free_phase(vertices, edges, faces, params)

Relax the two conditions independently at \(\beta=0\).

Returns {+1: ShearState, -1: ShearState}. These are the warm-start points for every nudged solve, and the pair from which \(\widehat J\) is read.

nudged_phase

nudged_phase(free, params, b)

Alternating (block-coordinate) nudged solves at nudge level b.

Each half-sweep minimises one condition with the partner's amplitude frozen, which is what makes the joint nudged equilibrium expressible against a one-mesh inner solver. Both the \(+\beta\) and \(-\beta\) calls enter from the same free states, so their residuals are correlated and largely cancel in the difference.

compliance_gradient

compliance_gradient(nudged_plus, nudged_minus, params)

The centered EP estimate, summed over both conditions.

beta_upper_bound

beta_upper_bound(j_measured: float, ratio: float = 0.1) -> float

Largest \(\beta\) at which the nudge is still a perturbation, not a constraint.

The nudge adds a stiffness \(\beta\cdot2/(2a_0J_{\rm tgt})^2\) along the mode; the tissue's own mode stiffness is \(\sum\hat c^2/J\). Linear response needs the first well below the second, so

\[ \beta \le \text{ratio}\cdot\frac{\sum\hat c^2}{J}\cdot\frac{(2a_0J_{\rm tgt})^2}{2} \]

with ratio the tolerated stiffness fraction (0.1 by default). This is a necessary condition, not a sufficient one — confirm linearity empirically by halving \(\beta\) and checking that the state response halves.

compliance_and_cost

compliance_and_cost(states) -> tuple[float, float]

\((\widehat J, \widehat{\mathcal C})\) from a pair of states.

beta_diagnostics

beta_diagnostics(free, nudged_plus) -> dict[str, float]

The two numbers to log at every outer step, which check that the nudge is in the linear regime.

rel_state_change must sit in \([10^{-4},10^{-2}]\) and beta_cost_over_energy below \(10^{-2}\). Outside those the "nudge" is either invisible or is a large perturbation, and in both cases the estimator is not measuring what it claims to.

epoch

epoch(vertices, edges, faces, params, outer_solver, outer_state)

One outer step: free phase, both nudged phases, the centered EP estimate, one optax update.

Returns (new_params, new_outer_state, diagnostics). The mode is not re-referenced here — the caller does that between epochs, so that the reference is a constant of the estimator within the epoch.