CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Fluent UDF and Scheme Programming (https://www.cfd-online.com/Forums/fluent-udf/)
-   -   How does FLUENT process an UDF!!!!???? (https://www.cfd-online.com/Forums/fluent-udf/149655-how-does-fluent-process-udf.html)

Bharadwaj B S March 6, 2015 00:11

How does FLUENT process an UDF!!!!????
 
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??

pakk March 6, 2015 03:43

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... :confused:

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.

Bharadwaj B S March 6, 2015 03:52

Quote:

Originally Posted by pakk (Post 534831)
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... :confused:

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

pakk March 6, 2015 04:11

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;
...
}


Bharadwaj B S March 6, 2015 05:41

Solution
 
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

pakk March 6, 2015 06:30

Quote:

Originally Posted by Bharadwaj B S (Post 534864)
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.

Bharadwaj B S March 6, 2015 06:43

Looping
 
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

pakk March 6, 2015 07:16

Quote:

Originally Posted by Bharadwaj B S (Post 534880)
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?

Bharadwaj B S March 6, 2015 07:38

Thanks
 
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 March 6, 2015 07:50

UDF for mass flow from surface
 
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

pakk March 6, 2015 08:08

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"?

Bharadwaj B S March 6, 2015 08:13

Thanks a ton
 
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

pakk March 6, 2015 08:25

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!

Bharadwaj B S March 6, 2015 08:32

Right.
 
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 March 6, 2015 09:07

Not working
 
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

pakk March 6, 2015 11:01

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.

Bharadwaj B S March 7, 2015 00:07

Decrement
 
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

pakk March 9, 2015 03:18

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.

Bharadwaj B S March 9, 2015 03:43

m >= 0
 
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 March 9, 2015 04:20

Current udf
 
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


All times are GMT -4. The time now is 22:53.