CFD Online Logo CFD Online URL
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

v2f - 'if' and 'else' condition

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

Like Tree1Likes
  • 1 Post By t.teschner

LinkBack Thread Tools Search this Thread Display Modes
Old   September 1, 2020, 10:55
Default v2f - 'if' and 'else' condition
Senior Member
Join Date: Apr 2017
Posts: 184
Rep Power: 7
gu1 is on a distinguished road

I need help to add an 'if' condition to my turbulence model. Unfortunately I have some difficulties with C++, I would be very happy if someone could help me.

The code example is as follows (from the v2f.C file):

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

     template<class BasicTurbulenceModel>
     tmp<volScalarField> v2fDES<BasicTurbulenceModel>::Ls() const
                CL_*max(pow(k_, 1.5)
                /epsilon_, Ceta_*pow025(pow3(this->nu())/epsilon_));
That's the idea, but I don't know if it's the right way to do it.
Would I also need to add something to v2f.H?

Please, whoever can teach me how to do it... as I said, is an example (that is, the concept I want).

gu1 is offline   Reply With Quote

Old   September 3, 2020, 17:31
Senior Member
Tom-Robin Teschner
Join Date: Dec 2011
Location: Cranfield, UK
Posts: 202
Rep Power: 15
t.teschner is on a distinguished road
You can't have if / else statements around function definitions and declarations. Your use case is screaming for templates, which is how we would deal with that most commonly. Take the following source code as an example

#include <iostream>
#include <string>

class option1 {
    std::string name() const { return std::string("option 1"); }

class option2 {
    std::string name() const { return std::string("option 2"); }

class v2f {
    template<class Option>
    std::string getOptionName(const Option &option) const {

int main() {

    option1 opt1;
    option2 opt2;

    v2f object;

    std::cout << "Option 1: " << object.getOptionName(opt1) << std::endl;
    std::cout << "Option 2: " << object.getOptionName(opt2) << std::endl;

    return 0;
My dummy class v2f has only one function getOptionName() but in the main function we call the same function twice on a different class object which prints results from two different classes (but instead of printing different text here, you could also have two different calculations performed and return those instead).

Well, the above code is quite shor but has several concepts which are important here, most importantly templates, but also template type deduction which are probaby concepts which are quite heavy to diggest if you have not come across them before.

So to answer your question, yes, it can be done, but it requires some more in depth c++ knowledge. Hope this helps anyways
gu1 likes this.
t.teschner is offline   Reply With Quote


c++, openfoam5

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

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