CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > OpenFOAM

User Defined Function in OF

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

Reply
 
LinkBack Thread Tools Display Modes
Old   February 16, 2011, 10:16
Default User Defined Function in OF
  #1
Member
 
Join Date: Oct 2010
Location: Naples
Posts: 50
Rep Power: 6
salvoblack is on a distinguished road
Hi!!
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
salvoblack is offline   Reply With Quote

Old   February 16, 2011, 10:37
Default UDF basically can't
  #2
Member
 
Charlie
Join Date: Dec 2010
Location: 415 Kinross Dr. Newark, DE 19711
Posts: 78
Rep Power: 6
cheng1988sjtu is on a distinguished road
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.

Zhen
cheng1988sjtu is offline   Reply With Quote

Old   February 16, 2011, 10:54
Default
  #3
Member
 
Join Date: Oct 2010
Location: Naples
Posts: 50
Rep Power: 6
salvoblack is on a distinguished road
Quote:
Originally Posted by cheng1988sjtu View Post
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.

Zhen
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???
salvoblack is offline   Reply With Quote

Old   February 16, 2011, 11:00
Default
  #4
Member
 
Charlie
Join Date: Dec 2010
Location: 415 Kinross Dr. Newark, DE 19711
Posts: 78
Rep Power: 6
cheng1988sjtu is on a distinguished road
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!

Zhen
cheng1988sjtu is offline   Reply With Quote

Old   February 16, 2011, 11:58
Default
  #5
Member
 
Sebastian Lang
Join Date: Aug 2009
Posts: 46
Rep Power: 8
sebonator is on a distinguished road
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
sebonator is offline   Reply With Quote

Old   February 16, 2011, 13:36
Default
  #6
Member
 
Join Date: Oct 2010
Location: Naples
Posts: 50
Rep Power: 6
salvoblack is on a distinguished road
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??
salvoblack is offline   Reply With Quote

Old   February 16, 2011, 14:51
Default
  #7
Member
 
Sebastian Lang
Join Date: Aug 2009
Posts: 46
Rep Power: 8
sebonator is on a distinguished road
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

Code:
forAll(scalarField1.boundaryField,I)
   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
sebonator is offline   Reply With Quote

Old   February 17, 2011, 04:55
Default
  #8
Member
 
Join Date: Oct 2010
Location: Naples
Posts: 50
Rep Power: 6
salvoblack is on a distinguished road
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)??
salvoblack is offline   Reply With Quote

Old   February 17, 2011, 09:53
Default
  #9
Member
 
Sebastian Lang
Join Date: Aug 2009
Posts: 46
Rep Power: 8
sebonator is on a distinguished road
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
Code:
mkdir -p $FOAM_USER_APPBIN
Then copy the solver-codes of simpleFoam into that directory with
Code:
cp -r $WM_PROJECT_DIR/applications/solvers/incompressible/simpleFoam $FOAM_RUN/../applications
Now go to that directory in
Code:
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
Code:
wclean
-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
EXE = $(FOAM_USER_APPBIN)/MODsimpleFoam
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
Code:
wmake
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
sebonator is offline   Reply With Quote

Old   February 17, 2011, 10:04
Default
  #10
Member
 
Join Date: Oct 2010
Location: Naples
Posts: 50
Rep Power: 6
salvoblack is on a distinguished road
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 is offline   Reply With Quote

Old   March 6, 2011, 05:50
Default
  #11
Member
 
Join Date: Oct 2010
Location: Naples
Posts: 50
Rep Power: 6
salvoblack is on a distinguished road
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));
nut_.correctBoundaryConditions();
how can I change the program to change the x coordinate, in order to fix the transition point?
salvoblack is offline   Reply With Quote

Old   March 7, 2011, 07:28
Default
  #12
Member
 
Join Date: Oct 2010
Location: Naples
Posts: 50
Rep Power: 6
salvoblack is on a distinguished road
any suggestions???
salvoblack is offline   Reply With Quote

Reply

Thread Tools
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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ParaView for OF-1.6-ext Chrisi1984 OpenFOAM Installation 0 December 31, 2010 07:42
return more than one value from a user function Benny CFX 0 July 18, 2008 10:00
User defined function - CO level hari FLUENT 0 October 24, 2007 00:11
Droplet Evaporation Christian Main CFD Forum 2 February 27, 2007 07:27
Help: user defined function alice FLUENT 3 December 13, 2000 01:10


All times are GMT -4. The time now is 17:18.