From 45db528cb5b5b59c63773f96216d950f6a465304 Mon Sep 17 00:00:00 2001 From: chrisbrahms Date: Thu, 5 Nov 2026 12:25:15 +0000 Subject: [PATCH] Revert the modified shot-noise model (#414) The modified shot-noise model (a constant noise field added inside the nonlinear operator, excluded from dispersion) does not give the correct physics, so this reverts the model of #414 (squash commit 9e9a2700): `shotnoise` is a Bool again and `true` adds one-photon-per-mode noise to the input field, as before #414. Kept from #414, because they are not part of the noise model: - the QueueExec fix in Scans.jl (a worker no longer recreates a finished scan's queue file), - the .gitignore entries, - the approximate comparison of the cached PPT rate in test_ionisation.jl, - the `rng` keyword of prop_capillary/prop_gnlse, now passed to Fields.ShotNoise(rng) for seeded input noise (new test in test_interface). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WM2AxPkj1PCFvBcaaaanzV --- docs/make.jl | 3 +- docs/src/model/noise.md | 154 ----------------------- src/Fields.jl | 77 ------------ src/Interface.jl | 100 +++++---------- src/Luna.jl | 40 +++--- src/NonlinearRHS.jl | 258 +++++++++----------------------------- test/runtests.jl | 5 - test/test_interface.jl | 13 ++ test/test_noise.jl | 265 ---------------------------------------- 9 files changed, 117 insertions(+), 798 deletions(-) delete mode 100644 docs/src/model/noise.md delete mode 100644 test/test_noise.jl diff --git a/docs/make.jl b/docs/make.jl index 61cab5bfd..8dc131533 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -11,8 +11,7 @@ makedocs( "The numerical model" => [ "General description" => "model/model.md", "Modal decompositions" => "model/modal_decompositions.md", - "Nonlinear responses" => "model/nonlinear_responses.md", - "Noise model" => "model/noise.md" + "Nonlinear responses" => "model/nonlinear_responses.md" ], "The simple interface" => "interface.md", "Parameter scans" => "scans.md", diff --git a/docs/src/model/noise.md b/docs/src/model/noise.md deleted file mode 100644 index 927848157..000000000 --- a/docs/src/model/noise.md +++ /dev/null @@ -1,154 +0,0 @@ -# Noise model - -## Background - -Noise-seeded processes are central to many phenomena in nonlinear fibre optics: spontaneous -Raman scattering seeds Raman combs; noise-seeded four-wave -mixing (FWM) drives modulational instability (MI); and the interplay of both produces the -shot-to-shot fluctuations characteristic of supercontinuum generation. An accurate noise -model is therefore essential for any simulation where noise-initiated nonlinear dynamics play -a role. - -Luna.jl supports two noise models, selected via the `shotnoise` keyword argument in -[`prop_capillary`](@ref Interface.prop_capillary) and [`prop_gnlse`](@ref Interface.prop_gnlse): - -- **`:input`** --- the traditional shot-noise approach. -- **`:modified`** or **`true`** (default) --- the modified shot-noise approach of Chen & Wise [1]. - -## Traditional shot noise (`:input`) - -The traditional approach adds one-photon-per-mode quantum noise directly to the input field -at ``z = 0``: - -```math -\tilde{E}(z=0, \omega) = \tilde{E}_{\mathrm{pulse}}(\omega) + \tilde{E}_{\mathrm{noise}}(\omega) -``` - -where ``|\tilde{E}_{\mathrm{noise}}(\omega)| = \sqrt{\hbar\omega\,\Delta\nu}`` with uniformly -random spectral phase and ``\Delta\nu`` is the frequency bin spacing. The propagation equation -then contains only stimulated (deterministic) terms, and all noise-seeded processes arise from -the stimulated evolution of the injected noise. - -While simple, this approach has several well-known limitations: - -1. **Artificial FWM phase-matching**: The noise field, superimposed on the input pulse, - acquires a coherent phase increment from dispersion during propagation. This means the - noise can satisfy phase-matching conditions for FWM, which is unphysical --- real vacuum - fluctuations are incoherent and cannot maintain phase-matching over extended distances. - -2. **Elevated noise floor**: The noise creates a persistent increase of the spectral - intensity at all frequencies that does not decay. - -3. **No Stokes/anti-Stokes asymmetry**: Spontaneous Raman generation exhibits a fundamental - spectral asymmetry (Stokes is favoured by an extra quantum of vacuum fluctuation). The - traditional model does not capture this. - -## Modified shot noise (`:modified`) - -The modified shot-noise model [1] resolves all of the above issues by including the noise -field in the **nonlinear operator** rather than in the input field: - -```math -\partial_z A = \hat{\mathcal{D}}\,A + \hat{\mathcal{N}}(A + A_{\mathrm{noise}}) -``` - -The key structural features are: - -- **Dispersion acts only on the physical field**: ``\hat{\mathcal{D}}\,A``, not - ``\hat{\mathcal{D}}(A + A_{\mathrm{noise}})``. This prevents the noise from acquiring - coherent phase increments that would cause artificial FWM phase-matching. - -- **The noise field is constant**: ``A_{\mathrm{noise}}(\omega)`` is generated once before - propagation with one-photon-per-mode spectral density and random phase, and held fixed - throughout. It does not evolve, grow, or accumulate. - -- **All nonlinear processes are correctly seeded**: Both Kerr (FWM/MI) and Raman - (spontaneous Stokes/anti-Stokes) processes are seeded through the same mechanism --- - the noise enters the nonlinear operator alongside the field. - -- **No elevated noise floor**: Because the noise is not added to the propagating field, - the spectrum at ``z = 0`` contains only the input pulse. Noise-seeded spectral components - grow continuously from zero as the pulse propagates. - -This approach is a **general-purpose noise model** applicable to all ``\chi^{(3)}`` -propagation problems, including MI-based supercontinuum generation, Raman comb formation, -soliton dynamics with self-frequency shift, and combined Kerr--Raman broadening. - -## Usage - -The modified shot-noise model is used by default (`shotnoise=true`, equivalent to -`shotnoise=:modified`). To use the traditional model instead, pass `shotnoise=:input`: - -```julia -# Default: modified shot-noise model (shotnoise=true) -output = prop_capillary(radius, flength, gas, pressure; - λ0=800e-9, λlims=(200e-9, 4000e-9), trange=400e-15, - τfwhm=30e-15, energy=1e-6, -) -``` - -For ensemble simulations (shot-to-shot statistics), use different random seeds for each shot: - -```julia -using Random -for shot in 1:100 - rng = MersenneTwister(shot) - output = prop_capillary(radius, flength, gas, pressure; - λ0=800e-9, λlims=(200e-9, 4000e-9), trange=400e-15, - τfwhm=30e-15, energy=1e-6, - shotnoise=:modified, rng=rng - ) - # ... process output ... -end -``` - -Setting `shotnoise=false` disables noise entirely. This prevents generation of the noise -field when using the modified model, and prevents adding shot noise to the input spectrum -when using the traditional model. - -## Ionization and plasma - -In the modified shot-noise model, the combined field ``A + A_{\mathrm{noise}}`` is passed to -**all** nonlinear response functions, including Kerr, Raman, and plasma/ionization. This is -physically reasonable because the noise amplitude is of order -``\sqrt{\hbar\omega\,\Delta\nu} \approx 5 \times 10^{-4}\;\sqrt{\mathrm{W}}`` per -frequency mode — roughly ``10^{-14}`` of a typical pulse peak power. The tunnelling -ionization rate depends on the electric field through a highly nonlinear (exponential) -threshold, so the negligible noise field has no measurable effect on the plasma response. -Only the Kerr and Raman processes, which are linear in the perturbation field, are -meaningfully affected by the noise injection. - -## Power diagnostics at low signal levels - -Because the noise is not added to the propagating field, spontaneously generated spectral -components start from exactly zero power at ``z = 0``. At very small ``z``, the power -``|A_{s'}|^2 \propto z^2`` (quadratic growth), which is the expected linearised result. -To recover the correct physical power including the vacuum fluctuation contribution, one -can temporarily add the noise before computing the power: - -```math -P_{s'}(z) = |A_{s'}(z) + A_{\mathrm{noise}}|^2 - P_{\mathrm{noise}} -``` - -This correction is only necessary when the generated power is comparable to or below the -noise floor. At higher powers (the stimulated/saturated regime), ``|A_{s'}|^2`` directly -gives the correct result. - -## Comparison - -| Feature | Traditional (`:input`) | Modified (`:modified`) | -|---------|----------------------|----------------------| -| Artificial FWM phase-matching | Yes | No | -| Elevated noise floor | Yes | No | -| Stokes/anti-Stokes asymmetry | Not captured | Effectively captured | -| Captures all cascaded processes | Only through stimulated evolution | Automatically | -| Step-size sensitivity | No | No | - -## References - -1. Y.-H. Chen and F. W. Wise, "A simple accurate way to model noise-seeded ultrafast - nonlinear processes", [arXiv:2410.20567](https://arxiv.org/abs/2410.20567) (2024). - -2. Y.-H. Chen and F. W. Wise, "Unified theory for Raman scattering in gas-filled - hollow-core fibers", [APL Photonics **9**, 030902](https://doi.org/10.1063/5.0189749) - (2024). diff --git a/src/Fields.jl b/src/Fields.jl index 4bd18c1fa..ec6eeb741 100644 --- a/src/Fields.jl +++ b/src/Fields.jl @@ -320,83 +320,6 @@ function (s::ShotNoise)(grid::Grid.EnvGrid, FT=nothing) @. FFTamp * exp(1im*φ) end -""" - generate_noise_field(grid::Grid.RealGrid; rng=GLOBAL_RNG, nmodes=1) - -Generate a one-photon-per-mode spectral noise field for the modified shot-noise model -(Chen & Wise, arXiv:2410.20567). - -Returns a complex frequency-domain array with amplitude `√(ħω/δω) × √(2π)/(2δt)` and -uniformly random spectral phase, matching the convention of [`ShotNoise`](@ref) for a -real-valued field (rFFT normalisation). When `nmodes > 1`, returns an `(nω, nmodes)` array -with independent noise per mode for multimode propagation. - -Unlike [`ShotNoise`](@ref), which is added to the input field at `z = 0`, this noise field -is intended to be injected into the **nonlinear operator** at every propagation step via the -`noise_field` keyword argument. The dispersion operator does not act on this noise, -preventing artificial FWM phase-matching — the key advantage over the traditional shot-noise -approach. - -The noise field is generated once before propagation and held constant throughout. Different -random seeds (via `rng`) produce different noise realisations for ensemble/shot-to-shot -statistics. - -# References -- Chen & Wise, "A simple accurate way to model noise-seeded ultrafast nonlinear processes", - arXiv:2410.20567 (2024) -""" -function generate_noise_field(grid::Grid.RealGrid; rng=GLOBAL_RNG, nmodes=1) - # Frequency spacing on the angular-frequency grid - δω = grid.ω[2] - grid.ω[1] - δt = grid.t[2] - grid.t[1] - # One-photon-per-mode amplitude: √(ħω/δω), matching ShotNoise - amp = @. sqrt(PhysData.ħ * grid.ω / δω) - # Scale for rFFT convention (real-valued field, half-spectrum): - # factor √(2π)/(2δt) converts from angular-frequency density to DFT bins - rFFTamp = sqrt(2π) / 2δt * amp - # Uniformly random spectral phase ∈ [0, 2π) - # For multimode (nmodes > 1), generate independent noise per mode → (nω, nmodes) - if nmodes > 1 - φ = 2π * rand(rng, length(grid.ω), nmodes) - rFFTamp .* exp.(1im .* φ) - else - φ = 2π * rand(rng, size(grid.ω)...) - @. rFFTamp * exp(1im * φ) - end -end - -""" - generate_noise_field(grid::Grid.EnvGrid; rng=GLOBAL_RNG, nmodes=1) - -Generate a one-photon-per-mode spectral noise field for the modified shot-noise model -(Chen & Wise, arXiv:2410.20567), for envelope-field propagation. - -Returns a complex frequency-domain array with amplitude `√(ħω/δω) × √(2π)/δt` and -uniformly random spectral phase, matching the convention of [`ShotNoise`](@ref) for a -complex envelope field (full FFT normalisation). Only frequency bins within `grid.sidx` -(the signal window) receive non-zero noise. - -See [`generate_noise_field(::Grid.RealGrid)`](@ref) for details on the modified shot-noise -model. -""" -function generate_noise_field(grid::Grid.EnvGrid; rng=GLOBAL_RNG, nmodes=1) - δω = grid.ω[2] - grid.ω[1] - δt = grid.t[2] - grid.t[1] - amp = zero(grid.ω) - # Only populate signal frequencies (grid.sidx masks out negative/out-of-band bins) - amp[grid.sidx] = @. sqrt(PhysData.ħ * grid.ω[grid.sidx] / δω) - # Scale for full FFT convention (complex envelope): - # factor √(2π)/δt converts from angular-frequency density to DFT bins - FFTamp = sqrt(2π) / δt * amp - if nmodes > 1 - φ = 2π * rand(rng, length(grid.ω), nmodes) - FFTamp .* exp.(1im .* φ) - else - φ = 2π * rand(rng, size(grid.ω)...) - @. FFTamp * exp(1im * φ) - end -end - """ SpatioTemporalField(λ0, energy, ϕ, τ0, Ishape) diff --git a/src/Interface.jl b/src/Interface.jl index b28fae180..2e50d41ff 100644 --- a/src/Interface.jl +++ b/src/Interface.jl @@ -2,7 +2,7 @@ module Interface using Luna import Luna.PhysData: wlfreq, roomtemp import Luna: Grid, Modes, Output, Fields -import Random: AbstractRNG, GLOBAL_RNG +import Random: GLOBAL_RNG import Logging: @info, @debug module Pulses @@ -288,19 +288,10 @@ In this case, all keyword arguments except for `λ0` are ignored. elliptical polarisation is always the y-axis. - `propagator`: A function `propagator!(Eω, grid)` which **mutates** its first argument to apply an arbitrary propagation to the pulse before the simulation starts. -- `shotnoise`: Whether and how to include quantum noise. Can be one of: - - `true` (default) -- same as `:modified`. - - `false` -- disable all noise. - - `:modified` -- use the modified shot-noise model of Chen & Wise - (arXiv:2410.20567), where a constant noise field enters the nonlinear operator - at every step but is excluded from dispersion. This prevents artificial FWM - phase-matching and elevated noise floor artefacts. - - `:input` -- use traditional one-photon-per-mode shot noise added to the input - field at `z = 0`. - See the [Noise model](@ref) documentation for details. -- `rng`: Random number generator for noise field generation. Defaults to `GLOBAL_RNG`. - Pass a seeded RNG (e.g. `MersenneTwister(seed)`) for reproducible noise realisations, - or different seeds for ensemble/shot-to-shot statistics. +- `shotnoise`: If `true` (default), one-photon-per-mode quantum noise is included. +- `rng`: Random number generator for the shot noise. Defaults to `GLOBAL_RNG`. Pass a seeded + RNG (e.g. `MersenneTwister(seed)`) for reproducible noise realisations, or different seeds + for ensemble/shot-to-shot statistics. # Modes options - `modes`: Defines which modes are included in the propagation. Can be any of: @@ -376,8 +367,7 @@ function prop_capillary_args(radius, flength, gas, pressure; power=nothing, energy=nothing, pulseshape=:gauss, polarisation=:linear, propagator=nothing, pulses=nothing, - shotnoise=true, - rng=GLOBAL_RNG, + shotnoise=true, rng=GLOBAL_RNG, modes=:HE11, model=:full, loss=true, radial_integral_rtol=1e-3, raman=nothing, kerr=true, plasma=nothing, @@ -410,10 +400,9 @@ function prop_capillary_args(radius, flength, gas, pressure; PPT_options, preionfrac, temperature) inputs = makeinputs(mode_s, λ0, pulses, τfwhm, τw, ϕ, power, energy, pulseshape, polarisation, propagator) - inputs, noise_field = makenoise(grid, mode_s, inputs, shotnoise, rng) + inputs = shotnoise_maybe(inputs, mode_s, shotnoise, rng) linop, Eω, transform, FT = setup(grid, mode_s, density, resp, inputs, pol, - radial_integral_rtol, const_linop(radius, pressure); - noise_field) + radial_integral_rtol, const_linop(radius, pressure)) stats = Stats.default(grid, Eω, mode_s, linop, transform; gas=gas, stats_kwargs...) output = makeoutput(grid, saveN, stats, filepath, scan, scanidx, filename) @@ -805,50 +794,31 @@ function ellfields(pulse::Pulses.DataPulse, pf) f1, f2 end -function makenoise(grid, mode_s, inputs, shotnoise::Bool, rng) - shotnoise ? makenoise(grid, mode_s, inputs, :modified, rng) : (inputs, nothing) -end - -function makenoise(grid, mode_s, inputs, shotnoise::Symbol, rng) - if shotnoise == :modified - nm = mode_s isa AbstractArray ? length(mode_s) : 1 - noise_field = Fields.generate_noise_field(grid; rng, nmodes=nm) - @info("Modified shot-noise model enabled. Traditional input shot noise is " * - "disabled (noise enters through nonlinear operator instead).") - return (inputs, noise_field) - elseif shotnoise == :input - inputs = _add_input_shotnoise(inputs, mode_s, rng) - return (inputs, nothing) - else - throw(DomainError(shotnoise, "Unknown shotnoise=$shotnoise. Use true, false, :modified, or :input.")) - end -end - -function _add_input_shotnoise(inputs, mode::Modes.AbstractMode, rng) +function shotnoise_maybe(inputs, mode::Modes.AbstractMode, shotnoise::Bool, rng=GLOBAL_RNG) + shotnoise || return inputs (inputs..., (mode=1, fields=(Fields.ShotNoise(rng),))) end -function _add_input_shotnoise(inputs, modes, rng) +function shotnoise_maybe(inputs, modes, shotnoise::Bool, rng=GLOBAL_RNG) + shotnoise || return inputs (inputs..., [(mode=ii, fields=(Fields.ShotNoise(rng),)) for ii in eachindex(modes)]...) end -function setup(grid, mode::Modes.AbstractMode, density, responses, inputs, pol, rtol, - c::Val{true}; noise_field=nothing) +function setup(grid, mode::Modes.AbstractMode, density, responses, inputs, pol, rtol, c::Val{true}) @info("Using mode-averaged propagation.") linop, βfun!, _, _ = LinearOps.make_const_linop(grid, mode, grid.referenceλ) Eω, transform, FT = Luna.setup(grid, density, responses, inputs, - βfun!, z -> Modes.Aeff(mode, z=z); noise_field) + βfun!, z -> Modes.Aeff(mode, z=z)) linop, Eω, transform, FT end -function setup(grid, mode::Modes.AbstractMode, density, responses, inputs, pol, rtol, - c::Val{false}; noise_field=nothing) +function setup(grid, mode::Modes.AbstractMode, density, responses, inputs, pol, rtol, c::Val{false}) @info("Using mode-averaged propagation.") linop, βfun! = LinearOps.make_linop(grid, mode, grid.referenceλ) Eω, transform, FT = Luna.setup(grid, density, responses, inputs, - βfun!, z -> Modes.Aeff(mode, z=z); noise_field) + βfun!, z -> Modes.Aeff(mode, z=z)) linop, Eω, transform, FT end @@ -856,23 +826,21 @@ needfull(modes) = !all(modes) do mode (mode.kind == :HE) && (mode.n == 1) end -function setup(grid, modes, density, responses, inputs, pol, rtol, c::Val{true}; - noise_field=nothing) +function setup(grid, modes, density, responses, inputs, pol, rtol, c::Val{true}) nf = needfull(modes) @info(nf ? "Using full 2-D modal integral." : "Using radial modal integral.") linop = LinearOps.make_const_linop(grid, modes, grid.referenceλ) Eω, transform, FT = Luna.setup(grid, density, responses, inputs, modes, - pol ? :xy : :y; full=nf, rtol, noise_field) + pol ? :xy : :y; full=nf, rtol) linop, Eω, transform, FT end -function setup(grid, modes, density, responses, inputs, pol, rtol, c::Val{false}; - noise_field=nothing) +function setup(grid, modes, density, responses, inputs, pol, rtol, c::Val{false}) nf = needfull(modes) @info(nf ? "Using full 2-D modal integral." : "Using radial modal integral.") linop = LinearOps.make_linop(grid, modes, grid.referenceλ) Eω, transform, FT = Luna.setup(grid, density, responses, inputs, modes, - pol ? :xy : :y; full=nf, rtol, noise_field) + pol ? :xy : :y; full=nf, rtol) linop, Eω, transform, FT end @@ -931,19 +899,10 @@ Note that the current GNLSE model is single mode only. elliptical polarisation is always the y-axis. - `propagator`: A function `propagator!(Eω, grid)` which **mutates** its first argument to apply an arbitrary propagation to the pulse before the simulation starts. -- `shotnoise`: Whether and how to include quantum noise. Can be one of: - - `true` (default) -- same as `:modified`. - - `false` -- disable all noise. - - `:modified` -- use the modified shot-noise model of Chen & Wise - (arXiv:2410.20567), where a constant noise field enters the nonlinear operator - at every step but is excluded from dispersion. This prevents artificial FWM - phase-matching and elevated noise floor artefacts. - - `:input` -- use traditional one-photon-per-mode shot noise added to the input - field at `z = 0`. - See the [Noise model](@ref) documentation for details. -- `rng`: Random number generator for noise field generation. Defaults to `GLOBAL_RNG`. - Pass a seeded RNG (e.g. `MersenneTwister(seed)`) for reproducible noise realisations, - or different seeds for ensemble/shot-to-shot statistics. +- `shotnoise`: If `true` (default), one-photon-per-mode quantum noise is included. +- `rng`: Random number generator for the shot noise. Defaults to `GLOBAL_RNG`. Pass a seeded + RNG (e.g. `MersenneTwister(seed)`) for reproducible noise realisations, or different seeds + for ensemble/shot-to-shot statistics. # GNLSE options - `shock::Bool`: Whether to include the shock derivative term. Default is `true`. @@ -991,8 +950,7 @@ function prop_gnlse_args(γ, flength, βs; λ0, λlims, trange, power=nothing, energy=nothing, pulseshape=:gauss, propagator=nothing, pulses=nothing, - shotnoise=true, shock=true, - rng=GLOBAL_RNG, + shotnoise=true, shock=true, rng=GLOBAL_RNG, loss=0.0, raman=true, fr=0.18, ramanmodel=:sdo, τ1=12.2e-15, τ2=32e-15, saveN=201, filepath=nothing, @@ -1033,18 +991,16 @@ function prop_gnlse_args(γ, flength, βs; λ0, λlims, trange, inputs = makeinputs(mode_s, λ0, pulses, τfwhm, τw, ϕ, power, energy, pulseshape, polarisation, propagator) - inputs, noise_field = makenoise(grid, mode_s, inputs, shotnoise, rng) + inputs = shotnoise_maybe(inputs, mode_s, shotnoise, rng) norm! = NonlinearRHS.norm_mode_average_gnlse(grid, aeff; shock) - Eω, transform, FT = Luna.setup(grid, density, resp, inputs, βfun!, aeff; - norm!, noise_field) + Eω, transform, FT = Luna.setup(grid, density, resp, inputs, βfun!, aeff, norm! = norm!) stats = Stats.default(grid, Eω, mode_s, linop, transform) output = makeoutput(grid, saveN, stats, filepath, scan, scanidx, filename) saveargs(output; γ, flength, βs, λlims, trange, envelope, thg, δt, λ0, τfwhm, τw, ϕ, power, energy, pulseshape, polarisation, propagator, pulses, - shotnoise, shock, loss, raman, ramanmodel, fr, τ1, τ2, - saveN, filepath, filename) + shotnoise, shock, loss, raman, ramanmodel, fr, τ1, τ2, saveN, filepath, filename) return Eω, grid, linop, transform, FT, output end diff --git a/src/Luna.jl b/src/Luna.jl index cc206f2da..9369d70dc 100644 --- a/src/Luna.jl +++ b/src/Luna.jl @@ -113,15 +113,13 @@ function doinput_sm(grid, inputs::Tuple{Vararg{T} where T <: NamedTuple{<:Any, < end function setup(grid::Grid.RealGrid, densityfun, responses, inputs, βfun!, aeff; - norm! = NonlinearRHS.norm_mode_average(grid, βfun!, aeff), - noise_field=nothing) + norm! = NonlinearRHS.norm_mode_average(grid, βfun!, aeff)) Logging.@info("Setting up and planning FFTs...") flush(stderr) Utils.loadFFTwisdom() xo = Array{Float64}(undef, length(grid.to)) FTo = FFTW.plan_rfft(xo, 1, flags=settings["fftw_flag"]) - transform = NonlinearRHS.TransModeAvg(grid, FTo, responses, densityfun, norm!, aeff; - noise_field) + transform = NonlinearRHS.TransModeAvg(grid, FTo, responses, densityfun, norm!, aeff) x = Array{Float64}(undef, length(grid.t)) FT = FFTW.plan_rfft(x, 1, flags=settings["fftw_flag"]) Eω = doinput_sm(grid, inputs, FT) @@ -134,8 +132,7 @@ function setup(grid::Grid.RealGrid, densityfun, responses, inputs, βfun!, aeff; end function setup(grid::Grid.EnvGrid, densityfun, responses, inputs, βfun!, aeff; - norm! = NonlinearRHS.norm_mode_average(grid, βfun!, aeff), - noise_field=nothing) + norm! = NonlinearRHS.norm_mode_average(grid, βfun!, aeff)) Logging.@info("Setting up and planning FFTs...") flush(stderr) Utils.loadFFTwisdom() @@ -143,8 +140,7 @@ function setup(grid::Grid.EnvGrid, densityfun, responses, inputs, βfun!, aeff; FT = FFTW.plan_fft(x, 1, flags=settings["fftw_flag"]) xo = Array{ComplexF64}(undef, length(grid.to)) FTo = FFTW.plan_fft(xo, 1, flags=settings["fftw_flag"]) - transform = NonlinearRHS.TransModeAvg(grid, FTo, responses, densityfun, norm!, aeff; - noise_field) + transform = NonlinearRHS.TransModeAvg(grid, FTo, responses, densityfun, norm!, aeff) Eω = doinput_sm(grid, inputs, FT) inv(FT) # create inverse FT plans now, so wisdom is saved inv(FTo) @@ -177,7 +173,7 @@ end function setup(grid::Grid.RealGrid, densityfun, responses, inputs, modes::Modes.ModeCollection, components; full=false, norm! = NonlinearRHS.norm_modal(grid), - rtol=1e-3, atol=0.0, mfcn=512, noise_field=nothing) + rtol=1e-3, atol=0.0, mfcn=512) Logging.@info("Setting up and planning FFTs...") flush(stderr) ts = Modes.ToSpace(modes, components=components) @@ -192,7 +188,7 @@ function setup(grid::Grid.RealGrid, densityfun, responses, inputs, FTo = FFTW.plan_rfft(xo, 1, flags=settings["fftw_flag"]) transform = NonlinearRHS.TransModal(grid, ts, FTo, responses, densityfun, norm!; - rtol, atol, mfcn, full, noise_field) + rtol, atol, mfcn, full) inv(FT) # create inverse FT plans now, so wisdom is saved inv(FTo) Utils.saveFFTwisdom() @@ -204,7 +200,7 @@ end function setup(grid::Grid.EnvGrid, densityfun, responses, inputs, modes::Modes.ModeCollection, components; full=false, norm! = NonlinearRHS.norm_modal(grid), - rtol=1e-3, atol=0.0, mfcn=512, noise_field=nothing) + rtol=1e-3, atol=0.0, mfcn=512) Logging.@info("Setting up and planning FFTs...") flush(stderr) ts = Modes.ToSpace(modes, components=components) @@ -219,7 +215,7 @@ function setup(grid::Grid.EnvGrid, densityfun, responses, inputs, FTo = FFTW.plan_fft(xo, 1, flags=settings["fftw_flag"]) transform = NonlinearRHS.TransModal(grid, ts, FTo, responses, densityfun, norm!; - rtol, atol, mfcn, full, noise_field) + rtol, atol, mfcn, full) inv(FT) # create inverse FT plans now, so wisdom is saved inv(FTo) Utils.saveFFTwisdom() @@ -241,7 +237,7 @@ function doinputs_fs!(Eωk, grid, spacegrid::Union{Hankel.QDHT,Grid.FreeGrid}, F end function setup(grid::Grid.RealGrid, q::Hankel.QDHT, - densityfun, normfun, responses, inputs; noise_field=nothing) + densityfun, normfun, responses, inputs) Logging.@info("Setting up and planning FFTs...") flush(stderr) Utils.loadFFTwisdom() @@ -252,8 +248,7 @@ function setup(grid::Grid.RealGrid, q::Hankel.QDHT, doinputs_fs!(Eωk, grid, q, FT, inputs) xo = Array{Float64}(undef, length(grid.to), length(q.r)) FTo = FFTW.plan_rfft(xo, 1, flags=settings["fftw_flag"]) - transform = NonlinearRHS.TransRadial(grid, q, FTo, responses, densityfun, normfun; - noise_field) + transform = NonlinearRHS.TransRadial(grid, q, FTo, responses, densityfun, normfun) inv(FT) # create inverse FT plans now, so wisdom is saved inv(FTo) Utils.saveFFTwisdom() @@ -263,7 +258,7 @@ function setup(grid::Grid.RealGrid, q::Hankel.QDHT, end function setup(grid::Grid.EnvGrid, q::Hankel.QDHT, - densityfun, normfun, responses, inputs; noise_field=nothing) + densityfun, normfun, responses, inputs) Logging.@info("Setting up and planning FFTs...") flush(stderr) Utils.loadFFTwisdom() @@ -274,8 +269,7 @@ function setup(grid::Grid.EnvGrid, q::Hankel.QDHT, doinputs_fs!(Eωk, grid, q, FT, inputs) xo = Array{ComplexF64}(undef, length(grid.to), length(q.r)) FTo = FFTW.plan_fft(xo, 1, flags=settings["fftw_flag"]) - transform = NonlinearRHS.TransRadial(grid, q, FTo, responses, densityfun, normfun; - noise_field) + transform = NonlinearRHS.TransRadial(grid, q, FTo, responses, densityfun, normfun) inv(FT) # create inverse FT plans now, so wisdom is saved inv(FTo) Utils.saveFFTwisdom() @@ -285,7 +279,7 @@ function setup(grid::Grid.EnvGrid, q::Hankel.QDHT, end function setup(grid::Grid.RealGrid, xygrid::Grid.FreeGrid, - densityfun, normfun, responses, inputs; noise_field=nothing) + densityfun, normfun, responses, inputs) Logging.@info("Setting up and planning FFTs...") flush(stderr) Utils.loadFFTwisdom() @@ -298,8 +292,7 @@ function setup(grid::Grid.RealGrid, xygrid::Grid.FreeGrid, xo = Array{Float64}(undef, length(grid.to), length(y), length(x)) FTo = FFTW.plan_rfft(xo, (1, 2, 3), flags=settings["fftw_flag"]) transform = NonlinearRHS.TransFree(grid, xygrid, FTo, - responses, densityfun, normfun; - noise_field) + responses, densityfun, normfun) inv(FT) # create inverse FT plans now, so wisdom is saved inv(FTo) Utils.saveFFTwisdom() @@ -309,7 +302,7 @@ function setup(grid::Grid.RealGrid, xygrid::Grid.FreeGrid, end function setup(grid::Grid.EnvGrid, xygrid::Grid.FreeGrid, - densityfun, normfun, responses, inputs; noise_field=nothing) + densityfun, normfun, responses, inputs) Logging.@info("Setting up and planning FFTs...") flush(stderr) Utils.loadFFTwisdom() @@ -322,8 +315,7 @@ function setup(grid::Grid.EnvGrid, xygrid::Grid.FreeGrid, xo = Array{ComplexF64}(undef, length(grid.to), length(y), length(x)) FTo = FFTW.plan_fft(xo, (1, 2, 3), flags=settings["fftw_flag"]) transform = NonlinearRHS.TransFree(grid, xygrid, FTo, - responses, densityfun, normfun; - noise_field) + responses, densityfun, normfun) inv(FT) # create inverse FT plans now, so wisdom is saved inv(FTo) Utils.saveFFTwisdom() diff --git a/src/NonlinearRHS.jl b/src/NonlinearRHS.jl index 722a462b6..c020b333d 100644 --- a/src/NonlinearRHS.jl +++ b/src/NonlinearRHS.jl @@ -113,12 +113,6 @@ function _cpscb_core(dest, source, N, scale, idcs) end end -# Note on noise and ionization/plasma: when the modified shot-noise model is active, -# Et_to_Pt! receives the combined field (field + noise). The noise amplitude is of order -# √(ħω·Δν) ≈ 5×10⁻⁴ √W per mode — roughly 10⁻¹⁴ of typical pulse peak power. This is -# completely negligible for the highly nonlinear ionization rate and plasma -# response, so including noise in the field passed to all response functions is physically -# reasonable. The noise meaningfully affects only Kerr and Raman processes, as intended. """ Et_to_Pt!(Pt, Et, responses, density) @@ -149,17 +143,9 @@ end """ TransModal -Transform E(ω) -> Pₙₗ(ω) for multimode propagation via spatial integration. - -# Fields -- `Emω_noise`: modal noise field `(nω, nmodes)` for the modified shot-noise model, or - `nothing`. When present, the noise is projected to real space at each integration point - and combined with the field in a separate buffer (`Er_nl`) for nonlinear evaluation. - The propagating field (`Er`) is never modified. -- `Er_noise`: preallocated buffer for the real-space time-domain noise, same shape as `Er`. -- `Er_nl`: preallocated buffer for the combined field + noise, passed to `Et_to_Pt!`. +Transform E(ω) -> Pₙₗ(ω) for modal fields. """ -mutable struct TransModal{tsT, lT, TT, FTT, rT, gT, dT, ddT, nT, eT, enT, enlT} +mutable struct TransModal{tsT, lT, TT, FTT, rT, gT, dT, ddT, nT} ts::tsT full::Bool dimlimits::lT @@ -183,9 +169,6 @@ mutable struct TransModal{tsT, lT, TT, FTT, rT, gT, dT, ddT, nT, eT, enT, enlT} atol::Float64 mfcn::Int err::Array{ComplexF64,2} - Emω_noise::eT # modal noise field for modified shot-noise model, or nothing - Er_noise::enT # buffer for real-space time-domain noise, or nothing - Er_nl::enlT # buffer for field+noise passed to Et_to_Pt!, or nothing end function show(io::IO, t::TransModal) @@ -201,7 +184,7 @@ function show(io::IO, t::TransModal) end """ - TransModal(grid, ts, FT, resp, densityfun, norm!; rtol=1e-3, atol=0.0, mfcn=300, full=false, noise_field=nothing) + TransModal(grid, ts, FT, resp, densityfun, norm!; rtol=1e-3, atol=0.0, mfcn=300, full=false) Construct a `TransModal`, transform E(ω) -> Pₙₗ(ω) for modal fields. @@ -216,12 +199,9 @@ Construct a `TransModal`, transform E(ω) -> Pₙₗ(ω) for modal fields. - `atol::Float=0.0` : absolute tolerance on the `HCubature` integration - `mfcn::Int=512` : maximum number of function evaluations for one modal integration - `full::Bool=false` : if `true`, use full 2-D mode integral, if `false`, only do radial integral -- `noise_field=nothing` : optional `(nω, nmodes)` noise field for the modified shot-noise - model. Each mode column should contain independent noise with the one-photon-per-mode - spectral density. Generate with [`Fields.generate_noise_field`](@ref Luna.Fields.generate_noise_field). """ function TransModal(tT, grid, ts::Modes.ToSpace, FT, resp, densityfun, norm!; - rtol=1e-3, atol=0.0, mfcn=512, full=false, noise_field=nothing) + rtol=1e-3, atol=0.0, mfcn=512, full=false) Emω = Array{ComplexF64,2}(undef, length(grid.ω), ts.nmodes) Erω = Array{ComplexF64,2}(undef, length(grid.ω), ts.npol) Erωo = Array{ComplexF64,2}(undef, length(grid.ωo), ts.npol) @@ -231,21 +211,8 @@ function TransModal(tT, grid, ts::Modes.ToSpace, FT, resp, densityfun, norm!; Prωo = Array{ComplexF64,2}(undef, length(grid.ωo), ts.npol) Prmω = Array{ComplexF64,2}(undef, length(grid.ω), ts.nmodes) IFT = inv(FT) - # For the modified shot-noise model, store the modal noise field and allocate a buffer - # for the real-space time-domain noise. The noise is projected to space at each - # integration point in Erω_to_Prω!, so we store it in the modal domain. - if !isnothing(noise_field) - Emω_noise = copy(noise_field) - Er_noise = Array{tT,2}(undef, length(grid.to), ts.npol) - Er_nl = Array{tT,2}(undef, length(grid.to), ts.npol) - else - Emω_noise = nothing - Er_noise = nothing - Er_nl = nothing - end TransModal(ts, full, Modes.dimlimits(ts.ms[1]), Emω, Erω, Erωo, Er, Pr, Prω, Prωo, Prmω, - FT, resp, grid, densityfun, densityfun(0.0), norm!, 0, 0.0, rtol, atol, mfcn, - similar(Prmω), Emω_noise, Er_noise, Er_nl) + FT, resp, grid, densityfun, densityfun(0.0), norm!, 0, 0.0, rtol, atol, mfcn, similar(Prmω)) end function TransModal(grid::Grid.RealGrid, args...; kwargs...) @@ -306,17 +273,8 @@ end function Erω_to_Prω!(t, x) Modes.to_space!(t.Erω, t.Emω, x, t.ts, z=t.z) to_time!(t.Er, t.Erω, t.Erωo, inv(t.FT)) - # Modified shot-noise model: project noise modes to real space at this spatial point, - # convert to oversampled time domain, and combine with field in a separate buffer (Er_nl) - # so the propagating field (Er) is never contaminated. - if !isnothing(t.Emω_noise) - Modes.to_space!(t.Erω, t.Emω_noise, x, t.ts, z=t.z) - to_time!(t.Er_noise, t.Erω, t.Erωo, inv(t.FT)) - @. t.Er_nl = t.Er + t.Er_noise - Et_to_Pt!(t.Pr, t.Er_nl, t.resp, t.density) - else - Et_to_Pt!(t.Pr, t.Er, t.resp, t.density) - end + # get nonlinear pol at r,θ + Et_to_Pt!(t.Pr, t.Er, t.resp, t.density) @. t.Pr *= t.grid.towin to_freq!(t.Prω, t.Prωo, t.Pr, t.FT) @. t.Prω *= t.grid.ωwin @@ -361,15 +319,8 @@ end TransModeAvg Transform E(ω) -> Pₙₗ(ω) for mode-averaged single-mode propagation. - -# Fields -- `Et_noise`: precomputed time-domain noise on the oversampled grid for the modified - shot-noise model (Chen & Wise, arXiv:2410.20567), or `nothing` for the traditional model. -- `Et_nl`: preallocated buffer for the combined field + noise. When `Et_noise` is present, - `Et_nl = Eto + Et_noise` is computed at each step and passed to `Et_to_Pt!`. The - propagating field (`Eto`) is never modified; dispersion acts only on the physical field. """ -struct TransModeAvg{TT, FTT, rT, gT, dT, nT, aT, eT, nlT} +struct TransModeAvg{TT, FTT, rT, gT, dT, nT, aT} Pto::Vector{TT} Eto::Vector{TT} Eωo::Vector{ComplexF64} @@ -380,8 +331,6 @@ struct TransModeAvg{TT, FTT, rT, gT, dT, nT, aT, eT, nlT} densityfun::dT norm!::nT aeff::aT # function which returns effective area - Et_noise::eT # time-domain noise for modified shot-noise model, or nothing - Et_nl::nlT # buffer for field+noise passed to Et_to_Pt!, or nothing end function show(io::IO, t::TransModeAvg) @@ -392,60 +341,28 @@ function show(io::IO, t::TransModeAvg) print(io, out) end -""" - TransModeAvg(TT, grid, FT, resp, densityfun, norm!, aeff; noise_field=nothing) - -Construct a `TransModeAvg` transform for mode-averaged propagation. - -# Keyword arguments -- `noise_field=nothing`: optional frequency-domain noise field (on the normal grid) for the - modified shot-noise model. When provided, it is converted to the oversampled time grid and - stored as `Et_noise` for injection into the nonlinear operator at every propagation step. - Generate with [`Fields.generate_noise_field`](@ref Luna.Fields.generate_noise_field). -""" -function TransModeAvg(TT, grid, FT, resp, densityfun, norm!, aeff; noise_field=nothing) +function TransModeAvg(TT, grid, FT, resp, densityfun, norm!, aeff) Eωo = zeros(ComplexF64, length(grid.ωo)) Eto = zeros(TT, length(grid.to)) Pto = similar(Eto) Pωo = similar(Eωo) - # Precompute time-domain noise on the oversampled grid if noise_field is provided. - # Uses the same ω→t conversion path as to_time!: copy_scale! into oversampled spectral - # array, then inverse FFT. The result is constant throughout propagation. - if !isnothing(noise_field) - Eωo_noise = zeros(ComplexF64, length(grid.ωo)) - Et_noise = zeros(TT, length(grid.to)) - to_time!(Et_noise, noise_field, Eωo_noise, inv(FT)) - Et_nl = zeros(TT, length(grid.to)) - else - Et_noise = nothing - Et_nl = nothing - end - TransModeAvg(Pto, Eto, Eωo, Pωo, FT, resp, grid, densityfun, norm!, aeff, Et_noise, Et_nl) + TransModeAvg(Pto, Eto, Eωo, Pωo, FT, resp, grid, densityfun, norm!, aeff) end -function TransModeAvg(grid::Grid.RealGrid, FT, resp, densityfun, norm!, aeff; kwargs...) - TransModeAvg(Float64, grid, FT, resp, densityfun, norm!, aeff; kwargs...) +function TransModeAvg(grid::Grid.RealGrid, FT, resp, densityfun, norm!, aeff) + TransModeAvg(Float64, grid, FT, resp, densityfun, norm!, aeff) end -function TransModeAvg(grid::Grid.EnvGrid, FT, resp, densityfun, norm!, aeff; kwargs...) - TransModeAvg(ComplexF64, grid, FT, resp, densityfun, norm!, aeff; kwargs...) +function TransModeAvg(grid::Grid.EnvGrid, FT, resp, densityfun, norm!, aeff) + TransModeAvg(ComplexF64, grid, FT, resp, densityfun, norm!, aeff) end const nlscale = sqrt(PhysData.ε_0*PhysData.c/2) function (t::TransModeAvg)(nl, Eω, z) to_time!(t.Eto, Eω, t.Eωo, inv(t.FT)) - sc = nlscale*sqrt(t.aeff(z)) - @. t.Eto /= sc - # Modified shot-noise model: compute field+noise in a separate buffer (Et_nl) so that - # the propagating field (Eto) is never contaminated. The noise is scaled by the same - # normalisation factor (nlscale × √Aeff) so it enters in physical units. - if !isnothing(t.Et_noise) - @. t.Et_nl = t.Eto + t.Et_noise / sc - Et_to_Pt!(t.Pto, t.Et_nl, t.resp, t.densityfun(z)) - else - Et_to_Pt!(t.Pto, t.Eto, t.resp, t.densityfun(z)) - end + @. t.Eto /= nlscale*sqrt(t.aeff(z)) + Et_to_Pt!(t.Pto, t.Eto, t.resp, t.densityfun(z)) @. t.Pto *= t.grid.towin to_freq!(nl, t.Pωo, t.Pto, t.FT) t.norm!(nl, z) @@ -484,15 +401,9 @@ end """ TransRadial -Transform E(ω) -> Pₙₗ(ω) for radially symmetric free-space propagation. - -# Fields -- `Et_noise`: precomputed time-domain noise on the oversampled real-space grid `(nto, nr)` - for the modified shot-noise model, or `nothing`. -- `Et_nl`: preallocated buffer for the combined field + noise, passed to `Et_to_Pt!`. The - propagating field (`Eto`) is never modified. +Transform E(ω) -> Pₙₗ(ω) for radially symetric free-space propagation """ -struct TransRadial{TT, HTT, FTT, nT, rT, gT, dT, iT, eT, nlT} +struct TransRadial{TT, HTT, FTT, nT, rT, gT, dT, iT} QDHT::HTT # Hankel transform (space to k-space) FT::FTT # Fourier transform (time to frequency) normfun::nT # Function which returns normalisation factor @@ -504,8 +415,6 @@ struct TransRadial{TT, HTT, FTT, nT, rT, gT, dT, iT, eT, nlT} Eωo::Array{ComplexF64,2} # Buffer array for field on oversampled frequency grid Pωo::Array{ComplexF64,2} # Buffer array for NL polarisation on oversampled frequency grid idcs::iT # CartesianIndices for Et_to_Pt! to iterate over - Et_noise::eT # time-domain noise for modified shot-noise model, or nothing - Et_nl::nlT # buffer for field+noise passed to Et_to_Pt!, or nothing end function show(io::IO, t::TransRadial) @@ -518,43 +427,34 @@ function show(io::IO, t::TransRadial) print(io, out) end -""" - TransRadial(TT, grid, HT, FT, responses, densityfun, normfun; noise_field=nothing) - -Construct a `TransRadial` to calculate the reciprocal-domain nonlinear polarisation. - -# Keyword arguments -- `noise_field=nothing`: optional `(nω, nk)` frequency/k-space noise field for the modified - shot-noise model. When provided, it is converted to the real-space time domain `(nto, nr)` - via inverse FFT and inverse Hankel transform, and stored as `Et_noise`. - Generate with [`Fields.generate_noise_field`](@ref Luna.Fields.generate_noise_field). -""" -function TransRadial(TT, grid, HT, FT, responses, densityfun, normfun; noise_field=nothing) +function TransRadial(TT, grid, HT, FT, responses, densityfun, normfun) Eωo = zeros(ComplexF64, (length(grid.ωo), HT.N)) Eto = zeros(TT, (length(grid.to), HT.N)) Pto = similar(Eto) Pωo = similar(Eωo) idcs = CartesianIndices(size(Pto)[2:end]) - # Precompute time-domain noise in real space: ω→t via to_time!, then k→r via QDHT⁻¹ - if !isnothing(noise_field) - Eωo_noise = zeros(ComplexF64, (length(grid.ωo), HT.N)) - Et_noise = zeros(TT, (length(grid.to), HT.N)) - to_time!(Et_noise, noise_field, Eωo_noise, inv(FT)) - ldiv!(Et_noise, HT, Et_noise) - Et_nl = zeros(TT, (length(grid.to), HT.N)) - else - Et_noise = nothing - Et_nl = nothing - end - TransRadial(HT, FT, normfun, responses, grid, densityfun, Pto, Eto, Eωo, Pωo, idcs, Et_noise, Et_nl) + TransRadial(HT, FT, normfun, responses, grid, densityfun, Pto, Eto, Eωo, Pωo, idcs) end -function TransRadial(grid::Grid.RealGrid, args...; kwargs...) - TransRadial(Float64, grid, args...; kwargs...) +""" + TransRadial(grid, HT, FT, responses, densityfun, normfun) + +Construct a `TransRadial` to calculate the reciprocal-domain nonlinear polarisation. + +# Arguments +- `grid::AbstractGrid` : the grid used in the simulation +- `HT::QDHT` : the Hankel transform which defines the spatial grid +- `FT::FFTW.Plan` : the time-frequency Fourier transform for the oversampled time grid +- `responses` : `Tuple` of response functions +- `densityfun` : callable which returns the gas density as a function of `z` +- `normfun` : normalisation factor as fctn of `z`, can be created via [`norm_radial`](@ref) +""" +function TransRadial(grid::Grid.RealGrid, args...) + TransRadial(Float64, grid, args...) end -function TransRadial(grid::Grid.EnvGrid, args...; kwargs...) - TransRadial(ComplexF64, grid, args...; kwargs...) +function TransRadial(grid::Grid.EnvGrid, args...) + TransRadial(ComplexF64, grid, args...) end """ @@ -566,14 +466,7 @@ place the result in `nl` function (t::TransRadial)(nl, Eω, z) to_time!(t.Eto, Eω, t.Eωo, inv(t.FT)) # transform ω -> t ldiv!(t.Eto, t.QDHT, t.Eto) # transform k -> r - # Modified shot-noise: compute field+noise in separate buffer (Et_nl) so the - # propagating field (Eto) is never contaminated. - if !isnothing(t.Et_noise) - @. t.Et_nl = t.Eto + t.Et_noise - Et_to_Pt!(t.Pto, t.Et_nl, t.resp, t.densityfun(z), t.idcs) - else - Et_to_Pt!(t.Pto, t.Eto, t.resp, t.densityfun(z), t.idcs) - end + Et_to_Pt!(t.Pto, t.Eto, t.resp, t.densityfun(z), t.idcs) # add up responses @. t.Pto *= t.grid.towin # apodisation mul!(t.Pto, t.QDHT, t.Pto) # transform r -> k to_freq!(nl, t.Pωo, t.Pto, t.FT) # transform t -> ω @@ -630,18 +523,7 @@ function norm_radial(grid, q, nfun) return norm end -""" - TransFree - -Transform E(ω) -> Pₙₗ(ω) for 3D free-space propagation. - -# Fields -- `Et_noise`: precomputed time-domain noise on the oversampled real-space grid `(nto, ny, nx)` - for the modified shot-noise model, or `nothing`. -- `Et_nl`: preallocated buffer for the combined field + noise, passed to `Et_to_Pt!`. The - propagating field (`Eto`) is never modified. -""" -mutable struct TransFree{TT, FTT, nT, rT, gT, xygT, dT, iT, eT, nlT} +mutable struct TransFree{TT, FTT, nT, rT, gT, xygT, dT, iT} FT::FTT # 3D Fourier transform (space to k-space and time to frequency) normfun::nT # Function which returns normalisation factor resp::rT # nonlinear responses (tuple of callables) @@ -654,8 +536,6 @@ mutable struct TransFree{TT, FTT, nT, rT, gT, xygT, dT, iT, eT, nlT} Pωo::Array{ComplexF64, 3} # buffer for oversampled frequency-domain NL polarisation scale::Float64 # scale factor to be applied during oversampling idcs::iT # iterating over these slices Eto/Pto into Vectors, one at each position - Et_noise::eT # time-domain noise for modified shot-noise model, or nothing - Et_nl::nlT # buffer for field+noise passed to Et_to_Pt!, or nothing end function show(io::IO, t::TransFree) @@ -668,20 +548,7 @@ function show(io::IO, t::TransFree) print(io, out) end -""" - TransFree(TT, scale, grid, xygrid, FT, responses, densityfun, normfun; noise_field=nothing) - -Construct a `TransFree` to calculate the reciprocal-domain nonlinear polarisation for 3D -free-space propagation. - -# Keyword arguments -- `noise_field=nothing`: optional `(nω, ny, nx)` frequency/k-space noise field for the - modified shot-noise model. When provided, it is converted to the real-space oversampled - time domain `(nto, ny, nx)` via `copy_scale!` and 3D inverse FFT, and stored as `Et_noise`. - Generate with [`Fields.generate_noise_field`](@ref Luna.Fields.generate_noise_field). -""" -function TransFree(TT, scale, grid, xygrid, FT, responses, densityfun, normfun; - noise_field=nothing) +function TransFree(TT, scale, grid, xygrid, FT, responses, densityfun, normfun) Ny = length(xygrid.y) Nx = length(xygrid.x) Eωo = zeros(ComplexF64, (length(grid.ωo), Ny, Nx)) @@ -689,35 +556,35 @@ function TransFree(TT, scale, grid, xygrid, FT, responses, densityfun, normfun; Pto = similar(Eto) Pωo = similar(Eωo) idcs = CartesianIndices((Ny, Nx)) - # Precompute time-domain noise in real space: - # copy_scale! into oversampled spectral grid, then 3D IFFT: (ω,ky,kx) → (t,y,x) - if !isnothing(noise_field) - Eωo_noise = zeros(ComplexF64, (length(grid.ωo), Ny, Nx)) - N = length(grid.ω) - copy_scale!(Eωo_noise, noise_field, N, scale) - Et_noise = zeros(TT, (length(grid.to), Ny, Nx)) - ldiv!(Et_noise, FT, Eωo_noise) - Et_nl = zeros(TT, (length(grid.to), Ny, Nx)) - else - Et_noise = nothing - Et_nl = nothing - end TransFree(FT, normfun, responses, grid, xygrid, densityfun, - Pto, Eto, Eωo, Pωo, scale, idcs, Et_noise, Et_nl) + Pto, Eto, Eωo, Pωo, scale, idcs) end -function TransFree(grid::Grid.RealGrid, args...; kwargs...) +""" + TransFree(grid, xygrid, FT, responses, densityfun, normfun) + +Construct a `TransFree` to calculate the reciprocal-domain nonlinear polarisation. + +# Arguments +- `grid::AbstractGrid` : the grid used in the simulation +- `xygrid` : the spatial grid (instances of [`Grid.FreeGrid`](@ref)) +- `FT::FFTW.Plan` : the full 3D (t-y-x) Fourier transform for the oversampled time grid +- `responses` : `Tuple` of response functions +- `densityfun` : callable which returns the gas density as a function of `z` +- `normfun` : normalisation factor as fctn of `z`, can be created via [`norm_free`](@ref) +""" +function TransFree(grid::Grid.RealGrid, args...) N = length(grid.ω) No = length(grid.ωo) scale = (No-1)/(N-1) - TransFree(Float64, scale, grid, args...; kwargs...) + TransFree(Float64, scale, grid, args...) end -function TransFree(grid::Grid.EnvGrid, args...; kwargs...) +function TransFree(grid::Grid.EnvGrid, args...) N = length(grid.ω) No = length(grid.ωo) scale = No/N - TransFree(ComplexF64, scale, grid, args...; kwargs...) + TransFree(ComplexF64, scale, grid, args...) end """ @@ -730,14 +597,7 @@ function (t::TransFree)(nl, Eωk, z) fill!(t.Eωo, 0) copy_scale!(t.Eωo, Eωk, length(t.grid.ω), t.scale) ldiv!(t.Eto, t.FT, t.Eωo) # transform (ω, ky, kx) -> (t, y, x) - # Modified shot-noise: compute field+noise in separate buffer (Et_nl) so the - # propagating field (Eto) is never contaminated. - if !isnothing(t.Et_noise) - @. t.Et_nl = t.Eto + t.Et_noise - Et_to_Pt!(t.Pto, t.Et_nl, t.resp, t.densityfun(z), t.idcs) - else - Et_to_Pt!(t.Pto, t.Eto, t.resp, t.densityfun(z), t.idcs) - end + Et_to_Pt!(t.Pto, t.Eto, t.resp, t.densityfun(z), t.idcs) # add up responses @. t.Pto *= t.grid.towin # apodisation mul!(t.Pωo, t.FT, t.Pto) # transform (t, y, x) -> (ω, ky, kx) copy_scale!(nl, t.Pωo, length(t.grid.ω), 1/t.scale) diff --git a/test/runtests.jl b/test/runtests.jl index 48ac8dd9c..a2552f24c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -157,9 +157,4 @@ end include(joinpath(testdir, "test_gnlse.jl")) end -@testset "Noise model" begin - @info("================= test_noise.jl") - include(joinpath(testdir, "test_noise.jl")) -end - end \ No newline at end of file diff --git a/test/test_interface.jl b/test/test_interface.jl index f5276b8fd..e5e6c293f 100644 --- a/test/test_interface.jl +++ b/test/test_interface.jl @@ -3,6 +3,7 @@ import Luna.Capillary: besselj, get_unm import Luna.Modes: hquadrature import Test: @test, @testset, @test_throws import Logging +import Random logger = Logging.SimpleLogger(stdout, Logging.Warn) old_logger = Logging.global_logger(logger) @@ -435,5 +436,17 @@ end @test Pto300 == Pto300_2 end +@testset "shot noise rng" begin + args = (1.0, 0.1, [-1e-26]) # γ, flength, βs + kw = (λ0=800e-9, λlims=(400e-9, 2000e-9), trange=1e-12, τfwhm=30e-15, energy=1e-9) + Eω1 = Interface.prop_gnlse_args(args...; kw..., rng=Random.MersenneTwister(1))[1] + Eω2 = Interface.prop_gnlse_args(args...; kw..., rng=Random.MersenneTwister(1))[1] + Eω3 = Interface.prop_gnlse_args(args...; kw..., rng=Random.MersenneTwister(2))[1] + Eω0 = Interface.prop_gnlse_args(args...; kw..., shotnoise=false)[1] + @test Eω1 == Eω2 + @test Eω1 != Eω3 + @test Eω1 != Eω0 +end + ## Logging.global_logger(old_logger) diff --git a/test/test_noise.jl b/test/test_noise.jl deleted file mode 100644 index a3715b342..000000000 --- a/test/test_noise.jl +++ /dev/null @@ -1,265 +0,0 @@ -using Luna -import Test: @test, @testset, @test_throws -import Luna: Fields, Grid, Processing -import Random -import Logging - -logger = Logging.SimpleLogger(stdout, Logging.Warn) -old_logger = Logging.global_logger(logger) - -# Small parameters for fast tests (matching test_interface.jl patterns) -const noise_args = (100e-6, 0.1, :He, 1) # a, flength, gas, pressure -const noise_kwargs = (λ0=800e-9, τfwhm=10e-15, energy=1e-12, - trange=400e-15, λlims=(200e-9, 4e-6)) - -@testset "generate_noise_field" begin - @testset "RealGrid" begin - grid = Grid.RealGrid(0.1, 800e-9, (200e-9, 4e-6), 400e-15) - nf = Fields.generate_noise_field(grid) - @test size(nf) == size(grid.ω) - @test eltype(nf) == ComplexF64 - @test any(!iszero, nf) - # Different seeds produce different noise - rng1 = Random.MersenneTwister(42) - rng2 = Random.MersenneTwister(123) - nf1 = Fields.generate_noise_field(grid; rng=rng1) - nf2 = Fields.generate_noise_field(grid; rng=rng2) - @test nf1 != nf2 - # Same seed reproduces identical noise - rng3 = Random.MersenneTwister(42) - nf3 = Fields.generate_noise_field(grid; rng=rng3) - @test nf1 == nf3 - end - @testset "EnvGrid" begin - grid = Grid.EnvGrid(0.1, 800e-9, (200e-9, 4e-6), 400e-15) - nf = Fields.generate_noise_field(grid) - @test size(nf) == size(grid.ω) - @test eltype(nf) == ComplexF64 - # Non-signal bins should be zero - @test all(iszero, nf[.!grid.sidx]) - @test any(!iszero, nf[grid.sidx]) - end - @testset "Amplitude matches ShotNoise (one-photon-per-mode)" begin - # Chen & Wise Eq. A19: |A_noise(ω)| = √(hν·Δν) - # generate_noise_field and ShotNoise use the same formula, so their - # amplitudes must be identical (phases differ only by RNG seed). - grid_r = Grid.RealGrid(0.1, 800e-9, (200e-9, 4e-6), 400e-15) - sn = Fields.ShotNoise(Random.MersenneTwister(42)) - snf = sn(grid_r) - nf = Fields.generate_noise_field(grid_r; rng=Random.MersenneTwister(42)) - @test abs.(nf) ≈ abs.(snf) - - grid_e = Grid.EnvGrid(0.1, 800e-9, (200e-9, 4e-6), 400e-15) - sn_e = Fields.ShotNoise(Random.MersenneTwister(99)) - snf_e = sn_e(grid_e) - nf_e = Fields.generate_noise_field(grid_e; rng=Random.MersenneTwister(99)) - @test abs.(nf_e) ≈ abs.(snf_e) - end - @testset "Multimode RealGrid" begin - grid = Grid.RealGrid(0.1, 800e-9, (200e-9, 4e-6), 400e-15) - nm = 3 - nf = Fields.generate_noise_field(grid; nmodes=nm) - # Shape: (nω, nmodes) - @test size(nf) == (length(grid.ω), nm) - @test eltype(nf) == ComplexF64 - # All columns non-zero - @test all(any(!iszero, nf[:, j]) for j in 1:nm) - # Columns are independent (different random phases) - @test nf[:, 1] != nf[:, 2] - @test nf[:, 2] != nf[:, 3] - # Amplitude identical across modes (same one-photon-per-mode formula) - @test abs.(nf[:, 1]) ≈ abs.(nf[:, 2]) - @test abs.(nf[:, 2]) ≈ abs.(nf[:, 3]) - # Reproducible with same seed - rng1 = Random.MersenneTwister(42) - rng2 = Random.MersenneTwister(42) - @test Fields.generate_noise_field(grid; rng=rng1, nmodes=nm) == - Fields.generate_noise_field(grid; rng=rng2, nmodes=nm) - # nmodes=1 still returns 1D array (no regression) - nf1 = Fields.generate_noise_field(grid; nmodes=1) - @test ndims(nf1) == 1 - end - @testset "Multimode EnvGrid" begin - grid = Grid.EnvGrid(0.1, 800e-9, (200e-9, 4e-6), 400e-15) - nm = 3 - nf = Fields.generate_noise_field(grid; nmodes=nm) - @test size(nf) == (length(grid.ω), nm) - @test eltype(nf) == ComplexF64 - # Non-signal bins should be zero in all modes - for j in 1:nm - @test all(iszero, nf[.!grid.sidx, j]) - @test any(!iszero, nf[grid.sidx, j]) - end - # Columns are independent - @test nf[:, 1] != nf[:, 2] - # Amplitude identical across modes - @test abs.(nf[:, 1]) ≈ abs.(nf[:, 2]) - end -end - -@testset "Multimode modified noise integration" begin - # prop_capillary with modes=2 and shotnoise=:modified exercises the - # multimode noise path (nmodes > 1) in generate_noise_field. - out_mm = prop_capillary(noise_args...; noise_kwargs..., - shotnoise=:modified, modes=2, - rng=Random.MersenneTwister(42)) - # Reproducibility: same seed → identical output - out_mm2 = prop_capillary(noise_args...; noise_kwargs..., - shotnoise=:modified, modes=2, - rng=Random.MersenneTwister(42)) - @test out_mm["Eω"] == out_mm2["Eω"] - # Clean input: z=0 field is identical to a no-noise run - ref_mm = prop_capillary(noise_args...; noise_kwargs..., - shotnoise=false, modes=2) - @test out_mm["Eω"][:, :, 1] == ref_mm["Eω"][:, :, 1] -end - -@testset "Invalid shotnoise" begin - @test_throws DomainError prop_capillary( - noise_args...; noise_kwargs..., shotnoise=:invalid) -end - -@testset "Clean input spectrum" begin - # With shotnoise=:modified, the field at z=0 should be identical to a no-noise run - # because noise only enters the nonlinear operator, not the initial field. - ref = prop_capillary(noise_args...; noise_kwargs..., shotnoise=false) - mod = prop_capillary(noise_args...; noise_kwargs..., - shotnoise=:modified, - rng=Random.MersenneTwister(42)) - @test ref["Eω"][:, 1] == mod["Eω"][:, 1] -end - -@testset "Energy conservation" begin - # Output energy with modified noise model should closely match a no-noise reference - # (noise energy is negligible: ~ħω per bin) - ref = prop_capillary(noise_args...; noise_kwargs..., shotnoise=false) - mod = prop_capillary(noise_args...; noise_kwargs..., - shotnoise=:modified, - rng=Random.MersenneTwister(42)) - eref = Processing.energy(ref)[end] - emod = Processing.energy(mod)[end] - @test isapprox(emod, eref; rtol=0.01) -end - -@testset "Backward compatibility" begin - # shotnoise=false should produce identical results regardless of how it's - # specified (Bool false or explicit shotnoise=false) - out1 = prop_capillary(noise_args...; noise_kwargs..., shotnoise=false) - out2 = prop_capillary(noise_args...; noise_kwargs..., - shotnoise=false) - @test out1["Eω"] == out2["Eω"] -end - -@testset "Reproducibility with fixed seed" begin - # The noise field is generated once before propagation and held constant - # (Chen & Wise §A2: z-dependent random noise gives step-size-dependent results). - # Same seed must produce bit-identical output. - out1 = prop_capillary(noise_args...; noise_kwargs..., - shotnoise=:modified, - rng=Random.MersenneTwister(42)) - out2 = prop_capillary(noise_args...; noise_kwargs..., - shotnoise=:modified, - rng=Random.MersenneTwister(42)) - @test out1["Eω"] == out2["Eω"] -end - -# Physics tests: noise model behaviour with H₂ Raman scattering. -# H₂ auto-enables Raman. The key physical signature of the modified noise model is that -# noise enters the nonlinear operator (not the input field), so: -# - The Stokes spectral region has NO elevated noise floor at z=0 (unlike traditional) -# - The traditional model adds one-photon-per-mode noise to the input, creating a -# measurable noise floor across all frequencies at z=0 -# We use a Stokes band far from the pump (1100-1500 nm) to avoid the input pulse's -# spectral tails, and moderate parameters where the gain is low enough that the -# noise floor difference is the dominant observable effect. -const raman_args = (100e-6, 0.3, :H2, 3) # 100 μm, 30 cm, H₂ at 3 bar -const raman_kwargs = (λ0=800e-9, τfwhm=30e-15, energy=5e-6, - trange=1000e-15, λlims=(200e-9, 4e-6), - kerr=false, plasma=false, saveN=51) -const stokes_band = (1100e-9, 1500e-9) # vibrational Stokes region, far from pump -const anti_stokes_band = (500e-9, 680e-9) # H₂ vibrational anti-Stokes region - -@testset "Noise model physics with Raman" begin - # Modified noise model: no noise floor at z=0 in the Stokes region. - mod = prop_capillary(raman_args...; raman_kwargs..., - shotnoise=:modified, - rng=Random.MersenneTwister(42)) - E_stokes_mod = Processing.energy(mod; bandpass=stokes_band) - - # Traditional shot noise: one-photon-per-mode noise is added to the input field, - # creating a measurable noise floor across all frequencies at z=0. - trad = prop_capillary(raman_args...; raman_kwargs..., - shotnoise=:input) - E_stokes_trad = Processing.energy(trad; bandpass=stokes_band) - - # Key physics check: the traditional model has an elevated noise floor at z=0 - # that is many orders of magnitude above the modified model's clean spectrum. - @test E_stokes_trad[1] > 1e6 * E_stokes_mod[1] - - # Modified model: clean input spectrum (negligible energy in Stokes band at z=0) - @test E_stokes_mod[1] < 1e-30 - - # Both models produce similar Stokes energy at z=L (either from noise-seeded or - # stimulated Raman of the pulse's spectral tails) - @test E_stokes_mod[end] > 0 - @test E_stokes_trad[end] > 0 - - # Stokes preference over anti-Stokes (Chen & Wise main text, Fig. 1): - # The modified model correctly captures the spontaneous Raman asymmetry — - # Stokes generation grows faster than anti-Stokes from noise. - # We compare growth ratios (end/start) since the anti-Stokes band has - # residual pulse-tail energy that dominates absolute values. - E_astokes_mod = Processing.energy(mod; bandpass=anti_stokes_band) - stokes_growth = E_stokes_mod[end] / max(E_stokes_mod[1], 1e-50) - astokes_growth = E_astokes_mod[end] / max(E_astokes_mod[1], 1e-50) - @test stokes_growth > astokes_growth -end - -@testset "Noise floor Stokes vs anti-Stokes" begin - # With Kerr enabled (default), both Raman and FWM act on the noise. - # The traditional model creates an elevated noise floor at BOTH Stokes and anti-Stokes - # at z=0, while the modified model has a clean spectrum. - raman_kwargs_kerr = (λ0=800e-9, τfwhm=30e-15, energy=5e-6, - trange=1000e-15, λlims=(200e-9, 4e-6), - plasma=false, saveN=51) - - mod = prop_capillary(raman_args...; raman_kwargs_kerr..., - shotnoise=:modified, - rng=Random.MersenneTwister(42)) - E_s_mod = Processing.energy(mod; bandpass=stokes_band) - E_as_mod = Processing.energy(mod; bandpass=anti_stokes_band) - - trad = prop_capillary(raman_args...; raman_kwargs_kerr..., - shotnoise=:input) - E_s_trad = Processing.energy(trad; bandpass=stokes_band) - E_as_trad = Processing.energy(trad; bandpass=anti_stokes_band) - - # Traditional has higher Stokes noise floor at z=0 than modified - @test E_s_trad[1] > 1e6 * E_s_mod[1] - # Traditional has higher anti-Stokes noise floor at z=0 than modified - @test E_as_trad[1] > 5 * E_as_mod[1] - # Modified Stokes is clean at z=0 - @test E_s_mod[1] < 1e-30 - # Modified Stokes grows from near-zero during propagation - @test E_s_mod[end] > E_s_mod[1] - # Both models converge at output (stimulated processes dominate) - @test isapprox(E_as_mod[end], E_as_trad[end]; rtol=0.1) -end - -@testset "Stokes growth with modified noise" begin - # The modified model shows Stokes energy growing by many orders of magnitude - # from the noise-seeded spontaneous Raman process. This confirms that the - # noise successfully seeds Raman gain even though it enters only the nonlinear - # operator (not the input field). - mod = prop_capillary(raman_args...; raman_kwargs..., - shotnoise=:modified, - rng=Random.MersenneTwister(42)) - E_s = Processing.energy(mod; bandpass=stokes_band) - - # Large growth factor from noise-seeded spontaneous Raman - @test E_s[end] / E_s[1] > 1e10 - # Monotonic growth in the last ~10 z-points (exponential gain regime) - @test all(diff(E_s[end-9:end]) .> 0) -end - -Logging.global_logger(old_logger)