Skip to content

Energies

vertax.energy

Energy related functions, used for the inner optimization.

They can be user defined functions but they have to respect a strict signature.

For a PbcMesh and PbcBilevelOptimizer, the energy function must have the following signature:

vertTable: Array,
heTable: Array,
faceTable: Array,
vert_params: Array,
he_params: Array,
face_params: Array,

and return an Array (or float).

The names can vary and you can give default parameters. But the number and type of parameters is important. You don't have to use every parameters but they all have to be here. An unused parameters can of course also have the type None.

Same for BoundedMesh and BoundedBilevelOptimizer, but with a slightly different signature:

vertTable: Array,
angTable: Array,
heTable: Array,
faceTable: Array,
selected_verts: Array | None,
selected_hes: Array | None,
selected_faces: Array | None,
vert_params: Array,
he_params: Array,
face_params: Array,

and return an Array (or float).

Hopefully the variable names are self-explanatory.

You can create a function with this signature exactly that uses also locally-accessible external variable if you want.

energy_shape_factor_homo

energy_shape_factor_homo(vertTable: Array, heTable: Array, faceTable: Array, width: float, height: float, face_params: Array) -> Array

E1 energy where the shape factor is uniform (give only one face_params, it will be broadcasted).

energy_shape_factor_hetero

energy_shape_factor_hetero(vertTable: Array, heTable: Array, faceTable: Array, width: float, height: float, selected_faces: Array, face_params: Array) -> Array

E1 energy where the shape factor depends on the cell.

make_energy_shape_factor

make_energy_shape_factor(lambda_area: float | Array = 1.0) -> Callable

Factory for the periodic E1 energy with an explicit area stiffness.

Returns a callable with the signature of energy_shape_factor_hetero, implementing

\[ E_1 = \sum_\alpha \lambda_A\,(A_\alpha - 1)^2 + (P_\alpha - p^0_\alpha)^2 , \]

with face_params holding the per-cell preferred perimeter \(p^0_\alpha\). At lambda_area = 1 it is energy_shape_factor_hetero exactly.

lambda_area is closed over, not stored at module level, and it is never converted to a Python float, so it may be a traced array. That is what makes it trainable: an energy that reads lambda_area = exp(kappa) from an outer-parameter slot can call this factory inside itself, and jax.grad with respect to kappa flows through the area term. The exponential keeps the stiffness positive without a constraint.

make_energy_line_tensions

make_energy_line_tensions(K_areas: float) -> Callable

Factory for the periodic E2 energy with an explicit area stiffness.

Returns the function energy_line_tensions(vertTable, heTable, faceTable, width, height, he_params, face_params), implementing exactly

\[ E_2 = \tfrac12\sum_{he}\Lambda_{he}\,\ell_{he} + \tfrac{K}{2}\sum_\alpha (A_\alpha - A^0_\alpha)^2 , \]

where the leading 1/2 on the tension term cancels the half-edge double count, so he_params stores the physical per-edge tension directly.

K_areas is a required argument: it must be passed explicitly at every call site so that the value used is always visible in the driver/notebook rather than buried in the library.

The returned function takes the box size and not vert_params, so it does not have the 6-argument inner-loss signature of the periodic optimizers (see the module docstring). Wrap it to use it as PbcBilevelOptimizer.loss_function_inner:

E2 = make_energy_line_tensions(K_areas)
optimizer.loss_function_inner = lambda v, h, f, _vp, hp, fp: E2(v, h, f, width, height, hp, fp)

make_energy_line_tensions_bounded

make_energy_line_tensions_bounded(K_areas: float, *, sigmoid_tensions: bool = False) -> Callable

Factory for the bounded E2 energy, in the same convention as the periodic one.

\[ E_2 = \sum_{\text{edges}}\Lambda_e\,\ell_e + \tfrac{K}{2}\sum_\alpha (A_\alpha - A^0_\alpha)^2 . \]

In this convention:

  • K_areas is an explicit argument, so the area-stiffness value used is always visible at the call site, and matches K_areas used by every periodic driver.
  • Target areas are taken per cell from face_params, so heterogeneous target areas are supported.
  • sigmoid_tensions is off by default: he_params are the raw physical tensions, as in the periodic case. When enabled, he_params is instead passed through sigmoid(he_params) + 1, confining tensions to (1, 2); this is an opt-in option because it usefully enforces positivity for inverse-design runs, but it makes he_params mean a different quantity than in the periodic case, so it must be requested explicitly and reported.