How to save solution in a loop?

Hello everyone,
I want to save solutions at each time iteration of heat/Stokes equation. I know how to save solution for the Poisson equation with function ‘ofstream’ but I don’t know how to storage the solutions of the heat equation in a “for” loop of time iterations. I really appreciate any idea or answer!
Thank you very much!

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

Hello Robert,
Thank you very much! It works well! I am very grateful to see your answer!