Field Examples
Complex Waveform Field with Initializer
Section titled “Complex Waveform Field with Initializer”local ctx = ooc.sim_create()ooc.sim_set_timestep(ctx, 0.01)
local wave = ooc.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})
ooc.log("wave rank=%d first=%0.2f+%0.2fi", wave:rank(), wave:values()[1][1], wave:values()[1][2])2D Real Field Snapshot for Diffusion Experiments
Section titled “2D Real Field Snapshot for Diffusion Experiments”local ctx = ooc.sim_create()local grid = ooc.sim_add_field(ctx, {256, 256}, { fill = 0.0 })
-- Snapshot values (row-major: x fastest, y next)local shape = grid:shape()local width = shape[2]local vals = grid:values()local function idx(x, y) return (y - 1) * width + x endooc.log("center=%0.3f", vals[idx(128, 128)])
-- values() returns a copy; use operators or C hooks to write into fields.Reading 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 = ooc.sim_get_field(ctx, 0)local info = ooc.sim_field_info(ctx, 0)ooc.log("format=%s bytes=%d", info.format, info.byte_size)local snapshot = field:values()
local stats = ooc.sim_field_stats(ctx, field)ooc.log("rms=%0.4f max=%0.4f", stats.rms, stats.max_abs)stats:refresh()
local counts = ooc.sim_field_continuity_counts(ctx, 0)ooc.log("continuity dirty=%d stable=%d", counts.dirty, counts.stable)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.