Skip to main content

Forward Library (FWDLIB)

The Forward library computes MEG and EEG forward solutions — the mapping from source currents in the brain to sensor measurements. It supports BEM (Boundary Element Method) models with constant or linear collocation, multi-layer concentric-sphere EEG head models, and arbitrary coil/electrode configurations. The library also provides sensor-to-surface field mapping for topographic interpolation. All classes reside in the FWDLIB namespace.

Architecture

Class Inventory

Forward Solution

ClassDescriptionMNE-PythonMNE-C
FwdStatic wrapper for reading forward solutions from FIFF filesmne.read_forward_solution()mne_read_forward_solution
ComputeFwdMain worker that orchestrates forward solution computation: initialises source space, BEM/sphere model, coils, and dispatches computation to threadsmne.make_forward_solution()Forward solver kernel
ComputeFwdSettingsContainer for all forward computation parameters: source space path, BEM path, EEG model, coordinate frameConfiguration struct

BEM (Boundary Element Method)

ClassDescriptionMNE-PythonMNE-C
FwdBemModelBEM model holding tissue surfaces (scalp, outer skull, inner skull), conductivity parameters, and the BEM solution matrixmne.make_bem_model() / mne.read_bem_solution()fwdBemModel
FwdBemSolutionPrecomputed BEM solution matrix (n_coil × n_potential_points) mapping infinite-medium potentials to sensor readingsInternalfwdBemSolution

Key BEM Methods:

MethodDescription
fwd_bem_constant_collocation_solution()Compute BEM solution using constant-basis collocation on triangle centroids
fwd_bem_linear_collocation_solution()Compute BEM solution using linear-basis collocation on triangle vertices
fwd_bem_inf_field() / fwd_bem_inf_pot()Compute the magnetic field / electric potential in an infinite homogeneous medium
fwd_bem_solid_angles()Compute solid-angle matrix for the BEM geometry
fwd_bem_pot_calc() / fwd_bem_lin_pot_calc()Compute potentials at electrodes given BEM solution (constant / linear collocation)
fwd_bem_pot_grad_calc()Compute potentials and spatial gradients at electrodes
make_guesses()Generate dipole guess-point grid within the inner-skull boundary

Coils & Sensors

ClassDescriptionMNE-PythonMNE-C
FwdCoilSingle MEG coil or EEG electrode: integration points, weights, orientation, coordinate frameInternal (via pick_info)fwdCoil
FwdCoilSetCollection of FwdCoil objects representing the full sensor array; supports creation from channel info and transformation between coordinate framesfwd['info']fwdCoilSet
FwdCompDataCTF compensation coils and field computation routines for gradiometer-to-magnetometer compensationmne.io.CompensationGradefwdCompData

Key Coil Methods:

MethodDescription
create_meg_coils()Create MEG coil set from measurement info and coordinate transform
create_eeg_els()Create EEG electrode set from measurement info
read_coil_defs()Read coil definitions from coil_def.dat
is_axial_coil() / is_planar_coil() / is_magnetometer_coil()Query coil geometry type
dup_coil_set(transform)Duplicate coil set and apply a coordinate transform

EEG Sphere Model

ClassDescriptionMNE-PythonMNE-C
FwdEegSphereLayerSingle concentric sphere layer with radius (relative to outermost) and conductivityLayer in eeg_sphere_modelfwdEegSphereLayer
FwdEegSphereModelMulti-layer spherical head model for EEG forward computation using Legendre polynomial series expansionmne.make_sphere_model()fwdEegSphereModel
FwdEegSphereModelSetCollection of EEG sphere models (e.g., default four-shell plus user-defined)fwdEegSphereModelSet

Key Sphere Methods:

MethodDescription
setup_eeg_sphere_model()Initialise Berg-Scherg equivalent parameters via optimisation
eeg_multi_sphere_pot()Compute scalp potential for a current dipole using Legendre series
next_legen()Legendre polynomial recursion helper (P, P') for series evaluation
fwd_eeg_get_multi_sphere_model_coeff()Compute series expansion coefficients for the multi-layer geometry

Field Mapping / Interpolation

ClassDescriptionMNE-PythonMNE-C
FwdFieldMapSphere-model-based sensor-to-surface field mapping using Legendre polynomials and SVD pseudo-inverse; supports MEG and EEG with optional SSP projectionmne.forward._field_interpolation._make_surface_mapping()— (ported from Python)

Key Methods:

MethodDescription
computeMegMapping()Compute dense sensor → surface mapping matrix for MEG
computeEegMapping()Compute dense sensor → surface mapping matrix for EEG (with average reference option)

Parallelisation

ClassDescriptionMNE-PythonMNE-C
FwdThreadArgThread-local workspace for parallel forward computation: source range, coils, result buffer, field function pointersImplicit in parallel loopsfwdThreadArg

Function pointer types for field callbacks:

TypeSignature
fwdFieldFunc(rd, Q, coils, result, client) → int — single dipole, single orientation
fwdVecFieldFunc(rd, coils, result_3col, client) → int — single dipole, all 3 orientations
fwdFieldGradFunc(rd, Q, coils, result, xgrad, ygrad, zgrad, client) → int — field + spatial gradients

Usage Example

#include <fwd/fwd.h>
#include <fwd/compute_fwd/compute_fwd.h>
#include <fwd/compute_fwd/compute_fwd_settings.h>

using namespace FWDLIB;

// Option A: Read an existing forward solution
MNEForwardSolution fwd = Fwd::read_forward_solution(
"sample_audvis-meg-eeg-oct-6-fwd.fif");

// Option B: Compute from scratch
ComputeFwdSettings settings;
settings.srcname = "sample-oct-6-src.fif";
settings.bemname = "sample-5120-5120-5120-bem-sol.fif";
settings.measname = "sample_audvis_raw.fif";
settings.eeg = true;
settings.meg = true;

ComputeFwd compute(&settings);
compute.calculateFwd(); // result stored in compute.fwdSolution

// Option C: Sensor-to-surface field mapping
Eigen::MatrixXd megMapping =
FwdFieldMap::computeMegMapping(info, trans, surface, sphereModel);

Doxygen Reference

For method signatures, inheritance diagrams, and source-level documentation see the auto-generated FWDLIB namespace in the Doxygen API reference.

See Also