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/)
-   -   v2f - 'if' and 'else' condition (https://www.cfd-online.com/Forums/openfoam-programming-development/229937-v2f-if-else-condition.html)

gu1 September 1, 2020 09:55

v2f - 'if' and 'else' condition
 
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

t.teschner September 3, 2020 16:31

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


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