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/)
-   -   How to understand the BasicTurbulenceModel class in OpenFOAM (https://www.cfd-online.com/Forums/openfoam-programming-development/219626-how-understand-basicturbulencemodel-class-openfoam.html)

dzsmoglai August 2, 2019 08:47

How to understand the BasicTurbulenceModel class in OpenFOAM
 
Recently I was doing model modification of OF. The question that I really encounted very often was what the BasicTurbulentModel class was in OF. Can someone tell me where I can find the .H or .C file or it?
Thanks very much

wyldckat August 4, 2019 06:26

Quick answer: Look at the source code documentation for the version you are using, e.g.: https://cpp.openfoam.org/v7/

Oh... now I understand, the problem is that "BasicTurbulentModel" is a template class name designation of sorts... a placeholder if you will, it's not a specific class. The actual class is constructed by:

You can use Github search feature at the top of the page to look for names... I looked for "makeTurbulenceModelTypes" and found that file.

Santiago August 4, 2019 09:32

everything was easier for us mortals (non CS majors) before version 4, where turbulence models were handled via autoPointers...

I have never understood this paradigm switch (really, not being sarcastic). I dont see any speed gains from it.

wyldckat August 4, 2019 11:40

Quick answers:
Quote:

Originally Posted by Santiago (Post 741033)
everything was easier for us mortals (non CS majors) before version 4, where turbulence models were handled via autoPointers...

They are still handled with auto-pointers...

Quote:

Originally Posted by Santiago (Post 741033)
I have never understood this paradigm switch (really, not being sarcastic). I dont see any speed gains from it.

This was explained in one of the release notes... It's explained in detail here: https://openfoam.org/release/2-3-0/m...se/#turbulence - section "Multiphase Turbulence".

In a nutshell: For example, k-epsilon turbulence models are nearly identical for all single and multiphase solvers. The only things that usually change is the presence of density and phase fraction. But the way the code was written in the past was to have copy-paste-adapt each and every turbulence class for each type of fluid flow modelling.

With these template classes, you only need to write each turbulence model once and deploy for all types and phases of fluid flow.


edit: The question on the first post was also asked some 4 months ago here: https://www.cfd-online.com/Forums/op...nfoam-6-a.html

Santiago August 4, 2019 15:07

then this requires to implement a turbulence model that works somehow for density based/compressible/incompressible models, am I right?

wyldckat August 4, 2019 15:41

Quote:

Originally Posted by Santiago (Post 741043)
then this requires to implement a turbulence model that works somehow for density based/compressible/incompressible models, am I right?

Yes, but it's usually fairly straight forward, it's just a matter of following the other turbulence modelling examples.

Or you could simply ignore the "alpha" and "rho" input arguments if you want to not care about them... in other words, to keep using the old equation formulation.

Santiago August 4, 2019 15:53

Quote:

Originally Posted by wyldckat (Post 741045)
Yes, but it's usually fairly straight forward, it's just a matter of following the other turbulence modelling examples.

Or you could simply ignore the "alpha" and "rho" input arguments if you want to not care about them... in other words, to keep using the old equation formulation.

For Smagorinsky LES, for instance, I find this new framework to be rather useless and, at the user level may lead to the impression that the models available actually work for density/velocity bases solvers.

I guess that in the RANS realm it might be more useful, given the empiricism inhetent in most of the models...

Has someone testes whether this templating is indeed faster than just "repeating" code? Asking for a friend...

wyldckat August 4, 2019 16:25

Somewhat quick answers:
Quote:

Originally Posted by Santiago (Post 741047)
For Smagorinsky LES, for instance, I find this new framework to be rather useless and, at the user level may lead to the impression that the models available actually work for density/velocity bases solvers.

?? I'm not familiar with the LES implementation in OpenFOAM, but ... after looking at the code, see my answer(s) on the next paragraph...


Quote:

Originally Posted by Santiago (Post 741047)
Has someone testes whether this templating is indeed faster than just "repeating" code? Asking for a friend...

I really don't know where you are getting the information or idea that this change in OpenFOAM was to make the code run faster... the primary objective was to reduce the maintenance effort down to a minimum.

Whenever there was a bug in a turbulence model, the same bug had to be fixed 4 or 5 different classes, which were implementing the same turbulence model. That was a massive hazard and would easily result in bugs staying not fixed for some flow types, because developers would/could forget to update one or two classes for the same model... it was almost insane that it was ever implemented that way, from a development/sustainability perspective.
It was easier to look at the code and re-adapt it, but it was not to the ones doing the maintenance of the code.

.... Right, back to your question: in practice, the CPU performance should be (nearly) identical, since the compiler will reinterpret the code to each fluid flow model, namely will do the copy-paste-adapt for us. This way a bug is fixed once, but in fact fixes for all 4-5 instantiated models, as well as any new fluid flow models people can come up with.

dzsmoglai August 5, 2019 05:41

Dear Santos, Thanks for your quick answer. I am really happy that my first post could be answered by other Foamers so quickly. However, I am still kinds of confused about the BasciTurbulenceModel. I clearly know sometimes it is just a placeholder of a template. But just as mentioned 4 months ago here in linearViscousStress.H:
Code:

template<class BasicTurbulenceModel>
class linearViscousStress
:
    public BasicTurbulenceModel
{

public:

    typedef typename BasicTurbulenceModel::alphaField alphaField;
    typedef typename BasicTurbulenceModel::rhoField rhoField;
    typedef typename BasicTurbulenceModel::transportModel transportModel;


    // Constructors

        //- Construct from components
        linearViscousStress
        (
            const word& modelName,
            const alphaField& alpha,
            const rhoField& rho,
            const volVectorField& U,
            const surfaceScalarField& alphaRhoPhi,
            const surfaceScalarField& phi,
            const transportModel& transport,
            const word& propertiesName
        );


    //- Destructor
    virtual ~linearViscousStress()
    {}

If I am right, the KEpsilon class inherits from eddyViscosity, which inherits from linearViscousStress. And in the code above, LinearViscousStress class has no own member variables, all of its member variables are from "parrent class" BasicTurbulenceModel. So What I am really eager to know is:
1. What member variables class BasicTurbulenceModel has;
2. What member functions class BasicTurbulenceModel has;
Which also means where I can find the declaration of it.

Thank you very much again.
Zongshi

wyldckat August 5, 2019 19:38

Quick answer: https://cpp.openfoam.org/v7/classFoa...ousStress.html

There is a connection graph right at the start... it gives an overview of what classes are used by this template class "linearViscousStress".
If you look at this class implementation: https://github.com/OpenFOAM/OpenFOAM...iscousStress.C - what it adds a few more methods that will take advantage of the base turbulence model.

For example, this template class "linearViscousStress" provides the method "divDevRhoReff" that the solvers are looking for. This method is common to all turbulence models, hence the reason why "linearViscousStress" exists: it's sort of a wrapper for all turbulence models.


Quote:

Originally Posted by dzsmoglai (Post 741105)
So What I am really eager to know is:
1. What member variables class BasicTurbulenceModel has;
2. What member functions class BasicTurbulenceModel has;
Which also means where I can find the declaration of it.

It has whichever methods the used turbulence model has got. For example, if "RASModel" is used, then it has these methods: https://cpp.openfoam.org/v7/classFoam_1_1RASModel.html
  • Which in turn depends on which sub-model you want to use... for example.... oh, now I see what you mean...
So the simpler way to figure out which methods exist, is by going into the turbulence model you are looking for, e.g. kEpsilon: https://cpp.openfoam.org/v7/classFoa...1kEpsilon.html


... sigh... it's not easy to point to a specific location and say how it is... but the diagram shown in https://www.cfd-online.com/Forums/op...tml#post730795 - post #3, does give a good topological view of the methods to expect...


I don't have time right now to try and draw a diagram of how the turbulence models are constructed and which methods are made available at a specific point... I'll try to look into this later this week, if someone else doesn't beat me to it before then...


edit: OK, no need for me to look into this further... the mentioned blog post below is fortunately enough for people to figure it out ;)

dzsmoglai August 8, 2019 09:53

BasicTurbulenceModel
 
Quote:

Originally Posted by dzsmoglai (Post 741105)
So What I am really eager to know is:
1. What member variables class BasicTurbulenceModel has;
2. What member functions class BasicTurbulenceModel has;
Which also means where I can find the declaration of it.

Thank you very much again.
Zongshi

Hey, guys!
After reading some blogs and posts online and the source codes of OpenFoam as well, I think now I know what BasicTurbulenceModel is:

Actually, it is a template parameter which indicates the compressibility of the flow. As far as I know so far, it can be either incompressibleTurbulenceModel or compressibleTurbulenceModel, which are also two classed based on the class turbulenceModel.

one can easily find the definition of turbulenceModel as below in
//$FOAM_SRC/TurbulenceModels/turbulenceModels/turbulenceModel.H(.C as well)

#ifndef turbulenceModel_H
#define turbulenceModel_H

#include "IOdictionary.H"
..............
............

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

namespace Foam
{

// Forward declarations
class fvMesh;

/*---------------------------------------------------------------------------*\
Class turbulenceModel Declaration
\*---------------------------------------------------------------------------*/

class turbulenceModel
:
public IOdictionary
{
protected:

// Protected data

const Time& runTime_;
const fvMesh& mesh_;

..................
.....................
}

So everytime you see it, it could be either incompressibleTurbulenceModel or compressibleTurbulenceModel.


Moreover, to better understand the turbulenceModel structure in OpenFOAM, one can see this blog (in Chinese unfortunately)
https://marinecfd.xyz/post/openfoam-...mework-part-1/

Thanks again for dear Foamers who answered my question.:)

mAlletto October 29, 2022 06:16

I wanted to access a specific turbulence model in a coded function object(a compressible Spalart Allmaras model).



I achieve it by this line of code



Code:



const auto& turbulence = 

        mesh().lookupObject<Foam::RASModels::SpalartAllmaras<
CompressibleTurbulenceModel<
compressibleTransportModel>>>("turbulenceProperties");

in order to get the object with template function lookupOpject i need to specify the correct type of the object I wanted to get.



Since the SpalartAllmaras model takes the template argument SpalartAllmaras<BasicTurbulenceModel> I had to specify a specific type of BasicTurbulenceModel. I wanted to have a compressible turbulence model.


The BasicTurbulenceModel is also a template class with an argument transportModel BasicTurbulenceModel<transportModel>. For this reason i needed a specific type of transport model. Finally the specific transport model was a compressibleTransportModel. This class is no template class and finally the compilation worked.


I did the whole exercise in order to have access to the Stilda() function of the Spalart Allmaras model. But then I realized it is a protected function and cannot be accessed outside the class hierarchy :(



But at the end I understood how the turbulence models are build :)


Or does anyone know how to access a protected member function from outside the class hierarchy?


Best


Michael


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