Code Generator#

neuralmag.common.engine.dA(dim=3, normal=2, region='rhoxy', idx=':', **kwargs)#

Surface integral measure.

Parameters:
  • dim (int, optional) – Dimension of the mesh to integrate over.

  • normal (int, optional) – Principal axis normal to the integration surface.

  • region (str, optional) – Name of the cell function that acts as a region indicator.

  • idx (str, optional) – Index selecting the surface layer along the normal axis.

  • **kwargs – Additional meta information.

Returns:

The integration measure.

Return type:

sympy.Expr

neuralmag.common.engine.dV(dim=3, region='rho', **kwargs)#

Volume integral measure.

Parameters:
  • dim (int, optional) – Dimension of the mesh to integrate over.

  • region (str, optional) – Name of the cell function that acts as a region indicator.

  • **kwargs – Additional meta information.

Returns:

The integration measure.

Return type:

sympy.Expr

neuralmag.common.engine.dX(**kwargs)#

Generic integration measure.

Parameters:

**kwargs – Arbitrary meta information.

Returns:

The integration measure.

Return type:

sympy.Expr

neuralmag.common.engine.Variable(name, spaces, shape=())#

Symbolic representation of a field given as SymPy expression.

Parameters:
  • name (str) – The name of the field.

  • spaces (str) – The function spaces of the field in the principal coordinate directions given as string with ‘c’ representing a cell-based discretization and ‘n’ representing a node-based discretization.

  • shape (tuple, optional) – The shape (dimension) of the field, e.g. () for a scalar field and (3,) for a vector field.

Returns:

The variable.

Return type:

sympy.Expr

neuralmag.common.engine.extract_kernel(form_fn, dim, options)#

Extract the pointwise integrand (kernel) of a weak form.

The magnetization m is built as a nodal quadrature field and passed to form_fn(m, dim, options); every other fully or partially node-discretized field built via Variable() (a hand-written test v, a coupling field, a projection source) is also interpolated to the quadrature point. The measure is stripped and each field’s value and gradient are rewritten into the independent qv_sym() / qg_sym() symbols, leaving a small pointwise expression. No differentiation happens here – form_fn may be an energy density or a hand-written linear form.

Returns:

(kernel, meta, args) where kernel is the integrand without the measure, meta maps every interpolated field name to (spaces, shape), and args is the measure metadata (dims).

Return type:

tuple

neuralmag.common.engine.variation(kernel, by, meta, dim)#

Variation (Gâteaux derivative) of a kernel with respect to a field.

The variation is taken with respect to the field named by: the coefficients of the test value and test gradient in the resulting linear form. For an energy density this turns the energy into its effective-field weak form; for a form already linear in by it simply extracts that form’s coefficients.

The symbolic sp.diff here is the explicit place where “vary the energy” happens; a future backend could compute the same Kv / Kg by runtime autodiff of the kernel without changing callers.

Returns:

(Kv, Kg, test) where Kv[l] = d kernel / d value_l, Kg[l][d] = d kernel / d grad_{l,d} and test = (spaces, shape) of the by field.

Return type:

tuple