CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM

writing a scalar in the output

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 20, 2013, 19:30
Default writing a scalar in the output
  #1
Senior Member
 
Mahdi Hosseinali
Join Date: Apr 2009
Location: NB, Canada
Posts: 273
Rep Power: 17
anishtain4 is on a distinguished road
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?

Last edited by anishtain4; January 20, 2013 at 19:46.
anishtain4 is offline   Reply With Quote

Old   January 21, 2013, 03:53
Default
  #2
Senior Member
 
Bernhard
Join Date: Sep 2009
Location: Delft
Posts: 790
Rep Power: 21
Bernhard is on a distinguished road
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?
Bernhard is offline   Reply With Quote

Old   January 21, 2013, 10:07
Default
  #3
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by anishtain4 View Post
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
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Old   January 22, 2013, 08:59
Default
  #4
Senior Member
 
Mahdi Hosseinali
Join Date: Apr 2009
Location: NB, Canada
Posts: 273
Rep Power: 17
anishtain4 is on a distinguished road
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 is offline   Reply With Quote

Old   January 22, 2013, 09:38
Default
  #5
Senior Member
 
Mahdi Hosseinali
Join Date: Apr 2009
Location: NB, Canada
Posts: 273
Rep Power: 17
anishtain4 is on a distinguished road
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
anishtain4 is offline   Reply With Quote

Old   January 22, 2013, 14:44
Default
  #6
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by anishtain4 View Post
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 View Post
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 View Post
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 View Post
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
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Old   January 25, 2013, 05:16
Default
  #7
Senior Member
 
Mahdi Hosseinali
Join Date: Apr 2009
Location: NB, Canada
Posts: 273
Rep Power: 17
anishtain4 is on a distinguished road
Thanks bernard, I will take a look at it right after I finish my thesis
anishtain4 is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Mix AIR and WATER Elvis1991 FLUENT 12 December 1, 2016 13:28
dieselFoam problem!! trying to introduce a new heat transfer model vivek070176 OpenFOAM Programming & Development 10 December 24, 2014 00:48
compressible flow in turbocharger riesotto OpenFOAM 50 May 26, 2014 02:47
writing Force in output file emreg FLUENT 0 May 25, 2011 15:53
Writing scalar to file Tobb OpenFOAM Programming & Development 6 December 1, 2010 17:19


All times are GMT -4. The time now is 21:12.