|
[Sponsors] |
March 25, 2017, 22:51 |
To tmp or not not?
|
#1 |
Senior Member
Adhiraj
Join Date: Sep 2010
Location: Karnataka, India
Posts: 187
Rep Power: 16 |
Hello all:
I am trying to create a library for my work, and I have a question about the use of tmp in OpenFOAM. I have a little bit of understanding of how it works, but I don't know enough about the implementation to know anything about efficiency (running faster) for my case. So here is the question: at every time step I need to obtain a volScalarField phi, and use its value. Something like this Code:
phi = sqrt(a) + sqr(b); where a and b are volScalarFields. In this case one option is to define phi as a private member of the class and just do this: Code:
phi *= 0; phi = sqrt(a) + sqr(b); Code:
tmp<volScalarField> tphi ( new volScalarField ( IOobject ( "phiTemp", kappa_.mesh().time().timeName(), kappa_.mesh(), IOobject::NO_READ, IOobject::NO_WRITE, false ), kappa_.mesh(), dimensionedScalar("zero", dimless, 0.0) ) ); volScalarField& phi = tphi(); phi = sqrt(a) + sqr(b); Any help is appreciated, thanks. |
|
March 27, 2017, 09:39 |
|
#2 |
Senior Member
Timofey Mukha
Join Date: Mar 2012
Location: Stockholm, Sweden
Posts: 119
Rep Power: 14 |
The main idea with tmp is to be able to return a reference to an object created locally inside a function.
Imagine you have a function f() which you use to compute a volScalarField, the size of which scales with your mesh size. If you use a regular return statement, this will lead to the giant volScalarField being copied. Instead one would like to return a reference to the created field. But one cannot do that because returning a reference to a local variable is not permitted (which makes sense, since it goes out of scope as soon as you exit the function). The tmp class allows to by-pass this. In your case you are directly updating a member of a class, so I don't think tmp will bring any benefit. |
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
[IHFOAM] The IHFOAM Thread | Phicau | OpenFOAM Community Contributions | 392 | September 8, 2023 19:10 |
call a tmp formal value without a parentheses? | sharonyue | OpenFOAM Programming & Development | 4 | May 23, 2020 18:48 |
error adding void fraction into the solver & Error when chemistry is on | cmigueis | OpenFOAM Programming & Development | 23 | August 14, 2016 15:53 |
LES correct function | luiz eduardo | OpenFOAM Running, Solving & CFD | 6 | September 14, 2007 05:10 |