You are on page 1of 8

Web : www.learncax.

com
Email : info@learncax.com

Writing a User Defined Function (UDF) for CFD Modeling : Second Edition

Subhransu Majhi1
LearnCAx, CAx education division
Centre for Computational Technologies Pvt. Ltd., Pune, India

Note: Videos used in this blog (if any) are not included in this document. Please visit the
original blog to view the videos.

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
Data Center CFD Simulation tool in which the user simply creates the Data Center
geometry and the whole CFD simulation is a complete automatic process.

1
Subhransu Majhi (subhranshu@cctech.co.in), LearnCAx, Centre for Computational Technologies Pvt. Ltd.

1 Akshay Residency, 50 Anand Park, Aundh, Pune – 411007, India © Copyrights : www.LearnCAx.com
Web : www.learncax.com
Email : info@learncax.com

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.

Syntax :

DEFINE_ON_DEMAND(UDF_name)
{
Expressions;
}

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 :

1 Akshay Residency, 50 Anand Park, Aundh, Pune – 411007, India © Copyrights : www.LearnCAx.com
Web : www.learncax.com
Email : info@learncax.com

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…

1 Akshay Residency, 50 Anand Park, Aundh, Pune – 411007, India © Copyrights : www.LearnCAx.com
Web : www.learncax.com
Email : info@learncax.com

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.

1 Akshay Residency, 50 Anand Park, Aundh, Pune – 411007, India © Copyrights : www.LearnCAx.com
Web : www.learncax.com
Email : info@learncax.com

Now, I will first summarize the steps to get this done :

1. I will write the zone ID of the outlet boundary into a file


2. Then I will read this ID from the file into the UDF’s C code
3. Use the area weighted average function which I have created inside the UDF to get the
average temperature for the outlet ID
4. 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.

1 Akshay Residency, 50 Anand Park, Aundh, Pune – 411007, India © Copyrights : www.LearnCAx.com
Web : www.learncax.com
Email : info@learncax.com

Step 2, 3 & 4 :

Now let us look at the UDF code.

#include "udf.h"
real avg_outlet_temp(int id)
{
Thread *t;
Thread *ct;
Domain *d;
face_t f;
cell_t c;

real area =0.0, avg_outlet_temp =0.0;


real A[ND_ND];

d = Get_Domain(1);
t = Lookup_Thread(d,id);

begin_f_loop(f,t)
{
c = F_C0(f,t);
ct = THREAD_T0(t);

F_AREA(A,f,t);
area = area + NV_MAG(A);
avg_outlet_temp = avg_outlet_temp + (NV_MAG(A) * F_T(f,t));
}
end_f_loop(f,t)

avg_outlet_temp = avg_outlet_temp/area;
return avg_outlet_temp;
}

DEFINE_ON_DEMAND(write_data)
{
FILE *fp;
int id;
real outlet_temp;

fp = fopen("outlet_id.txt","r");
fscanf(fp, "%d", &id);
fclose(fp);

fp = fopen("outlet_temp.txt","w");
outlet_temp = avg_outlet_temp(id);
fprintf(fp,"outlet_temp = %lf\n",outlet_temp);
fclose(fp);
}

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

1 Akshay Residency, 50 Anand Park, Aundh, Pune – 411007, India © Copyrights : www.LearnCAx.com
Web : www.learncax.com
Email : info@learncax.com

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, 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.

1 Akshay Residency, 50 Anand Park, Aundh, Pune – 411007, India © Copyrights : www.LearnCAx.com
Web : www.learncax.com
Email : info@learncax.com

LearnCAx knowledge database

Courses offered by LearnCAx are designed to meet all


your needs. It has range of FREE and PREMIUM courses
which is designed to meet the industry requirements. All
the courses are self-contained with video lectures, quiz,
assignments and projects. Every course comes with FAQs
and discussion forums where you can get answers to all
your questions. Each course contains live assistance from
LearnCAx faculty where faculties will guide you through
online sessions and desktop sharing.

Blogs is the place where our coaches share their


knowledge through articles. This includes best practices,
advance techniques, and recent development in
respective field. LearnCAx is backed with strong
industrial consultancy team. This team does projects for
industries. As LearnCAx main focus is “from academics
to industry”, blogs gives us an opportunity to share
details about our industrial work. It’s not only about
what is done, but also about how the project is done.
The objective is to give student’s more knowledge about
industrial project so that they feel connected to the
industry.

No matter what is the form of learning, an interaction


with experts is an inevitable part of every learning
process. LearnCAx faculty conducts webinars to share
the knowledge with you. Let it be knowhow of software,
introduction to a particular topics or discussing
fundamentals of a subject, all webinars are targeted
towards sharing the knowledge and getting feedback
about what your training needs are? Webinar is also a
place for our consultancy team to share their work with
you. All these live sessions would give an opportunity to
you to talk to the experts in the domain.

Create FREE LearnCAx account to access all the knowledge base

1 Akshay Residency, 50 Anand Park, Aundh, Pune – 411007, India © Copyrights : www.LearnCAx.com

You might also like