wifi-densepose/v2/crates/nvsim
ruv a6ac08c662 feat(nvsim): source.rs Biot–Savart synthesis [nvsim:pass2]
Pass 2 of the implementation plan. Adds magnetic-field synthesis at
arbitrary sensor locations, all in f64 for near-field stability per
plan §7-1.

Public API (re-exported from lib.rs):

- dipole_field(&DipoleSource, sensor_pos) -> ([f64; 3], near_field_flag)
  Closed-form analytic dipole: B = (μ₀ / 4π r³)[3(m·r̂)r̂ − m]
  (Jackson 3e §5.6).
- current_loop_field(&CurrentLoop, sensor_pos) -> (Vec3, flag)
  Numerical Biot–Savart over n_segments straight chords (default 64);
  flag fires if any chord midpoint < R_MIN_M (1 mm) of sensor.
- ferrous_field(&FerrousObject, ambient_b, sensor_pos) -> (Vec3, flag)
  Linear induced moment m = χ·V·H_ambient (Cullity & Graham 2e §2),
  re-radiates as a dipole.
- scene_field_at(&Scene, sensor_pos) -> (Vec3, flag) — aggregate.
- scene_field_at_sensors(&Scene) -> Vec<(Vec3, flag)> — for every sensor.
- R_MIN_M = 1 mm — near-field clamp constant.

Pass 2 acceptance per plan §3 — n=8 RMS gate ≤ 0.5%. Test
`dipole_n8_directions_within_half_percent_rms` independently
recomputes the formula in-test rather than calling the implementation
twice, so the gate guards against an implementation that
accidentally agrees with a buggy reference.

7 new tests:
- on-axis dipole matches B_z = μ₀ m / (2π z³)
- equatorial dipole matches B_z = -μ₀ m / (4π r³)
- n=8 directions RMS ≤ 0.5% — Pass 2 acceptance gate
- on-axis current loop matches μ₀ I a² / [2(a²+z²)^(3/2)]
- near-field r < 1 mm clamps to (0, flag=true)
- zero-ambient ferrous object emits zero field
- two opposite dipoles aggregate to zero at colocated sensor

Validated:
- cargo test -p nvsim → 19 passed (was 12; +7).
- cargo test --workspace --no-default-features → 1,594 passed,
  0 failed, 8 ignored (was 1,587; +7).
- ESP32-S3 on COM7 streaming live CSI (cb #8900).

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-04-26 16:11:49 -04:00
..
src feat(nvsim): source.rs Biot–Savart synthesis [nvsim:pass2] 2026-04-26 16:11:49 -04:00
Cargo.toml feat(nvsim): scaffold + scene + frame [nvsim:pass1] 2026-04-26 15:57:58 -04:00
README.md feat(nvsim): scaffold + scene + frame [nvsim:pass1] 2026-04-26 15:57:58 -04:00

README.md

nvsim

Deterministic Rust simulator for NV-diamond ensemble magnetometer pipelines.

nvsim models a forward-only magnetic sensing path:

scene
  → magnetic source synthesis
  → material attenuation
  → NV-ensemble response
  → digitisation
  → binary magnetic feature frames
  → deterministic SHA-256 witness

It is designed for ferrous-anomaly modeling, eddy-current sanity checks, synthetic magnetic traces, sensor education, and regression testing.

It is not a hardware-control stack, microscope simulator, full Hamiltonian solver, or claim of fT-level sensitivity. This crate does not control lasers, microwave sources, ADC hardware, or real NV sensors.

Deterministic in the strong sense: a simulator with explicit physics approximations, conjectural propagation defaults that are documented as such, a linear NV-ensemble readout proxy validated by Barry et al. Rev. Mod. Phys. 92, 015004 (2020) §III.A, and no hidden mocks.

Quick start

use nvsim::scene::{Scene, DipoleSource};
use nvsim::frame::{MagFrame, MAG_FRAME_MAGIC};

let mut scene = Scene::new();
scene.add_dipole(DipoleSource::new([0.0, 0.0, 0.5], [0.0, 0.0, 1e-6]));
scene.add_sensor([0.0, 0.0, 0.0]);

// Pass 2+ adds source synthesis, propagation, sensor, digitiser, pipeline.

Acceptance commitments (per implementation plan §5)

  • Pipeline throughput: ≥ 1 kHz simulated samples per second of wall-clock on a Cortex-A53-class CPU.
  • Determinism: same (scene, seed) produces byte-identical proof-bundle output across runs and machines.
  • Noise floor reproduction: simulator with shot-noise OFF reproduces the analytical BiotSavart result to ≤ 0.1% RMS.
  • Lockin SNR floor: 1 nT @ 1 kHz vs 100 pT/√Hz floor → SNR ≥ 10 in 1 s.

Pass 1 (this build) ships only the scaffold + scene types + binary frame shape; the four acceptance numbers come online over Passes 26 per the plan.

Physics primary sources

  • Jackson, Classical Electrodynamics 3e (1999), §5.45.8 — BiotSavart, dipole field.
  • Doherty et al., Phys. Rep. 528, 1 (2013) — NV ground-state Hamiltonian, ODMR transition.
  • Barry et al., Rev. Mod. Phys. 92, 015004 (2020) — NV-ensemble sensitivity, Lorentzian lineshape.
  • Wolf et al., Phys. Rev. X 5, 041001 (2015) — bulk-diamond pT/√Hz reference floor.
  • Ortner & Bandeira, SoftwareX 11, 100466 (2020) — Magpylib reference implementation.

See docs/research/quantum-sensing/14-nv-diamond-sensor-simulator.md for context and 15-nvsim-implementation-plan.md for the build spec.

Optional integrations

nvsim is a standalone leaf crate. RuView ecosystem integrations (wifi-densepose-core frame alignment, ruvector-core trace compression, etc.) land behind feature flags in follow-up passes once the core simulator ships. None are required to use this crate.

License

MIT OR Apache-2.0 (matches workspace default).