CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Question about codeData in a codedFunctionObject (https://www.cfd-online.com/Forums/openfoam-programming-development/233824-question-about-codedata-codedfunctionobject.html)

mAlletto February 13, 2021 03:49

Question about codeData in a codedFunctionObject
 
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::ofstream is used but not in the case of autoPtr<OFstream>.


My question is: why is this so?


Best


Michael


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