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/)
-   -   Problem with writeIntegratedHeader functionObject in parallel processing (https://www.cfd-online.com/Forums/openfoam-programming-development/236150-problem-writeintegratedheader-functionobject-parallel-processing.html)

Shibi May 16, 2021 07:12

Problem with writeIntegratedHeader functionObject in parallel processing
 
Hello to all,

I am doing a functionObject (FO) for analyzing some data in OpenFoam . I took the forceCoeffs FO as a starting point.

However, I am facing one difficulty. When running the FO in serial it gives me no problems, but when running the simulation in parallel I get an error saying:
Code:

[1]
[1] --> FOAM FATAL ERROR: (openfoam-2012)
[1] unallocated autoPtr of type N4Foam8OFstreamE
[1]
[1]    From T& Foam::autoPtr<T>::operator*() [with T = Foam::OFstream]
[1]    in file /home/pc/OpenFOAM/OpenFOAM-v2012/src/OpenFOAM/lnInclude/autoPtrI.H at line 154.
[1]
FOAM parallel run aborting
[1]

The script I am currently using, only writes a file called "coefficient.dat" to the post postProcessing folder.

The script:
myForceCoefs.H

Code:

#ifndef functionObjects_myForceCoeffs_H
#define functionObjects_myForceCoeffs_H

#include "fvMeshFunctionObject.H"
#include "writeFile.H"
#include "cartesianCS.H"
#include "volFieldsFwd.H"
#include "HashSet.H"
#include "Tuple2.H"
#include "OFstream.H"
#include "Switch.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{
namespace functionObjects
{

/*---------------------------------------------------------------------------*\
                        Class myForceCoeffs Declaration
\*---------------------------------------------------------------------------*/

class myForceCoeffs
:
    public fvMeshFunctionObject,
    public writeFile
{
    // Private data

        // Free-stream conditions

        // File streams

            //- Integrated coefficients
            autoPtr<OFstream> coeffFilePtr_;

    // Private Member Functions

        //- No copy construct
        myForceCoeffs(const myForceCoeffs&) = delete;

        //- No copy assignment
        void operator=(const myForceCoeffs&) = delete;


protected:

    // Protected Member Functions

        //- Create the output files
        void createFiles();

        //- Write header for integrated data
        void writeIntegratedHeader(const word& header, Ostream& os) const;

        //- Write header for binned data
        void writeBinHeader(const word& header, Ostream& os) const;

        //- Write integrated data
        void writeIntegratedData
        (
            const word& title,
            const List<Field<scalar>>& coeff
        ) const;

        //- Write binned data
        void writeBinData(const List<Field<scalar>> coeffs, Ostream& os) const;


public:

    //- Runtime type information
    TypeName("myForceCoeffs");


    // Constructors

        //- Construct from Time and dictionary
        myForceCoeffs
        (
            const word& name,
            const Time& runTime,
            const dictionary&,
            const bool readFields = true
        );


    //- Destructor
    virtual ~myForceCoeffs() = default;


    // Member Functions

        //- Read the forces data
        virtual bool read(const dictionary&);

        //- Execute
        virtual bool execute();

        //- Write the forces
        virtual bool write();
};


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace functionObjects
} // End namespace Foam

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#endif

// ************************************************************************* //

myForceCoefs.C
Code:

#include "myForceCoeffs.H"
#include "dictionary.H"
#include "Time.H"
#include "Pstream.H"
#include "IOmanip.H"
#include "fvMesh.H"
#include "dimensionedTypes.H"
#include "volFields.H"
#include "addToRunTimeSelectionTable.H"

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
namespace functionObjects
{
    defineTypeNameAndDebug(myForceCoeffs, 0);
    addToRunTimeSelectionTable(functionObject, myForceCoeffs, dictionary);
}
}


// * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //

void Foam::functionObjects::myForceCoeffs::createFiles()
{
    // Note: Only possible to create bin files after bins have been initialised

    if (writeToFile() && !coeffFilePtr_)
    {
        coeffFilePtr_ = createFile("coefficient");
        writeIntegratedHeader("Coefficients", coeffFilePtr_());
    }
}


void Foam::functionObjects::myForceCoeffs::writeIntegratedHeader
(
    const word& header,
    Ostream& os
) const
{
    writeHeader(os, "Force coefficients");
    writeHeader(os, "");
    writeCommented(os, "Time");
    writeTabbed(os, "Cd");
    os  << endl;
}


void Foam::functionObjects::myForceCoeffs::writeBinHeader
(
    const word& header,
    Ostream& os
) const
{

}


void Foam::functionObjects::myForceCoeffs::writeIntegratedData
(
    const word& title,
    const List<Field<scalar>>& coeff
) const
{
}


void Foam::functionObjects::myForceCoeffs::writeBinData
(
    const List<Field<scalar>> coeffs,
    Ostream& os
) const
{

}


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::functionObjects::myForceCoeffs::myForceCoeffs
(
    const word& name,
    const Time& runTime,
    const dictionary& dict,
    const bool readFields
)
:
    fvMeshFunctionObject(name, runTime, dict),
    writeFile(mesh_, name),
    coeffFilePtr_()
{
    if (readFields)
    {
        read(dict);
        Info<< endl;
    }
}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

bool Foam::functionObjects::myForceCoeffs::read(const dictionary& dict)
{
    return true;
}


bool Foam::functionObjects::myForceCoeffs::execute()
{
    createFiles();

    Info << "I am being executed" << endl;
    return true;
}


bool Foam::functionObjects::myForceCoeffs::write()
{
    return true;
}

I was able to track the problem to the line
Code:

writeIntegratedHeader("Coefficients", coeffFilePtr_())
However, I cannot yet understand what pointer is not allocated.


How really appreciate some help!


Best Regards

Shibi May 16, 2021 15:44

This is solved.

Had to change the createFiles function to be:

Code:


void Foam::functionObjects::myForceCoeffs::createFiles()
{
  if (Pstream::master())
  {
        if (writeToFile() && !coeffFilePtr_)
        {
            coeffFilePtr_ = createFile("coefficient");
            writeIntegratedHeader("Coefficients", coeffFilePtr_());
        }
  }
}



All times are GMT -4. The time now is 20:06.