CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM (
-   -   Reading a arbitrary file in OpenFoam (

fs82 December 9, 2009 06:11

Reading a arbitrary file in OpenFoam

I have to ask a question concerning IO in OpenFoam. I have to "convert" some date to OpenFoam and after thinking a while about the problem I want to do this in a OpenFoam utility. First I thought lets do it in Fortran but the problem of reading the foam mesh comes up and seems to be very time expensive. So now my question:

I already noticed that there is the possibility of reading a arbitrary List of Scalars from a dictionary. But I am not used to C++ so I am not able to understand at the moment what have to be done to read a List from a dictionary. Is there somewhere a tutorial or a simple example like for new turbulence models? May be someone has done this before and commented the code, that I could understand how to adopt this to my problem?


olesen December 10, 2009 03:41

There are lots of cases of reading lists from dictionaries, but if you just have lists you'd probably want to read them directly and not via a dictionary.

It would be best if you gave an indication of what you want to read or parse, and then we might be able to provide suggestions.

fs82 December 10, 2009 06:13

Thx a lot but I think I found a suitable solution for me in plot3dToFoam.C. There is a way presented how to read in coordinates of a simple block structured mesh and this is easy to adopt to read in data on a block structured equally spaced mesh. Thx for your help.


fs82 December 10, 2009 07:47

Stop I still have a question. I opened my file in the following way:

fileName dataFile(args.additionalArgs()[0]); // set input argument as filename
IFstream dataStream(dataFile);

But after reading I do not need this file anymore so I want to close it. How do I close the file? There is no function available like dataStream.close()?


eelcovv May 28, 2010 09:50

Hi Fabian,

I am trying to achieve more or less the same: I want to import an external data field with columns of x y z data and interpolate that on a openFoam field. Preferably with an utility that then writes the new openfoam field to the 0 directory
Have you achieved already what wanted? Perhaps I can use it as well


benk May 28, 2010 10:35

I also am interersted in this since I need to import data into OpenFOAM for interpolation purposes.

In my case I have experimental data that describes how a diffusion coefficient changes with concentration and temperature. My model solves for both concentration and temperature and so I'd like to be able to import this diffusion data into OpenFOAM and then interpolate in two directions, concentration first and then temperature.

fs82 May 31, 2010 02:57

Hello Guys,

I used the foam Utility plot3dToFoam as an example. I do not use any interpolation because I only read in a scalar which is than averaged over a range of foam cells. So my problem was to find an efficient routine to search for cells on the OpenFoam mesh. But there are ways to interpolate between different meshes in OpenFoam. Please search the forum for this purpose, I am sure you will find something.

kind regards,

eelcovv June 4, 2010 07:36

source code
Hi Fabian,

I am not really experienced in C++ programming. I can understand what happens in Plot3dToFoam, but in this program a plot3d mesh is read and a new mesh is created based on the plot3d mesh.
In my case I want to read the plot3d data (also on a regular grid), but then map the plot3d data on the existing openfoam grid data. Is your code something simular to this ? Would it be possible to post your source so I can use this as an example for my problem?



fs82 June 4, 2010 09:43

I am sorry but I will not post my source code because it includes many ideas not published up to now and therefore it is still top secret :-D But as I mentioned before my solution seems to be not suitable for you because my mapping scheme is a simple averaging.
I would suggest as a first simple idea to create a mesh for OpenFOAM which has the same regular grid like the data you want to import (use blockmesh oder gambit). Than have a look to plot3dToFoam and read in your datafile together with the created foam mesh. Than loop over all foam cells and find the related point of your data. After you have done so write the created field and use mapFields to map this mesh to your desired simulation mesh. This is not easy to achieve even if you have no real experience in C++ but I am not an C++ expert too. I started learning it last year and with the help of this forum an the dokumentation site of OpenFOAM it was feasible for me and therefore it should be feasible to you too :-D


All times are GMT -4. The time now is 11:27.