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

Create a custom function object

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 1 Post By t.oliveira
  • 1 Post By floquation

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   May 11, 2016, 11:58
Default Create a custom function object
  #1
Senior Member
 
Thomas Oliveira
Join Date: Apr 2015
Posts: 114
Rep Power: 12
t.oliveira is on a distinguished road
Dear all,

The excellent book "The OpenFOAM Technology Primer", by Tomislav Maric, Jens Höpken, Kyle Mooney, was developed using OpenFOAM 2.1.1. In this book, they write that to create a custom function object library one can use the script newFunctionObject.

I cannot find this script in OpenFOAM 2.3.x. I guess that it doesn't exist any more, and that the steps for creating a custom function object changed since OF 2.1.1. On the forum, I see some discussions about "coded function object" that include the code:

Code:
newFunctionObjectName
{
    functionObjectLibs ("libutilityFunctionObjects.so");
    type coded;
    ....
    code
    #{
    ...
    #};
}
Is this the new standard way to write a custom function object, which has superseded the workflow that involved the usage of the newFunctionObject script?

Best regards,
Thomas Oliveira
t.oliveira is offline   Reply With Quote

Old   May 13, 2016, 08:38
Default
  #2
Senior Member
 
floquation's Avatar
 
Kevin van As
Join Date: Sep 2014
Location: TU Delft, The Netherlands
Posts: 252
Rep Power: 21
floquation will become famous soon enough
I do not know the "newFunctionObject" script, but I can tell you the following:

The coded function object allows you to create a function object at case-level. Whether this is desirable depends on what you are trying to achieve.
If it is a function object with functionality specific to your case, then this is most likely the best way to do it.
If you are trying to achieve something more general, then it would be better to write a stand-alone function object.

To do the latter, I'd simply take an existing function object, copy it, and take it from there. You can compile your custom function object(s) into a library and include that library in your controlDict. Similar to the way "swak4Foam" does it.
floquation is offline   Reply With Quote

Old   May 13, 2016, 14:19
Default
  #3
Senior Member
 
Mahdi Hosseinali
Join Date: Apr 2009
Location: NB, Canada
Posts: 273
Rep Power: 18
anishtain4 is on a distinguished road
Are you sure it was part of the OF 2.1? Sounds like a script that comes with the book
anishtain4 is offline   Reply With Quote

Old   May 19, 2016, 15:17
Default
  #4
Senior Member
 
Thomas Oliveira
Join Date: Apr 2015
Posts: 114
Rep Power: 12
t.oliveira is on a distinguished road
Kevin,

Thank you for the orientation about when to choose between a coded function object or a stand-alone function object.

I noticed that the coded function object (and I suppose that also a stand-alone function object) is executed after Foam::Time::write is called. I don't know if this could be changed. Since I needed to execute the code of my function before Foam::Time::write, I chose to write the code directly inside the application and recompile it.

Best wishes,
Thomas
t.oliveira is offline   Reply With Quote

Old   May 19, 2016, 15:18
Default
  #5
Senior Member
 
Thomas Oliveira
Join Date: Apr 2015
Posts: 114
Rep Power: 12
t.oliveira is on a distinguished road
Mahdi, thank you for the hint. You are right, and the book mentions it in the beginning of the section. The newFunctionObject script comes with the book and is available on https://bitbucket.org/sourceflux/primer-code/src/ , at src/scripts.
kk2017 likes this.
t.oliveira is offline   Reply With Quote

Old   May 20, 2016, 04:33
Default
  #6
Senior Member
 
floquation's Avatar
 
Kevin van As
Join Date: Sep 2014
Location: TU Delft, The Netherlands
Posts: 252
Rep Power: 21
floquation will become famous soon enough
Quote:
Originally Posted by t.oliveira View Post
I noticed that the coded function object (and I suppose that also a stand-alone function object) is executed after Foam::Time::write is called.
I don't think that is true in general. We probably have to consider the lifecycle of a function object:
functionObjects_.start(); is called during the very first timestep
functionObjects_.end(); is called during the very final timestep
functionObjects_.execute(); is called in all other situations

Now where are function objects being called? All solvers look as follows:
Code:
while (runTime.run())
{
    doStuff();
    runTime.write();
}
Function objects are being called by "runTime.run()", which occurs before runTime.write().

So it is seen that function objects are called before writing occurs. Comparing this with the lifecycle of a function object, however, we should note that "start()" is called the first time. This means that "execute()" is only called after one runTime.write() was called.

If you need to call execute() before write(), the only way is to have start() call execute().
This is easy in the case of a normal function object: all you need to do is add one line in its start()-method.

In the case of a coded function object, I have no clue. Looking at the relevant source code:
$FOAM_SRC/OpenFOAM/db/dictionary/functionEntries/codeStream
$FOAM_SRC/postProcessing/functionObjects/utilities/codedFunctionObject
I would quickly conclude that you can't (although I don't know!).
One work-around could perhaps be the "executeIfStartTime" function object, which I think is part of "swak4foam". I'm not sure if that works, but you can look into it.

I hope that can get you going.
floquation is offline   Reply With Quote

Old   May 20, 2016, 14:48
Default
  #7
Senior Member
 
Thomas Oliveira
Join Date: Apr 2015
Posts: 114
Rep Power: 12
t.oliveira is on a distinguished road
Dear Kevin,

Thank you for the detailed explanation, which will be very helpful in my future uses of function objects.

For this case in specific, I wanted to call the function object between doStuff() and runtime.write(), that is, after the ODE is solved but before results are written. I couldn't find a workaround for that. Do you think it would be possible?

Best wishes,
Thomas
t.oliveira is offline   Reply With Quote

Old   May 23, 2016, 04:12
Default
  #8
Senior Member
 
floquation's Avatar
 
Kevin van As
Join Date: Sep 2014
Location: TU Delft, The Netherlands
Posts: 252
Rep Power: 21
floquation will become famous soon enough
Quote:
Originally Posted by t.oliveira View Post
For this case in specific, I wanted to call the function object between doStuff() and runtime.write(), that is, after the ODE is solved but before results are written.
Is it possible? Most certainly!
Can it be done without modifying the solver? Nope... You'll need to edit the solver and add a line like:
Code:
runTime.functionObjects().execute(forceWrite);
//where "forceWrite" is a boolean.
Which will execute all functionObjects at that given moment in the code.
Or to execute a single functionObject by name:
Code:
int i = runTime.functionObjects().findObjectID("name");
runTime.functionObjects()[i].execute(forceWrite);
//where "forceWrite" is a boolean.

Tip: I found the correct methods to call using the IDE "Eclipse" within a few minutes.
t.oliveira likes this.
floquation is offline   Reply With Quote

Old   May 26, 2016, 15:41
Default
  #9
New Member
 
Ricardo
Join Date: Sep 2011
Posts: 15
Rep Power: 15
rvmedina20 is on a distinguished road
Hi Kevin, though I am not having trouble anymore I am curious to know one thing (hopefully you can help clarify):

I have a user-defined solver based on the simple solver. It was initially constructed with
Code:
for(runTime++;!runTime.end();runTime++)
which produced an empty probe file (using the probe function object). However, changing to
Code:
 while(runTime.loop)
works. I am curious as to why the time definition affects the functionObject output?





Quote:
Originally Posted by floquation View Post
Is it possible? Most certainly!
Can it be done without modifying the solver? Nope... You'll need to edit the solver and add a line like:
Code:
runTime.functionObjects().execute(forceWrite);
//where "forceWrite" is a boolean.
Which will execute all functionObjects at that given moment in the code.
Or to execute a single functionObject by name:
Code:
int i = runTime.functionObjects().findObjectID("name");
runTime.functionObjects()[i].execute(forceWrite);
//where "forceWrite" is a boolean.

Tip: I found the correct methods to call using the IDE "Eclipse" within a few minutes.
rvmedina20 is offline   Reply With Quote

Old   May 27, 2016, 03:56
Default
  #10
Senior Member
 
floquation's Avatar
 
Kevin van As
Join Date: Sep 2014
Location: TU Delft, The Netherlands
Posts: 252
Rep Power: 21
floquation will become famous soon enough
Quote:
Originally Posted by rvmedina20 View Post
I have a user-defined solver based on the simple solver. It was initially constructed with
Code:
for(runTime++;!runTime.end();runTime++)
which produced an empty probe file (using the probe function object). However, changing to
Code:
 while(runTime.loop)
works.
If you have a look at the file $FOAM_SRC/OpenFOAM/db/Time/Time.C (I used OF23x):
It has three relevant functions:
run(), loop() and operator++.
Looking at the implementations, it can be seen that loop() calls both run() and operator++. run() and operator++ do not call any of those three.

Now, the calling of function objects is taken care of by run(). Hence if run() is not called in the sequence, function objects will not be executed.

In your first implementation, operator++ is called. This does not call run(). Hence no function objects for you.
In your second implementation, loop() is called, which calls both run() and operator++. Hence function objects are executed.
floquation is offline   Reply With Quote

Old   May 29, 2016, 12:03
Default
  #11
Senior Member
 
Thomas Oliveira
Join Date: Apr 2015
Posts: 114
Rep Power: 12
t.oliveira is on a distinguished road
Dear Kevin,

Problem solved! Thank you.

Best regards,
Thomas
t.oliveira is offline   Reply With Quote

Old   October 11, 2016, 04:53
Default
  #12
New Member
 
Ovid
Join Date: Oct 2016
Location: Spain
Posts: 28
Rep Power: 10
Fole is on a distinguished road
Hi, I am using OpenFOAM v4.

I have found "OPENFOAM_Version4_INSTALL_DIR/bin/foamNewFunctionObject". I think it does something similar to that script, but I am now inspecting the code.

My reason for implementing a custom OpenFOAM function object is that I want to calculate the heat flux in a patch during run time (instead of saving lots of time directories and use wallHeatFlux at post-processing time).

If you find a tutorial about doing a function object, I would appreciate if you share it (if possible).

Cheers.
Fole is offline   Reply With Quote

Old   January 27, 2017, 19:59
Default
  #13
Senior Member
 
Yuehan
Join Date: Nov 2012
Posts: 142
Rep Power: 14
wc34071209 is on a distinguished road
Hi,

I am also converting the wallHeatFlux utility into a functionObject. Do you find any solution?

Thank you.




Quote:
Originally Posted by Fole View Post
Hi, I am using OpenFOAM v4.

I have found "OPENFOAM_Version4_INSTALL_DIR/bin/foamNewFunctionObject". I think it does something similar to that script, but I am now inspecting the code.

My reason for implementing a custom OpenFOAM function object is that I want to calculate the heat flux in a patch during run time (instead of saving lots of time directories and use wallHeatFlux at post-processing time).

If you find a tutorial about doing a function object, I would appreciate if you share it (if possible).

Cheers.
wc34071209 is offline   Reply With Quote

Old   June 24, 2022, 06:41
Smile
  #14
Member
 
sadra mahmoudi
Join Date: Feb 2021
Location: Austria
Posts: 39
Rep Power: 5
sadra2003 is on a distinguished road
Dear Dear Kevin,

I have a small problem and I would be appreciative if you share with me your opinion.
I am trying to simulate single bubble in a solution of water and sugar. In order to solve concentration (sugar) in just one phase, I used "phaseScalarTransport" which is a function object (which is called in the controlDict fil, i the case). When someone use phaseScalarTransport, the final result (distribution of concentration in the primary phase) will be printed in a variable called "alphaS.water" in the paraview. In my base solver (interFoam), I would like to make the density dependant on the concentration (alphaS.water). In fact, I would need something like:

rho=alpha2*mu2 + alpha1*(0.0008*exp(0.0486*alphaS.water))

in which alpha2 is air and alpha1 is water.
Now, the problem is, in spite of defining alphaS.water in the creatFiled of solver, the solver can not be compiled because it does not recongnise alphaS.water.
I would be thankful if anybody give me a hand.

Best regards,
Sadra



Quote:
Originally Posted by floquation View Post
If you have a look at the file $FOAM_SRC/OpenFOAM/db/Time/Time.C (I used OF23x):
It has three relevant functions:
run(), loop() and operator++.
Looking at the implementations, it can be seen that loop() calls both run() and operator++. run() and operator++ do not call any of those three.

Now, the calling of function objects is taken care of by run(). Hence if run() is not called in the sequence, function objects will not be executed.

In your first implementation, operator++ is called. This does not call run(). Hence no function objects for you.
In your second implementation, loop() is called, which calls both run() and operator++. Hence function objects are executed.
sadra2003 is offline   Reply With Quote

Reply

Tags
function object


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
[Other] refineWallLayer Error Yuby OpenFOAM Meshing & Mesh Conversion 2 November 11, 2021 12:04
[blockMesh] Errors during blockMesh meshing Madeleine P. Vincent OpenFOAM Meshing & Mesh Conversion 51 May 30, 2016 11:51
[snappyHexMesh] How to define to right point for locationInMesh Mirage12 OpenFOAM Meshing & Mesh Conversion 7 March 13, 2016 15:07
Running UDF with Supercomputer roi247 FLUENT 4 October 15, 2015 14:41
Problem with compile the setParabolicInlet ivanyao OpenFOAM Running, Solving & CFD 6 September 5, 2008 21:50


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