Cannot find why Stack Overflow problem appears

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Cannot find why Stack Overflow problem appears

Aleksandr Mikheev
I am trying to make an octree for randomly distributed set of particles. I have been trying to find where Stack Overflow Error appears whole day, but it was in vain. Could you please help me? The problem should be somewhere in (c == [0]) && (a > 0) part, however, I have no idea where. Thank you in advance!

type particle
  R::Vector{Float64}          # coordinates of the particle
  m::Float64                  # mass of the particle
  a_tree::Float64             # acceleration of particle calculated from tree
  a_exact::Float64            # acceleration of particle calculated by exact summation
end

type node
  parent::Int64               # node's parent number
  children::Vector{Int64}     # set of childrens' numbers
  l::Float64                  # size of the node
  R::Vector{Float64}          # radius of geometric center
  m::Float64                  # total mass of the node
  ptcl::Int64                 # pointer to the particle in it
end

function problem3_2(N::Int64)
particles = Vector{particle}(N)                 # Basically, our system
tree = Vector{node}(1)                          # Initial tree (root)
tree[1] = node(0,[0],1.0,[0.0,0.0,0.0],0.0,0)

for i in eachindex(particles)
  particles[i] = particle([rand() - 1/2, rand() - 1/2, rand() - 1/2], 1.0, 0.0, 0.0)
  tree = InsertParticle(i,1,tree,particles)
end

return tree
end

function InsertParticle(i::Int64,k::Int64,tree,particles)
  c = tree[k].children
  a = tree[k].ptcl
  if (c == [0]) && (a == 0)
    tree[k].ptcl = i
  elseif (c == [0]) && (a > 0)
    j_1 = length(tree) + DetermOctant(particles[tree[k].ptcl],tree[k])
    j_2 = length(tree) + DetermOctant(particles[i],tree[k])
    tree[k].children = [x for x in length(tree)+1:length(tree)+8]
      for p = 1:8
        push!(tree,node(k,[0],tree[k].l/2,[0.0,0.0,0.0],0.0,0))
        if (p == 1) || (p == 4) || (p == 5) || (p == 8)
          tree[k+p].R[1] = tree[k].R[1] + tree[k+p].l/2
        else
          tree[k+p].R[1] = tree[k].R[1] - tree[k+p].l/2
        end
        if (p == 1) || (p == 2) || (p == 5) || (p == 6)
          tree[k+p].R[2] = tree[k].R[2] + tree[k+p].l/2
        else
          tree[k+p].R[2] = tree[k].R[2] - tree[k+p].l/2
        end
        if (p == 1) || (p == 2) || (p == 3) || (p == 4)
          tree[k+p].R[3] = tree[k].R[3] + tree[k+p].l/2
        else
          tree[k+p].R[3] = tree[k].R[3] - tree[k+p].l/2
        end

        end

    InsertParticle(tree[k].ptcl,j_1,tree,particles)
    InsertParticle(i,j_2,tree,particles)
    tree[k].ptcl = 0
  elseif (c != [0])
      j = DetermOctant(particles[i],tree[k])
      InsertParticle(i,tree[k].children[j],tree,particles)
  end
return tree
end


function DetermOctant(x::particle,y::node)
  c1 = y.R[1]; c2 = y.R[2]; c3 = y.R[3]
  x1 = sign(x.R[1] - c1); x2 = sign(x.R[2] - c2); x3 = sign(x.R[3] - c3)
  if (x1 > 0) && (x2 > 0) && (x3 > 0)
    n = 1
  elseif (x1 < 0) && (x2 > 0) && (x3 > 0)
    n = 2
  elseif (x1 < 0) && (x2 < 0) && (x3 > 0)
    n = 3
  elseif (x1 > 0) && (x2 < 0) && (x3 > 0)
    n = 4
  elseif (x1 > 0) && (x2 > 0) && (x3 < 0)
    n = 5
  elseif (x1 < 0) && (x2 > 0) && (x3 < 0)
    n = 6
  elseif (x1 < 0) && (x2 < 0) && (x3 < 0)
    n = 7
  else
    n = 8
  end
return n
end


Reply | Threaded
Open this post in threaded view
|

Re: Cannot find why Stack Overflow problem appears

Aleksandr Mikheev
I used debug tools, found out that something doesn't work when I try to insert a point into the node which already contains a point, and they both should go to the same subnode. For instance,


particles
[1] = particle([0.1, 0.1, 0.1], 1.0, [0.0,0.0,0.0], [0.0,0.0,0.0])

tree
= InsertParticle(1,1,tree,particles)

particles
[2] = particle([0.13, 0.2, 0.15], 1.0, [0.0,0.0,0.0], [0.0,0.0,0.0])

tree
= InsertParticle(2,1,tree,particles)



leads to infinite loop. However, I cannot see the reason. 

Reply | Threaded
Open this post in threaded view
|

Re: Cannot find why Stack Overflow problem appears

Angel de Vicente
Hi Aleksandr,

Aleksandr Mikheev <[hidden email]> writes:

> I used debug tools, found out that something doesn't work when I try to insert a
> point into the node which already contains a point, and they both should go to
> the same subnode. For instance,

didn't have the time to look into your code, but just a few days ago I
wrote a Barnes-Hut N-body code with Julia (search the list for
Barnes-Hut around Nov 1). This was a port of a previous code that I
wrote in Fortran, and I verified that both codes produce identical
values, Julia's version being about 3x slower than the Fortran one,
though some further performance improvement can probably be done. If you
would like to have a look at it, let me know and I will try to clean the
code a bit and put it public somewhere.

Cheers,
--
Ángel de Vicente
http://www.iac.es/galeria/angelv/