Functions, arrays, and fields
A mathematical function describes how a value depends on position, but a
numerical operation also needs to know where its samples lie and how the
function behaves at the boundary. A NumPy array supplies the sampled values;
a FLODI Field keeps those values together with their grid and any known
outer-face values. By retaining that information, the field allows a later
integral or derivative to use the geometry and boundary condition that give
the samples their physical meaning.
Sample a callable
A callable passed to grid.sample receives a coordinate object. Write it
using NumPy operations so that it works on coordinate arrays:
import numpy as np
from flodi import RadialGrid
grid = RadialGrid.uniform(n_r=120, r_max=5.0)
f = grid.sample(lambda c: np.exp(-c.r**2))
print(f.values.shape)
print(f.boundary)
Here c is an ordinary Python parameter whose coordinate attributes are
provided by the grid. The expression c.r selects the radial coordinate,
allowing the same callable to be evaluated at both the centres and the
outer face. If your function depends on another coordinate, such as
c.z, choose a geometry that supplies it; Choose and inspect a grid lists the
available names.
Sampling a known function supplies both centre and face values.
Bind a function to named coordinates
GridFunction is convenient when an existing function takes coordinate
arrays as named arguments:
from flodi import GridFunction
profile = GridFunction(lambda r: np.exp(-r**2), coordinates=("r",))
same_f = grid.sample(profile)
np.testing.assert_allclose(same_f.values, f.values)
The tuple tells GridFunction which grid coordinates to pass to the
function, in its argument order. This mapping is useful when reusing the
same mathematical profile on different geometries, but it still needs a
physical interpretation: on a two-centre grid, for example, r measures
from the midpoint whereas r_a measures from one focus. Choose the
coordinate that appears in the equation you intend to sample.
Sample an array or a constant
An array must match grid.shape and supplies centre values only. Add the
face values explicitly if the next operation needs them:
centre_values = np.exp(-grid.r**2)
from_array = grid.sample(
centre_values, outer_boundary=lambda c: np.exp(-c.r**2)
)
zero_everywhere = grid.sample(0.0)
A scalar has no such missing spatial information: sampling it defines the same constant at both centres and faces. With an array, the face must be specified separately because its last element still represents a centre inside the domain. The Boundary conditions belong to the operation guide develops this distinction and its consequences for differentiation.
Arithmetic keeps the numerical meaning
g = 2 * f + f**2
density_like = abs(f)**2
conjugate = f.conj()
product_integral = grid.integrate(f.conj(), g)
These expressions act pointwise, so the algebra follows the corresponding operations on functions. Boundary information follows the same rule when all the required operands supply it; if one boundary is unknown, the result’s boundary will generally be unknown too. The fields must also belong to the same grid object, since values can only be combined directly when they refer to the same physical samples. When comparing different grids, use physical integrals or an explicitly chosen interpolation rather than relying on equal array shapes.
The underlying NumPy array remains available as field.values for
inspection and plotting. Editing it changes the centre samples without
automatically updating the stored boundary, so resampling with consistent
face data is often clearer when changing an entire function. Once you
return to a FLODI operation, pass the field object so that the grid and
boundary information remain attached to its values.