Differentiate a capacitance#
Get the derivative of a capacitance with respect to the permittivity of every
element, without writing an adjoint. jax.grad of any objective built from
an electrostatic result reaches the per-element values of every
gfem.MaterialField in the model. This page also holds what the other
gradient recipes share: batches of designs, the memory kept for gradients,
jax.lax.stop_gradient, and what to do when a gradient raises.
A mutual capacitance against a permittivity field#
The mesh stack.msh has an oxide and a nitride layer, terminals on its top
face and on the oxide-nitride interface, and the ground on its bottom face.
import jax
import jax.numpy as jnp
import gfem
mesh = gfem.Mesh.from_gmsh("stack.msh")
model = gfem.Model(
mesh,
materials={"oxide": gfem.Material(eps_r=3.9), "nitride": gfem.Material(eps_r=7.5)},
)
problem = gfem.Electrostatic(
terminals={"top": "top", "mid": "oxide__nitride"}, ground="bottom"
)
def coupling(eps):
"""Mutual capacitance top-mid in fF, for a permittivity per oxide element."""
m = model.with_material("oxide", gfem.MaterialField(eps_r=eps))
return gfem.solve(m, problem).mutual[0, 1] * 1e15
n = len(mesh.elements("oxide"))
value, grad = jax.value_and_grad(coupling)(jnp.full(n, 3.9)) # grad: (n,), fF
grad[i]is the change of the mutual capacitance, in fF, per unit change of the relative permittivity of oxide elementi.result.mutualandresult.inversedifferentiate likeresult.capacitance, and so doesresult.capacitance_per_length, the matrix per metre of a cross-section read withgfem.Mesh.from_gmsh_cross_section().The region energies
result.energydifferentiate too, with respect to the permittivity and the node coordinates. A gradient through them costs one adjoint solve for all terminals together, on both linear solvers.The gradient costs no extra solve. Each terminal’s potential is its own adjoint, so the backward pass (the part of
jax.gradthat runs after the solve) is one element-wise integral.
Differentiate a whole region’s permittivity#
For the sensitivity to one constant permittivity of a whole region, assign
gfem.MaterialField.uniform("oxide", gfem.Material(eps_r=3.9)). Its values
are scalars, so the gradient has one value per property. A
gfem.Material’s values are fixed constants: differentiating with
respect to one raises gfem.NonDifferentiableError, whose message gives this
remedy.
Several designs at once#
jax.jit and jax.vmap work on the same functions. Under jax.vmap the
solves run one after the other, so jax.vmap(jax.grad(...)) over a batch of
designs (an eroded, nominal and dilated triple, say) gives what a Python loop
over them gives:
designs = jnp.stack([eps_eroded, eps_nominal, eps_dilated]) # (3, n)
grads = jax.vmap(jax.grad(coupling))(designs) # (3, n)
robust = jax.grad(lambda ds: jnp.max(jax.vmap(coupling)(ds)))(designs)
Several gradients from one solve#
jax.jacrev of the whole capacitance matrix, and the function jax.vjp
returns called several times, give the same numbers as separate jax.grad
calls. Between the solve and its gradients gfem keeps what the gradients need
(for a capacitance, the terminal potentials; for an energy or potential
gradient also the prepared operator, so the backward does not prepare again):
It keeps up to
SolverOptions(retain_memory=...)bytes, half of the memory the process may use by default, and drops the oldest entries first.A dropped entry is recomputed from the design when its gradient runs. This costs time, never accuracy.
Results do not depend on it. For a driven solve the same budget also holds what later designs reuse: see “In an optimization loop” in Differentiate S-parameters and driven fields.
retain_memory=0keeps nothing and always recomputes. Use it when memory, not time, is short.gfem.release_memorydrops everything kept so far, at once.
What every other input gives#
Every input of the model has one of three outcomes:
a gradient, for what the problem reads and gfem differentiates (for electrostatics:
eps_r, the real part when it is complex, and the mesh node coordinates, see Shape gradients);an exact zero, for what the problem never reads. Electrostatics does not read
conductivity,tan_delta,mu_r, the thermal properties or the elements of excluded volumes, so oneMaterialFieldcan serve an electrostatic and a driven objective together;gfem.NonDifferentiableError, raised whenjax.gradis called, before any solve, for what the problem reads but gfem cannot differentiate yet. The message names the input or output and the remedy.
gfem never returns a silent zero for an input the physics reads.
Use a value without its derivative#
jax.lax.stop_gradient is the one way to use a value without its derivative.
For example, jax.grad over a whole model also differentiates the mesh
nodes, a shape gradient you may not need (it costs one more pass over the
elements). Freeze them first:
def objective(model):
frozen = model.replace(mesh=jax.lax.stop_gradient(model.mesh))
return gfem.solve(frozen, problem).capacitance[1, 1]
grads = jax.grad(objective)(model) # grads.materials[...] holds the field gradients
The same works on an output that has no gradient: wrap it in
stop_gradient where the objective uses it.
When a gradient raises#
Some errors can only be found while jax.grad computes the gradient, after
the solve has succeeded. Examples are a lumped port’s node moved by a shape update
(gfem.PortGeometryError) and a degenerate mode cluster your objective
does not treat symmetrically (gfem.DegenerateModeError). JAX does not
pass gfem’s error class through at that stage. You get a
jax.errors.JaxRuntimeError or, from a jitted function that has already run
successfully, a ValueError. The message is gfem’s own: it names the error
and how to fix it. Catch those two classes and read the message:
try:
grad = jax.grad(coupling)(eps)
except (jax.errors.JaxRuntimeError, ValueError) as error:
print(error) # names the gfem error and its remedy
Check your results#
Compare the gradient with a central difference of your objective along a random direction
v:(f(p + h v) - f(p - h v)) / (2 h)againstgrad @ v. Their difference shrinks ashshrinks, until the solver tolerance limits it.
How gfem itself is checked: How gfem is checked.
Limits#
Only reverse mode is supported:
jax.jvpandjax.jacfwdof a solve raise JAX’s ownTypeError. Usejax.grad,jax.vjporjax.jacrev.
Next#
gfem.Electrostatic, gfem.MaterialField,
gfem.SolverOptions; Differentiate S-parameters and driven fields,
Differentiate eigenfrequencies, Q and participation ratios, Shape gradients; Capabilities.