|
[Sponsors] |
May 11, 2016, 11:58 |
Create a custom function object
|
#1 |
Senior Member
Thomas Oliveira
Join Date: Apr 2015
Posts: 114
Rep Power: 12 |
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 #{ ... #}; } Best regards, Thomas Oliveira |
|
May 13, 2016, 08:38 |
|
#2 |
Senior Member
Kevin van As
Join Date: Sep 2014
Location: TU Delft, The Netherlands
Posts: 252
Rep Power: 21 |
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. |
|
May 13, 2016, 14:19 |
|
#3 |
Senior Member
Mahdi Hosseinali
Join Date: Apr 2009
Location: NB, Canada
Posts: 273
Rep Power: 18 |
Are you sure it was part of the OF 2.1? Sounds like a script that comes with the book
|
|
May 19, 2016, 15:17 |
|
#4 |
Senior Member
Thomas Oliveira
Join Date: Apr 2015
Posts: 114
Rep Power: 12 |
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 |
|
May 19, 2016, 15:18 |
|
#5 |
Senior Member
Thomas Oliveira
Join Date: Apr 2015
Posts: 114
Rep Power: 12 |
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.
|
|
May 20, 2016, 04:33 |
|
#6 | |
Senior Member
Kevin van As
Join Date: Sep 2014
Location: TU Delft, The Netherlands
Posts: 252
Rep Power: 21 |
Quote:
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(); } 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. |
||
May 20, 2016, 14:48 |
|
#7 |
Senior Member
Thomas Oliveira
Join Date: Apr 2015
Posts: 114
Rep Power: 12 |
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 |
|
May 23, 2016, 04:12 |
|
#8 | |
Senior Member
Kevin van As
Join Date: Sep 2014
Location: TU Delft, The Netherlands
Posts: 252
Rep Power: 21 |
Quote:
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. 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. |
||
May 26, 2016, 15:41 |
|
#9 | |
New Member
Ricardo
Join Date: Sep 2011
Posts: 15
Rep Power: 15 |
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++) Code:
while(runTime.loop) Quote:
|
||
May 27, 2016, 03:56 |
|
#10 | |
Senior Member
Kevin van As
Join Date: Sep 2014
Location: TU Delft, The Netherlands
Posts: 252
Rep Power: 21 |
Quote:
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. |
||
May 29, 2016, 12:03 |
|
#11 |
Senior Member
Thomas Oliveira
Join Date: Apr 2015
Posts: 114
Rep Power: 12 |
Dear Kevin,
Problem solved! Thank you. Best regards, Thomas |
|
October 11, 2016, 04:53 |
|
#12 |
New Member
Ovid
Join Date: Oct 2016
Location: Spain
Posts: 28
Rep Power: 10 |
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. |
|
January 27, 2017, 19:59 |
|
#13 | |
Senior Member
Yuehan
Join Date: Nov 2012
Posts: 142
Rep Power: 14 |
Hi,
I am also converting the wallHeatFlux utility into a functionObject. Do you find any solution? Thank you. Quote:
|
||
June 24, 2022, 06:41 |
|
#14 | |
Member
sadra mahmoudi
Join Date: Feb 2021
Location: Austria
Posts: 39
Rep Power: 5 |
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:
|
||
Tags |
function object |
|
|
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 |