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

Construct and return a clone - trying to understand this

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 2 Post By olesen

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   October 27, 2020, 16:53
Default Construct and return a clone - trying to understand this
  #1
Member
 
Stanley John
Join Date: Sep 2018
Posts: 79
Rep Power: 7
sjohn2 is on a distinguished road
Hi,

I am trying to understand openfoam coding and I have come across this piece of code to often. What does this do?


It located in /src/lagrangian/parcel/submodels/Momentum/ParticleForces/Gravity/GravityForce.H


PHP Code:
        //- Construct and return a clone
        
virtual autoPtr<ParticleForce<CloudType>> clone() const
        {
            return 
autoPtr<ParticleForce<CloudType>>
            (
                new 
ParticleForce<CloudType>(*this)
            );
        } 
This is in the definition file for each of this particle forces, when is it invoked?

In /src/lagrangian/parcel/submodels/ForceTypes/ParticleForceList/ParticleForceList.C

I do see a piece of code

PHP Code:
if (readFields)
    {
        
wordList modelNames(dict.toc());

        
Info<< "Constructing particle forces" << endl;

        if (
modelNames.size() > 0)
        {
            
this->setSize(modelNames.size());

            
label i 0;
            
forAllConstIter(IDLList<entry>, dictiter)
            {
                const 
wordmodel iter().keyword();
                if (
iter().isDict())
                {
                    
this->set
                    
(
                        
i++,
                        
ParticleForce<CloudType>::New
                        (
                            
owner,
                            
mesh,
                            
iter().dict(),
                            
model
                        
)
                    );
                }
                else
                {
                    
this->set
                    
(
                        
i++,
                        
ParticleForce<CloudType>::New
                        (
                            
owner,
                            
mesh,
                            
dictionary::null,
                            
model
                        
)
                    );
                }
            }
        }
        else
        {
            
Info<< "    none" << endl;
        }
    } 
how does it recognize the particle force lists? and when does it invoke the contructor?
sjohn2 is offline   Reply With Quote

Old   October 29, 2020, 16:15
Default
  #2
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,685
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Too many questions all at once. Which is your first one?
olesen is offline   Reply With Quote

Old   October 29, 2020, 20:34
Default
  #3
Member
 
Stanley John
Join Date: Sep 2018
Posts: 79
Rep Power: 7
sjohn2 is on a distinguished road
Thanks Mark, lets go top top bottom as I understand the flow goes from:

1) ParticleForceList
2) ParticleForce
3) Individual particle forces for eg: gravity

As I understand in ParticleForceList.C it reads user defined input for particles forces and based on user input it invokes
PHP Code:
ParticleForce<CloudType>::New 
Ques 1) I think this invoking the constructor, how does it recoganize which particle force constructor to invoke. I think this is defined in ParticleForceNew.C

Ques2) In object particleForce object and also individual particle forces (eg gravity) there is base constructor and a clone of it. Also below it there is
PHP Code:
        //- Construct and return a clone
        
virtual autoPtr<ParticleForce<CloudType>> clone() const
        {
            return 
autoPtr<ParticleForce<CloudType>>
            (
                new 
ParticleForce<CloudType>(*this)
            );
        } 
What is the purpose of this style of coding? To to be specific why do we need a clone for the constructor and clone for autoptr?

Really appreciate your help!
sjohn2 is offline   Reply With Quote

Old   October 30, 2020, 18:34
Default
  #4
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,685
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by sjohn2 View Post
1) I think this invoking the constructor, how does it recoganize which particle force constructor to invoke. I think this is defined in ParticleForceNew.C
OK, even with a decent level of C++ knowledge, it's not obvious how any of this works. The quick summary: many base-level classes have an embedded local class that "babysits" a HashTable of pointers. Lookup by name, return a pointer. In this case a pointer to wrap a constructor to a derived class. If you add more models, you register their name and constructor to the base-level bookkeeper. The "New" static methods are HashTable lookups and dispatching. In all majority of the cases an autoPtr is used for the memory management, so you don't leak or otherwise be sloppy with your pointers. In some cases, a "tmp" is used for the memory management instead (other reasons, a different post).

To understand the benefit of the clone() method, consider not having it - what would that look like for the following task?
Make a copy of an item. If we know the type is "ABaseClass", we can write that, but what about "BDerivedClass" and "CDerivedClass" ? How would you create a copy of each list item, which derived from "A"?

Enter the clone() method. Since it is virtual, it "knows" it's own type. Call it, and you get the copy you were looking for without knowing the type.
Farid and Muthaiah like this.
olesen is offline   Reply With Quote

Old   November 3, 2020, 01:29
Default
  #5
Member
 
Stanley John
Join Date: Sep 2018
Posts: 79
Rep Power: 7
sjohn2 is on a distinguished road
Thank you the explanation, as wannabe openFoam programmer, can you recommend any examples/keywords/lookups videos that can help one understand this, with out the additional baggage of dealling with 100's of differnet files? Anything that you used to self learn and climb the ladder up?

Thanks once again
sjohn2 is offline   Reply With Quote

Old   November 3, 2020, 15:20
Default
  #6
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,685
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by sjohn2 View Post
Anything that you used to self learn and climb the ladder up?
Most of this is stuff that a regular user/programmer using OpenFOAM doesn't really need to know. Just find existing code patterns that mostly resemble what you need and go from there. And don't be overly smart and try to do something totally different if you don't understand why the original code was written that way.
Like any natural language, it takes time and practice to become reasonably fluent.
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
No matching function error: Phase change source term added to interMixingFoam wavefunction OpenFOAM Programming & Development 2 February 4, 2022 07:46
undefined reference to a C++'s class member function linyanx OpenFOAM Programming & Development 5 May 12, 2017 12:44
error adding void fraction into the solver & Error when chemistry is on cmigueis OpenFOAM Programming & Development 23 August 14, 2016 14:53
Add Temperature to multiphaseInterFoam nthiers OpenFOAM Programming & Development 6 February 17, 2016 08:42
Compiled custom wall function unknown by solver juhuettn OpenFOAM Programming & Development 22 October 22, 2014 09:56


All times are GMT -4. The time now is 01:07.