Field Examples
Complex Waveform Field with Initializer
Section titled “Complex Waveform Field with Initializer”local ctx = sim.sim_create()sim.sim_set_timestep(ctx, 0.01)
local wave = sim.sim_add_field(ctx, {1024}, { type = "complex_double", origin = -math.pi, spacing = 2 * math.pi / 1024, initializer = function(i, x) return { math.cos(3 * x), math.sin(3 * x) } end})
sim.log("wave rank=%d first=%0.2f+%0.2fi", wave:rank(), wave:values()[1][1], wave:values()[1][2])2D Real Field for Diffusion Experiments
Section titled “2D Real Field for Diffusion Experiments”local ctx = sim.sim_create()local grid = sim.sim_add_field(ctx, {256, 256}, { fill = 0.0 })
-- Drop a Gaussian bump into the center (Lua-side)local vals = grid:values()local function idx(x, y) return (y - 1) * 256 + x endfor y = 1, 256 do for x = 1, 256 do local dx = (x - 128) / 32 local dy = (y - 128) / 32 vals[idx(x, y)] = math.exp(-(dx * dx + dy * dy)) endendReading Parameters Mid-Run
Section titled “Reading Parameters Mid-Run”Add fields and operators (sim_add_field, sim_add_stimulus_operator, etc.), step the context, then inspect fields:
local field = sim.sim_get_field(ctx, 0)local info = sim.sim_field_info(ctx, 0)sim.log("format=%s bytes=%d", info.format, info.byte_size)local snapshot = field:values()After grabbing snapshots, feed them into Lua visualizations or write them to disk; the Field handle remains valid as long as the context is alive.