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

Coding issue / OpenFOAM

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree2Likes
  • 1 Post By hk318i
  • 1 Post By hk318i

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   June 30, 2016, 15:49
Default Coding issue / OpenFOAM
  #1
Senior Member
 
SinaJ
Join Date: Nov 2009
Posts: 136
Rep Power: 15
sina_mech is on a distinguished road
Hi,

I'm trying to compile a solver. I have several functions that those functions want to access some mesh data, and I would like to keep those functions in separate C files, and having access in main C file via header files. Please see "testme.C" below! I get errors, cuase I can't access to 'vector', 'label', 'mesh', and etc. How can I fix that?

Example (I know the purpose of the function doesn't make sense ... but it's just an example!)

Code:
// testme.C file
double TestMe(std::vector <double> & myvec)
{
	vector myPoint(1.0, 1.0, 1.0);
	label myCell = mesh.findCell(myPoint);
	
	double u_here = U[myCell].component(0);
	
	return u_here;
}

Code:
// testme.h file
double TestMe(std::vector <double> & myvec);

Code:
// mySolver.C file


...
int main(int argc, char *argv[])
{
 ...
 
	std::vector <double> myvec;
	double x_com = TestMe (myvec);
	
	....
	....
	
}
sina_mech is offline   Reply With Quote

Old   June 30, 2016, 18:43
Default
  #2
Senior Member
 
SinaJ
Join Date: Nov 2009
Posts: 136
Rep Power: 15
sina_mech is on a distinguished road
Any suggestions would be really helpful

Last edited by sina_mech; July 1, 2016 at 14:59.
sina_mech is offline   Reply With Quote

Old   July 4, 2016, 09:25
Default
  #3
Senior Member
 
Hassan Kassem
Join Date: May 2010
Location: Germany
Posts: 241
Rep Power: 16
hk318i is on a distinguished road
Your code has some major issues, I will try to highlight the main problems;

  • The data types like label, scalar and vector are not available because the header files are not included.
  • The variable ``mesh`` is not available within the function scope. You may need to pass a reference to it.
  • You are mixing between std C++ data types and OpenFOAM which is not a good practice and could cause some problems later.
  • Creating functions or classes and compile them directory with a solver is not OpenFOAM style.
  • OpenFOAM has few utilities which create header, source and application files which follow OpenFOAM style. (foamNew, foamNewBC, foamNewSource, foamNewApp, foamNewCase, foamNewTemplate)
Here is a minimal example,
Code:
//fun.H
#ifndef fun_H
#define fun_H

#include "scalar.H"
#include "vector.H"

namespace Foam
{
    vector TestMe(const vector& myvec);

}
#endif
Code:
//fun.C
#include "fun.H"

Foam::vector Foam::TestMe(const vector& myvec)
{
    Info<< "Hello from TestMe" << endl;
    Info<< "vector: " << myvec << endl;
    Info<< "vectorXcmpt: " << myvec.component(0) << endl;
    Info<< "vectorYcmpt: " << myvec.component(1) << endl;
    Info<< "vectorZcmpt: " << myvec.component(2) << endl;
    return myvec;
}
Code:
//exApp.C
#include "fun.H"

using namespace Foam;

int main(int argc, char *argv[])
{
    vector A(1,2,3);
    vector B(4,5,6);
    vector a = TestMe(A);
    vector b = TestMe(B);
    Info<< "Hello from the main()" << endl;
    Info<< "vector a: "  << a << endl;
    Info<< "vector b: "  << b << endl;
    return 0;
}
Code:
//Make/files
fun.C
exApp.C

EXE = $(FOAM_USER_APPBIN)/exApp
and you can leave Make/options empty for this case but for a basic case you may need something like this
Code:
//Make/files
EXE_INC = \
    -I$(LIB_SRC)/finiteVolume/lnInclude \
    -I$(LIB_SRC)/meshTools/lnInclude

EXE_LIBS = \
    -lfiniteVolume \
    -lmeshTools
I know it looks very long for minimal example but that is how C++ works.

Best wishes,
Hassan
sina_mech likes this.
__________________
@HIKassem | HassanKassem.me
hk318i is offline   Reply With Quote

Old   July 6, 2016, 12:52
Default
  #4
Senior Member
 
SinaJ
Join Date: Nov 2009
Posts: 136
Rep Power: 15
sina_mech is on a distinguished road
Quote:
Originally Posted by hk318i View Post
Your code has some major issues, I will try to highlight the main problems;

  • The data types like label, scalar and vector are not available because the header files are not included.
  • The variable ``mesh`` is not available within the function scope. You may need to pass a reference to it.
  • You are mixing between std C++ data types and OpenFOAM which is not a good practice and could cause some problems later.
  • Creating functions or classes and compile them directory with a solver is not OpenFOAM style.
  • OpenFOAM has few utilities which create header, source and application files which follow OpenFOAM style. (foamNew, foamNewBC, foamNewSource, foamNewApp, foamNewCase, foamNewTemplate)
Here is a minimal example,
Code:
//fun.H
#ifndef fun_H
#define fun_H

#include "scalar.H"
#include "vector.H"

namespace Foam
{
    vector TestMe(const vector& myvec);

}
#endif
Code:
//fun.C
#include "fun.H"

Foam::vector Foam::TestMe(const vector& myvec)
{
    Info<< "Hello from TestMe" << endl;
    Info<< "vector: " << myvec << endl;
    Info<< "vectorXcmpt: " << myvec.component(0) << endl;
    Info<< "vectorYcmpt: " << myvec.component(1) << endl;
    Info<< "vectorZcmpt: " << myvec.component(2) << endl;
    return myvec;
}
Code:
//exApp.C
#include "fun.H"

using namespace Foam;

int main(int argc, char *argv[])
{
    vector A(1,2,3);
    vector B(4,5,6);
    vector a = TestMe(A);
    vector b = TestMe(B);
    Info<< "Hello from the main()" << endl;
    Info<< "vector a: "  << a << endl;
    Info<< "vector b: "  << b << endl;
    return 0;
}
Code:
//Make/files
fun.C
exApp.C

EXE = $(FOAM_USER_APPBIN)/exApp
and you can leave Make/options empty for this case but for a basic case you may need something like this
Code:
//Make/files
EXE_INC = \
    -I$(LIB_SRC)/finiteVolume/lnInclude \
    -I$(LIB_SRC)/meshTools/lnInclude

EXE_LIBS = \
    -lfiniteVolume \
    -lmeshTools
I know it looks very long for minimal example but that is how C++ works.

Best wishes,
Hassan
Hi Hassan,

Thank you for your reply I know i would be better to stick to openFoam's protocols, but in a case, I tried this out, and seems to be working:

Code:
//myOwnCFile.C

#include "fvMesh.H"
#include "fvCFD.H"

void MyFunction(std::vector<double> & test, fvMesh const& mesh, vectorField & U)
...
In the function above, I use mesh and velocity field (U) as variables by passing a reference to it. What do you think about it?
sina_mech is offline   Reply With Quote

Old   July 6, 2016, 14:10
Default
  #5
Senior Member
 
Hassan Kassem
Join Date: May 2010
Location: Germany
Posts: 241
Rep Power: 16
hk318i is on a distinguished road
It is up to you to use std C++ or not. If you are passing a value by refrence and not planning to change the variable within the function or class, use ``const`` identifier.

Code:
void MyFunction(const std::vector<double>& test, const fvMesh& mesh, const vectorField& U)
{
    ... ....
}
sina_mech likes this.
__________________
@HIKassem | HassanKassem.me
hk318i is offline   Reply With Quote

Old   September 14, 2022, 23:44
Default
  #6
Member
 
chengan.wang
Join Date: Jan 2016
Location: china
Posts: 47
Rep Power: 8
wangchengan2003 is on a distinguished road
Send a message via Skype™ to wangchengan2003
Dear Hassan,

I want to create some functions in the solver. I have tested the code you proposed. They are correct perfectly.

I just want to replace the fun.H file by

Code:
template<class CloudType>
inline const Foam::tmp<Foam::volVectorField>
Foam::KinematicCloud<CloudType>::UPar() const
{
    tmp<volVectorField> tUPar
    (
        volVectorField::New
        (
            this->name() + ":UPar",
            mesh_,
            dimensionedVector(dimVelocity, vector(0,0,0)),
            extrapolatedCalculatedFvPatchVectorField::typeName
        )
    );


    volVectorField& UPar = tUPar.ref();
    forAllConstIter(typename KinematicCloud<CloudType>, *this, iter)
    {
        const parcelType& p = iter();
        const label celli = p.cell();
        UPar[celli] += (p.U())/(p.nParticle());
    }
    UPar.primitiveFieldRef() /= 1;//mesh_.V();
    UPar.correctBoundaryConditions();

    return tUPar;
}



  
template<class CloudType>
inline const Foam::tmp<Foam::volScalarField>
Foam::KinematicCloud<CloudType>::SumPar() const
{
    tmp<volScalarField> tSumPar
    (
        volScalarField::New
        (
            this->name() + ":SumPar",
            mesh_,
            dimensionedScalar(dimless, 0),
            extrapolatedCalculatedFvPatchScalarField::typeName
        )
    );


    volScalarField& SumPar = tSumPar.ref();
    forAllConstIter(typename KinematicCloud<CloudType>, *this, iter)
    {
        const parcelType& p = iter();
        const label celli = p.cell();
        SumPar[celli] += p.nParticle();
    
    }

    SumPar.primitiveFieldRef() /= 1;//mesh_.V();
    SumPar.correctBoundaryConditions();

    return tSumPar;
}
from Lagrangian data - particle velocity post processing

such as

Code:
//fun.H
#ifndef fun_H
#define fun_H


template<class CloudType>
inline const Foam::tmp<Foam::volVectorField>
Foam::KinematicCloud<CloudType>::UPar() const
{
    tmp<volVectorField> tUPar
    (
        volVectorField::New
        (
            this->name() + ":UPar",
            mesh_,
            dimensionedVector(dimVelocity, vector(0,0,0)),
            extrapolatedCalculatedFvPatchVectorField::typeName
        )
    );


    volVectorField& UPar = tUPar.ref();
    forAllConstIter(typename KinematicCloud<CloudType>, *this, iter)
    {
        const parcelType& p = iter();
        const label celli = p.cell();
        UPar[celli] += (p.U())/(p.nParticle());
    }
    UPar.primitiveFieldRef() /= 1;//mesh_.V();
    UPar.correctBoundaryConditions();

    return tUPar;
}



  
template<class CloudType>
inline const Foam::tmp<Foam::volScalarField>
Foam::KinematicCloud<CloudType>::SumPar() const
{
    tmp<volScalarField> tSumPar
    (
        volScalarField::New
        (
            this->name() + ":SumPar",
            mesh_,
            dimensionedScalar(dimless, 0),
            extrapolatedCalculatedFvPatchScalarField::typeName
        )
    );


    volScalarField& SumPar = tSumPar.ref();
    forAllConstIter(typename KinematicCloud<CloudType>, *this, iter)
    {
        const parcelType& p = iter();
        const label celli = p.cell();
        SumPar[celli] += p.nParticle();
    
    }

    SumPar.primitiveFieldRef() /= 1;//mesh_.V();
    SumPar.correctBoundaryConditions();

    return tSumPar;
}


#endif
And I don't use fun.C file. After compiling, there are some mistakes;
Code:
fun.H:16:41: error: no ‘const Foam::tmp<Foam::GeometricField<Foam::Vector<double>, Foam::fvPatchField, Foam::volMesh> > Foam::KinematicCloud<CloudType>::UPar() const’ member function declared in class ‘Foam::KinematicCloud<CloudType>’
 Foam::KinematicCloud<CloudType>::UPar() const
                                         ^
fun.H:48:43: error: no ‘const Foam::tmp<Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh> > Foam::KinematicCloud<CloudType>::SumPar() const’ member function declared in class ‘Foam::KinematicCloud<CloudType>’
 Foam::KinematicCloud<CloudType>::SumPar() const
I think I need to add some codes in fun.H. Could you give me some suggestions?

Best regards,

Chengan
wangchengan2003 is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
[OpenFOAM.org] A Mac OS X of23x Development Environment Using Docker rt08 OpenFOAM Installation 1 February 28, 2016 20:00
OpenFoam parallel running log issue clktp OpenFOAM Running, Solving & CFD 2 January 19, 2016 10:12
OpenFOAM Training, London, Chicago, Munich, Sep-Oct 2015 cfd.direct OpenFOAM Announcements from Other Sources 2 August 31, 2015 14:36
CyclicAMI Issue In OpenFOAM 2.2.0 prasant OpenFOAM Running, Solving & CFD 17 March 16, 2013 03:00
Issue installation OpenFOAM - libopen-rte.so.0 Voyage_gui OpenFOAM 1 August 12, 2011 04:46


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