CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Reading values from file (https://www.cfd-online.com/Forums/openfoam-programming-development/148329-reading-values-file.html)

Tobi February 9, 2015 12:43

Reading values from file
 
Hi all,

I want to read a file with FOAM classes. I checked out the boundary condition "timeVaryingMappedFixedValue" but I did not get the point how to read a file and store the values in an array (scalarField etc.)

At the moment I created an IFstream:
Code:


const fileName pathToFile = "foobar";

IFstream foo(pathToFile);

But now I do not know how to read that file.


Thanks in advance
Tobi

ssss February 9, 2015 15:43

Dear Tobi,

As I can see in the openfoam code (IFstream.H - .C) there are practically no implemented functions for the IFstream class.

Why don't you use the original ifstream C++ class? You can read everything with it.

Tobi February 9, 2015 16:53

Because I wanna use FOAM classes :)
But yes as you told, it seems to be no implementation of functions for that file but it sdhould be possible to do sth. like that.

alexeym February 10, 2015 03:02

Hi,

Can you please clarify several moments:

1. You'd like to read values into array (i.e. just a sequence of values) or it should be a Field (i.e. values have spatial variations and linked to the mesh)?

2. You'd like to read ASCII file, binary file?

3. What is wrong with ISstream's read method? http://foam.sourceforge.net/docs/cpp...e693cadb1d0305

4. If I am not mistaken there are also overloaded right shift operators.

4. Finally there is DataEntry class which can read values from OpenFOAM's dictionaries/CSV files. Though in this case it's more complicated as the class adds interpolation.

Tobi February 10, 2015 03:20

Hi Alex,

thanks for your reply. Did not know why I am not using ISstream. Yesterday I had a look at Doxygen and got the point that ISstream would be a good class (necessary functions available like getLine() ...).

I will test it today in the evening. At least to your questions:

  • The values are like a list of different scalars like:
Code:

#List which has to be read
#
#
1e4
1e2
0
-1e2
-1e5
-3e5


  • This file is read in ascii format but I also has to read binary files


Thanks in advance,

alexeym February 10, 2015 04:03

Hi,

Quote:

Originally Posted by Tobi (Post 531193)
thanks for your replay. Did not know why I am not using ISstream. Yesterday I had a look to Doxygen and get the point that ISstream would be a good class (necessary functions available like getLine() ...).

IFstream is a child of ISstream (see inheritance diagram: http://foam.sourceforge.net/docs/cpp/a01016.html), so public method of ISstream are available in IFstream.

In case of ASCII files solution if straightforward, read line, if it is not a comment try to parse number.

In case of binary files everything is a little bit more complicated. Read into char buffer, convert into scalar. Though I'm not quite sure that is not an invention of the wheel.

About timeVaryingMappedFixedValue, list is read during construction of this property of the class:

Code:

autoPtr<DataEntry<Type> > offset_;
and then it is used in the code like this

Code:

offset_->value(t)
but as I said earlier DataEntry assumes value pairs (time, value).

Tobi February 10, 2015 04:37

Thank you again.
Well I am stupid because I checked the Doxygen and saw that IFstream is a child of ISstream but in the header I checked the wrong class which has no derivation to ISstream. After your hints I checked it again and now I get the lines:
Code:

  82 class IFstream    83 :
    84    public IFstreamAllocator,
    85    public ISstream
    86 {

So then it should be possible to use IFstream and use the ISstream functionallity. I try it today in the evening.


All times are GMT -4. The time now is 13:55.