Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions tests/backends/dispersive_route_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
from legacy_geometry import LEGACY_GRID, bare_absorber
from modal_provider_cases import GENERAL_Z_END, WAVELENGTH, general_grid
from sessions.runs import native_source
from sessions.runs import run as session_run
from sessions.runs import summary as run_summary
from solprop.backends.numpy_ref.passes import apply_hard_mask
from solprop.backends.session import ExecutionOptions
from solprop.boundaries import absorber
Expand Down Expand Up @@ -215,38 +213,6 @@ def source(
backend.close()


def run(
backend_class: type,
problem: PropagationProblem,
run_request,
grid: TimeGrid,
**options: object,
) -> tuple[np.ndarray, Any]:
"""Run the session to the last save plane; return the field and the run's summary.

The summary spans the run, every plane's attempts together
(:func:`sessions.runs.summary`).
"""
backend = backend_class(problem, run_request, grid=grid, **options)
try:
# The step driver's march computed no statistic, which a case's profiles
# would be needed for.
result = session_run(
backend, problem, backend.request.for_run(stats=StatsSpec())
)
return result.fields[-1], run_summary(result)
finally:
backend.close()


def disagreement(ours: np.ndarray, theirs: np.ndarray) -> float:
"""Return the relative L2 difference of two arrays of the same size."""
ours, theirs = np.ravel(ours), np.ravel(theirs)
scale = float(np.linalg.norm(theirs))
assert scale > 0
return float(np.linalg.norm(ours - theirs) / scale)


def evaluation_bound(problem: PropagationProblem) -> float:
"""Bound one evaluation's rounding on a spatial or general route (the docstring)."""
leaf = problem.responses.dispersive_kerr
Expand Down
51 changes: 7 additions & 44 deletions tests/backends/test_dispersive_kerr.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@
"""

import dataclasses
import warnings
from collections.abc import Mapping
from typing import Any, cast

import numpy as np
import pytest

from backends.dispersive_cases import capillary as dispersive_capillary
from backends.matched import dispersive_response_bound
from bounds import assert_within
from builders import numpy_ref_launch_derivative as stage_derivative
from solprop.backends.numpy_ref import NumpyRefBackend, passes
from solprop.boundaries import Absorber
from solprop.constants import EPS0
from solprop.driver import Simulation
from solprop.exceptions import BackendError, MaterialRangeWarning
from solprop.exceptions import BackendError
from solprop.grid import FieldGrid, RadialGrid
from solprop.nonlinear.responses import (
DispersiveKerrResponse,
Expand All @@ -40,7 +40,7 @@
mixture_filter,
)
from solprop.output import MemoryOutput
from solprop.physdata import MaterialSelection, Mixture
from solprop.physdata import Mixture
from solprop.physdata.nonlinear_data import DispersiveKerrModel, OptmatKerr
from solprop.pulses import GaussianPulse, SpatialPulse
from solprop.spatial import SpatialMedium
Expand All @@ -67,31 +67,9 @@
}


def capillary(
policy: str = "gas-polynomial",
*,
gas: str | Mapping[str, float] = "Ar",
**overrides: object,
) -> Simulation:
"""Return a short single-mode argon capillary under an electronic policy."""
# The keywords of one entry point, of as many types as it takes.
arguments: dict[str, Any] = {
"wavelength": 800e-9,
"wavelength_limits": (150e-9, 4e-6),
"trange": 150e-15,
"fwhm": 10e-15,
"energy": 30e-6,
"materials": MaterialSelection(electronic=policy),
"plasma": False,
"raman": False,
"backend": "numpy_ref",
"n_save": 2,
} | overrides
with warnings.catch_warnings():
warnings.simplefilter("ignore", MaterialRangeWarning)
return Simulation.from_capillary(
75e-6, LENGTH, gas, 2.0, **{"noise": None, **arguments}
)
def capillary(policy: str = "gas-polynomial", **overrides: object) -> Simulation:
"""Return :func:`backends.dispersive_cases.capillary` with one mode, the HE11."""
return dispersive_capillary(policy, **{"modes": 1, **overrides})


def node_field(problem, bundle):
Expand Down Expand Up @@ -232,21 +210,6 @@ def test_a_symmetric_mixture_is_one_cubic_under_its_mixture_filter():
assert_within(got[0], expected, bound, what="polarisation")


def stage_derivative(problem, run_request, grid):
"""Return ``numpy_ref``'s stage derivative at the launch."""
backend = NumpyRefBackend(problem, run_request, grid=grid)
try:
backend.set_initial(problem.initial.field0, None)
backend._hoist(0.0)
return backend._nonlinear(
backend._require_state(),
backend._stage_scalars(0.0, 0.0),
np.ones((1, *problem.shape), complex),
)
finally:
backend.close()


@pytest.mark.parametrize("thg", [True, False])
def test_without_dispersion_the_mode_averaged_derivative_is_the_constant_one(thg):
"""The whole fused path -- projection, transforms, prefactor -- around the leaf.
Expand Down
18 changes: 2 additions & 16 deletions tests/backends/test_dispersive_kerr_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@

from backends.matched import dispersive_response_bound
from bounds import assert_within
from builders import numpy_ref_launch_derivative as stage_derivative
from legacy_geometry import LEGACY_GRID, bare_absorber
from modal_provider_cases import GENERAL_Z_END, WAVELENGTH, general_grid
from solprop.backends import node_chunk
from solprop.backends.node_chunk import DEFAULT_NODE_BUDGET
from solprop.backends.numpy_ref import NumpyRefBackend, general_modal, passes
from solprop.backends.numpy_ref import general_modal, passes
from solprop.backends.numpy_ref.backend import _general_maps
from solprop.boundaries import Absorber
from solprop.driver import Simulation, SimulationSettings
Expand Down Expand Up @@ -144,21 +145,6 @@ def unit_collar(translation):
)


def stage_derivative(problem, run_request, grid):
"""Return ``numpy_ref``'s stage derivative at the launch."""
backend = NumpyRefBackend(problem, run_request, grid=grid)
try:
backend.set_initial(problem.initial.field0, None)
backend._hoist(0.0)
return backend._nonlinear(
backend._require_state(),
backend._stage_scalars(0.0, 0.0),
np.ones((1, *problem.shape), complex),
)
finally:
backend.close()


def assert_same_derivative(got, expected):
"""Hold two stage derivatives to the few FFT round trips the leaf adds.

Expand Down
50 changes: 17 additions & 33 deletions tests/backends/test_low_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1367,26 +1367,28 @@ def test_the_ledger_holds_three_registers_the_scratch_and_the_table(mode, collar
# --- the noise (section 7.1, Q3) ----------------------------------------------------


def test_the_noise_draw_never_enters_the_propagating_field():
"""With the draw and no response the state is the pure flow of its input."""
length = 1e-3
def noisy_free_case(length, seed, steps, responses):
"""A Cartesian envelope case with a modified-noise draw, on `steps` equal steps.

Returns the grid, the space, the draw, the schedule, the problem and its request.
"""
grid = EnvelopeGrid.from_limits(
length, 800e-9, (600e-9, 1.2e-6), 150e-15, thg=False
)
space = FreeGrid(3e-4, 9, 4e-4, 12, collar_fraction=0)
noise = spatial_noise(
SpatialFlux.resolve(grid, space, SpatialMedium()),
np.random.default_rng(3),
np.random.default_rng(seed),
model="modified",
)
pulse = SpatialPulse(
GaussianPulse(wavelength=800e-9, fwhm=30e-15, energy=1e-7), 80e-6
)
schedule = FixedSchedule.uniform(0.0, length, length / 8)
schedule = FixedSchedule.uniform(0.0, length, length / steps)
problem, run_request = assemble(
grid,
None,
[],
responses,
pulse.build(grid, space, medium=SpatialMedium()),
spatial_grid=space,
medium=SpatialMedium(),
Expand All @@ -1398,6 +1400,13 @@ def test_the_noise_draw_never_enters_the_propagating_field():
schedule=schedule,
integrator=MARCH,
)
return grid, space, noise, schedule, problem, run_request


def test_the_noise_draw_never_enters_the_propagating_field():
"""With the draw and no response the state is the pure flow of its input."""
length = 1e-3
grid, _, noise, schedule, problem, run_request = noisy_free_case(length, 3, 8, [])
backend = NumpyRefBackend(problem, run_request, grid=grid)
backend.set_initial(problem.initial.field0, noise)
backend.propagate_to(length)
Expand All @@ -1413,33 +1422,8 @@ def test_each_pass_carries_the_draw_at_its_own_position(monkeypatch):
every active bin, as ``test_free_noise`` forms it; the bound is that test's.
"""
length = 1e-4
grid = EnvelopeGrid.from_limits(
length, 800e-9, (600e-9, 1.2e-6), 150e-15, thg=False
)
space = FreeGrid(3e-4, 9, 4e-4, 12, collar_fraction=0)
noise = spatial_noise(
SpatialFlux.resolve(grid, space, SpatialMedium()),
np.random.default_rng(94),
model="modified",
)
pulse = SpatialPulse(
GaussianPulse(wavelength=800e-9, fwhm=30e-15, energy=1e-7), 80e-6
)
schedule = FixedSchedule.uniform(0.0, length, length / 2)
problem, run_request = assemble(
grid,
None,
[KerrResponse(1e-22, thg=False)],
pulse.build(grid, space, medium=SpatialMedium()),
spatial_grid=space,
medium=SpatialMedium(),
noise=noise,
controller=PIController(rtol=1e-8, atol=1e-14),
save_planes=[length],
dz_max=schedule.max_step,
dz_init=schedule.max_step,
schedule=schedule,
integrator=MARCH,
grid, space, noise, schedule, problem, run_request = noisy_free_case(
length, 94, 2, [KerrResponse(1e-22, thg=False)]
)
backend = NumpyRefBackend(problem, run_request, grid=grid)
backend.set_initial(problem.initial.field0, noise)
Expand Down
5 changes: 2 additions & 3 deletions tests/backends/test_native_dispersive_pathological.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@
import numpy as np
import pytest

from backends.dispersive_cases import LENGTH, POLICIES, STEPS
from backends.dispersive_cases import LENGTH, POLICIES, STEPS, disagreement
from backends.dispersive_cases import run as march
from backends.dispersive_route_cases import (
ARGON_FILL,
disagreement,
evaluation_bound,
general_problem,
strand,
)
from backends.dispersive_route_cases import run as march
from backends.dispersive_route_cases import source as launch_source
from bounds import assert_within, step_evaluations
from sessions.runs import native_source
Expand Down
3 changes: 1 addition & 2 deletions tests/backends/test_native_dispersive_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import pytest

import flow_parity
from backends.dispersive_cases import disagreement, run
from backends.dispersive_route_cases import (
ARGON,
ARGON_FILL,
Expand All @@ -43,13 +44,11 @@
HELIUM,
STEPS,
STRANDS,
disagreement,
free_grid,
free_problem,
general_problem,
planar_problem,
raman,
run,
source,
strand,
)
Expand Down
Loading
Loading