Skip to content

Regions and materials

Per-region (sub-domain) integration

A weak term integrates over the region of the coordinates it is written on — exactly the rule that already routes boundary terms. Bind the trial/test to a sub-region's coordinates and the term integrates over that sub-domain's cells only. Name a region with domain.tag(name, predicate) (or use a multi-part mesh's geometry parts) and ask for its coordinates with domain.variable(name, split=True):

d.tag("core", lambda x, y: (x - 0.5)**2 + (y - 0.5)**2 < 0.2**2)   # an interior sub-region
xc, yc, _ = d.variable("core", split=True)
uc, vc = u.bind(x=xc, y=yc), phi.bind(x=xc, y=yc)

fem = jno.fem([
    ui.x * vi.x + ui.y * vi.y,            # ∫_Ω   ∇u·∇v        (whole domain)
    9.0 * (uc.x * vc.x + uc.y * vc.y),    # ∫_core 9 ∇u·∇v     (k = 1 outside, 10 inside `core`)
    q * vc,                               # ∫_core q·v          (a localized source)
    u(xb, yb) - 0.0,
])

Multi-material conduction is then one term per material; a data-fit / QoI confined to a region is (uc - u_data) * vc. A cell belongs to a region iff its centroid does (classified once at assembly — exact when the mesh respects the region boundaries, e.g. gmsh meshing each part separately; O(h) at an arbitrary-predicate interface).

Region integration is a scalar mask on the integrand, so it composes with everything: constant / jno.fn / .freeze() / trainable coefficients, and the steady-linear, nonlinear, transient, coupled, and 3-D forms. A jno.np.parameter that multiplies a sub-region term is recovered per sub-domain through crux.

Every field still spans the whole mesh

fem_symbols has no region argument, so a field restricted in its terms still carries DOFs everywhere. If every term touching a field is region-restricted, that field's DOFs on the rest of the mesh appear in no equation at all and the system is structurally singular — the case that shows up the moment you write different physics per region (Navier–Stokes on fluid, elasticity on solid).

jno.fem refuses that at build time, naming the field, the count and the region:

jno.fem: these DOFs appear in no term and carry no prescribed value, so the system is
structurally singular:
  field 'u' (block 0): 87 DOFs, every term reaching it is restricted to 'lower'

The fix is to give the field a term over the region it is missing from; where it carries no physics there, a cheap 1e-8 * u * phi on that region is enough.

A Dirichlet pin on the region is not a reliable substitute: an essential condition resolves its nodes through domain.tag_node_mask, which for a volume region is a proximity test against sampled points rather than a containment test, and can miss interior nodes (measured: it reached 32 of the 33 ungoverned DOFs on a two-block domain). Reach itself is measured by cell centroid — the assembler's own resolution — so the check cannot disagree with what was assembled.

Scope: this finds a DOF that appears in no term. It is not a rank test, and does not detect a missing pressure gauge or an unrestrained rigid-body mode.

domain.attach — many materials as one equation

For many regions, writing one term per region is noisy. Declare the property on each region with attach, then read it back as d.<name>: a single coefficient whose value is chosen, per cell, by the region the cell's centroid lies in — so the whole multi-material weak form is one equation over the whole interior:

xi, yi, _ = d.variable("interior", split=True)        # whole domain, bound once
ui, vi = u.bind(x=xi, y=yi), phi.bind(x=xi, y=yi)

d.attach("steel", k=16.0).attach("air", k=0.026).attach("core", k=25.0)
d.attach("core", Q=heat_source).attach(Q=0.0)         # a bare attach = the default for the rest
fem = jno.fem([d.k * (ui.x*vi.x + ui.y*vi.y) - d.Q * vi, u(xb, yb) - 0.0])

Omit the target for a default. attach(Q=0.0) gives the value for every region that names none of its own — the source that is zero outside one body. An explicit target always wins over a bare one, whichever order they are written in: without that rule the two orderings would differ silently. A default applies to volume regions only; boundary tags are not a partition of the boundary, so a surface default has nothing to mean and is refused.

A value can be a scalar, a jno.fn field, or a trainable jno.np.parameter, so the same primitive expresses conductivity, a source, a density, a reaction rate, an elastic modulus — and trainable per-region values compose for free, calibrated through crux. Each target must be a geometry part (from_regions), a domain.tag, or a mesh-file region. It desugars to sum_r RegionMask(r)·value_r, inheriting the centroid classification.

Changing a value after jno.fem is built. d.<name> reads plain values (numbers, arrays) from the domain when the terms are assembled, so a later attach reaches an existing fem: its next solve() re-assembles and says so in the log (jno.fem: re-assembled, since domain.attach changed 'q' ...). The mesh, and anything built on it such as an enclosure's view factors, is not rebuilt.

fem = jno.fem([d.k * (ui.x*vi.x + ui.y*vi.y) - d.Q * vi, u(xb, yb) - 0.0])
T1 = fem.solve()
d.attach("core", Q=2.0e6)                             # a plain number: watched
T2 = fem.solve()                                      # re-assembled with the new Q

A value that is itself an expression (a function of position, a jno.fn, a trainable parameter) is captured when the term is written, so re-attaching one raises at the next solve; build a new jno.fem after such a change. d.attached(name) returns the values as a plain mapping at the moment it is called and is not watched.

Not yet wired: second-order-in-time (u_tt) sub-region terms (fail loud). 3-D sub-regions are defined by a predicate where(x, y, z).

Material laws — d.k(T)

A property can depend on a field: conductivity on temperature, reluctivity on the flux density, permeability on the pressure head. Attach the law with the material, as a function, and evaluate it where the weak form uses it, the way the equation is written, ∫ k(T) ∇T·∇v:

d = (felt.name("insulation").attach(k=lambda T: 0.05 + 2e-4 * T)   # a law of the field T
     + crystal.name("crystal").attach(k=220.0)                      # a number, as before
     ).domain()
T, v = d.fem_symbols(names=("T", "v"))
Ti, vi = T.bind(x=x, y=y), v.bind(x=x, y=y)
heat = d.k(T) * (Ti.x * vi.x + Ti.y * vi.y) - d.q * vi              # numbers pass through, laws are evaluated

Write the law as the equation it is, e.g. a fibre insulation whose conductivity grows with the radiation between its fibres, k(T) = k0 + k1 T + k3 T^3:

k_felt = lambda T: k0 + k1 * T + k3 * T**3

Any callable that is not a jNO expression is a law: a lambda, a function, or an object with __call__ (a table of datasheet points interpolated with jno.np operations, where no formula is known).

Trainable coefficients. A coefficient of the law may be a jno.np.parameter; fem.solve() then returns a trace node and jno.core fits the coefficient through the nonlinear solve (implicit differentiation, never an unrolled Newton loop):

theta = jno.np.parameter((2,), name="theta")             # .initialize(...), .optimizer(optax.lbfgs())
d.attach("insulation", k=lambda T: theta[0] * (1 + theta[1] * T))
T_h = jno.fem([d.k(T) * (Ti.x * vi.x + Ti.y * vi.y), ...]).solve(nonlinear=jno.solve.newton(direct=True))
jno.core([(T_h - T_measured).mse]).solve(30)

Only what the data can see is recovered: with the temperature fixed on both ends of a slab, a uniform scale of k leaves T unchanged, so theta[0] stays where it started while theta[1] is found (measured: b = 2.0000 from a start of 0.5, tests/test_fem_material_laws.py). The argument is any expression -- the field, a bound view of it, or a derived quantity (d.nu(Bx**2 + By**2)). Because the law contains the unknown, the form is nonlinear and Newton's tangent carries dk/dT exactly; nothing is passed to fem.solve().

The rules that keep a law apart from a function of position (.attach(k=lambda r, z: ...), called with the coordinates when d.k is read, as before):

  • A plain function whose parameters are exactly the coordinates (one per dimension) is a function of position, unless one of its parameters is named after a declared field (fem_symbols(names=...)), which makes it a law. So lambda T, c: ... on a 2-D domain is a law once T is a field, not silently a function of (x, y).
  • A property with a law must be called: bare d.k returns a placeholder that raises in any arithmetic, naming the regions and the fix. d.k(T) on a property of numbers only is the same coefficient as d.k, so one spelling serves either way.
  • d.k(x, y) with coordinate variables keeps its meaning (binding to those coordinates).
  • A law is captured as written: re-attaching it after jno.fem is built refuses at the next solve (the re-attached number path still re-assembles).

Pinned in tests/test_fem_material_laws.py (a law and a number in two layers against the exact Kirchhoff profile, a datasheet object, and each guard).

domain.mask — a field's values on one region

d.mask(name) is a boolean mask over the mesh nodes (d.mesh.points): every node of the region's cells, read from the mesh's own cell labels. Use it to evaluate a solution on one part:

T = fem.solve()                       # a P1 scalar field: one value per mesh node
core = d.mask("core")
T[core].mean(), T[core].max()

The mask has one entry per mesh node, so it lines up with a solution that has one value per mesh node. For other fields, index the part that does:

  • a vector field (value_shape=(n,)): the solution is interleaved per node, T.reshape(-1, n)[core];
  • P2 and above on a straight-sided mesh: the solution lists the mesh nodes first and then the added mid-side nodes, so T[: len(core)][core] gives the values at the region's vertices. For every DOF of the region, select on fem.points (the coordinates the DOFs live on) instead.

A node on an interface belongs to both regions, and on a curved mesh the mid-side nodes are included. It works for the parts of a jno.shape plan and for the named volumes of a loaded mesh, and raises KeyError listing the mesh's regions for any other name. Boundary tags go through domain.tag_node_mask instead.


Mapped regions — .attach(map=f)

A region of the mesh can stand for another region of space. Attach the map from its mesh coordinates x′ to the physical coordinates x = f(x′), written with jno.np, and every weak term integrated there is evaluated by the change of variables (J = ∂f/∂x′ by automatic differentiation of f): a coordinate becomes f(x′), a derivative becomes J⁻ᵀ∇′, the measure picks up det J. The equations are written once for the whole domain.

The unbounded outside of a body, closed by a finite shell whose outer edge is infinity:

def to_infinity(r, z):                      # rho = R^2 / (2R - rho'): R <= rho' <= 2R covers R <= rho < infinity
    rho = jno.np.sqrt(r**2 + z**2)
    q = R**2 / ((2 * R - rho) * rho)
    return r * q, z * q

d = (... + (half(2 * R) - half(R)).name("infinity").attach(map=to_infinity)).sized(h).domain()
fem = jno.fem([nu * curl(A) @ curl(W) * dV - J * W * dV, A(axis) - 0.0, A(arc) - 0.0])   # arc = infinity

Every decaying mode rho^-(n+1) becomes a polynomial of the mesh coordinate, so a coarse shell resolves it. A complex map is an absorbing layer for waves (rho~ = rho′ + (i/k) ∫σ, with Bermúdez' σ = 1/(L − s) needing no tuning); the form then becomes complex and goes through the real-equivalent complex route. Maps along one axis close half-spaces (with corner regions mapping both axes).

Checked: f must be the identity where the region meets an unmapped one (otherwise the field would jump; raises).

Scope (first version): first derivatives (a second derivative in a mapped region raises); volume terms (Dirichlet values are not mapped; surface terms on a mapped region's own faces are not supported); fields whose components transform as numbers -- not edge or face elements, which need a Piola map. A 1/rho field in an axisymmetric shell wants the shell graded towards its outer edge (measured: error ~h^0.8 on a uniform shell, ~h^2 graded).

Pinned in tests/test_fem_mapped_regions.py: a current loop (elliptic-integral field), a sphere at fixed temperature in an unbounded medium, a line source under a grounded surface (axis maps and corners), a 2-D net source, an outgoing wave through a complex map (Hankel), and the interface guard.