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

Variable declaration and initialisation revisited

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 2 Post By GerhardHolzinger

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   August 30, 2012, 07:19
Default Variable declaration and initialisation revisited
  #1
Senior Member
 
Gerhard Holzinger
Join Date: Feb 2012
Location: Austria
Posts: 339
Rep Power: 28
GerhardHolzinger will become famous soon enoughGerhardHolzinger will become famous soon enough
Previously on ...

http://www.cfd-online.com/Forums/ope...rocessing.html

http://www.cfd-online.com/Forums/ope...alization.html

In the first thread there is a discussion about using functionObjects for postprocessing. In this thread there is somewhere the source code of a tool posted, which works in the described way.

One downside of this tool is, that the fields which are read directly coded. I was playing around with the code to make it possible to use this tool even if some fields are not present.

The first section of code is of the original code:
Code:
Info<< "Reading field U\n" << endl;
    volVectorField U
    (
        IOobject
        (
            "U",
            runTime.timeName(),
            mesh,
            IOobject::READ_IF_PRESENT,
            IOobject::NO_WRITE
        ),
        mesh
    );
If fields are read this way, one has to program two versions of this tool. One for single phase problems (p and U) and one for multiphase problems (p, Ua and Ub or p, U1 and U2 respectively).

I tried to make a tool that reads fields if they are present. It is still somewhat hardcoded, for all the possible fields are listed in the source code. Somewhere in this forum I found this way to handle conditional reading of the fields.

Code:
IOobject UbHeader
    (
        "Ub",
        runTime.timeName(),
        mesh,
        IOobject::NO_READ
    );

    volVectorField* UbPointer;
    bool UbExist = false;

    if (UbHeader.headerOk())
    {
        UbExist = true;
        Info<< "Reading Ub.\n" << endl;

        UbPointer = new volVectorField
        (
            IOobject
            (
                "Ub",
                runTime.timeName(),
                mesh,
                IOobject::MUST_READ,
                IOobject::AUTO_WRITE
            ),
            mesh
        );
    }
This code happens to work, but it only works once. The field Ub is only read once. The output of a probe looks like this

Code:
#           x        0.0254
#           y        0.0253
#           z             0
#        Time
            0             (0 0 0)
       0.0001             (0 0 0)
       0.0002             (0 0 0)
       0.0003             (0 0 0)
       0.0004             (0 0 0)
       0.0005             (0 0 0)
       0.0006             (0 0 0)
       0.0007             (0 0 0)
In the second thread above, there is another way of conditionally reading fields presented.

Code:
IOobject UaHeader
    (
        "Ua",
        runTime.timeName(),
        mesh,
        IOobject::NO_READ
    );

    volVectorField* UaPointer;
    bool UaExist = false;
    
    autoPtr<volVectorField> Ua;

    if (UaHeader.headerOk())
    {
        UaExist = true;
        Info<< "Reading Ua.\n" << endl;

        //UaPointer = new volVectorField
        Ua.set(new volVectorField
        (
            IOobject
            (
                "Ua",
                runTime.timeName(),
                mesh,
                IOobject::MUST_READ,
                IOobject::AUTO_WRITE
            ),
            mesh
        ));
    }
This code uses an autoPointer and the output of a probe looks this way

Code:
#           x        0.0254
#           y        0.0253
#           z             0
#        Time
            0             (0 0 0)
       0.0001             (-0.095783 -0.0959882 0.251371)
       0.0002             (-0.0163107 -0.0163035 0.306008)
       0.0003             (-0.00224914 -0.00223754 0.320491)
       0.0004             (-0.000290503 -0.000285572 0.333185)
       0.0005             (-0.000397366 -0.000393842 0.343691)
       0.0006             (-0.000691265 -0.000688015 0.353534)
       0.0007             (-0.000787074 -0.000783954 0.362863)

I don't know the reason why conditional reading of fields behaves differently in the two cases. When using autoPointers it works, when not using autoPointers the tool reads only the first time step and uses this value further on.

Attached you will find a modified version of the postAverage utility of Eelco van Vliet. All possible fields that can be read have their lines of code in the createFields.H file. If a field exists it is read, if it does not exist, no attempt of reading it is made. Consequently the tool will run without crashing when a field is not present.


A possible next step would be to define the fields to read in a dictionary and program a somehow generic version createFields.H to enable the tool to read any field defined by the user.
Attached Files
File Type: h createFields.H (6.9 KB, 50 views)
File Type: c postAverageTest.C (2.1 KB, 68 views)
Pagoda and beatlejuice like this.
GerhardHolzinger is offline   Reply With Quote

Reply


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



All times are GMT -4. The time now is 22:48.