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/)
-   -   How can I load a file.txt in OpenFoam? (https://www.cfd-online.com/Forums/openfoam-programming-development/102198-how-can-i-load-file-txt-openfoam.html)

Marco_poli May 24, 2012 15:59

How can I load a file.txt in OpenFoam?
 
Hello,

I have a table containing the y+ u+ k+ and omega+ values for the kOmegaSST turbulence model and this table is stored in a txt file.
I want to modify the kOmegaSST.C and kOmegaSST.H to be able to load this file in the model (for example in a matrix) and take these values if need them. I'm studing a new way to impose the boundary condition but I'm quite new in programming in OpenFoam.
My main questions are:
-How could I initialize a matrix of scalar values?
-How could I load the file.txt and put its values in the matrix?
Any suggestions will be appreciate:)

Thank you very much
Best regards
Marco

JimKnopf May 25, 2012 04:47

File reading:

you can use<fstream> to read in files, like in standard c++

string line;ifstream myfile ("example.txt");
getline (myfile,line);

than convert to what ever you have.

Matrix of scalar values:

What exactly is it you expect to have? A scalarField, like p or a vector field like u?
Or a Matrix of size nxm?

Best Regards,
Jim

Marco_poli May 26, 2012 09:17

Thank you very much Jim!

Finally I solve the problem using IFstream as you suggest:

RectangularMatrix<doubleScalar> TABLE(IFstream("table.txt")());

that save the table in a matrix.
If someone is interested the file table.txt must have this shape

2 3
(
( 1 2 3)
( 4 5 6)
)

where in the first line 2 is the number of rows and 3 is the number of columns.
In this way we store the following matrix

[ 1 2 3
4 5 6]

Best Regards
Marco

pakanatiakash September 13, 2016 10:24

Hi

I guess its an old post but I thought its worth asking. I am also in the same situation where I need to read values from tables and use them in solving my equations.

I have a 2x2 matrix -

2 2
(
(-0.01 1.0e10)
(-0.005 8.e10)
);

I read it by following the same routine -

RectangularMatrix<doubleScalar>NucTuc(IFstream("co nstant/innocValues")());

NucTuc is my matrix. Now the question is, after reading the table, how can i access each value. For example I want to access the value -0.01 which is located in 1x1 position, how do I do it?

Thanks!

Akash

S.Colucci May 10, 2017 07:24

Hi Akash,
did you find a solution to get the values from a RectangularMatrix?
Thanks
Simone

stanweer December 21, 2017 04:59

You can access the element -0.01 as:
NucTuc(0,0);

Antimony January 31, 2018 22:34

Hi,

Just to add on to the last post. I don't think NucTuc(0,0) works. What worked for me was NucTuc[0][0] - the standard C++ way to traverse array/matrices.

Hope this helps.

Cheers,
Antimony

stanweer February 1, 2018 07:19

Hi Antimony,
I am using OpenFOAM 4.1 and in my case NucTuc(0,0) works perfectly.

best regards,
Shayan Tanweer

feiyuyang April 3, 2018 13:48

Quote:

Originally Posted by Marco_poli (Post 363235)
Thank you very much Jim!

Finally I solve the problem using IFstream as you suggest:

RectangularMatrix<doubleScalar> TABLE(IFstream("table.txt")());

that save the table in a matrix.
If someone is interested the file table.txt must have this shape

2 3
(
( 1 2 3)
( 4 5 6)
)

where in the first line 2 is the number of rows and 3 is the number of columns.
In this way we store the following matrix

[ 1 2 3
4 5 6]

Best Regards
Marco

Hello Marco_poli,
I am a new OF user. When I try to use your method to read a matrix, I get "‘IFstream’ was not declared in this scope" error, should I include some header file ? Thank you.

Best Regards
Feiyu


All times are GMT -4. The time now is 09:46.