How to save solution in a loop?

Hello lietvo,

You could try the following:

int iter=.... // number of iterations
int i;
for(int i = 0; i<=iter; i++)
{
...
   {
    ofstream file("Heat.txt",append);
    file<<Heat<<endl; // or Heat(x,y) if you have a desired monitoring point
   }
}

“append” should save the solution at each iteration using the same file.

Or, If you need separated files

int iter=.... // number of iterations
int i;
for(int i = 0; i<=iter; i++)
{
...
   {
    ofstream file("Heat"+i+".txt");
    file<<Heat<<endl; 
   }
}

Additionally you could add “if” conditions before ofstream to select a range of iterations.

Hope it helps,
Robert

1 Like