无法找到出现Stack Overflow错误的原因

时间:2016-11-10 19:58:44

标签: stack-overflow julia

我正在尝试制作octree for randomly distributed set of particles。我一直试图找到Stack Overflow Error整天出现的地方,但这是徒劳的。请你帮助我好吗?问题应该在(c == [0])&& (a> 0)部分,然而,我不知道在哪里。提前谢谢!

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

基本上,我确实知道错误应该在某处:

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

但是,我无法看到无限循环出现的位置。很抱歉以这种形式将这些全部带到这里,我只是想避免XY问题。

提前谢谢!

0 个答案:

没有答案