@dongwang, how did you manage to run OpenFOAM without knowing much c++?
Read each line into a string using getline(). Ignore them all until you come to the one with the "internalField" indicator.
After reading the next line into a string, stringstream it into an int N, the number of data points.
Skip the next line if it's just a (.
Then loop through the next N lines, stringstreaming the result of getline() into char, double, double, double. Those doubles will be the velocity vector components that you want. You can process them however you want as soon as you've read them.
Here's a basic example that simply reads the file and writes the velocity vectors to screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
void readFoamFile( istream &strm )
{
string line;
int N;
// Read lines; the while loop will stop when it contains "internalField"
while ( getline( strm, line ) && line.find( "internalField" ) == string::npos ) ;
getline( strm, line ); stringstream( line ) >> N; // Get the number of data points
getline( strm, line ); // Ignore the next line
// Loop through the data points, grabbing the velocity vector (U,V,W)
char c;
double u, v, w;
for ( int i = 0; i < N; i++ )
{
getline( strm, line );
stringstream( line ) >> c >> u >> v >> w;
// Do whatever you want with u, v, w; I'll just write to screen
cout << u << " " << v << " " << w << '\n';
}
}
int main()
{
string filename = "foamFile"; // or whatever
// ifstream strm( filename ); // You want to read from file
stringstream strm( "FoamFile \n" // I'll fake it with a stringstream
"{ \n"
"version 2.0; \n"
"format ascii; \n"
"class volVectorField; \n"
"location 401; \n"
"object U; \n"
"} \n"
" \n"
"dimensions [0 1 -1 0 0 0 0]; \n"
" \n"
"internalField nonuniform List<vector>\n"
"5 \n"
"( \n"
"(1.01069 0.0216525 -0.00441842) \n"
"(1.00634 0.00667446 -0.000859575) \n"
"(1.00557 0.00505487 -0.000599214) \n"
"(1.00487 0.00373986 -0.000414328) \n"
"(1.00213 6.22804e-05 4.43194e-06) \n"
") \n"
"; \n" );
readFoamFile( strm );
}
| |
1.01069 0.0216525 -0.00441842
1.00634 0.00667446 -0.000859575
1.00557 0.00505487 -0.000599214
1.00487 0.00373986 -0.000414328
1.00213 6.22804e-005 4.43194e-006 |
That great repository of everything CFD, CFD Online, also has a forum devoted to OpenFOAM:
https://www.cfd-online.com/Forums/openfoam/