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/)
-   -   creating functionObject for uTau - compilation i.O. - application error (https://www.cfd-online.com/Forums/openfoam-programming-development/140340-creating-functionobject-utau-compilation-i-o-application-error.html)

aylalisa August 13, 2014 10:29

creating functionObject for uTau - compilation i.O. - application error
 
Hi Foamers,

I've tried to adapt a functionObject to finally get uTau during simulation.

Following error comes up once I've start the case with the 'new' FO activated:

Quote:

[2] --> FOAM FATAL ERROR:
[2] attempted to assign to a const reference to constant object of type N4Foam14GeometricFieldIdNS_12fvPatchFieldENS_7volM eshEEE
[2]
[2] From function Foam::tmp<T>::operator=(const tmp<T>&)
[2] in file /home/elisabeth/OpenFOAM/OpenFOAM-2.3.0/src/OpenFOAM/lnInclude/tmpI.H at line 306.
[2]
FOAM parallel run aborting
uTau.H
Code:

\*---------------------------------------------------------------------------*/

#ifndef uTau_H
#define uTau_H

#include "functionObjectFile.H"
#include "volFieldsFwd.H"
#include "Switch.H"
#include "OFstream.H"

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

namespace Foam
{

// Forward declaration of classes
class objectRegistry;
class dictionary;
class polyMesh;
class mapPolyMesh;
class fvMesh;

/*---------------------------------------------------------------------------*\
                      Class uTau Declaration
\*---------------------------------------------------------------------------*/

class uTau
:
    public functionObjectFile
{
protected:

    // Protected data

        //- Name of this set of uTau object
        word name_;

        const objectRegistry& obr_;

        //- on/off switch
        bool active_;

        //- Switch to send output to Info as well as to file
        Switch log_;

        //- Optional list of patches to process
        labelHashSet patchSet_;


    // Protected Member Functions

        //- File header information
        virtual void writeFileHeader(const label i);

        //- Calculate the shear stress
        void calcuTau
        (
            const fvMesh& mesh,
            volScalarField& uTau,
            const volVectorField& U,
            volScalarField nuLam

        );

        //- Disallow default bitwise copy construct
        uTau(const uTau&);

        //- Disallow default bitwise assignment
        void operator=(const uTau&);


public:

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


    // Constructors

        //- Construct for given objectRegistry and dictionary.
        //  Allow the possibility to load fields from files
        uTau
        (
            const word& name,
            const objectRegistry&,
            const dictionary&,
            const bool loadFromFiles = false
        );


    //- Destructor
    virtual ~uTau();


    // Member Functions

        //- Return name of the set of uTau
        virtual const word& name() const
        {
            return name_;
        }

        //- Read the uTau data
        virtual void read(const dictionary&);

        //- Execute, currently does nothing
        virtual void execute();

        //- Execute at the final time-loop, currently does nothing
        virtual void end();

        //- Called when time was set at the end of the Time::operator++
        virtual void timeSet();

        //- Calculate the uTau and write
        virtual void write();

        //- Update for changes of mesh
        virtual void updateMesh(const mapPolyMesh&)
        {}

        //- Update for changes of mesh
        virtual void movePoints(const polyMesh&)
        {}
};


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

} // End namespace Foam

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

#endif

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

uTau.C

Code:

\*---------------------------------------------------------------------------*/

#include "uTau.H"
#include "volFields.H"
#include "surfaceFields.H"
#include "incompressible/turbulenceModel/turbulenceModel.H"
#include "compressible/turbulenceModel/turbulenceModel.H"
#include "wallPolyPatch.H"

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

namespace Foam
{
defineTypeNameAndDebug(uTau, 0);
}


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

void Foam::uTau::writeFileHeader(const label i)
{
    // Add headers to output data
    writeHeader(file(), "uTau");
    writeCommented(file(), "Time");
    writeTabbed(file(), "patch");
    writeTabbed(file(), "min");
    writeTabbed(file(), "max");
}


void Foam::uTau::calcuTau
(
    const fvMesh& mesh,
    volScalarField& uTauField,
    const volVectorField& U,
    volScalarField nuLam
)
{
    forAllConstIter(labelHashSet, patchSet_, iter)
    {
        label patchI = iter.key();
        const polyPatch& pp = mesh.boundaryMesh()[patchI];

        scalarField& uTau_p = uTauField.boundaryField()[patchI];

        uTau_p = sqrt
                (
                    nuLam.boundaryField()[patchI]
                  *mag(U.boundaryField()[patchI].snGrad())
                );

        scalar minUTau = gMin(uTau_p);
        scalar maxUTau = gMax(uTau_p);

        if (Pstream::master())
        {
            file() << mesh.time().value()
                << token::TAB << pp.name()
                << token::TAB << minUTau
                << token::TAB << maxUTau
                << endl;
        }

        Info(log_)<< "    min/max(" << pp.name() << ") = "
            << minUTau << ", " << maxUTau << endl;
    }
}


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

Foam::uTau::uTau
(
    const word& name,
    const objectRegistry& obr,
    const dictionary& dict,
    const bool loadFromFiles
)
:
    functionObjectFile(obr, name, typeName),
    name_(name),
    obr_(obr),
    active_(true),
    log_(true),
    patchSet_()
{
    // Check if the available mesh is an fvMesh, otherwise deactivate
    if (!isA<fvMesh>(obr_))
    {
        active_ = false;
        WarningIn
        (
            "uTau::uTau"
            "("
                "const word&, "
                "const objectRegistry&, "
                "const dictionary&, "
                "const bool"
            ")"
        )  << "No fvMesh available, deactivating " << name_ << nl
            << endl;
    }

    if (active_)
    {
        const fvMesh& mesh = refCast<const fvMesh>(obr_);

        volScalarField* uTauPtr
        (
            new volScalarField
            (
                IOobject
                (
                    type(),
                    mesh.time().timeName(),
                    mesh,
                    IOobject::NO_READ,
                    IOobject::NO_WRITE
                ),
                mesh,
                dimensionedScalar
                (
                    "0",
                    dimless,
                    //sqr(dimLength)/sqr(dimTime),
                    0
                )
            )
        );

        mesh.objectRegistry::store(uTauPtr);
    }

    read(dict);
}


// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

Foam::uTau::~uTau()
{}


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

void Foam::uTau::read(const dictionary& dict)
{
    if (active_)
    {
        log_ = dict.lookupOrDefault<Switch>("log", true);

        const fvMesh& mesh = refCast<const fvMesh>(obr_);
        const polyBoundaryMesh& pbm = mesh.boundaryMesh();

        patchSet_ =
            mesh.boundaryMesh().patchSet
            (
                wordReList(dict.lookupOrDefault("patches", wordReList()))
            );

        Info<< type() << " " << name_ << ":" << nl;

        if (patchSet_.empty())
        {
            forAll(pbm, patchI)
            {
                if (isA<wallPolyPatch>(pbm[patchI]))
                {
                    patchSet_.insert(patchI);
                }
            }

            Info<< "    processing all wall patches" << nl << endl;
        }
        else
        {
            Info<< "    processing wall patches: " << nl;
            labelHashSet filteredPatchSet;
            forAllConstIter(labelHashSet, patchSet_, iter)
            {
                label patchI = iter.key();
                if (isA<wallPolyPatch>(pbm[patchI]))
                {
                    filteredPatchSet.insert(patchI);
                    Info<< "        " << pbm[patchI].name() << endl;
                }
                else
                {
                    WarningIn("void uTau::read(const dictionary&)")
                        << "Requested uTau on non-wall boundary "
                        << "type patch: " << pbm[patchI].name() << endl;
                }
            }

            Info<< endl;

            patchSet_ = filteredPatchSet;
        }
    }
}


void Foam::uTau::execute()
{
    //typedef compressible::turbulenceModel cmpModel;
    typedef incompressible::turbulenceModel icoModel;

    if (active_)
    {
        functionObjectFile::write();

        const fvMesh& mesh = refCast<const fvMesh>(obr_);

        volScalarField& uTau =
            const_cast<volScalarField&>
            (
                mesh.lookupObject<volScalarField>(type())
            );

        Info(log_)<< type() << " " << name_ << " output:" << nl;

        //neu
      const volVectorField& U = obr_.lookupObject<volVectorField>("U");

        tmp<volScalarField> nuLam;

                if (mesh.foundObject<icoModel>("turbulenceModel"))
                {
                    const icoModel& model =
                        mesh.lookupObject<icoModel>("turbulenceModel");

                    nuLam = model.nu();
                }
                else
                {
                    FatalErrorIn("void Foam::uTau::write()")
                        << "Unable to find turbulence model in the "
                        << "database" << exit(FatalError);
                }

        calcuTau(mesh, uTau, U, nuLam);
    }
}


void Foam::uTau::end()
{
    if (active_)
    {
        execute();
    }
}


void Foam::uTau::timeSet()
{
    // Do nothing
}


void Foam::uTau::write()
{
    if (active_)
    {
        functionObjectFile::write();

        const volVectorField& uTau =
            obr_.lookupObject<volVectorField>(type());

        Info(log_)<< type() << " " << name_ << " output:" << nl
            << "    writing field " << uTau.name() << nl
            << endl;

        uTau.write();
    }
}


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

Could somebody support me?

Thanks!
Ayla

nainasjdr September 18, 2014 20:04

How could you do this ?

gu1 April 29, 2018 14:50

Quote:

Originally Posted by aylalisa (Post 505779)
Hi Foamers,

I've tried to adapt a functionObject to finally get uTau during simulation.

Following error comes up once I've start the case with the 'new' FO activated:

uTau.H
Code:

\*---------------------------------------------------------------------------*/

#ifndef uTau_H
#define uTau_H

#include "functionObjectFile.H"
#include "volFieldsFwd.H"
#include "Switch.H"
#include "OFstream.H"

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

namespace Foam
{

// Forward declaration of classes
class objectRegistry;
class dictionary;
class polyMesh;
class mapPolyMesh;
class fvMesh;

/*---------------------------------------------------------------------------*\
                      Class uTau Declaration
\*---------------------------------------------------------------------------*/

class uTau
:
    public functionObjectFile
{
protected:

    // Protected data

        //- Name of this set of uTau object
        word name_;

        const objectRegistry& obr_;

        //- on/off switch
        bool active_;

        //- Switch to send output to Info as well as to file
        Switch log_;

        //- Optional list of patches to process
        labelHashSet patchSet_;


    // Protected Member Functions

        //- File header information
        virtual void writeFileHeader(const label i);

        //- Calculate the shear stress
        void calcuTau
        (
            const fvMesh& mesh,
            volScalarField& uTau,
            const volVectorField& U,
            volScalarField nuLam

        );

        //- Disallow default bitwise copy construct
        uTau(const uTau&);

        //- Disallow default bitwise assignment
        void operator=(const uTau&);


public:

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


    // Constructors

        //- Construct for given objectRegistry and dictionary.
        //  Allow the possibility to load fields from files
        uTau
        (
            const word& name,
            const objectRegistry&,
            const dictionary&,
            const bool loadFromFiles = false
        );


    //- Destructor
    virtual ~uTau();


    // Member Functions

        //- Return name of the set of uTau
        virtual const word& name() const
        {
            return name_;
        }

        //- Read the uTau data
        virtual void read(const dictionary&);

        //- Execute, currently does nothing
        virtual void execute();

        //- Execute at the final time-loop, currently does nothing
        virtual void end();

        //- Called when time was set at the end of the Time::operator++
        virtual void timeSet();

        //- Calculate the uTau and write
        virtual void write();

        //- Update for changes of mesh
        virtual void updateMesh(const mapPolyMesh&)
        {}

        //- Update for changes of mesh
        virtual void movePoints(const polyMesh&)
        {}
};


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

} // End namespace Foam

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

#endif

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

uTau.C

Code:

\*---------------------------------------------------------------------------*/

#include "uTau.H"
#include "volFields.H"
#include "surfaceFields.H"
#include "incompressible/turbulenceModel/turbulenceModel.H"
#include "compressible/turbulenceModel/turbulenceModel.H"
#include "wallPolyPatch.H"

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

namespace Foam
{
defineTypeNameAndDebug(uTau, 0);
}


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

void Foam::uTau::writeFileHeader(const label i)
{
    // Add headers to output data
    writeHeader(file(), "uTau");
    writeCommented(file(), "Time");
    writeTabbed(file(), "patch");
    writeTabbed(file(), "min");
    writeTabbed(file(), "max");
}


void Foam::uTau::calcuTau
(
    const fvMesh& mesh,
    volScalarField& uTauField,
    const volVectorField& U,
    volScalarField nuLam
)
{
    forAllConstIter(labelHashSet, patchSet_, iter)
    {
        label patchI = iter.key();
        const polyPatch& pp = mesh.boundaryMesh()[patchI];

        scalarField& uTau_p = uTauField.boundaryField()[patchI];

        uTau_p = sqrt
                (
                    nuLam.boundaryField()[patchI]
                  *mag(U.boundaryField()[patchI].snGrad())
                );

        scalar minUTau = gMin(uTau_p);
        scalar maxUTau = gMax(uTau_p);

        if (Pstream::master())
        {
            file() << mesh.time().value()
                << token::TAB << pp.name()
                << token::TAB << minUTau
                << token::TAB << maxUTau
                << endl;
        }

        Info(log_)<< "    min/max(" << pp.name() << ") = "
            << minUTau << ", " << maxUTau << endl;
    }
}


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

Foam::uTau::uTau
(
    const word& name,
    const objectRegistry& obr,
    const dictionary& dict,
    const bool loadFromFiles
)
:
    functionObjectFile(obr, name, typeName),
    name_(name),
    obr_(obr),
    active_(true),
    log_(true),
    patchSet_()
{
    // Check if the available mesh is an fvMesh, otherwise deactivate
    if (!isA<fvMesh>(obr_))
    {
        active_ = false;
        WarningIn
        (
            "uTau::uTau"
            "("
                "const word&, "
                "const objectRegistry&, "
                "const dictionary&, "
                "const bool"
            ")"
        )  << "No fvMesh available, deactivating " << name_ << nl
            << endl;
    }

    if (active_)
    {
        const fvMesh& mesh = refCast<const fvMesh>(obr_);

        volScalarField* uTauPtr
        (
            new volScalarField
            (
                IOobject
                (
                    type(),
                    mesh.time().timeName(),
                    mesh,
                    IOobject::NO_READ,
                    IOobject::NO_WRITE
                ),
                mesh,
                dimensionedScalar
                (
                    "0",
                    dimless,
                    //sqr(dimLength)/sqr(dimTime),
                    0
                )
            )
        );

        mesh.objectRegistry::store(uTauPtr);
    }

    read(dict);
}


// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

Foam::uTau::~uTau()
{}


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

void Foam::uTau::read(const dictionary& dict)
{
    if (active_)
    {
        log_ = dict.lookupOrDefault<Switch>("log", true);

        const fvMesh& mesh = refCast<const fvMesh>(obr_);
        const polyBoundaryMesh& pbm = mesh.boundaryMesh();

        patchSet_ =
            mesh.boundaryMesh().patchSet
            (
                wordReList(dict.lookupOrDefault("patches", wordReList()))
            );

        Info<< type() << " " << name_ << ":" << nl;

        if (patchSet_.empty())
        {
            forAll(pbm, patchI)
            {
                if (isA<wallPolyPatch>(pbm[patchI]))
                {
                    patchSet_.insert(patchI);
                }
            }

            Info<< "    processing all wall patches" << nl << endl;
        }
        else
        {
            Info<< "    processing wall patches: " << nl;
            labelHashSet filteredPatchSet;
            forAllConstIter(labelHashSet, patchSet_, iter)
            {
                label patchI = iter.key();
                if (isA<wallPolyPatch>(pbm[patchI]))
                {
                    filteredPatchSet.insert(patchI);
                    Info<< "        " << pbm[patchI].name() << endl;
                }
                else
                {
                    WarningIn("void uTau::read(const dictionary&)")
                        << "Requested uTau on non-wall boundary "
                        << "type patch: " << pbm[patchI].name() << endl;
                }
            }

            Info<< endl;

            patchSet_ = filteredPatchSet;
        }
    }
}


void Foam::uTau::execute()
{
    //typedef compressible::turbulenceModel cmpModel;
    typedef incompressible::turbulenceModel icoModel;

    if (active_)
    {
        functionObjectFile::write();

        const fvMesh& mesh = refCast<const fvMesh>(obr_);

        volScalarField& uTau =
            const_cast<volScalarField&>
            (
                mesh.lookupObject<volScalarField>(type())
            );

        Info(log_)<< type() << " " << name_ << " output:" << nl;

        //neu
      const volVectorField& U = obr_.lookupObject<volVectorField>("U");

        tmp<volScalarField> nuLam;

                if (mesh.foundObject<icoModel>("turbulenceModel"))
                {
                    const icoModel& model =
                        mesh.lookupObject<icoModel>("turbulenceModel");

                    nuLam = model.nu();
                }
                else
                {
                    FatalErrorIn("void Foam::uTau::write()")
                        << "Unable to find turbulence model in the "
                        << "database" << exit(FatalError);
                }

        calcuTau(mesh, uTau, U, nuLam);
    }
}


void Foam::uTau::end()
{
    if (active_)
    {
        execute();
    }
}


void Foam::uTau::timeSet()
{
    // Do nothing
}


void Foam::uTau::write()
{
    if (active_)
    {
        functionObjectFile::write();

        const volVectorField& uTau =
            obr_.lookupObject<volVectorField>(type());

        Info(log_)<< type() << " " << name_ << " output:" << nl
            << "    writing field " << uTau.name() << nl
            << endl;

        uTau.write();
    }
}


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

Could somebody support me?

Thanks!
Ayla

Can you teach me, how to plot y+/u+?


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