From d44b107bdc0ae1c22b58c4fe7f864ecc567e639c Mon Sep 17 00:00:00 2001 From: Tristan Goodwill <48371600+tristangdwl@users.noreply.github.com> Date: Tue, 1 Dec 2026 17:04:36 -0500 Subject: [PATCH] Round root box size to a power of 2 --- src/Helmholtz/hfmm3d.f | 3 +- src/Helmholtz/hfmm3d_memest.f | 3 +- src/Helmholtz/hfmm3d_ndiv.f | 3 +- src/Laplace/lfmm3d.f | 3 +- src/Laplace/lfmm3d_ndiv.f | 3 +- test/Helmholtz/test_hfmm3d_scale.f | 84 ++++++++++++++++++++++++++++-- test/Laplace/test_lfmm3d_scale.f | 79 ++++++++++++++++++++++++++-- 7 files changed, 167 insertions(+), 11 deletions(-) diff --git a/src/Helmholtz/hfmm3d.f b/src/Helmholtz/hfmm3d.f index 45de64a..e718b1d 100644 --- a/src/Helmholtz/hfmm3d.f +++ b/src/Helmholtz/hfmm3d.f @@ -294,7 +294,8 @@ subroutine hfmm3d(nd,eps,zk,nsource,source,ifcharge, c c Set rescaling parameters c - b0 = boxsize(0) +c round b0 up to a power of 2 so that rescaling is exact + b0 = scale(1.0d0, exponent(boxsize(0))) b0inv = 1.0d0/b0 b0inv2 = b0inv**2 diff --git a/src/Helmholtz/hfmm3d_memest.f b/src/Helmholtz/hfmm3d_memest.f index c576de9..fb481b2 100644 --- a/src/Helmholtz/hfmm3d_memest.f +++ b/src/Helmholtz/hfmm3d_memest.f @@ -259,7 +259,8 @@ subroutine hfmm3d_memest(nd,eps,zk,nsource,source,ifcharge, c c Set rescaling parameters c - b0 = boxsize(0) +c round b0 up to a power of 2 so that rescaling is exact + b0 = scale(1.0d0, exponent(boxsize(0))) b0inv = 1.0d0/b0 b0inv2 = b0inv**2 diff --git a/src/Helmholtz/hfmm3d_ndiv.f b/src/Helmholtz/hfmm3d_ndiv.f index 0e5e41c..14bc71e 100644 --- a/src/Helmholtz/hfmm3d_ndiv.f +++ b/src/Helmholtz/hfmm3d_ndiv.f @@ -238,7 +238,8 @@ subroutine hfmm3d_ndiv(nd,eps,zk,nsource,source,ifcharge, c c Set rescaling parameters c - b0 = boxsize(0) +c round b0 up to a power of 2 so that rescaling is exact + b0 = scale(1.0d0, exponent(boxsize(0))) b0inv = 1.0d0/b0 b0inv2 = b0inv**2 diff --git a/src/Laplace/lfmm3d.f b/src/Laplace/lfmm3d.f index 4cbd01c..63ae59f 100644 --- a/src/Laplace/lfmm3d.f +++ b/src/Laplace/lfmm3d.f @@ -248,7 +248,8 @@ subroutine lfmm3d(nd,eps,nsource,source,ifcharge, c c Set rescaling parameters c - b0 = boxsize(0) +c round b0 up to a power of 2 so that rescaling is exact + b0 = scale(1.0d0, exponent(boxsize(0))) b0inv = 1.0d0/b0 b0inv2 = b0inv**2 b0inv3 = b0inv2*b0inv diff --git a/src/Laplace/lfmm3d_ndiv.f b/src/Laplace/lfmm3d_ndiv.f index ecb9617..13f6b15 100644 --- a/src/Laplace/lfmm3d_ndiv.f +++ b/src/Laplace/lfmm3d_ndiv.f @@ -231,7 +231,8 @@ subroutine lfmm3d_ndiv(nd,eps,nsource,source,ifcharge, c c Set rescaling parameters c - b0 = boxsize(0) +c round b0 up to a power of 2 so that rescaling is exact + b0 = scale(1.0d0, exponent(boxsize(0))) b0inv = 1.0d0/b0 b0inv2 = b0inv**2 b0inv3 = b0inv2*b0inv diff --git a/test/Helmholtz/test_hfmm3d_scale.f b/test/Helmholtz/test_hfmm3d_scale.f index f2099e5..68a364e 100644 --- a/test/Helmholtz/test_hfmm3d_scale.f +++ b/test/Helmholtz/test_hfmm3d_scale.f @@ -1,12 +1,12 @@ implicit real *8 (a-h,o-z) complex *16 z1,z2,zk,ima - integer *8 i1,i2,i3,i4,i5,i6 + integer *8 i1,i2,i3,i4,i5,i6,i7 data ima/(0.0d0,1.0d0)/ done = 1 pi = atan(done)*4 - ntest = 6 + ntest = 7 z1 = 8 + ima*0.2d0 @@ -35,8 +35,11 @@ rsc = 1.0d-9 zk = z1*2*pi/rsc call test_hfmm3d_scale(rsc,zk,i6) + + zk = 1.1d0 + call test_hfmm3d_near(zk,i7) - nsuccess = i1+i2+i3+i4+i5+i6 + nsuccess = i1+i2+i3+i4+i5+i6+i7 open(unit=33,file='print_testres.txt',access='append') write(*,'(a,i2,a,i2,a)') 'Successfully completed ', nsuccess, 1 ' out of ', ntest, ' in hfmm3d scale testing suite' @@ -171,3 +174,78 @@ subroutine test_hfmm3d_scale(rsc,zk,isuccess) end c---------------------------------------------------------- c + + + subroutine test_hfmm3d_near(zk,isuccess) +c +c Targets at distance 1e-10 from sources, in a root box whose +c size is not a power of 2. Checks that rescaling the points to +c the unit box does not perturb near interactions. +c + implicit none + integer *8 ns,i,j,ier,isuccess,ione + double precision, allocatable :: source(:,:),targ(:,:) + double precision, allocatable :: dnorm(:,:) + complex *16, allocatable :: charge(:),dipvec(:,:) + complex *16, allocatable :: pot(:),potex(:) + complex *16, allocatable :: grad(:,:),gradex(:,:) + complex *16 zk + double precision eps,d,r,thresh,erra,ra,hkrand + + ns = 2000 + ione = 1 + allocate(source(3,ns),targ(3,ns),dnorm(3,ns)) + allocate(charge(ns),dipvec(3,ns)) + allocate(pot(ns),grad(3,ns),potex(ns),gradex(3,ns)) + + eps = 0.51d-9 + d = 1.0d-10 + + do i=1,ns + do j=1,3 + dnorm(j,i) = 2*hkrand(0)-1 + enddo + r = sqrt(dnorm(1,i)**2+dnorm(2,i)**2+dnorm(3,i)**2) + do j=1,3 + dnorm(j,i) = dnorm(j,i)/r + dipvec(j,i) = dnorm(j,i) + enddo + source(1,i) = 0.37d0 + 1.3d0*dnorm(1,i) + source(2,i) = 0.21d0 + 1.3d0*dnorm(2,i) + source(3,i) = -0.11d0 + 1.3d0*dnorm(3,i) + do j=1,3 + targ(j,i) = source(j,i) + d*dnorm(j,i) + enddo + charge(i) = hkrand(0)-0.5d0 + potex(i) = 0 + do j=1,3 + gradex(j,i) = 0 + enddo + enddo + + call hfmm3d_t_cd_g(eps,zk,ns,source,charge,dipvec,ns,targ, + 1 pot,grad,ier) + + thresh = 1.0d-300 + call h3ddirectcdg(ione,zk,source,charge,dipvec,ns,targ,ns, + 1 potex,gradex,thresh) + + erra = 0 + ra = 0 + do i=1,ns + erra = erra + abs(potex(i)-pot(i))**2 + ra = ra + abs(potex(i))**2 + do j=1,3 + erra = erra + abs(gradex(j,i)-grad(j,i))**2 + ra = ra + abs(gradex(j,i))**2 + enddo + enddo + erra = sqrt(erra/ra) + + call prin2('near-field l2 rel err=*',erra,1) + + isuccess = 0 + if(erra.lt.5*eps) isuccess = 1 + + return + end diff --git a/test/Laplace/test_lfmm3d_scale.f b/test/Laplace/test_lfmm3d_scale.f index fc4e1c2..4cf8ed6 100644 --- a/test/Laplace/test_lfmm3d_scale.f +++ b/test/Laplace/test_lfmm3d_scale.f @@ -1,10 +1,10 @@ implicit real *8 (a-h,o-z) - integer *8 i1,i2 + integer *8 i1,i2,i3 done = 1 pi = atan(done)*4 - ntest = 2 + ntest = 3 rsc = 1.0d10 @@ -13,7 +13,9 @@ rsc = 1.0d-9 call test_lfmm3d_scale(rsc,i2) - nsuccess = i1+i2 + call test_lfmm3d_near(i3) + + nsuccess = i1+i2+i3 open(unit=33,file='print_testres.txt',access='append') write(33,'(a,i1,a,i1,a)') 'Successfully completed ', nsuccess, 1 ' out of ', ntest, ' in lfmm3d scale testing suite' @@ -141,3 +143,74 @@ subroutine test_lfmm3d_scale(rsc,isuccess) end c---------------------------------------------------------- c + + + subroutine test_lfmm3d_near(isuccess) +c +c Targets at distance 1e-10 from sources, in a root box whose +c size is not a power of 2. Checks that rescaling the points to +c the unit box does not perturb near interactions. +c + implicit none + integer *8 ns,i,j,ier,isuccess,ione + double precision, allocatable :: source(:,:),targ(:,:) + double precision, allocatable :: charge(:),dipvec(:,:) + double precision, allocatable :: pot(:),potex(:) + double precision, allocatable :: grad(:,:),gradex(:,:) + double precision eps,d,r,thresh,erra,ra,hkrand + + ns = 2000 + ione = 1 + allocate(source(3,ns),targ(3,ns),charge(ns),dipvec(3,ns)) + allocate(pot(ns),grad(3,ns),potex(ns),gradex(3,ns)) + + eps = 0.51d-9 + d = 1.0d-10 + + do i=1,ns + do j=1,3 + dipvec(j,i) = 2*hkrand(0)-1 + enddo + r = sqrt(dipvec(1,i)**2+dipvec(2,i)**2+dipvec(3,i)**2) + do j=1,3 + dipvec(j,i) = dipvec(j,i)/r + enddo + source(1,i) = 0.37d0 + 1.3d0*dipvec(1,i) + source(2,i) = 0.21d0 + 1.3d0*dipvec(2,i) + source(3,i) = -0.11d0 + 1.3d0*dipvec(3,i) + do j=1,3 + targ(j,i) = source(j,i) + d*dipvec(j,i) + enddo + charge(i) = hkrand(0)-0.5d0 + potex(i) = 0 + do j=1,3 + gradex(j,i) = 0 + enddo + enddo + + call lfmm3d_t_cd_g(eps,ns,source,charge,dipvec,ns,targ, + 1 pot,grad,ier) + + thresh = 1.0d-300 + call l3ddirectcdg(ione,source,charge,dipvec,ns,targ,ns, + 1 potex,gradex,thresh) + + erra = 0 + ra = 0 + do i=1,ns + erra = erra + abs(potex(i)-pot(i))**2 + ra = ra + abs(potex(i))**2 + do j=1,3 + erra = erra + abs(gradex(j,i)-grad(j,i))**2 + ra = ra + abs(gradex(j,i))**2 + enddo + enddo + erra = sqrt(erra/ra) + + call prin2('near-field l2 rel err=*',erra,1) + + isuccess = 0 + if(erra.lt.5*eps) isuccess = 1 + + return + end