CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Programming & Development (
-   -   Writing scalar to file (

Tobb November 11, 2010 08:19

Writing scalar to file
Hello everybody!

I think I got a comparatively easy problem, but I can't find a solution.

I have a program that calculates the power inserted by an impeller for every time step. But at the moment the value is just displayed bei "Info <<...... " on the screen. I'd like to write it into a file as kind of a list, with the time and the corresponding power value, so that I can easily plot it with gnuplot.

I tried something with a IOdictionary but did'nt get very far. And I don't really know how to handle the list class.

I would be glad if someone could tell me some details oder just show me where I can find an introduction of that topic.

Thank you!

l_r_mcglashan November 11, 2010 08:51

To be honest, if you just want a file with 2 columns (Time and power), just use the standard library. Change Info to a file, so something like:


    std::ofstream file("results.txt");
    file << Time[i] << " " << Power[i] << std::endl;

Where I've assumed Time and Power are lists.

Tobb November 11, 2010 09:39

First of all thank you for your answer. But if I got you right, you basically told me how to merge two existing lists into one file.

So I think I have to be more precise. My routine is built as a loop. It reads in the velocity field, calculates the power (using rho, nu and the gradient of U) and than moves on to the next time directory. So I actually don't have any time or power list.

My aim is to add the current time and the corresponding power value to a file after each loop. So the list or the file where the data should be stored doesn't exist right from the start. I have to create it either before starting my program or implement the construction once right at the beginning of the code and than add every entry step by step.

But the construction of a such a list as well as writing data to a file are things I haven't really done before.
I hope I managed to make things clearer now.

Thank you in advance!

nimasam November 11, 2010 09:58

u can use Info, but save the result in a log file

solver > log

then use foamlog ofcourse you should add new written variable in foamLog data base
so i suggest : search for foamlog

Fransje November 19, 2010 08:38

Hi Tobb!

If you want a file with only two columns, you could use the makeGraph function! You can specify:

makeGraph(Time, Power, "Your_Name", path, graphFormat);
And this will create a file Your_name.xy at every timestep, in the timestep directory. If you make sure your Time and Power variable can be stored during the whole run, you might be able to call makeGraph only for the final timestep, and let it print out the two lists in one file instead of at every timestep.

The format can be obtained from your runTime object with the command:

const word& graphFormat = runTime.graphFormat();
The path can be asked from one of your variables, for instance with:

Good luck!

Kind regards,


Tobb November 19, 2010 12:55

Thank you very much for your hint!
It sounds like this could be what I was looking for. I will try it out after the weekend and let you know whether I was successful or not. ;)

Fransje December 1, 2010 17:19

You can also replace the graphFormat option directly by (keep the quotes):
  • "raw" -> giving raw xy graph outputs in .xy files.
  • "gnuplot" -> giving gnuplot compatible .gplt files.
  • "jplot" -> giving jplot .dat files.
  • "xmgr" -> giving xmgrace compatible .agr files.

Kind regards,


All times are GMT -4. The time now is 04:15.