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

Question about codeData in a codedFunctionObject

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   February 13, 2021, 03:49
Default Question about codeData in a codedFunctionObject
  #1
Senior Member
 
Michael Alletto
Join Date: Jun 2018
Location: Bremen
Posts: 615
Rep Power: 15
mAlletto will become famous soon enough
I have following coded function object:



Code:

    centerOfGravityAir
    {
        libs        ("libutilityFunctionObjects.so");

        type coded;
        // Name of on-the-fly generated functionObject
        name writeCenterOfGravityAir;
        writeControl  timeStep;
        
        executeControl timeStep;
        
        codeOptions
        #{
            -I$(LIB_SRC)/finiteVolume/lnInclude \
            -I$(LIB_SRC)/OpenFOAM/lnInclude
        #};


        codeInclude
        #{
            #include "volFieldsFwd.H"
            #include "OFstream.H"
        #};
        
        codeData
        #{
              fileName outPutDir;
              std::ofstream fout;
              autoPtr<OFstream> outputFilePtr;
        #};
            
        codeRead
        #{
           
             outputFilePtr.reset(new OFstream("centerOfGravityAir.dat"));
             fout.open("centerOfGravityAir.txt");
           
        #};
        
        codeExecute
        #{
        
             const volScalarField& alpha1 = mesh().lookupObject<volScalarField>("alpha.water");
             
             scalar alphaTot (0.0);
             scalar cOG (0.0);
             
             
             forAll(alpha1,cellI)
             {
                 alphaTot += 1.0 - alpha1[cellI];
                 cOG += (1.0 - alpha1[cellI])*alpha1.mesh().C()[cellI].z();
             }
             
             reduce(alphaTot, sumOp<scalar>());
             reduce(cOG, sumOp<scalar>());
             
             cOG /= alphaTot;
            
             if (Pstream::myProcNo() == 0)
             {
            
                 fout << mesh().time().timeName() << " " << cOG << endl;
                 outputFilePtr() << mesh().time().timeName() << " " << cOG << endl;
             }
             
             Info << "cog = "  << mesh().time().timeName() << " " << cOG << endl;
             
        #};
        
        codeWrite
        #{

        #};
    }
I have a two-phase flow simulation with a bubble exiting from an orifice.



The purpose of the function object is to write the center of gravity in z-direction of the air to a file.



I have two file: one centerOfGravity.dat and one centerofGravity.txt.


centerofGravity.txt is opened with an ofstream of c++ and the other one with and autoPtr<OFstream>. For the latter works and the former not.


Apparently the reference to the variable gets somewhere lost if an std:fstream is used but not in the case of autoPtr<OFstream>.


My question is: why is this so?


Best


Michael
mAlletto 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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[swak4Foam] Difference in calculating div(U*U) swak4FOAM vs. OpenFOAM codedFunctionObject polo OpenFOAM Community Contributions 7 October 1, 2013 10:22
small question about the functionalities of topological changes in OpenFoam ngj OpenFOAM Running, Solving & CFD 2 February 28, 2013 10:02
Question Re Engineering Data Source imnull ANSYS 0 March 5, 2012 13:51
internal field question - PitzDaily Case atareen64 OpenFOAM Running, Solving & CFD 2 January 26, 2011 15:26
Poisson Solver question Suresh Main CFD Forum 3 August 12, 2005 04:37


All times are GMT -4. The time now is 18:15.