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

Call a constant in a function

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   September 10, 2020, 12:02
Default Call a constant in a function
  #1
gu1
Senior Member
 
Guilherme
Join Date: Apr 2017
Posts: 225
Rep Power: 10
gu1 is on a distinguished road
Hello,

I'm having trouble calling a constant to be resolved within my turbulence model... could someone help me?

The problem is as follows, within the v2f.C file there is the function:

Code:
template<class BasicTurbulenceModel>
tmp<volScalarField> v2f<BasicTurbulenceModel>::Ts() const
{
    return max(k_/epsilon_, 6.0*sqrt(this->nu()/epsilon_));
}
However if I wanted to make this function receive a constant such as G:

Code:
const volScalarField G(this->GName(), nut*S2);
How would I do it?

I tried that way:

v2f.C
Code:
template<class BasicTurbulenceModel>
tmp<volScalarField> v2f<BasicTurbulenceModel>::Ts(const volScalarField& G) const
{
    return max((k_*G)/epsilon_, 6.0*sqrt(this->nu()/epsilon_));
}
...and:

v2f.H
Code:
volScalarField  Ts(const volScalarField& G) const;
...but I was not successful.

The CorrectNut() function requires the value of Ts() and since I didn't find out how to call the constant in the above function (Ts), I didn't progress.

Thanks

#OpenFoam 5.0
gu1 is offline   Reply With Quote

Old   September 10, 2020, 14:57
Default
  #2
Senior Member
 
Tom-Robin Teschner
Join Date: Dec 2011
Location: Cranfield, UK
Posts: 204
Rep Power: 16
t.teschner is on a distinguished road
The cleanest solution I can see here is to use function overloading, i.e. provide two different functions with the same name and return type but with different function arguments.

Code:
// v2f.H file

//- default function declaration
volScalarField  Ts() const;

//- newly added function
volScalarField  Ts(const volScalarField& G) const;
Code:
// v2f.C

template<class BasicTurbulenceModel>
tmp<volScalarField> v2f<BasicTurbulenceModel>::Ts() const
{
    return max(k_/epsilon_, 6.0*sqrt(this->nu()/epsilon_));
}

template<class BasicTurbulenceModel>
tmp<volScalarField> v2f<BasicTurbulenceModel>::Ts(const volScalarField& G) const
{
    return max((k_*G)/epsilon_, 6.0*sqrt(this->nu()/epsilon_));
}
In this way both definition can happily coexist and the compiler will choose the correct function at compile time. For more info on function overloading, see this link: https://www.learncpp.com/cpp-tutoria...n-overloading/
t.teschner is offline   Reply With Quote

Old   September 10, 2020, 15:30
Default
  #3
gu1
Senior Member
 
Guilherme
Join Date: Apr 2017
Posts: 225
Rep Power: 10
gu1 is on a distinguished road
Hello,

Thanks for the answer.

What about CorrectNut()? How would I do it? Since it calls the Ts() function return.
gu1 is offline   Reply With Quote

Old   September 10, 2020, 16:56
Default
  #4
Senior Member
 
Tom-Robin Teschner
Join Date: Dec 2011
Location: Cranfield, UK
Posts: 204
Rep Power: 16
t.teschner is on a distinguished road
What exactly is the problem with CorrectNut()? Could you show the source code and explain what you are trying to do?
__________________
Learn to write CFD solvers at cfd.university
t.teschner is offline   Reply With Quote

Old   September 11, 2020, 07:21
Default
  #5
gu1
Senior Member
 
Guilherme
Join Date: Apr 2017
Posts: 225
Rep Power: 10
gu1 is on a distinguished road
Quote:
Originally Posted by t.teschner View Post
What exactly is the problem with CorrectNut()? Could you show the source code and explain what you are trying to do?
Code:
template<class BasicTurbulenceModel>
void v2f<BasicTurbulenceModel>::correctNut()
{
    this->nut_ = min(CmuKEps_*sqr(k_)/epsilon_, this->Cmu_*v2_*Ts());
    this->nut_.correctBoundaryConditions();
    fv::options::New(this->mesh_).correct(this->nut_);

    BasicTurbulenceModel::correctNut();
}
The correctNut() function requires the response of the Ts() function. If I'm going to assign a constant to it, doesn't that constant need to be referenced? Like this:

Code:
template<class BasicTurbulenceModel>
void v2f<BasicTurbulenceModel>::correctNut(G)
{
    this->nut_ = min(CmuKEps_*sqr(k_)/epsilon_, this->Cmu_*v2_*Ts(G));
    this->nut_.correctBoundaryConditions();
    fv::options::New(this->mesh_).correct(this->nut_);

    BasicTurbulenceModel::correctNut();
}
gu1 is offline   Reply With Quote

Old   September 11, 2020, 09:28
Default
  #6
Senior Member
 
Tom-Robin Teschner
Join Date: Dec 2011
Location: Cranfield, UK
Posts: 204
Rep Power: 16
t.teschner is on a distinguished road
well, the same logic as before applies here, you would need to have two function declarations in your header, i.e.

Code:
//- default
template<class BasicTurbulenceModel>
void v2f<BasicTurbulenceModel>::correctNut();

//- new declaration
template<class BasicTurbulenceModel>
void v2f<BasicTurbulenceModel>::correctNut(const volScalarField& G)
that propagates all the way down the call stack to where you actually define the constant G.

Since these are all part of the same class v2f, it might be worthwhile to consider making G a private / protected member of the class and then initialise it in the constructor or wherever it makes sense. In this way you don't have to pass the variable around all the time (that's a clear code smell and we should avoid it).

May I suggest to study up a bit more on c++ classes? I think there is some basic understanding missing, this is not a difficult problem (no offense) but with a bit more background in object orientated programming you will hit less of these problems.
t.teschner is offline   Reply With Quote

Old   September 11, 2020, 13:56
Default
  #7
gu1
Senior Member
 
Guilherme
Join Date: Apr 2017
Posts: 225
Rep Power: 10
gu1 is on a distinguished road
Hi,

I appreciate your help and understand the reasons for making such a recommendation. I know how to do the basics, but unfortunately something I don't understand, because of how the OF is built. See the error below, this happens when I declare that correctNut() has to receive the constant G for it to be used in the function Ts():

Code:
In file included from /opt/openfoam5/src/TurbulenceModels/turbulenceModels/lnInclude/LESeddyViscosity.H:42:0,
                 from v2f.H:52,
/opt/openfoam5/src/TurbulenceModels/turbulenceModels/lnInclude/eddyViscosity.H:69:22: note: 	void Foam::eddyViscosity<BasicTurbulenceModel>::correctNut() [with BasicTurbulenceModel = Foam::LESModel<Foam::IncompressibleTurbulenceModel<Foam::transportModel> >]
         virtual void correctNut() = 0;
                      ^
/opt/openfoam5/wmake/rules/General/transform:25: recipe for target 'Make/linux64GccDPInt32Opt/makeTurbModel.o' failed
I understand that the relationship is in another file (eddyViscosity.H), but at the same time I don't know how to get around it.

I did it:

Code:
template<class BasicTurbulenceModel>
void v2f<BasicTurbulenceModel>::correctNut(const volScalarField& G)
{
    this->nut_ = min(CmuKEps_*sqr(k_)/epsilon_, this->Cmu_*v2_*Ts(G));
    this->nut_.correctBoundaryConditions();
    fv::options::New(this->mesh_).correct(this->nut_);

    BasicTurbulenceModel::correctNut(??); //I don't know if it's necessary.
}
gu1 is offline   Reply With Quote

Old   September 12, 2020, 07:19
Default
  #8
Senior Member
 
Tom-Robin Teschner
Join Date: Dec 2011
Location: Cranfield, UK
Posts: 204
Rep Power: 16
t.teschner is on a distinguished road
The error is telling you that the file eddyViscosity.H only contains the definition for

Code:
virtual void correctNut() = 0;
So you need to add your new definition to that file as well. It may appear logical now to extend the file so that you have

Code:
virtual void correctNut() = 0;
virtual void correctNut(const volScalarField& G) = 0;
However, this is probalby going to result in an immediate or later compilation failure (because of the = 0 at the end, this is a pure virtual function and could result in other classes making use of that file to get a compilation error because you need to define this function then everywhere which is not what you want. This might all be confusing, but again, without background in polymorphism (in this case), i.e. virtual functions, this all may not make much sense)

TL;DR

modify the eddyViscosity.H file so that you have the following function declarations:

Code:
virtual void correctNut() = 0;
virtual void correctNut(const volScalarField& G);
t.teschner is offline   Reply With Quote

Reply

Tags
openfoam5


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
[mesh manipulation] RefineMesh Error and Foam warning jiahui_93 OpenFOAM Meshing & Mesh Conversion 4 March 3, 2018 11:32
[snappyHexMesh] How to define to right point for locationInMesh Mirage12 OpenFOAM Meshing & Mesh Conversion 7 March 13, 2016 14:07
LiencubiclowRemodel nzy102 OpenFOAM Bugs 14 January 10, 2012 08:53
Error with Wmake skabilan OpenFOAM Installation 3 July 28, 2009 00:35
DecomposePar links against liblamso0 with OpenMPI jens_klostermann OpenFOAM Bugs 11 June 28, 2007 17:51


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