Skip to content

Commit

Permalink
adjust regular and outgoing basis to work for fields of higher dimens…
Browse files Browse the repository at this point in the history
…ion as matrix multiplication
  • Loading branch information
arturgower committed Dec 29, 2023
1 parent 665ccff commit 2c0a737
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 43 deletions.
2 changes: 1 addition & 1 deletion docs/src/example/generate-READMEs.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# find julia files to be converted into README.md

function gernerate_README(examplestoconvert, folder)
function generate_README(examplestoconvert, folder)

ps = map(examplestoconvert) do str
fs = filter(s -> split(s, ".")[end] == "jl", readdir(folder*str))
Expand Down
6 changes: 3 additions & 3 deletions docs/src/example/particles_in_circle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ big_particle_simulation = FrequencySimulation([big_particle], source)
using Plots
height = 300
#gr(size=(1.4*height,height))
pyplot(leg=false, size=(1.4*height,height))
#pyplot(leg=false, size=(1.4*height,height))

ω = 0.5
#plot(big_particle_simulation, ω; res=15, bounds = box);
Expand All @@ -64,8 +64,8 @@ big_result = run(big_particle_simulation, x, ωs)

#plot(result, lab = "scattering from particles")
#plot!(big_result,
# lab = "scattering from big particle",
# title="Compare scattered wave from one big particle, \n and a circle filled with small particles")
#lab = "scattering from big particle",
#title="Compare scattered wave from one big particle, \n and a circle filled with small particles")
```
![The response comparison](plot_response_compare.png)

4 changes: 2 additions & 2 deletions src/physics/acoustics/acoustics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ function internal_field(x::AbstractVector{T}, p::Particle{Dim,Acoustic{T,Dim}},
vos = regular_radial_basis(p.medium, ω, order, r)
us = outgoing_radial_basis(source.medium, ω, order, r)

internal_coefs = (vs .* (inv(t_mat) * fs) + us .* fs) ./ vos
internal_coefs = (vs[:] .* (inv(t_mat) * fs) + us[:] .* fs) ./ vos[:]
inner_basis = regular_basis_function(p, ω)

return sum(inner_basis(order, x-origin(p)) .* internal_coefs)
return inner_basis(order, x-origin(p)) * internal_coefs
end
end
6 changes: 2 additions & 4 deletions src/physics/acoustics/concentric_capsule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function internal_field(x::AbstractArray{T}, p::CapsuleParticle{2,Acoustic{T,2},
coefs = - q0 * forces .* Ydn.(-Nh:Nh, a0*k1, a0*k1)
basis = regular_basis_function(p.inner, ω)

return sum(basis(Nh,x-origin(p)) .* coefs)
return basis(Nh,x-origin(p)) * coefs
else # calculate field in the mid region
J_coefs = forces .* [
q0*besselj(n, a0*k0)*diffhankelh1(n, a0*k1) - hankelh1(n, a0*k1)*diffbesselj(n, a0*k0)
Expand All @@ -78,8 +78,6 @@ function internal_field(x::AbstractArray{T}, p::CapsuleParticle{2,Acoustic{T,2},

J_basis = regular_basis_function(p.outer, ω)
H_basis = outgoing_basis_function(p.outer.medium, ω)
return sum(
J_basis(Nh,x-origin(p)) .* J_coefs .+ H_basis(Nh,x-origin(p)) .* H_coefs
)
return J_basis(Nh,x-origin(p)) * J_coefs + H_basis(Nh,x-origin(p)) * H_coefs
end
end
16 changes: 8 additions & 8 deletions src/physics/physical_medium.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,21 @@ basislength_to_basisorder(::Type{P},len::Int) where P <: PhysicalMedium{2,1} = I

function outgoing_radial_basis(medium::PhysicalMedium{2,1}, ω::T, order::Integer, r::T) where {T<:Number}
k = ω/medium.c
return hankelh1.(-order:order,k*r)
return transpose(hankelh1.(-order:order,k*r))
end

function outgoing_basis_function(medium::PhysicalMedium{2,1}, ω::T) where {T<:Number}
return function (order::Integer, x::AbstractVector{T})
r, θ = cartesian_to_radial_coordinates(x)
k = ω/medium.c
[hankelh1(m,k*r)*exp(im*θ*m) for m = -order:order]
[hankelh1(m,k*r)*exp(im*θ*m) for m = -order:order] |> transpose
end
end

function outgoing_radial_basis(medium::PhysicalMedium{3,1}, ω::T, order::Integer, r::T) where {T<:Number}
k = ω/medium.c
hs = shankelh1.(0:order,k*r)
return [hs[l+1] for l = 0:order for m = -l:l]
return [hs[l+1] for l = 0:order for m = -l:l] |> transpose
end

function outgoing_basis_function(medium::PhysicalMedium{3,1}, ω::T) where {T<:Number}
Expand All @@ -91,7 +91,7 @@ function outgoing_basis_function(medium::PhysicalMedium{3,1}, ω::T) where {T<:N

lm_to_n = lm_to_spherical_harmonic_index

return [hs[l+1] * Ys[lm_to_n(l,m)] for l = 0:order for m = -l:l]
return [hs[l+1] * Ys[lm_to_n(l,m)] for l = 0:order for m = -l:l] |> transpose
end
end

Expand Down Expand Up @@ -142,23 +142,23 @@ regular_basis_function(medium::PhysicalMedium{Dim,1}, ω::Union{T,Complex{T}}) w

function regular_radial_basis(medium::PhysicalMedium{2,1}, ω::T, order::Integer, r::T) where {T<:Number}
k = ω / medium.c
return besselj.(-order:order,k*r)
return transpose(besselj.(-order:order,k*r))
end

function regular_basis_function(wavenumber::Union{T,Complex{T}}, ::PhysicalMedium{2,1}) where T
return function (order::Integer, x::AbstractVector{T})
r, θ = cartesian_to_radial_coordinates(x)
k = wavenumber

return [besselj(m,k*r)*exp(im*θ*m) for m = -order:order]
return [besselj(m,k*r)*exp(im*θ*m) for m = -order:order] |> transpose
end
end

function regular_radial_basis(medium::PhysicalMedium{3,1}, ω::T, order::Integer, r::T) where {T<:Number}
k = ω / medium.c
js = sbesselj.(0:order,k*r)

return [js[l+1] for l = 0:order for m = -l:l]
return [js[l+1] for l = 0:order for m = -l:l] |> transpose
end

function regular_basis_function(wavenumber::Union{T,Complex{T}}, ::PhysicalMedium{3,1}) where T
Expand All @@ -170,7 +170,7 @@ function regular_basis_function(wavenumber::Union{T,Complex{T}}, ::PhysicalMediu

lm_to_n = lm_to_spherical_harmonic_index

return [js[l+1] * Ys[lm_to_n(l,m)] for l = 0:order for m = -l:l]
return [js[l+1] * Ys[lm_to_n(l,m)] for l = 0:order for m = -l:l] |> transpose
end
end

Expand Down
34 changes: 17 additions & 17 deletions src/physics/special_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,30 @@ function diffsbessel(f::Function,n::Number,z::Number)
return f(n-1,z) - (n+1) * f(n,z) / z
end

"""
diffsbessel(f::Function,m,x,n::Int)
# """
# diffsbessel(f::Function,m,x,n::Int)

Differentiates 'n' times any spherical Bessel function 'f' of order 'm' and at the argument 'x'. Uses the formula
# Differentiates 'n' times any spherical Bessel function 'f' of order 'm' and at the argument 'x'. Uses the formula

``(1/z d/dz)^n (z^{m+1} f_m(z)) = z^{m-n+1} f_{m-n}``
# ``(1/z d/dz)^n (z^{m+1} f_m(z)) = z^{m-n+1} f_{m-n}``

which leads to
# which leads to

``(1/z d/dz)^{n} (z^{m+1} f_m(z)) = (1/z d/dz)^{n-1} (1/z d/dz) (z^{m+1} f_m(z))``
# ``(1/z d/dz)^{n} (z^{m+1} f_m(z)) = (1/z d/dz)^{n-1} (1/z d/dz) (z^{m+1} f_m(z))``

"""
function diffsbessel(f::Function,m::Number,z,n::Int)
if n == 0
return f(m, z)
elseif n > 0
n = n - 1
return diffsbessel(f,m,z)
# """
# function diffsbessel(f::Function,m::Number,z,n::Int)
# if n == 0
# return f(m, z)
# elseif n > 0
# n = n - 1
# return diffsbessel(f,m,z)


else
error("Can not differentiate a negative number of times")
end
end
# else
# error("Can not differentiate a negative number of times")
# end
# end

function diffsbesselj(n::Number,z::Number)
return if n == 0
Expand Down
2 changes: 1 addition & 1 deletion src/simulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ function field(sim::FrequencySimulation{Dim,P}, ω::Number, x_vec::Vector{V}, a_
function sum_basis(x)
sum(eachindex(sim.particles)) do i
p = sim.particles[i]
sum(a_vec[:,i] .* basis(N, x-origin(p)))
basis(N, x-origin(p)) * a_vec[:,i]
end
end
map(x_vec) do x
Expand Down
5 changes: 3 additions & 2 deletions src/source.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function regular_spherical_source(medium::PhysicalMedium{Dim},regular_coefficien

function source_field(x,ω)
vs = regular_basis_function(medium, ω)(coeff_order,x-position)
return amplitude * sum(vs .* regular_coefficients)
return amplitude * (vs * regular_coefficients)

end

Expand Down Expand Up @@ -171,7 +171,8 @@ function source_expand(source::AbstractSource, centre::AbstractVector{T}; basis_
vs = regular_basis_function(source.medium, ω)
regular_coefficients = regular_spherical_coefficients(source)

res = sum(regular_coefficients(basis_order,centre,ω) .* vs(basis_order, x - centre), dims = 1)[:]
res = vs(basis_order, x - centre) * regular_coefficients(basis_order,centre,ω)
# res = sum(regular_coefficients(basis_order,centre,ω) .* vs(basis_order, x - centre), dims = 1)[:]
if length(res) == 1
res = res[1]
end
Expand Down
8 changes: 4 additions & 4 deletions test/acoustic_physics_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ end
vs = regular_basis_function(medium, ω)(4*order,r)
us = outgoing_basis_function(medium, ω)(order,r + d)

@test maximum(abs.(U * vs - us) ./ abs.(us)) < 3e-6 # for linux: 4e-7
@test maximum(abs.(U * vs[:] - us[:]) ./ abs.(us[:])) < 3e-6 # for linux: 4e-7

V = regular_translation_matrix(medium, 4*order, order, ω, d)
v1s = regular_basis_function(medium, ω)(4*order,r)
v2s = regular_basis_function(medium, ω)(order,r + d)

@test maximum(abs.(V * v1s - v2s) ./ abs.(v2s)) < 1e-8 # for linux: 4e-7
@test maximum(abs.(V * v1s[:] - v2s[:]) ./ abs.(v2s[:])) < 1e-8 # for linux: 4e-7

# Test 2D outgoing translation matrix
ω = rand() + 0.1
Expand All @@ -71,13 +71,13 @@ end
vs = regular_basis_function(medium, ω)(larger_order,r)
us = outgoing_basis_function(medium, ω)(order,r + d)

@test maximum(abs.(U * vs - us) ./ abs.(us)) < 1e-9
@test maximum(abs.(U * vs[:] - us[:]) ./ abs.(us[:])) < 1e-9

V = regular_translation_matrix(medium, larger_order, order, ω, d)
v1s = regular_basis_function(medium, ω)(larger_order,r)
v2s = regular_basis_function(medium, ω)(order,r + d)

@test maximum(abs.(V * v1s - v2s) ./ abs.(v2s)) < 1e-10
@test maximum(abs.(V * v1s[:] - v2s[:]) ./ abs.(v2s[:])) < 1e-10

end

Expand Down
2 changes: 1 addition & 1 deletion test/run_examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using MultipleScattering, Test

include(folder*"generate-READMEs.jl")

gernerate_README(examplestoconvert, folder)
generate_README(examplestoconvert, folder)

@test true
end

0 comments on commit 2c0a737

Please sign in to comment.