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

MemberFunction and Private Member Function

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 1 Post By Tobermory
  • 1 Post By amirhosseintaran

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 15, 2023, 09:21
Default MemberFunction and Private Member Function
  #1
Member
 
Amirhossein Taran
Join Date: Sep 2016
Location: Dublin, Ireland
Posts: 57
Rep Power: 10
amirhosseintaran is on a distinguished road
Dear Foamers,
If you navigate through a class like sixDofRigidBodyMotion, you can see that in the sixDoFRigidBodyMotionI.H there is a private member function for body velocity and it is declared as:

Code:
inline const Foam::vector& Foam::sixDoFRigidBodyMotionNew::v() const
{
    return motionState_.v();
}
in the member function section, there is also a member function for angular velocity called omega() and it declared as:

Code:
inline Foam::vector Foam::sixDoFRigidBodyMotionNew::omega() const
{
    return  Q() & (inv(momentOfInertia_) & pi());
}
Now the question is, If I want to have access to the object's velocity, how should I get that?
the same procedure for the angular velocity is easy as calling the member function object.omega().

Generally, when should we declare sth like velocity as a private member function and not member function?

Bests,
Amirhossein.
amirhosseintaran is offline   Reply With Quote

Old   March 16, 2023, 07:49
Default
  #2
Senior Member
 
Join Date: Apr 2020
Location: UK
Posts: 746
Rep Power: 14
Tobermory will become famous soon enough
If you read the header file for sixDofRigidBodyMotion carefully, you will see that there are in fact two versions of member function v() - a private version that returns a non-const reference to the vector, and a public one that returns a const reference. These are implemented in the inline header file (https://cpp.openfoam.org/v8/sixDoFRi...8H_source.html). Can you now see why?

The public one is an access function, and is meant to allow other classes to just read the value (hence const). The private one is for use within the class, and is therefore not const.

To answer your wider question - this seems like a pretty good approach to me. Allow for const member access functions where useful, and keep the non-const version private.
amirhosseintaran likes this.
Tobermory is offline   Reply With Quote

Old   March 16, 2023, 09:06
Default
  #3
Member
 
Amirhossein Taran
Join Date: Sep 2016
Location: Dublin, Ireland
Posts: 57
Rep Power: 10
amirhosseintaran is on a distinguished road
Quote:
Originally Posted by Tobermory View Post
If you read the header file for sixDofRigidBodyMotion carefully, you will see that there are in fact two versions of member function v() - a private version that returns a non-const reference to the vector, and a public one that returns a const reference. These are implemented in the inline header file (https://cpp.openfoam.org/v8/sixDoFRi...8H_source.html). Can you now see why?

The public one is an access function, and is meant to allow other classes to just read the value (hence const). The private one is for use within the class, and is therefore not const.

To answer your wider question - this seems like a pretty good approach to me. Allow for const member access functions where useful, and keep the non-const version private.
Thanks, Tobermory for the reply,
yeah the fact you are saying is true, anyway in foam-extend there is no other version for v so I had to create a member function that just returns v!

And thanks for the explanation, I got my answer.
Bests,
Amirhossein
Tobermory likes this.
amirhosseintaran is offline   Reply With Quote

Old   November 8, 2024, 00:33
Default
  #4
New Member
 
Join Date: Mar 2024
Posts: 18
Rep Power: 2
panda007 is on a distinguished road
Hi Taran,

I'm working on a FSI problem and wanted to access the omega() value under the sixDoFRigidBodyMotion class. I'm struggling how to get the value, do you have any suggestion?

I want to write the #codestream to customize a damping value based on the transient omega(). But so far has no success. TIA
panda007 is offline   Reply With Quote

Old   November 8, 2024, 07:19
Default
  #5
Member
 
Amirhossein Taran
Join Date: Sep 2016
Location: Dublin, Ireland
Posts: 57
Rep Power: 10
amirhosseintaran is on a distinguished road
Quote:
Originally Posted by panda007 View Post
Hi Taran,

I'm working on a FSI problem and wanted to access the omega() value under the sixDoFRigidBodyMotion class. I'm struggling how to get the value, do you have any suggestion?

I want to write the #codestream to customize a damping value based on the transient omega(). But so far has no success. TIA
Hi Panda,

Although I havent tried it, but something like this might help, Could you try please and let me know ?

Code:
myDampingFactor
#codeStream
{
    codeInclude
    #{
        #include "sixDoFRigidBodyMotion.H"
    #};

    code
    #{
        // Retrieve the pointer to the sixDoFRigidBodyMotion object
        const auto& sixDoFModel = mesh.lookupObject<sixDoFRigidBodyMotion>("<motionObjectName>");
        
        // Access the angular velocity, Omega
        const vector omega = sixDoFModel.Omega();

        // Calculate a custom damping factor based on omega
        scalar dampingFactor = <your_custom_function>(mag(omega));
        
        // Output the calculated damping factor
        os << dampingFactor;
    #};
    }
#;
amirhosseintaran is offline   Reply With Quote

Old   November 8, 2024, 10:40
Default
  #6
New Member
 
Join Date: Mar 2024
Posts: 18
Rep Power: 2
panda007 is on a distinguished road
Quote:
Originally Posted by amirhosseintaran View Post
Hi Panda,

Although I havent tried it, but something like this might help, Could you try please and let me know ?

Code:
myDampingFactor
#codeStream
{
    codeInclude
    #{
        #include "sixDoFRigidBodyMotion.H"
    #};

    code
    #{
        // Retrieve the pointer to the sixDoFRigidBodyMotion object
        const auto& sixDoFModel = mesh.lookupObject<sixDoFRigidBodyMotion>("<motionObjectName>");
        
        // Access the angular velocity, Omega
        const vector omega = sixDoFModel.Omega();

        // Calculate a custom damping factor based on omega
        scalar dampingFactor = <your_custom_function>(mag(omega));
        
        // Output the calculated damping factor
        os << dampingFactor;
    #};
    }
#;
Hi Taran,

Thanks for the quick response!!

I added the codeOptions and codeLibs to include the library, and just tried. It gave me an error, mesh is not declared in this scope. See screenshot.
Attached Images
File Type: png Screenshot 2024-11-08 093754.png (70.4 KB, 1 views)
panda007 is offline   Reply With Quote

Old   November 8, 2024, 10:44
Default
  #7
New Member
 
Join Date: Mar 2024
Posts: 18
Rep Power: 2
panda007 is on a distinguished road
Here is the code I put in, I have tested the rest of the code without the retrieving value, just assign omega as a constant value, it ran without problem. The problem is retrieving syntax.


Code:
damping #codeStream
            {
                codeInclude
                #{
                    #include "sixDoFRigidBodyMotion.H"
                    // #include "sixDoFRigidBodyMotionI.H"
                    // #include "sixDoFRigidBodyMotionState.H"
                #};

                codeOptions
                #{
                    -I$(WM_PROJECT_DIR)/src/sixDoFRigidBodyMotion/lnInclude \
                #};

                codeLibs
                #{
                    -lsixDoFRigidBodyMotion
                #};

                code
                #{
                    const auto& sixDoFModel = mesh.lookupObject<sixDoFRigidBodyMotion>("<motionObjectName>");
                    const vector omega = sixDoFModel.Omega();
                    Foam::scalar omegaMag = mag(omega);

                    Foam::scalar dampingValue;

                    if (omegaMag >= 0.01)
                    {
                        dampingValue = 10.24 / omegaMag;
                    }
                    else
                    {
                        dampingValue = 1024 * omegaMag;
                    }
                    os << dampingValue;
                #};
                os << damping;
            };

I tried to include fvCFD.H and the corresponding library and options, it still gave me the same error, mesh was not declared in this scope.
panda007 is offline   Reply With Quote

Reply

Tags
foam-extend 4.1, member function, openfoam programming, private member function


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 14:42.