CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM

To tmp or not not?

Register Blogs Community New Posts Updated Threads Search

Like Tree3Likes
  • 3 Post By tiam

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 25, 2017, 21:51
Default To tmp or not not?
  #1
Senior Member
 
Adhiraj
Join Date: Sep 2010
Location: Karnataka, India
Posts: 187
Rep Power: 15
adhiraj is on a distinguished road
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);
The other option is to do this
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);
Sine this computation is done several times every time step, which option is better for efficiency purposes, and why?

Any help is appreciated, thanks.
adhiraj is offline   Reply With Quote

Old   March 27, 2017, 08:39
Default
  #2
Senior Member
 
Timofey Mukha
Join Date: Mar 2012
Location: Stockholm, Sweden
Posts: 118
Rep Power: 14
tiam is on a distinguished road
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.
floquation, ugurtan666 and lpz456 like this.
tiam is offline   Reply With Quote

Reply


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[IHFOAM] The IHFOAM Thread Phicau OpenFOAM Community Contributions 392 September 8, 2023 18:10
call a tmp formal value without a parentheses? sharonyue OpenFOAM Programming & Development 4 May 23, 2020 17:48
error adding void fraction into the solver & Error when chemistry is on cmigueis OpenFOAM Programming & Development 23 August 14, 2016 14:53
LES correct function luiz eduardo OpenFOAM Running, Solving & CFD 6 September 14, 2007 04:10


All times are GMT -4. The time now is 17:59.