Skip to content

Commit

Permalink
Merge pull request #132 from ADACS-Australia/fix-sink-segfault
Browse files Browse the repository at this point in the history
Fix seg fault in test suite on sink particle creation
  • Loading branch information
danieljprice committed Jul 19, 2021
2 parents 06df6dd + a79497c commit c9816dc
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/main/linklist_kdtree.F90
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module linklist
type(kdnode), public, allocatable :: node(:)
integer, allocatable :: nodemap(:)
integer, public , allocatable :: listneigh(:)
integer, public , allocatable :: listneigh_global(:)
!$omp threadprivate(listneigh)
integer(kind=8), public :: ncells
real, public :: dxcell
Expand Down Expand Up @@ -66,6 +67,7 @@ subroutine allocate_linklist
!$omp parallel
call allocate_array('listneigh',listneigh,maxp)
!$omp end parallel
call allocate_array('listneigh_global',listneigh_global,maxp)

end subroutine allocate_linklist

Expand All @@ -80,6 +82,7 @@ subroutine deallocate_linklist
!$omp parallel
if (allocated(listneigh)) deallocate(listneigh)
!$omp end parallel
if (allocated(listneigh_global)) deallocate(listneigh_global)
call deallocate_kdtree()

end subroutine deallocate_linklist
Expand Down
7 changes: 3 additions & 4 deletions src/main/ptmass.F90
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ subroutine ptmass_accrete(is,nptmass,xi,yi,zi,hi,vxi,vyi,vzi,fxi,fyi,fzi, &
real, intent(in) :: xyzmh_ptmass(nsinkproperties,maxptmass)
real, intent(in) :: vxyz_ptmass(3,maxptmass)
logical, intent(out) :: accreted
real, intent(inout) :: dptmass(ndptmass,maxptmass)
real, intent(inout) :: dptmass(:,:)
integer(kind=1), intent(in) :: nbinmax
integer(kind=1), intent(inout) :: ibin_wakei
integer, optional, intent(out) :: nfaili
Expand Down Expand Up @@ -826,7 +826,7 @@ subroutine ptmass_create(nptmass,npart,itest,xyzh,vxyzu,fxyzu,fext,divcurlv,pote
#ifdef IND_TIMESTEPS
use part, only:ibin,ibin_wake
#endif
use linklist, only:getneigh_pos,ifirstincell
use linklist, only:getneigh_pos,ifirstincell,listneigh=>listneigh_global
use eos, only:equationofstate,gamma,gamma_pwp,utherm
use options, only:ieos
use units, only:unit_density
Expand All @@ -844,13 +844,12 @@ subroutine ptmass_create(nptmass,npart,itest,xyzh,vxyzu,fxyzu,fext,divcurlv,pote
real, intent(in) :: time
integer(kind=1) :: iphasei,ibin_wakei
integer :: nneigh
integer :: listneigh(maxneigh)
integer, parameter :: maxcache = 12000
integer, parameter :: nneigh_thresh = 1024 ! approximate epot if neigh>neigh_thresh; (-ve for off)
#ifdef IND_TIMESTEPS
integer(kind=1) :: ibin_itest
#endif
real :: xyzcache(maxcache,3)
real, save :: xyzcache(maxcache,3)
real :: dptmass(ndptmass,nptmass+1)
real :: xi,yi,zi,hi,hi1,hi21,xj,yj,zj,hj1,hj21,xk,yk,zk,hk1
real :: rij2,rik2,rjk2,dx,dy,dz
Expand Down
9 changes: 7 additions & 2 deletions src/setup/set_shock.f90
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module setshock
subroutine set_shock(latticetype,id,master,itype,rholeft,rhoright,xmin,xmax,ymin,ymax,zmin,zmax,&
xshock,dxleft,hfact,smooth_fac,npart,xyzh,massoftype,iverbose,ierr,mask)
use unifdis, only:set_unifdis,get_ny_nz_closepacked,is_closepacked,mask_prototype,mask_true
use stretchmap, only:rho_func
character(len=*), intent(in) :: latticetype
integer, intent(in) :: id,master,itype,iverbose
real, intent(in) :: rholeft,rhoright,xshock,xmin,xmax,ymin,ymax,zmin,zmax
Expand All @@ -44,6 +45,7 @@ subroutine set_shock(latticetype,id,master,itype,rholeft,rhoright,xmin,xmax,ymin
integer, intent(out) :: ierr
procedure(mask_prototype), optional :: mask
procedure(mask_prototype), pointer :: my_mask
procedure(rho_func), pointer :: density_func
integer :: npartold,ny,nz
real :: totmass,volume,dxright
real :: xminleft(3),xmaxleft(3),xminright(3),xmaxright(3)
Expand All @@ -61,6 +63,9 @@ subroutine set_shock(latticetype,id,master,itype,rholeft,rhoright,xmin,xmax,ymin
else
my_mask => mask_true
endif

density_func => rhosmooth

!
! set limits of the different domains
!
Expand All @@ -80,7 +85,7 @@ subroutine set_shock(latticetype,id,master,itype,rholeft,rhoright,xmin,xmax,ymin
! set up a uniform lattice for left half
call set_unifdis(latticetype,id,master,xminleft(1),xmaxleft(1),xminleft(2), &
xmaxleft(2),xminleft(3),xmaxleft(3),dxleft,hfact,npart,xyzh,&
periodic,rhofunc=rhosmooth,mask=my_mask)
periodic,rhofunc=density_func,mask=my_mask)

! set particle mass
volume = product(xmaxleft-xminleft)
Expand All @@ -101,7 +106,7 @@ subroutine set_shock(latticetype,id,master,itype,rholeft,rhoright,xmin,xmax,ymin

call set_unifdis(latticetype,id,master,xminright(1),xmaxright(1), &
xminright(2),xmaxright(2),xminright(3),xmaxright(3),dxright,hfact,&
npart,xyzh,periodic,npy=ny,npz=nz,rhofunc=rhosmooth,mask=my_mask) ! set right half
npart,xyzh,periodic,npy=ny,npz=nz,rhofunc=density_func,mask=my_mask) ! set right half

else ! set all of volume if densities are equal
write(*,'(3(a,es16.8))') 'shock: uniform density ',xminleft(1), ' to ',xmaxright(1), ' with dx = ',dxleft
Expand Down
4 changes: 2 additions & 2 deletions src/setup/set_sphere.f90
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ module spherical
!-----------------------------------------------------------------------
subroutine set_sphere(lattice,id,master,rmin,rmax,delta,hfact,np,xyzh, &
rhofunc,rhotab,rtab,xyz_origin,nptot,dir,exactN,np_requested,mask)
use stretchmap, only:set_density_profile
use stretchmap, only:set_density_profile,rho_func
character(len=*), intent(in) :: lattice
integer, intent(in) :: id,master
integer, intent(inout) :: np
real, intent(in) :: rmin,rmax,hfact
real, intent(out) :: xyzh(:,:)
real, intent(inout) :: delta
real, external, optional :: rhofunc
procedure(rho_func), pointer, optional :: rhofunc
real, intent(in), optional :: rhotab(:), rtab(:)
integer, intent(in), optional :: dir
integer, intent(in), optional :: np_requested
Expand Down
4 changes: 2 additions & 2 deletions src/setup/set_unifdis.f90
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ subroutine set_unifdis(lattice,id,master,xmin,xmax,ymin,ymax, &
rmin,rmax,rcylmin,rcylmax,rellipsoid,in_ellipsoid, &
nptot,npy,npz,rhofunc,inputiseed,verbose,centre,dir,geom,mask,err)
use random, only:ran2
use stretchmap, only:set_density_profile
use stretchmap, only:set_density_profile,rho_func
!use domain, only:i_belong
character(len=*), intent(in) :: lattice
integer, intent(in) :: id,master
Expand All @@ -63,7 +63,7 @@ subroutine set_unifdis(lattice,id,master,xmin,xmax,ymin,ymax, &
real, intent(in), optional :: rellipsoid(3)
integer(kind=8), intent(inout), optional :: nptot
integer, intent(in), optional :: npy,npz,dir,geom
real, external, optional :: rhofunc
procedure(rho_func), pointer, optional :: rhofunc
integer, intent(in), optional :: inputiseed
logical, intent(in), optional :: verbose,centre,in_ellipsoid
integer, intent(out), optional :: err
Expand Down
16 changes: 11 additions & 5 deletions src/setup/stretchmap.f90
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module stretchmap
real, parameter :: pi = 4.*atan(1.) ! the circle of life
public :: set_density_profile
public :: get_mass_r
public :: rho_func

integer, private :: ngrid = 1024 ! number of points used when integrating rho to get mass
integer, parameter, private :: maxits = 100 ! max number of iterations
Expand All @@ -38,6 +39,11 @@ module stretchmap
ierr_memory_allocation = 2, & ! error code
ierr_table_size_differs = 3, & ! error code
ierr_not_converged = -1 ! error code
abstract interface
real function rho_func(x)
real, intent(in) :: x
end function rho_func
end interface

private

Expand Down Expand Up @@ -84,7 +90,7 @@ subroutine set_density_profile(np,xyzh,min,max,rhofunc,rhotab,xtab,start,geom,co
integer, intent(in) :: np
real, intent(inout) :: xyzh(:,:)
real, intent(in) :: min,max
real, external, optional :: rhofunc
procedure(rho_func), pointer, optional :: rhofunc
real, intent(in), optional :: rhotab(:),xtab(:)
integer, intent(in), optional :: start, geom, coord
logical, intent(in), optional :: verbose
Expand Down Expand Up @@ -198,7 +204,7 @@ subroutine set_density_profile(np,xyzh,min,max,rhofunc,rhotab,xtab,start,geom,co
nerr = 0
!$omp parallel do default(none) &
!$omp shared(np,xyzh,rhozero,igeom,use_rhotab,rhotab,xtable,masstab,nt) &
!$omp shared(xmin,xmax,totmass,icoord,is_r,is_rcyl,istart) &
!$omp shared(xmin,xmax,totmass,icoord,is_r,is_rcyl,istart,rhofunc) &
!$omp private(x,xold,xt,fracmassold,its,xprev,xi,hi,rhoi) &
!$omp private(func,dfunc,xminbisect,xmaxbisect,bisect) &
!$omp reduction(+:nerr)
Expand Down Expand Up @@ -354,7 +360,7 @@ real function get_mass_r(rhofunc,r,rmin)
! mass integrated along spherical radius
!
real, intent(in) :: r,rmin
real, external :: rhofunc
procedure(rho_func), pointer :: rhofunc
real :: dr,ri,dmi,dmprev
integer :: i

Expand All @@ -376,7 +382,7 @@ real function get_mass_rcyl(rhofunc,rcyl,rmin)
! mass integrated along cylindrical radius
!
real, intent(in) :: rcyl,rmin
real, external :: rhofunc
procedure(rho_func), pointer :: rhofunc
real :: dr,ri,dmi,dmprev
integer :: i

Expand All @@ -398,7 +404,7 @@ real function get_mass(rhofunc,x,xmin)
! mass integrated along cartesian direction
!
real, intent(in) :: x,xmin
real, external :: rhofunc
procedure(rho_func), pointer :: rhofunc
real :: dx,xi,dmi,dmprev
integer :: i

Expand Down
6 changes: 5 additions & 1 deletion src/tests/test_ptmass.F90
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ subroutine test_ptmass(ntests,npass)
use part, only:ibin,ibin_old
#endif
use mpiutils, only:bcast_mpi,reduce_in_place_mpi,reduceloc_mpi
use stretchmap, only:rho_func
integer, intent(inout) :: ntests,npass
integer :: i,nsteps,nbinary_tests,itest,nerr,nwarn,itestp
integer :: nparttot
Expand All @@ -79,9 +80,12 @@ subroutine test_ptmass(ntests,npass)
integer :: id_rhomax,ipart_rhomax_global
integer(kind=1) :: ibin_wakei
character(len=20) :: dumpfile,filename
procedure(rho_func), pointer :: density_func

if (id==master) write(*,"(/,a,/)") '--> TESTING PTMASS MODULE'

density_func => gaussianr

test_binary = .true.
test_accretion = .true.
test_createsink = .true.
Expand Down Expand Up @@ -501,7 +505,7 @@ subroutine test_ptmass(ntests,npass)
psep = 0.05 ! required as a variable since this may change under conditions not requested here
if (itest==2) then
! use random so particle with maximum density is unique
call set_sphere('cubic',id,master,0.,0.2,psep,hfact,npartoftype(igas),xyzh,rhofunc=gaussianr)
call set_sphere('cubic',id,master,0.,0.2,psep,hfact,npartoftype(igas),xyzh,rhofunc=density_func)
else
call set_sphere('cubic',id,master,0.,0.2,psep,hfact,npartoftype(igas),xyzh)
endif
Expand Down

0 comments on commit c9816dc

Please sign in to comment.