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

How and when use tmp<vol****Fields>?

Register Blogs Community New Posts Updated Threads Search

Like Tree4Likes
  • 1 Post By agustinvo
  • 1 Post By Santiago
  • 1 Post By Santiago
  • 1 Post By Santiago

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   July 28, 2022, 02:00
Default How and when use tmp<vol****Fields>?
  #1
Senior Member
 
Agustín Villa
Join Date: Apr 2013
Location: Alcorcón
Posts: 313
Rep Power: 15
agustinvo is on a distinguished road
Hi,


I know that this topic has been discussed previously:
But it still not clear, at least ceraint aspects, for me. I am not a C++ developer but a OpenFOAM user that deals with turbulence model implementations. I had at the university a course where pointers were introduced (15 years ago), so please don't be so acid me if I don't get the idea inmediately. These are my ideas with respect tmp fields

  • tmp fields/matrices: we use them when we want (or need) to clear memory, because these fields/matrices are large. This is what simpleFoam does with UEqn as a tmp<fvVectorMatrix>. We generate tFields, tEqns... to indicate that are tmp objects.
    Code:
    tmp<fvVectorMatrix> tUEqn
        (
            fvm::div(phi, U)
          + MRF.DDt(U)
          + turbulence->divDevSigma(U)
         ==
            fvModels.source(U)
        );
  • Once this tmp<fvVectorMatrix> is defined, where do we see the memory usage impact of this method? Only when clearing tUEqn?
  • Is it needed to clear all the tFields, tEqns... in the code? I have heard a bit abut scopes using curly braces (this is what), and AFAIK if I define a variable inside a scope they are destroyed (cleared) once the program goes out of the scope. If I use these tFields inside a scope, am I clearing them automatically? If I clear, for example, tUEqn, what happens with UEqn? Does it stays in memory?
  • What does tUEqn.ref() means? As far as I understood, when we do
    Code:
    fvVectorMatrix& UEqn = tUEqn.ref();
    we are pointing to the tUEqn.
    Why can't I use tUEqn directly? If UEqn is defined to point to the tUEqn, I suppose you have a reference to a pointer, but if the original data is available, I don't see the point of creating this intermediate object.
Thank you for your help!
edsaac likes this.
agustinvo is offline   Reply With Quote

Old   August 2, 2022, 10:31
Default
  #2
Senior Member
 
Santiago Lopez Castano
Join Date: Nov 2012
Posts: 354
Rep Power: 15
Santiago is on a distinguished road
tmp<> is an pre-STL version of a pointer Handle or weak_ptr<>. Is a means of moving (as opposed to copying) data across function calls while avoiding registering said data in the time selection mechanism (Factory) in FOAM. You use it when you don't want to duplicate data, for short lived templated datatypes that don't need be registered in the time selection mechanism, or when you are not sure of the lifetime of the original data (a reference can be deallocated, and become invalid, a tmp guarantees the lifetime of the underlying object by keeping a count).

There is another use: geometricFields (and many other things in FOAM) have no null-constructor. You may want to declare a tmp<geometricField<...> > in one scope and DEFINE it in some other scope, for example.
agustinvo likes this.
Santiago is offline   Reply With Quote

Old   August 2, 2022, 10:44
Default
  #3
Senior Member
 
Santiago Lopez Castano
Join Date: Nov 2012
Posts: 354
Rep Power: 15
Santiago is on a distinguished road
ref() is a construct in new versions of FOAM: it returns a reference of what is inside the tmp, so you don't have to de-reference the tmp everytime you do operations on it. Earlier versions of FOAM used operator() for returning the reference.

BTW, the code you show has little context so it is difficult to say why tmp was used there.
agustinvo likes this.
Santiago is offline   Reply With Quote

Old   August 2, 2022, 10:49
Default
  #4
Senior Member
 
Santiago Lopez Castano
Join Date: Nov 2012
Posts: 354
Rep Power: 15
Santiago is on a distinguished road
Don't want to be acid but the rest of the questions refer to basic C/C++ (scopes, lifetime, what is memory) so ill refer you to "accelerated c++" for details.
agustinvo likes this.
Santiago is offline   Reply With Quote

Old   August 3, 2022, 00:04
Default
  #5
Senior Member
 
Agustín Villa
Join Date: Apr 2013
Location: Alcorcón
Posts: 313
Rep Power: 15
agustinvo is on a distinguished road
Thank you for your comments! I will check that reference about C++. I hope it helps me to get the idea behind using these concepts.
agustinvo is offline   Reply With Quote

Old   June 30, 2023, 05:37
Default
  #6
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,686
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Need to comment that there is yet another reason for the tmp - it can either hold a managed pointer (somewhat like a mix of unique_ptr and shared_ptr) but it can also hold a reference to an object (somewhat like std reference_wrapper, but can be nullptr initialized).
This duality is be particularly convenient.
There is also a refPtr wrapper that is similar in many ways to tmp but does not require object to have embedded reference counting.

olesen 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



All times are GMT -4. The time now is 16:30.