CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Fluent UDF and Scheme Programming

How does FLUENT process an UDF!!!!????

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

Reply
 
LinkBack Thread Tools Display Modes
Old   March 6, 2015, 01:11
Default How does FLUENT process an UDF!!!!????
  #1
Member
 
Baradwaj B S
Join Date: Jan 2015
Posts: 70
Rep Power: 2
Bharadwaj B S is on a distinguished road
I have an UDF for providing mass flow rate from a surface. In that I have one "for loop" with initial value, condition and an DECREMENT.

Contions,
m =1.0 (arbitary initial mass (1 kg)), this mass should reduce by 0.5 kg for next iteration and "m" should be greater than 0.0.

When all this conditions are true it should go inside the loop and do the calculations for mass flow rate, for which I have used F_PROFILE and some constant value is assigned to it.

NOW QUESTION:

Whenever I run this UDF, for all time steps it is taking the conditions to be TRUE and goes into the loop and assigns the mass flow rate value, even though the loop is WRITTEN such that it should end by two TIME STEPS (because decrement is 0.5 (1-0.5)).

Since this is taking the conditions always true, I get a doubt that FLUENT(compiler) is running the UDF every time fresh. i.e, once it completes reading the UDF, it goes back and when there is a need again, it is again reading the UDF, is this how FLUENT reads an UDF or am I wrong??
Bharadwaj B S is offline   Reply With Quote

Old   March 6, 2015, 04:43
Default
  #2
Senior Member
 
Join Date: Nov 2013
Posts: 677
Rep Power: 9
pakk is on a distinguished road
Quote:
I get a doubt that FLUENT(compiler) is running the UDF every time fresh. i.e, once it completes reading the UDF, it goes back and when there is a need again, it is again reading the UDF, is this how FLUENT reads an UDF or am I wrong??
When Fluents needs the UDF, it executes the code in the UDF. When Fluent needs the UDF a second time, it again executes the code in the UDF. If that is what you mean, then you are correct. I don't understand how you expect Fluent to function otherwise...

It is hard to guess without seeing your code, but I think your question actually is: Does Fluent remember the value of "m" that I give, or does it reset this value every time? If Fluent resets the value of "m" every time to one, then the condition is always true, and you get the behavior what you said.

But you don't want that. So make the variable global or static, depending on what you understand best.
pakk is offline   Reply With Quote

Old   March 6, 2015, 04:52
Default
  #3
Member
 
Baradwaj B S
Join Date: Jan 2015
Posts: 70
Rep Power: 2
Bharadwaj B S is on a distinguished road
Quote:
Originally Posted by pakk View Post
When Fluents needs the UDF, it executes the code in the UDF. When Fluent needs the UDF a second time, it again executes the code in the UDF. If that is what you mean, then you are correct. I don't understand how you expect Fluent to function otherwise...

It is hard to guess without seeing your code, but I think your question actually is: Does Fluent remember the value of "m" that I give, or does it reset this value every time? If Fluent resets the value of "m" every time to one, then the condition is always true, and you get the behavior what you said.

But you don't want that. So make the variable global or static, depending on what you understand best.
Hello pakk,

Thanks for your speedy reply.

Yes. I should have asked the question as you have mentioned. So if I just define the variable inside a function, it will be reset, right??

And how do I define it globally?? do you mean defining before the function??

Thanks in advance,

Bharadwaj B S
Bharadwaj B S is offline   Reply With Quote

Old   March 6, 2015, 05:11
Default
  #4
Senior Member
 
Join Date: Nov 2013
Posts: 677
Rep Power: 9
pakk is on a distinguished road
Yes.

Local: only your function can use m, and it is reset every time.
Code:
int f(){
real m = 1.0;
...
}
Global: all functions can use m.
Code:
real m = 1.0;
int f(){
...
}
Static: only your function can use m, it is initialized only the first time, and the value will be reused.
Code:
int f(){
static real m = 1.0;
...
}
pakk is offline   Reply With Quote

Old   March 6, 2015, 06:41
Default Solution
  #5
Member
 
Baradwaj B S
Join Date: Jan 2015
Posts: 70
Rep Power: 2
Bharadwaj B S is on a distinguished road
Dear pakk,

Thanks a lot for your valuable suggestion. I will try this in my UDF.

One more question,

If I use for loop, I will have to give the initial value as a condition in braces. So do you recommend me using "for loop" or will the "while loop" serve the purpose for me???

Thanks in advance,

Bharadwaj B S
Bharadwaj B S is offline   Reply With Quote

Old   March 6, 2015, 07:30
Default
  #6
Senior Member
 
Join Date: Nov 2013
Posts: 677
Rep Power: 9
pakk is on a distinguished road
Quote:
Originally Posted by Bharadwaj B S View Post
Dear pakk,

Thanks a lot for your valuable suggestion. I will try this in my UDF.

One more question,

If I use for loop, I will have to give the initial value as a condition in braces. So do you recommend me using "for loop" or will the "while loop" serve the purpose for me???

Thanks in advance,

Bharadwaj B S
Use a for-loop if you need a for-loop, and a while-loop is you need a while-loop. I don't know why you want to put a loop in your code, and what you are looping about, so I can not make my advice more specific.
Perhaps you don't even need a loop.
pakk is offline   Reply With Quote

Old   March 6, 2015, 07:43
Default Looping
  #7
Member
 
Baradwaj B S
Join Date: Jan 2015
Posts: 70
Rep Power: 2
Bharadwaj B S is on a distinguished road
Hi pakk,

I want that m to be reduced for every iteration by some value, say 0.05. So that whatever the flux calculations inside should stop when this "m" value is zero. That is at 20 iterations "m" will become zero.

For this to work, from what I know, I should use a "if else" or "for" or a "while loop", for decrementing that "m" value with the conditions that "m>=0 and m<=1".

Hope you got my point.

Thanks in advance,
Bharadwaj B S
Bharadwaj B S is offline   Reply With Quote

Old   March 6, 2015, 08:16
Default
  #8
Senior Member
 
Join Date: Nov 2013
Posts: 677
Rep Power: 9
pakk is on a distinguished road
Quote:
Originally Posted by Bharadwaj B S View Post
Hi pakk,

I want that m to be reduced for every iteration by some value, say 0.05. So that whatever the flux calculations inside should stop when this "m" value is zero. That is at 20 iterations "m" will become zero.

For this to work, from what I know, I should use a "if else" or "for" or a "while loop", for decrementing that "m" value with the conditions that "m>=0 and m<=1".

Hope you got my point.

Thanks in advance,
Bharadwaj B S
So, you want m to decrease by 0.05 every time that the udf is called. Why do you think you need a loop for that? The simplest solution would be
Code:
m=m-0.05;
No loop required. I don't understand what kind of loop you are suggesting. What would be your loop variable? Where would it start, where would it end?
pakk is offline   Reply With Quote

Old   March 6, 2015, 08:38
Default Thanks
  #9
Member
 
Baradwaj B S
Join Date: Jan 2015
Posts: 70
Rep Power: 2
Bharadwaj B S is on a distinguished road
Hello pakk,

Thanks for bearing, I am very much new. I will send you my UDF. Please go through and tell me if there is any mistake and suggest me changes. I think that will be the right thing to do. Hope it makes sense.

Thanks again,
Bharadwaj B S
Bharadwaj B S is offline   Reply With Quote

Old   March 6, 2015, 08:50
Default UDF for mass flow from surface
  #10
Member
 
Baradwaj B S
Join Date: Jan 2015
Posts: 70
Rep Power: 2
Bharadwaj B S is on a distinguished road
Here is the UDF which I wrote, this is what I could do from my understanding, please go through and suggest me for modifications.

#include "udf.h"

int ID = 13;
float source;
Domain *d;
face_t f;
Thread *t;
Thread c_thread;
cell_t c;
real flx;
float a;
float b;
float g;
float h;

DEFINE_PROFILE(flux, t, i)
{
Domain *d;

d = Get_Domain(1);
t = Lookup_Thread(d, ID);
flx = 0.0;
a = - 0.05;
b = 0.05;
g = 0.0;
h = 1.0;

begin_f_loop(f, t)
{
for(source = h; source >= g; source = source - b)
{
flx = - a;
F_PROFILE(f, t, i) = flx;
if(source == g)
break;
}
}
end_f_loop(f,t)
}


Thanks in advance,
Bharadwaj B S
Bharadwaj B S is offline   Reply With Quote

Old   March 6, 2015, 09:08
Default
  #11
Senior Member
 
Join Date: Nov 2013
Posts: 677
Rep Power: 9
pakk is on a distinguished road
I guess you want something like:
Code:
#include "udf.h"


DEFINE_PROFILE(flux, t, i)
{
 int ID = 13;
 static float source = 1.0;
 Domain *d;
 face_t f;
 Thread *t;
 float a = - 0.05;
 float b = 0.05;

 source = source - b;
 if (source>0) {
   d = Get_Domain(1);
   t = Lookup_Thread(d, ID);
   begin_f_loop(f, t)
   {
     F_PROFILE(f, t, i) = -a;
   }
   end_f_loop(f,t);
 }
}
And a tip to improve readability: don't use variable names "a" and "b", but use something that describes the function. So instead of "b" use something like "massdecrement". And why do you use the variable name "source"? I thought you were talking about a mass, so why don't you just call it "mass"?
pakk is offline   Reply With Quote

Old   March 6, 2015, 09:13
Default Thanks a ton
  #12
Member
 
Baradwaj B S
Join Date: Jan 2015
Posts: 70
Rep Power: 2
Bharadwaj B S is on a distinguished road
Hi pakk,

Thanks a lot for your time and suggestion. Definitely I am going to make changes and yes I started with that name source, I did not changed that, just kept it as it is. I was unaware of the fact of giving names instead of a,b,c. I thought it was simple, that's the whole thing behind that.

Regards,
Bharadwaj B S
Bharadwaj B S is offline   Reply With Quote

Old   March 6, 2015, 09:25
Default
  #13
Senior Member
 
Join Date: Nov 2013
Posts: 677
Rep Power: 9
pakk is on a distinguished road
Yes, now it seems simple, but I promise you that if you read it back in a year, you will have a hard time finding out what the meaning of "a" and "b" is!

Good luck!
pakk is offline   Reply With Quote

Old   March 6, 2015, 09:32
Default Right.
  #14
Member
 
Baradwaj B S
Join Date: Jan 2015
Posts: 70
Rep Power: 2
Bharadwaj B S is on a distinguished road
Yeah absolutely. That I was unaware of these things. I have given a run with the changes. I will let you know once it is complete.

Regards,
Bharadwaj B S
Bharadwaj B S is offline   Reply With Quote

Old   March 6, 2015, 10:07
Default Not working
  #15
Member
 
Baradwaj B S
Join Date: Jan 2015
Posts: 70
Rep Power: 2
Bharadwaj B S is on a distinguished road
Hi pakk,

I made the changes as you suggested and ran the case for a decrements of 0.5, so that I can complete the calculations fast. But after doing this, I was not able to obtain the result. i.e, according to this, there should not be any mass flow rate from the surface once the value of mass reduces to 0.

It is clear, for second iteration itself "m' becomes zero(decrement is 0.5) and the mass flow rate (F_PROFILE) calculation should not be read (or considered), after this, right?

Still it is giving out the flux from the surface (sphere in my case).

Anything you know to make it stop once "m" becomes zero(0)???.

Thanks in advance,
Bharadwaj B S
Bharadwaj B S is offline   Reply With Quote

Old   March 6, 2015, 12:01
Default
  #16
Senior Member
 
Join Date: Nov 2013
Posts: 677
Rep Power: 9
pakk is on a distinguished road
You should be more exact.

You never specified that the flux should become zero when m is below zero. That is why it is not in the code. And that is why the code does not do that.

You never specified what should happen when m is below zero. So nothing happens.

If you want something to happen when m is below zero, you should add it in the code. Fluent can not guess what you want.
pakk is offline   Reply With Quote

Old   March 7, 2015, 01:07
Default Decrement
  #17
Member
 
Baradwaj B S
Join Date: Jan 2015
Posts: 70
Rep Power: 2
Bharadwaj B S is on a distinguished road
Hello pakk,

Pardon for not mentioning it earlier. I thought just by decreasing the "m' by some decrements, I would get to stop the mass flow rate.

In the earlier UDF, where I had used "for loop". I ran that in TURBO C, I was getting the decrements. So, that decremented value I thought of using it for condition.

Thanks in advance,

Bharadwaj B S
Bharadwaj B S is offline   Reply With Quote

Old   March 9, 2015, 04:18
Default
  #18
Senior Member
 
Join Date: Nov 2013
Posts: 677
Rep Power: 9
pakk is on a distinguished road
I had the impression that I had already given you enough hints...

Currently, you program does the following:
If the mass is larger than zero, make the massflux -a.

Now you want to add the following:

If the mass is smaller or equal to zero, make the massflux 0.

Try to understand the code, then you should see what to add to the code to make it work.
pakk is offline   Reply With Quote

Old   March 9, 2015, 04:43
Default m >= 0
  #19
Member
 
Baradwaj B S
Join Date: Jan 2015
Posts: 70
Rep Power: 2
Bharadwaj B S is on a distinguished road
Hello pakk,

Thanks for your reply. I tried the same if(m>=0), if(m < 1 && m>0) and if(m>0), all three conditions are either taking the condition true or false. None of them are stopping at any instance.
Bharadwaj B S is offline   Reply With Quote

Old   March 9, 2015, 05:20
Default Current udf
  #20
Member
 
Baradwaj B S
Join Date: Jan 2015
Posts: 70
Rep Power: 2
Bharadwaj B S is on a distinguished road
Hi pakk,

This is the one which I tried,


#include "udf.h"

Thread *t;
float mass = 10;

DEFINE_PROFILE(flux, t, i)
{
Domain *d;
int ID = 13;
face_t f;
float flwrt = -0.5;
float decrement = 2;
d = Get_Domain(1);
t = Lookup_Thread(d, ID);

mass = mass - decrement;
if(mass <= 0)
{
begin_f_loop(f, t)
F_PROFILE (f, t, i) = 0.0;
end_f_loop(f,t)
}
else
{
begin_f_loop(f, t)
F_PROFILE(f, t, i) = -flwrt;
end_f_loop(f,t)
}
}

Please go through and suggest me if there is any mistakes.

Thanks in advance,
Bharadwaj B S
Bharadwaj B S 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
solving a conduction problem in FLUENT using UDF Avin2407 Fluent UDF and Scheme Programming 1 March 13, 2015 03:02
looking for a smart interface matlab fluent chary FLUENT 8 July 2, 2014 07:14
Consulting for some issues of FLUENT UDF and UDS wjl163 Fluent UDF and Scheme Programming 2 November 12, 2012 04:24
fluent UDF external library lapack problem Rick FLUENT 0 May 7, 2008 10:16
UDF of Zimont model in fluent Z Main CFD Forum 0 February 17, 2005 04:07


All times are GMT -4. The time now is 03:46.