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/)
-   -   Obtaining forces/moments during runtime (https://www.cfd-online.com/Forums/openfoam-programming-development/240907-obtaining-forces-moments-during-runtime.html)

adrian.delser January 31, 2022 08:22

Obtaining forces/moments during runtime
 
Hi,


Is there a way to obtain forces and moments on an object in a simulation directly in the solver (eg myCustomSolver.C). I know I can generate them using ForceCoeffs and output them in a .dat file or a log file, but I would like to obtain the most recent moment/force as a variable in myCustomSolver.C during runtime directly (instead of trying to read it from the log/dat files, which might be slower. Is this possible?


Thanks.

Tobi February 1, 2022 17:19

The latest developments of OpenFOAM v2112 has to feature to sent the data of any FO to other FO. Hence, it should be stored in the db() and hence, you should be able to access it in your own solver directly without reading it from any file.

adrian.delser February 2, 2022 17:12

Quote:

Originally Posted by Tobi (Post 821379)
The latest developments of OpenFOAM v2112 has to feature to sent the data of any FO to other FO. Hence, it should be stored in the db() and hence, you should be able to access it in your own solver directly without reading it from any file.




Hi Tobi,


This is good to know! I am still relatively new to openfoam, how would one practically access this value? I am unfamiliar with db() (for now). Say I wish to access the moment coefficient of the patch "obstacle" along the axis (0 0 1) as one might define in this example forceCoeffs file:


Code:

forceCoeffs1

{
    type forceCoeffs;
    libs ("libforces.so");
   
    writeControl timeStep;
    timeInterval 20;
   
    log yes;
   
    patches    (obstacle);
    rho rhoInf;    //Indicates Incompressible flow
    rhoInf;    //irrelevent for inc flow
    liftDir      (0 1 0);
    dragDir      (1 0 0);
    CofR          (0 0 0);
    pitchAxis    (0 0 1);
    magUInf    7.5e-4;
    lRef        4; //diameter
    Aref        8; //projected area
}

Best,


Adrian

Tobi February 3, 2022 17:31

https://www.openfoam.com/documentati...ce.html#l00234

Here we have the setResults() functionality which stores (need to check further) the data in the database. So what we might need is:
Code:

const scalar cd = this->db().lookupObject<scalar>("Cd");
Never tested it and no idea if it works :)
However, if that one is not stored in the db() you should get an error and a list of objects which are stored there :). Probably, we first need to access the FO itself and then we have access to the values.

Tobi February 4, 2022 04:30

I got the hint from the developers, that the results are stored in the functionObjectProperties which is owned by Time. So you should have access in that way.

adrian.delser February 7, 2022 09:45

Quote:

Originally Posted by Tobi (Post 821581)
I got the hint from the developers, that the results are stored in the functionObjectProperties which is owned by Time. So you should have access in that way.




Excellent, thank you, I will be testing this shortly. I need to have boundary conditions that depend on certain runtime field quantities (the moment potentially being one of them). This would be very useful for runtime control systems (such as simulating a 2d "rocket" landing and trying to keep itself upright with either small jets or fins like the SpaceX Falcon 9, but in OpenFOAM. codedFixedValue boundary conditions come to mind but I'll have to see how they work.

quarkz April 7, 2022 02:34

Hi everyone,

I also need to obtain the forces to modify my custom inlet boundary condition using codedFixedValue. Currently, I need to filter the force output file, and then read in to get the value. It works but it's slow and ugly!

I tried to use the mtd suggested by Tobi:

Code:

const scalar cd = this->db().lookupObject<scalar>("Cd");
But I got this error during compile:

Code:

Creating new library in "dynamicCode/u_6dof_change/platforms/linux64GccDPInt32Opt/lib/libu_6dof_change_a2f851b6065ae560f5bda633cf2c3064fa5c7783.so"
Invoking wmake libso /home/user/Woodstock_OF_tmp/3D/WS_full_S0R45_PV_5e-4_dihedral_overset_sym_tb_0.9M_6dof_x/dynamicCode/u_6dof_change
wmake libso /home/user/Woodstock_OF_tmp/3D/WS_full_S0R45_PV_5e-4_dihedral_overset_sym_tb_0.9M_6dof_x/dynamicCode/u_6dof_change
    ln: ./lnInclude
    dep: fixedValueFvPatchFieldTemplate.C
    Ctoo: fixedValueFvPatchFieldTemplate.C
In file included from /usr/lib/openfoam/openfoam2112/src/OpenFOAM/lnInclude/objectRegistry.H:571,
                from /usr/lib/openfoam/openfoam2112/src/OpenFOAM/lnInclude/polyMesh.H:47,
                from /usr/lib/openfoam/openfoam2112/src/finiteVolume/lnInclude/fvMesh.H:53,
                from /usr/lib/openfoam/openfoam2112/src/finiteVolume/lnInclude/fvPatchField.C:31,
                from /usr/lib/openfoam/openfoam2112/src/finiteVolume/lnInclude/fvPatchField.H:608,
                from /usr/lib/openfoam/openfoam2112/src/finiteVolume/lnInclude/fixedValueFvPatchField.H:59,
                from /usr/lib/openfoam/openfoam2112/src/finiteVolume/lnInclude/fixedValueFvPatchFields.H:31,
                from fixedValueFvPatchFieldTemplate.H:40,
                from fixedValueFvPatchFieldTemplate.C:29:
/usr/lib/openfoam/openfoam2112/src/OpenFOAM/lnInclude/objectRegistryTemplates.C: In instantiation of ‘const Type& Foam::objectRegistry::lookupObject(const Foam::word&, bool) const [with Type = double]’:
/home/user/Woodstock_OF_tmp/3D/WS_full_S0R45_PV_5e-4_dihedral_overset_sym_tb_0.9M_6dof_x/0/U.boundaryField.inlet:182:53:  required from here
/usr/lib/openfoam/openfoam2112/src/OpenFOAM/lnInclude/objectRegistryTemplates.C:443:27: error: cannot dynamic_cast ‘iter.Foam::HashTable<Foam::regIOobject*>::const_iterator::operator()()’ (of type ‘class Foam::regIOobject* const’) to type ‘const double*’ (target is not pointer or reference to class)
  443 |        const Type* ptr = dynamic_cast<const Type*>(iter());
      |                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/openfoam/openfoam2112/src/OpenFOAM/lnInclude/objectRegistryTemplates.C:454:37: error: ‘typeName’ is not a member of ‘double’
  454 |            << ")\n    expected a " << Type::typeName
/usr/lib/openfoam/openfoam2112/src/OpenFOAM/lnInclude/objectRegistryTemplates.C:467:48: error: ‘typeName’ is not a member of ‘double’
  467 |        << ")\n    available objects of type " << Type::typeName
make: *** [/usr/lib/openfoam/openfoam2112/wmake/rules/General/transform:35: Make/linux64GccDPInt32Opt/fixedValueFvPatchFieldTemplate.o] Error 1


--> FOAM FATAL IO ERROR: (openfoam-2112)
Failed wmake "dynamicCode/u_6dof_change/platforms/linux64GccDPInt32Opt/lib/libu_6dof_change_a2f851b6065ae560f5bda633cf2c3064fa5c7783.so"


file: 0/U.boundaryField.inlet at line 64 to 250.

    From void Foam::codedBase::createLibrary(Foam::dynamicCode&, const Foam::dynamicCodeContext&) const
    in file db/dynamicLibrary/codedBase/codedBase.C at line 240.

FOAM exiting

Can someone help? Or is there anywhere I can look to get some info? I don't know where I should search to get related info.

PS: Btw, I can access the deltaT using :

Code:

delta_time = this->db().time().deltaTValue();

quarkz April 15, 2022 21:00

Hi,

I realised that I can get the force thru:

Code:

const vector force = this->db().lookupObject<vector>("force");
Compiling my custom BC using the above gave no error, which is good. :)

However, when I run my case and I tried to print the force value:

Code:

Info << "drag from force " << force[0] << endl;
I got the error:

Code:

FOAM parallel run exiting
[4]
[13]
[13] --> FOAM FATAL ERROR: (openfoam-2112)
[13]
    failed lookup of force (objectRegistry region0)
    available objects of type vector:
0()
[13]
[13]
[13]    From const Type& Foam::objectRegistry::lookupObject(const Foam::word&, bool) const [with Type = Foam::Vector<double>]
[13]    in file /usr/lib/openfoam/openfoam2112/src/OpenFOAM/lnInclude/objectRegistryTemplates.C at line 463.

Anyone knows how to debug this error?

Thanks.


All times are GMT -4. The time now is 13:08.