From 626fcd1beebf472487f11cd588661e2d5303de7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefanos=20Carlstr=C3=B6m?= Date: Wed, 6 Oct 2021 13:13:27 +0200 Subject: [PATCH] Fix span for sum of disjoint fields (#17) * Fix timeaxis for zero-length intervals * Fix span for sum of disjoint fields * Bump version --- Project.toml | 2 +- src/arithmetic.jl | 6 +++++- src/time_axis.jl | 7 ++++--- test/arithmetic.jl | 3 +++ test/field_types.jl | 7 +++++++ 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index fee9185..cd763e3 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ElectricFields" uuid = "2f84ce32-9bb1-11e8-0d9f-3dce90a4beca" authors = ["Stefanos Carlström "] -version = "0.1.3" +version = "0.1.4" [deps] FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341" diff --git a/src/arithmetic.jl b/src/arithmetic.jl index 3133025..bf19cee 100644 --- a/src/arithmetic.jl +++ b/src/arithmetic.jl @@ -112,7 +112,11 @@ vector_potential(f::SumField, t::Number) = polarization(f::SumField) = polarization(f.a) -span(f::SumField) = span(f.a) ∪ span(f.b) +function span(f::SumField) + sa = span(f.a) + sb = span(f.b) + min(sa.left,sb.left)..max(sa.right,sb.right) +end steps(f::SumField, ndt::Int) = steps(f, ndt/min(austrip.(period.((f.a,f.b)))...)) for fun in [:wavelength, :period, :frequency, :wavenumber, :fundamental, :photon_energy] diff --git a/src/time_axis.jl b/src/time_axis.jl index d767c09..3e94059 100644 --- a/src/time_axis.jl +++ b/src/time_axis.jl @@ -35,13 +35,14 @@ frequency `fs`. """ function timeaxis(f::AbstractField, fs::Number=default_sampling_frequency(f)) num_steps = steps(f, fs) - if num_steps > 1 - range(span(f), length=num_steps) + s = span(f) + if num_steps > 1 || s.left == s.right + range(s, length=num_steps) else # This hack is mainly useful for calculations where exactly # one time step should be taken, (b-a) becomes the step # length. - a,b = endpoints(span(f)) + a,b = endpoints(s) a:(b-a):a end end diff --git a/test/arithmetic.jl b/test/arithmetic.jl index 3e87efa..93c8f96 100644 --- a/test/arithmetic.jl +++ b/test/arithmetic.jl @@ -141,6 +141,9 @@ Z = X + delay(Y, 3/2π) @test field_amplitude(Z, 4.0) ≈ field_amplitude(X, 4.0) + field_amplitude(Y, 4.0 - 3/2π) + + W = X + delay(Y, 35.0) + @test span(W) == span(X).left..(span(Y).right+35.0) end @testset "Padded fields" begin diff --git a/test/field_types.jl b/test/field_types.jl index caee877..80e6d25 100644 --- a/test/field_types.jl +++ b/test/field_types.jl @@ -165,6 +165,13 @@ @test photon_energy(F) == 2π @test dimensions(F) == 1 + + @field(Fzero) do + I₀ = 4.0 + tmax = 0.0 + kind = :constant + end + @test timeaxis(Fzero) == range(0, stop=0, length=0) end @testset "Ramps" begin