CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM (
-   -   User Defined Function in OF (

salvoblack February 16, 2011 10:16

User Defined Function in OF
I have to implement a UDF in OF that i have used in Fluent.
the problem, as you know, is that in OF there isn't an "interface" as in Fluent.
Can you help me?? I'm new with programming in OpenFoam

cheng1988sjtu February 16, 2011 10:37

UDF basically can't
Bsically, the UDF in fluent can't be used directly in OF.

You need to modify the source code to accomplish what you need. After modifying the code, you need to compile it.


salvoblack February 16, 2011 10:54


Originally Posted by cheng1988sjtu (Post 295531)
Bsically, the UDF in fluent can't be used directly in OF.

You need to modify the source code to accomplish what you need. After modifying the code, you need to compile it.


and after the compilation of my new udf, what i have to do?? how can i implement it in OF??
where may i put the file???

cheng1988sjtu February 16, 2011 11:00

you would like to put the new file inside a new folder in the opt/openfoam1.7.x/application/solvers/...

and when you compile the new file, in the Make folder you can define a keyword to call for it, for example you can refer to the incompressible/channelFoam/Make/files to learn how they do it.

To answer how to call it, in the work directiory, under 'system' folder, you can put the keyword in the right place to call for the application you defined.

Hope this helps!


sebonator February 16, 2011 11:58

Hi salvoblack,

OpenFOAM also provides special folders for self-programmed utilities.

For example $FOAM_USER_APPBIN is a place where you can put your own executable files. This is a standard search-path for OpenFOAM, what means that you can execute the programs you put in $FOAM_USER_APPBIN directly from the case-folders you work in.

In this way, your own executables can be used as the standard OpenFOAM executables, i.e. blockMesh, icoFoam etc. In fact, those utilities and solvers are nothing but executable files which lie in the $FOAM_APPBIN folder.

Hope this helps for the general understanding. If you want to do some special things during a simulation, my way would be to take the solver you want to use, copy its source-folders from $WM_PROJECT_DIR/solvers to the $FOAM_APP directory and edit the source codes on this copy. Of course, you have to change the name of the source-code etc. to distinguish your executable from the original one later on.

Thats the general way so far, at least my general way ;-)
Hope that helped,
greetings Sebastian

salvoblack February 16, 2011 13:36

Suppose I know the transition point, which I call x_tr.
which file should I change to require that
1) if x <x_tr then I can impose nut = 0;
2) if x> x_tr then I can say that nut! = 0??

sebonator February 16, 2011 14:51

Hi salvoblack,

mhm, I think you want it a bit too fast...

First question is: which solver do you want to use? You need the whole source code for the solver. All the solver-codes are located in folder $WM_PROJECT_DIR/solvers.

I did something similar some time ago:
I changed the boundary condition during simulation depending on a scalar value in a patch face. What I did was something like


  if (scalarField1.boundaryField[I] < 1)
      scalarField2.boundaryField[I] = 5

This code will go through all patch faces and set the patch value of scalarField2 to 5, if the value of scalarField1 is smaller than 1. Of course thats just an example, but I think thats something you want to do.

What you see in this example, too, is that OpenFOAM programming is based on object-orientation. scalarField1 and scalarField2 might be objects of the class volScalarField and this field's method boundaryField returns all the patch values related to this volScalarField.

Maybe you know that, as you already have experience with programming UDF's. But I think the UDF's of Fluent are written in C, aren't they? Well, OpenFOAM uses C++ which is related to C, but I don't think you will have a lot of fun with OpenFOAM-customizing if you are not aware of basic object-oriented concepts and the syntax of C++, of course.

If you need more explanations, could you then please tell something about your experiences in programming and object-orientation? That makes it easier to help you, because we could leave out the basic things, if you already know about object-orientation for example.

Feel free to ask again!
Greetings Sebastian

salvoblack February 17, 2011 04:55

Hi Sebastian,
thank you for the quick reply.
Unfortunately no, I'm not very familiar with programming in C + + and especially with the object-orientation. My UDF in Fluent is very simple and I think I will have many difficulties to implement it in OF.
However I wanted to ask, since I want to use as simpleFoam solver, could you tell me which file I have to go change to force the change of nuts depending on the value of x (with respect to the transition)??

sebonator February 17, 2011 09:53

Hi salvoblack,

first of all, I have to apologize, because I told you a wrong folder: The solver codes are located in $WM_PROJECT_DIR/applications/solvers and not in $WM_PROJECT_DIR/solvers as I said in my former posts. I think you know about the environmental variables, do you? All parts of paths I write which start with a $-sign a environmental variables. For example $HOME is the path of your home dictionary /home/username/.

Alright, I'll try to give you a step-by-step guide. All the codes I use have to be executed in the terminal:

First of all, create your folder for self-compiled applications with

Then copy the solver-codes of simpleFoam into that directory with

cp -r $WM_PROJECT_DIR/applications/solvers/incompressible/simpleFoam $FOAM_RUN/../applications
Now go to that directory in

cd $FOAM_RUN/../applications
Do the following before you change anything in the source-code:
-change the directory-name of your copied simpleFoam-sourcecodes to a name you want, for example MODsimpleFoam for modified simpleFoam
-go into the renamed directory and do

-rename the source-code-file simpleFoam.C and give it the same name you used for the folder two steps before. This file is the major-file of simpleFoam! As solvers are procedural algorithms by nature, you can read through this file easily with basic knowledge of procedural programming, but don't mind that by now.
-go into the Make subfolder
-edit the textfile named 'files' and change the first word (it should be 'simpleFoam.C') to the name you decided for the solver (for example 'MODsimpleFoam.C')
-two lines under that in the 'files'-textfile, you find this:
EXE = $(FOAM_APPBIN)/simpleFoam
-edit that and change it to
this line says how your executable will be named after compilation and where it will be located. Here you see:the original simpleFoam is a standard-solver of OpenFOAM and therefore is stored in $FOAM_APPBIN. The customized solver will be your own, so don't store it inside the standard-folders, because you will get messed up later on if you don't seperate customized and standard-utilities. To avoid this confusion, your solver will be located in $FOAM_USER_APPBIN, from which it can be started from every directory you want, because it belongs to the standard-searchpaths of OpenFOAM.

So, now your ready to edit the major-sourcecode MODsimpleFoam.C:
You should try to understand the solving-procedure first, before you change anything. Your modifications should be placed in this file, but I am sorry that I can't tell you in detail what you have to change by now.

After you did your modifications, you can compile the solver with

from within your solver-directory (the one in which the MODsimpleFoam.C lies).

I hope you are able to create your customized solver by now. If I can help you again, just ask here, but I think you will at least have to learn some basic terminology of object-orientation, because almost the whole solver is written with constructors and methods of classes. You should at least invest some time and learn the meaning of these elements of object-orientation. I don't think you will be successful in creating a modified solver if you don't know the basics, because everything in OpenFOAM is based on this stuff.

Good luck!
Greetings Sebastian

salvoblack February 17, 2011 10:04

thank you very much for your help, you were very kind!!!
now i'll try to study something about the programming of c++.
thanks a lot!!

salvoblack March 6, 2011 05:50

Hello everyone. I urgently need help.
I'm trying, as mentioned in previous posts, to amend the komega SST turbulence model. I want to do is to impose the transition point so as to put nut = 0 when the flow is laminar.
I should change this part of the program
// Re-calculate viscosity
nut_ = a1_*k_/max(a1_*omega_, F2()*sqrt(S2));
how can I change the program to change the x coordinate, in order to fix the transition point?

salvoblack March 7, 2011 07:28

any suggestions???

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