|
[Sponsors] |
September 1, 2020, 10:55 |
v2f - 'if' and 'else' condition
|
#1 |
Senior Member
Guilherme
Join Date: Apr 2017
Posts: 245
Rep Power: 10 |
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_)); } } 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 |
|
September 3, 2020, 17:31 |
|
#2 |
Senior Member
Tom-Robin Teschner
Join Date: Dec 2011
Location: Cranfield, UK
Posts: 211
Rep Power: 16 |
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; } 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 |
|
Tags |
c++, openfoam5 |
|
|