CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
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

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

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):

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

else 
{
     template<class BasicTurbulenceModel>
     tmp<volScalarField> v2fDES<BasicTurbulenceModel>::Ls() const
     {
            return
                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).

*OF5
gu1 is offline   Reply With Quote

Old   September 3, 2020, 17:31
Default
  #2
Senior Member
 
Tom-Robin Teschner
Join Date: Dec 2011
Location: Cranfield, UK
Posts: 203
Rep Power: 16
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

Code:
#include <iostream>
#include <string>

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

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

class v2f {
public:
    template<class Option>
    std::string getOptionName(const Option &option) const {
        return option.name();
    }
};

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

Reply

Tags
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 12:03.