我正在尝试编写一个程序来计算n体问题。我试图改变我的代码遇到了一个问题,因此很容易适应任何数量的机构。函数corr()似乎存在问题,在使用函数corr()之后,对此函数中类粒子实例的某些变量所做的更改似乎会丢失。我该如何解决这个问题?
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <fstream>
#define h 1000.0
#define G 6.67384*pow(10.0,-11)
using namespace std;
class particle{
public:
double kx1,kx2,kx3,kx4, kv1, kv2, kv3, kv4;
double ky1, ky2, ky3, ky4, kvy1, kvy2, kvy3, kvy4;
double x,y,vx,vy,m;
double dist(particle aap){
double dx = x - aap.x;
double dy = y - aap.y;
return sqrt(pow(dx,2.0)+pow(dy,2.0));
}
double g(double x1, double y1,particle aap){
return G*aap.m*(aap.x-x1)/pow(dist(aap),3.0);
}
double p(double x1, double y1, particle aap){
return G*aap.m*(aap.y-y1)/pow(dist(aap),3.0);
}
void update(){ //zet het object 1 stap vooruit
x = x + (1/6.0)*(kx1+2*kx2+2*kx3+kx4);
vx = vx + (1/6.0)*(kv1+2*kv2+2*kv3+kv4);
y = y + (1/6.0)*(ky1+2*ky2+2*ky3+ky4);
vy = vy + (1/6.0)*(kvy1+2*kvy2+2*kvy3+kvy4);
}
void create(double x1, double y1, double vx1, double vy1, double m1){
x = x1;
y = y1;
vx = vx1;
vy = vy1;
m =m1;
}
bool operator ==(particle &other){
if(x == other.x && y == other.y && vx == other.vx && vy == other.vy){
return true;
}
}
};
particle zon, maan, aarde;
void set(){
zon.create(1, 1, -2, 1, 2*pow(10.0,30));
aarde.create(1.5*pow(10.0,11), 0, 2, 29780, 6*pow(10.0,24));
maan.create(aarde.x + 1, aarde .y + 3.844399*pow(10.0,8), aarde.vx + -1022.0, aarde.vy + 1, 7.3347*pow(10.0,22));
}
double xforce(double x1, double y1, particle aap){ //kracht in de x-richting
particle bodies[] = {zon, aarde, maan};
double fx;
for (int i = 0; i < 3; i++){
if (bodies[i].x == aap.x && bodies[i].y == aap.y && bodies[i].vx == aap.vx && bodies[i].vy == aap.vy ){;}
else{
fx += aap.g(x1,y1,bodies[i]);
}
}
return fx;
}
double yforce(double x1, double y1, particle aap){ //kracht in de y-richting
particle bodies[] = {zon, aarde, maan};
double fy;
for (int i = 0; i <= 3; i++){
if (bodies[i].x == aap.x && bodies[i].y == aap.y && bodies[i].vx == aap.vx && bodies[i].vy == aap.vy) {;}
else{
fy += aap.p(x1,y1,bodies[i]);
}
}
return fy;
}
void corr(particle body){
body.kx1 = h*body.vx;
body.kv1 = h*xforce(body.x, body.y, body);
body.ky1 = h*body.vy;
body.kvy1 =h*yforce(body.x, body.y, body);
body.kx2 = h*(body.vx + 0.5*body.kv1);
body.kv2 = h*xforce(body.x + 0.5*body.kx1, body.y + 0.5*body.ky1, body);
body.ky2 = h*(body.vy + 0.5*body.kvy1);
body.kvy2 = h*yforce(body.x + 0.5*body.kx1, body.y + 0.5*body.ky1, body);
body.kx3 = h*(body.vx+ 0.5*body.kv2);
body.kv3 = h*xforce(body.x + 0.5*body.kx2, body.y + 0.5*body.ky2, body);
body.ky3 = h*(body.vy+ 0.5*body.kvy2);
body.kvy3 = h*yforce(body.x + 0.5*body.kx2, body.y + 0.5*body.ky2,body);
body.kx4 = h*(body.vx+body.kv3);
body.kv4 = h*xforce(body.x+ body.kx3, body.y + body.ky3, body);
body.ky4 = h*(body.vy + body.kvy3);
body.kvy4 = h*yforce(body.x + body.kx3, body.y + body.ky3, body);
}
void bereken(){
set();
ofstream file;
file.open("3body.txt");
for(int i =0; i <=30000; i++){
corr(maan);
corr(zon);
corr(aarde);
zon.update();
aarde.update();
maan.update();
file << i*h <<" "<< zon.x << " "<< zon.y << " "<< zon.vx<< " "<< zon.vy <<" "<< aarde.x << " " << aarde.y <<" "<< aarde.vx <<" " << aarde.vy <<" "<< maan.x<<" "<<maan.y<<"\n";
}
file.close();
}
int main()
{
set();
bereken();
system("pause");
return 0;
}
答案 0 :(得分:1)
只需使用参考:
void corr(particle& body)
必须在其他地方应用相同的修复程序。您目前拥有的代码是 COPIES 您的对象,对其执行一些计算然后只删除此临时对象...通过使用引用(&
),您可以有效地“共享”调用者和被调用者之间的对象。
在你的类方法中你也应该使用引用传递这个对象,但这次出于效率原因 - 传递引用很简单(通常与指针相同),但复制这个巨大的对象需要时间。
答案 1 :(得分:0)
Freddie Chopin提出的建议工作并修复了函数corr()。然而,现在我发现函数xforce()和yforce()不能正常工作。它们应该返回某个粒子上的总力,这是他们在第一步中所做的,但在此之后它们似乎返回了错误的值。任何人都知道为什么会这样?