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/)
-   -   make custom functionObject available with postProcess (https://www.cfd-online.com/Forums/openfoam-programming-development/237388-make-custom-functionobject-available-postprocess.html)

Shibi July 15, 2021 07:57

make custom functionObject available with postProcess
 
Hello to all,

I would like to know how to make a custom functionObject available via the postProcess utility in OpenFOAM from ESI (v2012).

As an example, I have duplicated the writeCellCentres functionObject and renamed it as myWriteCellCentres. Did the same inside the .H and .C files.

Prepared a Make folder with (copied from OpenFOAM-v2012/src/functionObjects/field):

files
Code:

myWriteCellCentres.C

LIB = $(FOAM_USER_LIBBIN)/libmyFunctionObjects

options

Code:

EXE_INC = \
    -I$(LIB_SRC)/finiteVolume/lnInclude \
    -I$(LIB_SRC)/fileFormats/lnInclude \
    -I$(LIB_SRC)/surfMesh/lnInclude \
    -I$(LIB_SRC)/meshTools/lnInclude \
    -I$(LIB_SRC)/sampling/lnInclude \
    -I$(LIB_SRC)/lagrangian/basic/lnInclude \
    -I$(LIB_SRC)/lagrangian/distributionModels/lnInclude \
    -I$(LIB_SRC)/thermophysicalModels/basic/lnInclude \
    -I$(LIB_SRC)/transportModels \
    -I$(LIB_SRC)/transportModels/compressible/lnInclude \
    -I$(LIB_SRC)/TurbulenceModels/turbulenceModels/lnInclude \
    -I$(LIB_SRC)/TurbulenceModels/incompressible/lnInclude \
    -I$(LIB_SRC)/TurbulenceModels/compressible/lnInclude \
    -I$(LIB_SRC)/transportModels/compressible/lnInclude \
    -I$(LIB_SRC)/thermophysicalModels/basic/lnInclude \
    -I$(LIB_SRC)/thermophysicalModels/solidThermo/lnInclude \
    -I$(LIB_SRC)/thermophysicalModels/chemistryModel/lnInclude \
    -I$(LIB_SRC)/thermophysicalModels/reactionThermo/lnInclude \
    -I$(LIB_SRC)/thermophysicalModels/specie/lnInclude \
    -I$(LIB_SRC)/fvAgglomerationMethods/pairPatchAgglomeration/lnInclude

LIB_LIBS = \
    -lfiniteVolume \
    -lfileFormats \
    -lsurfMesh \
    -lmeshTools \
    -lsampling \
    -llagrangian \
    -ldistributionModels \
    -lfluidThermophysicalModels \
    -lsolidThermo \
    -lincompressibleTransportModels \
    -lturbulenceModels \
    -lcompressibleTransportModels \
    -lincompressibleTurbulenceModels \
    -lcompressibleTurbulenceModels \
    -lchemistryModel \
    -lreactionThermophysicalModels \
    -lpairPatchAgglomeration

In the controlDict I have:
Code:

functions
{

    writeBoundaryCells1
    {
        // Mandatory entries (unmodifiable)
        type            myWriteCellCentres;
        libs            (libmyFunctionObjects.so);
    }
}

The functionObject work as expected.


When I try to execute it with the postProcess utility with:

Code:

postProcess -func myWriteCellCentres -lib myFunctionObjects
I get:

Code:

--> FOAM Warning :
    From static bool Foam::functionObjectList::readFunctionObject(const Foam::string&, Foam::dictionary&, Foam::HashSet<Foam::wordRe>&, const Foam::word&)
    in file db/functionObjects/functionObjectList/functionObjectList.C at line 336
    Cannot find functionObject file myWriteCellCentres


What is the correct way of calling a custom functionObject with postProcess?

Shibi July 17, 2021 14:44

Anyone can give a hand with this issue?

ybapat July 19, 2021 00:14

You can try by including your compiled library in libs entry in controlDict file. This will load your library while running the case and your functionObject should be then available for postProcess utility.

-Yogesh

olesen July 19, 2021 03:21

Quote:

Originally Posted by Shibi (Post 808339)
Anyone can give a hand with this issue?

Try calling postprocces with your function "writeBoundaryCells1" instead. Probably don't need the - lib argument since it is already mentioned in the dictionary.
The function execution is by your function name, not by the type of functionObject.

trailer July 19, 2021 04:51

1 Attachment(s)
Quote:

Originally Posted by olesen (Post 808409)
Try calling postprocces with your function "writeBoundaryCells1" instead. Probably don't need the - lib argument since it is already mentioned in the dictionary.
The function execution is by your function name, not by the type of functionObject.


Hello Mr. Olesen,

I had no luck with that.


I know you have a very busy schedule but I will attach the case with the functionObject in case your are interested in having a look.

Attachment 85456

Voulet October 4, 2023 02:55

Same issue here
 
Hi there. I'm facing the same issue. I use the primer openfoam.



I've compilated my custom function object with


Code:

LIB = $(FOAM_USER_LIBBIN)/libcalcul_surface_faceFunctionObject
I put this in my controlDict :

Code:

functions


    calc_SF
    {
        type            calcul_surface_face;
        libs ("libcalcul_surface_faceFunctionObject.so");

        executeControl  writeTime;
        writeControl    writeTime;
    }
}

And when doing postProcess -func calcul_surface_face I get the following error:


Code:

--> FOAM Warning :
    From static bool Foam::functionObjectList::readFunctionObject(const Foam::string&, Foam::dictionary&, Foam::HashSet<Foam::wordRe>&, const Foam::word&)
    in file db/functionObjects/functionObjectList/functionObjectList.C at line 290
    Cannot find functionObject file calcul_surface_face

Can any one give insights ?

olesen October 5, 2023 18:40

try with the foamLibrary utility if your library can even load properly. If it does load then did you also remember to register the function in your code as well?

Voulet October 5, 2023 22:56

Hello Olesen. Thanks for your message. I'm sad because it seems that I'm not enough skilled to fully understand it.

Yes foamHasLibrary -detail libcalcul_surface_faceFunctionObject.so do returns me the lib.

What do you mean by register the function ? I put TypeName("calcul_surface_face"); inside the header of my class declaraction if this is what you means.


Code:

/*---------------------------------------------------------------------------*\
  =========                |
  \\      /  F ield        | OpenFOAM: The Open Source CFD Toolbox
  \\    /  O peration    |
    \\  /    A nd          | www.openfoam.com
    \\/    M anipulation  |
\*---------------------------------------------------------------------------*/

#ifndef functionObjects_calcul_surface_face_H
#define functionObjects_calcul_surface_face_H

#include "fvMeshFunctionObject.H"

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

namespace Foam
{
namespace functionObjects
{

/*---------------------------------------------------------------------------*\
                      Class calcul_surface_face Declaration
\*---------------------------------------------------------------------------*/

class calcul_surface_face
:
    public fvMeshFunctionObject
{
public:

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


    // Constructors

        //- Construct from Time and dictionary
        calcul_surface_face
        (
            const word& name,
            const Time& runTime,
            const dictionary& dict
        );

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

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


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


    // Member Functions

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

        //- Do nothing
        virtual bool execute();

        //- Write the cell-volume fields
        virtual bool write();
};


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

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

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

#endif

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


olesen October 6, 2023 04:36

Quote:

Originally Posted by Voulet (Post 857940)
Hello Olesen. Thanks for your message. I'm sad because it seems that I'm not enough skilled to fully understand it.

Yes foamHasLibrary -detail libcalcul_surface_faceFunctionObject.so do returns me the lib.

What do you mean by register the function ? I put TypeName("calcul_surface_face"); inside the header of my class declaraction if this is what you means.

You need something like this (but of course with your own function):

Code:



#include "addToRunTimeSelectionTable.H"

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


Voulet October 6, 2023 04:50

Yes I have it :


Code:

/*---------------------------------------------------------------------------*\
  =========                |
  \\      /  F ield        | OpenFOAM: The Open Source CFD Toolbox
  \\    /  O peration    |
    \\  /    A nd          | www.openfoam.com
    \\/    M anipulation  |
-------------------------------------------------------------------------------
    Copyright (C) 2016 OpenFOAM Foundation
    Copyright (C) 2016-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

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

#include "calcul_surface_face.H"
#include "surfaceFields.H"
#include "fvMesh.H"
#include "Time.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

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


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

Foam::functionObjects::calcul_surface_face::calcul_surface_face
(
    const word& name,
    const Time& runTime,
    const dictionary& dict
)
:
    fvMeshFunctionObject(name, runTime, dict)
{
    read(dict);
}


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

bool Foam::functionObjects::calcul_surface_face::read(const dictionary& dict)
{
    return fvMeshFunctionObject::read(dict);
}


bool Foam::functionObjects::calcul_surface_face::execute()
{
    return true;
}


bool Foam::functionObjects::calcul_surface_face::write()
{
    surfaceVectorField Sf
    (
        IOobject
        ( 
            mesh_.Sf().name(),
            time_.timeName(),
            mesh_,
            IOobject::NO_READ,
            IOobject::NO_WRITE,
            IOobject::NO_REGISTER
        ),
        mesh_,
        Zero
    );

    Sf.ref() = mesh_.Sf();

    // Without correctBoundaryConditions() - boundary faces have zero volume

    Log << type() << " " << name() << " write:" << nl
        << "    writing cell-face area field Sf"
        << " to " << time_.timeName() << endl;

    Sf.write();

    return true;
}


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


Voulet October 6, 2023 05:05

In fact when I run it on a tutorial case with one of the following comand :

postProcess -func calcul_surface_face
or
postProcess

it does works !

But the openfoam case I wanted to treat was an old case which was run with openfoam8. With this case : (using ofv23 and the objectFunction compiled with ofv23)

postProcess -func calcul_surface_face doesn't find the function
or
postProcess works

This is quite strange but I suppose my issue is solved.
Thanks Olesen


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