Writing a User Defined Function (UDF) for CFD Modeling : Second Edition
Hello Friends, its being a long time I haven't shared any new information on User Defined Functions (UDFs) available in ANSYS Fluent. Well that may make you think that UDFs are so tough to create, that this person took so long to write his second experience (blog) on UDF! Yeah, it’s funny, but let me tell that this is not the case with me, they (UDFs) are actually so interesting …and huge time & manual effort saver …and productive …and… I can go on and on if I start listing down all the plus points of using UDFs in Fluent. So, long story made short, I was fully enjoying past few months in learning and using this UDF & Scheme programming knowledge to Automate CFD studies in ANSYS Fluent.
Let me share my CFD Journey with you, after joining CCTech, I started working as a CFD Engineer, mentoring LearnCAx students on their CFD related Academic projects. It was a brilliant experience to communicate with wonderful engineers from various parts of the globe and help them inspire, educate and mentor throughout their project duration. I must say, most of the time I learnt more and more of CFD along with them. I then started applying this knowledge for carrying out industrial CFD Simulations. Somewhere down the road, I realized that ANSYS Fluent has much more capabilities than what is visible to our eyes on the GUI. I started developing interest in automating the complete CFD Process. And as the programming bug had bit me in my early days at college, I quickly started removing my inputs as a user of this tool and implementing pieces of code written in C program, scheme program, shell script, etc. and here I am today, developing commercial DataCenter CFD Simulation tool in which the user simply creates the DataCenter geometry and the whole CFD simulation is a complete automatic process.
According to you, what is it that supports this level of automation in ANSYS Fluent? Most of you are absolutely right, it’s the UDFs, and without them it wouldn’t have been possible. In the last edition, I had written about the “DEFINE_PROFILE” UDF Macro which was used to provide variable values of a particular Boundary Condition to a surface. Today, I am going to talk about another very widely used UDF Macro “DEFINE_ON_DEMAND”. DEFINE_ON_DEMAND as the name suggests it helps you to run a set of C code inside ANSYS Fluent at any point of your desire. It can be used before or after simulation, but not during simulation as it is called by user either manually or through journal/scheme files. Let us go step by step and understand this UDF’s use with an example.
The syntax is quite simple, it has to be defined using a name, which will be visible inside Fluent. You can write down your C code inside this udf, but remember that this UDF function doesn’t return anything.
Interpreting it in Fluent :
As we had already discussed last time, we again do the same, interpret the UDF by following methods –
GUI : Menu Bar -> Define -> User-Defined -> Functions -> Interpreted
TUI : Define > user-defined > interpreted-functions > “udf_name.c”
Now your UDF is ready for use. We can execute the UDF by the following methods –
GUI : Menu Bar -> Define -> User-Defined -> Execute on Demand…
TUI : Define > user-defined > execute-on-demand > “function_name”
That’s it! Isn’t that simple friends? So why not understand its use with an example. Let me think of a scenario where I can use it…….. Alrite, let me keep the physics and the simulation very simple, we will take up a “flow through pipe” example, with a velocity inlet and pressure-outlet boundary condition.
Water flows in at a high temperature, and the walls convect heat to the atmosphere. Hence what should be the temperature at the outlet? Now let me use the UDF to write down the area weighted average of temperature at the outlet into a text file. I know that this operation can be done directly from the GUI or TUI, but let us do it using the UDF. Basically, I don’t want to get into long and complex coding in this blog hence, let us keep it simple. To begin with, I have got the simulation converged using the normal procedure, i.e. from GUI.
Now, I will first summarize the steps to get this done :
- I will write the zone ID of the outlet boundary into a file
- Then I will read this ID from the file into the UDF’s C code
- Use the area weighted average function which I have created inside the UDF to get the average temperature for the outlet ID
- Then use C code to write the average temperature into a file
You may now be wondering, why I am first writing the zone ID of the outlet to a file, and then again reading it from within the UDF! There are other straight forward ways to get the zone ID into the UDF but that would make this blog more complex for the beginners if I start introducing other ANSYS Fluent’s inbuilt functions. Hence, let us stick to the above 4 steps.
Step 1 :
I will use the following scheme code to write the outlet zone ID
(let ((analysis-port #f))
(set! analysis-port (open-file "outlet_id.txt" "w"))
(format analysis-port "~a" (number->string (thread-id (get-thread "outlet"))))
You can download the above code (available at the start of this page - requires login) to write zone IDs of any surface into a file. Simply paste the above text into notepad, replace the word outlet with your desired surface name and save it with the extension .scm and read it in Fluent as shown below.
After doing this, I got a file created in the present directory with the name “outlet_id.txt”, it now contains just the ID of the outlet, which is 11 in my case.
Step 2, 3 & 4 :
Now let us look at the UDF code.
real avg_outlet_temp(int id)
real area =0.0, avg_outlet_temp =0.0;
d = Get_Domain(1);
t = Lookup_Thread(d,id);
c = F_C0(f,t);
ct = THREAD_T0(t);
area = area + NV_MAG(A);
avg_outlet_temp = avg_outlet_temp + (NV_MAG(A) * F_T(f,t));
avg_outlet_temp = avg_outlet_temp/area;
fp = fopen("outlet_id.txt","r");
fscanf(fp, "%d", &id);
fp = fopen("outlet_temp.txt","w");
outlet_temp = avg_outlet_temp(id);
fprintf(fp,"outlet_temp = %lf\n",outlet_temp);
As usual, the UDF begins by including the udf.h header file. The next set of code is actually a function to calculate the area weighted average value of temperature of the zone ID that has been passed on to this function by the DEFINE_ON_DEMAND UDF Macro which is written on the latter half of the UDF. The logic behind this UDF is that, once this UDF is executed inside Fluent, the DEFINE_ON_DEMAND function is called. Within this function, the outlet_id.txt file is read into memory and then the secondary function which I have written inside the UDF is called to get the average temperature of this zone ID, later this value is written into another file. You may use the above code as per your requirement by downloading the file containing this UDF code at the start of this blog (requires login).
I understand that if I start explaining each and every line of the above UDF then this blog might turn into a mixture of ANSYS Fluent UDF Guide & Let us C book. Just kidding, anyhow, I am open for all your queries regarding any content from the blog or any other content related to UDFs. You may put up your queries as comment to this blog, or else drop a mail on my email ID.
I am thinking to come up with something interesting next time, and I hope that it won’t turn out to be a one sided discussion. To be specific, it will be a small scale project extended over couple of weekends to automate a complete CFD simulation using ANSYS ICEM CFD and Fluent software. In this personal project, I am in need of few members to work with me. It will mostly include basic knowledge of shell scripting, ICEM CFD scripting, UDFs and scheme programming. If you are interested then kindly login/register with your LearnCAx account and use the comment box below to tell me about yourself and the reason why you want to team up with me on this project. Also don’t forget to mention how much you know about the above mentioned scripts and languages.