"""Reusable finite-volume vector Born-series engine.
Time convention exp(-i omega t). Relative dielectric contrast is linearized
as 2 n0 (delta_n + i kappa) at every order. Amplitudes interfere coherently before the
incident-mode intensity average is evaluated.
"""
from dataclasses import dataclass, field
import numpy as np
from .green import GreenOperator
from .grid import Grid
from .directions import Directions
from .results import BornResult
from .source import Source
from numpy.typing import ArrayLike
from .polarization import PolarizationState
from .units import Quantity, validate_quantity
from .volume import Volume
[docs]
@dataclass(frozen=True, kw_only=True, eq=False)
class BornSeries:
"""Own the fixed geometry and propagation operator for numerical Born orders.
Parameters
----------
grid : Grid
Shared spatial configuration, also used by the input Volume.
Required for generation and scattering.
background_refractive_index : float
Positive uniform background refractive index, dimensionless.
wavelength : Quantity
Positive vacuum wavelength; explicit length units are required.
directions : Directions
Validated, immutable Cartesian unit observation vectors.
order : int, optional
Highest cumulative Born order, from 1 to 12; default 3.
incident_direction : array_like, optional
Cartesian unit propagation vector; defaults to positive z.
polarization : array_like, optional
Complex transverse unit electric-field vector; omit for unpolarized light.
Attributes
----------
operator : bornsim.green.GreenOperator
One open-boundary FFT Green operator reused for every compatible volume.
Notes
-----
The cached incident plane wave follows incident_direction. A supplied
complex polarization defines one pure mode; otherwise two orthogonal
transverse modes are averaged incoherently.
For each call, ``E[0] = E_inc`` and ``E[m] = K(chi*E[m-1])``, where
``chi = 2*n0*delta_refractive_index`` and K integrates the background dyadic Green tensor
multiplied by ``k0**2``. Order-m far-field amplitude is computed from
``chi*E[m-1]``. Cumulative intensity is ``|sum_m f[m]|**2``, averaged over the incident
polarization modes and divided by the entire voxel-box volume.
Geometry, incident field, and the Green tensor are reused; contrast,
propagated fields, amplitudes, and diagnostics are local to each solve.
Compatible volumes must have exactly the configured grid and background.
Configuration is frozen to prevent stale propagation settings. Decreasing
field terms do not establish universal convergence; the dielectric
linearization is unchanged at higher orders.
"""
grid: Grid
shape: tuple = field(init=False)
spacing: Quantity = field(init=False)
background_refractive_index: float
wavelength: Quantity
directions: Directions
order: int = 3
incident_direction: ArrayLike = (0.0, 0.0, 1.0)
polarization: PolarizationState | ArrayLike | None = None
operator: GreenOperator = field(init=False, repr=False)
_positions: Quantity = field(init=False, repr=False)
_incident_field: np.ndarray = field(init=False, repr=False)
def __post_init__(self) -> None:
grid = Grid._resolve(grid=self.grid)
object.__setattr__(self, "grid", grid)
volume = Volume(
delta_refractive_index=np.zeros(grid.shape),
grid=grid,
background_refractive_index=self.background_refractive_index,
)
shape = grid.shape
wavelength = self.wavelength
validate_quantity(
self.wavelength,
unit="meter",
name="wavelength",
scalar=True,
)
if not np.isfinite(wavelength) or wavelength <= 0:
raise ValueError("wavelength must be finite and positive.")
if isinstance(self.order, bool) or not isinstance(self.order, (int, np.integer)) or not 1 <= self.order <= 12:
raise ValueError("order must be an integer between 1 and 12.")
order = int(self.order)
if not isinstance(self.directions, Directions):
raise TypeError("directions must be a Directions instance.")
directions = self.directions
positions = grid.positions
positions.magnitude.setflags(write=False)
illumination = Source(
wavelength=wavelength,
incident_direction=self.incident_direction,
polarization=self.polarization,
)
distance_along_beam = np.sum(positions * illumination.incident_direction, axis=-1)
phase = np.exp(1j * 2 * np.pi / wavelength * volume.background_refractive_index * distance_along_beam)
incident = phase.to("dimensionless").magnitude[..., None, None] * illumination.polarization_vectors
incident.setflags(write=False)
operator = GreenOperator(
shape=shape,
spacing=grid.spacing,
wavelength=wavelength,
background_refractive_index=volume.background_refractive_index,
)
for name, value in (
("shape", shape),
("spacing", grid.spacing),
("background_refractive_index", volume.background_refractive_index),
("wavelength", wavelength.copy()),
("directions", directions),
("order", order),
("incident_direction", illumination.incident_direction),
("polarization", illumination.polarization),
("operator", operator),
("_positions", positions),
("_incident_field", incident),
):
object.__setattr__(self, name, value)
def _far_field(self, *, source: np.ndarray) -> np.ndarray:
"""Project the voxel source onto transverse outgoing amplitudes in metres."""
# Fourier projection uses numeric SI coordinates and amplitudes in metres.
k0 = 2 * np.pi / float(self.wavelength.to("meter").magnitude)
k = k0 * self.background_refractive_index
positions = self._positions.to("meter").magnitude.reshape(-1, 3)
voxel_spacing_m = float(self.spacing.to("meter").magnitude)
values = source.reshape(-1, self._incident_field.shape[-2], 3)
amplitude = np.empty((len(self.directions), self._incident_field.shape[-2], 3), dtype=complex)
for start in range(0, len(self.directions), 64):
direction = self.directions.vectors[start : start + 64]
phase = np.exp(-1j * k * (direction @ positions.T))
integral = np.einsum("dn,npv->dpv", phase, values) * k0**2 * voxel_spacing_m**3 / (4 * np.pi)
amplitude[start : start + 64] = (
integral - direction[:, None, :] * np.einsum("dv,dpv->dp", direction, integral)[..., None]
)
return amplitude
[docs]
def solve(self, *, volume: Volume) -> BornResult:
"""Compute coherent scattering from one compatible finite refractive index volume.
Parameters
----------
volume : Volume
Fixed fluctuation field with positive linearized permittivity,
matching this engine's grid spacing, dimensions and background.
Returns
-------
result : bornsim.results.BornResult
Fresh numeric SI amplitudes, cumulative and isolated intensities,
relative field norms, and diagnostic messages.
Raises
------
TypeError
If volume is not a Volume.
ValueError
If grid or background differs from the engine configuration, or
iteration produces nonfinite fields.
Notes
-----
Each solve starts with the incident field. Amplitudes are summed
before squaring, preserving interference. Intensities are finite-sample
cross sections divided by the entire voxel-box volume, not intrinsic
infinite-medium transport coefficients. The dielectric contrast is
``2*n0*delta_refractive_index`` at every order. Growing field terms generate a
diagnostic; decreasing terms do not certify convergence.
Examples
--------
>>> from bornsim.units import ureg
>>> from bornsim import Directions, Grid, GaussianMedium
>>> from bornsim.series import BornSeries
...
...
>>> medium = GaussianMedium(
... background_refractive_index=1.33,
... refractive_index_std=0.01,
... correlation_length=100e-9 * ureg.meter,
... )
...
>>> grid = Grid(
... shape=(2, 2, 2),
... spacing=50 * ureg.nanometer,
... )
>>> volume = medium.to_volume(
... grid=grid,
... seed=42,
... )
>>> directions = Directions(vectors=[[0.0, 0.0, 1.0]])
>>> engine = BornSeries(
... grid=grid,
... background_refractive_index=volume.background_refractive_index,
... wavelength=633 * ureg.nanometer,
... directions=directions,
... order=2,
... )
>>> result = engine.solve(volume=volume)
>>> result.amplitudes.shape
(2, 1, 2, 3)
"""
if not isinstance(volume, Volume):
raise TypeError("volume must be a Volume.")
incompatible_volume = (
volume.delta_refractive_index.shape != self.shape
or volume.spacing != self.spacing
or volume.background_refractive_index != self.background_refractive_index
)
if incompatible_volume:
raise ValueError("volume grid and background_refractive_index must match the BornSeries configuration.")
field = self._incident_field
incident_norm = np.linalg.norm(field)
polarization_count = field.shape[-2]
contrast = np.asarray(2 * volume.background_refractive_index * volume.delta_refractive_index, dtype=complex)
if np.any(volume.extinction_coefficient):
contrast = contrast + 2j * volume.background_refractive_index * volume.extinction_coefficient
total_field = np.array(field, copy=True)
absorption = []
k0 = 2 * np.pi / float(self.wavelength.to("meter").magnitude)
amplitude_terms, norms = [], []
for _ in range(self.order):
# P_abs / (incident intensity * box volume), averaged over polarizations.
absorption.append(
float(
2
* k0
/ polarization_count
* np.mean(volume.extinction_coefficient * np.sum(np.abs(total_field) ** 2, axis=(-1, -2)))
)
)
source = contrast[..., None, None] * field
amplitude_terms.append(self._far_field(source=source))
field = self.operator.apply(source=source)
if not np.all(np.isfinite(field)):
raise ValueError("Born terms overflowed; reduce contrast or order.")
norms.append(float(np.linalg.norm(field) / incident_norm))
total_field += field
amplitudes = np.asarray(amplitude_terms)
cumulative = np.cumsum(amplitudes, axis=0)
differential = np.sum(np.abs(cumulative) ** 2, axis=(-1, -2)) / (
polarization_count * float(volume.volume.to("meter**3").magnitude)
)
separate = np.sum(np.abs(amplitudes) ** 2, axis=(-1, -2)) / (
polarization_count * float(volume.volume.to("meter**3").magnitude)
)
warnings = []
if volume.spacing > self.wavelength / volume.background_refractive_index / 10:
warnings.append("Voxel spacing exceeds one tenth of the background wavelength; refine the grid.")
if len(norms) > 1 and any(b >= a and b > 0 for a, b in zip(norms, norms[1:])):
warnings.append("Successive field terms are not decreasing; Born convergence is not established.")
return BornResult(
directions=self.directions.vectors.copy(),
amplitudes=amplitudes,
differential=differential,
term_differential=separate,
field_norms=np.array(norms),
warnings=tuple(warnings),
mu_a=np.array(absorption),
residual_norms=np.array(
norms[1:]
+ [float(np.linalg.norm(self.operator.apply(source=contrast[..., None, None] * field)) / incident_norm)]
),
)