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

Creating UDF with changing velocity with time

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 9, 2017, 02:12
Unhappy Creating UDF with changing velocity with time
  #1
New Member
 
Join Date: Mar 2017
Posts: 6
Rep Power: 9
jabeztan is on a distinguished road
Hi all,

I was asked to write the UDF for a 2D room and it has to change velocity in terms of time. Like, when the time is 0 to 2 seconds, the velocity magnitude is horizontal. Then 2 to 4 seconds, the velocity changes magnitude and so on until the velocity is vertical. For example, the velocity is flowing into the inlet at 0 degree then changes with time to 90 degrees. After that, it flows down to 0 degree then repeat. It is like moving fan blowing into the inlet from 0 degree to 90 degrees.

Please help with this because I have no knowledge on writing UDF at all.

Thanks!
jabeztan is offline   Reply With Quote

Old   March 9, 2017, 04:21
Default
  #2
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
If you have no knowledge on UDFs at all, then start by looking in the Fluent help. See if you can find a UDF that does something similar to what you want, and think about what should be changed. Try this (and don't be afraid to make mistakes).

Then come back here with your first attempt, and we can help from there.
pakk is offline   Reply With Quote

Old   March 9, 2017, 04:53
Default
  #3
New Member
 
Join Date: Mar 2017
Posts: 6
Rep Power: 9
jabeztan is on a distinguished road
Quote:
Originally Posted by pakk View Post
If you have no knowledge on UDFs at all, then start by looking in the Fluent help. See if you can find a UDF that does something similar to what you want, and think about what should be changed. Try this (and don't be afraid to make mistakes).

Then come back here with your first attempt, and we can help from there.

Can you help me see whether this CFD is valid?
/************************************************** *********************
UDF for transient
************************************************** **********************/
#include "udf.h"
DEFINE_PROFILE(velocity_magnitude, t, i)
{
real velocity;
real the_current_time;
face_t f;
real x;
real y;

the_current_time = CURRENT_TIME;

if ((the_current_time>=0) && (the_current_time<2))
{
x=0.1;
y=0;
}
if ((the_current_time>=2) && (the_current_time<4))
{
x=0.1;
y=0.05;
}
if ((the_current_time>=4) && (the_current_time<6))
{
x=0.1;
y=0.1;
}
if ((the_current_time>=6) && (the_current_time<8))
{
x=0.15;
y=0.15;
}
if ((the_current_time>=8) && (the_current_time<10))
{
x=0.20;
y=0.20;
}
if ((the_current_time>=10) && (the_current_time<12))
{
x=0.25;
y=0.25;
}
if ((the_current_time>=12) && (the_current_time<14))
{
x=0.30;
y=0.30;
}
if ((the_current_time>=14) && (the_current_time<16))
{
x=0.35;
y=0.35;
}
if ((the_current_time>=16) && (the_current_time<18))
{
x=0.40;
y=0.40;
}
if ((the_current_time>=18) && (the_current_time<20))
{
x=0.45;
y=0.45;
}
if ((the_current_time>=20) && (the_current_time<22))
{
x=0.50;
y=0.50;
}
if ((the_current_time>=22) && (the_current_time<24))
{
x=0.55;
y=0.55;
}
if ((the_current_time>=24) && (the_current_time<26))
{
x=0.60;
y=0.60;
}
if ((the_current_time>=26) && (the_current_time<28))
{
x=0.65;
y=0.65;
}
if ((the_current_time>=28) && (the_current_time<30))
{
x=0.70;
y=0.70;
}
if ((the_current_time>=30) && (the_current_time<32))
{
x=0.75;
y=0.75;
}
if ((the_current_time>=32) && (the_current_time<34))
{
x=0.80;
y=0.80;
}
if ((the_current_time>=34) && (the_current_time<36))
{
x=0.85;
y=0.85;
}
if ((the_current_time>=36) && (the_current_time<38))
{
x=0.90;
y=0.90;
}
if ((the_current_time>=38) && (the_current_time<40))
{
x=0.95;
y=0.95;
}
if ((the_current_time>=40) && (the_current_time<42))
{
x=1.0;
y=1.0;
}
if ((the_current_time>=42) && (the_current_time<44))
{
x=1.05;
y=1.05;
}
if ((the_current_time>=44) && (the_current_time<46))
{
x=1.10;
y=1.10;
}
if ((the_current_time>=46) && (the_current_time<48))
{
x=1.15;
y=1.15;
}
if ((the_current_time>=48) && (the_current_time<50))
{
x=1.20;
y=1.20;
}
if ((the_current_time>=50) && (the_current_time<52))
{
x=1.25;
y=1.25;
}
if ((the_current_time>=52) && (the_current_time<54))
{
x=1.30;
y=1.30;
}
if ((the_current_time>=54) && (the_current_time<56))
{
x=1.35;
y=1.35;
}
if ((the_current_time>=56) && (the_current_time<58))
{
x=1.40;
y=1.40;
}
if ((the_current_time>=58) && (the_current_time<60))
{
x=1.45;
y=1.45;
}
if ((the_current_time>=60))
{
x=1.5;
y=1.5;
}


begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = velocity=sqrt(pow(x,2)+pow(y,2));
}
end_f_loop(f,t)
}
jabeztan is offline   Reply With Quote

Old   March 9, 2017, 05:04
Default
  #4
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
Quote:
Originally Posted by jabeztan View Post
Can you help me see whether this CFD is valid?
/************************************************** *********************
UDF for transient
************************************************** **********************/
#include "udf.h"
DEFINE_PROFILE(velocity_magnitude, t, i)
{
real velocity;
real the_current_time;
face_t f;
real x;
real y;

the_current_time = CURRENT_TIME;

if ((the_current_time>=0) && (the_current_time<2))
{
x=0.1;
y=0;
}
*** removed a lot ***
begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = velocity=sqrt(pow(x,2)+pow(y,2));
}
end_f_loop(f,t)
}
It looks almost like a valid program that does something similar to what you asked.

I see only one problem in this line:
Code:
F_PROFILE(f,t,i) = velocity=sqrt(pow(x,2)+pow(y,2));
You are obviously trying to give the velocity magnitude, but you should give the components.

The solution that is easiest to understand: make two udfs, one for the x-component and one for the y-component. They can look identical to your UDF, except for the name and the F_PROFILE-line:

Code:
DEFINE_PROFILE(velocity_x, t, i)
....
  F_PROFILE(f,t,i) = x;
...
...
DEFINE_PROFILE(velocity_y, t, i)
....
  F_PROFILE(f,t,i) = y;
...
}
There are many tricks you can do to produce shorter code for this, but if this is your first UDF, I would focus on getting it working in an easy way, so you can still understand what the program does.
pakk is offline   Reply With Quote

Old   March 9, 2017, 05:37
Default
  #5
New Member
 
Join Date: Mar 2017
Posts: 6
Rep Power: 9
jabeztan is on a distinguished road
Quote:
Originally Posted by pakk View Post
It looks almost like a valid program that does something similar to what you asked.

I see only one problem in this line:
Code:
F_PROFILE(f,t,i) = velocity=sqrt(pow(x,2)+pow(y,2));
You are obviously trying to give the velocity magnitude, but you should give the components.

The solution that is easiest to understand: make two udfs, one for the x-component and one for the y-component. They can look identical to your UDF, except for the name and the F_PROFILE-line:

Code:
DEFINE_PROFILE(velocity_x, t, i)
....
  F_PROFILE(f,t,i) = x;
...
...
DEFINE_PROFILE(velocity_y, t, i)
....
  F_PROFILE(f,t,i) = y;
...
}
There are many tricks you can do to produce shorter code for this, but if this is your first UDF, I would focus on getting it working in an easy way, so you can still understand what the program does.
Thanks for the advice!

Actually I was thinking that it can be travelling in constant velocity but the direction of flow needs to vary. So I might want to throw in an angle, like theta to simulate the change of direction of the flow. How can I go about it? or is there other functions to write on it?
jabeztan is offline   Reply With Quote

Old   March 9, 2017, 05:46
Default
  #6
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
You still have to give x and y components to Fluent. So if you want to use an angle, you have to calculate components by yourself.

But you can use the sine and cosine functions. So, if you have calculated/specified velocity magnitude vmag and angle theta (in radians!), you can use this kind of line:

Code:
F_PROFILE(f,t,i) = vmag * cos(theta);
If you use cos for the x-component and sin for the y-component, this corresponds to defining theta relative to the x-axis.
pakk is offline   Reply With Quote

Old   March 9, 2017, 10:27
Default
  #7
New Member
 
Join Date: Mar 2017
Posts: 6
Rep Power: 9
jabeztan is on a distinguished road
Quote:
Originally Posted by pakk View Post
You still have to give x and y components to Fluent. So if you want to use an angle, you have to calculate components by yourself.

But you can use the sine and cosine functions. So, if you have calculated/specified velocity magnitude vmag and angle theta (in radians!), you can use this kind of line:

Code:
F_PROFILE(f,t,i) = vmag * cos(theta);
If you use cos for the x-component and sin for the y-component, this corresponds to defining theta relative to the x-axis.

Hi! Does this seem correct?
/************************************************** *********************
UDF for transient
************************************************** **********************/
#include "udf.h"
DEFINE_PROFILE(velocity_x, t, i)
{
face_t f;
real x;
real the_current_time;
the_current_time = CURRENT_TIME;

if ((the_current_time>=0) && (the_current_time<10))
{
x=0.1;
}

begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = x;
}
end_f_loop(f,t)
}

DEFINE_PROFILE(velocity_y, t, i)
{
face_t f;
real y;
real the_current_time;
the_current_time = CURRENT_TIME;

if ((the_current_time>=0) && (the_current_time<2))
{
y=0;
}
if ((the_current_time>=2) && (the_current_time<4))
{
y=0.05;
}
if ((the_current_time>=4) && (the_current_time<6))
{
y=0.1;
}
if ((the_current_time>=6) && (the_current_time<8))
{
y=0.2;
}
if ((the_current_time>=8) && (the_current_time<10))
{
y=0.3;
}
begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = y;
}
end_f_loop(f,t)
}


DEFINE_PROFILE(velocity_magnitude, t, i)
{
real velocity;
real theta;
face_t f;
real x;
real y;

begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = velocity * cos(theta);
velocity=sqrt(pow(x,2)+pow(y,2));
theta=atan2(y,x);
}
end_f_loop(f,t)
}
jabeztan is offline   Reply With Quote

Old   March 9, 2017, 10:40
Default
  #8
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
The x-component and y-component seem ok.

The "velocity_magnitude" is wrong. First of all: you don't need it.
But even if you would need it:
* you calculate vmag and theta after you use them. That is the wrong order.
* you calculate vmag and theta using x and y, but x and y are not defined. (They are defined in a different function, but that does not count.)
* the velocity magnitude is sqrt(x^2+y^2), no need for cos(theta).

So, remove the velocity_magnitude part. And then just try it. The best way to know if your UDF is correct is by using it.
pakk is offline   Reply With Quote

Old   March 9, 2017, 10:46
Default
  #9
New Member
 
Join Date: Mar 2017
Posts: 6
Rep Power: 9
jabeztan is on a distinguished road
Quote:
Originally Posted by pakk View Post
The x-component and y-component seem ok.

The "velocity_magnitude" is wrong. First of all: you don't need it.
But even if you would need it:
* you calculate vmag and theta after you use them. That is the wrong order.
* you calculate vmag and theta using x and y, but x and y are not defined. (They are defined in a different function, but that does not count.)
* the velocity magnitude is sqrt(x^2+y^2), no need for cos(theta).

So, remove the velocity_magnitude part. And then just try it. The best way to know if your UDF is correct is by using it.
Thanks!
But how do i set the time step and number of time steps?
I keep getting reversed flow after running for awhile.
jabeztan is offline   Reply With Quote

Reply

Tags
#ansys, #cfd, #fluent, #timestep, #velocity


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
AMI speed performance danny123 OpenFOAM 21 October 24, 2020 05:13
Time dependent angular velocity calculation UDF shashankmechguy Fluent UDF and Scheme Programming 1 July 26, 2018 03:23
UDF for time dependent angular velocity calculation shashankmechguy Fluent UDF and Scheme Programming 2 October 24, 2016 05:43
Floating point exception error lpz_michele OpenFOAM Running, Solving & CFD 53 October 19, 2015 03:50
Moving mesh Niklas Wikstrom (Wikstrom) OpenFOAM Running, Solving & CFD 122 June 15, 2014 07:20


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