|
[Sponsors] |
March 15, 2023, 09:21 |
MemberFunction and Private Member Function
|
#1 |
Member
Amirhossein Taran
Join Date: Sep 2016
Location: Dublin, Ireland
Posts: 57
Rep Power: 10 |
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(); } Code:
inline Foam::vector Foam::sixDoFRigidBodyMotionNew::omega() const { return Q() & (inv(momentOfInertia_) & pi()); } 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. |
|
March 16, 2023, 07:49 |
|
#2 |
Senior Member
Join Date: Apr 2020
Location: UK
Posts: 746
Rep Power: 14 |
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. |
|
March 16, 2023, 09:06 |
|
#3 | |
Member
Amirhossein Taran
Join Date: Sep 2016
Location: Dublin, Ireland
Posts: 57
Rep Power: 10 |
Quote:
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 |
||
November 8, 2024, 00:33 |
|
#4 |
New Member
Join Date: Mar 2024
Posts: 18
Rep Power: 2 |
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 |
|
November 8, 2024, 07:19 |
|
#5 | |
Member
Amirhossein Taran
Join Date: Sep 2016
Location: Dublin, Ireland
Posts: 57
Rep Power: 10 |
Quote:
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; #}; } #; |
||
November 8, 2024, 10:40 |
|
#6 | |
New Member
Join Date: Mar 2024
Posts: 18
Rep Power: 2 |
Quote:
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. |
||
November 8, 2024, 10:44 |
|
#7 |
New Member
Join Date: Mar 2024
Posts: 18
Rep Power: 2 |
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. |
|
Tags |
foam-extend 4.1, member function, openfoam programming, private member function |
|
|