Thursday, 8 August 2013

I need my equations to overwrite data in a file every 10 iterations of a loop

I need my equations to overwrite data in a file every 10 iterations of a loop

double rho[1001], rhonew[1001];
int main(void)
{
int tstep, tmax, n, nmax, r;
double t, dt, x, dx;
dt = 0.001;
tmax = 1000;
dx = 0.1;
nmax = 1000;
rho0=1.0;
r=1;
FILE *afinal;
afinal = fopen("afinal.txt","w");
FILE *amid;
amid = fopen("amid.txt","w");
for (n = 0; n <= nmax; n++)
{
rho[n] = 500;
}
for (n = 0; n <= nmax; n++)
{
rhonew[n] = 1;
}
for (tstep=1; tstep<=tmax; tstep++)
{
rho[tstep] += -tstep;
if(tstep == r*10)
//I want this if statement to execute every 10 "tsteps" to overwrite the
data in amid.txt
{
for (n = 0; n <= nmax; n++)
{
x = n*dx;
fprintf(amid, "%f \t %f \n", x, rho[n]);
}
fclose(amid);
r++;
}
}
for (n = 0; n <= nmax; n++)
{
x = n*dx;
fprintf(afinal, "%f \t %f \n", x, rho[n]);
}
fclose(afinal);
return 0;
}
My array "amid" only writes once, but I want it to write information, then
overwrite that old information with new information several times within
the greater "tmax" loop. With this, I want to graph snapshots of my data
through gnuplot "over time" so I can watch my differential equations' work
evolve.

No comments:

Post a Comment