|
[Sponsors] |
What is does the "()" operator do in "turbulence->k() ()"? |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
May 24, 2017, 14:42 |
What does the "()" operator do in "turbulence->k() ()"?
|
#1 |
Member
Join Date: May 2016
Posts: 33
Rep Power: 10 |
I am trying to understand what the purpose of the "()" operator is. What is the difference between
Code:
turbulence->k() () Code:
turbulence->k() In this case, "turbulence" is declared as Code:
autoPtr<compressible::turbulenceModel> & turbulence Is it related to "const"? Would this return a "const" or a "non-const"? How can I get the same thing without "const"? Is it different in OpenFOAM v.4 compared to v.3? (maybe related to this commit). Any hints would be useful, because I don't know how and where to search for this kind of operator. Edit: I still don't know what it does, but it looks like it works in OpenFOAM 4.1 if I write: Code:
turbulence->k().ref() Last edited by MakisH; May 25, 2017 at 05:53. |
|
May 25, 2017, 12:03 |
|
#2 |
Member
Tomas Denk
Join Date: May 2017
Posts: 30
Rep Power: 9 |
I think it is dereference operator. You can check tmp class for details:
Code:
//- Const dereference operator inline const T& operator()() const; Code:
tmp<scalarField> myFld(new scalarField(SIZE, VALUE)); refValue() = myFld(); Code:
scalarField myFld = patch.lookupPatchField<volScalarField, scalar> (NAME); refValue() = myFld; |
|
May 26, 2017, 09:05 |
|
#3 | |
Member
Join Date: May 2016
Posts: 33
Rep Power: 10 |
Thank you very much TomasDenk! I am still a bit confused about the purpose of the operator.
However, I found this useful note in the release notes of OpenFOAM 4: Quote:
|
||
May 30, 2017, 09:38 |
|
#4 |
Member
Tomas Denk
Join Date: May 2017
Posts: 30
Rep Power: 9 |
You understood well the note that you quote and the points 1 through 3 are correct. As for the purpose of the "()" operator - it gives you access to the actual field wrapped in tmp<> template class. And as you mention, you can use .ref() to access non-const field.
<P>Why would you use tmp<> class? It is customary to wrap temporary fields in tmp<> to make sure that the memory is released properly. However, I'm not solid C++ programmer and someone with deepr insight could provide you with more detailed answer. |
|
June 1, 2017, 07:33 |
|
#6 |
Member
Join Date: May 2016
Posts: 33
Rep Power: 10 |
Thank you both for the help!
I have another question on this: what would happen if I did not use this operator? I think this would really help me understanding. I.e. what is the difference between the two lines in my first post (for OpenFOAM version < 4)? Code:
turbulence->k() () vs turbulence->k() 1. Using these in an inline computation or printing. 2. Using these as arguments to call a method. @TomasDenk: From what I read on OpenFOAM Wiki, I think the most important reason to use the tmp<> is to avoid copying large data around whenever a function is called. But of course the "proper release of memory" is also important. |
|
June 13, 2017, 10:32 |
|
#7 |
Member
Tomas Denk
Join Date: May 2017
Posts: 30
Rep Power: 9 |
Sorry for my late reply, I was very busy while traveling.
Suppose you want to use such field wrapped in tmp class for setting BC. I think you can use the wrapper object without harm: Code:
tmp<scalarField> fld(new scalarField(fldSize, anyValue)); // do some field operations refValue() = fld; Code:
forAll(*this, faceI) { refValue()[faceI] = someFunction(fld[faceI]); } Code:
forAll(*this, faceI) { refValue()[faceI] = someFunction(fld()[faceI]); } |
|
Tags |
c++, openfoam, operator overloading, programming, reference |
|
|