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
114 changes: 93 additions & 21 deletions julia/src/FMM3D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module FMM3D

using FMM3D_jll

export FMMVals, hfmm3d, lfmm3d, h3ddir, l3ddir, lowerlevel_routs
export FMMVals, hfmm3d, hfmm3d_ndiv, lfmm3d, lfmm3d_ndiv, h3ddir, l3ddir, lowerlevel_routs
export stfmm3d, st3ddir, emfmm3d, em3ddir
export besseljs3d

Expand Down Expand Up @@ -262,11 +262,11 @@ in the order: ``\\partial_{xx}``, ``\\partial_{yy}``,
Non-zero values may indicate insufficient memory available. See the documentation for the FMM3D library.
If not set (`nothing`), then FMM3D library was never called.
"""
function hfmm3d(eps::Float64,zk::Union{Float64,ComplexF64},
sources::Array{Float64};
charges::TCN=nothing,dipvecs::TCN=nothing,
targets::TFN=nothing,pg::Integer=0,pgt::Integer=0,
nd::Integer=1)
function _hfmm3d_impl(eps::Float64,zk::Union{Float64,ComplexF64},
sources::Array{Float64};
charges::TCN=nothing,dipvecs::TCN=nothing,
targets::TFN=nothing,pg::Integer=0,pgt::Integer=0,
nd::Integer=1,ndiv::Union{Integer,Nothing}=nothing)

zk = complex(zk)

Expand Down Expand Up @@ -303,6 +303,10 @@ function hfmm3d(eps::Float64,zk::Union{Float64,ComplexF64},
if anyfail
return vals
end
if ndiv !== nothing && ndiv < 1

Copilot AI Feb 17, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validation for ndiv checks if it is less than 1, but the condition should be ndiv <= 0 for clarity, since the error message states "ndiv must be a positive integer" which means ndiv must be greater than 0. While the current condition (ndiv < 1) is mathematically equivalent for integers, using ndiv <= 0 makes the intent more explicit and aligns better with the error message.

Suggested change
if ndiv !== nothing && ndiv < 1
if ndiv !== nothing && ndiv <= 0

Copilot uses AI. Check for mistakes.
@warn "ndiv must be a positive integer, computing nothing"
return vals
end

if (ifcharge == 0); charges = zero end
if (ifdipole == 0); dipvecs = zero end
Expand Down Expand Up @@ -369,12 +373,27 @@ function hfmm3d(eps::Float64,zk::Union{Float64,ComplexF64},
ier = Integer(0)
iper = Integer(0)

ccall((:hfmm3d_,libfmm3d),Cvoid,(Fi,Fd,Fc,Fi,Fd,Fi,Fc,
Fi,Fc,Fi,Fi,Fc,Fc,Fc,Fi,
Fd,Fi,Fc,Fc,Fc,Fi),
nd,eps,zk,n,sources,ifcharge,charges,ifdipole,
dipvecs,iper,pg,pot,grad,hess,nt,targets,pgt,
pottarg,gradtarg,hesstarg,ier)
if ndiv === nothing
ccall((:hfmm3d_,libfmm3d),Cvoid,(Fi,Fd,Fc,Fi,Fd,Fi,Fc,
Fi,Fc,Fi,Fi,Fc,Fc,Fc,Fi,
Fd,Fi,Fc,Fc,Fc,Fi),
nd,eps,zk,n,sources,ifcharge,charges,ifdipole,
dipvecs,iper,pg,pot,grad,hess,nt,targets,pgt,
pottarg,gradtarg,hesstarg,ier)
else
ndiv64 = Int64(ndiv)
idivflag = Int64(0)
ifnear = Int64(1)
timeinfo = zeros(Float64,6)
ccall((:hfmm3d_ndiv_,libfmm3d),Cvoid,(Fi,Fd,Fc,Fi,Fd,Fi,Fc,
Fi,Fc,Fi,Fi,Fc,Fc,Fc,Fi,
Fd,Fi,Fc,Fc,Fc,Fi,Fi,Fi,
Fd,Fi),
nd,eps,zk,n,sources,ifcharge,charges,ifdipole,
dipvecs,iper,pg,pot,grad,hess,nt,targets,pgt,
pottarg,gradtarg,hesstarg,ndiv64,idivflag,ifnear,
timeinfo,ier)
end

vals.ier = ier
if (ier != 0); @warn "libfmm3d had an error, see vals.ier"; end
Expand All @@ -392,6 +411,24 @@ function hfmm3d(eps::Float64,zk::Union{Float64,ComplexF64},

end

function hfmm3d(eps::Float64,zk::Union{Float64,ComplexF64},
sources::Array{Float64};
charges::TCN=nothing,dipvecs::TCN=nothing,
targets::TFN=nothing,pg::Integer=0,pgt::Integer=0,
nd::Integer=1)
return _hfmm3d_impl(eps,zk,sources,charges=charges,dipvecs=dipvecs,
targets=targets,pg=pg,pgt=pgt,nd=nd)
end

function hfmm3d_ndiv(eps::Float64,zk::Union{Float64,ComplexF64},
sources::Array{Float64},ndiv::Integer;
charges::TCN=nothing,dipvecs::TCN=nothing,
targets::TFN=nothing,pg::Integer=0,pgt::Integer=0,
nd::Integer=1)
return _hfmm3d_impl(eps,zk,sources,charges=charges,dipvecs=dipvecs,
targets=targets,pg=pg,pgt=pgt,nd=nd,ndiv=ndiv)
end
Comment on lines +423 to +430

Copilot AI Feb 17, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new hfmm3d_ndiv function is missing documentation. Based on the codebase conventions, all public functions should have docstrings that explain the purpose, parameters (especially the new ndiv parameter), and return values. Consider adding a docstring similar to the hfmm3d function but including information about the ndiv parameter and when it should be used.

Copilot uses AI. Check for mistakes.

"""
```julia
vals = h3ddir(zk,sources,targets;charges=nothing,
Expand Down Expand Up @@ -739,9 +776,10 @@ in the order: ``\\partial_{xx}``, ``\\partial_{yy}``,
Non-zero values may indicate insufficient memory available. See the documentation for the FMM3D library.
If not set (`nothing`), then FMM3D library was never called.
"""
function lfmm3d(eps::Float64,sources::Array{Float64};charges::TFN=nothing,
dipvecs::TFN=nothing,targets::TFN=nothing,
pg::Integer=0,pgt::Integer=0,nd::Integer=1)
function _lfmm3d_impl(eps::Float64,sources::Array{Float64};charges::TFN=nothing,
dipvecs::TFN=nothing,targets::TFN=nothing,
pg::Integer=0,pgt::Integer=0,nd::Integer=1,
ndiv::Union{Integer,Nothing}=nothing)

# default values

Expand All @@ -767,6 +805,10 @@ function lfmm3d(eps::Float64,sources::Array{Float64};charges::TFN=nothing,
if anyfail
return vals
end
if ndiv !== nothing && ndiv < 1

Copilot AI Feb 17, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validation for ndiv checks if it is less than 1, but the condition should be ndiv <= 0 for clarity, since the error message states "ndiv must be a positive integer" which means ndiv must be greater than 0. While the current condition (ndiv < 1) is mathematically equivalent for integers, using ndiv <= 0 makes the intent more explicit and aligns better with the error message.

Suggested change
if ndiv !== nothing && ndiv < 1
if ndiv !== nothing && ndiv <= 0

Copilot uses AI. Check for mistakes.
@warn "ndiv must be a positive integer, computing nothing"
return vals
end

if (ifcharge == 0); charges = zero end
if (ifdipole == 0); dipvecs = zero end
Expand Down Expand Up @@ -835,12 +877,27 @@ function lfmm3d(eps::Float64,sources::Array{Float64};charges::TFN=nothing,
ier = Integer(0)
iper = Integer(0)

ccall((:lfmm3d_,libfmm3d),Cvoid,(Fi,Fd,Fi,Fd,Fi,Fd,
Fi,Fd,Fi,Fi,Fd,Fd,Fd,Fi,
Fd,Fi,Fd,Fd,Fd,Fi),
nd,eps,n,sources,ifcharge,charges,ifdipole,
dipvecs,iper,pg,pot,grad,hess,nt,targets,pgt,
pottarg,gradtarg,hesstarg,ier)
if ndiv === nothing
ccall((:lfmm3d_,libfmm3d),Cvoid,(Fi,Fd,Fi,Fd,Fi,Fd,
Fi,Fd,Fi,Fi,Fd,Fd,Fd,Fi,
Fd,Fi,Fd,Fd,Fd,Fi),
nd,eps,n,sources,ifcharge,charges,ifdipole,
dipvecs,iper,pg,pot,grad,hess,nt,targets,pgt,
pottarg,gradtarg,hesstarg,ier)
else
ndiv64 = Int64(ndiv)
idivflag = Int64(0)
ifnear = Int64(1)
timeinfo = zeros(Float64,6)
ccall((:lfmm3d_ndiv_,libfmm3d),Cvoid,(Fi,Fd,Fi,Fd,Fi,Fd,
Fi,Fd,Fi,Fi,Fd,Fd,Fd,Fi,
Fd,Fi,Fd,Fd,Fd,Fi,Fi,Fi,
Fd,Fi),
nd,eps,n,sources,ifcharge,charges,ifdipole,
dipvecs,iper,pg,pot,grad,hess,nt,targets,pgt,
pottarg,gradtarg,hesstarg,ndiv64,idivflag,ifnear,
timeinfo,ier)
end

if (ier != 0); @warn "libfmm3d had an error, see vals.ier"; end
vals.ier = ier
Expand All @@ -858,6 +915,21 @@ function lfmm3d(eps::Float64,sources::Array{Float64};charges::TFN=nothing,

end

function lfmm3d(eps::Float64,sources::Array{Float64};charges::TFN=nothing,
dipvecs::TFN=nothing,targets::TFN=nothing,
pg::Integer=0,pgt::Integer=0,nd::Integer=1)
return _lfmm3d_impl(eps,sources,charges=charges,dipvecs=dipvecs,
targets=targets,pg=pg,pgt=pgt,nd=nd)
end

function lfmm3d_ndiv(eps::Float64,sources::Array{Float64},ndiv::Integer;
charges::TFN=nothing,dipvecs::TFN=nothing,
targets::TFN=nothing,pg::Integer=0,pgt::Integer=0,
nd::Integer=1)
return _lfmm3d_impl(eps,sources,charges=charges,dipvecs=dipvecs,
targets=targets,pg=pg,pgt=pgt,nd=nd,ndiv=ndiv)
end
Comment on lines +925 to +931

Copilot AI Feb 17, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new lfmm3d_ndiv function is missing documentation. Based on the codebase conventions, all public functions should have docstrings that explain the purpose, parameters (especially the new ndiv parameter), and return values. Consider adding a docstring similar to the lfmm3d function but including information about the ndiv parameter and when it should be used.

Copilot uses AI. Check for mistakes.


"""
```julia
Expand Down
14 changes: 14 additions & 0 deletions julia/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ using LinearAlgebra
pg=2,pgt=2)
vals2dc = hfmm3d(eps,zk,sources,targets=targets,charges=charges,
dipvecs=dipvecs,pg=2,pgt=2)
vals2dc_ndiv = hfmm3d_ndiv(eps,zk,sources,50,targets=targets,charges=charges,
dipvecs=dipvecs,pg=2,pgt=2)

@test norm(vals2c.pot-vals1cs.pottarg)/norm(vals1cs.pottarg) < eps
@test norm(vals2c.grad-vals1cs.gradtarg)/norm(vals1cs.gradtarg) < eps
Expand All @@ -53,6 +55,11 @@ using LinearAlgebra
@test norm(vals2dc.grad-vals1dcs.gradtarg)/norm(vals1dcs.gradtarg) < eps
@test norm(vals2dc.pottarg-vals1dc.pottarg)/norm(vals1dc.pottarg) < eps
@test norm(vals2dc.gradtarg-vals1dc.gradtarg)/norm(vals1dc.gradtarg) < eps
@test norm(vals2dc_ndiv.pot-vals1dcs.pottarg)/norm(vals1dcs.pottarg) < eps
@test norm(vals2dc_ndiv.grad-vals1dcs.gradtarg)/norm(vals1dcs.gradtarg) < eps
@test norm(vals2dc_ndiv.pottarg-vals1dc.pottarg)/norm(vals1dc.pottarg) < eps
@test norm(vals2dc_ndiv.gradtarg-vals1dc.gradtarg)/norm(vals1dc.gradtarg) < eps
@test vals2dc_ndiv.ier == 0


end
Expand Down Expand Up @@ -91,6 +98,8 @@ end
pg=2,pgt=2)
vals2dc = lfmm3d(eps,sources,targets=targets,charges=charges,
dipvecs=dipvecs,pg=2,pgt=2)
vals2dc_ndiv = lfmm3d_ndiv(eps,sources,50,targets=targets,charges=charges,
dipvecs=dipvecs,pg=2,pgt=2)

@test norm(vals2c.pot-vals1cs.pottarg)/norm(vals1cs.pottarg) < eps
@test norm(vals2c.grad-vals1cs.gradtarg)/norm(vals1cs.gradtarg) < eps
Expand All @@ -106,6 +115,11 @@ end
@test norm(vals2dc.grad-vals1dcs.gradtarg)/norm(vals1dcs.gradtarg) < eps
@test norm(vals2dc.pottarg-vals1dc.pottarg)/norm(vals1dc.pottarg) < eps
@test norm(vals2dc.gradtarg-vals1dc.gradtarg)/norm(vals1dc.gradtarg) < eps
@test norm(vals2dc_ndiv.pot-vals1dcs.pottarg)/norm(vals1dcs.pottarg) < eps
@test norm(vals2dc_ndiv.grad-vals1dcs.gradtarg)/norm(vals1dcs.gradtarg) < eps
@test norm(vals2dc_ndiv.pottarg-vals1dc.pottarg)/norm(vals1dc.pottarg) < eps
@test norm(vals2dc_ndiv.gradtarg-vals1dc.gradtarg)/norm(vals1dc.gradtarg) < eps
@test vals2dc_ndiv.ier == 0


end
Expand Down
Loading