CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Post-Processing (http://www.cfd-online.com/Forums/openfoam-post-processing/)
-   -   How to create points and field files? (http://www.cfd-online.com/Forums/openfoam-post-processing/66571-how-create-points-field-files.html)

sega July 19, 2009 10:53

How to create points and field files?
 
Hello again.

Can somebody tell me how I can manage to create points files and field files?
I looked through the sample tool, which can create foamFile outputs, but I haven't found the corresponding code.

What I want to do, is to
  • read a field value at different specific location inside the domain,
  • put the value inside a field file
  • and its location in a points file
So, the first problem I'm searching a solution for is how to store the values obtained from this loop:

Code:

    surfaceScalarField ps = fvc::interpolate(p);

    for (label k=0; k<facesSet.size(); k++)
    {
        int faceNumber = faces[k];
        Info<< "Entry " << k << " equals "<< ps[faceNumber]<< endl;
    };

I want the different values of ps[faceNumber] to be stored and somehow transfered to the mentioned field file and its corresponding points file.

Does anybody know, how this can be done?

henrik July 19, 2009 14:34

Sebastian,

I think you intended to do something like

Code:

for (label k=0; k<facesSet.size(); k++)
{
    label faceNumber = facesSet[k];
    // copy data
}

but a faceSet is a labelHash. Therefore, you better use iterators

Code:

forAllConstIter(faceSet, facesSet, f)
{
    label faceNumber = f.key();
    // copy data
}

OR ask for the table of content

Code:

const labelList faces = facesSet.toc();
for (label k=0; k<faces.size(); k++)
{
    label faceNumber = faces[k];
    // copy data
}

OR do something foamish like:

Code:

scalarField data(0);
vectorField pnts(0);
const labelList faces = facesSet.toc();
data.map(ps, faces);
pnts.map(mesh.Cf(), faces);

I haven't tested any of this and knowing myself none of this will work straightaway. But it should work along these principals.

Henrik

sega July 19, 2009 17:08

Dear Henrik.

Maybe we are misunderstanding each other.
I already have a way of accessing the field data of interest.

My question is how I can manage to make my tool output these field data and its corresponding coordinates into a single points file and a single field value file.
Similar to the sample tools foamFile output.

Hope this makes my interest a little bit clearer.
S.

henrik July 19, 2009 18:12

Dear Sebastian,

Code:

OFstream os ("points");
os << pnts;

Henrik

sega July 20, 2009 07:38

Quote:

Originally Posted by henrik (Post 223212)
Dear Sebastian,

Code:

OFstream os ("points");
os << pnts;

Henrik

Hi Henrik.

I assume "points" is the name of the variable containing the coordinates, while
Code:

os << pnts;
outputs the file?

If this is correct it will lead to two further questions:

1) If I work through the above loop which contains some different face values how do I store them in an appropriate array before outputting them to a file?

I would do this in some kind of MATLAB style:
Code:

// creating an empty field
scalarField pf(0);

// accessing the new field with the loop index k
// and putting the previous read value into this new place
pf[k] = ps[faceNumber]

2) What is the code for the faceCenter coordinate?
I have tried
Code:

ps.Cf()[k]
but this doesn't seem to be right?

henrik July 20, 2009 08:05

Dear Sebastian,

Quote:

I assume "points" is the name of the variable containing the coordinates, while outputs the file?
C++ variable names are never enclosed in brackets!

"points" is the name of the file at the other end of that OFstream (os). pnts is the name of the variable to be output to this stream (or file).

@1) There is a DynamicList container in OF, but I suggest you allocate the memory and then fill the array because you know how long the Field will be.

Code:

vectorField pnts(facesSet.size());
scalarField data(facesSet.size());

The good thing about map in the third exaple is that it will do the resizing for you.

@2) Try mesh.Cf()()[k]

Quote:

I already have a way of accessing the field data of interest.
Are you sure? :p

Henrik

sega July 20, 2009 08:34

Quote:

Originally Posted by henrik (Post 223296)
Try mesh.Cf()()[k]

Code:

Info<< "Cf(): "<< mesh.Cf()()[k] << endl;
Leads to this message while compiling:

Code:

error: no match for call to ‘(const Foam::GeometricField<Foam::Vector<double>, Foam::fvsPatchField, Foam::surfaceMesh>) ()’

sega July 20, 2009 08:39

Quote:

Originally Posted by henrik (Post 223296)
"points" is the name of the file at the other end of that OFstream (os). pnts is the name of the variable to be output to this stream (or file).

Looks like this is working, but
Code:

        OFstream os ("p");
        os << faceCenterPressure;

Just gives me a single file in the case directory, which is overwritten at each timestep.

How can I manage to put it inside of separate subdirectory, which contains also the time step name?!


Something like
Code:

    OFstream os("data/" + runTime.timeName() + "/p")
didn't work out which means there is no file written.
Maybe the runTime.timeName() has to be rewritten into a string?!

henrik July 20, 2009 08:49

You are on the right track, but you need to create the directory structure.

$FOAM_SRC/postProcessing/forces/forces/forces.C

Henrik

henrik July 20, 2009 08:54

And what was the error message for mesh.Cf()[k]?

sega July 20, 2009 09:07

Quote:

Originally Posted by henrik (Post 223305)
And what was the error message for mesh.Cf()[k]?

There is none. Maybe there was something else wrong.

sega July 21, 2009 13:57

Quote:

Originally Posted by henrik (Post 223303)
You are on the right track, but you need to create the directory structure.

$FOAM_SRC/postProcessing/forces/forces/forces.C

Henrik

Ok Henrik.
I had a look into the forces.C file, but in spite of thinking I got it, I didn't.

I'm not able to handle the expression for the directory structure.

Code:

fileName directoryP = "/data/"runTime.timeName()"/p";
is my best guess, which is not working
Code:

error: expected ‘,’ or ‘;’ before ‘runTime’
even with a comma before and/or after...

What may be wrong?

henrik July 21, 2009 14:09

Sebastian,

the "/"-operators are overloaded for fileName. So

Code:

fileName directoryP = runTime.timeName()/p;
is NOT a division, but will - on a single processor run - yield something like

[case_dir]/[time]/p

which is close to what you are looking for.

Henrik

sega July 21, 2009 16:36

1 Attachment(s)
Quote:

Originally Posted by henrik (Post 223518)
yield something like

[case_dir]/[time]/p

Unfortunately not. This exact statement outputs the attached message while compiling.

What if I want to add some directory before the time-step name?
Something like

[Case-Dir]/data/0.01/p

so the written file will not interfere with the actual timesteps...

henrik July 21, 2009 16:48

Sebastian,

you had too many and I was missing some :). Very foamish error message, thus :D. Let's settle for a compromise ;)

Code:

fileName directoryP = runTime.path()/"data"/runTime.time()/"p";
Make sure to create the directory!

Henrik

sega July 21, 2009 17:08

1 Attachment(s)
Quote:

Originally Posted by henrik (Post 223537)

Code:

fileName directoryP = runTime.path()/"data"/runTime.time()/"p";

Nope.
Code:

error: no match for ‘operator/’ in ‘Foam::operator/(const Foam::string&, const Foam::string&)(((const Foam::string&)(& Foam::string(((const char*)"data"))))) / runTime.Foam::Time::<anonymous>.Foam::objectRegistry::time()’
I think we should reconsider in the morning ...

henrik July 21, 2009 18:10

Yes, indeed:

Code:

fileName directoryP = runTime.path()/"data"/runTime.timeName()/"p";

sega July 22, 2009 02:21

Yes its working!
Great work!

Tell me, is this basic C++ or OpenFOAM related knowledge?

sega July 22, 2009 02:43

If you don't mind I will draw you attention to my post about file headers, which is connected to the matters discussed here:
http://www.cfd-online.com/Forums/ope...tml#post223590

henrik July 22, 2009 03:16

Sebastian,

first of all, I have the feeling, that the error messages make little sense to you. Maybe you could go back, break this code and analyse the error messages.

Have a look into forces.C again. In fact, this statement looks very similar to what you where looking for.
Code:

forcesDir = obr_.time().path()/name_/obr_.time().timeName();
Ask yourself, what went wrong when I modified it to
Code:

fileName directoryP = "/data/"runTime.timeName()"/p";
Where are the operators?

The next problem, and you are not alone, is how to find your way in the library. There are many options. For example Doxygen or you can try:
Code:

egrep \:\:timeName $FOAM_SRC/OpenFOAM/lnInclude/*
This outputs a little more than you need, but try and find the function's declaration and the associated code.

Henrik


All times are GMT -4. The time now is 01:39.