Simulation

Release · v2.2.0MarkdownLLM docs
이 페이지의 내용

Simulation holds one model: the domain and mesh, boundaries, materials, geometry, sources, ports and observables. You build it with add_* calls, check it with preflight(), and solve it with run() or, for gradients, forward(). This page lists the constructor and those entry points. The mental model behind them is in Core Concepts.

Constructor

Simulation(freq_max, domain, *, boundary="cpml", cpml_layers=16,
cpml_kappa_max=1.0, dx=None, mode="3d",
dz_profile=None, dx_profile=None, dy_profile=None, dt=None,
dt_min_cell=None, precision="float32", solver="yee",
stencil_order=2, nominal_profiles=None,
interface_eps="sampled", snap="strict")
import numpy as np
from rfx import Box, GaussianPulse, Simulation
sim = Simulation(
freq_max=5e9, # highest frequency of interest (Hz)
domain=(0.04, 0.03, 0.03), # x, y, z size (m)
dx=2e-3, # cubic cell size (m)
boundary="cpml",
cpml_layers=6,
)
ArgumentMeaning
freq_maxHighest frequency you care about, in Hz. It sets the default time step, the default dx and the mesh checks in preflight.
domain(Lx, Ly, Lz) in metres. On a graded axis, pass 0 for that axis and the size comes from the profile’s sum.
dxCell size in metres. If omitted, rfx derives it from freq_max.
boundary"cpml" (default), "pec", "upml", or a BoundarySpec for per-face control, including "pmc" and "periodic". See Core Concepts.
cpml_layersAbsorber thickness in cells (default 16). rfx adds the absorber outside domain on each CPML face, so the whole domain is interior. Keep sources and structures a few cells from its edge.
dx_profile, dy_profile, dz_profileCell sizes along an axis, for a graded mesh. See Non-Uniform Mesh.
mode"3d" (default), "2d_tmz" (Ez, Hx, Hy) or "2d_tez" (Hz, Ex, Ey).
precision"float32" (default), "float64" (enable JAX x64 first) or "mixed" (float16 fields). Float64 runs on one device, on a uniform or graded mesh; mixed on a uniform mesh on one device.
pec_occupancy_model"edge_keep" (default) scales the E-edge update; "kottke" converts occupancy to an inverse-permittivity tensor on single-device uniform forward() and topology. Graded forward() (including distributed) refuses "kottke" with an occupancy input. Uniform distributed forward() refuses occupancy overrides; run() has no occupancy input.
solver"yee" (default). "adi" was removed in 2.1 and is refused by name.
stencil_order2 (default) or 4. Order 4 only runs on a uniform, PEC or periodic, non-dispersive model.
dt, dt_min_cellPin the time step on a graded mesh (for mesh-as-design-variable work). Refused on a uniform mesh.

Set the occupancy model when constructing Simulation, before tracing or calling forward(). The removed RFX_PEC_OCC_KOTTKE environment variable raises at construction while set, even to 0; unset it and use the constructor option.

Put PEC faces in a BoundarySpec. Neither is a finite ground plane: for that, draw a sheet with add_thin_conductor().

Building the model

Every add_* method returns the simulation, so calls can be chained.

MethodPurposeReference
add_material(name, *, eps_r, sigma, mu_r, ...)register a named materialGeometry & Materials
add(shape, *, material)fill a shape with a materialGeometry & Materials
add_thin_conductor(shape, ...)a zero-thickness metal sheetGeometry & Materials
add_source, add_polarized_source, add_tfsf_sourcefield excitation, no impedanceSources & Ports
add_port, add_msl_port, add_waveguide_port, add_coaxial_portports for S-parametersSources & Ports
add_lumped_rlcan R/L/C element in one cellSources & Ports
add_probe, add_vector_probe, add_dft_plane_probe, add_flux_monitor, add_ntff_boxobservablesResults & Observables
add_refinement(z_range, *, ratio=4, ...)a finer z slab (subgridding)refused by default; with validation="research", only run() on a uniform mesh accepts it
sim.add_material("slab", eps_r=3.0, sigma=0.01)
sim.add(Box((0.018, 0.0, 0.0), (0.024, 0.03, 0.03)), material="slab")
sim.add_source((0.010, 0.015, 0.015), "ez",
waveform=GaussianPulse(f0=3e9, bandwidth=0.8),
amplitude_kind="current")
sim.add_probe((0.030, 0.015, 0.015), "ez")

Checking before you run

MethodReturnsUse
preflight(*, strict=False, check_ntff=True, check_resolution=True, ...)PreflightReportSetup errors and warnings. run() calls it for you.
preflight_sparameters(*, calculator="run", strict=False, ...)PreflightReportIs this port family valid for the S-parameter calculator you plan to call? See Sources & Ports.
fidelity_report(print_report=True)reportWhere each shape actually landed on the grid, next to what you drew.
freeze_mesh()Grid or NonUniformGridFix the mesh now, so you can read grid indices before placing more geometry.
plan_mesh(*, n_steps=None, available_memory_gb=None, sparameter_calculator=None, ...)MeshPlanA serializable summary of the mesh, memory estimate and checks. See Automation.
mesh_intelligence_report(...)MeshIntelligenceReportGrid, preflight issues and AD memory in one report.
clear_inspection_cache()NoneFree the cached preflight assembly context, realized-geometry record and coax stamp after inspection; the next call that needs them rebuilds them. Solves release them automatically.
report = sim.preflight()
print(report.ok, len(report.errors), len(report.warnings))
report.raise_for_failure() # raises only on errors
sim.fidelity_report()

PreflightReport is a list of coded issues. Read .ok, .errors, .warnings, .infos or .by_code(code). Don’t test it with if report:: that raises TypeError, because an advisory-only report is non-empty but fine. strict=True raises on any finding, warnings included; for an errors-only check call report.raise_for_failure() on a normal report.

Each finding also carries scope: object for affected declarations, place for a grid region, or model for the whole configuration. subjects lists the affected realization labels in declaration order. When available, region contains metre bounds_m and half-open grid cells; it is None when no concrete box is available, including traced meshes. These fields are included in to_dict() and let a UI select objects or highlight cells.

run()

run() is the general entry point. It returns a Result; see Results & Observables.

run(*, n_steps=None, num_periods=20.0, until_decay=None,
decay_check_interval=50, decay_min_steps=100, decay_max_steps=50000,
decay_energy_consecutive=2, ..., checkpoint=False,
compute_s_params=None, s_param_freqs=None, s_param_n_steps=None,
snapshot=None, subpixel_smoothing=False, conformal_pec=None,
conformal_min_weight=0.1, devices=None, exchange_interval=1,
skip_preflight=False, report_every=None, report_label="",
ringdown=None, until_identified=False) -> Result
result = sim.run(n_steps=300)
print(result.time_series.shape, result.settling_witness["status"])

Run length. Give exactly one of:

ArgumentMeaning
n_steps=Nexactly N steps
num_periods=PP periods at freq_max (default 20, used when nothing else is given)
until_decay=1e-3step until the interior field energy falls to that fraction of its largest value after the source ended, up to decay_max_steps

Without radiated_flux_box, until_decay watches the total interior field energy relative to its largest value after the drive ends. This applies to uniform meshes with closed or absorbing boundaries and graded meshes with absorbing boundaries. A lossless closed PEC box generally reaches the step cap; a lossy box can stop when its energy decays. The public graded-mesh runner still refuses closed-boundary until_decay (set n_steps). Use a fixed n_steps in convergence studies.

Other arguments you will use:

ArgumentMeaning
compute_s_params=True, s_param_freqs=S-parameters for lumped and wire ports (add_port) only. Other port families have their own calculator.
snapshot=SnapshotSpec(interval=10, components=("ez",), slice_axis=None, slice_index=None)Record field snapshots. See Visualization and Analysis.
report_every=NPrint progress (steps, rate, ETA) every N steps. Does not change the result.
subpixel_smoothing=TrueSmoothed permittivity at dielectric interfaces.
subpixel_smoothing="kottke_pec"Experimental. Also smooths metal faces, but it is not recommended for metal: on every measured case with a metal face off the grid it was farther from the reference than the staircase. A run that uses it with a PEC volume warns. For curved metal use conformal_pec=True.
conformal_pec=TrueCut-cell updates at curved metal: each cut edge and face uses its PEC-free length and area. On a PEC sphere it is closer to Mie than the staircase, but the error falls only first order with the cell size, and every run with it warns. Judge a curved body by a mesh series.
ringdown=RingdownSpec(...)Complete a wire port’s S-parameters when the record was cut while the structure still rings. See below.
devices=[...]Run across several devices (x-slab decomposition). exchange_interval must stay 1.
skip_preflight=TrueSkip advisories only, for both run() and forward(). Realization refusals still apply; snap='declared' warns on geometry size misses.

A run argument that the selected solver path does not implement raises before the first step, for example snapshots on a graded mesh. The one exception is report_every, which only prints progress: the graded, subgridded and multi-device paths ignore it with a warning.

Warnings after the run. run() warns when a probe recorded no field, when a Gaussian-pulse probe record had not rung down (end of record above −40 dB of peak), and when time_series or lumped/wire s_params contain NaN/Inf or an |S| above 1.1. These checks cover only those arrays. For DFT planes, flux and NTFF data, check np.isfinite(...) yourself. No warning does not mean the run converged.

Ring-down completion

from rfx.ringdown import RingdownSpec, then run(..., ringdown=RingdownSpec()). For a wire-port run (add_port(..., extent=...)) whose record ended too early, rfx fits the slowly decaying poles in the last part of the record and adds the missing tail of each spectrum in closed form. The completed matrix is result.ringdown.s_params, on the same frequencies as result.s_params, and result.ringdown.report holds the fitted poles and the checks. report.ok is decided by comparing the completions from [T/4, T] and [T/2, T]. When a source is still on in [T/4, T], that comparison cannot be formed and a shorter second window is compared instead (W2 in the report), which can read the error low. Neither comparison is an error bound. The source must be off in the fit window. It covers wire ports (one port on a uniform mesh, or a graded mesh) with a fixed n_steps. Other ports, TFSF, until_decay and devices= are refused with the reason. s_params is None when the report says no completion was produced.

forward(): the differentiable path

forward() runs the same physics with only the outputs a loss function needs, so it can sit inside jax.grad, jax.value_and_grad or jax.jit. It returns a ForwardResult with time_series, s_params/freqs (lumped/wire S11 vectors when port_s11_freqs= is set), ntff_data, dft_planes, grid and dt.

forward(*, eps_override=None, sigma_override=None, mu_r_override=None,
design_box=None, design_eps_override=None,
design_sigma_override=None, design_occupancy_override=None,
design_box_holds_ports=False, pec_mask_override=None,
pec_occupancy_override=None, subpixel_smoothing=False,
pinned_sheet_layout=None, n_steps=None, num_periods=20.0,
checkpoint=None, gradient="autodiff", checkpoint_segments=None,
emit_time_series=True, checkpoint_every=None, n_warmup=0,
skip_preflight=False, distributed=False, devices=None,
exchange_interval=1, port_s11_freqs=None,
rlc_values_override=None, ringdown=None) -> ForwardResult
import jax
import jax.numpy as jnp
grid = sim.freeze_mesh()
i0 = grid.position_to_index((0.018, 0.0, 0.0))[0]
i1 = grid.position_to_index((0.024, 0.0, 0.0))[0]
def loss(eps_slab):
eps = jnp.ones(grid.shape).at[i0:i1].set(eps_slab)
out = sim.forward(eps_override=eps, n_steps=300, skip_preflight=True)
return jnp.sum(out.time_series[:, 0] ** 2)
print(jax.grad(loss)(3.0))
InputWhat it differentiates
eps_override, sigma_override, mu_r_overridewhole-grid material arrays, shape grid.shape; they replace the registered materials everywhere
design_box= with design_eps_override= / design_sigma_override= / design_occupancy_override=a box-sized design region; far less reverse-mode memory than a whole-grid override (pass checkpoint=False with it)
pec_occupancy_overriderelaxed metal occupancy in [0, 1]
port_s11_freqs=lumped/wire S11 vectors, on either mesh; over several devices see Run on several devices
rlc_values_override={index: {"R": ..., "L": ..., "C": ...}}values of registered add_lumped_rlc elements

Per-cell versus per-edge conductivity in a design box. A single design_sigma_override array is per cell. rfx averages the four cells around each E edge, and that average also reaches the edge layer on the box’s plus faces. A 3-tuple (sigma_x, sigma_y, sigma_z) is per edge at the box indices and is written as-is. If a per-edge design needs that plus-face layer, declare the box one cell larger on that side.

Keep anything that changes array shapes, the geometry list or the solver path outside the function you differentiate. A combination the differentiable path does not support raises instead of returning a zero gradient. A model with add_refinement is refused by forward(). Guides: Autodiff and Adjoint, Inverse Design, Memory Reduction.

S-parameter calculators

Each port family has its own calculator method on Simulation: compute_msl_s_matrix(), compute_waveguide_s_matrix(), compute_coaxial_line_reflection() and compute_coaxial_two_port(). Lumped and wire ports use run(compute_s_params=True). compute_s_matrix(method=None, **kwargs) picks the right method from the registered ports and forwards your arguments. s_matrix_lane() tells you which one it would pick without running anything. Details: Sources & Ports.

Simulation.auto()

Simulation.auto(freq_range, *, accuracy="standard", **kwargs) -> Simulation
auto_sim = Simulation.auto(freq_range=(1.5e9, 3.5e9), accuracy="draft")

This derives dx, the domain and the CPML thickness from the frequency range alone. Without geometry, dx is about a tenth of the shortest wavelength (rounded down; 7.5 mm for 1.5–3.5 GHz) and the margin a tenth of that wavelength, whatever the accuracy. accuracy ("draft", "standard" or "high") then sets only the CPML depth (at least 6, 8 or 12 layers). Any keyword argument overrides the derived value. It warns on purpose: the domain was sized without your geometry, so check it after you add shapes. To derive a mesh from your geometry instead, use auto_configure or plan_mesh on the Automation page.

Other methods

These are documented in the generated reference: estimate_ad_memory, plan_ad_memory, ad_memory_preflight and explain_ad_memory (reverse-mode memory, see Memory Reduction); export_scene, export_artifact_bundle and artifact_report (saving a model and its results); conductor_mask.

Limits

  • A graded mesh accepts fewer features than a uniform one. Snapshots, coaxial ports, UPML and Floquet ports raise there. See Support Boundaries.
  • add_refinement is refused by default (validation="production"); with validation="research" only run() on a uniform mesh accepts it, with a warning. forward(), optimize() and topology_optimize() refuse a refined model.
  • precision="mixed" raises the CPML reflection floor. Don’t use it for S-parameters or low-reflection work.
  • A completed run is not a converged run. Check result.settling_db and do a convergence study before you quote a number.
Back to top