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

Reading rigidBody motion state from custom library

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   June 22, 2022, 18:56
Default Reading rigidBody motion state from custom library
  #1
New Member
 
Pere
Join Date: Mar 2022
Location: Mallorca
Posts: 16
Rep Power: 4
katiuskas is on a distinguished road
Dear foamers,

I am currently working on a custom type of source term (via a new library), which the user can specify through fvOptions. The idea is that the applied force field depends on the position and velocity of a rigid body, which is defined in dynamicMeshDict using the sixDoFRigidBody library:

Code:
...
dynamicFvMesh dynamicMotionSolverFvMesh;
motionSolverLibs 
(
  "libsixDoFRigidBodyMotion.so"
);
solver sixDoFRigidBodyMotion;
diffusivity     quadratic inverseDistance ( cylinder );
 
 sixDoFRigidBodyMotionCoeffs
 {
...
 }
My main concern is how to access the solid body motion state (at each time-step) from the library I am currently implementing. I know the state of the rigid body is stored by the classes sixDoFRigidBodyMotionState and sixDoFRigidBodyState, but I have no idea how to access the motion object and its properties.

I have seen a similar issue tackled in a 2015 post, but unfortunately, I cannot understand it fully. I think they propose modifying the original sixDoF library to export a motion dictionary, which can then be accessed from their custom program.

I think there has to be a well-defined manner to access and pass object information between different libraries, but I am very new to programming in OpenFOAM and such things still escape my understanding. Thus, I would really appreciate any guidance in this regard.

Thank you very much!

Last edited by katiuskas; June 23, 2022 at 05:53.
katiuskas is offline   Reply With Quote

Old   June 24, 2022, 08:07
Default Found a solution :)
  #2
New Member
 
Pere
Join Date: Mar 2022
Location: Mallorca
Posts: 16
Rep Power: 4
katiuskas is on a distinguished road
I finally went through the 2015 post and adapted the proposal from yuhan1991 (reply #5). Just in case anyone is trying to do something similar, this is what I did:
  • Create a copy of the rigid body library so that I can modify it without affecting the original one. Just called it mySixDoFRigidBodyMotion.
  • Modify the sixDoFRigidBodyMotion.C file so that every time the rigid body equations are solved, an IOdictonary (which I called "sixDoFMotion") is updated with its information.

    For this purpose, two new functions must be defined: one for creating the IOdictionary (to be called only once at the class constructor) and a second one for updating it with the rigid body motion information.

Code:
void Foam::sixDoFRigidBodyMotion::createDict()
{
    // Create dictionary if it has not been created before
    if(!time_.foundObject<IOdictionary>("sixDoFMotion"))
    {
        dictionary motionDict;
        time_.store
        (   
            new IOdictionary
            (   
                IOobject
                (   
                    "sixDoFMotion",
                    time_.timeName(),
                    time_,
                    IOobject::NO_READ,
                    IOobject::AUTO_WRITE
                ),
                motionDict
            )
        );

        Info << "Rigid body IOdictionary 'sixDoFMotion' created" << endl;

        updateDict();

    }
}
Code:
void Foam::sixDoFRigidBodyMotion::updateDict()
{
    if(time_.foundObject<IOdictionary>("sixDoFMotion"))
    {
        // Open and write
        const dictionary& motionDict = 
            time_.lookupObject<IOdictionary>("sixDoFMotion");

        // Update for the motion solver
        dictionary updateDbDictionary = &motionDict;
        updateDbDictionary.set("centreOfRotation", centreOfRotation());
        updateDbDictionary.set("orientation", orientation());
        updateDbDictionary.set("velocity", v());
        updateDbDictionary.set("omega", omega());

        // Needed to update the IOdictionary
        const_cast<dictionary& > (motionDict) = updateDbDictionary;
        
        Info << "Updating 'sixDoFMotion' IOdictionary" << endl;
       
    }
}
Here the const_cast is very important as it allows us overwrite the IOdictionary.

The createDict() function is called only once inside the constructor, whereas updateDict() is called inside sixDoFRigidBodyMotion::update():
Code:
...
    if (Pstream::master())
    {
        solver_->solve(firstIter, fGlobal, tauGlobal, deltaT, deltaT0);

        if (report_)
        {
            status();
        }

        updateDict();
    }
  • Finally, in the custom library, I access the newly created dictionary:
Code:
void Foam::fv::myLib::readRigidBodyDict(const fvMesh& mesh)
{
    // If rigid body dictionary exists
    if(mesh.time().foundObject<IOdictionary>("sixDoFMotion"))
    {
        // Access dictionary
        const dictionary& motionDict = mesh.time().lookupObject<IOdictionary>("sixDoFMotion");
        // Save its reference
        rigidBodyDict_ = motionDict;
    }
    else
    {
        Info << "Rigid body IOdictionary ('sixDoFMotion') could not be accessed." << endl;
    }
}
The data contained in the dictionary can then be accessed via the usual lookup action, eg. "rigidBodyDict_.lookup("centreOfRotation") >> rotCenter_".
Note that in sixDoFRigidBodyMotion.C, I had access to the runtime variable time_ whereas in my custom library I have access to the fvMesh object mesh. Thus, the syntax to access the dictionary in both cases might be slightly different.

This is, as of now, the only way I know to transfer data between classes. I am very open to any suggestions! I hope this can help someone in a similar situation.
katiuskas is offline   Reply With Quote

Old   November 21, 2023, 06:49
Default sharing .C
  #3
New Member
 
Ivan
Join Date: Dec 2021
Posts: 6
Rep Power: 5
isulovsky is on a distinguished road
Dear Katiuskas,

Thank you so much for posting the solution as I am dealing with something similar. Are you willing to share the .C file of the sixDoFRigidBodyMotion ?

I am also trying to inherit the rigid body state in my custom library and I think I have declared something wrong in the sixDoF file.

Kind regards,

Ivan
isulovsky is offline   Reply With Quote

Reply

Tags
dynamic mesh, fvoptions, rigid body, sixdof

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
transform in the new motion library rigidBodyMeshMotion jiadongw OpenFOAM Running, Solving & CFD 18 November 15, 2024 12:26
How to access to the solid body motion state from a custom interDyMFoam.C ? jmf OpenFOAM Programming & Development 9 January 29, 2022 20:31
[Commercial meshers] Problem converting fluent mesh vinz OpenFOAM Meshing & Mesh Conversion 28 October 12, 2015 07:37
Compression stoke is giving higher pressure than calculated nickjuana CFX 62 May 19, 2015 14:32
Constant velocity of the material Sas CFX 15 July 13, 2010 09:56


All times are GMT -4. The time now is 05:39.