gpt4 book ai didi

c++ - ofstream 不会将不同的数据写入两个不同的文件

转载 作者:行者123 更新时间:2023-11-30 05:18:31 25 4
gpt4 key购买 nike

我在使用 ofstream 时遇到问题一次将两个不同的输出写入两个不同的文件。程序编译运行正常,并向p1output.txt写入数据,但是当我打开p2output.txt时,除了第一行内容外是空白的:

Time x1 x2 v1 v2 Energy Angular Momentum

如果我删除将数据写入 p1output.txt 的代码行,程序会正确地将数据写入 p2output.txt。我唯一能想到的是,一个接一个地拥有两个 Leapfrog 函数会以某种方式阻止第二个函数执行和打印数据。我不知道这是为什么或如何解决它 - 有人可以帮忙吗?

代码如下:

#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;

#define D 2 // number of dimensions
struct particle
{
double x[D] ; // (x,y) coordinates
double v[D] ; // velocity
double F[D] ; // Force
double a[D] ; // acceleration
double GMm ; // gravitational parameter
double im ; // inverse mass
double PE ; // potential energy
double T ; // kinetic energy
double E ; // total energy
double J; // angular momentum
double r ; // distance from origin
};


void accel(particle &p_n) // acceleration of particle
{
for (int i=0; i<D; i++ )
{
p_n.a[i]=p_n.x[i]/(p_n.r*p_n.r*p_n.r) ;
}
}


void ForceEnergy(particle &p_n) //force and energy of particle
{
double R=0.0;

for(int i=0; i<D; i++)
{
R+=p_n.x[i]*p_n.x[i];
}

double r=sqrt(R);

p_n.PE=-p_n.GMm/r ; // PE of particle
p_n.r=r; // absolute distance of particle from origin

for(int i=0; i<D; i++)
{
p_n.F[i]=-p_n.GMm*p_n.x[i]/(p_n.r*p_n.r) ;
}

p_n.T=0.0;

for(int i=0; i<D; i++)
{
p_n.T+=0.5*p_n.v[i]*p_n.v[i]/p_n.im;
}

p_n.E=p_n.T+p_n.PE;

}

void VectorProduct(double x[],
double y[],
double z[],
int N) // finding the cross product of 2 vectors
{
double d[3], e[3];

for(int i=0; i<3; i++)
{
d[i]=0;
e[i]=0;
}

for(int i=0; i<N; i++)
{
d[i]=x[i];
e[i]=y[i];
}

z[0]=d[1]*e[2]-d[2]*e[1];
z[1]=d[2]*e[0]-d[0]*e[2];
z[2]=d[0]*e[1]-d[1]*e[0];
}

double VectorMag(double u[] ,
int n) // calculates magnitude of a vector
{
double vm=0;

for(int i=0; i<n; i++)
{
vm=vm+u[i]*u[i];
}

return sqrt(vm);
}

void AngMom(particle &p_n) // finding angular momentum about the origin
{
double z[3];

VectorProduct(p_n.x,
p_n.v,
z,
D);

p_n.J=VectorMag(z,3)/p_n.im;
}

void xchange(particle &p_n ,
double dt) // position increment
{
for(int i=0; i<D; i++)
{
p_n.x[i]+=dt*p_n.v[i] ;
}
}

void vchange(particle &p_n ,
double dt) // momentum increment
{
for(int i=0; i<D; i++)
{
p_n.v[i]+=dt*p_n.a[i] ;
}
}

void ParticleState(particle p_n ,
double t,
const char filename[]) // printing out the particle state
{
ofstream fxout;

fxout.open(filename,
ios::app);
if(fxout.good()==false)
{
cerr << "can't write to file " << filename << endl;
exit(0);
}
else
{
fxout << t << "\t" << p_n.x[0] << "\t" << p_n.x[1] << "\t"<< p_n.v[0] << "\t" << p_n.v[1] << "\t"<< p_n.E << "\t"<< p_n.J << endl;

fxout.close();
}

}

void Leapfrog(particle &p_n,
double dt,
double &t,
int N,
const char filename[])
{
while(t < N)
{
ParticleState(p_n,
t,
filename);
xchange(p_n,
dt*0.5); // position moved by a half-step
t+=0.5*dt;
accel(p_n); // computes acceleration at this position
vchange(p_n,
dt); // velocity moved by a full-step
xchange(p_n,
dt*0.5) ; // position moved by another half-step
t+=0.5*dt ;
}
}

int main()
{
particle p_1, p_2;
double dt=0.01; // time per step
double t=0.0; // start time
int N=1000; // number of time steps

p_1.im=0.02; p_2.im=0.5;
p_1.v[0]=0; p_2.v[0]=1;
p_1.v[1]=20; p_2.v[1]=20;
p_1.x[0]=4; p_2.x[0]=4;
p_1.x[1]=4; p_2.x[1]=4;

p_1.GMm=100;
p_2.GMm=100;

ForceEnergy(p_1);
accel(p_1);
AngMom(p_1);

ForceEnergy(p_2);
accel(p_2);
AngMom(p_2);

ofstream f1out;

f1out.open("p1output.txt");
f1out << "#Time" << "\t" << "x1" << "\t" << "x2" << "\t" << "v1" << "\t" << "v2" << "\t" << "Energy" << "\t" << "Angular Momentum" << endl;
f1out.close();

ofstream f2out;

f2out.open("p2output.txt");
f2out << "#Time" << "\t" << "x1" << "\t" << "x2" << "\t" << "v1" << "\t" <<"v2" << "\t" << "Energy" << "\t" << "Angular Momentum" << endl;
f2out.close();

Leapfrog(p_1,
dt,
t,
N,
"p1output.txt");

Leapfrog(p_2,
dt,
t,
N,
"p2output.txt");

return 0;
}

最佳答案

enter image description here

您通过引用获取“t”。因此,当您为 p2output.txt 编写参数时,“t”已经大于“N”。因此它不会进入 while(t < N) 循环。尝试从中删除“&” void Leapfrog(粒子 &p_n, 双dt, 成对的东西, 诠释 N, const char 文件名[])

关于c++ - ofstream 不会将不同的数据写入两个不同的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41674943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com