CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Construct and return a clone - trying to understand this (https://www.cfd-online.com/Forums/openfoam-programming-development/231233-construct-return-clone-trying-understand.html)

sjohn2 October 27, 2020 16:53

Construct and return a clone - trying to understand this
 
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?

olesen October 29, 2020 16:15

Too many questions all at once. Which is your first one?

sjohn2 October 29, 2020 20:34

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!

olesen October 30, 2020 18:34

Quote:

Originally Posted by sjohn2 (Post 786329)
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.

sjohn2 November 3, 2020 01:29

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

olesen November 3, 2020 15:20

Quote:

Originally Posted by sjohn2 (Post 786633)
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.


All times are GMT -4. The time now is 23:56.