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

Access the Time object in sixDoFRigidBodyMotionRestraints

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 1 Post By alexeym
  • 1 Post By SailorLiu

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 19, 2016, 13:31
Default Access the Time object in sixDoFRigidBodyMotionRestraints
  #1
Member
 
Yuanchuan Liu
Join Date: Oct 2012
Posts: 31
Rep Power: 14
SailorLiu is on a distinguished road
Dear Foamers,

I have been trying to implement a new restraint type for the sixDoFRigidBodyMotion class and need to access the Time object for the current time value as well as the time step size deltaT. Does anybody know how to do that in OpenFOAM-2.4.x?
Thanks in advance.

Best wishes,

Yuanchuan Liu
SailorLiu is offline   Reply With Quote

Old   January 20, 2016, 03:51
Default
  #2
New Member
 
Join Date: Dec 2015
Posts: 24
Rep Power: 11
metalfox is on a distinguished road
I believe it might be as simple as:

Code:
const Time &runTime= db().time()
metalfox is offline   Reply With Quote

Old   January 20, 2016, 07:36
Default
  #3
Member
 
Yuanchuan Liu
Join Date: Oct 2012
Posts: 31
Rep Power: 14
SailorLiu is on a distinguished road
Hi, thank you for your reply.

Unfortunately, inserting the line you suggested to my code results in an error message:
error: ‘db’ was not declared in this scope

It seems I do not have access to db as well. Cheers.
SailorLiu is offline   Reply With Quote

Old   January 20, 2016, 08:57
Default
  #4
New Member
 
Join Date: Dec 2015
Posts: 24
Rep Power: 11
metalfox is on a distinguished road
Code:
const objectRegistry & db () const
is a member function of IOObject.

IOObject belongs to the inheritance tree of sixDoFRigidBodyMotionSolver so it should be accessible whithin the scope of this class.

I assumed that you were modifying this class. Sorry if I missunderstood your question.

If you are working in the "application" scope, runTime should be directly accessible if you created it:

Code:
    #include "createTime.H"
db() may also be accessed through other objects.
metalfox is offline   Reply With Quote

Old   January 20, 2016, 09:00
Default
  #5
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,938
Rep Power: 39
alexeym has a spectacular aura aboutalexeym has a spectacular aura about
Send a message via Skype™ to alexeym
Hi,

If you take a look at documentation (http://foam.sourceforge.net/docs/cpp/a02312.html), you find, sixDoFRigidBodyMotion class has nothing connecting it to object registry (where you usually find Time object). It has general geometric fields, sixDoFRigidBodyMotionState, and lists of sixDoFRigidBodyMotionRestraint and sixDoFRigidBodyMotionConstrain. None of those classes has access to object registry. So, I guess, the problem, as it is described in the first post, has no solution.

Yet, you can describe in more details, what you are trying to achieve, maybe there is a solution (for example you can get object registry as a parameter or something else).
fumiya likes this.
alexeym is offline   Reply With Quote

Old   January 20, 2016, 09:49
Default
  #6
Member
 
Yuanchuan Liu
Join Date: Oct 2012
Posts: 31
Rep Power: 14
SailorLiu is on a distinguished road
Hi metalfox and alexeym,

Thank you both so much for your time. I think it might be best if I can describe what I want to achieve in more details.

Bascially, I created a new restraint class for the sixDoFRigidBodyMotion class. I just copied the built-in linearSpring restraint class and made some modifications based on it. The new restraint class is designed to employ a simple model to simulate mooring lines, where I need to know the time step size deltaT for force calculation and also the current time value to save the line geometry to VTK files with the current time name only to distinguish each file with others.

alexeym was right to the point. What makes it difficult is that None of those classes has access to object registry so I am unable to reference it in the way mentioned by metalfox. The current solution I adopted is to modify the sixDoFRigidBodyMotion class by adding some functions to get the values I need from its caller and also return the values to other classes which need them, as the sixDoFRigidBodyMotionRestraints class does not interact with other classes but the sixDoFRigidBodyMotion class. The method mentioned by alexeym might be a better solution as it only involves a single modification to the sixDoFRigidBodyMotion class so I do not have to edit it again if next time another variable related to the object registry is needed.

Thanks to your guys very much. Now I know it is not possible if I do not want to edit the sixDoFRigidBodyMotion class.

Best wishes,

Yuanchuan Liu
fumiya likes this.
SailorLiu is offline   Reply With Quote

Old   January 20, 2016, 11:37
Default
  #7
New Member
 
Join Date: Dec 2015
Posts: 24
Rep Power: 11
metalfox is on a distinguished road
I apologize because the OpenFOAM objects I'm familiar with usually inherit in some way from IOObject.

However, I believe that there is another workaround that would require no modification to sixDoFRigidBodyMotion.

Wouldn't:
Code:
extern Time runTime;
in your .cpp file do the thrick?

It's not very elegant but I think it migth work (though it may require the solver to be linked with -rdynamic, not sure...). Please let me know...
metalfox is offline   Reply With Quote

Old   January 20, 2016, 12:37
Default
  #8
Member
 
Yuanchuan Liu
Join Date: Oct 2012
Posts: 31
Rep Power: 14
SailorLiu is on a distinguished road
Hi metalfox,

I am very grateful for the help you provided. I am not very familiar with the extern keyword in C++. After a quick search, it seems to me that by defining a variable extern, you are telling the compiler that this variable has been defined somewhere else in another file so you can use it here.

I tried to insert the code into my cpp file and the library compiled successfully. However, when I tried to run my solver, there was an error saying the sixDoFRigidBodyMotionRestraint type is unknown. Maybe it has something to do with the linkage to the solver you mentioned?

Let us use a simple example to illustrate the problem. Say the solver I am going to use is mooringTest which is compiled from the source file mooringTest.C where I define the sixDoFRigidBodyMotion variable. The restraint class I inherited from the sixDoFRigidBodyMotionRestraints base class is called mooringLine which has a header file mooringLine.H and mooringLine.C. The mooringLine class is compiled to the libudfSixDoFRigidBodyMotionRestraints.so library. So when I want to use this restraint model with the mooringTest solver, I include the library in the case controlDict file.

If I want to use the method you suggested, should I add the
Code:
extern Time runTime;
line to the mooringLine.C file? What about the -rdynamic compiler option? Should I add it to the Make/options file of the mooringTest solver?

Thank you.
SailorLiu is offline   Reply With Quote

Old   January 20, 2016, 13:33
Default
  #9
New Member
 
Join Date: Dec 2015
Posts: 24
Rep Power: 11
metalfox is on a distinguished road
I've made a quick test, and I convinced myself that my solution should work:

File libfoo.cpp:
Code:
#include <iostream>

extern int foo;

void func() { std::cout << "foo = " << foo << std::endl; }
Compiling/linking the library:
Code:
g++ -fPIC -shared libfoo.cpp -o libfoo.so
File main.cpp:
Code:
void func();

int foo = 666;

int main() { func(); }
Compiling/linking the main program:
Code:
g++ main.cpp -o main -L. -lfoo
Running the program:
Code:
./main 
foo = 666
I think that the error you mentioned is related to other issue. If the extern trick wasn't working it would have caused the application to crash because of illegal memory access.
metalfox is offline   Reply With Quote

Old   January 20, 2016, 15:36
Default
  #10
Member
 
Yuanchuan Liu
Join Date: Oct 2012
Posts: 31
Rep Power: 14
SailorLiu is on a distinguished road
Hmm, things go a bit strange here.

I tried the test program you posted. For the libfoo.cpp:
Code:
#include <iostream>

extern int foo;

void func()
{
    std::cout << "foo = " << foo << std::endl; 
}
Compile it using:
Code:
g++ -fPIC -shared libfoo.cpp -o libfoo.so
For the main.cpp:
Code:
void func();

int foo = 666;

int main()
{
    func(); 
}
Compile and link using:
Code:
g++ main.cpp -o main -L. -lfoo
Nothing goes wrong until I try to execute the application with:
Code:
./main
An error message shows up:
Code:
./main: error while loading shared libraries: libfoo.so: cannot open shared object file: No such file or directory
Furthermore, since in my application the runTime variable is defined inside the main function, I moved
Code:
int foo = 666;
inside the main function. Now if I try to compile the main.cpp with the same command shown above, it says:
Code:
./libfoo.so: undefined reference to `foo'
Would you mind trying moving the part inside the main function to see if everything works fine? Thank you.
SailorLiu is offline   Reply With Quote

Old   January 21, 2016, 04:20
Default
  #11
New Member
 
Join Date: Dec 2015
Posts: 24
Rep Power: 11
metalfox is on a distinguished road
Dear SailorLiu

The executable does not find the library because it is not in the library path (LD_LIBRARY_PATH).

However, your are right. It would only work if runTime were in the global scope, which is not because argv need to be parsed for runTime to be constructed.

Furthermore, I was curious whether global variables can be accessed through shared libraries in the OpenFOAM framework. So, I created a Time object in the global scope of icoFoam and tried to access it through a custom bc. I got:
Code:
dlopen error : boundaryCondition/oddBC.so: undefined symbol: runTime
I'm afraid that alexeym was right. There do not appear to be a simple solution to your question.

I'm sorry.
metalfox is offline   Reply With Quote

Old   January 21, 2016, 08:50
Default
  #12
Member
 
Yuanchuan Liu
Join Date: Oct 2012
Posts: 31
Rep Power: 14
SailorLiu is on a distinguished road
Dear metalfox,

Please don't be sorry. I really appreciate the time and effort you devoted to my problem. But at least I can still solve it although not in an elegant way by cracking the sixDoFRigidBodyMotion class. Thank you very much! Hope everything goes well with you. Cheers.

Yuanchuan Liu
SailorLiu is offline   Reply With Quote

Old   January 15, 2017, 17:41
Default
  #13
New Member
 
Join Date: Dec 2013
Posts: 11
Rep Power: 13
Ricky-11 is on a distinguished road
Hey SailorLiu.

I've stumbled upon you post while looking for the same kind of info.

I've then played around a little bit with some of the existing libraries where time is actually used (e.g. forces in function objects) and managed to access the time in the library I'm trying to code.

"Simply" add the objectregistry in the constructors of your source file and to the class declaration in your header file and then access the time via "obr_.time().value()".

Let me know if you still have problems.

Hope this helps,

Rick
Ricky-11 is offline   Reply With Quote

Old   September 26, 2017, 10:48
Default Mooring line
  #14
New Member
 
Xiaokang Deng
Join Date: Sep 2017
Posts: 9
Rep Power: 9
Deng is on a distinguished road
Hi SailorLiu,

Do you want to add the mooring line in sixDoFRigidBodyMotion? Are you successful? I am studying how to add the mooring line in sixDoFRigidBodyMotion, but I am a newer. Can you share me some materials or examples. I think they will be very helpful to me.

Thank you in advance.

Xiaokang Deng
Deng is offline   Reply With Quote

Old   March 3, 2019, 04:17
Default
  #15
Member
 
Paul Palladium
Join Date: Jan 2016
Posts: 94
Rep Power: 10
Fauster is on a distinguished road
Quote:
Originally Posted by Ricky-11 View Post
Hey SailorLiu.

I've stumbled upon you post while looking for the same kind of info.

I've then played around a little bit with some of the existing libraries where time is actually used (e.g. forces in function objects) and managed to access the time in the library I'm trying to code.

"Simply" add the objectregistry in the constructors of your source file and to the class declaration in your header file and then access the time via "obr_.time().value()".

Let me know if you still have problems.

Hope this helps,

Rick

Dear Rick,
Could you give a little bit more detail about your solution ? Does it work within a restrain function ?My problem is the following :
I would like to use the function forces in a custom restraint :


Quote:
dictionary forcesDict;
forcesDict.add("type", functionObjects::forces::typeName);
forcesDict.add("patches", patches_);
forcesDict.add("rhoInf", rhoInf_);
forcesDict.add("rho", rhoName_);
forcesDict.add("CofR", vector::zero);

functionObjects::forces f("forces", db(), forcesDict);
f.calcForcesMoment();
For that I added #include "forces.H" in my .C file. Then I created a Make directory a linked my new restrain to the rigidBodyDynamic library wit appropriate option and files file.


The problem is I need to access to db() or mesh for the function forces.
alexeym says that is it not possible to access mesh because mesh is carried by the registry which is not accessible from the rigidBodyDynamic class (or sixDoFRigidBodyMotion).


Can you tell me how to add the objectregistry in the constructors of your source file ?


In my case :
Quote:
Foam::RBD::restraints::customRestraint::customRest raint
(
const word& name,
const dictionary& dict,
const rigidBodyModel& model
)
:

restraint(name, dict, model),
patches_(wordReList(dict.lookup("patches")))
{
read(dict);
}
alexeym explained that it's possible to add the registery as a parameter:

Quote:
for example you can get object registry as a parameter or something else
What is the way for doing such a thing ?

**********************

I have to mention that I tried to add :

Quote:
const objectRegistry & db () const;
in the .H file of my restraint below // Member Functions.
By this way the compilation works but when I tried to compile interDyMFoam (including the library created with my custom restraint) I get an error message :


/home/paul/OpenFOAM/paul-v1712/platforms/linux64GccDPInt32Opt/lib/libcustomRestraint.so : undefined reference to « Foam::RBD::restraints::customRestraint::db() const »
collect2: error: ld returned 1 exit status
/opt/OpenFOAM-v1712/wmake/makefiles/general:1

I know that there is a possibilty to extract forces from a txt.file (using IOstream) created by the solver by calling functionObjects::forces within each PIMPLE loop but I would like to find a more "elegant" way.

Any help will be amazing !

Last edited by Fauster; March 4, 2019 at 09:08. Reason: Add : IOstream possibilty
Fauster 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
Elastic or Plastic Deformations in OpenFOAM NickolasPl OpenFOAM Pre-Processing 36 September 23, 2023 09:22
Floating point exception error lpz_michele OpenFOAM Running, Solving & CFD 53 October 19, 2015 03:50
How to write k and epsilon before the abnormal end xiuying OpenFOAM Running, Solving & CFD 8 August 27, 2013 16:33
dynamic Mesh is faster than MRF???? sharonyue OpenFOAM Running, Solving & CFD 14 August 26, 2013 08:47
plot over time fferroni OpenFOAM Post-Processing 7 June 8, 2012 08:56


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