CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM (http://www.cfd-online.com/Forums/openfoam/)
-   -   writing a scalar in the output (http://www.cfd-online.com/Forums/openfoam/112030-writing-scalar-output.html)

anishtain4 January 20, 2013 19:30

writing a scalar in the output
 
I'm trying to write the Uprime of the field (in regard with spatial averaging not temporal) in a simple case. In order not to change the solver I'm trying to use the functionObjects and code stream, so I have:

Code:

functions
{
    kineticEnergy
    {
        type            coded;
        functionObjectLibs ( "libutilityFunctionObjects.so" );
        redirectType        error;
        code
        #{
                const volVectorField& U = mesh().lookupObject<volVectorField>("U");
                volScalarField TKE =magSqr(U-average(U));
                IOList<scalar> TKEO
                (
                        IOobject
                        (
                                "TKE",
                                word("constant"),
                                TKE.mesh(),
                                IOobject::NO_READ,
                                IOobject::AUTO_WRITE
                        ),
                        TKE
                );
                TKEO.write();
        #};
    }
}

but my problem is that I want this value to be appended in a file after each iteration, not the whole field be applied to a file in every timeName. I tried to use fstream or OFstream but the headers are not included and you can't include a header in middle of an code stream.

IS there anyway to output a scalar from OpenFoam? can you register it in objectRegistery to handle it with IOobject? Please give me a hint, I'm so short on time

I tried to use AverageIOField but it is not defined when you wanna reach through a function object, the only way I see remained is to use IOList with size=1, still I have the problem of how to declare it? second argument (now TKE.mesh) should be objectRegistery and no scalar can be made which is part of objectRegistery. Can anyone give me a hint please?

Bernhard January 21, 2013 03:53

Well, I don't know it in the proper OpenFOAM way, but if you're looking for a working solution and don't know about the rest, I have three suggestions

1. Keep it as you have it, and just write a small script to process the output files using your bash skills (like cat)
2. Use regular C++ file i/o, which you can just use to output to a single file
3. Output the scalar using Info << TKE, and obtain it from the log file using grep.

But your title reads that you want to write a single scalar, but from your text I understand you write a volScalarField? Can you clarify this?

gschaider January 21, 2013 10:07

Quote:

Originally Posted by anishtain4 (Post 402990)
I'm trying to write the Uprime of the field (in regard with spatial averaging not temporal) in a simple case. In order not to change the solver I'm trying to use the functionObjects and code stream, so I have:

Code:

functions
{
    kineticEnergy
    {
        type            coded;
        functionObjectLibs ( "libutilityFunctionObjects.so" );
        redirectType        error;
        code
        #{
                const volVectorField& U = mesh().lookupObject<volVectorField>("U");
                volScalarField TKE =magSqr(U-average(U));
                IOList<scalar> TKEO
                (
                        IOobject
                        (
                                "TKE",
                                word("constant"),
                                TKE.mesh(),
                                IOobject::NO_READ,
                                IOobject::AUTO_WRITE
                        ),
                        TKE
                );
                TKEO.write();
        #};
    }
}

but my problem is that I want this value to be appended in a file after each iteration, not the whole field be applied to a file in every timeName. I tried to use fstream or OFstream but the headers are not included and you can't include a header in middle of an code stream.

IS there anyway to output a scalar from OpenFoam? can you register it in objectRegistery to handle it with IOobject? Please give me a hint, I'm so short on time

I tried to use AverageIOField but it is not defined when you wanna reach through a function object, the only way I see remained is to use IOList with size=1, still I have the problem of how to declare it? second argument (now TKE.mesh) should be objectRegistery and no scalar can be made which is part of objectRegistery. Can anyone give me a hint please?

Unless the main purpose of you endeavour is learning C++ I'd recommend using swak4Foam. These function objects (using swak4Foam) should do what you want:
Code:

                    writeTKE {
                        type expressionField;
                        autowrite true;
                        outputControl outputTime;
                        fieldName TKE;
                        expression "magSqr(U-UAvg)";
                        variables (
                            "UAvg=sum(U*vol())/sum(vol());"
                        );
                    }
                    TKEAverage {
                        type swakExpression;
                        valueType internalField;
                        expression "vol()*TKE/sum(vol())";
                        accumulations (
                          sum
                        );
                        verbose true;
                    }

The first function object calculates the TKE (I took the liberty to change the averaging of U to volume-weighted because that is what people usually mean when they say "average"). That field gets written every time data is written. What I don't fully understand is your "one value"-thing. I guess you want the average of that field. The second function-object calculates that and writes it to the terminal and to a file

anishtain4 January 22, 2013 08:59

Bernhard,
1. I'm running an LES solution which solves a lot of time steps while it writes some of them, I want to calculate this scalar (and some similar ones) while I'm solving the case to check them for all the times, so a post process after the solution won't do much good for me.
2. I've tried it before, but when you add #include <fstream> compiler complains, and that is because it would be added in the wrong place, so no chance for this solution.
3. I though this might work, to put it in Info and get it by foamLog, but unfortunately it doesn't so I have to write a script myself which I'm not so expert in that.

Gschaider:
I'm not trying to learn c++, the matter is that I already know it but not swak4Foam, can you give me a hint about it?

as a final point I want to measure turbulent kinetic energy which would be average of what I have written, I tried to use a field because it is in objectRegistry but a single scalar is not, what exactly I need is to calculate this in every time step and APPEND it to a file so I can plot it later:
0.5*average(magSqr(U-average(U)))
of course this was just one of the things I wanted to do, but if I learn how to prepare it for plot I can manage the rest since I have enough control over openFoam objects but not it's outputs

anishtain4 January 22, 2013 09:38

I figured out what to do with foamLog, you have to copy the file foamLog.db from its source to current folder and add a line at the end according to the instructions it has mentioned in the header.
Still I want to learn how to do it with functionObjects

gschaider January 22, 2013 14:44

Quote:

Originally Posted by anishtain4 (Post 403296)
as a final point I want to measure turbulent kinetic energy which would be average of what I have written, I tried to use a field because it is in objectRegistry but a single scalar is not, what exactly I need is to calculate this in every time step and APPEND it to a file so I can plot it later:
0.5*average(magSqr(U-average(U)))

That is exactly what the two function objects above will give you. With the difference
- the 0.5 factor is missing
- the average is volume weighted (which makes a difference if the cells in your mesh are not of the same size)
The results (as a function of time) is found in a file (it is not hard to find)

A field with magSqr(U-average(U)) is written out as a bonus. But if you don't want that you can do it all with one function object.

Quote:

Originally Posted by anishtain4 (Post 403296)
Bernhard,
1. I'm running an LES solution which solves a lot of time steps while it writes some of them, I want to calculate this scalar (and some similar ones) while I'm solving the case to check them for all the times, so a post process after the solution won't do much good for me.
2. I've tried it before, but when you add #include <fstream> compiler complains, and that is because it would be added in the wrong place, so no chance for this solution.
3. I though this might work, to put it in Info and get it by foamLog, but unfortunately it doesn't so I have to write a script myself which I'm not so expert in that.

One problem (from the beginning) is that you're mixing the problem description with previous solution attempts. Your description above was the first time that it became certain to me what you want to achieve

Quote:

Originally Posted by anishtain4 (Post 403296)
Gschaider:
I'm not trying to learn c++, the matter is that I already know it but not swak4Foam, can you give me a hint about it?

Go to http://openfoamwiki.net/index.php/Contrib/swak4Foam
There is a link to a presentation from the 6th OF-workshop at PSU. It should give you an overview of the capabilities it had THEN. Another nice tutorial (not by me) is https://www.hpc.ntnu.no/display/hpc/...AM+-+swak4Foam

Quote:

Originally Posted by anishtain4 (Post 403296)
of course this was just one of the things I wanted to do, but if I learn how to prepare it for plot I can manage the rest since I have enough control over openFoam objects but not it's outputs


anishtain4 January 25, 2013 05:16

Thanks bernard, I will take a look at it right after I finish my thesis


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