我首先使用蛮力进行了二维n体仿真,但是随后http://arborjs.org/docs/barnes-hut之后,我实现了Barnes-Hut逼近算法。但是,这并没有给我想要的效果。
例如:
Barnes-Hut-> 2000具尸体;帧时平均32毫秒和5000; 164毫秒
蛮力-> 2000机构;帧时平均31毫秒和5000; 195毫秒
这些值均已关闭渲染。
我是否正确地假设我没有正确实现算法,因此性能没有得到实质性的提高?
Theta当前设置为s / d <0.5。将此值更改为例如1确实可以提高性能,但是很明显为什么不推荐这样做。
单线程
我的代码大致如下:
while(!close)
{
long newTime = System.currentTimeMillis();
long frameTime = newTime-currentTime;
System.out.println(frameTime);
currentTime = newTime;
update the bodies
}
在更新正文的函数中:
first insert all bodies into the quadtree with all its subnodes
for all bodies
{
compute the physics using Barnes-Hut which yields a net force per planet (doPhysics(body))
calculate instantaneous acceleration from net force
update the instantaneous velocity
}
barneshut函数:
doPhysics(body)
{
if(node is external (contains 1 body) and that body is not itself)
{
calculate the force between those two bodies
}else if(node is internal and s/d < 0.5)
{
create a pseudobody at the COM with the nodes total mass
calculate the force between the body and pseudobody
}else (if is it internal but s/d >= 0.5)
{
(this is where recursion comes in)
doPhysics on same body but on the NorthEast subnode
doPhysics on same body but on the NorthWest subnode
doPhysics on same body but on the SouthEast subnode
doPhysics on same body but on the SouthWest subnode
}
}
实际计算力:
calculateforce(body, otherbody)
{
if(body is not at exactly the same position (avoid division by 0))
{
calculate force using newtons law of gravitation in vector form
add the force to the bodies' net force in this frame
}
}
答案 0 :(得分:0)
您的代码仍然不完整(在SSCCE s上阅读),并且不完整代码的深入调试不是该站点的目的。但是,这就是我将如何确定错误(如果有的话)的下一步: