Skip to content
Oakfield Operator Calculus Function Reference Site

Field Creation

  • sim_add_field(ctx, shape[, options]) allocates a field and returns a Field handle (userdata) bound to the context. shape is an array of positive integers.
  • Element types: type = "double" (default real) or type = "complex_double" (real/imag pairs). Element size follows automatically.
  • Initialization: fill seeds every element (number or {re, im} for complex). initializer (Lua fn) overrides fill for 1D fields and is called with (index, coord).
  • Spatial metadata: origin and spacing annotate coordinate mapping for initializers and some operators; they do not change storage layout.
OptionTypeDefaultNotes
typestring"double""complex_double" stores interleaved {re, im} pairs.
fillnumber or {re, im}0Applied before initializer; complex fill requires a 2-element table.
initializerfunction(index, coord) -> number or {re, im}noneOnly supported for 1D fields. Called for each element; return type must match type.
originnumber0.0Starting coordinate passed to initializer as coord.
spacingnumber1.0Step size for coordinate progression in initializer.
local ctx = sim.sim_create()
-- Real 2D grid
local density = sim.sim_add_field(ctx, {256, 256}, { type = "double", fill = 0.0 })
-- Complex 1D grid with initializer
local wave = sim.sim_add_field(ctx, {1024}, {
type = "complex_double",
origin = -3.14,
spacing = 0.0062,
initializer = function(i, x)
return { math.cos(x), math.sin(x) }
end
})
  1. Provide a positive shape for each dimension; Lua errors on zero/negative entries.
  2. Choose type up front; it fixes element size (sizeof(double) or 2*sizeof(double)).
  3. Prefer fill for constant seeds and initializer for gradients or waveforms.
  4. Capture the returned Field handle to pass into operators and to query rank/shape/values later.